From 88704ce5cd68047008375c2eba6751e2e3cdd8c0 Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Tue, 30 Dec 2014 08:50:51 -0500 Subject: [PATCH] Verify IQ responses Fixes #20 Move fromServer/toServer to AbstractStanza --- .../siacs/conversations/parser/IqParser.java | 13 +++------- .../services/XmppConnectionService.java | 3 +-- .../conversations/xmpp/XmppConnection.java | 24 +++++++++++++++---- .../xmpp/stanzas/AbstractStanza.java | 17 ++++++++++++- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/IqParser.java b/src/main/java/eu/siacs/conversations/parser/IqParser.java index 70a6e3f73..3b501f333 100644 --- a/src/main/java/eu/siacs/conversations/parser/IqParser.java +++ b/src/main/java/eu/siacs/conversations/parser/IqParser.java @@ -71,16 +71,9 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { return super.avatarData(items); } - 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()) - || packet.getFrom().equals(account.getJid()); - } - @Override public void onIqPacketReceived(final Account account, final IqPacket packet) { - if (packet.hasChild("query", Xmlns.ROSTER) && fromServer(account, packet)) { + if (packet.hasChild("query", Xmlns.ROSTER) && packet.fromServer(account)) { final Element query = packet.findChild("query"); // If this is in response to a query for the whole roster: if (packet.getType() == IqPacket.TYPE.RESULT) { @@ -88,7 +81,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { } this.rosterItems(account, query); } else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) && - fromServer(account, packet)) { + packet.fromServer(account)) { // Block list or block push. Log.d(Config.LOGTAG, "Received blocklist update from server"); final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING); @@ -116,7 +109,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived { // Update the UI mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED); } else if (packet.hasChild("unblock", Xmlns.BLOCKING) && - fromServer(account, packet) && packet.getType() == IqPacket.TYPE.SET) { + packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) { Log.d(Config.LOGTAG, "Received unblock update from server"); final Collection items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren(); if (items.size() == 0) { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index fd0a4b9da..beba97fe4 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -84,7 +84,6 @@ import eu.siacs.conversations.xmpp.OnMessagePacketReceived; import eu.siacs.conversations.xmpp.OnPresencePacketReceived; import eu.siacs.conversations.xmpp.OnStatusChanged; import eu.siacs.conversations.xmpp.OnUpdateBlocklist; -import eu.siacs.conversations.xmpp.PacketReceived; import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.forms.Data; import eu.siacs.conversations.xmpp.forms.Field; @@ -979,7 +978,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa query.setCallback(callback); } callback.informUser(R.string.fetching_history_from_server); - } + } } }).start(); } diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index d954c0d45..56d3e560a 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -9,6 +9,7 @@ import android.os.PowerManager.WakeLock; import android.os.SystemClock; import android.preference.PreferenceManager; import android.util.Log; +import android.util.Pair; import android.util.SparseArray; import org.apache.http.conn.ssl.StrictHostnameVerifier; @@ -104,7 +105,7 @@ public class XmppConnection implements Runnable { private long lastConnect = 0; private long lastSessionStarted = 0; private int attempt = 0; - private final Map packetCallbacks = new Hashtable<>(); + private final Map> packetCallbacks = new Hashtable<>(); private OnPresencePacketReceived presenceListener = null; private OnJinglePacketReceived jingleListener = null; private OnIqPacketReceived unregisteredIqListener = null; @@ -444,8 +445,21 @@ public class XmppConnection implements Runnable { } } else { if (packetCallbacks.containsKey(packet.getId())) { - packetCallbacks.get(packet.getId()).onIqPacketReceived(account, packet); - packetCallbacks.remove(packet.getId()); + final Pair packetCallbackDuple = packetCallbacks.get(packet.getId()); + // Packets to the server should have responses from the server + if (packetCallbackDuple.first.toServer(account)) { + if (packet.fromServer(account)) { + packetCallbackDuple.second + .onIqPacketReceived(account, packet); + packetCallbacks.remove(packet.getId()); + } + } else { + if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) { + packetCallbackDuple.second + .onIqPacketReceived(account, packet); + packetCallbacks.remove(packet.getId()); + } + } } else if ((packet.getType() == IqPacket.TYPE.GET || packet .getType() == IqPacket.TYPE.SET) && this.unregisteredIqListener != null) { @@ -693,7 +707,7 @@ public class XmppConnection implements Runnable { }); if (this.streamFeatures.hasChild("session")) { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending deprecated session"); - final IqPacket startSession = new IqPacket(IqPacket.TYPE_SET); + final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET); startSession.addChild("session","urn:ietf:params:xml:ns:xmpp-session"); this.sendUnmodifiedIqPacket(startSession, null); } @@ -831,7 +845,7 @@ public class XmppConnection implements Runnable { if (packet.getId() == null) { packet.setId(nextRandomId()); } - packetCallbacks.put(packet.getId(), callback); + packetCallbacks.put(packet.getId(), new Pair<>(packet, callback)); } this.sendPacket(packet); } diff --git a/src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java b/src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java index 1a49b45e3..55256ecee 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java +++ b/src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java @@ -1,11 +1,12 @@ package eu.siacs.conversations.xmpp.stanzas; +import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.jid.Jid; public class AbstractStanza extends Element { - protected AbstractStanza(String name) { + protected AbstractStanza(final String name) { super(name); } @@ -36,4 +37,18 @@ public class AbstractStanza extends Element { public void setId(final String id) { setAttribute("id", id); } + + public boolean fromServer(final Account account) { + return getFrom() == null + || getFrom().equals(account.getServer()) + || getFrom().equals(account.getJid().toBareJid()) + || getFrom().equals(account.getJid()); + } + + public boolean toServer(final Account account) { + return getTo() == null + || getTo().equals(account.getServer()) + || getTo().equals(account.getJid().toBareJid()) + || getTo().equals(account.getJid()); + } }