Search in sources :

Example 1 with MUCUser

use of org.jivesoftware.smackx.packet.MUCUser in project ecf by eclipse.

the class AgentSession method handlePacket.

// PacketListener Implementation.
private void handlePacket(Packet packet) {
    if (packet instanceof OfferRequestProvider.OfferRequestPacket) {
        // Acknowledge the IQ set.
        IQ reply = new IQ() {

            public String getChildElementXML() {
                return null;
            }
        };
        reply.setPacketID(packet.getPacketID());
        reply.setTo(packet.getFrom());
        reply.setType(IQ.Type.RESULT);
        connection.sendPacket(reply);
        fireOfferRequestEvent((OfferRequestProvider.OfferRequestPacket) packet);
    } else 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.
        String queueName = StringUtils.parseResource(presence.getFrom());
        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.getExtension(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.getExtension(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.
        DefaultPacketExtension notifyAgents = (DefaultPacketExtension) presence.getExtension("notify-agents", "http://jabber.org/protocol/workgroup");
        if (notifyAgents != null) {
            int currentChats = Integer.parseInt(notifyAgents.getValue("current-chats"));
            int maxChats = Integer.parseInt(notifyAgents.getValue("max-chats"));
            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) message.getExtension("x", "http://jabber.org/protocol/muc#user");
        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.getExtension(SessionID.ELEMENT_NAME, SessionID.NAMESPACE);
            if (sessionIDExt != null) {
                sessionID = sessionIDExt.getSessionID();
            }
            MetaData metaDataExt = (MetaData) message.getExtension(MetaData.ELEMENT_NAME, MetaData.NAMESPACE);
            if (metaDataExt != null) {
                metaData = metaDataExt.getMetaData();
            }
            this.fireInvitationEvent(message.getFrom(), sessionID, message.getBody(), message.getFrom(), metaData);
        }
    } else if (packet instanceof OfferRevokeProvider.OfferRevokePacket) {
        // Acknowledge the IQ set.
        IQ reply = new IQ() {

            public String getChildElementXML() {
                return null;
            }
        };
        reply.setPacketID(packet.getPacketID());
        reply.setType(IQ.Type.RESULT);
        connection.sendPacket(reply);
        fireOfferRevokeEvent((OfferRevokeProvider.OfferRevokePacket) packet);
    }
}
Also used : MUCUser(org.jivesoftware.smackx.packet.MUCUser) MetaData(org.jivesoftware.smackx.workgroup.MetaData)

Example 2 with MUCUser

use of org.jivesoftware.smackx.packet.MUCUser in project ecf by eclipse.

the class MultiUserChat method init.

private void init() {
    // Create filters
    messageFilter = new AndFilter(new FromMatchesFilter(room), new MessageTypeFilter(Message.Type.groupchat));
    messageFilter = new AndFilter(messageFilter, new PacketFilter() {

        public boolean accept(Packet packet) {
            Message msg = (Message) packet;
            return msg.getBody() != null;
        }
    });
    presenceFilter = new AndFilter(new FromMatchesFilter(room), new PacketTypeFilter(Presence.class));
    // Create a collector for incoming messages.
    messageCollector = new ConnectionDetachedPacketCollector();
    // Create a listener for subject updates.
    PacketListener subjectListener = new PacketListener() {

        public void processPacket(Packet packet) {
            Message msg = (Message) packet;
            // Update the room subject
            subject = msg.getSubject();
            // Fire event for subject updated listeners
            fireSubjectUpdatedListeners(msg.getSubject(), msg.getFrom());
        }
    };
    // Create a listener for all presence updates.
    PacketListener presenceListener = new PacketListener() {

        public void processPacket(Packet packet) {
            Presence presence = (Presence) packet;
            String from = presence.getFrom();
            String myRoomJID = room + "/" + nickname;
            boolean isUserStatusModification = presence.getFrom().equals(myRoomJID);
            if (presence.getType() == Presence.Type.available) {
                Presence oldPresence = occupantsMap.put(from, presence);
                if (oldPresence != null) {
                    // Get the previous occupant's affiliation & role
                    MUCUser mucExtension = getMUCUserExtension(oldPresence);
                    String oldAffiliation = mucExtension.getItem().getAffiliation();
                    String oldRole = mucExtension.getItem().getRole();
                    // Get the new occupant's affiliation & role
                    mucExtension = getMUCUserExtension(presence);
                    String newAffiliation = mucExtension.getItem().getAffiliation();
                    String newRole = mucExtension.getItem().getRole();
                    // Fire role modification events
                    checkRoleModifications(oldRole, newRole, isUserStatusModification, from);
                    // Fire affiliation modification events
                    checkAffiliationModifications(oldAffiliation, newAffiliation, isUserStatusModification, from);
                } else {
                    // A new occupant has joined the room
                    if (!isUserStatusModification) {
                        List<String> params = new ArrayList<String>();
                        params.add(from);
                        fireParticipantStatusListeners("joined", params);
                    }
                }
            } else if (presence.getType() == Presence.Type.unavailable) {
                occupantsMap.remove(from);
                MUCUser mucUser = getMUCUserExtension(presence);
                if (mucUser != null && mucUser.getStatus() != null) {
                    // Fire events according to the received presence code
                    checkPresenceCode(mucUser.getStatus().getCode(), presence.getFrom().equals(myRoomJID), mucUser, from);
                } else {
                    // An occupant has left the room
                    if (!isUserStatusModification) {
                        List<String> params = new ArrayList<String>();
                        params.add(from);
                        fireParticipantStatusListeners("left", params);
                    }
                }
            }
        }
    };
    // Listens for all messages that include a MUCUser extension and fire the invitation
    // rejection listeners if the message includes an invitation rejection.
    PacketListener declinesListener = new PacketListener() {

        public void processPacket(Packet packet) {
            // Get the MUC User extension
            MUCUser mucUser = getMUCUserExtension(packet);
            // Check if the MUCUser informs that the invitee has declined the invitation
            if (mucUser.getDecline() != null && ((Message) packet).getType() != Message.Type.error) {
                // Fire event for invitation rejection listeners
                fireInvitationRejectionListeners(mucUser.getDecline().getFrom(), mucUser.getDecline().getReason());
            }
        }
    };
    PacketMultiplexListener packetMultiplexor = new PacketMultiplexListener(messageCollector, presenceListener, subjectListener, declinesListener);
    roomListenerMultiplexor = RoomListenerMultiplexor.getRoomMultiplexor(connection);
    roomListenerMultiplexor.addRoom(room, packetMultiplexor);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) MUCUser(org.jivesoftware.smackx.packet.MUCUser) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Message(org.jivesoftware.smack.packet.Message) ArrayList(java.util.ArrayList) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) PacketListener(org.jivesoftware.smack.PacketListener) AndFilter(org.jivesoftware.smack.filter.AndFilter) MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) MUCInitialPresence(org.jivesoftware.smackx.packet.MUCInitialPresence) Presence(org.jivesoftware.smack.packet.Presence) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with MUCUser

use of org.jivesoftware.smackx.packet.MUCUser in project ecf by eclipse.

the class MultiUserChat method create.

/**
 * Creates the room according to some default configuration, assign the requesting user
 * as the room owner, and add the owner to the room but not allow anyone else to enter
 * the room (effectively "locking" the room). The requesting user will join the room
 * under the specified nickname as soon as the room has been created.<p>
 *
 * To create an "Instant Room", that means a room with some default configuration that is
 * available for immediate access, the room's owner should send an empty form after creating
 * the room. {@link #sendConfigurationForm(Form)}<p>
 *
 * To create a "Reserved Room", that means a room manually configured by the room creator
 * before anyone is allowed to enter, the room's owner should complete and send a form after
 * creating the room. Once the completed configutation form is sent to the server, the server
 * will unlock the room. {@link #sendConfigurationForm(Form)}
 *
 * @param nickname the nickname to use.
 * @throws XMPPException if the room couldn't be created for some reason
 *          (e.g. room already exists; user already joined to an existant room or
 *          405 error if the user is not allowed to create the room)
 */
public synchronized void create(String nickname) throws XMPPException {
    if (nickname == null || nickname.equals("")) {
        throw new IllegalArgumentException("Nickname must not be null or blank.");
    }
    // nickname.
    if (joined) {
        throw new IllegalStateException("Creation failed - User already joined the room.");
    }
    // We create a room by sending a presence packet to room@service/nick
    // and signal support for MUC. The owner will be automatically logged into the room.
    Presence joinPresence = new Presence(Presence.Type.available);
    joinPresence.setTo(room + "/" + nickname);
    // Indicate the the client supports MUC
    joinPresence.addExtension(new MUCInitialPresence());
    // Invoke presence interceptors so that extra information can be dynamically added
    for (PacketInterceptor packetInterceptor : presenceInterceptors) {
        packetInterceptor.interceptPacket(joinPresence);
    }
    // Wait for a presence packet back from the server.
    PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(room + "/" + nickname), new PacketTypeFilter(Presence.class));
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Send create & join packet.
    connection.sendPacket(joinPresence);
    // Wait up to a certain number of seconds for a reply.
    Presence presence = (Presence) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    response.cancel();
    if (presence == null) {
        throw new XMPPException("No response from server.");
    } else if (presence.getError() != null) {
        throw new XMPPException(presence.getError());
    }
    // Whether the room existed before or was created, the user has joined the room
    this.nickname = nickname;
    joined = true;
    userHasJoined();
    // Look for confirmation of room creation from the server
    MUCUser mucUser = getMUCUserExtension(presence);
    if (mucUser != null && mucUser.getStatus() != null) {
        if ("201".equals(mucUser.getStatus().getCode())) {
            // Room was created and the user has joined the room
            return;
        }
    }
    // We need to leave the room since it seems that the room already existed
    leave();
    throw new XMPPException("Creation failed - Missing acknowledge of room creation.");
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) MUCUser(org.jivesoftware.smackx.packet.MUCUser) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) MUCInitialPresence(org.jivesoftware.smackx.packet.MUCInitialPresence) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) PacketCollector(org.jivesoftware.smack.PacketCollector) MUCInitialPresence(org.jivesoftware.smackx.packet.MUCInitialPresence) Presence(org.jivesoftware.smack.packet.Presence) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) XMPPException(org.jivesoftware.smack.XMPPException) PacketInterceptor(org.jivesoftware.smack.PacketInterceptor)

Example 4 with MUCUser

use of org.jivesoftware.smackx.packet.MUCUser in project ecf by eclipse.

the class MultiUserChat method decline.

/**
 * Informs the sender of an invitation that the invitee declines the invitation. The rejection
 * will be sent to the room which in turn will forward the rejection to the inviter.
 *
 * @param conn the connection to use for sending the rejection.
 * @param room the room that sent the original invitation.
 * @param inviter the inviter of the declined invitation.
 * @param reason the reason why the invitee is declining the invitation.
 */
public static void decline(Connection conn, String room, String inviter, String reason) {
    Message message = new Message(room);
    // Create the MUCUser packet that will include the rejection
    MUCUser mucUser = new MUCUser();
    MUCUser.Decline decline = new MUCUser.Decline();
    decline.setTo(inviter);
    decline.setReason(reason);
    mucUser.setDecline(decline);
    // Add the MUCUser packet that includes the rejection
    message.addExtension(mucUser);
    conn.sendPacket(message);
}
Also used : MUCUser(org.jivesoftware.smackx.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message)

Example 5 with MUCUser

use of org.jivesoftware.smackx.packet.MUCUser in project ecf by eclipse.

the class MultiUserChat method invite.

/**
 * Invites another user to the room in which one is an occupant using a given Message. The invitation
 * will be sent to the room which in turn will forward the invitation to the invitee.<p>
 *
 * If the room is password-protected, the invitee will receive a password to use to join
 * the room. If the room is members-only, the the invitee may be added to the member list.
 *
 * @param message the message to use for sending the invitation.
 * @param user the user to invite to the room.(e.g. hecate@shakespeare.lit)
 * @param reason the reason why the user is being invited.
 */
public void invite(Message message, String user, String reason) {
    // TODO listen for 404 error code when inviter supplies a non-existent JID
    message.setTo(room);
    // Create the MUCUser packet that will include the invitation
    MUCUser mucUser = new MUCUser();
    MUCUser.Invite invite = new MUCUser.Invite();
    invite.setTo(user);
    invite.setReason(reason);
    mucUser.setInvite(invite);
    // Add the MUCUser packet that includes the invitation to the message
    message.addExtension(mucUser);
    connection.sendPacket(message);
}
Also used : MUCUser(org.jivesoftware.smackx.packet.MUCUser)

Aggregations

MUCUser (org.jivesoftware.smackx.packet.MUCUser)7 Message (org.jivesoftware.smack.packet.Message)3 Presence (org.jivesoftware.smack.packet.Presence)3 AndFilter (org.jivesoftware.smack.filter.AndFilter)2 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)2 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)2 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)2 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)2 MetaData (org.jivesoftware.smackx.workgroup.MetaData)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 MessageEvent (org.eclipse.ecf.internal.provider.xmpp.events.MessageEvent)1 ContainerMessage (org.eclipse.ecf.provider.generic.ContainerMessage)1 PacketCollector (org.jivesoftware.smack.PacketCollector)1 PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)1 PacketListener (org.jivesoftware.smack.PacketListener)1 XMPPException (org.jivesoftware.smack.XMPPException)1 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)1