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	public String avatarData(IqPacket packet) {
45		Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
46		if (pubsub==null) {
47			return null;
48		}
49		Element items = pubsub.findChild("items");
50		if (items==null) {
51			return null;
52		}
53		return super.avatarData(items);
54	}
55
56	@Override
57	public void onIqPacketReceived(Account account, IqPacket packet) {
58		if (packet.hasChild("query", "jabber:iq:roster")) {
59			String from = packet.getFrom();
60			if ((from == null) || (from.equals(account.getJid()))) {
61				Element query = packet.findChild("query");
62				this.rosterItems(account, query);
63			}
64		} else if (packet
65				.hasChild("open", "http://jabber.org/protocol/ibb")
66				|| packet
67						.hasChild("data", "http://jabber.org/protocol/ibb")) {
68			mXmppConnectionService.getJingleConnectionManager().deliverIbbPacket(account, packet);
69		} else if (packet.hasChild("query",
70				"http://jabber.org/protocol/disco#info")) {
71			IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(packet);
72			account.getXmppConnection().sendIqPacket(response, null);
73		} else {
74			if ((packet.getType() == IqPacket.TYPE_GET)
75					|| (packet.getType() == IqPacket.TYPE_SET)) {
76				IqPacket response = packet
77						.generateRespone(IqPacket.TYPE_ERROR);
78				Element error = response.addChild("error");
79				error.setAttribute("type", "cancel");
80				error.addChild("feature-not-implemented",
81						"urn:ietf:params:xml:ns:xmpp-stanzas");
82				account.getXmppConnection().sendIqPacket(response, null);
83			}
84		}
85	}
86
87}