Search in sources :

Example 51 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class AgentRoster method getPresence.

/**
 * Returns the presence info for a particular agent, or <code>null</code> if the agent
 * is unavailable (offline) or if no presence information is available.<p>
 *
 * @param user a fully qualified xmpp JID. The address could be in any valid format (e.g.
 *             "domain/resource", "user@domain" or "user@domain/resource").
 * @return the agent's current presence, or <code>null</code> if the agent is unavailable
 *         or if no presence information is available..
 */
public Presence getPresence(Jid user) {
    Jid key = getPresenceMapKey(user);
    Map<Resourcepart, Presence> userPresences = presenceMap.get(key);
    if (userPresences == null) {
        Presence presence = StanzaBuilder.buildPresence().ofType(Presence.Type.unavailable).from(user).build();
        return presence;
    } else {
        // Find the resource with the highest priority
        // Might be changed to use the resource with the highest availability instead.
        Iterator<Resourcepart> it = userPresences.keySet().iterator();
        Presence p;
        Presence presence = null;
        while (it.hasNext()) {
            p = userPresences.get(it.next());
            if (presence == null) {
                presence = p;
            } else {
                if (p.getPriority() > presence.getPriority()) {
                    presence = p;
                }
            }
        }
        if (presence == null) {
            presence = synthesizeUnvailablePresence(user);
            return presence;
        } else {
            return presence;
        }
    }
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 52 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class AgentSession method handlePacket.

// PacketListener Implementation.
private void handlePacket(Stanza packet) {
    if (packet instanceof Presence) {
        Presence presence = (Presence) packet;
        // The workgroup can send us a number of different presence packets. We
        // check for different packet extensions to see what type of presence
        // packet it is.
        Resourcepart queueName = presence.getFrom().getResourceOrNull();
        WorkgroupQueue queue = queues.get(queueName);
        // If there isn't already an entry for the queue, create a new one.
        if (queue == null) {
            queue = new WorkgroupQueue(queueName);
            queues.put(queueName, queue);
        }
        // QueueOverview packet extensions contain basic information about a queue.
        QueueOverview queueOverview = (QueueOverview) presence.getExtensionElement(QueueOverview.ELEMENT_NAME, QueueOverview.NAMESPACE);
        if (queueOverview != null) {
            if (queueOverview.getStatus() == null) {
                queue.setStatus(WorkgroupQueue.Status.CLOSED);
            } else {
                queue.setStatus(queueOverview.getStatus());
            }
            queue.setAverageWaitTime(queueOverview.getAverageWaitTime());
            queue.setOldestEntry(queueOverview.getOldestEntry());
            // Fire event.
            fireQueueUsersEvent(queue, queueOverview.getStatus(), queueOverview.getAverageWaitTime(), queueOverview.getOldestEntry(), null);
            return;
        }
        // QueueDetails packet extensions contain information about the users in
        // a queue.
        QueueDetails queueDetails = (QueueDetails) packet.getExtensionElement(QueueDetails.ELEMENT_NAME, QueueDetails.NAMESPACE);
        if (queueDetails != null) {
            queue.setUsers(queueDetails.getUsers());
            // Fire event.
            fireQueueUsersEvent(queue, null, -1, null, queueDetails.getUsers());
            return;
        }
        // Notify agent packets gives an overview of agent activity in a queue.
        StandardExtensionElement notifyAgents = (StandardExtensionElement) presence.getExtensionElement("notify-agents", "http://jabber.org/protocol/workgroup");
        if (notifyAgents != null) {
            int currentChats = Integer.parseInt(notifyAgents.getFirstElement("current-chats", "http://jabber.org/protocol/workgroup").getText());
            int maxChats = Integer.parseInt(notifyAgents.getFirstElement("max-chats", "http://jabber.org/protocol/workgroup").getText());
            queue.setCurrentChats(currentChats);
            queue.setMaxChats(maxChats);
            // TODO: might need another event for current chats and max chats of queue
            return;
        }
    } else if (packet instanceof Message) {
        Message message = (Message) packet;
        // Check if a room invitation was sent and if the sender is the workgroup
        MUCUser mucUser = MUCUser.from(message);
        MUCUser.Invite invite = mucUser != null ? mucUser.getInvite() : null;
        if (invite != null && workgroupJID.equals(invite.getFrom())) {
            String sessionID = null;
            Map<String, List<String>> metaData = null;
            SessionID sessionIDExt = (SessionID) message.getExtensionElement(SessionID.ELEMENT_NAME, SessionID.NAMESPACE);
            if (sessionIDExt != null) {
                sessionID = sessionIDExt.getSessionID();
            }
            MetaData metaDataExt = (MetaData) message.getExtensionElement(MetaData.ELEMENT_NAME, MetaData.NAMESPACE);
            if (metaDataExt != null) {
                metaData = metaDataExt.getMetaData();
            }
            this.fireInvitationEvent(message.getFrom(), sessionID, message.getBody(), message.getFrom(), metaData);
        }
    }
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) QueueOverview(org.jivesoftware.smackx.workgroup.packet.QueueOverview) Resourcepart(org.jxmpp.jid.parts.Resourcepart) QueueDetails(org.jivesoftware.smackx.workgroup.packet.QueueDetails) MetaData(org.jivesoftware.smackx.workgroup.MetaData) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) Presence(org.jivesoftware.smack.packet.Presence) Map(java.util.Map) HashMap(java.util.HashMap) SessionID(org.jivesoftware.smackx.workgroup.packet.SessionID)

Example 53 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class Roster method getPresences.

/**
 * Returns a List of Presence objects for all of a user's current presences
 * or an unavailable presence if the user is unavailable (offline) or if no presence
 * information is available, such as when you are not subscribed to the user's presence
 * updates.
 *
 * @param jid an XMPP ID, e.g. jdoe@example.com.
 * @return a List of Presence objects for all the user's current presences,
 *         or an unavailable presence if the user is offline or if no presence information
 *         is available.
 */
public List<Presence> getPresences(BareJid jid) {
    List<Presence> res;
    Map<Resourcepart, Presence> userPresences = getPresencesInternal(jid);
    if (userPresences == null) {
        Presence presence = synthesizeUnvailablePresence(jid);
        res = Arrays.asList(presence);
    } else {
        List<Presence> answer = new ArrayList<>();
        // Used in case no available presence is found
        Presence unavailable = null;
        for (Presence presence : userPresences.values()) {
            if (presence.isAvailable()) {
                answer.add(presence);
            } else {
                unavailable = presence;
            }
        }
        if (!answer.isEmpty()) {
            res = answer;
        } else if (unavailable != null) {
            res = Arrays.asList(unavailable);
        } else {
            Presence presence = synthesizeUnvailablePresence(jid);
            res = Arrays.asList(presence);
        }
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 54 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class MultiUserChat method changeRole.

private void changeRole(Collection<Resourcepart> nicknames, MUCRole role) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    MUCAdmin iq = new MUCAdmin();
    iq.setTo(room);
    iq.setType(IQ.Type.set);
    for (Resourcepart nickname : nicknames) {
        // Set the new role.
        MUCItem item = new MUCItem(role, nickname);
        iq.addItem(item);
    }
    connection.sendIqRequestAndWaitForResponse(iq);
}
Also used : MUCItem(org.jivesoftware.smackx.muc.packet.MUCItem) MUCAdmin(org.jivesoftware.smackx.muc.packet.MUCAdmin) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Aggregations

Resourcepart (org.jxmpp.jid.parts.Resourcepart)54 EntityBareJid (org.jxmpp.jid.EntityBareJid)24 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)20 XMPPException (org.jivesoftware.smack.XMPPException)19 TestNotPossibleException (org.igniterealtime.smack.inttest.TestNotPossibleException)17 SmackException (org.jivesoftware.smack.SmackException)16 Presence (org.jivesoftware.smack.packet.Presence)16 ResultSyncPoint (org.igniterealtime.smack.inttest.util.ResultSyncPoint)15 EntityFullJid (org.jxmpp.jid.EntityFullJid)13 UserJid (com.xabber.android.data.entity.UserJid)11 Jid (org.jxmpp.jid.Jid)9 AccountJid (com.xabber.android.data.entity.AccountJid)8 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)8 DomainBareJid (org.jxmpp.jid.DomainBareJid)7 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)6 AccountItem (com.xabber.android.data.account.AccountItem)4 Localpart (org.jxmpp.jid.parts.Localpart)4 Attachment (com.xabber.android.data.database.messagerealm.Attachment)3 ForwardId (com.xabber.android.data.database.messagerealm.ForwardId)3 ArrayList (java.util.ArrayList)3