public void process(Presence packet) { // Ignore unavailable presences if (Presence.Type.unavailable == packet.getType()) { return; } // Examine the packet and check if it has caps info, // if not -- do nothing by returning. Element capsElement = packet.getChildElement("c", "http://jabber.org/protocol/caps"); if (capsElement == null) { return; } // Examine the packet and check if it's in legacy format (pre version 1.4 // of XEP-0115). If so, do nothing by returning. // TODO: if this packet is in legacy format, we SHOULD check the 'node', // 'ver', and 'ext' combinations as specified in the archived version // 1.3 of the specification, and cache the results. See JM-1447 final String hashAttribute = capsElement.attributeValue("hash"); if (hashAttribute == null || hashAttribute.trim().length() == 0) { return; } // Examine the packet and check if it has and a 'ver' hash // if not -- do nothing by returning. final String newVerAttribute = capsElement.attributeValue("ver"); if (newVerAttribute == null || newVerAttribute.trim().length() == 0) { return; } // Check to see if the 'ver' hash is already in our cache. if (isInCapsCache(newVerAttribute)) { // The 'ver' hash is in the cache already, so let's update the // entityCapabilitiesUserMap for the user that sent the caps // packet. entityCapabilitiesUserMap.put(packet.getFrom(), newVerAttribute); } else { // The 'ver' hash is not in the cache so send out a disco#info query // so that we may begin recognizing this 'ver' hash. IQ iq = new IQ(IQ.Type.get); iq.setTo(packet.getFrom()); String serverName = XmppServer.getInstance().getServerInfo().getXMPPDomain(); iq.setFrom(serverName); iq.setChildElement("query", "http://jabber.org/protocol/disco#info"); String packetId = iq.getID(); final EntityCapabilities caps = new EntityCapabilities(); caps.setHashAttribute(hashAttribute); caps.setVerAttribute(newVerAttribute); verAttributes.put(packetId, caps); final IQRouter iqRouter = XmppServer.getInstance().getIQRouter(); iqRouter.addIQResultListener(packetId, this); iqRouter.route(iq); } }
private void process(Presence presence) { if (presence.getType() == Presence.Type.unavailable) { MUCRole mucRole = room.getOccupantByFullJID(realjid); if (mucRole != null) { room.leaveRoom(mucRole); } } else { throw new UnsupportedOperationException( "Cannot process Presence packets of remote users: " + presence); } }
public void processPacket(Packet packet) { // Check that we are getting an answer to a presence probe if (packet instanceof Presence) { Presence presence = (Presence) packet; if (presence.isAvailable() || presence.getType() == Presence.Type.unavailable || presence.getType() == Presence.Type.error) { // Store answer of presence probes probedPresence.put(presence.getFrom().toString(), presence); } } }
private void handle(Presence packet) { try { // 取得信息包类型 Presence.Type type = packet.getType(); // Presence updates (null == 'available') if (type == null || Presence.Type.unavailable == type) { presenceUpdateHandler.process(packet); } else { log.warn("Unknown presence type"); } } catch (Exception e) { log.error("Could not route packet", e); // 出现异常关闭会话 Session session = sessionManager.getSession(packet.getFrom()); if (session != null) { session.close(); } } }
public void process(Presence presence) throws PacketException { try { JID senderJID = presence.getFrom(); JID recipientJID = presence.getTo(); Presence.Type type = presence.getType(); // Reject presence subscription requests sent to the local server itself. if (recipientJID == null || recipientJID.toString().equals(serverName)) { if (type == Presence.Type.subscribe) { Presence reply = new Presence(); reply.setTo(senderJID); reply.setFrom(recipientJID); reply.setType(Presence.Type.unsubscribed); deliverer.deliver(reply); } return; } try { Roster senderRoster = getRoster(senderJID); if (senderRoster != null) { manageSub(recipientJID, true, type, senderRoster); } Roster recipientRoster = getRoster(recipientJID); boolean recipientSubChanged = false; if (recipientRoster != null) { recipientSubChanged = manageSub(senderJID, false, type, recipientRoster); } // Do not forward the packet to the recipient if the presence is of type subscribed // and the recipient user has not changed its subscription state. if (!(type == Presence.Type.subscribed && recipientRoster != null && !recipientSubChanged)) { // If the user is already subscribed to the *local* user's presence then do not // forward the subscription request. Also, do not send an auto-reply on behalf // of the user. This presence stanza is the user's server know that it MUST no // longer send notification of the subscription state change to the user. // See http://tools.ietf.org/html/rfc3921#section-7 and/or OF-38 if (type == Presence.Type.subscribe && recipientRoster != null && !recipientSubChanged) { try { RosterItem.SubType subType = recipientRoster.getRosterItem(senderJID).getSubStatus(); if (subType == RosterItem.SUB_FROM || subType == RosterItem.SUB_BOTH) { return; } } catch (UserNotFoundException e) { // Weird case: Roster item does not exist. Should never happen Log.error( "User does not exist while trying to update roster item. " + "This should never happen (this indicates a programming " + "logic error). Processing stanza: " + presence.toString(), e); } } // Try to obtain a handler for the packet based on the routes. If the handler is // a module, the module will be able to handle the packet. If the handler is a // Session the packet will be routed to the client. If a route cannot be found // then the packet will be delivered based on its recipient and sender. List<JID> jids = routingTable.getRoutes(recipientJID, null); if (!jids.isEmpty()) { for (JID jid : jids) { Presence presenteToSend = presence.createCopy(); // Stamp the presence with the user's bare JID as the 'from' address, // as required by section 8.2.5 of RFC 3921 presenteToSend.setFrom(senderJID.toBareJID()); routingTable.routePacket(jid, presenteToSend, false); } } else { deliverer.deliver(presence.createCopy()); } if (type == Presence.Type.subscribed) { // Send the presence of the local user to the remote user. The remote user // subscribed to the presence of the local user and the local user accepted JID prober = localServer.isLocal(recipientJID) ? new JID(recipientJID.toBareJID()) : recipientJID; presenceManager.probePresence(prober, senderJID); PresenceEventDispatcher.subscribedToPresence(recipientJID, senderJID); } } if (type == Presence.Type.unsubscribed) { // Send unavailable presence from all of the local user's available resources // to the remote user presenceManager.sendUnavailableFromSessions(recipientJID, senderJID); PresenceEventDispatcher.unsubscribedToPresence(senderJID, recipientJID); } } catch (SharedGroupException e) { Presence result = presence.createCopy(); JID sender = result.getFrom(); result.setFrom(presence.getTo()); result.setTo(sender); result.setError(PacketError.Condition.not_acceptable); deliverer.deliver(result); } } catch (Exception e) { Log.error(LocaleUtils.getLocalizedString("admin.error"), e); } }