diff options
author | Daniel Gultsch <daniel@gultsch.de> | 2014-12-22 15:00:51 +0100 |
---|---|---|
committer | Daniel Gultsch <daniel@gultsch.de> | 2014-12-22 15:00:51 +0100 |
commit | a94663aaa4d52ba27ada76abe55d2d113304b4e0 (patch) | |
tree | 38e73cc693fef7462ecabffd984348a86b768ae3 /src/main/java/eu/siacs/conversations/parser | |
parent | 96569700512e9618efa18eeb277665045678e16d (diff) | |
parent | af7a64491fa5c514866d0f1952c034a7672bb508 (diff) |
Merge pull request #796 from SamWhited/xep0191
XEP-0191 Support
Diffstat (limited to 'src/main/java/eu/siacs/conversations/parser')
-rw-r--r-- | src/main/java/eu/siacs/conversations/parser/AbstractParser.java | 8 | ||||
-rw-r--r-- | src/main/java/eu/siacs/conversations/parser/IqParser.java | 107 |
2 files changed, 91 insertions, 24 deletions
diff --git a/src/main/java/eu/siacs/conversations/parser/AbstractParser.java b/src/main/java/eu/siacs/conversations/parser/AbstractParser.java index 39cbff4f..8afc2ae0 100644 --- a/src/main/java/eu/siacs/conversations/parser/AbstractParser.java +++ b/src/main/java/eu/siacs/conversations/parser/AbstractParser.java @@ -53,10 +53,10 @@ public abstract class AbstractParser { protected void updateLastseen(final Element packet, final Account account, final boolean presenceOverwrite) { - Jid from = packet.getAttributeAsJid("from"); - String presence = from == null || from.isBareJid() ? "" : from.getResourcepart(); - Contact contact = account.getRoster().getContact(from); - long timestamp = getTimestamp(packet); + final Jid from = packet.getAttributeAsJid("from"); + final String presence = from == null || from.isBareJid() ? "" : from.getResourcepart(); + final Contact contact = account.getRoster().getContact(from); + final long timestamp = getTimestamp(packet); if (timestamp >= contact.lastseen.time) { contact.lastseen.time = timestamp; if (!presence.isEmpty() && presenceOverwrite) { diff --git a/src/main/java/eu/siacs/conversations/parser/IqParser.java b/src/main/java/eu/siacs/conversations/parser/IqParser.java index b583a1a1..b77d460d 100644 --- a/src/main/java/eu/siacs/conversations/parser/IqParser.java +++ b/src/main/java/eu/siacs/conversations/parser/IqParser.java @@ -2,36 +2,40 @@ package eu.siacs.conversations.parser; import android.util.Log; +import java.util.ArrayList; +import java.util.Collection; + import eu.siacs.conversations.Config; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.services.XmppConnectionService; +import eu.siacs.conversations.utils.Xmlns; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.OnIqPacketReceived; -import eu.siacs.conversations.xmpp.jid.InvalidJidException; +import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.stanzas.IqPacket; public class IqParser extends AbstractParser implements OnIqPacketReceived { - public IqParser(XmppConnectionService service) { + public IqParser(final XmppConnectionService service) { super(service); } - public void rosterItems(Account account, Element query) { - String version = query.getAttribute("ver"); + public void rosterItems(final Account account, final Element query) { + final String version = query.getAttribute("ver"); if (version != null) { account.getRoster().setVersion(version); } - for (Element item : query.getChildren()) { + for (final Element item : query.getChildren()) { if (item.getName().equals("item")) { final Jid jid = item.getAttributeAsJid("jid"); if (jid == null) { continue; } - String name = item.getAttribute("name"); - String subscription = item.getAttribute("subscription"); - Contact contact = account.getRoster().getContact(jid); + final String name = item.getAttribute("name"); + final String subscription = item.getAttribute("subscription"); + final Contact contact = account.getRoster().getContact(jid); if (!contact.getOption(Contact.Options.DIRTY_PUSH)) { contact.setServerName(name); contact.parseGroupsFromElement(item); @@ -54,13 +58,13 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { mXmppConnectionService.updateRosterUi(); } - public String avatarData(IqPacket packet) { - Element pubsub = packet.findChild("pubsub", + public String avatarData(final IqPacket packet) { + final Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub"); if (pubsub == null) { return null; } - Element items = pubsub.findChild("items"); + final Element items = pubsub.findChild("items"); if (items == null) { return null; } @@ -68,13 +72,76 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { } @Override - public void onIqPacketReceived(Account account, IqPacket packet) { + public void onIqPacketReceived(final Account account, final IqPacket packet) { if (packet.hasChild("query", "jabber:iq:roster")) { final Jid from = packet.getFrom(); if ((from == null) || (from.equals(account.getJid().toBareJid()))) { - Element query = packet.findChild("query"); + final Element query = packet.findChild("query"); this.rosterItems(account, query); } + } else if (packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) { + // Only accept block list changes from the server. + // The server should probably prevent other people from faking a blocklist push, + // but just in case let's prevent it client side as well. + final Jid from = packet.getFrom(); + if (from == null || from.equals(account.getServer()) || from.equals(account.getJid().toBareJid())) { + Log.d(Config.LOGTAG, "Received blocklist update from server"); + final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING); + final Element block = packet.findChild("block", Xmlns.BLOCKING); + final Collection<Element> items = blocklist != null ? blocklist.getChildren() : + (block != null ? block.getChildren() : null); + // If this is a response to a blocklist query, clear the block list and replace with the new one. + // Otherwise, just update the existing blocklist. + if (packet.getType() == IqPacket.TYPE_RESULT) { + account.clearBlocklist(); + } + if (items != null) { + final Collection<Jid> jids = new ArrayList<>(items.size()); + // Create a collection of Jids from the packet + for (final Element item : items) { + if (item.getName().equals("item")) { + final Jid jid = item.getAttributeAsJid("jid"); + if (jid != null) { + jids.add(jid); + } + } + } + account.getBlocklist().addAll(jids); + } + // Update the UI + mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED); + } else { + Log.d(Config.LOGTAG, "Received blocklist update from invalid jid: " + from.toString()); + } + } else if (packet.hasChild("unblock", Xmlns.BLOCKING)) { + final Jid from = packet.getFrom(); + if ((from == null || from.equals(account.getServer()) || from.equals(account.getJid().toBareJid())) && + packet.getType() == IqPacket.TYPE_SET) { + Log.d(Config.LOGTAG, "Received unblock update from server"); + final Collection<Element> items = packet.getChildren().get(0).getChildren(); + if (items.size() == 0) { + // No children to unblock == unblock all + account.getBlocklist().clear(); + } else { + final Collection<Jid> jids = new ArrayList<>(items.size()); + for (final Element item : items) { + if (item.getName().equals("item")) { + final Jid jid = item.getAttributeAsJid("jid"); + if (jid != null) { + jids.add(jid); + } + } + } + account.getBlocklist().removeAll(jids); + mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED); + } + } else { + if (packet.getType() == IqPacket.TYPE_SET) { + Log.d(Config.LOGTAG, "Received unblock update from invalid jid " + from.toString()); + } else { + Log.d(Config.LOGTAG, "Received unblock update with invalid type " + packet.getType()); + } + } } else { if (packet.getFrom() == null) { Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": received iq with invalid from "+packet.toString()); @@ -82,24 +149,24 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { } else if (packet.hasChild("open", "http://jabber.org/protocol/ibb") || packet.hasChild("data", "http://jabber.org/protocol/ibb")) { mXmppConnectionService.getJingleConnectionManager() - .deliverIbbPacket(account, packet); + .deliverIbbPacket(account, packet); } else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) { - IqPacket response = mXmppConnectionService.getIqGenerator() - .discoResponse(packet); + final IqPacket response = mXmppConnectionService.getIqGenerator() + .discoResponse(packet); account.getXmppConnection().sendIqPacket(response, null); } else if (packet.hasChild("ping", "urn:xmpp:ping")) { - IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT); + final IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT); mXmppConnectionService.sendIqPacket(account, response, null); } else { if ((packet.getType() == IqPacket.TYPE_GET) || (packet.getType() == IqPacket.TYPE_SET)) { - IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR); - Element error = response.addChild("error"); + final IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR); + final Element error = response.addChild("error"); error.setAttribute("type", "cancel"); error.addChild("feature-not-implemented", "urn:ietf:params:xml:ns:xmpp-stanzas"); account.getXmppConnection().sendIqPacket(response, null); - } + } } } } |