IqParser.java

  1package eu.siacs.conversations.parser;
  2
  3import android.util.Log;
  4
  5import java.util.ArrayList;
  6import java.util.Collection;
  7
  8import eu.siacs.conversations.Config;
  9import eu.siacs.conversations.entities.Account;
 10import eu.siacs.conversations.entities.Contact;
 11import eu.siacs.conversations.services.XmppConnectionService;
 12import eu.siacs.conversations.utils.Xmlns;
 13import eu.siacs.conversations.xml.Element;
 14import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 15import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
 16import eu.siacs.conversations.xmpp.jid.Jid;
 17import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 18
 19public class IqParser extends AbstractParser implements OnIqPacketReceived {
 20
 21	public IqParser(final XmppConnectionService service) {
 22		super(service);
 23	}
 24
 25	private void rosterItems(final Account account, final Element query) {
 26		final String version = query.getAttribute("ver");
 27		if (version != null) {
 28			account.getRoster().setVersion(version);
 29		}
 30		for (final Element item : query.getChildren()) {
 31			if (item.getName().equals("item")) {
 32				final Jid jid = item.getAttributeAsJid("jid");
 33				if (jid == null) {
 34					continue;
 35				}
 36				final String name = item.getAttribute("name");
 37				final String subscription = item.getAttribute("subscription");
 38				final Contact contact = account.getRoster().getContact(jid);
 39				if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
 40					contact.setServerName(name);
 41					contact.parseGroupsFromElement(item);
 42				}
 43				if (subscription != null) {
 44					if (subscription.equals("remove")) {
 45						contact.resetOption(Contact.Options.IN_ROSTER);
 46						contact.resetOption(Contact.Options.DIRTY_DELETE);
 47						contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
 48					} else {
 49						contact.setOption(Contact.Options.IN_ROSTER);
 50						contact.resetOption(Contact.Options.DIRTY_PUSH);
 51						contact.parseSubscriptionFromElement(item);
 52					}
 53				}
 54				mXmppConnectionService.getAvatarService().clear(contact);
 55			}
 56		}
 57		mXmppConnectionService.updateConversationUi();
 58		mXmppConnectionService.updateRosterUi();
 59	}
 60
 61	public String avatarData(final IqPacket packet) {
 62		final Element pubsub = packet.findChild("pubsub",
 63				"http://jabber.org/protocol/pubsub");
 64		if (pubsub == null) {
 65			return null;
 66		}
 67		final Element items = pubsub.findChild("items");
 68		if (items == null) {
 69			return null;
 70		}
 71		return super.avatarData(items);
 72	}
 73
 74	public static boolean fromServer(final Account account, final IqPacket packet) {
 75		return packet.getFrom() == null || packet.getFrom().equals(account.getServer()) || packet.getFrom().equals(account.getJid().toBareJid());
 76	}
 77
 78	@Override
 79	public void onIqPacketReceived(final Account account, final IqPacket packet) {
 80		if (packet.hasChild("query", Xmlns.ROSTER) && fromServer(account, packet)) {
 81			final Element query = packet.findChild("query");
 82			// If this is in response to a query for the whole roster:
 83			if (packet.getType() == IqPacket.TYPE_RESULT) {
 84				account.getRoster().markAllAsNotInRoster();
 85			}
 86			this.rosterItems(account, query);
 87		} else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) &&
 88				fromServer(account, packet)) {
 89			// Block list or block push.
 90			Log.d(Config.LOGTAG, "Received blocklist update from server");
 91			final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING);
 92			final Element block = packet.findChild("block", Xmlns.BLOCKING);
 93			final Collection<Element> items = blocklist != null ? blocklist.getChildren() :
 94				(block != null ? block.getChildren() : null);
 95			// If this is a response to a blocklist query, clear the block list and replace with the new one.
 96			// Otherwise, just update the existing blocklist.
 97			if (packet.getType() == IqPacket.TYPE_RESULT) {
 98				account.clearBlocklist();
 99			}
100			if (items != null) {
101				final Collection<Jid> jids = new ArrayList<>(items.size());
102				// Create a collection of Jids from the packet
103				for (final Element item : items) {
104					if (item.getName().equals("item")) {
105						final Jid jid = item.getAttributeAsJid("jid");
106						if (jid != null) {
107							jids.add(jid);
108						}
109					}
110				}
111				account.getBlocklist().addAll(jids);
112			}
113			// Update the UI
114			mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
115		} else if (packet.hasChild("unblock", Xmlns.BLOCKING) &&
116				fromServer(account, packet) && packet.getType() == IqPacket.TYPE_SET) {
117			Log.d(Config.LOGTAG, "Received unblock update from server");
118			final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
119			if (items.size() == 0) {
120				// No children to unblock == unblock all
121				account.getBlocklist().clear();
122			} else {
123				final Collection<Jid> jids = new ArrayList<>(items.size());
124				for (final Element item : items) {
125					if (item.getName().equals("item")) {
126						final Jid jid = item.getAttributeAsJid("jid");
127						if (jid != null) {
128							jids.add(jid);
129						}
130					}
131				}
132				account.getBlocklist().removeAll(jids);
133			}
134			mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
135		} else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
136				|| packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
137			mXmppConnectionService.getJingleConnectionManager()
138				.deliverIbbPacket(account, packet);
139		} else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
140			final IqPacket response = mXmppConnectionService.getIqGenerator()
141				.discoResponse(packet);
142			account.getXmppConnection().sendIqPacket(response, null);
143		} else if (packet.hasChild("ping", "urn:xmpp:ping")) {
144			final IqPacket response = packet.generateResponse(IqPacket.TYPE_RESULT);
145			mXmppConnectionService.sendIqPacket(account, response, null);
146		} else {
147			if ((packet.getType() == IqPacket.TYPE_GET)
148					|| (packet.getType() == IqPacket.TYPE_SET)) {
149				final IqPacket response = packet.generateResponse(IqPacket.TYPE_ERROR);
150				final Element error = response.addChild("error");
151				error.setAttribute("type", "cancel");
152				error.addChild("feature-not-implemented",
153						"urn:ietf:params:xml:ns:xmpp-stanzas");
154				account.getXmppConnection().sendIqPacket(response, null);
155					}
156		}
157	}
158
159}