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