Java Code Examples for org.jivesoftware.smackx.muc.packet.MUCUser#from()

The following examples show how to use org.jivesoftware.smackx.muc.packet.MUCUser#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiUserChat.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Like {@link #create(Resourcepart)}, but will return a {@link MucCreateConfigFormHandle} if the room creation was acknowledged by
 * the service (with an 201 status code). It's up to the caller to decide, based on the return
 * value, if he needs to continue sending the room configuration. If {@code null} is returned, the room
 * already existed and the user is able to join right away, without sending a form.
 *
 * @param mucEnterConfiguration the configuration used to enter the MUC.
 * @return A {@link MucCreateConfigFormHandle} if the room was created while joining, or {@code null} if the room was just joined.
 * @throws XMPPErrorException if the room couldn't be created for some reason (e.g. 405 error if
 *         the user is not allowed to create the room)
 * @throws NoResponseException if there was no response from the server.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws MucAlreadyJoinedException if the MUC is already joined
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws NotAMucServiceException if the entity is not a MUC serivce.
 */
public synchronized MucCreateConfigFormHandle createOrJoin(MucEnterConfiguration mucEnterConfiguration)
                throws NoResponseException, XMPPErrorException, InterruptedException, MucAlreadyJoinedException, NotConnectedException, NotAMucServiceException {
    if (isJoined()) {
        throw new MucAlreadyJoinedException();
    }

    Presence presence = enter(mucEnterConfiguration);

    // Look for confirmation of room creation from the server
    MUCUser mucUser = MUCUser.from(presence);
    if (mucUser != null && mucUser.getStatus().contains(Status.ROOM_CREATED_201)) {
        // Room was created and the user has joined the room
        return new MucCreateConfigFormHandle();
    }
    return null;
}
 
Example 2
Source File: MultiUserChatIntegrationTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
@SmackIntegrationTest
public void mucJoinLeaveTest() throws XmppStringprepException, NotAMucServiceException, NoResponseException,
        XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException {
    EntityBareJid mucAddress = JidCreate.entityBareFrom(Localpart.from("smack-inttest-join-leave-" + randomString),
            mucService.getDomain());

    MultiUserChat muc = mucManagerOne.getMultiUserChat(mucAddress);

    muc.join(Resourcepart.from("nick-one"));

    Presence reflectedLeavePresence = muc.leave();

    MUCUser mucUser = MUCUser.from(reflectedLeavePresence);
    assertNotNull(mucUser);

    assertTrue(mucUser.getStatus().contains(MUCUser.Status.PRESENCE_TO_SELF_110));
}
 
Example 3
Source File: MUCUserStatusCodeFilter.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(Stanza stanza) {
    MUCUser mucUser = MUCUser.from(stanza);
    if (mucUser == null) {
        return false;
    }
    return mucUser.getStatus().contains(status);
}
 
Example 4
Source File: AgentSession.java    From Smack with Apache License 2.0 4 votes vote down vote up
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);
            // Fire event.
            // 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);
        }
    }
}
 
Example 5
Source File: Workgroup.java    From Smack with Apache License 2.0 4 votes vote down vote up
private void handlePacket(Stanza packet) {
    if (packet instanceof Message) {
        Message msg = (Message) packet;
        // Check to see if the user left the queue.
        ExtensionElement pe = msg.getExtensionElement("depart-queue", "http://jabber.org/protocol/workgroup");
        ExtensionElement queueStatus = msg.getExtensionElement("queue-status", "http://jabber.org/protocol/workgroup");

        if (pe != null) {
            fireQueueDepartedEvent();
        }
        else if (queueStatus != null) {
            QueueUpdate queueUpdate = (QueueUpdate) queueStatus;
            if (queueUpdate.getPosition() != -1) {
                fireQueuePositionEvent(queueUpdate.getPosition());
            }
            if (queueUpdate.getRemaingTime() != -1) {
                fireQueueTimeEvent(queueUpdate.getRemaingTime());
            }
        }

        else {
            // Check if a room invitation was sent and if the sender is the workgroup
            MUCUser mucUser = MUCUser.from(msg);
            MUCUser.Invite invite = mucUser != null ? mucUser.getInvite() : null;
            if (invite != null && workgroupJID.equals(invite.getFrom())) {
                String sessionID = null;
                Map<String, List<String>> metaData = null;

                pe = msg.getExtensionElement(SessionID.ELEMENT_NAME,
                        SessionID.NAMESPACE);
                if (pe != null) {
                    sessionID = ((SessionID) pe).getSessionID();
                }

                pe = msg.getExtensionElement(MetaData.ELEMENT_NAME,
                        MetaData.NAMESPACE);
                if (pe != null) {
                    metaData = ((MetaData) pe).getMetaData();
                }

                WorkgroupInvitation inv = new WorkgroupInvitation(connection.getUser(), msg.getFrom(),
                        workgroupJID, sessionID, msg.getBody(),
                        msg.getFrom(), metaData);

                fireInvitationEvent(inv);
            }
        }
    }
}