Search in sources :

Example 71 with Presence

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

the class MessageTest method testHighestShow.

/**
     * User0 is connected from 2 resources. User0 is available in both resources
     * but with different show values. User1 sends a message to the
     * bare JID of User0. Check that the resource with highest show value will get
     * the messages.
     *
     * @throws Exception if an error occurs.
     */
public void testHighestShow() throws Exception {
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
    conn3.connect();
    conn3.login(getUsername(0), getPassword(0), "Home");
    // Set this connection as highest priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.away);
    conn3.sendStanza(presence);
    // Set this connection as highest priority
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen in both connected clients
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    StanzaCollector coll3 = conn3.createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    // User1 sends a message to the bare JID of User0
    Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
    chat.sendMessage("Test 1");
    chat.sendMessage("Test 2");
    // Check that messages were sent to resource with highest priority
    Message message = (Message) coll3.nextResult(2000);
    assertNull("Resource with lowest show value got the message", message);
    message = (Message) collector.nextResult(2000);
    assertNotNull(message);
    assertEquals("Test 1", message.getBody());
    message = (Message) collector.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
    conn3.disconnect();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 72 with Presence

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

the class SmackServiceNode method searchServices.

private static MappedNodes searchServices(final ConcurrentHashMap<String, String> visited, final XMPPConnection xmppConnection, final int maxEntries, final int maxDepth, final int maxSearchNodes, final String protocol, final boolean searchBuddies, final MappedNodes mappedNodes) {
    if (xmppConnection == null || !xmppConnection.isConnected()) {
        return null;
    }
    searchDiscoItems(xmppConnection, maxEntries, xmppConnection.getServiceName(), mappedNodes, maxDepth - 1, maxSearchNodes, protocol, visited);
    // Request to Server
    deepSearch(xmppConnection, maxEntries, xmppConnection.getHost(), mappedNodes, maxDepth - 1, maxSearchNodes, protocol, visited);
    // Request to Buddies
    if (xmppConnection.getRoster() != null && searchBuddies) {
        for (final RosterEntry re : xmppConnection.getRoster().getEntries()) {
            for (final Iterator<Presence> i = xmppConnection.getRoster().getPresences(re.getUser()); i.hasNext(); ) {
                final Presence presence = i.next();
                if (presence.isAvailable()) {
                    deepSearch(xmppConnection, maxEntries, presence.getFrom(), mappedNodes, maxDepth - 1, maxSearchNodes, protocol, visited);
                }
            }
        }
    }
    return mappedNodes;
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 73 with Presence

use of org.jivesoftware.smack.packet.Presence 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)

Example 74 with Presence

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

the class XMPPSession method constructCurrentLegacyPresencePacket.

/**
     * Returns a (legacy/Smack-based) Presence stanza that represents the
     * current presence of this session. The Presence includes relevant Mode,
     * Status and VCardUpdate information.
     * 
     * This method uses the fields {@link TransportSession#presence} and
     * {@link TransportSession#verboseStatus} to generate the result.
     * 
     * @return A Presence packet representing the current presence state of this
     *         session.
     */
public Presence constructCurrentLegacyPresencePacket() {
    final org.jivesoftware.smack.packet.Presence presence = new org.jivesoftware.smack.packet.Presence(org.jivesoftware.smack.packet.Presence.Type.available);
    final Presence.Mode pMode = ((XMPPTransport) getTransport()).convertGatewayStatusToXMPP(this.presence);
    if (pMode != null) {
        presence.setMode(pMode);
    }
    if (verboseStatus != null && verboseStatus.trim().length() > 0) {
        presence.setStatus(verboseStatus);
    }
    final Avatar avatar = getAvatar();
    if (avatar != null) {
        final VCardUpdateExtension ext = new VCardUpdateExtension();
        ext.setPhotoHash(avatar.getLegacyIdentifier());
        presence.addExtension(ext);
    }
    return presence;
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) VCardUpdateExtension(net.sf.kraken.protocols.xmpp.packet.VCardUpdateExtension) org.jivesoftware.smack(org.jivesoftware.smack) Presence(org.jivesoftware.smack.packet.Presence) Avatar(net.sf.kraken.avatars.Avatar)

Example 75 with Presence

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

the class OfflineMessageManagerTest method testFetchAndPurge.

/**
     * While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
     * performs some "Flexible Offline Message Retrieval" by fetching all the offline messages
     * and then removing all the offline messages.
     */
public void testFetchAndPurge() {
    // Make user2 unavailable
    getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
    try {
        Thread.sleep(500);
        // User1 sends some messages to User2 which is not available at the moment
        Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
        chat.sendMessage("Test 1");
        chat.sendMessage("Test 2");
        Thread.sleep(500);
        // User2 checks the number of offline messages
        OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
        assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
        // Get all offline messages
        Iterator<Message> messages = offlineManager.getMessages();
        assertTrue("No message was found", messages.hasNext());
        List<String> stamps = new ArrayList<String>();
        while (messages.hasNext()) {
            Message message = messages.next();
            OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", "http://jabber.org/protocol/offline");
            assertNotNull("No offline information was included in the offline message", info);
            assertNotNull("No stamp was found in the message header", info.getNode());
            stamps.add(info.getNode());
        }
        assertEquals("Wrong number of messages", 2, stamps.size());
        // Check that the offline messages have not been deleted
        assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
        // User2 becomes available again
        StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
        getConnection(1).sendStanza(new Presence(Presence.Type.available));
        // Check that no offline messages was sent to the user
        Message message = (Message) collector.nextResult(2500);
        assertNull("An offline message was sent from the server", message);
        // Delete all offline messages
        offlineManager.deleteMessages();
        // Check that there are no offline message for this user
        assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) Chat(org.jivesoftware.smack.Chat) ArrayList(java.util.ArrayList) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector) XMPPException(org.jivesoftware.smack.XMPPException)

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