Search in sources :

Example 26 with ExtensionElement

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

Example 27 with ExtensionElement

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

the class ItemsExtension method toXML.

@Override
public CharSequence toXML() {
    if ((items == null) || (items.size() == 0)) {
        return super.toXML();
    } else {
        StringBuilder builder = new StringBuilder("<");
        builder.append(getElementName());
        builder.append(" node='");
        builder.append(getNode());
        if (notify != null) {
            builder.append("' ");
            builder.append(type.getElementAttribute());
            builder.append("='");
            builder.append(notify.equals(Boolean.TRUE) ? 1 : 0);
            builder.append("'>");
        } else {
            builder.append("'>");
            for (ExtensionElement item : items) {
                builder.append(item.toXML());
            }
        }
        builder.append("</");
        builder.append(getElementName());
        builder.append('>');
        return builder.toString();
    }
}
Also used : ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement)

Example 28 with ExtensionElement

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

the class RSMManager method continuePage.

Collection<ExtensionElement> continuePage(int max, Collection<ExtensionElement> returnedExtensions, Collection<ExtensionElement> additionalExtensions) {
    if (returnedExtensions == null) {
        throw new IllegalArgumentException("returnedExtensions must no be null");
    }
    if (additionalExtensions == null) {
        additionalExtensions = new LinkedList<ExtensionElement>();
    }
    RSMSet resultRsmSet = PacketUtil.extensionElementFrom(returnedExtensions, RSMSet.ELEMENT, RSMSet.NAMESPACE);
    if (resultRsmSet == null) {
        throw new IllegalArgumentException("returnedExtensions did not contain a RSMset");
    }
    RSMSet continePageRsmSet = new RSMSet(max, resultRsmSet.getLast(), PageDirection.after);
    additionalExtensions.add(continePageRsmSet);
    return additionalExtensions;
}
Also used : RSMSet(org.jivesoftware.smackx.rsm.packet.RSMSet) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement)

Example 29 with ExtensionElement

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

the class ItemValidationTest method parseEmptyTag.

@Test
public void parseEmptyTag() throws Exception {
    String itemContent = "<foo xmlns='smack:test'><bar/></foo>";
    XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>");
    Stanza message = PacketParserUtils.parseMessage(parser);
    ExtensionElement eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns());
    assertTrue(eventExt instanceof EventElement);
    EventElement event = (EventElement) eventExt;
    assertEquals(EventElementType.items, event.getEventType());
    assertEquals(1, event.getExtensions().size());
    assertTrue(event.getExtensions().get(0) instanceof ItemsExtension);
    assertEquals(1, ((ItemsExtension) event.getExtensions().get(0)).items.size());
    ExtensionElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
    assertTrue(itemExt instanceof PayloadItem<?>);
    PayloadItem<?> item = (PayloadItem<?>) itemExt;
    assertEquals("testid1", item.getId());
    assertTrue(item.getPayload() instanceof SimplePayload);
    assertXMLEqual(itemContent, ((SimplePayload) item.getPayload()).toXML().toString());
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) XmlPullParser(org.xmlpull.v1.XmlPullParser) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) Test(org.junit.Test)

Example 30 with ExtensionElement

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

the class Workgroup method handlePacket.

// PacketListener Implementation.
private void handlePacket(Stanza packet) {
    if (packet instanceof Message) {
        Message msg = (Message) packet;
        // Check to see if the user left the queue.
        ExtensionElement pe = msg.getExtension("depart-queue", "http://jabber.org/protocol/workgroup");
        ExtensionElement queueStatus = msg.getExtension("queue-status", "http://jabber.org/protocol/workgroup");
        if (pe != null) {
            fireQueueDepartedEvent();
        } else if (queueStatus != null) {
            QueueUpdate queueUpdate = (QueueUpdate) queueStatus;
            if (queueUpdate.getPosition() != -1) {
                fireQueuePositionEvent(queueUpdate.getPosition());
            }
            if (queueUpdate.getRemaingTime() != -1) {
                fireQueueTimeEvent(queueUpdate.getRemaingTime());
            }
        } else {
            // Check if a room invitation was sent and if the sender is the workgroup
            MUCUser mucUser = (MUCUser) msg.getExtension("x", "http://jabber.org/protocol/muc#user");
            MUCUser.Invite invite = mucUser != null ? mucUser.getInvite() : null;
            if (invite != null && workgroupJID.equals(invite.getFrom())) {
                String sessionID = null;
                Map<String, List<String>> metaData = null;
                pe = msg.getExtension(SessionID.ELEMENT_NAME, SessionID.NAMESPACE);
                if (pe != null) {
                    sessionID = ((SessionID) pe).getSessionID();
                }
                pe = msg.getExtension(MetaData.ELEMENT_NAME, MetaData.NAMESPACE);
                if (pe != null) {
                    metaData = ((MetaData) pe).getMetaData();
                }
                WorkgroupInvitation inv = new WorkgroupInvitation(connection.getUser(), msg.getFrom(), workgroupJID, sessionID, msg.getBody(), msg.getFrom(), metaData);
                fireInvitationEvent(inv);
            }
        }
    }
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) MetaData(org.jivesoftware.smackx.workgroup.MetaData) WorkgroupInvitation(org.jivesoftware.smackx.workgroup.WorkgroupInvitation) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) QueueUpdate(org.jivesoftware.smackx.workgroup.packet.QueueUpdate) Map(java.util.Map) SessionID(org.jivesoftware.smackx.workgroup.packet.SessionID)

Aggregations

ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)38 Message (org.jivesoftware.smack.packet.Message)12 AccountItem (com.xabber.android.data.account.AccountItem)11 Test (org.junit.Test)9 XmlPullParser (org.xmlpull.v1.XmlPullParser)9 AccountJid (com.xabber.android.data.entity.AccountJid)6 UserJid (com.xabber.android.data.entity.UserJid)4 Presence (org.jivesoftware.smack.packet.Presence)4 Stanza (org.jivesoftware.smack.packet.Stanza)4 NetworkException (com.xabber.android.data.NetworkException)3 LinkedList (java.util.LinkedList)3 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)3 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)3 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)3 MucPrivateChatNotification (com.xabber.android.data.message.chat.MucPrivateChatNotification)2 Feature (com.xabber.xmpp.ssn.Feature)2 TreeSet (java.util.TreeSet)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 AMPExtension (org.jivesoftware.smackx.amp.packet.AMPExtension)2 AMPExtensionProvider (org.jivesoftware.smackx.amp.provider.AMPExtensionProvider)2