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