Merge pull request #823 from SamWhited/issue20

Verify IQ responses
This commit is contained in:
Daniel Gultsch 2015-01-04 15:05:24 +01:00
commit d64496eab6
10 changed files with 123 additions and 116 deletions

View File

@ -21,7 +21,7 @@ public class IqGenerator extends AbstractGenerator {
} }
public IqPacket discoResponse(final IqPacket request) { public IqPacket discoResponse(final IqPacket request) {
final IqPacket packet = new IqPacket(IqPacket.TYPE_RESULT); final IqPacket packet = new IqPacket(IqPacket.TYPE.RESULT);
packet.setId(request.getId()); packet.setId(request.getId());
packet.setTo(request.getFrom()); packet.setTo(request.getFrom());
final Element query = packet.addChild("query", final Element query = packet.addChild("query",
@ -40,7 +40,7 @@ public class IqGenerator extends AbstractGenerator {
} }
protected IqPacket publish(final String node, final Element item) { protected IqPacket publish(final String node, final Element item) {
final IqPacket packet = new IqPacket(IqPacket.TYPE_SET); final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
final Element pubsub = packet.addChild("pubsub", final Element pubsub = packet.addChild("pubsub",
"http://jabber.org/protocol/pubsub"); "http://jabber.org/protocol/pubsub");
final Element publish = pubsub.addChild("publish"); final Element publish = pubsub.addChild("publish");
@ -50,7 +50,7 @@ public class IqGenerator extends AbstractGenerator {
} }
protected IqPacket retrieve(String node, Element item) { protected IqPacket retrieve(String node, Element item) {
final IqPacket packet = new IqPacket(IqPacket.TYPE_GET); final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
final Element pubsub = packet.addChild("pubsub", final Element pubsub = packet.addChild("pubsub",
"http://jabber.org/protocol/pubsub"); "http://jabber.org/protocol/pubsub");
final Element items = pubsub.addChild("items"); final Element items = pubsub.addChild("items");
@ -100,7 +100,7 @@ public class IqGenerator extends AbstractGenerator {
} }
public IqPacket queryMessageArchiveManagement(final MessageArchiveService.Query mam) { public IqPacket queryMessageArchiveManagement(final MessageArchiveService.Query mam) {
final IqPacket packet = new IqPacket(IqPacket.TYPE_SET); final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
final Element query = packet.query("urn:xmpp:mam:0"); final Element query = packet.query("urn:xmpp:mam:0");
query.setAttribute("queryid",mam.getQueryId()); query.setAttribute("queryid",mam.getQueryId());
final Data data = new Data(); final Data data = new Data();
@ -119,28 +119,28 @@ public class IqGenerator extends AbstractGenerator {
return packet; return packet;
} }
public IqPacket generateGetBlockList() { public IqPacket generateGetBlockList() {
final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.addChild("blocklist", Xmlns.BLOCKING); iq.addChild("blocklist", Xmlns.BLOCKING);
return iq; return iq;
} }
public IqPacket generateSetBlockRequest(final Jid jid) { public IqPacket generateSetBlockRequest(final Jid jid) {
final IqPacket iq = new IqPacket(IqPacket.TYPE_SET); final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
final Element block = iq.addChild("block", Xmlns.BLOCKING); final Element block = iq.addChild("block", Xmlns.BLOCKING);
block.addChild("item").setAttribute("jid", jid.toBareJid().toString()); block.addChild("item").setAttribute("jid", jid.toBareJid().toString());
return iq; return iq;
} }
public IqPacket generateSetUnblockRequest(final Jid jid) { public IqPacket generateSetUnblockRequest(final Jid jid) {
final IqPacket iq = new IqPacket(IqPacket.TYPE_SET); final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
final Element block = iq.addChild("unblock", Xmlns.BLOCKING); final Element block = iq.addChild("unblock", Xmlns.BLOCKING);
block.addChild("item").setAttribute("jid", jid.toBareJid().toString()); block.addChild("item").setAttribute("jid", jid.toBareJid().toString());
return iq; return iq;
} }
public IqPacket generateSetPassword(final Account account, final String newPassword) { public IqPacket generateSetPassword(final Account account, final String newPassword) {
final IqPacket packet = new IqPacket(IqPacket.TYPE_SET); final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
packet.setTo(account.getServer()); packet.setTo(account.getServer());
final Element query = packet.addChild("query", Xmlns.REGISTER); final Element query = packet.addChild("query", Xmlns.REGISTER);
final Jid jid = account.getJid(); final Jid jid = account.getJid();

View File

@ -71,24 +71,17 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
return super.avatarData(items); 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 @Override
public void onIqPacketReceived(final Account account, final IqPacket packet) { 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"); final Element query = packet.findChild("query");
// If this is in response to a query for the whole roster: // If this is in response to a query for the whole roster:
if (packet.getType() == IqPacket.TYPE_RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.getRoster().markAllAsNotInRoster(); account.getRoster().markAllAsNotInRoster();
} }
this.rosterItems(account, query); this.rosterItems(account, query);
} else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) && } else if ((packet.hasChild("block", Xmlns.BLOCKING) || packet.hasChild("blocklist", Xmlns.BLOCKING)) &&
fromServer(account, packet)) { packet.fromServer(account)) {
// Block list or block push. // Block list or block push.
Log.d(Config.LOGTAG, "Received blocklist update from server"); Log.d(Config.LOGTAG, "Received blocklist update from server");
final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING); final Element blocklist = packet.findChild("blocklist", Xmlns.BLOCKING);
@ -97,7 +90,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
(block != null ? block.getChildren() : null); (block != null ? block.getChildren() : null);
// If this is a response to a blocklist query, clear the block list and replace with the new one. // 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. // Otherwise, just update the existing blocklist.
if (packet.getType() == IqPacket.TYPE_RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.clearBlocklist(); account.clearBlocklist();
} }
if (items != null) { if (items != null) {
@ -116,7 +109,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
// Update the UI // Update the UI
mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED); mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
} else if (packet.hasChild("unblock", Xmlns.BLOCKING) && } 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"); Log.d(Config.LOGTAG, "Received unblock update from server");
final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren(); final Collection<Element> items = packet.findChild("unblock", Xmlns.BLOCKING).getChildren();
if (items.size() == 0) { if (items.size() == 0) {
@ -144,12 +137,12 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
.discoResponse(packet); .discoResponse(packet);
account.getXmppConnection().sendIqPacket(response, null); account.getXmppConnection().sendIqPacket(response, null);
} else if (packet.hasChild("ping", "urn:xmpp:ping")) { } else if (packet.hasChild("ping", "urn:xmpp:ping")) {
final IqPacket response = packet.generateResponse(IqPacket.TYPE_RESULT); final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
mXmppConnectionService.sendIqPacket(account, response, null); mXmppConnectionService.sendIqPacket(account, response, null);
} else { } else {
if ((packet.getType() == IqPacket.TYPE_GET) if ((packet.getType() == IqPacket.TYPE.GET)
|| (packet.getType() == IqPacket.TYPE_SET)) { || (packet.getType() == IqPacket.TYPE.SET)) {
final IqPacket response = packet.generateResponse(IqPacket.TYPE_ERROR); final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
final Element error = response.addChild("error"); final Element error = response.addChild("error");
error.setAttribute("type", "cancel"); error.setAttribute("type", "cancel");
error.addChild("feature-not-implemented", error.addChild("feature-not-implemented",

View File

@ -111,7 +111,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() { this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_ERROR) { if (packet.getType() == IqPacket.TYPE.ERROR) {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString()); Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString());
finalizeQuery(query); finalizeQuery(query);
} }

View File

@ -84,7 +84,6 @@ import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.OnPresencePacketReceived; import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
import eu.siacs.conversations.xmpp.OnStatusChanged; import eu.siacs.conversations.xmpp.OnStatusChanged;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.PacketReceived;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.forms.Data; import eu.siacs.conversations.xmpp.forms.Data;
import eu.siacs.conversations.xmpp.forms.Field; import eu.siacs.conversations.xmpp.forms.Field;
@ -787,7 +786,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void fetchRosterFromServer(final Account account) { public void fetchRosterFromServer(final Account account) {
final IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
if (!"".equals(account.getRosterVersion())) { if (!"".equals(account.getRosterVersion())) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": fetching roster version " + account.getRosterVersion()); + ": fetching roster version " + account.getRosterVersion());
@ -800,7 +799,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void fetchBookmarks(final Account account) { public void fetchBookmarks(final Account account) {
final IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
final Element query = iqPacket.query("jabber:iq:private"); final Element query = iqPacket.query("jabber:iq:private");
query.addChild("storage", "storage:bookmarks"); query.addChild("storage", "storage:bookmarks");
final OnIqPacketReceived callback = new OnIqPacketReceived() { final OnIqPacketReceived callback = new OnIqPacketReceived() {
@ -835,7 +834,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void pushBookmarks(Account account) { public void pushBookmarks(Account account) {
IqPacket iqPacket = new IqPacket(IqPacket.TYPE_SET); IqPacket iqPacket = new IqPacket(IqPacket.TYPE.SET);
Element query = iqPacket.query("jabber:iq:private"); Element query = iqPacket.query("jabber:iq:private");
Element storage = query.addChild("storage", "storage:bookmarks"); Element storage = query.addChild("storage", "storage:bookmarks");
for (Bookmark bookmark : account.getBookmarks()) { for (Bookmark bookmark : account.getBookmarks()) {
@ -979,7 +978,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
query.setCallback(callback); query.setCallback(callback);
} }
callback.informUser(R.string.fetching_history_from_server); callback.informUser(R.string.fetching_history_from_server);
} }
} }
}).start(); }).start();
} }
@ -1107,7 +1106,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
sendIqPacket(account, iq, new OnIqPacketReceived() { sendIqPacket(account, iq, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(final Account account, final IqPacket packet) { public void onIqPacketReceived(final Account account, final IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.setPassword(newPassword); account.setPassword(newPassword);
databaseBackend.updateAccount(account); databaseBackend.updateAccount(account);
callback.onPasswordChangeSucceeded(); callback.onPasswordChangeSucceeded();
@ -1500,13 +1499,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void pushConferenceConfiguration(final Conversation conversation,final Bundle options, final OnConferenceOptionsPushed callback) { public void pushConferenceConfiguration(final Conversation conversation,final Bundle options, final OnConferenceOptionsPushed callback) {
IqPacket request = new IqPacket(IqPacket.TYPE_GET); IqPacket request = new IqPacket(IqPacket.TYPE.GET);
request.setTo(conversation.getJid().toBareJid()); request.setTo(conversation.getJid().toBareJid());
request.query("http://jabber.org/protocol/muc#owner"); request.query("http://jabber.org/protocol/muc#owner");
sendIqPacket(conversation.getAccount(),request,new OnIqPacketReceived() { sendIqPacket(conversation.getAccount(),request,new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() != IqPacket.TYPE_ERROR) { if (packet.getType() != IqPacket.TYPE.ERROR) {
Data data = Data.parse(packet.query().findChild("x", "jabber:x:data")); Data data = Data.parse(packet.query().findChild("x", "jabber:x:data"));
for (Field field : data.getFields()) { for (Field field : data.getFields()) {
if (options.containsKey(field.getName())) { if (options.containsKey(field.getName())) {
@ -1514,13 +1513,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} }
data.submit(); data.submit();
IqPacket set = new IqPacket(IqPacket.TYPE_SET); IqPacket set = new IqPacket(IqPacket.TYPE.SET);
set.setTo(conversation.getJid().toBareJid()); set.setTo(conversation.getJid().toBareJid());
set.query("http://jabber.org/protocol/muc#owner").addChild(data); set.query("http://jabber.org/protocol/muc#owner").addChild(data);
sendIqPacket(account, set, new OnIqPacketReceived() { sendIqPacket(account, set, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
if (callback != null) { if (callback != null) {
callback.onPushSucceeded(); callback.onPushSucceeded();
} }
@ -1662,7 +1661,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
final boolean sendUpdates = contact final boolean sendUpdates = contact
.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST) .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
&& contact.getOption(Contact.Options.PREEMPTIVE_GRANT); && contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
final IqPacket iq = new IqPacket(IqPacket.TYPE_SET); final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.query(Xmlns.ROSTER).addChild(contact.asElement()); iq.query(Xmlns.ROSTER).addChild(contact.asElement());
account.getXmppConnection().sendIqPacket(iq, null); account.getXmppConnection().sendIqPacket(iq, null);
if (sendUpdates) { if (sendUpdates) {
@ -1702,7 +1701,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
@Override @Override
public void onIqPacketReceived(Account account, IqPacket result) { public void onIqPacketReceived(Account account, IqPacket result) {
if (result.getType() == IqPacket.TYPE_RESULT) { if (result.getType() == IqPacket.TYPE.RESULT) {
final IqPacket packet = XmppConnectionService.this.mIqGenerator final IqPacket packet = XmppConnectionService.this.mIqGenerator
.publishAvatarMetadata(avatar); .publishAvatarMetadata(avatar);
sendIqPacket(account, packet, new OnIqPacketReceived() { sendIqPacket(account, packet, new OnIqPacketReceived() {
@ -1710,7 +1709,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
@Override @Override
public void onIqPacketReceived(Account account, public void onIqPacketReceived(Account account,
IqPacket result) { IqPacket result) {
if (result.getType() == IqPacket.TYPE_RESULT) { if (result.getType() == IqPacket.TYPE.RESULT) {
if (account.setAvatar(avatar.getFilename())) { if (account.setAvatar(avatar.getFilename())) {
databaseBackend.updateAccount(account); databaseBackend.updateAccount(account);
} }
@ -1747,7 +1746,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void onIqPacketReceived(Account account, IqPacket result) { public void onIqPacketReceived(Account account, IqPacket result) {
final String ERROR = account.getJid().toBareJid() final String ERROR = account.getJid().toBareJid()
+ ": fetching avatar for " + avatar.owner + " failed "; + ": fetching avatar for " + avatar.owner + " failed ";
if (result.getType() == IqPacket.TYPE_RESULT) { if (result.getType() == IqPacket.TYPE.RESULT) {
avatar.image = mIqParser.avatarData(result); avatar.image = mIqParser.avatarData(result);
if (avatar.image != null) { if (avatar.image != null) {
if (getFileBackend().save(avatar)) { if (getFileBackend().save(avatar)) {
@ -1801,7 +1800,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
Element pubsub = packet.findChild("pubsub", Element pubsub = packet.findChild("pubsub",
"http://jabber.org/protocol/pubsub"); "http://jabber.org/protocol/pubsub");
if (pubsub != null) { if (pubsub != null) {
@ -1835,7 +1834,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
contact.setOption(Contact.Options.DIRTY_DELETE); contact.setOption(Contact.Options.DIRTY_DELETE);
Account account = contact.getAccount(); Account account = contact.getAccount();
if (account.getStatus() == Account.State.ONLINE) { if (account.getStatus() == Account.State.ONLINE) {
IqPacket iq = new IqPacket(IqPacket.TYPE_SET); IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
Element item = iq.query(Xmlns.ROSTER).addChild("item"); Element item = iq.query(Xmlns.ROSTER).addChild("item");
item.setAttribute("jid", contact.getJid().toString()); item.setAttribute("jid", contact.getJid().toString());
item.setAttribute("subscription", "remove"); item.setAttribute("subscription", "remove");
@ -2202,7 +2201,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
@Override @Override
public void onIqPacketReceived(final Account account, final IqPacket packet) { public void onIqPacketReceived(final Account account, final IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.getBlocklist().add(jid); account.getBlocklist().add(jid);
updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED); updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
} }
@ -2217,7 +2216,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetUnblockRequest(jid), new OnIqPacketReceived() { this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetUnblockRequest(jid), new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(final Account account, final IqPacket packet) { public void onIqPacketReceived(final Account account, final IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.getBlocklist().remove(jid); account.getBlocklist().remove(jid);
updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED); updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
} }

View File

@ -9,6 +9,7 @@ import android.os.PowerManager.WakeLock;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import android.util.SparseArray; import android.util.SparseArray;
import org.apache.http.conn.ssl.StrictHostnameVerifier; import org.apache.http.conn.ssl.StrictHostnameVerifier;
@ -104,7 +105,7 @@ public class XmppConnection implements Runnable {
private long lastConnect = 0; private long lastConnect = 0;
private long lastSessionStarted = 0; private long lastSessionStarted = 0;
private int attempt = 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 OnPresencePacketReceived presenceListener = null;
private OnJinglePacketReceived jingleListener = null; private OnJinglePacketReceived jingleListener = null;
private OnIqPacketReceived unregisteredIqListener = null; private OnIqPacketReceived unregisteredIqListener = null;
@ -372,7 +373,7 @@ public class XmppConnection implements Runnable {
private void sendInitialPing() { private void sendInitialPing() {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping"); Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping");
final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setFrom(account.getJid()); iq.setFrom(account.getJid());
iq.addChild("ping", "urn:xmpp:ping"); iq.addChild("ping", "urn:xmpp:ping");
this.sendIqPacket(iq, new OnIqPacketReceived() { this.sendIqPacket(iq, new OnIqPacketReceived() {
@ -444,13 +445,26 @@ public class XmppConnection implements Runnable {
} }
} else { } else {
if (packetCallbacks.containsKey(packet.getId())) { if (packetCallbacks.containsKey(packet.getId())) {
packetCallbacks.get(packet.getId()).onIqPacketReceived(account, packet); final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple = packetCallbacks.get(packet.getId());
packetCallbacks.remove(packet.getId()); // Packets to the server should have responses from the server
} else if ((packet.getType() == IqPacket.TYPE_GET || packet if (packetCallbackDuple.first.toServer(account)) {
.getType() == IqPacket.TYPE_SET) 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) { && this.unregisteredIqListener != null) {
this.unregisteredIqListener.onIqPacketReceived(account, packet); this.unregisteredIqListener.onIqPacketReceived(account, packet);
} }
} }
} }
@ -479,19 +493,19 @@ public class XmppConnection implements Runnable {
} }
private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException { private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
tagReader.readTag(); tagReader.readTag();
try { try {
final SSLContext sc = SSLContext.getInstance("TLS"); final SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null,new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},mXmppConnectionService.getRNG()); sc.init(null,new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},mXmppConnectionService.getRNG());
final SSLSocketFactory factory = sc.getSocketFactory(); final SSLSocketFactory factory = sc.getSocketFactory();
final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier()); final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier());
final InetAddress address = socket == null ? null : socket.getInetAddress(); final InetAddress address = socket == null ? null : socket.getInetAddress();
if (factory == null || address == null || verifier == null) { if (factory == null || address == null || verifier == null) {
throw new IOException("could not setup ssl"); throw new IOException("could not setup ssl");
} }
final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true); final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true);
if (sslSocket == null) { if (sslSocket == null) {
throw new IOException("could not initialize ssl socket"); throw new IOException("could not initialize ssl socket");
@ -506,13 +520,13 @@ public class XmppConnection implements Runnable {
supportedProtocols.remove("SSLv3"); supportedProtocols.remove("SSLv3");
supportProtocols = new String[supportedProtocols.size()]; supportProtocols = new String[supportedProtocols.size()];
supportedProtocols.toArray(supportProtocols); supportedProtocols.toArray(supportProtocols);
} }
sslSocket.setEnabledProtocols(supportProtocols); sslSocket.setEnabledProtocols(supportProtocols);
if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) { if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
disconnect(true); disconnect(true);
changeStatus(Account.State.SECURITY_ERROR); changeStatus(Account.State.SECURITY_ERROR);
} }
tagReader.setInputStream(sslSocket.getInputStream()); tagReader.setInputStream(sslSocket.getInputStream());
tagWriter.setOutputStream(sslSocket.getOutputStream()); tagWriter.setOutputStream(sslSocket.getOutputStream());
@ -521,11 +535,11 @@ public class XmppConnection implements Runnable {
enabledEncryption = true; enabledEncryption = true;
processStream(tagReader.readTag()); processStream(tagReader.readTag());
sslSocket.close(); sslSocket.close();
} catch (final NoSuchAlgorithmException | KeyManagementException e1) { } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
disconnect(true); disconnect(true);
changeStatus(Account.State.SECURITY_ERROR); changeStatus(Account.State.SECURITY_ERROR);
} }
} }
private void processStreamFeatures(final Tag currentTag) private void processStreamFeatures(final Tag currentTag)
@ -598,7 +612,7 @@ public class XmppConnection implements Runnable {
} }
private void sendRegistryRequest() { private void sendRegistryRequest() {
final IqPacket register = new IqPacket(IqPacket.TYPE_GET); final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
register.query("jabber:iq:register"); register.query("jabber:iq:register");
register.setTo(account.getServer()); register.setTo(account.getServer());
sendIqPacket(register, new OnIqPacketReceived() { sendIqPacket(register, new OnIqPacketReceived() {
@ -608,7 +622,7 @@ public class XmppConnection implements Runnable {
final Element instructions = packet.query().findChild("instructions"); final Element instructions = packet.query().findChild("instructions");
if (packet.query().hasChild("username") if (packet.query().hasChild("username")
&& (packet.query().hasChild("password"))) { && (packet.query().hasChild("password"))) {
final IqPacket register = new IqPacket(IqPacket.TYPE_SET); final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
final Element username = new Element("username") final Element username = new Element("username")
.setContent(account.getUsername()); .setContent(account.getUsername());
final Element password = new Element("password") final Element password = new Element("password")
@ -619,7 +633,7 @@ public class XmppConnection implements Runnable {
@Override @Override
public void onIqPacketReceived(final Account account, final IqPacket packet) { public void onIqPacketReceived(final Account account, final IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.setOption(Account.OPTION_REGISTER, account.setOption(Account.OPTION_REGISTER,
false); false);
changeStatus(Account.State.REGISTRATION_SUCCESSFUL); changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
@ -646,7 +660,7 @@ public class XmppConnection implements Runnable {
} }
private void sendBindRequest() { private void sendBindRequest() {
final IqPacket iq = new IqPacket(IqPacket.TYPE_SET); final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind") iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind")
.addChild("resource").setContent(account.getResource()); .addChild("resource").setContent(account.getResource());
this.sendUnmodifiedIqPacket(iq, new OnIqPacketReceived() { this.sendUnmodifiedIqPacket(iq, new OnIqPacketReceived() {
@ -693,7 +707,7 @@ public class XmppConnection implements Runnable {
}); });
if (this.streamFeatures.hasChild("session")) { if (this.streamFeatures.hasChild("session")) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending deprecated 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"); startSession.addChild("session","urn:ietf:params:xml:ns:xmpp-session");
this.sendUnmodifiedIqPacket(startSession, null); this.sendUnmodifiedIqPacket(startSession, null);
} }
@ -705,7 +719,7 @@ public class XmppConnection implements Runnable {
enableAdvancedStreamFeatures(); enableAdvancedStreamFeatures();
} }
} else { } else {
final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setTo(server.toDomainJid()); iq.setTo(server.toDomainJid());
iq.query("http://jabber.org/protocol/disco#info"); iq.query("http://jabber.org/protocol/disco#info");
this.sendIqPacket(iq, new OnIqPacketReceived() { this.sendIqPacket(iq, new OnIqPacketReceived() {
@ -750,7 +764,7 @@ public class XmppConnection implements Runnable {
} }
private void sendServiceDiscoveryItems(final Jid server) { private void sendServiceDiscoveryItems(final Jid server) {
final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setTo(server.toDomainJid()); iq.setTo(server.toDomainJid());
iq.query("http://jabber.org/protocol/disco#items"); iq.query("http://jabber.org/protocol/disco#items");
this.sendIqPacket(iq, new OnIqPacketReceived() { this.sendIqPacket(iq, new OnIqPacketReceived() {
@ -771,7 +785,7 @@ public class XmppConnection implements Runnable {
} }
private void sendEnableCarbons() { private void sendEnableCarbons() {
final IqPacket iq = new IqPacket(IqPacket.TYPE_SET); final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.addChild("enable", "urn:xmpp:carbons:2"); iq.addChild("enable", "urn:xmpp:carbons:2");
this.sendIqPacket(iq, new OnIqPacketReceived() { this.sendIqPacket(iq, new OnIqPacketReceived() {
@ -831,7 +845,7 @@ public class XmppConnection implements Runnable {
if (packet.getId() == null) { if (packet.getId() == null) {
packet.setId(nextRandomId()); packet.setId(nextRandomId());
} }
packetCallbacks.put(packet.getId(), callback); packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
} }
this.sendPacket(packet); this.sendPacket(packet);
} }
@ -861,7 +875,7 @@ public class XmppConnection implements Runnable {
if (streamFeatures.hasChild("sm")) { if (streamFeatures.hasChild("sm")) {
tagWriter.writeStanzaAsync(new RequestPacket(smVersion)); tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
} else { } else {
final IqPacket iq = new IqPacket(IqPacket.TYPE_GET); final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setFrom(account.getJid()); iq.setFrom(account.getJid());
iq.addChild("ping", "urn:xmpp:ping"); iq.addChild("ping", "urn:xmpp:ping");
this.sendIqPacket(iq, null); this.sendIqPacket(iq, null);

View File

@ -75,7 +75,7 @@ public class JingleConnection implements Downloadable {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_ERROR) { if (packet.getType() == IqPacket.TYPE.ERROR) {
fail(); fail();
} }
} }
@ -191,10 +191,10 @@ public class JingleConnection implements Downloadable {
} }
IqPacket response; IqPacket response;
if (returnResult) { if (returnResult) {
response = packet.generateResponse(IqPacket.TYPE_RESULT); response = packet.generateResponse(IqPacket.TYPE.RESULT);
} else { } else {
response = packet.generateResponse(IqPacket.TYPE_ERROR); response = packet.generateResponse(IqPacket.TYPE.ERROR);
} }
account.getXmppConnection().sendIqPacket(response, null); account.getXmppConnection().sendIqPacket(response, null);
} }
@ -552,7 +552,7 @@ public class JingleConnection implements Downloadable {
Log.d(Config.LOGTAG, "candidate " Log.d(Config.LOGTAG, "candidate "
+ connection.getCandidate().getCid() + connection.getCandidate().getCid()
+ " was our proxy. going to activate"); + " was our proxy. going to activate");
IqPacket activation = new IqPacket(IqPacket.TYPE_SET); IqPacket activation = new IqPacket(IqPacket.TYPE.SET);
activation.setTo(connection.getCandidate().getJid()); activation.setTo(connection.getCandidate().getJid());
activation.query("http://jabber.org/protocol/bytestreams") activation.query("http://jabber.org/protocol/bytestreams")
.setAttribute("sid", this.getSessionId()); .setAttribute("sid", this.getSessionId());
@ -564,7 +564,7 @@ public class JingleConnection implements Downloadable {
@Override @Override
public void onIqPacketReceived(Account account, public void onIqPacketReceived(Account account,
IqPacket packet) { IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_ERROR) { if (packet.getType() == IqPacket.TYPE.ERROR) {
onProxyActivated.failed(); onProxyActivated.failed();
} else { } else {
onProxyActivated.success(); onProxyActivated.success();

View File

@ -46,7 +46,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
return; return;
} }
} }
IqPacket response = packet.generateResponse(IqPacket.TYPE_ERROR); IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
Element error = response.addChild("error"); Element error = response.addChild("error");
error.setAttribute("type", "cancel"); error.setAttribute("type", "cancel");
error.addChild("item-not-found", error.addChild("item-not-found",
@ -84,7 +84,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
final String proxy = account.getXmppConnection() final String proxy = account.getXmppConnection()
.findDiscoItemByFeature(xmlns); .findDiscoItemByFeature(xmlns);
if (proxy != null) { if (proxy != null) {
IqPacket iq = new IqPacket(IqPacket.TYPE_GET); IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setAttribute("to", proxy); iq.setAttribute("to", proxy);
iq.query(xmlns); iq.query(xmlns);
account.getXmppConnection().sendIqPacket(iq, account.getXmppConnection().sendIqPacket(iq,

View File

@ -44,7 +44,7 @@ public class JingleInbandTransport extends JingleTransport {
private OnIqPacketReceived onAckReceived = new OnIqPacketReceived() { private OnIqPacketReceived onAckReceived = new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (connected && packet.getType() == IqPacket.TYPE_RESULT) { if (connected && packet.getType() == IqPacket.TYPE.RESULT) {
sendNextBlock(); sendNextBlock();
} }
} }
@ -60,7 +60,7 @@ public class JingleInbandTransport extends JingleTransport {
} }
public void connect(final OnTransportConnected callback) { public void connect(final OnTransportConnected callback) {
IqPacket iq = new IqPacket(IqPacket.TYPE_SET); IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.setTo(this.counterpart); iq.setTo(this.counterpart);
Element open = iq.addChild("open", "http://jabber.org/protocol/ibb"); Element open = iq.addChild("open", "http://jabber.org/protocol/ibb");
open.setAttribute("sid", this.sessionId); open.setAttribute("sid", this.sessionId);
@ -73,7 +73,7 @@ public class JingleInbandTransport extends JingleTransport {
@Override @Override
public void onIqPacketReceived(Account account, public void onIqPacketReceived(Account account,
IqPacket packet) { IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_ERROR) { if (packet.getType() == IqPacket.TYPE.ERROR) {
callback.failed(); callback.failed();
} else { } else {
callback.established(); callback.established();
@ -157,7 +157,7 @@ public class JingleInbandTransport extends JingleTransport {
this.remainingSize -= count; this.remainingSize -= count;
this.digest.update(buffer); this.digest.update(buffer);
String base64 = Base64.encodeToString(buffer, Base64.NO_WRAP); String base64 = Base64.encodeToString(buffer, Base64.NO_WRAP);
IqPacket iq = new IqPacket(IqPacket.TYPE_SET); IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.setTo(this.counterpart); iq.setTo(this.counterpart);
Element data = iq.addChild("data", Element data = iq.addChild("data",
"http://jabber.org/protocol/ibb"); "http://jabber.org/protocol/ibb");
@ -208,15 +208,15 @@ public class JingleInbandTransport extends JingleTransport {
established = true; established = true;
connected = true; connected = true;
this.account.getXmppConnection().sendIqPacket( this.account.getXmppConnection().sendIqPacket(
packet.generateResponse(IqPacket.TYPE_RESULT), null); packet.generateResponse(IqPacket.TYPE.RESULT), null);
} else { } else {
this.account.getXmppConnection().sendIqPacket( this.account.getXmppConnection().sendIqPacket(
packet.generateResponse(IqPacket.TYPE_ERROR), null); packet.generateResponse(IqPacket.TYPE.ERROR), null);
} }
} else if (connected && payload.getName().equals("data")) { } else if (connected && payload.getName().equals("data")) {
this.receiveNextBlock(payload.getContent()); this.receiveNextBlock(payload.getContent());
this.account.getXmppConnection().sendIqPacket( this.account.getXmppConnection().sendIqPacket(
packet.generateResponse(IqPacket.TYPE_RESULT), null); packet.generateResponse(IqPacket.TYPE.RESULT), null);
} else { } else {
// TODO some sort of exception // TODO some sort of exception
} }

View File

@ -1,11 +1,12 @@
package eu.siacs.conversations.xmpp.stanzas; package eu.siacs.conversations.xmpp.stanzas;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
public class AbstractStanza extends Element { public class AbstractStanza extends Element {
protected AbstractStanza(String name) { protected AbstractStanza(final String name) {
super(name); super(name);
} }
@ -36,4 +37,18 @@ public class AbstractStanza extends Element {
public void setId(final String id) { public void setId(final String id) {
setAttribute("id", 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());
}
} }

View File

@ -4,32 +4,18 @@ import eu.siacs.conversations.xml.Element;
public class IqPacket extends AbstractStanza { public class IqPacket extends AbstractStanza {
public static final int TYPE_ERROR = -1; public static enum TYPE {
public static final int TYPE_SET = 0; ERROR,
public static final int TYPE_RESULT = 1; SET,
public static final int TYPE_GET = 2; RESULT,
GET,
private IqPacket(final String name) { INVALID
super(name);
} }
public IqPacket(final int type) { public IqPacket(final TYPE type) {
super("iq"); super("iq");
switch (type) { if (type != TYPE.INVALID) {
case TYPE_SET: this.setAttribute("type", type.toString().toLowerCase());
this.setAttribute("type", "set");
break;
case TYPE_GET:
this.setAttribute("type", "get");
break;
case TYPE_RESULT:
this.setAttribute("type", "result");
break;
case TYPE_ERROR:
this.setAttribute("type", "error");
break;
default:
break;
} }
} }
@ -51,23 +37,23 @@ public class IqPacket extends AbstractStanza {
return query(); return query();
} }
public int getType() { public TYPE getType() {
final String type = getAttribute("type"); final String type = getAttribute("type");
switch (type) { switch (type) {
case "error": case "error":
return TYPE_ERROR; return TYPE.ERROR;
case "result": case "result":
return TYPE_RESULT; return TYPE.RESULT;
case "set": case "set":
return TYPE_SET; return TYPE.SET;
case "get": case "get":
return TYPE_GET; return TYPE.GET;
default: default:
return 1000; return TYPE.INVALID;
} }
} }
public IqPacket generateResponse(final int type) { public IqPacket generateResponse(final TYPE type) {
final IqPacket packet = new IqPacket(type); final IqPacket packet = new IqPacket(type);
packet.setTo(this.getFrom()); packet.setTo(this.getFrom());
packet.setId(this.getId()); packet.setId(this.getId());