Search in sources :

Example 51 with Presence

use of org.jivesoftware.smack.packet.Presence in project Essentials by drtshock.

the class XMPPManager method connect.

private boolean connect() {
    final String server = config.getString("xmpp.server");
    if (server == null || server.equals("example.com")) {
        LOGGER.log(Level.WARNING, "config broken for xmpp");
        return false;
    }
    final int port = config.getInt("xmpp.port", 5222);
    final String serviceName = config.getString("xmpp.servicename", server);
    final String xmppuser = config.getString("xmpp.user");
    final String password = config.getString("xmpp.password");
    final ConnectionConfiguration connConf = new ConnectionConfiguration(server, port, serviceName);
    final StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("Connecting to xmpp server ").append(server).append(":").append(port);
    stringBuilder.append(" as user ").append(xmppuser).append(".");
    LOGGER.log(Level.INFO, stringBuilder.toString());
    connConf.setSASLAuthenticationEnabled(config.getBoolean("xmpp.sasl-enabled", false));
    connConf.setSendPresence(true);
    connConf.setReconnectionAllowed(true);
    connConf.setDebuggerEnabled(config.getBoolean("debug", false));
    connection = new XMPPConnection(connConf);
    try {
        connection.connect();
        connection.login(xmppuser, password, "Essentials-XMPP");
        connection.sendPacket(new Presence(Presence.Type.available, "No one online.", 2, Presence.Mode.available));
        connection.getRoster().setSubscriptionMode(SubscriptionMode.accept_all);
        chatManager = connection.getChatManager();
        chatManager.addChatListener(this);
        return true;
    } catch (XMPPException ex) {
        LOGGER.log(Level.WARNING, "Failed to connect to server: " + server, ex);
        return false;
    }
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 52 with Presence

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

the class PresenceManager method getStatusText.

public String getStatusText(AccountJid account, UserJid bareAddress) {
    final Occupant occupant = getOccupant(account, bareAddress);
    if (occupant != null) {
        return occupant.getStatusText();
    }
    final Presence presence = RosterManager.getInstance().getPresence(account, bareAddress);
    if (presence == null) {
        return null;
    } else {
        return presence.getStatus();
    }
}
Also used : Occupant(com.xabber.android.data.extension.muc.Occupant) Presence(org.jivesoftware.smack.packet.Presence)

Example 53 with Presence

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

the class AvatarManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (!(stanza instanceof Presence)) {
        return;
    }
    AccountJid account = ((AccountItem) connection).getAccount();
    Presence presence = (Presence) stanza;
    if (presence.getType() == Presence.Type.error) {
        return;
    }
    for (ExtensionElement packetExtension : presence.getExtensions()) {
        if (packetExtension instanceof VCardUpdate) {
            VCardUpdate vCardUpdate = (VCardUpdate) packetExtension;
            if (vCardUpdate.isValid() && vCardUpdate.isPhotoReady()) {
                try {
                    onPhotoReady(account, UserJid.from(stanza.getFrom()), vCardUpdate);
                } catch (UserJid.UserJidCreateException e) {
                    LogManager.exception(this, e);
                }
            }
        }
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) Presence(org.jivesoftware.smack.packet.Presence) UserJid(com.xabber.android.data.entity.UserJid) VCardUpdate(com.xabber.xmpp.vcardupdate.VCardUpdate)

Example 54 with Presence

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

the class ChatStateManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (stanza.getFrom() == null) {
        return;
    }
    final Resourcepart resource = stanza.getFrom().getResourceOrNull();
    if (resource == null) {
        return;
    }
    final AccountJid account = ((AccountItem) connection).getAccount();
    final UserJid bareUserJid;
    try {
        bareUserJid = UserJid.from(stanza.getFrom()).getBareUserJid();
    } catch (UserJid.UserJidCreateException e) {
        return;
    }
    if (stanza instanceof Presence) {
        Presence presence = (Presence) stanza;
        if (presence.getType() != Type.unavailable) {
            return;
        }
        chatStates.remove(account.toString(), bareUserJid.toString(), resource);
        removeCallback(account, bareUserJid.getBareJid(), resource);
        supports.remove(account.toString(), bareUserJid.toString(), resource);
    } else if (stanza instanceof Message) {
        boolean support = false;
        for (ExtensionElement extension : stanza.getExtensions()) if (extension instanceof ChatStateExtension) {
            removeCallback(account, bareUserJid.getBareJid(), resource);
            ChatState chatState = ((ChatStateExtension) extension).getChatState();
            chatStates.put(account.toString(), bareUserJid.toString(), resource, chatState);
            if (chatState != ChatState.active) {
                Runnable runnable = new Runnable() {

                    @Override
                    public void run() {
                        if (this != stateCleaners.get(account.toString(), bareUserJid.toString(), resource)) {
                            return;
                        }
                        chatStates.remove(account.toString(), bareUserJid.toString(), resource);
                        removeCallback(account, bareUserJid.getBareJid(), resource);
                        RosterManager.onChatStateChanged(account, bareUserJid);
                    }
                };
                handler.postDelayed(runnable, REMOVE_STATE_DELAY);
                stateCleaners.put(account.toString(), bareUserJid.toString(), resource, runnable);
            }
            RosterManager.onChatStateChanged(account, bareUserJid);
            support = true;
            break;
        }
        Message message = (Message) stanza;
        if (message.getType() != Message.Type.chat && message.getType() != Message.Type.groupchat) {
            return;
        }
        if (support) {
            supports.put(account.toString(), bareUserJid.toString(), resource, true);
        } else if (supports.get(account.toString(), bareUserJid.toString(), resource) == null) {
            // Disable only if there no information about support.
            supports.put(account.toString(), bareUserJid.toString(), resource, false);
        }
    }
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState) Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension) AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) UserJid(com.xabber.android.data.entity.UserJid) Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 55 with Presence

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

the class VCardManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (!(connection instanceof AccountItem)) {
        return;
    }
    AccountJid account = connection.getAccount();
    if (stanza instanceof Presence && ((Presence) stanza).getType() != Presence.Type.error) {
        Jid from = stanza.getFrom();
        if (from == null) {
            return;
        }
        Jid addressForVcard = from;
        if (MUCManager.getInstance().hasRoom(account, from.asEntityBareJidIfPossible())) {
            addressForVcard = from;
        }
        // Request vCard for new users
        if (!names.containsKey(addressForVcard)) {
            if (SettingsManager.connectionLoadVCard()) {
                request(account, addressForVcard);
            }
        }
    }
}
Also used : UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) 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