Search in sources :

Example 1 with MUCInitialPresence

use of org.jivesoftware.smackx.packet.MUCInitialPresence 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 2 with MUCInitialPresence

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

the class MultiUserChat method join.

/**
 * Joins the chat room using the specified nickname and password. If already joined
 * using another nickname, this method will first leave the room and then
 * re-join using the new nickname.<p>
 *
 * To control the amount of history to receive while joining a room you will need to provide
 * a configured DiscussionHistory object.<p>
 *
 * A password is required when joining password protected rooms. If the room does
 * not require a password there is no need to provide one.<p>
 *
 * If the room does not already exist when the user seeks to enter it, the server will
 * decide to create a new room or not.
 *
 * @param nickname the nickname to use.
 * @param password the password to use.
 * @param history the amount of discussion history to receive while joining a room.
 * @param timeout the amount of time to wait for a reply from the MUC service(in milleseconds).
 * @throws XMPPException if an error occurs joining the room. In particular, a
 *      401 error can occur if no password was provided and one is required; or a
 *      403 error can occur if the user is banned; or a
 *      404 error can occur if the room does not exist or is locked; or a
 *      407 error can occur if user is not on the member list; or a
 *      409 error can occur if someone is already in the group chat with the same nickname.
 */
public synchronized void join(String nickname, String password, DiscussionHistory history, long timeout) throws XMPPException {
    if (nickname == null || nickname.equals("")) {
        throw new IllegalArgumentException("Nickname must not be null or blank.");
    }
    // nickname.
    if (joined) {
        leave();
    }
    // We join a room by sending a presence packet where the "to"
    // field is in the form "roomName@service/nickname"
    Presence joinPresence = new Presence(Presence.Type.available);
    joinPresence.setTo(room + "/" + nickname);
    // Indicate the the client supports MUC
    MUCInitialPresence mucInitialPresence = new MUCInitialPresence();
    if (password != null) {
        mucInitialPresence.setPassword(password);
    }
    if (history != null) {
        mucInitialPresence.setHistory(history.getMUCHistory());
    }
    joinPresence.addExtension(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 = null;
    Presence presence;
    try {
        response = connection.createPacketCollector(responseFilter);
        // Send join packet.
        connection.sendPacket(joinPresence);
        // Wait up to a certain number of seconds for a reply.
        presence = (Presence) response.nextResult(timeout);
    } finally {
        // Stop queuing results
        if (response != null) {
            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;
    joined = true;
    userHasJoined();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) 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)

Aggregations

PacketCollector (org.jivesoftware.smack.PacketCollector)2 PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)2 XMPPException (org.jivesoftware.smack.XMPPException)2 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 Presence (org.jivesoftware.smack.packet.Presence)2 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)2 MUCUser (org.jivesoftware.smackx.packet.MUCUser)1