Search in sources :

Example 21 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter 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 22 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class MultiUserChat method getAffiliatesByAdmin.

/**
 * Returns a collection of <code>Affiliate</code> that have the specified room affiliation
 * sending a request in the admin namespace.
 *
 * @param affiliation the affiliation of the users in the room.
 * @return a collection of <code>Affiliate</code> that have the specified room affiliation.
 * @throws XMPPException if an error occured while performing the request to the server or you
 *         don't have enough privileges to get this information.
 */
private Collection<Affiliate> getAffiliatesByAdmin(String affiliation) throws XMPPException {
    MUCAdmin iq = new MUCAdmin();
    iq.setTo(room);
    iq.setType(IQ.Type.GET);
    // Set the specified affiliation. This may request the list of owners/admins/members/outcasts.
    MUCAdmin.Item item = new MUCAdmin.Item(affiliation, null);
    iq.addItem(item);
    // Wait for a response packet back from the server.
    PacketFilter responseFilter = new PacketIDFilter(iq.getPacketID());
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Send the request to the server.
    connection.sendPacket(iq);
    // Wait up to a certain number of seconds for a reply.
    MUCAdmin answer = (MUCAdmin) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    response.cancel();
    if (answer == null) {
        throw new XMPPException("No response from server.");
    } else if (answer.getError() != null) {
        throw new XMPPException(answer.getError());
    }
    // Get the list of affiliates from the server's answer
    List<Affiliate> affiliates = new ArrayList<Affiliate>();
    for (Iterator<MUCAdmin.Item> it = answer.getItems(); it.hasNext(); ) {
        affiliates.add(new Affiliate(it.next()));
    }
    return affiliates;
}
Also used : PacketFilter(org.jivesoftware.smack.filter.PacketFilter) MUCAdmin(org.jivesoftware.smackx.packet.MUCAdmin) PacketCollector(org.jivesoftware.smack.PacketCollector) ArrayList(java.util.ArrayList) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 23 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class MultiUserChat method changeNickname.

/**
 * Changes the occupant's nickname to a new nickname within the room. Each room occupant
 * will receive two presence packets. One of type "unavailable" for the old nickname and one
 * indicating availability for the new nickname. The unavailable presence will contain the new
 * nickname and an appropriate status code (namely 303) as extended presence information. The
 * status code 303 indicates that the occupant is changing his/her nickname.
 *
 * @param nickname the new nickname within the room.
 * @throws XMPPException if the new nickname is already in use by another occupant.
 */
public void changeNickname(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("Must be logged into the room to change nickname.");
    }
    // We change the nickname by sending a presence packet where the "to"
    // field is in the form "roomName@service/nickname"
    // We don't have to signal the MUC support again
    Presence joinPresence = new Presence(Presence.Type.available);
    joinPresence.setTo(room + "/" + nickname);
    // 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 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());
    }
    this.nickname = nickname;
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) 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 24 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter 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 25 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class MultiUserChat method changeRole.

private void changeRole(String nickname, String role, String reason) throws XMPPException {
    MUCAdmin iq = new MUCAdmin();
    iq.setTo(room);
    iq.setType(IQ.Type.SET);
    // Set the new role.
    MUCAdmin.Item item = new MUCAdmin.Item(null, role);
    item.setNick(nickname);
    item.setReason(reason);
    iq.addItem(item);
    // Wait for a response packet back from the server.
    PacketFilter responseFilter = new PacketIDFilter(iq.getPacketID());
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Send the change request to the server.
    connection.sendPacket(iq);
    // Wait up to a certain number of seconds for a reply.
    IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    response.cancel();
    if (answer == null) {
        throw new XMPPException("No response from server.");
    } else if (answer.getError() != null) {
        throw new XMPPException(answer.getError());
    }
}
Also used : PacketFilter(org.jivesoftware.smack.filter.PacketFilter) MUCAdmin(org.jivesoftware.smackx.packet.MUCAdmin) PacketCollector(org.jivesoftware.smack.PacketCollector) IQ(org.jivesoftware.smack.packet.IQ) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

PacketFilter (org.jivesoftware.smack.filter.PacketFilter)40 XMPPException (org.jivesoftware.smack.XMPPException)23 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)22 PacketCollector (org.jivesoftware.smack.PacketCollector)21 IQ (org.jivesoftware.smack.packet.IQ)18 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)15 AndFilter (org.jivesoftware.smack.filter.AndFilter)13 Packet (org.jivesoftware.smack.packet.Packet)13 Registration (org.jivesoftware.smack.packet.Registration)6 MUCAdmin (org.jivesoftware.smackx.packet.MUCAdmin)6 MUCOwner (org.jivesoftware.smackx.packet.MUCOwner)6 ArrayList (java.util.ArrayList)5 PacketListener (org.jivesoftware.smack.PacketListener)5 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)5 Presence (org.jivesoftware.smack.packet.Presence)5 PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 Message (org.jivesoftware.smack.packet.Message)4 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)4 HashMap (java.util.HashMap)2