2014-02-28 12:46:01 -05:00
|
|
|
package eu.siacs.conversations.xmpp;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
2014-11-03 04:03:45 -05:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Parcelable;
|
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.os.PowerManager.WakeLock;
|
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.util.Log;
|
2014-12-30 08:50:51 -05:00
|
|
|
import android.util.Pair;
|
2014-11-03 04:03:45 -05:00
|
|
|
import android.util.SparseArray;
|
|
|
|
|
|
|
|
import org.apache.http.conn.ssl.StrictHostnameVerifier;
|
2014-11-15 09:42:40 -05:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2014-11-03 04:03:45 -05:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
2014-01-30 10:42:35 -05:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.math.BigInteger;
|
2014-11-17 19:48:16 -05:00
|
|
|
import java.net.ConnectException;
|
2014-11-07 23:08:31 -05:00
|
|
|
import java.net.IDN;
|
2014-11-16 06:00:53 -05:00
|
|
|
import java.net.InetAddress;
|
2014-10-27 16:48:25 -04:00
|
|
|
import java.net.InetSocketAddress;
|
2014-01-30 10:42:35 -05:00
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2014-03-06 14:03:35 -05:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2014-03-14 23:59:18 -04:00
|
|
|
import java.util.ArrayList;
|
2014-10-18 15:56:59 -04:00
|
|
|
import java.util.Arrays;
|
2014-12-21 15:43:58 -05:00
|
|
|
import java.util.Collection;
|
2014-04-08 17:15:55 -04:00
|
|
|
import java.util.HashMap;
|
2014-01-31 19:25:56 -05:00
|
|
|
import java.util.Hashtable;
|
2015-08-06 08:54:37 -04:00
|
|
|
import java.util.Iterator;
|
2014-10-19 15:53:03 -04:00
|
|
|
import java.util.LinkedList;
|
2014-02-09 08:10:52 -05:00
|
|
|
import java.util.List;
|
2014-12-21 15:43:58 -05:00
|
|
|
import java.util.Map;
|
2014-04-08 17:15:55 -04:00
|
|
|
import java.util.Map.Entry;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
2014-07-22 11:27:44 -04:00
|
|
|
import javax.net.ssl.HostnameVerifier;
|
2014-03-06 14:03:35 -05:00
|
|
|
import javax.net.ssl.SSLContext;
|
2014-01-30 10:42:35 -05:00
|
|
|
import javax.net.ssl.SSLSocket;
|
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
2014-03-06 14:03:35 -05:00
|
|
|
import javax.net.ssl.X509TrustManager;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
2014-08-31 10:28:21 -04:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-11-12 10:15:38 -05:00
|
|
|
import eu.siacs.conversations.crypto.sasl.DigestMd5;
|
|
|
|
import eu.siacs.conversations.crypto.sasl.Plain;
|
|
|
|
import eu.siacs.conversations.crypto.sasl.SaslMechanism;
|
2014-11-12 15:35:44 -05:00
|
|
|
import eu.siacs.conversations.crypto.sasl.ScramSha1;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2014-12-21 15:43:58 -05:00
|
|
|
import eu.siacs.conversations.generator.IqGenerator;
|
2014-06-20 11:30:19 -04:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2015-01-14 12:20:02 -05:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.utils.DNSHelper;
|
2014-12-21 15:43:58 -05:00
|
|
|
import eu.siacs.conversations.utils.Xmlns;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
|
|
|
import eu.siacs.conversations.xml.Tag;
|
|
|
|
import eu.siacs.conversations.xml.TagWriter;
|
|
|
|
import eu.siacs.conversations.xml.XmlReader;
|
2014-11-05 15:55:47 -05:00
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-04-08 17:15:55 -04:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
|
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
|
2014-03-10 14:22:13 -04:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
|
2014-08-26 11:43:44 -04:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.csi.ActivePacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.csi.InactivePacket;
|
2014-03-10 14:22:13 -04:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.streammgmt.AckPacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.streammgmt.EnablePacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.streammgmt.RequestPacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.streammgmt.ResumePacket;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
|
|
|
public class XmppConnection implements Runnable {
|
|
|
|
|
2014-11-08 14:52:02 -05:00
|
|
|
private static final int PACKET_IQ = 0;
|
|
|
|
private static final int PACKET_MESSAGE = 1;
|
|
|
|
private static final int PACKET_PRESENCE = 2;
|
2014-01-30 10:42:35 -05:00
|
|
|
protected Account account;
|
2014-12-21 15:43:58 -05:00
|
|
|
private final WakeLock wakeLock;
|
2014-01-30 10:42:35 -05:00
|
|
|
private Socket socket;
|
|
|
|
private XmlReader tagReader;
|
|
|
|
private TagWriter tagWriter;
|
2014-12-21 15:43:58 -05:00
|
|
|
private final Features features = new Features(this);
|
2014-01-31 19:25:56 -05:00
|
|
|
private boolean shouldBind = true;
|
|
|
|
private boolean shouldAuthenticate = true;
|
|
|
|
private Element streamFeatures;
|
2015-04-25 12:24:10 -04:00
|
|
|
private final HashMap<Jid, Info> disco = new HashMap<>();
|
2014-11-05 15:55:47 -05:00
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
private String streamId = null;
|
2014-04-05 06:08:35 -04:00
|
|
|
private int smVersion = 3;
|
2015-08-06 14:48:55 -04:00
|
|
|
private final SparseArray<String> mStanzaReceipts = new SparseArray<>();
|
2014-11-05 15:55:47 -05:00
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
private int stanzasReceived = 0;
|
|
|
|
private int stanzasSent = 0;
|
2015-01-05 10:17:05 -05:00
|
|
|
private long lastPacketReceived = 0;
|
2014-08-19 09:06:50 -04:00
|
|
|
private long lastPingSent = 0;
|
|
|
|
private long lastConnect = 0;
|
|
|
|
private long lastSessionStarted = 0;
|
2014-05-18 05:25:04 -04:00
|
|
|
private int attempt = 0;
|
2014-12-30 08:50:51 -05:00
|
|
|
private final Map<String, Pair<IqPacket, OnIqPacketReceived>> packetCallbacks = new Hashtable<>();
|
2014-02-01 09:07:20 -05:00
|
|
|
private OnPresencePacketReceived presenceListener = null;
|
2014-03-26 21:02:59 -04:00
|
|
|
private OnJinglePacketReceived jingleListener = null;
|
2014-02-01 09:07:20 -05:00
|
|
|
private OnIqPacketReceived unregisteredIqListener = null;
|
|
|
|
private OnMessagePacketReceived messageListener = null;
|
2014-02-04 09:09:50 -05:00
|
|
|
private OnStatusChanged statusListener = null;
|
2014-03-14 15:43:54 -04:00
|
|
|
private OnBindListener bindListener = null;
|
2014-12-21 15:43:58 -05:00
|
|
|
private final ArrayList<OnAdvancedStreamFeaturesLoaded> advancedStreamFeaturesLoadedListeners = new ArrayList<>();
|
2014-08-26 10:52:42 -04:00
|
|
|
private OnMessageAcknowledged acknowledgedListener = null;
|
2014-11-09 04:50:45 -05:00
|
|
|
private XmppConnectionService mXmppConnectionService = null;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
2014-11-12 15:35:44 -05:00
|
|
|
private SaslMechanism saslMechanism;
|
2014-11-12 10:15:38 -05:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public XmppConnection(final Account account, final XmppConnectionService service) {
|
2014-01-30 10:42:35 -05:00
|
|
|
this.account = account;
|
2014-08-31 10:28:21 -04:00
|
|
|
this.wakeLock = service.getPowerManager().newWakeLock(
|
2014-11-12 15:35:44 -05:00
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, account.getJid().toBareJid().toString());
|
2014-01-30 10:42:35 -05:00
|
|
|
tagWriter = new TagWriter();
|
2014-11-09 04:50:45 -05:00
|
|
|
mXmppConnectionService = service;
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
2014-11-15 11:09:02 -05:00
|
|
|
protected void changeStatus(final Account.State nextStatus) {
|
2014-03-11 11:27:33 -04:00
|
|
|
if (account.getStatus() != nextStatus) {
|
2014-11-15 11:09:02 -05:00
|
|
|
if ((nextStatus == Account.State.OFFLINE)
|
|
|
|
&& (account.getStatus() != Account.State.CONNECTING)
|
|
|
|
&& (account.getStatus() != Account.State.ONLINE)
|
|
|
|
&& (account.getStatus() != Account.State.DISABLED)) {
|
2014-03-13 12:29:22 -04:00
|
|
|
return;
|
2014-12-21 15:43:58 -05:00
|
|
|
}
|
2014-11-15 11:09:02 -05:00
|
|
|
if (nextStatus == Account.State.ONLINE) {
|
2014-05-18 05:25:04 -04:00
|
|
|
this.attempt = 0;
|
|
|
|
}
|
2014-03-11 11:27:33 -04:00
|
|
|
account.setStatus(nextStatus);
|
|
|
|
if (statusListener != null) {
|
|
|
|
statusListener.onStatusChanged(account);
|
|
|
|
}
|
2014-03-05 21:57:29 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
|
|
|
|
protected void connect() {
|
2014-11-09 10:57:22 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting");
|
2015-01-05 10:17:05 -05:00
|
|
|
features.encryptionEnabled = false;
|
2014-03-12 17:31:50 -04:00
|
|
|
lastConnect = SystemClock.elapsedRealtime();
|
2014-08-19 09:06:50 -04:00
|
|
|
lastPingSent = SystemClock.elapsedRealtime();
|
2014-05-18 05:25:04 -04:00
|
|
|
this.attempt++;
|
2014-01-30 10:42:35 -05:00
|
|
|
try {
|
2015-01-05 10:17:05 -05:00
|
|
|
shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER);
|
2014-03-08 14:14:47 -05:00
|
|
|
tagReader = new XmlReader(wakeLock);
|
|
|
|
tagWriter = new TagWriter();
|
|
|
|
packetCallbacks.clear();
|
2014-11-15 11:09:02 -05:00
|
|
|
this.changeStatus(Account.State.CONNECTING);
|
2015-06-19 10:25:08 -04:00
|
|
|
if (DNSHelper.isIp(account.getServer().toString())) {
|
|
|
|
socket = new Socket();
|
|
|
|
try {
|
|
|
|
socket.connect(new InetSocketAddress(account.getServer().toString(), 5222), Config.SOCKET_TIMEOUT * 1000);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new UnknownHostException();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final Bundle result = DNSHelper.getSRVRecord(account.getServer());
|
2015-07-24 13:06:47 -04:00
|
|
|
if (result == null) {
|
|
|
|
throw new IOException("unhandled exception in DNS resolver");
|
|
|
|
}
|
2015-06-19 10:25:08 -04:00
|
|
|
final ArrayList<Parcelable> values = result.getParcelableArrayList("values");
|
|
|
|
if ("timeout".equals(result.getString("error"))) {
|
|
|
|
throw new IOException("timeout in dns");
|
|
|
|
} else if (values != null) {
|
|
|
|
int i = 0;
|
|
|
|
boolean socketError = true;
|
|
|
|
while (socketError && values.size() > i) {
|
|
|
|
final Bundle namePort = (Bundle) values.get(i);
|
2014-11-12 15:35:44 -05:00
|
|
|
try {
|
2015-06-19 10:25:08 -04:00
|
|
|
String srvRecordServer;
|
|
|
|
try {
|
|
|
|
srvRecordServer = IDN.toASCII(namePort.getString("name"));
|
|
|
|
} catch (final IllegalArgumentException e) {
|
|
|
|
// TODO: Handle me?`
|
|
|
|
srvRecordServer = "";
|
|
|
|
}
|
|
|
|
final int srvRecordPort = namePort.getInt("port");
|
|
|
|
final String srvIpServer = namePort.getString("ip");
|
|
|
|
final InetSocketAddress addr;
|
|
|
|
if (srvIpServer != null) {
|
|
|
|
addr = new InetSocketAddress(srvIpServer, srvRecordPort);
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
|
|
|
+ ": using values from dns " + srvRecordServer
|
|
|
|
+ "[" + srvIpServer + "]:" + srvRecordPort);
|
|
|
|
} else {
|
|
|
|
addr = new InetSocketAddress(srvRecordServer, srvRecordPort);
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
|
|
|
+ ": using values from dns "
|
|
|
|
+ srvRecordServer + ":" + srvRecordPort);
|
|
|
|
}
|
|
|
|
socket = new Socket();
|
|
|
|
socket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
|
|
|
|
socketError = false;
|
|
|
|
} catch (final UnknownHostException e) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
|
|
|
i++;
|
|
|
|
} catch (final IOException e) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
|
|
|
i++;
|
2014-10-16 17:31:48 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-19 10:25:08 -04:00
|
|
|
if (socketError) {
|
|
|
|
throw new UnknownHostException();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new IOException("unhandled exception in DNS resolver");
|
2014-11-03 04:03:45 -05:00
|
|
|
}
|
2014-02-07 00:52:09 -05:00
|
|
|
}
|
2014-12-21 15:43:58 -05:00
|
|
|
final OutputStream out = socket.getOutputStream();
|
2014-01-30 10:42:35 -05:00
|
|
|
tagWriter.setOutputStream(out);
|
2014-12-21 15:43:58 -05:00
|
|
|
final InputStream in = socket.getInputStream();
|
2014-01-30 10:42:35 -05:00
|
|
|
tagReader.setInputStream(in);
|
|
|
|
tagWriter.beginDocument();
|
|
|
|
sendStartStream();
|
|
|
|
Tag nextTag;
|
|
|
|
while ((nextTag = tagReader.readTag()) != null) {
|
|
|
|
if (nextTag.isStart("stream")) {
|
|
|
|
processStream(nextTag);
|
2014-02-04 09:09:50 -05:00
|
|
|
break;
|
2014-01-30 10:42:35 -05:00
|
|
|
} else {
|
2014-11-17 19:48:16 -05:00
|
|
|
throw new IOException("unknown tag on connect");
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 09:09:50 -05:00
|
|
|
if (socket.isConnected()) {
|
|
|
|
socket.close();
|
|
|
|
}
|
2015-05-08 00:30:06 -04:00
|
|
|
} catch (final IncompatibleServerException e) {
|
|
|
|
this.changeStatus(Account.State.INCOMPATIBLE_SERVER);
|
|
|
|
} catch (final SecurityException e) {
|
|
|
|
this.changeStatus(Account.State.SECURITY_ERROR);
|
|
|
|
} catch (final UnauthorizedException e) {
|
|
|
|
this.changeStatus(Account.State.UNAUTHORIZED);
|
2014-12-21 15:43:58 -05:00
|
|
|
} catch (final UnknownHostException | ConnectException e) {
|
2014-11-17 19:48:16 -05:00
|
|
|
this.changeStatus(Account.State.SERVER_NOT_FOUND);
|
2014-12-21 15:43:58 -05:00
|
|
|
} catch (final IOException | XmlPullParserException | NoSuchAlgorithmException e) {
|
2014-11-09 10:57:22 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
2014-11-15 11:09:02 -05:00
|
|
|
this.changeStatus(Account.State.OFFLINE);
|
2015-01-29 17:35:31 -05:00
|
|
|
this.attempt--; //don't count attempt when reconnecting instantly anyway
|
2014-11-17 19:48:16 -05:00
|
|
|
} finally {
|
2015-05-08 00:30:06 -04:00
|
|
|
if (socket != null) {
|
|
|
|
try {
|
|
|
|
socket.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 12:16:14 -04:00
|
|
|
if (wakeLock.isHeld()) {
|
2014-08-31 10:28:21 -04:00
|
|
|
try {
|
|
|
|
wakeLock.release();
|
2014-11-05 15:55:47 -05:00
|
|
|
} catch (final RuntimeException ignored) {
|
2014-08-31 10:28:21 -04:00
|
|
|
}
|
2014-03-05 21:30:03 -05:00
|
|
|
}
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-31 19:25:56 -05:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-03-05 09:46:33 -05:00
|
|
|
try {
|
|
|
|
if (socket != null) {
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
} catch (final IOException ignored) {
|
|
|
|
|
|
|
|
}
|
2014-03-05 21:30:03 -05:00
|
|
|
connect();
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
2014-11-05 15:55:47 -05:00
|
|
|
private void processStream(final Tag currentTag) throws XmlPullParserException,
|
2014-11-12 15:35:44 -05:00
|
|
|
IOException, NoSuchAlgorithmException {
|
|
|
|
Tag nextTag = tagReader.readTag();
|
|
|
|
|
|
|
|
while ((nextTag != null) && (!nextTag.isEnd("stream"))) {
|
|
|
|
if (nextTag.isStart("error")) {
|
|
|
|
processStreamError(nextTag);
|
|
|
|
} else if (nextTag.isStart("features")) {
|
|
|
|
processStreamFeatures(nextTag);
|
|
|
|
} else if (nextTag.isStart("proceed")) {
|
|
|
|
switchOverToTls(nextTag);
|
|
|
|
} else if (nextTag.isStart("success")) {
|
|
|
|
final String challenge = tagReader.readElement(nextTag).getContent();
|
|
|
|
try {
|
|
|
|
saslMechanism.getResponse(challenge);
|
2014-11-15 08:48:40 -05:00
|
|
|
} catch (final SaslMechanism.AuthenticationException e) {
|
2014-11-12 15:35:44 -05:00
|
|
|
disconnect(true);
|
|
|
|
Log.e(Config.LOGTAG, String.valueOf(e));
|
|
|
|
}
|
2014-11-15 08:48:40 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
|
2014-11-15 09:42:40 -05:00
|
|
|
account.setKey(Account.PINNED_MECHANISM_KEY,
|
2014-12-21 15:43:58 -05:00
|
|
|
String.valueOf(saslMechanism.getPriority()));
|
2014-11-12 15:35:44 -05:00
|
|
|
tagReader.reset();
|
|
|
|
sendStartStream();
|
|
|
|
processStream(tagReader.readTag());
|
|
|
|
break;
|
|
|
|
} else if (nextTag.isStart("failure")) {
|
2015-05-08 00:30:06 -04:00
|
|
|
throw new UnauthorizedException();
|
2014-11-12 15:35:44 -05:00
|
|
|
} else if (nextTag.isStart("challenge")) {
|
|
|
|
final String challenge = tagReader.readElement(nextTag).getContent();
|
|
|
|
final Element response = new Element("response");
|
|
|
|
response.setAttribute("xmlns",
|
|
|
|
"urn:ietf:params:xml:ns:xmpp-sasl");
|
|
|
|
try {
|
|
|
|
response.setContent(saslMechanism.getResponse(challenge));
|
2014-11-15 08:48:40 -05:00
|
|
|
} catch (final SaslMechanism.AuthenticationException e) {
|
2014-11-12 15:35:44 -05:00
|
|
|
// TODO: Send auth abort tag.
|
|
|
|
Log.e(Config.LOGTAG, e.toString());
|
|
|
|
}
|
|
|
|
tagWriter.writeElement(response);
|
|
|
|
} else if (nextTag.isStart("enabled")) {
|
2014-12-21 15:43:58 -05:00
|
|
|
final Element enabled = tagReader.readElement(nextTag);
|
2014-11-12 15:35:44 -05:00
|
|
|
if ("true".equals(enabled.getAttribute("resume"))) {
|
|
|
|
this.streamId = enabled.getAttribute("id");
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
|
|
|
+ ": stream managment(" + smVersion
|
|
|
|
+ ") enabled (resumable)");
|
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
|
|
|
+ ": stream managment(" + smVersion + ") enabled");
|
|
|
|
}
|
|
|
|
this.lastSessionStarted = SystemClock.elapsedRealtime();
|
|
|
|
this.stanzasReceived = 0;
|
2014-12-21 15:43:58 -05:00
|
|
|
final RequestPacket r = new RequestPacket(smVersion);
|
2014-11-12 15:35:44 -05:00
|
|
|
tagWriter.writeStanzaAsync(r);
|
|
|
|
} else if (nextTag.isStart("resumed")) {
|
2015-01-05 10:17:05 -05:00
|
|
|
lastPacketReceived = SystemClock.elapsedRealtime();
|
2014-12-21 15:43:58 -05:00
|
|
|
final Element resumed = tagReader.readElement(nextTag);
|
|
|
|
final String h = resumed.getAttribute("h");
|
2014-11-12 15:35:44 -05:00
|
|
|
try {
|
2014-12-21 15:43:58 -05:00
|
|
|
final int serverCount = Integer.parseInt(h);
|
2014-11-12 15:35:44 -05:00
|
|
|
if (serverCount != stanzasSent) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
|
|
|
+ ": session resumed with lost packages");
|
|
|
|
stanzasSent = serverCount;
|
|
|
|
} else {
|
2015-08-06 14:48:55 -04:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed");
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2015-08-06 14:48:55 -04:00
|
|
|
acknowledgeStanzaUpTo(serverCount);
|
|
|
|
ArrayList<IqPacket> failedIqPackets = new ArrayList<>();
|
|
|
|
for(int i = 0; i < this.mStanzaReceipts.size(); ++i) {
|
|
|
|
String id = mStanzaReceipts.valueAt(i);
|
|
|
|
Pair<IqPacket,OnIqPacketReceived> pair = id == null ? null : this.packetCallbacks.get(id);
|
|
|
|
if (pair != null) {
|
|
|
|
failedIqPackets.add(pair.first);
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
|
|
|
}
|
2015-08-06 14:48:55 -04:00
|
|
|
mStanzaReceipts.clear();
|
|
|
|
Log.d(Config.LOGTAG,"resending "+failedIqPackets.size()+" iq stanza");
|
|
|
|
for(IqPacket packet : failedIqPackets) {
|
|
|
|
sendUnmodifiedIqPacket(packet,null);
|
|
|
|
}
|
2014-11-12 15:35:44 -05:00
|
|
|
} catch (final NumberFormatException ignored) {
|
|
|
|
}
|
|
|
|
sendInitialPing();
|
|
|
|
} else if (nextTag.isStart("r")) {
|
|
|
|
tagReader.readElement(nextTag);
|
2015-04-29 10:15:07 -04:00
|
|
|
if (Config.EXTENDED_SM_LOGGING) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": acknowledging stanza #" + this.stanzasReceived);
|
|
|
|
}
|
2014-12-21 15:43:58 -05:00
|
|
|
final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
|
2014-11-12 15:35:44 -05:00
|
|
|
tagWriter.writeStanzaAsync(ack);
|
|
|
|
} else if (nextTag.isStart("a")) {
|
2014-12-21 15:43:58 -05:00
|
|
|
final Element ack = tagReader.readElement(nextTag);
|
2015-01-05 10:17:05 -05:00
|
|
|
lastPacketReceived = SystemClock.elapsedRealtime();
|
2015-07-10 06:09:59 -04:00
|
|
|
try {
|
|
|
|
final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
|
2015-08-06 14:48:55 -04:00
|
|
|
acknowledgeStanzaUpTo(serverSequence);
|
2015-07-10 06:09:59 -04:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": server send ack without sequence number");
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
|
|
|
} else if (nextTag.isStart("failed")) {
|
|
|
|
tagReader.readElement(nextTag);
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": resumption failed");
|
|
|
|
streamId = null;
|
2014-11-15 11:09:02 -05:00
|
|
|
if (account.getStatus() != Account.State.ONLINE) {
|
2014-11-12 15:35:44 -05:00
|
|
|
sendBindRequest();
|
|
|
|
}
|
|
|
|
} else if (nextTag.isStart("iq")) {
|
|
|
|
processIq(nextTag);
|
|
|
|
} else if (nextTag.isStart("message")) {
|
|
|
|
processMessage(nextTag);
|
|
|
|
} else if (nextTag.isStart("presence")) {
|
|
|
|
processPresence(nextTag);
|
|
|
|
}
|
|
|
|
nextTag = tagReader.readTag();
|
|
|
|
}
|
2014-11-15 11:09:02 -05:00
|
|
|
if (account.getStatus() == Account.State.ONLINE) {
|
|
|
|
account. setStatus(Account.State.OFFLINE);
|
2014-11-12 15:35:44 -05:00
|
|
|
if (statusListener != null) {
|
|
|
|
statusListener.onStatusChanged(account);
|
2014-08-27 08:51:00 -04:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
2014-10-05 08:26:06 -04:00
|
|
|
|
2015-08-06 14:48:55 -04:00
|
|
|
private void acknowledgeStanzaUpTo(int serverCount) {
|
|
|
|
for (int i = 0; i < mStanzaReceipts.size(); ++i) {
|
|
|
|
if (serverCount >= mStanzaReceipts.keyAt(i)) {
|
|
|
|
if (Config.EXTENDED_SM_LOGGING) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server acknowledged stanza #" + mStanzaReceipts.keyAt(i));
|
|
|
|
}
|
|
|
|
String id = mStanzaReceipts.valueAt(i);
|
|
|
|
if (acknowledgedListener != null) {
|
|
|
|
acknowledgedListener.onMessageAcknowledged(account, id);
|
|
|
|
}
|
|
|
|
mStanzaReceipts.removeAt(i);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 04:46:31 -04:00
|
|
|
private void sendInitialPing() {
|
2014-11-09 10:57:22 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping");
|
2014-12-30 08:16:25 -05:00
|
|
|
final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
|
2014-11-09 10:57:22 -05:00
|
|
|
iq.setFrom(account.getJid());
|
2014-10-05 04:46:31 -04:00
|
|
|
iq.addChild("ping", "urn:xmpp:ping");
|
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
2014-10-20 15:08:33 -04:00
|
|
|
@Override
|
2014-12-21 15:43:58 -05:00
|
|
|
public void onIqPacketReceived(final Account account, final IqPacket packet) {
|
2014-11-09 10:57:22 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
2014-10-20 15:08:33 -04:00
|
|
|
+ ": online with resource " + account.getResource());
|
2014-11-15 11:09:02 -05:00
|
|
|
changeStatus(Account.State.ONLINE);
|
2014-10-20 15:08:33 -04:00
|
|
|
}
|
|
|
|
});
|
2014-10-05 04:46:31 -04:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
private Element processPacket(final Tag currentTag, final int packetType)
|
2014-11-12 15:35:44 -05:00
|
|
|
throws XmlPullParserException, IOException {
|
2014-01-30 18:33:01 -05:00
|
|
|
Element element;
|
|
|
|
switch (packetType) {
|
2014-11-03 04:03:45 -05:00
|
|
|
case PACKET_IQ:
|
|
|
|
element = new IqPacket();
|
|
|
|
break;
|
|
|
|
case PACKET_MESSAGE:
|
|
|
|
element = new MessagePacket();
|
|
|
|
break;
|
|
|
|
case PACKET_PRESENCE:
|
|
|
|
element = new PresencePacket();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null;
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
2014-01-30 18:33:01 -05:00
|
|
|
element.setAttributes(currentTag.getAttributes());
|
2014-01-30 10:42:35 -05:00
|
|
|
Tag nextTag = tagReader.readTag();
|
2014-08-31 10:28:21 -04:00
|
|
|
if (nextTag == null) {
|
2014-05-23 04:15:58 -04:00
|
|
|
throw new IOException("interrupted mid tag");
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
while (!nextTag.isEnd(element.getName())) {
|
2014-01-30 18:33:01 -05:00
|
|
|
if (!nextTag.isNo()) {
|
2014-12-21 15:43:58 -05:00
|
|
|
final Element child = tagReader.readElement(nextTag);
|
|
|
|
final String type = currentTag.getAttribute("type");
|
2014-10-05 08:26:06 -04:00
|
|
|
if (packetType == PACKET_IQ
|
|
|
|
&& "jingle".equals(child.getName())
|
|
|
|
&& ("set".equalsIgnoreCase(type) || "get"
|
2014-11-12 15:35:44 -05:00
|
|
|
.equalsIgnoreCase(type))) {
|
2014-03-26 21:02:59 -04:00
|
|
|
element = new JinglePacket();
|
|
|
|
element.setAttributes(currentTag.getAttributes());
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2014-01-30 18:33:01 -05:00
|
|
|
element.addChild(child);
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
nextTag = tagReader.readTag();
|
2014-08-31 10:28:21 -04:00
|
|
|
if (nextTag == null) {
|
2014-05-23 04:15:58 -04:00
|
|
|
throw new IOException("interrupted mid tag");
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
2015-05-05 22:29:45 -04:00
|
|
|
if (stanzasReceived == Integer.MAX_VALUE) {
|
|
|
|
resetStreamId();
|
|
|
|
throw new IOException("time to restart the session. cant handle >2 billion pcks");
|
|
|
|
}
|
2014-03-10 14:22:13 -04:00
|
|
|
++stanzasReceived;
|
2015-01-05 10:17:05 -05:00
|
|
|
lastPacketReceived = SystemClock.elapsedRealtime();
|
2014-01-30 18:33:01 -05:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2015-01-04 09:40:09 -05:00
|
|
|
private void processIq(final Tag currentTag) throws XmlPullParserException, IOException {
|
|
|
|
final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2015-01-04 09:40:09 -05:00
|
|
|
if (packet.getId() == null) {
|
|
|
|
return; // an iq packet without id is definitely invalid
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2015-01-04 09:40:09 -05:00
|
|
|
if (packet instanceof JinglePacket) {
|
|
|
|
if (this.jingleListener != null) {
|
|
|
|
this.jingleListener.onJinglePacketReceived(account,(JinglePacket) packet);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (packetCallbacks.containsKey(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 {
|
|
|
|
Log.e(Config.LOGTAG,account.getJid().toBareJid().toString()+": ignoring spoofed iq packet");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
|
|
|
|
packetCallbackDuple.second.onIqPacketReceived(account, packet);
|
|
|
|
packetCallbacks.remove(packet.getId());
|
|
|
|
} else {
|
|
|
|
Log.e(Config.LOGTAG,account.getJid().toBareJid().toString()+": ignoring spoofed iq packet");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (packet.getType() == IqPacket.TYPE.GET|| packet.getType() == IqPacket.TYPE.SET) {
|
|
|
|
this.unregisteredIqListener.onIqPacketReceived(account, packet);
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 18:33:01 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2015-01-04 06:09:39 -05:00
|
|
|
private void processMessage(final Tag currentTag) throws XmlPullParserException, IOException {
|
|
|
|
final MessagePacket packet = (MessagePacket) processPacket(currentTag,PACKET_MESSAGE);
|
|
|
|
this.messageListener.onMessagePacketReceived(account, packet);
|
2014-01-30 18:33:01 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2015-01-04 06:09:39 -05:00
|
|
|
private void processPresence(final Tag currentTag) throws XmlPullParserException, IOException {
|
|
|
|
PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE);
|
|
|
|
this.presenceListener.onPresencePacketReceived(account, packet);
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
private void sendStartTLS() throws IOException {
|
2014-12-21 15:43:58 -05:00
|
|
|
final Tag startTLS = Tag.empty("starttls");
|
2014-01-30 10:42:35 -05:00
|
|
|
startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls");
|
2014-02-04 09:09:50 -05:00
|
|
|
tagWriter.writeTag(startTLS);
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 19:17:11 -05:00
|
|
|
private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
|
2014-12-30 08:16:25 -05:00
|
|
|
tagReader.readTag();
|
|
|
|
try {
|
2014-12-29 19:17:11 -05:00
|
|
|
final SSLContext sc = SSLContext.getInstance("TLS");
|
|
|
|
sc.init(null,new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},mXmppConnectionService.getRNG());
|
|
|
|
final SSLSocketFactory factory = sc.getSocketFactory();
|
|
|
|
final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier());
|
2014-12-30 08:16:25 -05:00
|
|
|
final InetAddress address = socket == null ? null : socket.getInetAddress();
|
2014-12-29 19:17:11 -05:00
|
|
|
|
|
|
|
if (factory == null || address == null || verifier == null) {
|
|
|
|
throw new IOException("could not setup ssl");
|
|
|
|
}
|
2014-11-03 04:03:45 -05:00
|
|
|
|
2014-12-30 08:16:25 -05:00
|
|
|
final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true);
|
2014-11-08 14:52:02 -05:00
|
|
|
|
2014-12-29 19:17:11 -05:00
|
|
|
if (sslSocket == null) {
|
|
|
|
throw new IOException("could not initialize ssl socket");
|
|
|
|
}
|
2014-11-16 06:00:53 -05:00
|
|
|
|
2014-12-29 19:17:11 -05:00
|
|
|
final String[] supportProtocols;
|
2015-01-17 13:40:15 -05:00
|
|
|
final Collection<String> supportedProtocols = new LinkedList<>(
|
|
|
|
Arrays.asList(sslSocket.getSupportedProtocols()));
|
|
|
|
supportedProtocols.remove("SSLv3");
|
|
|
|
supportProtocols = supportedProtocols.toArray(new String[supportedProtocols.size()]);
|
|
|
|
|
2014-12-29 19:17:11 -05:00
|
|
|
sslSocket.setEnabledProtocols(supportProtocols);
|
|
|
|
|
2015-02-02 08:33:55 -05:00
|
|
|
final String[] cipherSuites = CryptoHelper.getOrderedCipherSuites(
|
2015-01-17 13:40:15 -05:00
|
|
|
sslSocket.getSupportedCipherSuites());
|
2015-02-09 10:41:13 -05:00
|
|
|
//Log.d(Config.LOGTAG, "Using ciphers: " + Arrays.toString(cipherSuites));
|
2015-01-17 13:40:15 -05:00
|
|
|
if (cipherSuites.length > 0) {
|
|
|
|
sslSocket.setEnabledCipherSuites(cipherSuites);
|
|
|
|
}
|
|
|
|
|
2014-12-30 08:16:25 -05:00
|
|
|
if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) {
|
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
|
2015-05-08 00:30:06 -04:00
|
|
|
throw new SecurityException();
|
2014-12-29 19:17:11 -05:00
|
|
|
}
|
|
|
|
tagReader.setInputStream(sslSocket.getInputStream());
|
|
|
|
tagWriter.setOutputStream(sslSocket.getOutputStream());
|
|
|
|
sendStartStream();
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": TLS connection established");
|
2015-01-05 10:17:05 -05:00
|
|
|
features.encryptionEnabled = true;
|
2014-12-29 19:17:11 -05:00
|
|
|
processStream(tagReader.readTag());
|
|
|
|
sslSocket.close();
|
2014-12-30 08:16:25 -05:00
|
|
|
} catch (final NoSuchAlgorithmException | KeyManagementException e1) {
|
2014-12-29 19:17:11 -05:00
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
|
2015-05-08 00:30:06 -04:00
|
|
|
throw new SecurityException();
|
2014-12-30 08:16:25 -05:00
|
|
|
}
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
private void processStreamFeatures(final Tag currentTag)
|
2014-11-12 15:35:44 -05:00
|
|
|
throws XmlPullParserException, IOException {
|
2014-01-31 19:25:56 -05:00
|
|
|
this.streamFeatures = tagReader.readElement(currentTag);
|
2015-01-05 10:17:05 -05:00
|
|
|
if (this.streamFeatures.hasChild("starttls") && !features.encryptionEnabled) {
|
2014-01-31 19:25:56 -05:00
|
|
|
sendStartTLS();
|
2014-05-21 16:22:36 -04:00
|
|
|
} else if (this.streamFeatures.hasChild("register")
|
2014-10-20 15:08:33 -04:00
|
|
|
&& account.isOptionSet(Account.OPTION_REGISTER)
|
2015-01-05 10:17:05 -05:00
|
|
|
&& features.encryptionEnabled) {
|
2014-05-21 16:22:36 -04:00
|
|
|
sendRegistryRequest();
|
|
|
|
} else if (!this.streamFeatures.hasChild("register")
|
2014-10-19 17:14:17 -04:00
|
|
|
&& account.isOptionSet(Account.OPTION_REGISTER)) {
|
2014-11-15 11:09:02 -05:00
|
|
|
changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED);
|
2014-03-14 13:56:52 -04:00
|
|
|
disconnect(true);
|
2014-02-09 08:10:52 -05:00
|
|
|
} else if (this.streamFeatures.hasChild("mechanisms")
|
2015-01-05 10:17:05 -05:00
|
|
|
&& shouldAuthenticate && features.encryptionEnabled) {
|
2014-11-12 10:15:38 -05:00
|
|
|
final List<String> mechanisms = extractMechanisms(streamFeatures
|
2014-05-21 16:22:36 -04:00
|
|
|
.findChild("mechanisms"));
|
2014-11-12 15:35:44 -05:00
|
|
|
final Element auth = new Element("auth");
|
|
|
|
auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
|
2014-11-15 09:42:40 -05:00
|
|
|
if (mechanisms.contains("SCRAM-SHA-1")) {
|
2014-11-12 15:35:44 -05:00
|
|
|
saslMechanism = new ScramSha1(tagWriter, account, mXmppConnectionService.getRNG());
|
2014-11-15 09:42:40 -05:00
|
|
|
} else if (mechanisms.contains("PLAIN")) {
|
2014-11-12 15:35:44 -05:00
|
|
|
saslMechanism = new Plain(tagWriter, account);
|
2015-01-01 19:39:19 -05:00
|
|
|
} else if (mechanisms.contains("DIGEST-MD5")) {
|
|
|
|
saslMechanism = new DigestMd5(tagWriter, account, mXmppConnectionService.getRNG());
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2015-05-02 06:10:56 -04:00
|
|
|
if (saslMechanism != null) {
|
|
|
|
final JSONObject keys = account.getKeys();
|
|
|
|
try {
|
|
|
|
if (keys.has(Account.PINNED_MECHANISM_KEY) &&
|
|
|
|
keys.getInt(Account.PINNED_MECHANISM_KEY) > saslMechanism.getPriority()) {
|
|
|
|
Log.e(Config.LOGTAG, "Auth failed. Authentication mechanism " + saslMechanism.getMechanism() +
|
|
|
|
" has lower priority (" + String.valueOf(saslMechanism.getPriority()) +
|
|
|
|
") than pinned priority (" + keys.getInt(Account.PINNED_MECHANISM_KEY) +
|
|
|
|
"). Possible downgrade attack?");
|
2015-05-08 00:30:06 -04:00
|
|
|
throw new SecurityException();
|
2015-05-02 06:10:56 -04:00
|
|
|
}
|
|
|
|
} catch (final JSONException e) {
|
|
|
|
Log.d(Config.LOGTAG, "Parse error while checking pinned auth mechanism");
|
|
|
|
}
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toString() + ": Authenticating with " + saslMechanism.getMechanism());
|
|
|
|
auth.setAttribute("mechanism", saslMechanism.getMechanism());
|
|
|
|
if (!saslMechanism.getClientFirstMessage().isEmpty()) {
|
|
|
|
auth.setContent(saslMechanism.getClientFirstMessage());
|
|
|
|
}
|
|
|
|
tagWriter.writeElement(auth);
|
|
|
|
} else {
|
2015-05-08 00:30:06 -04:00
|
|
|
throw new IncompatibleServerException();
|
2014-11-15 09:42:40 -05:00
|
|
|
}
|
2014-11-12 15:35:44 -05:00
|
|
|
} else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:"
|
|
|
|
+ smVersion)
|
2014-05-21 16:22:36 -04:00
|
|
|
&& streamId != null) {
|
2015-04-29 10:15:07 -04:00
|
|
|
if (Config.EXTENDED_SM_LOGGING) {
|
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": resuming after stanza #"+stanzasReceived);
|
|
|
|
}
|
|
|
|
final ResumePacket resume = new ResumePacket(this.streamId, stanzasReceived, smVersion);
|
2014-03-10 14:22:13 -04:00
|
|
|
this.tagWriter.writeStanzaAsync(resume);
|
|
|
|
} else if (this.streamFeatures.hasChild("bind") && shouldBind) {
|
2014-01-31 19:25:56 -05:00
|
|
|
sendBindRequest();
|
2014-10-19 17:14:17 -04:00
|
|
|
} else {
|
|
|
|
disconnect(true);
|
2014-11-15 14:54:28 -05:00
|
|
|
changeStatus(Account.State.INCOMPATIBLE_SERVER);
|
2014-03-10 14:22:13 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
private List<String> extractMechanisms(final Element stream) {
|
|
|
|
final ArrayList<String> mechanisms = new ArrayList<>(stream
|
2014-05-21 16:22:36 -04:00
|
|
|
.getChildren().size());
|
2014-12-21 15:43:58 -05:00
|
|
|
for (final Element child : stream.getChildren()) {
|
2014-03-30 15:21:55 -04:00
|
|
|
mechanisms.add(child.getContent());
|
|
|
|
}
|
|
|
|
return mechanisms;
|
|
|
|
}
|
|
|
|
|
2014-03-14 13:56:52 -04:00
|
|
|
private void sendRegistryRequest() {
|
2014-12-30 08:16:25 -05:00
|
|
|
final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
|
2014-03-14 13:56:52 -04:00
|
|
|
register.query("jabber:iq:register");
|
|
|
|
register.setTo(account.getServer());
|
|
|
|
sendIqPacket(register, new OnIqPacketReceived() {
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-14 13:56:52 -04:00
|
|
|
@Override
|
2014-12-21 15:43:58 -05:00
|
|
|
public void onIqPacketReceived(final Account account, final IqPacket packet) {
|
|
|
|
final Element instructions = packet.query().findChild("instructions");
|
2014-05-21 16:22:36 -04:00
|
|
|
if (packet.query().hasChild("username")
|
|
|
|
&& (packet.query().hasChild("password"))) {
|
2014-12-30 08:16:25 -05:00
|
|
|
final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
|
2015-05-05 22:33:21 -04:00
|
|
|
final Element username = new Element("username").setContent(account.getUsername());
|
|
|
|
final Element password = new Element("password").setContent(account.getPassword());
|
2014-03-20 10:49:53 -04:00
|
|
|
register.query("jabber:iq:register").addChild(username);
|
|
|
|
register.query().addChild(password);
|
2014-03-14 13:56:52 -04:00
|
|
|
sendIqPacket(register, new OnIqPacketReceived() {
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-14 13:56:52 -04:00
|
|
|
@Override
|
2014-12-21 15:43:58 -05:00
|
|
|
public void onIqPacketReceived(final Account account, final IqPacket packet) {
|
2014-12-30 08:16:25 -05:00
|
|
|
if (packet.getType() == IqPacket.TYPE.RESULT) {
|
2014-05-21 16:22:36 -04:00
|
|
|
account.setOption(Account.OPTION_REGISTER,
|
|
|
|
false);
|
2014-11-15 11:09:02 -05:00
|
|
|
changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
|
2014-05-21 16:22:36 -04:00
|
|
|
} else if (packet.hasChild("error")
|
|
|
|
&& (packet.findChild("error")
|
2015-05-05 22:33:21 -04:00
|
|
|
.hasChild("conflict"))) {
|
2014-11-15 11:09:02 -05:00
|
|
|
changeStatus(Account.State.REGISTRATION_CONFLICT);
|
2014-03-14 13:56:52 -04:00
|
|
|
} else {
|
2014-11-15 11:09:02 -05:00
|
|
|
changeStatus(Account.State.REGISTRATION_FAILED);
|
2014-08-31 10:28:21 -04:00
|
|
|
Log.d(Config.LOGTAG, packet.toString());
|
2014-03-14 13:56:52 -04:00
|
|
|
}
|
|
|
|
disconnect(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-11-15 11:09:02 -05:00
|
|
|
changeStatus(Account.State.REGISTRATION_FAILED);
|
2014-03-14 13:56:52 -04:00
|
|
|
disconnect(true);
|
2014-11-09 10:57:22 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
2014-05-21 16:22:36 -04:00
|
|
|
+ ": could not register. instructions are"
|
2015-05-17 06:32:04 -04:00
|
|
|
+ (instructions != null ? instructions.getContent() : ""));
|
2014-03-14 13:56:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
private void sendBindRequest() {
|
2015-02-12 12:53:00 -05:00
|
|
|
while(!mXmppConnectionService.areMessagesInitialized()) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
} catch (final InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 08:54:37 -04:00
|
|
|
clearIqCallbacks();
|
2014-12-30 08:16:25 -05:00
|
|
|
final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
|
2014-05-21 16:22:36 -04:00
|
|
|
iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind")
|
2015-05-05 22:33:21 -04:00
|
|
|
.addChild("resource").setContent(account.getResource());
|
2015-01-04 06:09:39 -05:00
|
|
|
this.sendUnmodifiedIqPacket(iq, new OnIqPacketReceived() {
|
2014-01-31 19:25:56 -05:00
|
|
|
@Override
|
2014-12-21 15:43:58 -05:00
|
|
|
public void onIqPacketReceived(final Account account, final IqPacket packet) {
|
|
|
|
final Element bind = packet.findChild("bind");
|
2014-08-31 10:28:21 -04:00
|
|
|
if (bind != null) {
|
2014-11-05 15:55:47 -05:00
|
|
|
final Element jid = bind.findChild("jid");
|
2014-09-22 10:04:37 -04:00
|
|
|
if (jid != null && jid.getContent() != null) {
|
2014-11-12 15:35:44 -05:00
|
|
|
try {
|
|
|
|
account.setResource(Jid.fromString(jid.getContent()).getResourcepart());
|
|
|
|
} catch (final InvalidJidException e) {
|
|
|
|
// TODO: Handle the case where an external JID is technically invalid?
|
|
|
|
}
|
2015-03-20 16:48:45 -04:00
|
|
|
if (streamFeatures.hasChild("session")) {
|
|
|
|
sendStartSession();
|
|
|
|
} else {
|
|
|
|
sendPostBindInitialization();
|
2014-07-26 09:44:32 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
disconnect(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
disconnect(true);
|
2014-02-04 09:09:50 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
}
|
|
|
|
});
|
2015-03-20 16:48:45 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 08:54:37 -04:00
|
|
|
private void clearIqCallbacks() {
|
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": clearing iq iq callbacks");
|
|
|
|
final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.ERROR);
|
|
|
|
Iterator<Entry<String, Pair<IqPacket, OnIqPacketReceived>>> iterator = this.packetCallbacks.entrySet().iterator();
|
|
|
|
while(iterator.hasNext()) {
|
|
|
|
Entry<String, Pair<IqPacket, OnIqPacketReceived>> entry = iterator.next();
|
|
|
|
entry.getValue().second.onIqPacketReceived(account,failurePacket);
|
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:48:45 -04:00
|
|
|
private void sendStartSession() {
|
|
|
|
final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET);
|
2015-08-06 14:48:55 -04:00
|
|
|
startSession.addChild("session", "urn:ietf:params:xml:ns:xmpp-session");
|
2015-03-20 16:48:45 -04:00
|
|
|
this.sendUnmodifiedIqPacket(startSession, new OnIqPacketReceived() {
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
|
|
|
if (packet.getType() == IqPacket.TYPE.RESULT) {
|
|
|
|
sendPostBindInitialization();
|
|
|
|
} else {
|
|
|
|
disconnect(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendPostBindInitialization() {
|
|
|
|
smVersion = 0;
|
|
|
|
if (streamFeatures.hasChild("sm", "urn:xmpp:sm:3")) {
|
|
|
|
smVersion = 3;
|
|
|
|
} else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) {
|
|
|
|
smVersion = 2;
|
|
|
|
}
|
|
|
|
if (smVersion != 0) {
|
|
|
|
final EnablePacket enable = new EnablePacket(smVersion);
|
|
|
|
tagWriter.writeStanzaAsync(enable);
|
|
|
|
stanzasSent = 0;
|
2015-08-06 14:48:55 -04:00
|
|
|
mStanzaReceipts.clear();
|
2015-03-20 16:48:45 -04:00
|
|
|
}
|
|
|
|
features.carbonsEnabled = false;
|
|
|
|
features.blockListRequested = false;
|
|
|
|
disco.clear();
|
|
|
|
sendServiceDiscoveryInfo(account.getServer());
|
2015-04-25 12:24:10 -04:00
|
|
|
sendServiceDiscoveryInfo(account.getJid().toBareJid());
|
2015-03-20 16:48:45 -04:00
|
|
|
sendServiceDiscoveryItems(account.getServer());
|
|
|
|
if (bindListener != null) {
|
|
|
|
bindListener.onBind(account);
|
2014-05-17 15:57:44 -04:00
|
|
|
}
|
2015-03-20 16:48:45 -04:00
|
|
|
sendInitialPing();
|
2014-02-09 08:10:52 -05:00
|
|
|
}
|
|
|
|
|
2015-04-25 12:24:10 -04:00
|
|
|
private void sendServiceDiscoveryInfo(final Jid jid) {
|
|
|
|
if (disco.containsKey(jid)) {
|
|
|
|
if (account.getServer().equals(jid)) {
|
2014-11-15 11:24:06 -05:00
|
|
|
enableAdvancedStreamFeatures();
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-30 08:16:25 -05:00
|
|
|
final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
|
2015-04-25 12:24:10 -04:00
|
|
|
iq.setTo(jid);
|
2014-11-15 11:24:06 -05:00
|
|
|
iq.query("http://jabber.org/protocol/disco#info");
|
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-11-15 11:24:06 -05:00
|
|
|
@Override
|
2014-12-21 15:43:58 -05:00
|
|
|
public void onIqPacketReceived(final Account account, final IqPacket packet) {
|
2014-11-15 11:24:06 -05:00
|
|
|
final List<Element> elements = packet.query().getChildren();
|
2015-04-25 12:24:10 -04:00
|
|
|
final Info info = new Info();
|
2014-12-21 15:43:58 -05:00
|
|
|
for (final Element element : elements) {
|
2014-11-21 14:34:19 -05:00
|
|
|
if (element.getName().equals("identity")) {
|
2015-04-25 12:24:10 -04:00
|
|
|
String type = element.getAttribute("type");
|
|
|
|
String category = element.getAttribute("category");
|
|
|
|
if (type != null && category != null) {
|
|
|
|
info.identities.add(new Pair<>(category,type));
|
2014-11-21 14:34:19 -05:00
|
|
|
}
|
|
|
|
} else if (element.getName().equals("feature")) {
|
2015-04-25 12:24:10 -04:00
|
|
|
info.features.add(element.getAttribute("var"));
|
2014-11-15 11:24:06 -05:00
|
|
|
}
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2015-04-25 12:24:10 -04:00
|
|
|
disco.put(jid, info);
|
2014-11-05 15:55:47 -05:00
|
|
|
|
2015-04-25 12:24:10 -04:00
|
|
|
if (account.getServer().equals(jid)) {
|
2014-11-15 11:24:06 -05:00
|
|
|
enableAdvancedStreamFeatures();
|
2014-12-21 15:43:58 -05:00
|
|
|
for (final OnAdvancedStreamFeaturesLoaded listener : advancedStreamFeaturesLoadedListeners) {
|
2014-12-08 15:59:14 -05:00
|
|
|
listener.onAdvancedStreamFeaturesAvailable(account);
|
2014-12-04 19:54:16 -05:00
|
|
|
}
|
2014-11-15 11:24:06 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
}
|
2014-11-15 11:24:06 -05:00
|
|
|
});
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-04-08 17:15:55 -04:00
|
|
|
private void enableAdvancedStreamFeatures() {
|
2015-01-05 10:17:05 -05:00
|
|
|
if (getFeatures().carbons() && !features.carbonsEnabled) {
|
|
|
|
sendEnableCarbons();
|
2014-04-08 17:15:55 -04:00
|
|
|
}
|
2015-01-05 10:17:05 -05:00
|
|
|
if (getFeatures().blocking() && !features.blockListRequested) {
|
2015-04-29 10:15:07 -04:00
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": Requesting block list");
|
2014-12-21 15:43:58 -05:00
|
|
|
this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser());
|
|
|
|
}
|
2014-04-08 17:15:55 -04:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-11-05 15:55:47 -05:00
|
|
|
private void sendServiceDiscoveryItems(final Jid server) {
|
2014-12-30 08:16:25 -05:00
|
|
|
final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
|
2014-11-05 15:55:47 -05:00
|
|
|
iq.setTo(server.toDomainJid());
|
2014-03-14 23:59:18 -04:00
|
|
|
iq.query("http://jabber.org/protocol/disco#items");
|
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
2014-12-21 15:43:58 -05:00
|
|
|
public void onIqPacketReceived(final Account account, final IqPacket packet) {
|
|
|
|
final List<Element> elements = packet.query().getChildren();
|
|
|
|
for (final Element element : elements) {
|
2014-11-12 15:35:44 -05:00
|
|
|
if (element.getName().equals("item")) {
|
2014-12-01 03:48:30 -05:00
|
|
|
final Jid jid = element.getAttributeAsJid("jid");
|
|
|
|
if (jid != null && !jid.equals(account.getServer())) {
|
|
|
|
sendServiceDiscoveryInfo(jid);
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 23:59:18 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
2014-02-09 08:10:52 -05:00
|
|
|
private void sendEnableCarbons() {
|
2014-12-30 08:16:25 -05:00
|
|
|
final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
|
2014-05-21 16:22:36 -04:00
|
|
|
iq.addChild("enable", "urn:xmpp:carbons:2");
|
2014-02-09 08:10:52 -05:00
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
2014-03-06 09:11:56 -05:00
|
|
|
|
2014-02-09 08:10:52 -05:00
|
|
|
@Override
|
2014-12-21 15:43:58 -05:00
|
|
|
public void onIqPacketReceived(final Account account, final IqPacket packet) {
|
2014-02-09 08:10:52 -05:00
|
|
|
if (!packet.hasChild("error")) {
|
2014-11-09 10:57:22 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
2014-03-06 09:11:56 -05:00
|
|
|
+ ": successfully enabled carbons");
|
2015-01-05 10:17:05 -05:00
|
|
|
features.carbonsEnabled = true;
|
2014-02-09 08:10:52 -05:00
|
|
|
} else {
|
2014-11-09 10:57:22 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
2014-03-06 09:11:56 -05:00
|
|
|
+ ": error enableing carbons " + packet.toString());
|
2014-02-09 08:10:52 -05:00
|
|
|
}
|
2014-01-31 19:25:56 -05:00
|
|
|
}
|
|
|
|
});
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
private void processStreamError(final Tag currentTag)
|
2014-11-12 15:35:44 -05:00
|
|
|
throws XmlPullParserException, IOException {
|
2014-12-21 15:43:58 -05:00
|
|
|
final Element streamError = tagReader.readElement(currentTag);
|
2014-09-08 17:58:37 -04:00
|
|
|
if (streamError != null && streamError.hasChild("conflict")) {
|
2014-11-05 15:55:47 -05:00
|
|
|
final String resource = account.getResource().split("\\.")[0];
|
2014-11-12 15:35:44 -05:00
|
|
|
account.setResource(resource + "." + nextRandomId());
|
|
|
|
Log.d(Config.LOGTAG,
|
2014-11-09 10:57:22 -05:00
|
|
|
account.getJid().toBareJid() + ": switching resource due to conflict ("
|
2014-12-21 15:43:58 -05:00
|
|
|
+ account.getResource() + ")");
|
2014-09-03 08:57:40 -04:00
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
private void sendStartStream() throws IOException {
|
2014-12-21 15:43:58 -05:00
|
|
|
final Tag stream = Tag.start("stream:stream");
|
2014-11-09 10:57:22 -05:00
|
|
|
stream.setAttribute("from", account.getJid().toBareJid().toString());
|
2014-11-05 15:55:47 -05:00
|
|
|
stream.setAttribute("to", account.getServer().toString());
|
2014-01-30 10:42:35 -05:00
|
|
|
stream.setAttribute("version", "1.0");
|
|
|
|
stream.setAttribute("xml:lang", "en");
|
|
|
|
stream.setAttribute("xmlns", "jabber:client");
|
|
|
|
stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
|
2014-02-04 09:09:50 -05:00
|
|
|
tagWriter.writeTag(stream);
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private String nextRandomId() {
|
2014-11-09 04:50:45 -05:00
|
|
|
return new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2015-01-04 06:09:39 -05:00
|
|
|
public void sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
|
2014-11-09 10:57:22 -05:00
|
|
|
packet.setFrom(account.getJid());
|
2015-08-06 14:48:55 -04:00
|
|
|
this.sendUnmodifiedIqPacket(packet, callback);
|
2015-01-04 06:09:39 -05:00
|
|
|
|
2014-01-31 19:25:56 -05:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2015-01-04 09:40:09 -05:00
|
|
|
private synchronized void sendUnmodifiedIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (packet.getId() == null) {
|
2014-12-21 15:43:58 -05:00
|
|
|
final String id = nextRandomId();
|
2014-05-18 11:32:20 -04:00
|
|
|
packet.setAttribute("id", id);
|
|
|
|
}
|
2015-01-04 06:09:39 -05:00
|
|
|
if (callback != null) {
|
2014-12-30 08:50:51 -05:00
|
|
|
packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
|
2015-01-04 06:09:39 -05:00
|
|
|
}
|
|
|
|
this.sendPacket(packet);
|
2014-05-18 11:32:20 -04:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public void sendMessagePacket(final MessagePacket packet) {
|
2015-01-04 06:09:39 -05:00
|
|
|
this.sendPacket(packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public void sendPresencePacket(final PresencePacket packet) {
|
2015-01-04 06:09:39 -05:00
|
|
|
this.sendPacket(packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2015-01-04 06:09:39 -05:00
|
|
|
private synchronized void sendPacket(final AbstractStanza packet) {
|
2015-05-05 22:29:45 -04:00
|
|
|
if (stanzasSent == Integer.MAX_VALUE) {
|
|
|
|
resetStreamId();
|
|
|
|
disconnect(true);
|
|
|
|
return;
|
|
|
|
}
|
2015-01-04 06:09:39 -05:00
|
|
|
final String name = packet.getName();
|
|
|
|
if (name.equals("iq") || name.equals("message") || name.equals("presence")) {
|
2014-08-26 10:52:42 -04:00
|
|
|
++stanzasSent;
|
2015-01-04 06:09:39 -05:00
|
|
|
}
|
2014-03-10 14:22:13 -04:00
|
|
|
tagWriter.writeStanzaAsync(packet);
|
2015-08-08 04:26:36 -04:00
|
|
|
if ((packet instanceof MessagePacket || packet instanceof IqPacket) && packet.getId() != null && this.streamId != null) {
|
2015-04-29 10:15:07 -04:00
|
|
|
if (Config.EXTENDED_SM_LOGGING) {
|
2015-08-06 14:48:55 -04:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": requesting ack for stanza #" + stanzasSent);
|
2015-04-29 10:15:07 -04:00
|
|
|
}
|
2015-08-06 14:48:55 -04:00
|
|
|
this.mStanzaReceipts.put(stanzasSent, packet.getId());
|
2014-08-26 10:52:42 -04:00
|
|
|
tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion));
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-02-01 09:07:20 -05:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-11 10:44:22 -04:00
|
|
|
public void sendPing() {
|
|
|
|
if (streamFeatures.hasChild("sm")) {
|
2014-04-05 06:08:35 -04:00
|
|
|
tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
|
2014-03-11 10:44:22 -04:00
|
|
|
} else {
|
2014-12-30 08:16:25 -05:00
|
|
|
final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
|
2014-11-09 10:57:22 -05:00
|
|
|
iq.setFrom(account.getJid());
|
2014-05-21 16:22:36 -04:00
|
|
|
iq.addChild("ping", "urn:xmpp:ping");
|
2014-03-11 10:44:22 -04:00
|
|
|
this.sendIqPacket(iq, null);
|
|
|
|
}
|
2014-08-19 09:06:50 -04:00
|
|
|
this.lastPingSent = SystemClock.elapsedRealtime();
|
2014-03-11 10:44:22 -04:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void setOnMessagePacketReceivedListener(
|
2014-12-21 15:43:58 -05:00
|
|
|
final OnMessagePacketReceived listener) {
|
2014-02-01 09:07:20 -05:00
|
|
|
this.messageListener = listener;
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void setOnUnregisteredIqPacketReceivedListener(
|
2014-12-21 15:43:58 -05:00
|
|
|
final OnIqPacketReceived listener) {
|
2014-02-01 09:07:20 -05:00
|
|
|
this.unregisteredIqListener = listener;
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void setOnPresencePacketReceivedListener(
|
2014-12-21 15:43:58 -05:00
|
|
|
final OnPresencePacketReceived listener) {
|
2014-02-01 09:07:20 -05:00
|
|
|
this.presenceListener = listener;
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
|
|
|
public void setOnJinglePacketReceivedListener(
|
2014-12-21 15:43:58 -05:00
|
|
|
final OnJinglePacketReceived listener) {
|
2014-03-26 21:02:59 -04:00
|
|
|
this.jingleListener = listener;
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public void setOnStatusChangedListener(final OnStatusChanged listener) {
|
2014-02-04 09:09:50 -05:00
|
|
|
this.statusListener = listener;
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public void setOnBindListener(final OnBindListener listener) {
|
2014-03-14 15:43:54 -04:00
|
|
|
this.bindListener = listener;
|
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public void setOnMessageAcknowledgeListener(final OnMessageAcknowledged listener) {
|
2014-08-26 10:52:42 -04:00
|
|
|
this.acknowledgedListener = listener;
|
|
|
|
}
|
2014-02-04 09:09:50 -05:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public void addOnAdvancedStreamFeaturesAvailableListener(final OnAdvancedStreamFeaturesLoaded listener) {
|
2014-12-08 15:59:14 -05:00
|
|
|
if (!this.advancedStreamFeaturesLoadedListeners.contains(listener)) {
|
|
|
|
this.advancedStreamFeaturesLoadedListeners.add(listener);
|
|
|
|
}
|
2014-12-04 19:54:16 -05:00
|
|
|
}
|
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public void disconnect(final boolean force) {
|
2014-11-09 10:57:22 -05:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting");
|
2014-03-10 14:22:13 -04:00
|
|
|
try {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (force) {
|
2014-03-10 14:22:13 -04:00
|
|
|
socket.close();
|
2014-03-12 17:31:50 -04:00
|
|
|
return;
|
2014-04-03 09:08:53 -04:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (tagWriter.isActive()) {
|
|
|
|
tagWriter.finish();
|
|
|
|
try {
|
2015-08-08 04:26:36 -04:00
|
|
|
while (!tagWriter.finished() && socket.isConnected()) {
|
2014-08-31 10:28:21 -04:00
|
|
|
Log.d(Config.LOGTAG, "not yet finished");
|
2014-05-21 16:22:36 -04:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
tagWriter.writeTag(Tag.end("stream:stream"));
|
2014-10-08 06:21:58 -04:00
|
|
|
socket.close();
|
2014-12-21 15:43:58 -05:00
|
|
|
} catch (final IOException e) {
|
2014-08-31 10:28:21 -04:00
|
|
|
Log.d(Config.LOGTAG,
|
|
|
|
"io exception during disconnect");
|
2014-12-21 15:43:58 -05:00
|
|
|
} catch (final InterruptedException e) {
|
2014-08-31 10:28:21 -04:00
|
|
|
Log.d(Config.LOGTAG, "interrupted");
|
2014-05-21 16:22:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 04:37:49 -04:00
|
|
|
}).start();
|
2014-12-21 15:43:58 -05:00
|
|
|
} catch (final IOException e) {
|
2014-08-31 10:28:21 -04:00
|
|
|
Log.d(Config.LOGTAG, "io exception during disconnect");
|
2014-03-10 14:22:13 -04:00
|
|
|
}
|
2014-02-04 09:09:50 -05:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2015-04-09 06:46:54 -04:00
|
|
|
public void resetStreamId() {
|
|
|
|
this.streamId = null;
|
|
|
|
}
|
|
|
|
|
2015-06-28 05:19:07 -04:00
|
|
|
public List<Jid> findDiscoItemsByFeature(final String feature) {
|
|
|
|
final List<Jid> items = new ArrayList<>();
|
2015-04-25 12:24:10 -04:00
|
|
|
for (final Entry<Jid, Info> cursor : disco.entrySet()) {
|
|
|
|
if (cursor.getValue().features.contains(feature)) {
|
2015-06-28 05:19:07 -04:00
|
|
|
items.add(cursor.getKey());
|
2014-05-21 16:22:36 -04:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 05:43:00 -04:00
|
|
|
return items;
|
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2015-06-28 05:19:07 -04:00
|
|
|
public Jid findDiscoItemByFeature(final String feature) {
|
|
|
|
final List<Jid> items = findDiscoItemsByFeature(feature);
|
2014-08-31 10:28:21 -04:00
|
|
|
if (items.size() >= 1) {
|
2014-06-30 05:43:00 -04:00
|
|
|
return items.get(0);
|
|
|
|
}
|
2014-04-08 17:15:55 -04:00
|
|
|
return null;
|
2014-03-13 12:29:22 -04:00
|
|
|
}
|
2014-03-10 14:22:13 -04:00
|
|
|
|
|
|
|
public void r() {
|
2014-04-05 06:08:35 -04:00
|
|
|
this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
|
2014-03-10 14:22:13 -04:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-03-14 23:59:18 -04:00
|
|
|
public String getMucServer() {
|
2015-04-25 12:24:10 -04:00
|
|
|
for (final Entry<Jid, Info> cursor : disco.entrySet()) {
|
|
|
|
final Info value = cursor.getValue();
|
|
|
|
if (value.features.contains("http://jabber.org/protocol/muc")
|
|
|
|
&& !value.features.contains("jabber:iq:gateway")
|
|
|
|
&& !value.identities.contains(new Pair<>("conference","irc"))) {
|
|
|
|
return cursor.getKey().toString();
|
2014-11-20 12:20:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2014-03-14 23:59:18 -04:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-05-18 05:25:04 -04:00
|
|
|
public int getTimeToNextAttempt() {
|
2014-12-21 15:43:58 -05:00
|
|
|
final int interval = (int) (25 * Math.pow(1.5, attempt));
|
|
|
|
final int secondsSinceLast = (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
|
2014-05-18 05:25:04 -04:00
|
|
|
return interval - secondsSinceLast;
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-05-18 05:25:04 -04:00
|
|
|
public int getAttempt() {
|
|
|
|
return this.attempt;
|
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-08-09 04:20:16 -04:00
|
|
|
public Features getFeatures() {
|
|
|
|
return this.features;
|
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-11-08 14:52:02 -05:00
|
|
|
public long getLastSessionEstablished() {
|
2014-12-21 15:43:58 -05:00
|
|
|
final long diff;
|
2014-11-08 14:52:02 -05:00
|
|
|
if (this.lastSessionStarted == 0) {
|
|
|
|
diff = SystemClock.elapsedRealtime() - this.lastConnect;
|
|
|
|
} else {
|
|
|
|
diff = SystemClock.elapsedRealtime() - this.lastSessionStarted;
|
|
|
|
}
|
|
|
|
return System.currentTimeMillis() - diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLastConnect() {
|
|
|
|
return this.lastConnect;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLastPingSent() {
|
|
|
|
return this.lastPingSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLastPacketReceived() {
|
2015-01-05 10:17:05 -05:00
|
|
|
return this.lastPacketReceived;
|
2014-11-08 14:52:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendActive() {
|
2015-01-04 06:09:39 -05:00
|
|
|
this.sendPacket(new ActivePacket());
|
2014-11-08 14:52:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendInactive() {
|
2015-01-04 06:09:39 -05:00
|
|
|
this.sendPacket(new InactivePacket());
|
2014-11-08 14:52:02 -05:00
|
|
|
}
|
|
|
|
|
2015-02-10 11:13:34 -05:00
|
|
|
public void resetAttemptCount() {
|
|
|
|
this.attempt = 0;
|
|
|
|
this.lastConnect = 0;
|
|
|
|
}
|
|
|
|
|
2015-04-25 12:24:10 -04:00
|
|
|
private class Info {
|
|
|
|
public final ArrayList<String> features = new ArrayList<>();
|
|
|
|
public final ArrayList<Pair<String,String>> identities = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
2015-05-08 00:30:06 -04:00
|
|
|
private class UnauthorizedException extends IOException {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private class SecurityException extends IOException {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private class IncompatibleServerException extends IOException {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-09 04:20:16 -04:00
|
|
|
public class Features {
|
|
|
|
XmppConnection connection;
|
2015-01-05 10:17:05 -05:00
|
|
|
private boolean carbonsEnabled = false;
|
|
|
|
private boolean encryptionEnabled = false;
|
|
|
|
private boolean blockListRequested = false;
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public Features(final XmppConnection connection) {
|
2014-08-09 04:20:16 -04:00
|
|
|
this.connection = connection;
|
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-11-05 15:55:47 -05:00
|
|
|
private boolean hasDiscoFeature(final Jid server, final String feature) {
|
2015-04-25 12:24:10 -04:00
|
|
|
return connection.disco.containsKey(server) &&
|
|
|
|
connection.disco.get(server).features.contains(feature);
|
2014-11-12 15:35:44 -05:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-08-09 04:20:16 -04:00
|
|
|
public boolean carbons() {
|
|
|
|
return hasDiscoFeature(account.getServer(), "urn:xmpp:carbons:2");
|
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-12-21 15:43:58 -05:00
|
|
|
public boolean blocking() {
|
|
|
|
return hasDiscoFeature(account.getServer(), Xmlns.BLOCKING);
|
|
|
|
}
|
|
|
|
|
2014-12-23 17:19:00 -05:00
|
|
|
public boolean register() {
|
|
|
|
return hasDiscoFeature(account.getServer(), Xmlns.REGISTER);
|
|
|
|
}
|
|
|
|
|
2014-08-09 04:20:16 -04:00
|
|
|
public boolean sm() {
|
2015-04-25 12:24:10 -04:00
|
|
|
return streamId != null
|
|
|
|
|| (connection.streamFeatures != null && connection.streamFeatures.hasChild("sm"));
|
2014-08-09 04:20:16 -04:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-08-26 11:43:44 -04:00
|
|
|
public boolean csi() {
|
2014-11-12 15:35:44 -05:00
|
|
|
return connection.streamFeatures != null && connection.streamFeatures.hasChild("csi", "urn:xmpp:csi:0");
|
2014-08-26 11:43:44 -04:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2015-04-25 12:24:10 -04:00
|
|
|
public boolean pep() {
|
|
|
|
final Pair<String,String> needle = new Pair<>("pubsub","pep");
|
|
|
|
Info info = disco.get(account.getServer());
|
|
|
|
if (info != null && info.identities.contains(needle)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
info = disco.get(account.getJid().toBareJid());
|
|
|
|
return info != null && info.identities.contains(needle);
|
|
|
|
}
|
2014-08-09 04:20:16 -04:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2014-11-08 14:52:02 -05:00
|
|
|
public boolean mam() {
|
2015-04-25 12:24:10 -04:00
|
|
|
if (hasDiscoFeature(account.getJid().toBareJid(), "urn:xmpp:mam:0")) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return hasDiscoFeature(account.getServer(), "urn:xmpp:mam:0");
|
|
|
|
}
|
2014-11-08 14:52:02 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 15:59:14 -05:00
|
|
|
public boolean advancedStreamFeaturesLoaded() {
|
2015-04-25 12:24:10 -04:00
|
|
|
return disco.containsKey(account.getServer());
|
2014-12-08 15:59:14 -05:00
|
|
|
}
|
|
|
|
|
2014-08-09 04:20:16 -04:00
|
|
|
public boolean rosterVersioning() {
|
2014-11-12 15:35:44 -05:00
|
|
|
return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
|
2014-08-09 04:20:16 -04:00
|
|
|
}
|
2014-08-31 10:28:21 -04:00
|
|
|
|
2015-01-05 10:17:05 -05:00
|
|
|
public void setBlockListRequested(boolean value) {
|
|
|
|
this.blockListRequested = value;
|
2014-08-19 09:06:50 -04:00
|
|
|
}
|
2015-06-28 05:19:07 -04:00
|
|
|
|
|
|
|
public boolean httpUpload() {
|
|
|
|
return findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD).size() > 0;
|
|
|
|
}
|
2014-08-19 09:06:50 -04:00
|
|
|
}
|
2014-12-21 15:43:58 -05:00
|
|
|
|
|
|
|
private IqGenerator getIqGenerator() {
|
|
|
|
return mXmppConnectionService.getIqGenerator();
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|