Search in sources :

Example 1 with OrFilter

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

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

the class Node method addItemDeleteListener.

/**
     * Register an listener for item delete events.  This listener
     * gets called whenever an item is deleted from the node.
     * 
     * @param listener The handler for the event
     */
public void addItemDeleteListener(ItemDeleteListener listener) {
    StanzaListener delListener = new ItemDeleteTranslator(listener);
    itemDeleteToListenerMap.put(listener, delListener);
    EventContentFilter deleteItem = new EventContentFilter(EventElementType.items.toString(), "retract");
    EventContentFilter purge = new EventContentFilter(EventElementType.purge.toString());
    pubSubManager.getConnection().addSyncStanzaListener(delListener, new OrFilter(deleteItem, purge));
}
Also used : StanzaListener(org.jivesoftware.smack.StanzaListener) OrFilter(org.jivesoftware.smack.filter.OrFilter)

Example 3 with OrFilter

use of org.jivesoftware.smack.filter.OrFilter in project camel by apache.

the class XmppConsumer method doStart.

@Override
protected void doStart() throws Exception {
    try {
        connection = endpoint.createConnection();
    } catch (SmackException e) {
        if (endpoint.isTestConnectionOnStartup()) {
            throw new RuntimeException("Could not connect to XMPP server.", e);
        } else {
            LOG.warn(e.getMessage());
            if (getExceptionHandler() != null) {
                getExceptionHandler().handleException(e.getMessage(), e);
            }
            scheduleDelayedStart();
            return;
        }
    }
    chatManager = ChatManager.getInstanceFor(connection);
    chatManager.addChatListener(this);
    OrFilter pubsubPacketFilter = new OrFilter();
    if (endpoint.isPubsub()) {
        //xep-0060: pubsub#notification_type can be 'headline' or 'normal'
        pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.headline));
        pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.normal));
        connection.addPacketListener(this, pubsubPacketFilter);
    }
    if (endpoint.getRoom() == null) {
        privateChat = chatManager.getThreadChat(endpoint.getChatId());
        if (privateChat != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding listener to existing chat opened to " + privateChat.getParticipant());
            }
            privateChat.addMessageListener(this);
        } else {
            privateChat = ChatManager.getInstanceFor(connection).createChat(endpoint.getParticipant(), endpoint.getChatId(), this);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Opening private chat to " + privateChat.getParticipant());
            }
        }
    } else {
        // add the presence packet listener to the connection so we only get packets that concerns us
        // we must add the listener before creating the muc
        final AndFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class));
        connection.addPacketListener(this, packetFilter);
        muc = new MultiUserChat(connection, endpoint.resolveRoom(connection));
        muc.addMessageListener(this);
        DiscussionHistory history = new DiscussionHistory();
        // we do not want any historical messages
        history.setMaxChars(0);
        muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getDefaultPacketReplyTimeout());
        if (LOG.isInfoEnabled()) {
            LOG.info("Joined room: {} as: {}", muc.getRoom(), endpoint.getNickname());
        }
    }
    this.startRobustConnectionMonitor();
    super.doStart();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) AndFilter(org.jivesoftware.smack.filter.AndFilter) MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) DiscussionHistory(org.jivesoftware.smackx.muc.DiscussionHistory) SmackException(org.jivesoftware.smack.SmackException) Presence(org.jivesoftware.smack.packet.Presence) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) OrFilter(org.jivesoftware.smack.filter.OrFilter)

Example 4 with OrFilter

use of org.jivesoftware.smack.filter.OrFilter in project Openfire by igniterealtime.

the class XMPPSession method logIn.

/**
     * @see net.sf.kraken.session.TransportSession#logIn(net.sf.kraken.type.PresenceType, String)
     */
@Override
public void logIn(PresenceType presenceType, String verboseStatus) {
    final org.jivesoftware.smack.packet.Presence presence = new org.jivesoftware.smack.packet.Presence(org.jivesoftware.smack.packet.Presence.Type.available);
    if (JiveGlobals.getBooleanProperty("plugin.gateway." + getTransport().getType() + ".avatars", true) && getAvatar() != null) {
        Avatar avatar = getAvatar();
        // Same thing in this case, so lets go ahead and set them.
        avatar.setLegacyIdentifier(avatar.getXmppHash());
        VCardUpdateExtension ext = new VCardUpdateExtension();
        ext.setPhotoHash(avatar.getLegacyIdentifier());
        presence.addExtension(ext);
    }
    final Presence.Mode pMode = ((XMPPTransport) getTransport()).convertGatewayStatusToXMPP(presenceType);
    if (pMode != null) {
        presence.setMode(pMode);
    }
    if (verboseStatus != null && verboseStatus.trim().length() > 0) {
        presence.setStatus(verboseStatus);
    }
    setPendingPresenceAndStatus(presenceType, verboseStatus);
    if (!this.isLoggedIn()) {
        listener = new XMPPListener(this);
        presenceHandler = new XMPPPresenceHandler(this);
        runThread = new Thread() {

            @Override
            public void run() {
                String userName = generateUsername(registration.getUsername());
                conn = new XMPPConnection(config);
                try {
                    conn.getSASLAuthentication().registerSASLMechanism("DIGEST-MD5", MySASLDigestMD5Mechanism.class);
                    if (getTransport().getType().equals(TransportType.facebook) && registration.getUsername().equals("{PLATFORM}")) {
                        conn.getSASLAuthentication().registerSASLMechanism("X-FACEBOOK-PLATFORM", FacebookConnectSASLMechanism.class);
                        conn.getSASLAuthentication().supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
                    }
                    Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
                    conn.connect();
                    conn.addConnectionListener(listener);
                    try {
                        conn.addPacketListener(presenceHandler, new PacketTypeFilter(org.jivesoftware.smack.packet.Presence.class));
                        // Use this to filter out anything we don't care about
                        conn.addPacketListener(listener, new OrFilter(new PacketTypeFilter(GoogleMailBoxPacket.class), new PacketExtensionFilter(GoogleNewMailExtension.ELEMENT_NAME, GoogleNewMailExtension.NAMESPACE)));
                        conn.login(userName, registration.getPassword(), xmppResource);
                        // send initial presence.
                        conn.sendPacket(presence);
                        conn.getChatManager().addChatListener(listener);
                        conn.getRoster().addRosterListener(listener);
                        if (JiveGlobals.getBooleanProperty("plugin.gateway." + getTransport().getType() + ".avatars", !TransportType.facebook.equals(getTransport().getType())) && getAvatar() != null) {
                            new Thread() {

                                @Override
                                public void run() {
                                    Avatar avatar = getAvatar();
                                    VCard vCard = new VCard();
                                    try {
                                        vCard.load(conn);
                                        vCard.setAvatar(Base64.decode(avatar.getImageData()), avatar.getMimeType());
                                        vCard.save(conn);
                                    } catch (XMPPException e) {
                                        Log.debug("XMPP: Error while updating vcard for avatar change.", e);
                                    } catch (NotFoundException e) {
                                        Log.debug("XMPP: Unable to find avatar while setting initial.", e);
                                    }
                                }
                            }.start();
                        }
                        setLoginStatus(TransportLoginStatus.LOGGED_IN);
                        syncUsers();
                        if (getTransport().getType().equals(TransportType.gtalk) && JiveGlobals.getBooleanProperty("plugin.gateway.gtalk.mailnotifications", true)) {
                            conn.sendPacket(new IQWithPacketExtension(generateFullJID(getRegistration().getUsername()), new GoogleUserSettingExtension(null, true, null), IQ.Type.SET));
                            conn.sendPacket(new IQWithPacketExtension(generateFullJID(getRegistration().getUsername()), new GoogleMailNotifyExtension()));
                            mailCheck = new MailCheck();
                            timer.schedule(mailCheck, timerInterval, timerInterval);
                        }
                    } catch (XMPPException e) {
                        Log.debug(getTransport().getType() + " user's login/password does not appear to be correct: " + getRegistration().getUsername(), e);
                        setFailureStatus(ConnectionFailureReason.USERNAME_OR_PASSWORD_INCORRECT);
                        sessionDisconnectedNoReconnect(LocaleUtils.getLocalizedString("gateway.xmpp.passwordincorrect", "kraken"));
                    }
                } catch (XMPPException e) {
                    Log.debug(getTransport().getType() + " user is not able to connect: " + getRegistration().getUsername(), e);
                    setFailureStatus(ConnectionFailureReason.CAN_NOT_CONNECT);
                    sessionDisconnected(LocaleUtils.getLocalizedString("gateway.xmpp.connectionfailed", "kraken"));
                }
            }
        };
        runThread.start();
    }
}
Also used : FacebookConnectSASLMechanism(net.sf.kraken.protocols.xmpp.mechanisms.FacebookConnectSASLMechanism) NotFoundException(org.jivesoftware.util.NotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) GoogleMailBoxPacket(net.sf.kraken.protocols.xmpp.packet.GoogleMailBoxPacket) VCardUpdateExtension(net.sf.kraken.protocols.xmpp.packet.VCardUpdateExtension) Presence(org.jivesoftware.smack.packet.Presence) VCard(org.jivesoftware.smackx.packet.VCard) GoogleUserSettingExtension(net.sf.kraken.protocols.xmpp.packet.GoogleUserSettingExtension) MySASLDigestMD5Mechanism(net.sf.kraken.protocols.xmpp.mechanisms.MySASLDigestMD5Mechanism) GoogleMailNotifyExtension(net.sf.kraken.protocols.xmpp.packet.GoogleMailNotifyExtension) Presence(org.jivesoftware.smack.packet.Presence) org.jivesoftware.smack(org.jivesoftware.smack) IQWithPacketExtension(net.sf.kraken.protocols.xmpp.packet.IQWithPacketExtension) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) OrFilter(org.jivesoftware.smack.filter.OrFilter) Avatar(net.sf.kraken.avatars.Avatar) PacketExtensionFilter(org.jivesoftware.smack.filter.PacketExtensionFilter)

Aggregations

OrFilter (org.jivesoftware.smack.filter.OrFilter)4 Presence (org.jivesoftware.smack.packet.Presence)3 AndFilter (org.jivesoftware.smack.filter.AndFilter)2 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)2 Avatar (net.sf.kraken.avatars.Avatar)1 FacebookConnectSASLMechanism (net.sf.kraken.protocols.xmpp.mechanisms.FacebookConnectSASLMechanism)1 MySASLDigestMD5Mechanism (net.sf.kraken.protocols.xmpp.mechanisms.MySASLDigestMD5Mechanism)1 GoogleMailBoxPacket (net.sf.kraken.protocols.xmpp.packet.GoogleMailBoxPacket)1 GoogleMailNotifyExtension (net.sf.kraken.protocols.xmpp.packet.GoogleMailNotifyExtension)1 GoogleUserSettingExtension (net.sf.kraken.protocols.xmpp.packet.GoogleUserSettingExtension)1 IQWithPacketExtension (net.sf.kraken.protocols.xmpp.packet.IQWithPacketExtension)1 VCardUpdateExtension (net.sf.kraken.protocols.xmpp.packet.VCardUpdateExtension)1 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)1 org.jivesoftware.smack (org.jivesoftware.smack)1 SmackException (org.jivesoftware.smack.SmackException)1 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 StanzaListener (org.jivesoftware.smack.StanzaListener)1 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)1 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)1