Verify IQ responses

Fixes #20

Move fromServer/toServer to AbstractStanza
This commit is contained in:
Sam Whited 2014-12-30 08:50:51 -05:00
parent eb7e683403
commit 88704ce5cd
4 changed files with 39 additions and 18 deletions

View File

@ -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<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
if (items.size() == 0) {

View File

@ -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();
}

View File

@ -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<String, OnIqPacketReceived> packetCallbacks = new Hashtable<>();
private final Map<String, Pair<IqPacket, OnIqPacketReceived>> 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<IqPacket, OnIqPacketReceived> 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);
}

View File

@ -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());
}
}