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.jid.InvalidJidException;
9import eu.siacs.conversations.xmpp.jid.Jid;
10import eu.siacs.conversations.xmpp.stanzas.IqPacket;
11
12public class IqParser extends AbstractParser implements OnIqPacketReceived {
13
14 public IqParser(XmppConnectionService service) {
15 super(service);
16 }
17
18 public void rosterItems(Account account, Element query) {
19 String version = query.getAttribute("ver");
20 if (version != null) {
21 account.getRoster().setVersion(version);
22 }
23 for (Element item : query.getChildren()) {
24 if (item.getName().equals("item")) {
25 Jid jid;
26 try {
27 jid = Jid.fromString(item.getAttribute("jid"));
28 } catch (final InvalidJidException e) {
29 // TODO: Handle this?
30 jid = null;
31 }
32 String name = item.getAttribute("name");
33 String subscription = item.getAttribute("subscription");
34 Contact contact = account.getRoster().getContact(jid);
35 if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
36 contact.setServerName(name);
37 contact.parseGroupsFromElement(item);
38 }
39 if (subscription != null) {
40 if (subscription.equals("remove")) {
41 contact.resetOption(Contact.Options.IN_ROSTER);
42 contact.resetOption(Contact.Options.DIRTY_DELETE);
43 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
44 } else {
45 contact.setOption(Contact.Options.IN_ROSTER);
46 contact.resetOption(Contact.Options.DIRTY_PUSH);
47 contact.parseSubscriptionFromElement(item);
48 }
49 }
50 }
51 }
52 mXmppConnectionService.updateRosterUi();
53 }
54
55 public String avatarData(IqPacket packet) {
56 Element pubsub = packet.findChild("pubsub",
57 "http://jabber.org/protocol/pubsub");
58 if (pubsub == null) {
59 return null;
60 }
61 Element items = pubsub.findChild("items");
62 if (items == null) {
63 return null;
64 }
65 return super.avatarData(items);
66 }
67
68 @Override
69 public void onIqPacketReceived(Account account, IqPacket packet) {
70 if (packet.hasChild("query", "jabber:iq:roster")) {
71 final Jid from = packet.getFrom();
72 if ((from == null) || (from.equals(account.getJid().toBareJid()))) {
73 Element query = packet.findChild("query");
74 this.rosterItems(account, query);
75 }
76 } else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
77 || packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
78 mXmppConnectionService.getJingleConnectionManager()
79 .deliverIbbPacket(account, packet);
80 } else if (packet.hasChild("query",
81 "http://jabber.org/protocol/disco#info")) {
82 IqPacket response = mXmppConnectionService.getIqGenerator()
83 .discoResponse(packet);
84 account.getXmppConnection().sendIqPacket(response, null);
85 } else if (packet.hasChild("ping", "urn:xmpp:ping")) {
86 IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
87 mXmppConnectionService.sendIqPacket(account, response, null);
88 } else {
89 if ((packet.getType() == IqPacket.TYPE_GET)
90 || (packet.getType() == IqPacket.TYPE_SET)) {
91 IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR);
92 Element error = response.addChild("error");
93 error.setAttribute("type", "cancel");
94 error.addChild("feature-not-implemented",
95 "urn:ietf:params:xml:ns:xmpp-stanzas");
96 account.getXmppConnection().sendIqPacket(response, null);
97 }
98 }
99 }
100
101}