Search in sources :

Example 41 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

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 XMPPErrorException if the new nickname is already in use by another occupant.
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     * @throws MucNotJoinedException 
     */
public synchronized void changeNickname(Resourcepart nickname) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException {
    StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank.");
    // nickname.
    if (!joined) {
        throw new MucNotJoinedException(this);
    }
    final EntityFullJid jid = JidCreate.fullFrom(room, 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(jid);
    // Wait for a presence packet back from the server.
    StanzaFilter responseFilter = new AndFilter(FromMatchesFilter.createFull(jid), new StanzaTypeFilter(Presence.class));
    StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, joinPresence);
    // Wait up to a certain number of seconds for a reply. If there is a negative reply, an
    // exception will be thrown
    response.nextResultOrThrow();
    this.nickname = nickname;
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) EntityFullJid(org.jxmpp.jid.EntityFullJid) Presence(org.jivesoftware.smack.packet.Presence) MUCInitialPresence(org.jivesoftware.smackx.muc.packet.MUCInitialPresence) MucNotJoinedException(org.jivesoftware.smackx.muc.MultiUserChatException.MucNotJoinedException) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 42 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MultiUserChat method createOrJoin.

/**
     * Like {@link #create(Resourcepart)}, but will return a {@link MucCreateConfigFormHandle} if the room creation was acknowledged by
     * the service (with an 201 status code). It's up to the caller to decide, based on the return
     * value, if he needs to continue sending the room configuration. If {@code null} is returned, the room
     * already existed and the user is able to join right away, without sending a form.
     *
     * @param mucEnterConfiguration the configuration used to enter the MUC.
     * @return A {@link MucCreateConfigFormHandle} if the room was created while joining, or {@code null} if the room was just joined.
     * @throws XMPPErrorException if the room couldn't be created for some reason (e.g. 405 error if
     *         the user is not allowed to create the room)
     * @throws NoResponseException if there was no response from the server.
     * @throws InterruptedException 
     * @throws MucAlreadyJoinedException if the MUC is already joined
     * @throws NotConnectedException 
     * @throws NotAMucServiceException 
     */
public synchronized MucCreateConfigFormHandle createOrJoin(MucEnterConfiguration mucEnterConfiguration) throws NoResponseException, XMPPErrorException, InterruptedException, MucAlreadyJoinedException, NotConnectedException, NotAMucServiceException {
    if (joined) {
        throw new MucAlreadyJoinedException();
    }
    Presence presence = enter(mucEnterConfiguration);
    // Look for confirmation of room creation from the server
    MUCUser mucUser = MUCUser.from(presence);
    if (mucUser != null && mucUser.getStatus().contains(Status.ROOM_CREATED_201)) {
        // Room was created and the user has joined the room
        return new MucCreateConfigFormHandle();
    }
    return null;
}
Also used : MucAlreadyJoinedException(org.jivesoftware.smackx.muc.MultiUserChatException.MucAlreadyJoinedException) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Presence(org.jivesoftware.smack.packet.Presence) MUCInitialPresence(org.jivesoftware.smackx.muc.packet.MUCInitialPresence)

Example 43 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MultiUserChat method enter.

/**
     * Enter a room, as described in XEP-45 7.2.
     *
     * @param conf the configuration used to enter the room.
     * @return the returned presence by the service after the client send the initial presence in order to enter the room.
     * @throws NotConnectedException
     * @throws NoResponseException
     * @throws XMPPErrorException
     * @throws InterruptedException
     * @throws NotAMucServiceException 
     * @see <a href="http://xmpp.org/extensions/xep-0045.html#enter">XEP-45 7.2 Entering a Room</a>
     */
private Presence enter(MucEnterConfiguration conf) throws NotConnectedException, NoResponseException, XMPPErrorException, InterruptedException, NotAMucServiceException {
    final DomainBareJid mucService = room.asDomainBareJid();
    if (!KNOWN_MUC_SERVICES.containsKey(mucService)) {
        if (multiUserChatManager.providesMucService(mucService)) {
            KNOWN_MUC_SERVICES.put(mucService, null);
        } else {
            throw new NotAMucServiceException(this);
        }
    }
    // We enter a room by sending a presence packet where the "to"
    // field is in the form "roomName@service/nickname"
    Presence joinPresence = conf.getJoinPresence(this);
    // Setup the messageListeners and presenceListeners *before* the join presence is send.
    connection.addSyncStanzaListener(messageListener, fromRoomGroupchatFilter);
    connection.addSyncStanzaListener(presenceListener, new AndFilter(fromRoomFilter, StanzaTypeFilter.PRESENCE));
    connection.addSyncStanzaListener(subjectListener, new AndFilter(fromRoomFilter, MessageWithSubjectFilter.INSTANCE, new NotFilter(MessageTypeFilter.ERROR)));
    connection.addSyncStanzaListener(declinesListener, DECLINE_FILTER);
    connection.addPacketInterceptor(presenceInterceptor, new AndFilter(ToMatchesFilter.create(room), StanzaTypeFilter.PRESENCE));
    messageCollector = connection.createStanzaCollector(fromRoomGroupchatFilter);
    // Wait for a presence packet back from the server.
    // @formatter:off
    StanzaFilter responseFilter = new AndFilter(StanzaTypeFilter.PRESENCE, new OrFilter(// We use a bare JID filter for positive responses, since the MUC service/room may rewrite the nickname.
    new AndFilter(FromMatchesFilter.createBare(getRoom()), MUCUserStatusCodeFilter.STATUS_110_PRESENCE_TO_SELF), // JID we send the join presence to.
    new AndFilter(FromMatchesFilter.createFull(joinPresence.getTo()), new StanzaIdFilter(joinPresence), PresenceTypeFilter.ERROR)));
    // @formatter:on
    Presence presence;
    try {
        presence = connection.createStanzaCollectorAndSend(responseFilter, joinPresence).nextResultOrThrow(conf.getTimeout());
    } catch (NotConnectedException | InterruptedException | NoResponseException | XMPPErrorException e) {
        // Ensure that all callbacks are removed if there is an exception
        removeConnectionCallbacks();
        throw e;
    }
    // This presence must be send from a full JID. We use the resourcepart of this JID as nick, since the room may
    // performed roomnick rewriting
    this.nickname = presence.getFrom().asEntityFullJidIfPossible().getResourcepart();
    joined = true;
    // Update the list of joined rooms
    multiUserChatManager.addJoinedRoom(room);
    return presence;
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) OrFilter(org.jivesoftware.smack.filter.OrFilter) NotAMucServiceException(org.jivesoftware.smackx.muc.MultiUserChatException.NotAMucServiceException) AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaIdFilter(org.jivesoftware.smack.filter.StanzaIdFilter) NotFilter(org.jivesoftware.smack.filter.NotFilter) Presence(org.jivesoftware.smack.packet.Presence) MUCInitialPresence(org.jivesoftware.smackx.muc.packet.MUCInitialPresence) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 44 with Presence

use of org.jivesoftware.smack.packet.Presence in project intellij-plugins by JetBrains.

the class JabberTransport_ConnectionTest method createLocalConnectionWithJabberUser.

private XMPPConnection createLocalConnectionWithJabberUser(String userName, String resource) throws XMPPException, InterruptedException {
    final XMPPConnection conn = new XMPPConnection("localhost");
    conn.getAccountManager().createAccount(userName, "123456");
    if (resource == null) {
        resource = "test";
    }
    conn.login(userName, "123456", resource);
    conn.getRoster().setSubscriptionMode(Roster.SubscriptionMode.accept_all);
    Presence presence = new Presence(Presence.Type.available);
    conn.sendPacket(presence);
    disposeOnTearDown(new Disposable() {

        @Override
        public void dispose() {
            try {
                if (conn.isConnected()) {
                    conn.getAccountManager().deleteAccount();
                }
            } catch (XMPPException e) {
                throw new RuntimeException(e);
            } finally {
                Thread thread = new Thread("jabber conn test") {

                    @Override
                    public void run() {
                        conn.close();
                    }
                };
                thread.start();
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    });
    return conn;
}
Also used : Disposable(org.picocontainer.Disposable) Presence(org.jivesoftware.smack.packet.Presence) UserPresence(jetbrains.communicator.core.users.UserPresence)

Example 45 with Presence

use of org.jivesoftware.smack.packet.Presence in project xabber-android by redsolution.

the class PresenceManager method requestSubscription.

/**
     * Requests subscription to the contact.
     *
     * @param account
     * @param bareAddress
     * @throws NetworkException
     */
public void requestSubscription(String account, String bareAddress) throws NetworkException {
    Presence packet = new Presence(Presence.Type.subscribe);
    packet.setTo(bareAddress);
    ConnectionManager.getInstance().sendStanza(account, packet);
    HashSet<String> set = requestedSubscriptions.get(account);
    if (set == null) {
        set = new HashSet<>();
        requestedSubscriptions.put(account, set);
    }
    set.add(bareAddress);
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)103 Message (org.jivesoftware.smack.packet.Message)21 Resourcepart (org.jxmpp.jid.parts.Resourcepart)12 Jid (org.jxmpp.jid.Jid)11 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)10 AccountItem (com.xabber.android.data.account.AccountItem)9 UserJid (com.xabber.android.data.entity.UserJid)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 StanzaCollector (org.jivesoftware.smack.StanzaCollector)7 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 UserPresence (jetbrains.communicator.core.users.UserPresence)6 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)6 BareJid (org.jxmpp.jid.BareJid)6 ClientInfo (com.xabber.android.data.extension.capability.ClientInfo)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)5 Test (org.junit.Test)5 EntityFullJid (org.jxmpp.jid.EntityFullJid)5