Search in sources :

Example 1 with MucPrivateChatNotification

use of com.xabber.android.data.message.chat.MucPrivateChatNotification in project xabber-android by redsolution.

the class MessageManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem)) {
        return;
    }
    String account = ((AccountItem) connection).getAccount();
    if (bareAddress == null) {
        return;
    }
    if (packet instanceof Message && MessageArchiveManager.getInstance().isModificationsSucceed(account) && Delay.isOfflineMessage(Jid.getServer(account), packet)) {
        // archive have been received.
        return;
    }
    String contact = bareAddress;
    if (packet instanceof Message) {
        Message message = (Message) packet;
        if (MUCManager.getInstance().hasRoom(account, bareAddress) && message.getType() != Message.Type.groupchat) {
            contact = packet.getFrom();
        }
    }
    final String user = packet.getFrom();
    boolean processed = false;
    for (AbstractChat chat : chats.getNested(account).values()) {
        if (chat.onPacket(contact, packet)) {
            processed = true;
            break;
        }
    }
    final AbstractChat chat = getChat(account, user);
    if (chat != null && packet instanceof Message) {
        if (chat.isPrivateMucChat() && !chat.isPrivateMucChatAccepted()) {
            if (mucPrivateChatRequestProvider.get(chat.getAccount(), chat.getUser()) == null) {
                if (!PrivateMucChatBlockingManager.getInstance().getBlockedContacts(account).contains(chat.getUser())) {
                    mucPrivateChatRequestProvider.add(new MucPrivateChatNotification(account, user), true);
                }
            }
        }
        return;
    }
    if (!processed && packet instanceof Message) {
        final Message message = (Message) packet;
        final String body = message.getBody();
        if (body == null) {
            return;
        }
        if (message.getType() == Message.Type.chat && MUCManager.getInstance().hasRoom(account, Jid.getBareAddress(user))) {
            createPrivateMucChat(account, user).onPacket(contact, packet);
            if (!PrivateMucChatBlockingManager.getInstance().getBlockedContacts(account).contains(user)) {
                mucPrivateChatRequestProvider.add(new MucPrivateChatNotification(account, user), true);
            }
            return;
        }
        for (ExtensionElement packetExtension : message.getExtensions()) {
            if (packetExtension instanceof MUCUser) {
                return;
            }
        }
        createChat(account, user).onPacket(contact, packet);
    }
}
Also used : MucPrivateChatNotification(com.xabber.android.data.message.chat.MucPrivateChatNotification) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) AccountItem(com.xabber.android.data.account.AccountItem) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement)

Example 2 with MucPrivateChatNotification

use of com.xabber.android.data.message.chat.MucPrivateChatNotification 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;
    for (AbstractChat chat : chats.getNested(account.toString()).values()) {
        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 : MucPrivateChatNotification(com.xabber.android.data.message.chat.MucPrivateChatNotification) Captcha(com.xabber.android.data.extension.captcha.Captcha) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) AccountJid(com.xabber.android.data.entity.AccountJid) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) UserJid(com.xabber.android.data.entity.UserJid) NetworkException(com.xabber.android.data.NetworkException)

Aggregations

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