1 package org.jivesoftware.messenger.handler; 2 3 import org.dom4j.Element; 4 import org.jivesoftware.messenger.IQHandlerInfo; 5 import org.jivesoftware.messenger.PresenceManager; 6 import org.jivesoftware.messenger.XMPPServer; 7 import org.jivesoftware.messenger.auth.UnauthorizedException; 8 import org.jivesoftware.messenger.disco.ServerFeaturesProvider; 9 import org.jivesoftware.messenger.roster.RosterItem; 10 import org.jivesoftware.messenger.user.User; 11 import org.jivesoftware.messenger.user.UserManager; 12 import org.jivesoftware.messenger.user.UserNotFoundException; 13 import org.xmpp.packet.IQ; 14 import org.xmpp.packet.PacketError; 15 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 19 27 public class IQLastActivityHandler extends IQHandler implements ServerFeaturesProvider { 28 29 private IQHandlerInfo info; 30 private PresenceManager presenceManager; 31 32 public IQLastActivityHandler() { 33 super("XMPP Last Activity Handler"); 34 info = new IQHandlerInfo("query", "jabber:iq:last"); 35 } 36 37 public IQ handleIQ(IQ packet) throws UnauthorizedException { 38 IQ reply = IQ.createResultIQ(packet); 39 Element lastActivity = reply.setChildElement("query", "jabber:iq:last"); 40 String sender = packet.getFrom().getNode(); 41 String username = packet.getTo().getNode(); 42 43 if (sender == null || username == null) { 45 reply.setError(PacketError.Condition.forbidden); 46 return reply; 47 } 48 49 try { 50 User user = UserManager.getInstance().getUser(username); 51 RosterItem item = user.getRoster().getRosterItem(packet.getFrom()); 52 if (item.getSubStatus() == RosterItem.SUB_FROM || 54 item.getSubStatus() == RosterItem.SUB_BOTH) { 55 if (sessionManager.getSessions(username).isEmpty()) { 56 long lastActivityTime = presenceManager.getLastActivity(user); 59 lastActivity.addAttribute("seconds", String.valueOf(lastActivityTime)); 60 String lastStatus = presenceManager.getLastPresenceStatus(user); 61 if (lastStatus != null && lastStatus.length() > 0) { 62 lastActivity.setText(lastStatus); 63 } 64 } 65 else { 66 lastActivity.addAttribute("seconds", "0"); 68 } 69 } 70 else { 71 reply.setError(PacketError.Condition.forbidden); 72 } 73 } 74 catch (UserNotFoundException e) { 75 reply.setError(PacketError.Condition.forbidden); 76 } 77 return reply; 78 } 79 80 public IQHandlerInfo getInfo() { 81 return info; 82 } 83 84 public Iterator getFeatures() { 85 ArrayList features = new ArrayList (); 86 features.add("jabber:iq:last"); 87 return features.iterator(); 88 } 89 90 public void initialize(XMPPServer server) { 91 super.initialize(server); 92 presenceManager = server.getPresenceManager(); 93 } 94 } | Popular Tags |