Search in sources :

Example 1 with PacketInterceptor

use of org.jivesoftware.smack.PacketInterceptor in project ecf by eclipse.

the class MultiUserChat method changeAvailabilityStatus.

/**
 * Changes the occupant's availability status within the room. The presence type
 * will remain available but with a new status that describes the presence update and
 * a new presence mode (e.g. Extended away).
 *
 * @param status a text message describing the presence update.
 * @param mode the mode type for the presence update.
 */
public void changeAvailabilityStatus(String status, Presence.Mode mode) {
    if (nickname == null || nickname.equals("")) {
        throw new IllegalArgumentException("Nickname must not be null or blank.");
    }
    // availability status.
    if (!joined) {
        throw new IllegalStateException("Must be logged into the room to change the " + "availability status.");
    }
    // We change the availability status by sending a presence packet to the room with the
    // new presence status and mode
    Presence joinPresence = new Presence(Presence.Type.available);
    joinPresence.setStatus(status);
    joinPresence.setMode(mode);
    joinPresence.setTo(room + "/" + nickname);
    // Invoke presence interceptors so that extra information can be dynamically added
    for (PacketInterceptor packetInterceptor : presenceInterceptors) {
        packetInterceptor.interceptPacket(joinPresence);
    }
    // Send join packet.
    connection.sendPacket(joinPresence);
}
Also used : MUCInitialPresence(org.jivesoftware.smackx.packet.MUCInitialPresence) Presence(org.jivesoftware.smack.packet.Presence) PacketInterceptor(org.jivesoftware.smack.PacketInterceptor)

Example 2 with PacketInterceptor

use of org.jivesoftware.smack.PacketInterceptor in project ecf by eclipse.

the class EntityCapsManager method init.

private void init() {
    Connection connection = weakRefConnection.get();
    instances.put(connection, this);
    connection.addConnectionListener(new ConnectionListener() {

        public void connectionClosed() {
            // Unregister this instance since the connection has been closed
            presenceSend = false;
            instances.remove(weakRefConnection.get());
        }

        public void connectionClosedOnError(Exception e) {
            presenceSend = false;
        }

        public void reconnectionFailed(Exception e) {
        // ignore
        }

        public void reconnectingIn(int seconds) {
        // ignore
        }

        public void reconnectionSuccessful() {
        // ignore
        }
    });
    // This calculates the local entity caps version
    updateLocalEntityCaps();
    if (SmackConfiguration.autoEnableEntityCaps())
        enableEntityCaps();
    PacketFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class), new PacketExtensionFilter(ELEMENT, NAMESPACE));
    connection.addPacketListener(new PacketListener() {

        // Listen for remote presence stanzas with the caps extension
        // If we receive such a stanza, record the JID and nodeVer
        public void processPacket(Packet packet) {
            if (!entityCapsEnabled())
                return;
            CapsExtension ext = (CapsExtension) packet.getExtension(EntityCapsManager.ELEMENT, EntityCapsManager.NAMESPACE);
            String hash = ext.getHash().toLowerCase();
            if (!SUPPORTED_HASHES.containsKey(hash))
                return;
            String from = packet.getFrom();
            String node = ext.getNode();
            String ver = ext.getVer();
            jidCaps.put(from, new NodeVerHash(node, ver, hash));
        }
    }, packetFilter);
    packetFilter = new AndFilter(new PacketTypeFilter(Presence.class), new NotFilter(new PacketExtensionFilter(ELEMENT, NAMESPACE)));
    connection.addPacketListener(new PacketListener() {

        public void processPacket(Packet packet) {
            // always remove the JID from the map, even if entityCaps are
            // disabled
            String from = packet.getFrom();
            jidCaps.remove(from);
        }
    }, packetFilter);
    packetFilter = new PacketTypeFilter(Presence.class);
    connection.addPacketSendingListener(new PacketListener() {

        public void processPacket(Packet packet) {
            presenceSend = true;
        }
    }, packetFilter);
    // Intercept presence packages and add caps data when intended.
    // XEP-0115 specifies that a client SHOULD include entity capabilities
    // with every presence notification it sends.
    PacketFilter capsPacketFilter = new PacketTypeFilter(Presence.class);
    PacketInterceptor packetInterceptor = new PacketInterceptor() {

        public void interceptPacket(Packet packet) {
            if (!entityCapsEnabled)
                return;
            CapsExtension caps = new CapsExtension(ENTITY_NODE, getCapsVersion(), "sha-1");
            packet.addExtension(caps);
        }
    };
    connection.addPacketInterceptor(packetInterceptor, capsPacketFilter);
    // It's important to do this as last action. Since it changes the
    // behavior of the SDM in some ways
    sdm.setEntityCapsManager(this);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Connection(org.jivesoftware.smack.Connection) XMPPConnection(org.jivesoftware.smack.XMPPConnection) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) ConnectionListener(org.jivesoftware.smack.ConnectionListener) CapsExtension(org.jivesoftware.smackx.entitycaps.packet.CapsExtension) PacketListener(org.jivesoftware.smack.PacketListener) PacketInterceptor(org.jivesoftware.smack.PacketInterceptor) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketExtensionFilter(org.jivesoftware.smack.filter.PacketExtensionFilter) NotFilter(org.jivesoftware.smack.filter.NotFilter) Presence(org.jivesoftware.smack.packet.Presence)

Example 3 with PacketInterceptor

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

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

use of org.jivesoftware.smack.PacketInterceptor in project ecf by eclipse.

the class MultiUserChat method leave.

/**
 * Leave the chat room.
 */
public synchronized void leave() {
    // If not joined already, do nothing.
    if (!joined) {
        return;
    }
    // We leave a room by sending a presence packet where the "to"
    // field is in the form "roomName@service/nickname"
    Presence leavePresence = new Presence(Presence.Type.unavailable);
    leavePresence.setTo(room + "/" + nickname);
    // Invoke presence interceptors so that extra information can be dynamically added
    for (PacketInterceptor packetInterceptor : presenceInterceptors) {
        packetInterceptor.interceptPacket(leavePresence);
    }
    connection.sendPacket(leavePresence);
    // Reset occupant information.
    occupantsMap.clear();
    nickname = null;
    joined = false;
    userHasLeft();
}
Also used : MUCInitialPresence(org.jivesoftware.smackx.packet.MUCInitialPresence) Presence(org.jivesoftware.smack.packet.Presence) PacketInterceptor(org.jivesoftware.smack.PacketInterceptor)

Aggregations

PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)6 Presence (org.jivesoftware.smack.packet.Presence)6 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)5 XMPPException (org.jivesoftware.smack.XMPPException)4 AndFilter (org.jivesoftware.smack.filter.AndFilter)4 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)4 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)4 PacketCollector (org.jivesoftware.smack.PacketCollector)3 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)3 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Connection (org.jivesoftware.smack.Connection)1 ConnectionListener (org.jivesoftware.smack.ConnectionListener)1 PacketListener (org.jivesoftware.smack.PacketListener)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 NotFilter (org.jivesoftware.smack.filter.NotFilter)1 PacketExtensionFilter (org.jivesoftware.smack.filter.PacketExtensionFilter)1 Packet (org.jivesoftware.smack.packet.Packet)1 CapsExtension (org.jivesoftware.smackx.entitycaps.packet.CapsExtension)1 MUCUser (org.jivesoftware.smackx.packet.MUCUser)1