Search in sources :

Example 1 with FromMatchesFilter

use of org.jivesoftware.smack.filter.FromMatchesFilter in project Smack by igniterealtime.

the class PacketReaderTest method testErrorWithNoText.

/**
 * Checks that parser still works when receiving an error text with no description.
 */
public void testErrorWithNoText() {
    // Send a regular message from user0 to user1
    Message packet = new Message();
    packet.setFrom(getFullJID(0));
    packet.setTo(getFullJID(1));
    packet.setBody("aloha");
    // User1 will always reply to user0 when a message is received
    getConnection(1).addAsyncPacketListener(new PacketListener() {

        public void processStanza(Packet packet) {
            System.out.println(new Date() + " " + packet);
            Message message = new Message(packet.getFrom());
            message.setFrom(getFullJID(1));
            message.setBody("HELLO");
            getConnection(1).sendStanza(message);
        }
    }, new StanzaTypeFilter(Message.class));
    // User0 listen for replies from user1
    StanzaCollector collector = getConnection(0).createStanzaCollector(new FromMatchesFilter(getFullJID(1)));
    // User0 sends the regular message to user1
    getConnection(0).sendStanza(packet);
    // Check that user0 got a reply from user1
    assertNotNull("No message was received", collector.nextResult(1000));
    // Send a message with an empty error text
    packet = new Message();
    packet.setFrom(getFullJID(0));
    packet.setTo(getFullJID(1));
    packet.setBody("aloha");
    packet.setError(new XMPPError(XMPPError.Condition.feature_not_implemented, null));
    getConnection(0).sendStanza(packet);
    // Check that user0 got a reply from user1
    assertNotNull("No message was received", collector.nextResult(1000));
}
Also used : StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) Date(java.util.Date)

Example 2 with FromMatchesFilter

use of org.jivesoftware.smack.filter.FromMatchesFilter 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 FromMatchesFilter

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

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

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

the class MultiUserChat method changeSubject.

/**
 * Changes the subject within the room. As a default, only users with a role of "moderator"
 * are allowed to change the subject in a room. Although some rooms may be configured to
 * allow a mere participant or even a visitor to change the subject.
 *
 * @param subject the new room's subject to set.
 * @throws XMPPException if someone without appropriate privileges attempts to change the
 *          room subject will throw an error with code 403 (i.e. Forbidden)
 */
public void changeSubject(final String subject) throws XMPPException {
    Message message = new Message(room, Message.Type.groupchat);
    message.setSubject(subject);
    // Wait for an error or confirmation message back from the server.
    PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(room), new PacketTypeFilter(Message.class));
    responseFilter = new AndFilter(responseFilter, new PacketFilter() {

        public boolean accept(Packet packet) {
            Message msg = (Message) packet;
            return subject.equals(msg.getSubject());
        }
    });
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Send change subject packet.
    connection.sendPacket(message);
    // Wait up to a certain number of seconds for a reply.
    Message answer = (Message) 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 : AndFilter(org.jivesoftware.smack.filter.AndFilter) Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Message(org.jivesoftware.smack.packet.Message) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) PacketCollector(org.jivesoftware.smack.PacketCollector) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)7 AndFilter (org.jivesoftware.smack.filter.AndFilter)6 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)6 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)5 PacketCollector (org.jivesoftware.smack.PacketCollector)4 XMPPException (org.jivesoftware.smack.XMPPException)4 Presence (org.jivesoftware.smack.packet.Presence)4 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)4 PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)3 Message (org.jivesoftware.smack.packet.Message)3 Packet (org.jivesoftware.smack.packet.Packet)3 MUCUser (org.jivesoftware.smackx.packet.MUCUser)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 PacketListener (org.jivesoftware.smack.PacketListener)1 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)1 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)1 Time (org.jivesoftware.smackx.packet.Time)1