1 11 12 package org.jivesoftware.messenger.handler; 13 14 import org.jivesoftware.messenger.*; 15 import org.jivesoftware.messenger.auth.UnauthorizedException; 16 import org.jivesoftware.messenger.disco.ServerFeaturesProvider; 17 import org.jivesoftware.messenger.roster.Roster; 18 import org.jivesoftware.messenger.roster.RosterItem; 19 import org.jivesoftware.messenger.user.User; 20 import org.jivesoftware.messenger.user.UserAlreadyExistsException; 21 import org.jivesoftware.messenger.user.UserManager; 22 import org.jivesoftware.messenger.user.UserNotFoundException; 23 import org.jivesoftware.util.LocaleUtils; 24 import org.jivesoftware.util.Log; 25 import org.xmpp.packet.IQ; 26 import org.xmpp.packet.JID; 27 import org.xmpp.packet.Packet; 28 import org.xmpp.packet.PacketError; 29 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 33 64 public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider { 65 66 private IQHandlerInfo info; 67 68 private UserManager userManager; 69 private XMPPServer localServer; 70 private SessionManager sessionManager; 71 private PacketRouter router; 72 73 public IQRosterHandler() { 74 super("XMPP Roster Handler"); 75 info = new IQHandlerInfo("query", "jabber:iq:roster"); 76 } 77 78 94 public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException { 95 try { 96 IQ returnPacket = null; 97 org.xmpp.packet.Roster roster = (org.xmpp.packet.Roster)packet; 98 99 JID recipientJID = packet.getTo(); 100 101 if (recipientJID == null || recipientJID.getNode() == null) { 103 returnPacket = manageRoster(roster); 104 } 105 else { 107 removeRosterItem(roster); 108 } 109 return returnPacket; 110 } 111 catch (SharedGroupException e) { 112 IQ result = IQ.createResultIQ(packet); 113 result.setChildElement(packet.getChildElement().createCopy()); 114 result.setError(PacketError.Condition.not_acceptable); 115 return result; 116 } 117 catch (Exception e) { 118 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 119 } 120 return null; 121 } 122 123 131 private void removeRosterItem(org.xmpp.packet.Roster packet) throws UnauthorizedException, 132 SharedGroupException { 133 JID recipientJID = packet.getTo(); 134 JID senderJID = packet.getFrom(); 135 try { 136 for (org.xmpp.packet.Roster.Item packetItem : packet.getItems()) { 137 if (packetItem.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) { 138 Roster roster = userManager.getUser(recipientJID.getNode()).getRoster(); 139 RosterItem item = roster.getRosterItem(senderJID); 140 roster.deleteRosterItem(senderJID, true); 141 item.setSubStatus(RosterItem.SUB_REMOVE); 142 item.setSubStatus(RosterItem.SUB_NONE); 143 144 Packet itemPacket = packet.createCopy(); 145 sessionManager.userBroadcast(recipientJID.getNode(), itemPacket); 146 } 147 } 148 } 149 catch (UserNotFoundException e) { 150 throw new UnauthorizedException(e); 151 } 152 } 153 154 163 private IQ manageRoster(org.xmpp.packet.Roster packet) throws UnauthorizedException, 164 UserAlreadyExistsException, SharedGroupException { 165 166 IQ returnPacket = null; 167 ClientSession session = sessionManager.getSession(packet.getFrom()); 168 169 IQ.Type type = packet.getType(); 170 171 try { 172 User sessionUser = userManager.getUser(session.getUsername()); 173 Roster cachedRoster = sessionUser.getRoster(); 174 if (IQ.Type.get == type) { 175 returnPacket = cachedRoster.getReset(); 176 returnPacket.setType(IQ.Type.result); 177 returnPacket.setTo(session.getAddress()); 178 returnPacket.setID(packet.getID()); 179 deliverer.deliver(returnPacket); 182 returnPacket = null; 183 } 184 else if (IQ.Type.set == type) { 185 186 for (org.xmpp.packet.Roster.Item item : packet.getItems()) { 187 if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) { 188 removeItem(cachedRoster, packet.getFrom(), item); 189 } 190 else { 191 if (cachedRoster.isRosterItem(item.getJID())) { 192 RosterItem cachedItem = cachedRoster.getRosterItem(item.getJID()); 194 cachedItem.setAsCopyOf(item); 195 cachedRoster.updateRosterItem(cachedItem); 196 } 197 else { 198 cachedRoster.createRosterItem(item); 200 } 201 } 202 } 203 returnPacket = IQ.createResultIQ(packet); 204 } 205 } 206 catch (UserNotFoundException e) { 207 throw new UnauthorizedException(e); 208 } 209 210 return returnPacket; 211 212 } 213 214 222 private void removeItem(org.jivesoftware.messenger.roster.Roster roster, JID sender, 223 org.xmpp.packet.Roster.Item item) throws SharedGroupException { 224 JID recipient = item.getJID(); 225 roster.deleteRosterItem(item.getJID(), true); 227 if (localServer.isLocal(recipient)) { try { 230 Roster recipientRoster = userManager.getUser(recipient.getNode()).getRoster(); 231 recipientRoster.deleteRosterItem(sender, true); 232 } 233 catch (UserNotFoundException e) { 234 } 235 } 236 else { 237 String serverDomain = localServer.getServerInfo().getName(); 239 if (!recipient.getDomain().contains(serverDomain)) { 241 } 243 else { 244 Packet removePacket = createRemoveForward(sender, recipient); 245 router.route(removePacket); 246 } 247 } 248 } 249 250 257 private Packet createRemoveForward(JID from, JID to) { 258 org.xmpp.packet.Roster response = new org.xmpp.packet.Roster(IQ.Type.set); 259 response.setFrom(from); 260 response.setTo(to); 261 response.addItem(from, org.xmpp.packet.Roster.Subscription.remove); 262 263 return response; 264 } 265 266 public void initialize(XMPPServer server) { 267 super.initialize(server); 268 localServer = server; 269 userManager = server.getUserManager(); 270 router = server.getPacketRouter(); 271 sessionManager = server.getSessionManager(); 272 } 273 274 public IQHandlerInfo getInfo() { 275 return info; 276 } 277 278 public Iterator getFeatures() { 279 ArrayList features = new ArrayList (); 280 features.add("jabber:iq:roster"); 281 return features.iterator(); 282 } 283 } | Popular Tags |