Search in sources :

Example 6 with StanzaFilter

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

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

the class MultiUserChatLightManager method getBlockingList.

private MUCLightBlockingIQ getBlockingList(DomainBareJid mucLightService) throws NoResponseException, XMPPErrorException, InterruptedException, NotConnectedException {
    MUCLightBlockingIQ mucLightBlockingIQ = new MUCLightBlockingIQ(null, null);
    mucLightBlockingIQ.setType(Type.get);
    mucLightBlockingIQ.setTo(mucLightService);
    StanzaFilter responseFilter = new IQReplyFilter(mucLightBlockingIQ, connection());
    IQ responseIq = connection().createStanzaCollectorAndSend(responseFilter, mucLightBlockingIQ).nextResultOrThrow();
    MUCLightBlockingIQ muclIghtBlockingIQResult = (MUCLightBlockingIQ) responseIq;
    return muclIghtBlockingIQResult;
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) MUCLightBlockingIQ(org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ) IQReplyFilter(org.jivesoftware.smack.filter.IQReplyFilter) MUCLightBlockingIQ(org.jivesoftware.smackx.muclight.element.MUCLightBlockingIQ) IQ(org.jivesoftware.smack.packet.IQ)

Example 8 with StanzaFilter

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

the class JingleManager method initJingleSessionRequestListeners.

/**
     * Register the listenerJingles, waiting for a Jingle stanza(/packet) that tries to
     * establish a new session.
     */
private void initJingleSessionRequestListeners() {
    StanzaFilter initRequestFilter = new StanzaFilter() {

        // Return true if we accept this packet
        @Override
        public boolean accept(Stanza pin) {
            if (pin instanceof IQ) {
                IQ iq = (IQ) pin;
                if (iq.getType().equals(IQ.Type.set)) {
                    if (iq instanceof Jingle) {
                        Jingle jin = (Jingle) pin;
                        if (jin.getAction().equals(JingleActionEnum.SESSION_INITIATE)) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    };
    jingleSessionRequestListeners = new ArrayList<JingleSessionRequestListener>();
    // Start a packet listener for session initiation requests
    connection.addAsyncStanzaListener(new StanzaListener() {

        @Override
        public void processStanza(Stanza packet) {
            triggerSessionRequested((Jingle) packet);
        }
    }, initRequestFilter);
}
Also used : Jingle(org.jivesoftware.smackx.jingleold.packet.Jingle) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener) JingleSessionRequestListener(org.jivesoftware.smackx.jingleold.listeners.JingleSessionRequestListener)

Example 9 with StanzaFilter

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

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 XMPPErrorException if someone without appropriate privileges attempts to change the
     *          room subject will throw an error with code 403 (i.e. Forbidden)
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void changeSubject(final String subject) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Message message = createMessage();
    message.setSubject(subject);
    // Wait for an error or confirmation message back from the server.
    StanzaFilter responseFilter = new AndFilter(fromRoomGroupchatFilter, new StanzaFilter() {

        @Override
        public boolean accept(Stanza packet) {
            Message msg = (Message) packet;
            return subject.equals(msg.getSubject());
        }
    });
    StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, message);
    // Wait up to a certain number of seconds for a reply.
    response.nextResultOrThrow();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 10 with StanzaFilter

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

the class Workgroup method isAvailable.

/**
     * Returns true if the workgroup is available for receiving new requests. The workgroup will be
     * available only when agents are available for this workgroup.
     *
     * @return true if the workgroup is available for receiving new requests.
     * @throws XMPPErrorException 
     * @throws NoResponseException 
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public boolean isAvailable() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Presence directedPresence = new Presence(Presence.Type.available);
    directedPresence.setTo(workgroupJID);
    StanzaFilter typeFilter = new StanzaTypeFilter(Presence.class);
    StanzaFilter fromFilter = FromMatchesFilter.create(workgroupJID);
    StanzaCollector collector = connection.createStanzaCollectorAndSend(new AndFilter(fromFilter, typeFilter), directedPresence);
    Presence response = (Presence) collector.nextResultOrThrow();
    return Presence.Type.available == response.getType();
}
Also used : StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)12 StanzaCollector (org.jivesoftware.smack.StanzaCollector)5 AndFilter (org.jivesoftware.smack.filter.AndFilter)5 Stanza (org.jivesoftware.smack.packet.Stanza)4 IQReplyFilter (org.jivesoftware.smack.filter.IQReplyFilter)3 IQ (org.jivesoftware.smack.packet.IQ)3 Message (org.jivesoftware.smack.packet.Message)3 Presence (org.jivesoftware.smack.packet.Presence)3 ArrayList (java.util.ArrayList)2 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)2 StanzaListener (org.jivesoftware.smack.StanzaListener)2 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)2 Jingle (org.jivesoftware.smackx.jingleold.packet.Jingle)2 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)2 SmackException (org.jivesoftware.smack.SmackException)1 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 XMPPException (org.jivesoftware.smack.XMPPException)1 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)1 NotFilter (org.jivesoftware.smack.filter.NotFilter)1