2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.parser;
|
|
|
|
|
2014-11-17 19:45:00 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
|
2014-11-17 19:45:00 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2014-12-21 20:43:58 +00:00
|
|
|
import eu.siacs.conversations.utils.Xmlns;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
|
|
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
2014-12-21 20:43:58 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
|
2014-11-05 20:55:47 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
|
|
|
|
public class IqParser extends AbstractParser implements OnIqPacketReceived {
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
public IqParser(final XmppConnectionService service) {
|
2014-10-22 16:38:44 +00:00
|
|
|
super(service);
|
|
|
|
}
|
|
|
|
|
2014-12-23 17:09:29 +00:00
|
|
|
private void rosterItems(final Account account, final Element query) {
|
2014-12-21 20:43:58 +00:00
|
|
|
final String version = query.getAttribute("ver");
|
2014-10-22 16:38:44 +00:00
|
|
|
if (version != null) {
|
|
|
|
account.getRoster().setVersion(version);
|
|
|
|
}
|
2014-12-21 20:43:58 +00:00
|
|
|
for (final Element item : query.getChildren()) {
|
2014-10-22 16:38:44 +00:00
|
|
|
if (item.getName().equals("item")) {
|
2014-12-01 09:25:36 +00:00
|
|
|
final Jid jid = item.getAttributeAsJid("jid");
|
|
|
|
if (jid == null) {
|
2014-12-19 12:40:16 +00:00
|
|
|
continue;
|
2014-12-01 09:25:36 +00:00
|
|
|
}
|
2014-12-21 20:43:58 +00:00
|
|
|
final String name = item.getAttribute("name");
|
|
|
|
final String subscription = item.getAttribute("subscription");
|
|
|
|
final Contact contact = account.getRoster().getContact(jid);
|
2014-10-22 16:38:44 +00:00
|
|
|
if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
|
|
|
|
contact.setServerName(name);
|
2014-11-16 16:21:21 +00:00
|
|
|
contact.parseGroupsFromElement(item);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
if (subscription != null) {
|
|
|
|
if (subscription.equals("remove")) {
|
|
|
|
contact.resetOption(Contact.Options.IN_ROSTER);
|
|
|
|
contact.resetOption(Contact.Options.DIRTY_DELETE);
|
|
|
|
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
|
|
|
|
} else {
|
|
|
|
contact.setOption(Contact.Options.IN_ROSTER);
|
|
|
|
contact.resetOption(Contact.Options.DIRTY_PUSH);
|
|
|
|
contact.parseSubscriptionFromElement(item);
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 20:28:16 +00:00
|
|
|
mXmppConnectionService.getAvatarService().clear(contact);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-17 20:28:16 +00:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-10-22 16:38:44 +00:00
|
|
|
mXmppConnectionService.updateRosterUi();
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
public String avatarData(final IqPacket packet) {
|
|
|
|
final Element pubsub = packet.findChild("pubsub",
|
2014-10-22 16:38:44 +00:00
|
|
|
"http://jabber.org/protocol/pubsub");
|
|
|
|
if (pubsub == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-12-21 20:43:58 +00:00
|
|
|
final Element items = pubsub.findChild("items");
|
2014-10-22 16:38:44 +00:00
|
|
|
if (items == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return super.avatarData(items);
|
|
|
|
}
|
|
|
|
|
2014-12-21 01:13:13 +00:00
|
|
|
public static boolean fromServer(final Account account, final IqPacket packet) {
|
|
|
|
return packet.getFrom() == null || packet.getFrom().equals(account.getServer()) || packet.getFrom().equals(account.getJid().toBareJid());
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
@Override
|
2014-12-21 20:43:58 +00:00
|
|
|
public void onIqPacketReceived(final Account account, final IqPacket packet) {
|
2014-12-21 01:13:13 +00:00
|
|
|
if (packet.hasChild("query", Xmlns.ROSTER) && fromServer(account, packet)) {
|
|
|
|
final Element query = packet.findChild("query");
|
|
|
|
// If this is in response to a query for the whole roster:
|
|
|
|
if (packet.getType() == IqPacket.TYPE_RESULT) {
|
|
|
|
account.getRoster().markAllAsNotInRoster();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2014-12-21 01:13:13 +00:00
|
|
|
this.rosterItems(account, query);
|
|
|
|
} else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) &&
|
|
|
|
fromServer(account, packet)) {
|
|
|
|
// Block list or block push.
|
|
|
|
Log.d(Config.LOGTAG, "Received blocklist update from server");
|
|
|
|
final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING);
|
|
|
|
final Element block = packet.findChild("block", Xmlns.BLOCKING);
|
|
|
|
final Collection<Element> items = blocklist != null ? blocklist.getChildren() :
|
|
|
|
(block != null ? block.getChildren() : null);
|
|
|
|
// If this is a response to a blocklist query, clear the block list and replace with the new one.
|
|
|
|
// Otherwise, just update the existing blocklist.
|
|
|
|
if (packet.getType() == IqPacket.TYPE_RESULT) {
|
|
|
|
account.clearBlocklist();
|
|
|
|
}
|
|
|
|
if (items != null) {
|
|
|
|
final Collection<Jid> jids = new ArrayList<>(items.size());
|
|
|
|
// Create a collection of Jids from the packet
|
|
|
|
for (final Element item : items) {
|
|
|
|
if (item.getName().equals("item")) {
|
|
|
|
final Jid jid = item.getAttributeAsJid("jid");
|
|
|
|
if (jid != null) {
|
|
|
|
jids.add(jid);
|
2014-12-21 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-21 01:13:13 +00:00
|
|
|
account.getBlocklist().addAll(jids);
|
2014-12-21 20:43:58 +00:00
|
|
|
}
|
2014-12-21 01:13:13 +00:00
|
|
|
// Update the UI
|
|
|
|
mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
|
|
|
|
} else if (packet.hasChild("unblock", Xmlns.BLOCKING) &&
|
|
|
|
fromServer(account, packet) && packet.getType() == IqPacket.TYPE_SET) {
|
|
|
|
Log.d(Config.LOGTAG, "Received unblock update from server");
|
|
|
|
final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
|
|
|
|
if (items.size() == 0) {
|
|
|
|
// No children to unblock == unblock all
|
|
|
|
account.getBlocklist().clear();
|
|
|
|
} else {
|
|
|
|
final Collection<Jid> jids = new ArrayList<>(items.size());
|
|
|
|
for (final Element item : items) {
|
|
|
|
if (item.getName().equals("item")) {
|
|
|
|
final Jid jid = item.getAttributeAsJid("jid");
|
|
|
|
if (jid != null) {
|
|
|
|
jids.add(jid);
|
2014-12-21 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-21 01:13:13 +00:00
|
|
|
account.getBlocklist().removeAll(jids);
|
2014-12-21 20:43:58 +00:00
|
|
|
}
|
2014-12-21 01:13:13 +00:00
|
|
|
mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
|
|
|
|
} else if (packet.hasChild("open", "http://jabber.org/protocol/ibb")
|
|
|
|
|| packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
|
|
|
|
mXmppConnectionService.getJingleConnectionManager()
|
|
|
|
.deliverIbbPacket(account, packet);
|
|
|
|
} else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
|
|
|
|
final IqPacket response = mXmppConnectionService.getIqGenerator()
|
|
|
|
.discoResponse(packet);
|
|
|
|
account.getXmppConnection().sendIqPacket(response, null);
|
|
|
|
} else if (packet.hasChild("ping", "urn:xmpp:ping")) {
|
|
|
|
final IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
|
|
|
|
mXmppConnectionService.sendIqPacket(account, response, null);
|
2014-10-22 16:38:44 +00:00
|
|
|
} else {
|
2014-12-21 01:13:13 +00:00
|
|
|
if ((packet.getType() == IqPacket.TYPE_GET)
|
|
|
|
|| (packet.getType() == IqPacket.TYPE_SET)) {
|
|
|
|
final IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR);
|
|
|
|
final Element error = response.addChild("error");
|
|
|
|
error.setAttribute("type", "cancel");
|
|
|
|
error.addChild("feature-not-implemented",
|
|
|
|
"urn:ietf:params:xml:ns:xmpp-stanzas");
|
2014-10-22 16:38:44 +00:00
|
|
|
account.getXmppConnection().sendIqPacket(response, null);
|
2014-12-21 01:13:13 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|