1package eu.siacs.conversations.parser;
2
3import android.util.Log;
4
5import java.util.ArrayList;
6import java.util.Collection;
7
8import eu.siacs.conversations.Config;
9import eu.siacs.conversations.entities.Account;
10import eu.siacs.conversations.entities.Contact;
11import eu.siacs.conversations.services.XmppConnectionService;
12import eu.siacs.conversations.utils.Xmlns;
13import eu.siacs.conversations.xml.Element;
14import eu.siacs.conversations.xmpp.OnIqPacketReceived;
15import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
16import eu.siacs.conversations.xmpp.jid.Jid;
17import eu.siacs.conversations.xmpp.stanzas.IqPacket;
18
19public class IqParser extends AbstractParser implements OnIqPacketReceived {
20
21 public IqParser(final XmppConnectionService service) {
22 super(service);
23 }
24
25 private void rosterItems(final Account account, final Element query) {
26 final String version = query.getAttribute("ver");
27 if (version != null) {
28 account.getRoster().setVersion(version);
29 }
30 for (final Element item : query.getChildren()) {
31 if (item.getName().equals("item")) {
32 final Jid jid = item.getAttributeAsJid("jid");
33 if (jid == null) {
34 continue;
35 }
36 final String name = item.getAttribute("name");
37 final String subscription = item.getAttribute("subscription");
38 final Contact contact = account.getRoster().getContact(jid);
39 if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
40 contact.setServerName(name);
41 contact.parseGroupsFromElement(item);
42 }
43 if (subscription != null) {
44 if (subscription.equals("remove")) {
45 contact.resetOption(Contact.Options.IN_ROSTER);
46 contact.resetOption(Contact.Options.DIRTY_DELETE);
47 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
48 } else {
49 contact.setOption(Contact.Options.IN_ROSTER);
50 contact.resetOption(Contact.Options.DIRTY_PUSH);
51 contact.parseSubscriptionFromElement(item);
52 }
53 }
54 mXmppConnectionService.getAvatarService().clear(contact);
55 }
56 }
57 mXmppConnectionService.updateConversationUi();
58 mXmppConnectionService.updateRosterUi();
59 }
60
61 public String avatarData(final IqPacket packet) {
62 final Element pubsub = packet.findChild("pubsub",
63 "http://jabber.org/protocol/pubsub");
64 if (pubsub == null) {
65 return null;
66 }
67 final Element items = pubsub.findChild("items");
68 if (items == null) {
69 return null;
70 }
71 return super.avatarData(items);
72 }
73
74 @Override
75 public void onIqPacketReceived(final Account account, final IqPacket packet) {
76 if (packet.hasChild("query", Xmlns.ROSTER) && packet.fromServer(account)) {
77 final Element query = packet.findChild("query");
78 // If this is in response to a query for the whole roster:
79 if (packet.getType() == IqPacket.TYPE.RESULT) {
80 account.getRoster().markAllAsNotInRoster();
81 }
82 this.rosterItems(account, query);
83 } else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) &&
84 packet.fromServer(account)) {
85 // Block list or block push.
86 Log.d(Config.LOGTAG, "Received blocklist update from server");
87 final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING);
88 final Element block = packet.findChild("block", Xmlns.BLOCKING);
89 final Collection<Element> items = blocklist != null ? blocklist.getChildren() :
90 (block != null ? block.getChildren() : null);
91 // If this is a response to a blocklist query, clear the block list and replace with the new one.
92 // Otherwise, just update the existing blocklist.
93 if (packet.getType() == IqPacket.TYPE.RESULT) {
94 account.clearBlocklist();
95 account.getXmppConnection().getFeatures().setBlockListRequested(true);
96 }
97 if (items != null) {
98 final Collection<Jid> jids = new ArrayList<>(items.size());
99 // Create a collection of Jids from the packet
100 for (final Element item : items) {
101 if (item.getName().equals("item")) {
102 final Jid jid = item.getAttributeAsJid("jid");
103 if (jid != null) {
104 jids.add(jid);
105 }
106 }
107 }
108 account.getBlocklist().addAll(jids);
109 }
110 // Update the UI
111 mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
112 } else if (packet.hasChild("unblock", Xmlns.BLOCKING) &&
113 packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) {
114 Log.d(Config.LOGTAG, "Received unblock update from server");
115 final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
116 if (items.size() == 0) {
117 // No children to unblock == unblock all
118 account.getBlocklist().clear();
119 } else {
120 final Collection<Jid> jids = new ArrayList<>(items.size());
121 for (final Element item : items) {
122 if (item.getName().equals("item")) {
123 final Jid jid = item.getAttributeAsJid("jid");
124 if (jid != null) {
125 jids.add(jid);
126 }
127 }
128 }
129 account.getBlocklist().removeAll(jids);
130 }
131 mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
132 } else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
133 || packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
134 mXmppConnectionService.getJingleConnectionManager()
135 .deliverIbbPacket(account, packet);
136 } else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
137 final IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(packet);
138 mXmppConnectionService.sendIqPacket(account, response, null);
139 } else if (packet.hasChild("query","jabber:iq:version")) {
140 final IqPacket response = mXmppConnectionService.getIqGenerator().versionResponse(packet);
141 mXmppConnectionService.sendIqPacket(account,response,null);
142 } else if (packet.hasChild("ping", "urn:xmpp:ping")) {
143 final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
144 mXmppConnectionService.sendIqPacket(account, response, null);
145 } else {
146 if ((packet.getType() == IqPacket.TYPE.GET)
147 || (packet.getType() == IqPacket.TYPE.SET)) {
148 final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
149 final Element error = response.addChild("error");
150 error.setAttribute("type", "cancel");
151 error.addChild("feature-not-implemented",
152 "urn:ietf:params:xml:ns:xmpp-stanzas");
153 account.getXmppConnection().sendIqPacket(response, null);
154 }
155 }
156 }
157
158}