IqParser.java

 1package eu.siacs.conversations.parser;
 2
 3import eu.siacs.conversations.entities.Account;
 4import eu.siacs.conversations.entities.Contact;
 5import eu.siacs.conversations.services.XmppConnectionService;
 6import eu.siacs.conversations.xml.Element;
 7import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 8import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 9
10public class IqParser extends AbstractParser implements OnIqPacketReceived {
11
12	public IqParser(XmppConnectionService service) {
13		super(service);
14	}
15	
16	public void rosterItems(Account account, Element query) {
17		String version = query.getAttribute("ver");
18		if (version != null) {
19			account.getRoster().setVersion(version);
20		}
21		for (Element item : query.getChildren()) {
22			if (item.getName().equals("item")) {
23				String jid = item.getAttribute("jid");
24				String name = item.getAttribute("name");
25				String subscription = item.getAttribute("subscription");
26				Contact contact = account.getRoster().getContact(jid);
27				if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
28					contact.setServerName(name);
29				}
30				if (subscription.equals("remove")) {
31					contact.resetOption(Contact.Options.IN_ROSTER);
32					contact.resetOption(Contact.Options.DIRTY_DELETE);
33					contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
34				} else {
35					contact.setOption(Contact.Options.IN_ROSTER);
36					contact.resetOption(Contact.Options.DIRTY_PUSH);
37					contact.parseSubscriptionFromElement(item);
38				}
39			}
40		}
41		mXmppConnectionService.updateRosterUi();
42	}
43
44	@Override
45	public void onIqPacketReceived(Account account, IqPacket packet) {
46		if (packet.hasChild("query", "jabber:iq:roster")) {
47			String from = packet.getFrom();
48			if ((from == null) || (from.equals(account.getJid()))) {
49				Element query = packet.findChild("query");
50				this.rosterItems(account, query);
51			}
52		} else if (packet
53				.hasChild("open", "http://jabber.org/protocol/ibb")
54				|| packet
55						.hasChild("data", "http://jabber.org/protocol/ibb")) {
56			mXmppConnectionService.getJingleConnectionManager().deliverIbbPacket(account, packet);
57		} else if (packet.hasChild("query",
58				"http://jabber.org/protocol/disco#info")) {
59			IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(packet);
60			account.getXmppConnection().sendIqPacket(response, null);
61		} else {
62			if ((packet.getType() == IqPacket.TYPE_GET)
63					|| (packet.getType() == IqPacket.TYPE_SET)) {
64				IqPacket response = packet
65						.generateRespone(IqPacket.TYPE_ERROR);
66				Element error = response.addChild("error");
67				error.setAttribute("type", "cancel");
68				error.addChild("feature-not-implemented",
69						"urn:ietf:params:xml:ns:xmpp-stanzas");
70				account.getXmppConnection().sendIqPacket(response, null);
71			}
72		}
73	}
74
75}