Search in sources :

Example 61 with Presence

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

the class AbstractContact method getClientSoftware.

public ClientSoftware getClientSoftware() {
    final Presence presence = RosterManager.getInstance().getPresence(account, user);
    if (presence == null || !presence.isAvailable()) {
        return ClientSoftware.unknown;
    }
    ClientInfo clientInfo = CapabilitiesManager.getInstance().getCachedClientInfo(presence.getFrom());
    if (clientInfo == null) {
        return ClientSoftware.unknown;
    } else {
        return clientInfo.getClientSoftware();
    }
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) ClientInfo(com.xabber.android.data.extension.capability.ClientInfo)

Example 62 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.
 *
 * @throws NetworkException
 */
public void requestSubscription(AccountJid account, UserJid user) throws NetworkException {
    Presence packet = new Presence(Presence.Type.subscribe);
    packet.setTo(user.getJid());
    StanzaSender.sendStanza(account, packet);
    Set<UserJid> set = requestedSubscriptions.get(account);
    if (set == null) {
        set = new HashSet<>();
        requestedSubscriptions.put(account, set);
    }
    set.add(user);
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) UserJid(com.xabber.android.data.entity.UserJid)

Example 63 with Presence

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

the class PresenceManager method acceptSubscription.

/**
 * Accepts subscription request from the entity (share own presence).
 */
public void acceptSubscription(AccountJid account, UserJid user) throws NetworkException {
    Presence packet = new Presence(Presence.Type.subscribed);
    packet.setTo(user.getJid());
    StanzaSender.sendStanza(account, packet);
    subscriptionRequestProvider.remove(account, user);
    removeRequestedSubscription(account, user);
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 64 with Presence

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

the class PresenceManager method discardSubscription.

/**
 * Discards subscription request from the entity (deny own presence
 * sharing).
 */
public void discardSubscription(AccountJid account, UserJid user) throws NetworkException {
    Presence packet = new Presence(Presence.Type.unsubscribed);
    packet.setTo(user.getJid());
    StanzaSender.sendStanza(account, packet);
    subscriptionRequestProvider.remove(account, user);
    removeRequestedSubscription(account, user);
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 65 with Presence

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

the class PresenceManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (!(connection instanceof AccountItem)) {
        return;
    }
    if (!(stanza instanceof Presence)) {
        return;
    }
    Presence presence = (Presence) stanza;
    UserJid from;
    try {
        from = UserJid.from(stanza.getFrom());
    } catch (UserJid.UserJidCreateException e) {
        LogManager.exception(this, e);
        return;
    }
    if (presence.getType() == Presence.Type.subscribe) {
        AccountJid account = connection.getAccount();
        // reject all subscribe-requests
        if (SettingsManager.spamFilterMode() == SettingsManager.SpamFilterMode.noAuth) {
            // send a warning message to sender
            MessageManager.getInstance().sendMessageWithoutChat(from.getJid(), StringUtils.randomString(12), account, Application.getInstance().getResources().getString(R.string.spam_filter_ban_subscription));
            // and discard subscription
            try {
                discardSubscription(account, UserJid.from(from.toString()));
            } catch (NetworkException | UserJid.UserJidCreateException e) {
                e.printStackTrace();
            }
            return;
        }
        // require captcha for subscription
        if (SettingsManager.spamFilterMode() == SettingsManager.SpamFilterMode.authCaptcha) {
            Captcha captcha = CaptchaManager.getInstance().getCaptcha(account, from);
            // if captcha for this user already exist, check expires time and discard if need
            if (captcha != null) {
                if (captcha.getExpiresDate() < System.currentTimeMillis()) {
                    // discard subscription
                    try {
                        discardSubscription(account, UserJid.from(from.toString()));
                    } catch (NetworkException | UserJid.UserJidCreateException e) {
                        e.printStackTrace();
                    }
                    return;
                }
                // skip subscription, waiting for captcha in messageManager
                return;
            } else {
                // generate captcha
                String captchaQuestion = CaptchaManager.getInstance().generateAndSaveCaptcha(account, from);
                // send captcha message to sender
                MessageManager.getInstance().sendMessageWithoutChat(from.getJid(), StringUtils.randomString(12), account, Application.getInstance().getResources().getString(R.string.spam_filter_limit_subscription) + " " + captchaQuestion);
                // and skip subscription, waiting for captcha in messageManager
                return;
            }
        }
        // subscription request
        handleSubscriptionRequest(account, from);
    }
}
Also used : Captcha(com.xabber.android.data.extension.captcha.Captcha) AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) Presence(org.jivesoftware.smack.packet.Presence) UserJid(com.xabber.android.data.entity.UserJid) NetworkException(com.xabber.android.data.NetworkException)

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