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.user.UserManager; 17 import org.jivesoftware.messenger.user.UserNotFoundException; 18 import org.jivesoftware.messenger.user.User; 19 import org.jivesoftware.messenger.PresenceManager; 20 import org.jivesoftware.messenger.XMPPServer; 21 import org.jivesoftware.util.JiveGlobals; 22 import org.xmpp.packet.Presence; 23 import org.xmpp.packet.JID; 24 25 import java.io.File ; 26 27 42 public class PresencePlugin implements Plugin { 43 44 private UserManager userManager; 45 private PresenceManager presenceManager; 46 private String hostname; 47 48 public void initializePlugin(PluginManager manager, File pluginDirectory) { 49 XMPPServer server = XMPPServer.getInstance(); 50 userManager = server.getUserManager(); 51 presenceManager = server.getPresenceManager(); 52 hostname = server.getServerInfo().getName(); 53 } 54 55 public void destroyPlugin() { 56 userManager = null; 57 presenceManager = null; 58 } 59 60 67 public boolean isPresencePublic() { 68 return JiveGlobals.getBooleanProperty("plugin.presence.public", false); 69 } 70 71 78 public void setPresencePublic(boolean presencesPublic) { 79 JiveGlobals.setProperty("plugin.presence.public", presencesPublic ? "true" : "false"); 80 } 81 82 94 public Presence getPresence(String sender, String jid) throws UserNotFoundException { 95 JID targetJID = new JID(jid); 96 if (targetJID.getDomain() == null || XMPPServer.getInstance().isRemote(targetJID)) { 98 throw new UserNotFoundException("Domain does not matches local server domain"); 99 } 100 if (!hostname.equals(targetJID.getDomain())) { 101 throw new UserNotFoundException("Presence of components not supported yet!"); 104 } 105 if (targetJID.getNode() == null) { 106 throw new UserNotFoundException("Username is null"); 108 } 109 if (!isPresencePublic()) { 110 if (sender == null) { 111 throw new UserNotFoundException("Sender is null"); 112 } 113 else if (!presenceManager.canProbePresence(new JID(sender), targetJID.getNode())) { 114 throw new UserNotFoundException("Sender is not allowed to probe this user"); 115 } 116 } 117 User user = userManager.getUser(targetJID.getNode()); 118 return presenceManager.getPresence(user); 119 } 120 } 121 | Popular Tags |