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 != null) {
31					if (subscription.equals("remove")) {
32						contact.resetOption(Contact.Options.IN_ROSTER);
33						contact.resetOption(Contact.Options.DIRTY_DELETE);
34						contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
35					} else {
36						contact.setOption(Contact.Options.IN_ROSTER);
37						contact.resetOption(Contact.Options.DIRTY_PUSH);
38						contact.parseSubscriptionFromElement(item);
39					}
40				}
41			}
42		}
43		mXmppConnectionService.updateRosterUi();
44	}
45
46	public String avatarData(IqPacket packet) {
47		Element pubsub = packet.findChild("pubsub",
48				"http://jabber.org/protocol/pubsub");
49		if (pubsub == null) {
50			return null;
51		}
52		Element items = pubsub.findChild("items");
53		if (items == null) {
54			return null;
55		}
56		return super.avatarData(items);
57	}
58
59	@Override
60	public void onIqPacketReceived(Account account, IqPacket packet) {
61		if (packet.hasChild("query", "jabber:iq:roster")) {
62			String from = packet.getFrom();
63			if ((from == null) || (from.equals(account.getJid()))) {
64				Element query = packet.findChild("query");
65				this.rosterItems(account, query);
66			}
67		} else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
68				|| packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
69			mXmppConnectionService.getJingleConnectionManager()
70					.deliverIbbPacket(account, packet);
71		} else if (packet.hasChild("query",
72				"http://jabber.org/protocol/disco#info")) {
73			IqPacket response = mXmppConnectionService.getIqGenerator()
74					.discoResponse(packet);
75			account.getXmppConnection().sendIqPacket(response, null);
76		} else if (packet.hasChild("ping", "urn:xmpp:ping")) {
77			IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
78			mXmppConnectionService.sendIqPacket(account, response, null);
79		} else {
80			if ((packet.getType() == IqPacket.TYPE_GET)
81					|| (packet.getType() == IqPacket.TYPE_SET)) {
82				IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR);
83				Element error = response.addChild("error");
84				error.setAttribute("type", "cancel");
85				error.addChild("feature-not-implemented",
86						"urn:ietf:params:xml:ns:xmpp-stanzas");
87				account.getXmppConnection().sendIqPacket(response, null);
88			}
89		}
90	}
91
92}