1 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 ; 34 import java.util.*; 35 36 45 public class BroadcastPlugin implements Plugin, Component, PropertyEventListener { 46 47 private String 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 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 70 public void initializePlugin(PluginManager manager, File pluginDirectory) { 71 pluginManager = manager; 72 sessionManager = SessionManager.getInstance(); 73 groupManager = GroupManager.getInstance(); 74 75 componentManager = ComponentManagerFactory.getComponentManager(); 77 try { 78 componentManager.addComponent(serviceName, this); 79 } 80 catch (Exception e) { 81 componentManager.getLog().error(e); 82 } 83 PropertyEventDispatcher.addListener(this); 84 } 85 86 public void destroyPlugin() { 87 PropertyEventDispatcher.removeListener(this); 88 try { 90 componentManager.removeComponent(serviceName); 91 } 92 catch (Exception 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 113 public String getName() { 114 return pluginManager.getName(this); 116 } 117 118 public String getDescription() { 119 return pluginManager.getDescription(this); 121 } 122 123 public void processPacket(Packet packet) { 124 if (packet instanceof Message) { 126 Message message = (Message)packet; 127 String toNode = message.getTo().getNode(); 128 String fromNode = message.getFrom().getNode(); 129 if ("all".equals(toNode)) { 131 if (allowedUsers.size() > 0) { 132 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 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 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 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 e) { 176 componentManager.getLog().error(e); 177 } 178 } 179 } 180 else { 181 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 e) { 195 componentManager.getLog().error(e); 196 } 197 } 198 } 199 catch (GroupNotFoundException gnfe) { 200 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 e) { 214 componentManager.getLog().error(e); 215 } 216 } 217 } 218 } 219 } 220 221 223 228 public String getServiceName() { 229 return serviceName; 230 } 231 232 237 public void setServiceName(String serviceName) { 238 JiveGlobals.setProperty("plugin.broadcast.serviceName", serviceName); 239 } 240 241 249 public Collection<JID> getGlobalAllowedUsers() { 250 return allowedUsers; 251 } 252 253 262 public void setGlobalAllowedUsers(Collection<String > allowedUsers) { 263 StringBuilder buf = new StringBuilder (); 264 for (String jid : allowedUsers) { 265 buf.append(jid).append(","); 266 } 267 JiveGlobals.setProperty("plugin.broadcast.allowedUsers", buf.toString()); 268 } 269 270 277 public boolean isGroupPermissionsDisabled() { 278 return disableGroupPermissions; 279 } 280 281 287 public void setGroupPermissionsDisabled(boolean disableGroupPermissions) { 288 this.disableGroupPermissions = disableGroupPermissions; 289 JiveGlobals.setProperty("plugin.broadcast.disableGroupPermissions", 290 Boolean.toString(disableGroupPermissions)); 291 } 292 293 302 public boolean isGroupMembersAllowed() { 303 return groupMembersAllowed; 304 } 305 306 315 public void setGroupMembersAllowed(boolean allowed) { 316 this.groupMembersAllowed = allowed; 317 JiveGlobals.setProperty("plugin.broadcast.groupMembersAllowed", Boolean.toString(allowed)); 318 } 319 320 322 public void propertySet(String property, Map params) { 323 if (property.equals("plugin.broadcast.groupMembersAllowed")) { 324 this.groupMembersAllowed = Boolean.parseBoolean((String )params.get("value")); 325 } 326 else if (property.equals("plugin.broadcast.disableGroupPermissions")) { 327 this.disableGroupPermissions = Boolean.parseBoolean((String )params.get("value")); 328 } 329 else if (property.equals("plugin.broadcast.allowedUsers")) { 330 this.allowedUsers = stringToList((String )params.get("value")); 331 } 332 else if (property.equals("plugin.broadcast.serviceName")) { 333 changeServiceName((String )params.get("value")); 334 } 335 } 336 337 public void propertyDeleted(String 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 property, Map params) { 353 } 355 356 public void xmlPropertyDeleted(String property, Map params) { 357 } 359 360 365 private void changeServiceName(String serviceName) { 366 if (serviceName == null) { 367 throw new NullPointerException ("Service name cannot be null"); 368 } 369 if (this.serviceName.equals(serviceName)) { 370 return; 371 } 372 373 try { 375 componentManager.removeComponent(this.serviceName); 376 } 377 catch (Exception e) { 378 componentManager.getLog().error(e); 379 } 380 try { 381 componentManager.addComponent(serviceName, this); 382 } 383 catch (Exception e) { 384 componentManager.getLog().error(e); 385 } 386 this.serviceName = serviceName; 387 } 388 389 395 private List<JID> stringToList(String str) { 396 List<JID> values = new ArrayList<JID>(); 397 StringTokenizer tokens = new StringTokenizer(str, ","); 398 while (tokens.hasMoreTokens()) { 399 String value = tokens.nextToken().trim(); 400 if (!value.equals("")) { 401 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 |