KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > plugin > BroadcastPlugin


1 /**
2  * $RCSfile: BroadcastPlugin.java,v $
3  * $Revision: 1.13 $
4  * $Date: 2005/04/13 17:36:12 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.plugin;
13
14 import org.jivesoftware.messenger.container.Plugin;
15 import org.jivesoftware.messenger.container.PluginManager;
16 import org.jivesoftware.messenger.*;
17 import org.jivesoftware.util.PropertyEventListener;
18 import org.jivesoftware.util.PropertyEventDispatcher;
19 import org.jivesoftware.messenger.auth.UnauthorizedException;
20 import org.jivesoftware.messenger.group.GroupManager;
21 import org.jivesoftware.messenger.group.Group;
22 import org.jivesoftware.messenger.group.GroupNotFoundException;
23 import org.jivesoftware.util.Log;
24 import org.jivesoftware.util.JiveGlobals;
25 import org.xmpp.packet.Message;
26 import org.xmpp.packet.Packet;
27 import org.xmpp.packet.PacketError;
28 import org.xmpp.packet.JID;
29 import org.xmpp.component.Component;
30 import org.xmpp.component.ComponentManager;
31 import org.xmpp.component.ComponentManagerFactory;
32
33 import java.io.File JavaDoc;
34 import java.util.*;
35
36 /**
37  * Broadcast service plugin. It accepts messages and broadcasts them out to
38  * groups of connected users. The address <tt>all@[serviceName].[server]</tt> is
39  * reserved for sending a broadcast message to all connected users. Otherwise,
40  * broadcast messages can be sent to groups of users by using addresses
41  * in the form <tt>[group]@[serviceName].[server]</tt>.
42  *
43  * @author Matt Tucker
44  */

45 public class BroadcastPlugin implements Plugin, Component, PropertyEventListener {
46
47     private String JavaDoc serviceName;
48     private SessionManager sessionManager;
49     private GroupManager groupManager;
50     private List<JID> allowedUsers;
51     private boolean groupMembersAllowed;
52     private boolean disableGroupPermissions;
53     private ComponentManager componentManager;
54     private PluginManager pluginManager;
55
56     /**
57      * Constructs a new broadcast plugin.
58      */

59     public BroadcastPlugin() {
60         serviceName = JiveGlobals.getProperty("plugin.broadcast.serviceName", "broadcast");
61         disableGroupPermissions = JiveGlobals.getBooleanProperty(
62                 "plugin.broadcast.disableGroupPermissions");
63         groupMembersAllowed = JiveGlobals.getBooleanProperty(
64                 "plugin.broadcast.groupMembersAllowed", true);
65         allowedUsers = stringToList(JiveGlobals.getProperty("plugin.broadcast.allowedUsers", ""));
66     }
67
68     // Plugin Interface
69

70     public void initializePlugin(PluginManager manager, File JavaDoc pluginDirectory) {
71         pluginManager = manager;
72         sessionManager = SessionManager.getInstance();
73         groupManager = GroupManager.getInstance();
74
75         // Register as a component.
76
componentManager = ComponentManagerFactory.getComponentManager();
77         try {
78             componentManager.addComponent(serviceName, this);
79         }
80         catch (Exception JavaDoc e) {
81             componentManager.getLog().error(e);
82         }
83         PropertyEventDispatcher.addListener(this);
84     }
85
86     public void destroyPlugin() {
87         PropertyEventDispatcher.removeListener(this);
88         // Unregister component.
89
try {
90             componentManager.removeComponent(serviceName);
91         }
92         catch (Exception JavaDoc e) {
93             componentManager.getLog().error(e);
94         }
95         componentManager = null;
96         pluginManager = null;
97         sessionManager = null;
98         groupManager = null;
99         allowedUsers.clear();
100     }
101
102     public void initialize(JID jid, ComponentManager componentManager) {
103     }
104
105     public void start() {
106     }
107
108     public void shutdown() {
109     }
110
111     // Component Interface
112

113     public String JavaDoc getName() {
114         // Get the name from the plugin.xml file.
115
return pluginManager.getName(this);
116     }
117
118     public String JavaDoc getDescription() {
119         // Get the description from the plugin.xml file.
120
return pluginManager.getDescription(this);
121     }
122
123     public void processPacket(Packet packet) {
124         // Only respond to incoming messages. TODO: handle disco, presence, etc.
125
if (packet instanceof Message) {
126             Message message = (Message)packet;
127             String JavaDoc toNode = message.getTo().getNode();
128             String JavaDoc fromNode = message.getFrom().getNode();
129             // Check to see if trying to broadcast to all connected users.
130
if ("all".equals(toNode)) {
131                 if (allowedUsers.size() > 0) {
132                     // See if the user is allowed to send the message.
133
JID address = new JID(message.getFrom().toBareJID());
134                     if (!allowedUsers.contains(address)) {
135                         Message error = new Message();
136                         if (message.getID() != null) {
137                             error.setID(message.getID());
138                         }
139                         error.setError(PacketError.Condition.not_allowed);
140                         error.setTo(message.getFrom());
141                         error.setSubject("Error sending broadcast message");
142                         error.setBody("Not allowed to send a broadcast message to " +
143                                 message.getTo());
144                         try {
145                             componentManager.sendPacket(this, error);
146                         }
147                         catch (Exception JavaDoc e) {
148                             componentManager.getLog().error(e);
149                         }
150                         return;
151                     }
152                 }
153                 try {
154                     sessionManager.broadcast(message);
155                 }
156                 catch (UnauthorizedException ue) {
157                     Log.error(ue);
158                 }
159             }
160             // See if the name is a group.
161
else {
162                 try {
163                     Group group = groupManager.getGroup(toNode);
164                     if (disableGroupPermissions || (groupMembersAllowed && group.isUser(fromNode)) ||
165                             group.getAdmins().contains(fromNode) ||
166                             allowedUsers.contains(message.getFrom().toBareJID()))
167                     {
168                         for (String JavaDoc user : group.getMembers()) {
169                             Message newMessage = message.createCopy();
170                             JID userJID = XMPPServer.getInstance().createJID(user, null);
171                             newMessage.setTo(userJID);
172                             try {
173                                 componentManager.sendPacket(this, newMessage);
174                             }
175                             catch (Exception JavaDoc e) {
176                                 componentManager.getLog().error(e);
177                             }
178                         }
179                     }
180                     else {
181                         // Otherwise, the address is recognized so send an error message back.
182
Message error = new Message();
183                         if (message.getID() != null) {
184                             error.setID(message.getID());
185                         }
186                         error.setTo(message.getFrom());
187                         error.setError(PacketError.Condition.not_allowed);
188                         error.setSubject("Error sending broadcast message");
189                         error.setBody("Not allowed to send a broadcast message to " +
190                                 message.getTo());
191                         try {
192                             componentManager.sendPacket(this, error);
193                         }
194                         catch (Exception JavaDoc e) {
195                             componentManager.getLog().error(e);
196                         }
197                     }
198                 }
199                 catch (GroupNotFoundException gnfe) {
200                     // Otherwise, the address is recognized so send an error message back.
201
Message error = new Message();
202                     if (message.getID() != null) {
203                         error.setID(message.getID());
204                     }
205                     error.setTo(message.getFrom());
206                     error.setError(PacketError.Condition.not_allowed);
207                     error.setSubject("Error sending broadcast message");
208                     error.setBody("Address not valid: " +
209                             message.getTo());
210                     try {
211                         componentManager.sendPacket(this, error);
212                     }
213                     catch (Exception JavaDoc e) {
214                         componentManager.getLog().error(e);
215                     }
216                 }
217             }
218         }
219     }
220
221     // Other Methods
222

223     /**
224      * Returns the service name of this component, which is "broadcast" by default.
225      *
226      * @return the service name of this component.
227      */

228     public String JavaDoc getServiceName() {
229         return serviceName;
230     }
231
232     /**
233      * Sets the service name of this component, which is "broadcast" by default.
234      *
235      * @param serviceName the service name of this component.
236      */

237     public void setServiceName(String JavaDoc serviceName) {
238         JiveGlobals.setProperty("plugin.broadcast.serviceName", serviceName);
239     }
240
241     /**
242      * Returns a collection of the addresses of users allowed to send broadcast
243      * messages. If no users are defined, anyone can send broadcast messages to
244      * all users. Additional users may also be allowed to send broadcast messages
245      * to specific groups depending on the group settings.
246      *
247      * @return the users allowed to send broadcast messages.
248      */

249     public Collection<JID> getGlobalAllowedUsers() {
250         return allowedUsers;
251     }
252
253     /**
254      * Sets the collection of addresses of users allowed to send broadcast
255      * messages. If the collection is empty, anyone can send broadcast messages.
256      * Additional users may also be allowed to send broadcast messages to
257      * specific groups depending on the group settings.
258      *
259      * @param allowedUsers collection of users allowed to send broadcast messages
260      * to all users.
261      */

262     public void setGlobalAllowedUsers(Collection<String JavaDoc> allowedUsers) {
263         StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
264         for (String JavaDoc jid : allowedUsers) {
265             buf.append(jid).append(",");
266         }
267         JiveGlobals.setProperty("plugin.broadcast.allowedUsers", buf.toString());
268     }
269
270     /**
271      * Returns true if all permission checking on sending messages to groups is disabled
272      * (enabled by default). When disabled, any user in the system can send a message to
273      * a group.
274      *
275      * @return true if group permission checking is disabled.
276      */

277     public boolean isGroupPermissionsDisabled() {
278         return disableGroupPermissions;
279     }
280
281     /**
282      * Enables or disables permission checking when sending messages to a group. When
283      * disabled, any user in the system can send a message to a group.
284      *
285      * @param disableGroupPermissions true if group permission checking should be disabled.
286      */

287     public void setGroupPermissionsDisabled(boolean disableGroupPermissions) {
288         this.disableGroupPermissions = disableGroupPermissions;
289         JiveGlobals.setProperty("plugin.broadcast.disableGroupPermissions",
290                 Boolean.toString(disableGroupPermissions));
291     }
292
293     /**
294      * Returns true if normal group members are allowed to send broadcast messages
295      * to groups they belong to. Otherwise, only group administrators can send
296      * broadcast messages to groups. Global allowed users can also send messages to
297      * groups.
298      *
299      * @return true if group members are allowed to broadcast messages; otherwise only
300      * group admins are allowed.
301      */

302     public boolean isGroupMembersAllowed() {
303         return groupMembersAllowed;
304     }
305
306     /**
307      * Sets whether normal group members are allowed to send broadcast messages
308      * to groups they belong to. Otherwise, only group administrators can send
309      * broadcast messages to groups. Global allowed users can also send messages to
310      * groups.
311      *
312      * @param allowed true if group members are allowed to broadcast messages; otherwise only
313      * group admins are allowed.
314      */

315     public void setGroupMembersAllowed(boolean allowed) {
316         this.groupMembersAllowed = allowed;
317         JiveGlobals.setProperty("plugin.broadcast.groupMembersAllowed", Boolean.toString(allowed));
318     }
319
320     // PropertyEventListener Methods
321

322     public void propertySet(String JavaDoc property, Map params) {
323         if (property.equals("plugin.broadcast.groupMembersAllowed")) {
324             this.groupMembersAllowed = Boolean.parseBoolean((String JavaDoc)params.get("value"));
325         }
326         else if (property.equals("plugin.broadcast.disableGroupPermissions")) {
327             this.disableGroupPermissions = Boolean.parseBoolean((String JavaDoc)params.get("value"));
328         }
329         else if (property.equals("plugin.broadcast.allowedUsers")) {
330             this.allowedUsers = stringToList((String JavaDoc)params.get("value"));
331         }
332         else if (property.equals("plugin.broadcast.serviceName")) {
333             changeServiceName((String JavaDoc)params.get("value"));
334         }
335     }
336
337     public void propertyDeleted(String JavaDoc property, Map params) {
338         if (property.equals("plugin.broadcast.groupMembersAllowed")) {
339             this.groupMembersAllowed = true;
340         }
341         else if (property.equals("plugin.broadcast.disableGroupPermissions")) {
342             this.disableGroupPermissions = false;
343         }
344         else if (property.equals("plugin.broadcast.allowedUsers")) {
345             this.allowedUsers = Collections.emptyList();
346         }
347         else if (property.equals("plugin.broadcast.serviceName")) {
348             changeServiceName("broadcast");
349         }
350     }
351
352     public void xmlPropertySet(String JavaDoc property, Map params) {
353         // Ignore.
354
}
355
356     public void xmlPropertyDeleted(String JavaDoc property, Map params) {
357         // Ignore.
358
}
359
360     /**
361      * Changes the service name to a new value.
362      *
363      * @param serviceName the service name.
364      */

365     private void changeServiceName(String JavaDoc serviceName) {
366          if (serviceName == null) {
367             throw new NullPointerException JavaDoc("Service name cannot be null");
368         }
369         if (this.serviceName.equals(serviceName)) {
370             return;
371         }
372
373         // Re-register the service.
374
try {
375             componentManager.removeComponent(this.serviceName);
376         }
377         catch (Exception JavaDoc e) {
378             componentManager.getLog().error(e);
379         }
380         try {
381             componentManager.addComponent(serviceName, this);
382         }
383         catch (Exception JavaDoc e) {
384             componentManager.getLog().error(e);
385         }
386         this.serviceName = serviceName;
387     }
388
389     /**
390      * Returns a comma-delimitted list of strings into a Collection of Strings.
391      *
392      * @param str the String.
393      * @return a list.
394      */

395     private List<JID> stringToList(String JavaDoc str) {
396         List<JID> values = new ArrayList<JID>();
397         StringTokenizer tokens = new StringTokenizer(str, ",");
398         while (tokens.hasMoreTokens()) {
399             String JavaDoc value = tokens.nextToken().trim();
400             if (!value.equals("")) {
401                 // See if this is a full JID or just a username.
402
if (value.contains("@")) {
403                     values.add(new JID(value));
404                 }
405                 else {
406                     values.add(XMPPServer.getInstance().createJID(value, null));
407                 }
408             }
409         }
410         return values;
411     }
412 }
Popular Tags