Commit ff5a11bd authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Added disco support.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3519 b35dd754-fafc-0310-a699-88a17e54d16e
parent 3c02e455
...@@ -43,6 +43,13 @@ ...@@ -43,6 +43,13 @@
<h1> <h1>
Broadcast Plugin Changelog Broadcast Plugin Changelog
</h1> </h1>
<p><b>1.5.0</b> -- March 06, 2006</p>
<ul>
<li>Added service discovery support.</li>
<li>Improved presence management.</li>
<li>Fixed NPE when sending packet to the service itself.</li>
</ul>
<p><b>1.4.0</b> -- March 02, 2006</p> <p><b>1.4.0</b> -- March 02, 2006</p>
<ul> <ul>
<li>Broadcast service can now be added as a roster contact.</li> <li>Broadcast service can now be added as a roster contact.</li>
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
<name>Broadcast</name> <name>Broadcast</name>
<description>Broadcasts messages to users.</description> <description>Broadcasts messages to users.</description>
<author>Jive Software</author> <author>Jive Software</author>
<version>1.4.0</version> <version>1.5.0</version>
<date>3/2/2006</date> <date>3/6/2006</date>
<url>http://www.jivesoftware.org</url> <url>http://www.jivesoftware.org</url>
<minServerVersion>2.4.0</minServerVersion> <minServerVersion>2.4.0</minServerVersion>
</plugin> </plugin>
\ No newline at end of file
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
package org.jivesoftware.wildfire.plugin; package org.jivesoftware.wildfire.plugin;
import org.dom4j.Element;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.util.PropertyEventDispatcher; import org.jivesoftware.util.PropertyEventDispatcher;
...@@ -124,7 +125,8 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -124,7 +125,8 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
Group group = null; Group group = null;
String toNode = packet.getTo().getNode(); String toNode = packet.getTo().getNode();
// Check if user is allowed to send packet to this service[+group] // Check if user is allowed to send packet to this service[+group]
if ("all".equals(toNode)) { boolean targetAll = "all".equals(toNode);
if (targetAll) {
// See if the user is allowed to send the packet. // See if the user is allowed to send the packet.
JID address = new JID(packet.getFrom().toBareJID()); JID address = new JID(packet.getFrom().toBareJID());
if (allowedUsers.isEmpty() || allowedUsers.contains(address)) { if (allowedUsers.isEmpty() || allowedUsers.contains(address)) {
...@@ -133,6 +135,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -133,6 +135,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
} }
else { else {
try { try {
if (toNode != null) {
group = groupManager.getGroup(toNode); group = groupManager.getGroup(toNode);
boolean isGroupUser = group.isUser(packet.getFrom()) || boolean isGroupUser = group.isUser(packet.getFrom()) ||
group.isUser(new JID(packet.getFrom().toBareJID())); group.isUser(new JID(packet.getFrom().toBareJID()));
...@@ -141,14 +144,35 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -141,14 +144,35 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
canProceed = true; canProceed = true;
} }
} }
}
catch (GroupNotFoundException e) { catch (GroupNotFoundException e) {
} }
} }
// Only respond to incoming messages. TODO: handle disco, presence, etc.
if (packet instanceof Message) { if (packet instanceof Message) {
// Respond to incoming messages
Message message = (Message)packet; Message message = (Message)packet;
processMessage(message, targetAll, group, canProceed);
}
else if (packet instanceof Presence) {
// Respond to presence subscription request or presence probe
Presence presence = (Presence) packet;
processPresence(canProceed, presence);
}
else if (packet instanceof IQ) {
// Handle disco packets
IQ iq = (IQ) packet;
// Ignore IQs of type ERROR or RESULT
if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) {
return;
}
processIQ(iq, targetAll, group, canProceed);
}
}
private void processMessage(Message message, boolean targetAll, Group group,
boolean canProceed) {
// Check to see if trying to broadcast to all connected users. // Check to see if trying to broadcast to all connected users.
if ("all".equals(toNode)) { if (targetAll) {
if (!canProceed) { if (!canProceed) {
Message error = new Message(); Message error = new Message();
if (message.getID() != null) { if (message.getID() != null) {
...@@ -228,33 +252,17 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -228,33 +252,17 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
} }
} }
} }
else if (packet instanceof Presence) {
Presence presence = (Presence) packet;
try {
if (!canProceed) {
// Send forbidden error since user is not allowed
Presence reply = new Presence();
reply.setID(presence.getID());
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
reply.setError(PacketError.Condition.forbidden);
componentManager.sendPacket(this, reply);
return;
}
private void processPresence(boolean canProceed, Presence presence) {
try {
if (Presence.Type.subscribe == presence.getType()) { if (Presence.Type.subscribe == presence.getType()) {
// Accept all presence requests // Accept all presence requests if user has permissions
// Reply that the subscription request was approved // Reply that the subscription request was approved or rejected
Presence reply = new Presence(); Presence reply = new Presence();
reply.setTo(presence.getFrom()); reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo()); reply.setFrom(presence.getTo());
reply.setType(Presence.Type.subscribed); reply.setType(canProceed ? Presence.Type.subscribed : Presence.Type.unsubscribed);
componentManager.sendPacket(this, reply); componentManager.sendPacket(this, reply);
// Send that the service is available
/*reply = new Presence();
reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo());
componentManager.sendPacket(this, reply);*/
} }
else if (Presence.Type.unsubscribe == presence.getType()) { else if (Presence.Type.unsubscribe == presence.getType()) {
// Send confirmation of unsubscription // Send confirmation of unsubscription
...@@ -263,6 +271,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -263,6 +271,7 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
reply.setFrom(presence.getTo()); reply.setFrom(presence.getTo());
reply.setType(Presence.Type.unsubscribed); reply.setType(Presence.Type.unsubscribed);
componentManager.sendPacket(this, reply); componentManager.sendPacket(this, reply);
if (!canProceed) {
// Send unavailable presence of the service // Send unavailable presence of the service
reply = new Presence(); reply = new Presence();
reply.setTo(presence.getFrom()); reply.setTo(presence.getFrom());
...@@ -270,11 +279,16 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -270,11 +279,16 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
reply.setType(Presence.Type.unavailable); reply.setType(Presence.Type.unavailable);
componentManager.sendPacket(this, reply); componentManager.sendPacket(this, reply);
} }
}
else if (Presence.Type.probe == presence.getType()) { else if (Presence.Type.probe == presence.getType()) {
// Send that the service is available // Send that the service is available
Presence reply = new Presence(); Presence reply = new Presence();
reply.setTo(presence.getFrom()); reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo()); reply.setFrom(presence.getTo());
if (!canProceed) {
// Send forbidden error since user is not allowed
reply.setError(PacketError.Condition.forbidden);
}
componentManager.sendPacket(this, reply); componentManager.sendPacket(this, reply);
} }
} }
...@@ -282,6 +296,86 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener ...@@ -282,6 +296,86 @@ public class BroadcastPlugin implements Plugin, Component, PropertyEventListener
componentManager.getLog().error(e); componentManager.getLog().error(e);
} }
} }
private void processIQ(IQ iq, boolean targetAll, Group group,
boolean canProceed) {
IQ reply = IQ.createResultIQ(iq);
Element childElement = iq.getChildElement();
String namespace = childElement.getNamespaceURI();
Element childElementCopy = iq.getChildElement().createCopy();
reply.setChildElement(childElementCopy);
if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
if (iq.getTo().getNode() == null) {
// Return service identity and features
Element identity = childElementCopy.addElement("identity");
identity.addAttribute("category", "component");
identity.addAttribute("type", "generic");
identity.addAttribute("name", "Broadcast service");
childElementCopy.addElement("feature")
.addAttribute("var", "http://jabber.org/protocol/disco#info");
childElementCopy.addElement("feature")
.addAttribute("var", "http://jabber.org/protocol/disco#items");
}
else {
if (targetAll) {
// Return identity and features of the "all" group
Element identity = childElementCopy.addElement("identity");
identity.addAttribute("category", "component");
identity.addAttribute("type", "generic");
identity.addAttribute("name", "Broadcast all connected users");
childElementCopy.addElement("feature")
.addAttribute("var", "http://jabber.org/protocol/disco#info");
}
else if (group != null && canProceed) {
// Return identity and features of the "all" group
Element identity = childElementCopy.addElement("identity");
identity.addAttribute("category", "component");
identity.addAttribute("type", "generic");
identity.addAttribute("name", "Broadcast " + group.getName());
childElementCopy.addElement("feature")
.addAttribute("var", "http://jabber.org/protocol/disco#info");
}
else {
// Group not found or not allowed to use that group so
// answer item_not_found error
reply.setError(PacketError.Condition.item_not_found);
}
}
}
else if ("http://jabber.org/protocol/disco#items".equals(namespace)) {
if (iq.getTo().getNode() == null) {
// Return the list of groups hosted by the service that can be used by the user
Collection<Group> groups = null;
JID address = new JID(iq.getFrom().toBareJID());
if (allowedUsers.contains(address)) {
groups = groupManager.getGroups();
}
else {
groups = groupManager.getGroups(iq.getFrom());
}
for (Group userGroup : groups) {
try {
JID groupJID = new JID(userGroup.getName() + "@" + serviceName + "." +
componentManager.getServerName());
childElementCopy.addElement("item")
.addAttribute("jid", groupJID.toString());
}
catch (Exception e) {
// Group name is not valid to be used as a JID
}
}
}
}
else {
// Answer an error since the server can't handle the requested namespace
reply.setError(PacketError.Condition.service_unavailable);
}
try {
componentManager.sendPacket(this, reply);
}
catch (Exception e) {
componentManager.getLog().error(e);
}
} }
// Other Methods // Other Methods
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment