Search in sources :

Example 1 with Captcha

use of com.xabber.android.data.extension.captcha.Captcha in project xabber-android by redsolution.

the class MessageManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (stanza.getFrom() == null) {
        return;
    }
    AccountJid account = connection.getAccount();
    final UserJid user;
    try {
        user = UserJid.from(stanza.getFrom()).getBareUserJid();
    } catch (UserJid.UserJidCreateException e) {
        return;
    }
    boolean processed = false;
    List<AbstractChat> chatsCopy = new ArrayList<>();
    chatsCopy.addAll(chats.getNested(account.toString()).values());
    for (AbstractChat chat : chatsCopy) {
        if (chat.onPacket(user, stanza, false)) {
            processed = true;
            break;
        }
    }
    final AbstractChat chat = getChat(account, user);
    if (chat != null && stanza instanceof Message) {
        if (chat.isPrivateMucChat() && !chat.isPrivateMucChatAccepted()) {
            if (mucPrivateChatRequestProvider.get(chat.getAccount(), chat.getUser()) == null) {
                mucPrivateChatRequestProvider.add(new MucPrivateChatNotification(account, user), true);
            }
        }
        return;
    }
    if (!processed && stanza instanceof Message) {
        final Message message = (Message) stanza;
        final String body = message.getBody();
        if (body == null) {
            return;
        }
        // check for spam
        if (SettingsManager.spamFilterMode() != SettingsManager.SpamFilterMode.disabled && RosterManager.getInstance().getRosterContact(account, user) == null) {
            String thread = ((Message) stanza).getThread();
            if (SettingsManager.spamFilterMode() == SettingsManager.SpamFilterMode.authCaptcha) {
                // check if this message is captcha-answer
                Captcha captcha = CaptchaManager.getInstance().getCaptcha(account, user);
                if (captcha != null) {
                    // attempt limit overhead
                    if (captcha.getAttemptCount() > CaptchaManager.CAPTCHA_MAX_ATTEMPT_COUNT) {
                        // remove this captcha
                        CaptchaManager.getInstance().removeCaptcha(account, user);
                        // discard subscription
                        try {
                            PresenceManager.getInstance().discardSubscription(account, user);
                        } catch (NetworkException e) {
                            e.printStackTrace();
                        }
                        sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_many_attempts));
                        return;
                    }
                    if (body.equals(captcha.getAnswer())) {
                        // captcha solved successfully
                        // remove this captcha
                        CaptchaManager.getInstance().removeCaptcha(account, user);
                        // show auth
                        PresenceManager.getInstance().handleSubscriptionRequest(account, user);
                        sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_correct));
                        return;
                    } else {
                        // captcha solved unsuccessfully
                        // increment attempt count
                        captcha.setAttemptCount(captcha.getAttemptCount() + 1);
                        // send warning-message
                        sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_incorrect));
                        return;
                    }
                } else {
                    // no captcha exist and user not from roster
                    sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_limit_message));
                    // and skip received message as spam
                    return;
                }
            } else {
                // if message from not-roster user
                // send a warning message to sender
                sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_limit_message));
                // and skip received message as spam
                return;
            }
        }
        if (message.getType() == Message.Type.chat && MUCManager.getInstance().hasRoom(account, user.getJid().asEntityBareJidIfPossible())) {
            try {
                createPrivateMucChat(account, user.getJid().asFullJidIfPossible()).onPacket(user, stanza, false);
            } catch (UserJid.UserJidCreateException e) {
                LogManager.exception(this, e);
            }
            mucPrivateChatRequestProvider.add(new MucPrivateChatNotification(account, user), true);
            return;
        }
        for (ExtensionElement packetExtension : message.getExtensions()) {
            if (packetExtension instanceof MUCUser) {
                return;
            }
        }
        createChat(account, user).onPacket(user, stanza, false);
    }
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) ArrayList(java.util.ArrayList) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) UserJid(com.xabber.android.data.entity.UserJid) MucPrivateChatNotification(com.xabber.android.data.message.chat.MucPrivateChatNotification) Captcha(com.xabber.android.data.extension.captcha.Captcha) AccountJid(com.xabber.android.data.entity.AccountJid) NetworkException(com.xabber.android.data.NetworkException)

Example 2 with Captcha

use of com.xabber.android.data.extension.captcha.Captcha 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

NetworkException (com.xabber.android.data.NetworkException)2 AccountJid (com.xabber.android.data.entity.AccountJid)2 UserJid (com.xabber.android.data.entity.UserJid)2 Captcha (com.xabber.android.data.extension.captcha.Captcha)2 AccountItem (com.xabber.android.data.account.AccountItem)1 MucPrivateChatNotification (com.xabber.android.data.message.chat.MucPrivateChatNotification)1 ArrayList (java.util.ArrayList)1 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)1 Message (org.jivesoftware.smack.packet.Message)1 Presence (org.jivesoftware.smack.packet.Presence)1 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)1