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);
}
}
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();
}
}
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;
}
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());
}
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);
}
}
}
}
Aggregations