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 public static boolean fromServer(final Account account, final IqPacket packet) {
75 return packet.getFrom() == null
76 || packet.getFrom().equals(account.getServer())
77 || packet.getFrom().equals(account.getJid().toBareJid())
78 || packet.getFrom().equals(account.getJid());
79 }
80
81 @Override
82 public void onIqPacketReceived(final Account account, final IqPacket packet) {
83 if (packet.hasChild("query", Xmlns.ROSTER) && fromServer(account, packet)) {
84 final Element query = packet.findChild("query");
85 // If this is in response to a query for the whole roster:
86 if (packet.getType() == IqPacket.TYPE_RESULT) {
87 account.getRoster().markAllAsNotInRoster();
88 }
89 this.rosterItems(account, query);
90 } else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) &&
91 fromServer(account, packet)) {
92 // Block list or block push.
93 Log.d(Config.LOGTAG, "Received blocklist update from server");
94 final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING);
95 final Element block = packet.findChild("block", Xmlns.BLOCKING);
96 final Collection<Element> items = blocklist != null ? blocklist.getChildren() :
97 (block != null ? block.getChildren() : null);
98 // If this is a response to a blocklist query, clear the block list and replace with the new one.
99 // Otherwise, just update the existing blocklist.
100 if (packet.getType() == IqPacket.TYPE_RESULT) {
101 account.clearBlocklist();
102 }
103 if (items != null) {
104 final Collection<Jid> jids = new ArrayList<>(items.size());
105 // Create a collection of Jids from the packet
106 for (final Element item : items) {
107 if (item.getName().equals("item")) {
108 final Jid jid = item.getAttributeAsJid("jid");
109 if (jid != null) {
110 jids.add(jid);
111 }
112 }
113 }
114 account.getBlocklist().addAll(jids);
115 }
116 // Update the UI
117 mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
118 } else if (packet.hasChild("unblock", Xmlns.BLOCKING) &&
119 fromServer(account, packet) && packet.getType() == IqPacket.TYPE_SET) {
120 Log.d(Config.LOGTAG, "Received unblock update from server");
121 final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
122 if (items.size() == 0) {
123 // No children to unblock == unblock all
124 account.getBlocklist().clear();
125 } else {
126 final Collection<Jid> jids = new ArrayList<>(items.size());
127 for (final Element item : items) {
128 if (item.getName().equals("item")) {
129 final Jid jid = item.getAttributeAsJid("jid");
130 if (jid != null) {
131 jids.add(jid);
132 }
133 }
134 }
135 account.getBlocklist().removeAll(jids);
136 }
137 mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
138 } else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
139 || packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
140 mXmppConnectionService.getJingleConnectionManager()
141 .deliverIbbPacket(account, packet);
142 } else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
143 final IqPacket response = mXmppConnectionService.getIqGenerator()
144 .discoResponse(packet);
145 account.getXmppConnection().sendIqPacket(response, null);
146 } else if (packet.hasChild("ping", "urn:xmpp:ping")) {
147 final IqPacket response = packet.generateResponse(IqPacket.TYPE_RESULT);
148 mXmppConnectionService.sendIqPacket(account, response, null);
149 } else {
150 if ((packet.getType() == IqPacket.TYPE_GET)
151 || (packet.getType() == IqPacket.TYPE_SET)) {
152 final IqPacket response = packet.generateResponse(IqPacket.TYPE_ERROR);
153 final Element error = response.addChild("error");
154 error.setAttribute("type", "cancel");
155 error.addChild("feature-not-implemented",
156 "urn:ietf:params:xml:ns:xmpp-stanzas");
157 account.getXmppConnection().sendIqPacket(response, null);
158 }
159 }
160 }
161
162}