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