2014-02-28 17:46:01 +00:00
|
|
|
package eu.siacs.conversations.xmpp;
|
2014-01-30 15:42:35 +00:00
|
|
|
|
2014-11-03 09:03:45 +00: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;
|
|
|
|
import android.util.SparseArray;
|
|
|
|
|
|
|
|
import org.apache.http.conn.ssl.StrictHostnameVerifier;
|
2014-11-15 14:42:40 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2014-11-03 09:03:45 +00:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.math.BigInteger;
|
2014-11-08 04:08:31 +00:00
|
|
|
import java.net.IDN;
|
2014-10-27 20:48:25 +00:00
|
|
|
import java.net.InetSocketAddress;
|
2014-01-30 15:42:35 +00:00
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2014-03-06 19:03:35 +00:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2014-03-15 03:59:18 +00:00
|
|
|
import java.util.ArrayList;
|
2014-10-18 19:56:59 +00:00
|
|
|
import java.util.Arrays;
|
2014-04-08 21:15:55 +00:00
|
|
|
import java.util.HashMap;
|
2014-02-01 00:25:56 +00:00
|
|
|
import java.util.Hashtable;
|
2014-10-19 19:53:03 +00:00
|
|
|
import java.util.LinkedList;
|
2014-02-09 13:10:52 +00:00
|
|
|
import java.util.List;
|
2014-04-08 21:15:55 +00:00
|
|
|
import java.util.Map.Entry;
|
2014-01-30 15:42:35 +00:00
|
|
|
|
2014-07-22 15:27:44 +00:00
|
|
|
import javax.net.ssl.HostnameVerifier;
|
2014-03-06 19:03:35 +00:00
|
|
|
import javax.net.ssl.SSLContext;
|
2014-01-30 15:42:35 +00:00
|
|
|
import javax.net.ssl.SSLSocket;
|
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
2014-03-06 19:03:35 +00:00
|
|
|
import javax.net.ssl.X509TrustManager;
|
2014-01-30 15:42:35 +00:00
|
|
|
|
2014-08-31 14:28:21 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-11-12 15:15:38 +00: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 20:35:44 +00:00
|
|
|
import eu.siacs.conversations.crypto.sasl.ScramSha1;
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2014-06-20 15:30:19 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.utils.DNSHelper;
|
2014-04-03 16:16:14 +00:00
|
|
|
import eu.siacs.conversations.utils.zlib.ZLibInputStream;
|
2014-11-03 09:03:45 +00:00
|
|
|
import eu.siacs.conversations.utils.zlib.ZLibOutputStream;
|
2014-02-28 17:46:01 +00: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 20:55:47 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
|
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
|
2014-03-10 18:22:13 +00: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 15:43:44 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.csi.ActivePacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.csi.InactivePacket;
|
2014-03-10 18:22:13 +00: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 15:42:35 +00:00
|
|
|
|
|
|
|
public class XmppConnection implements Runnable {
|
|
|
|
|
2014-11-08 19:52:02 +00:00
|
|
|
private static final int PACKET_IQ = 0;
|
|
|
|
private static final int PACKET_MESSAGE = 1;
|
|
|
|
private static final int PACKET_PRESENCE = 2;
|
|
|
|
private final Context applicationContext;
|
2014-01-30 15:42:35 +00:00
|
|
|
protected Account account;
|
2014-04-03 21:57:26 +00:00
|
|
|
private WakeLock wakeLock;
|
2014-01-30 15:42:35 +00:00
|
|
|
private Socket socket;
|
|
|
|
private XmlReader tagReader;
|
|
|
|
private TagWriter tagWriter;
|
2014-08-09 08:20:16 +00:00
|
|
|
private Features features = new Features(this);
|
2014-02-01 00:25:56 +00:00
|
|
|
private boolean shouldBind = true;
|
|
|
|
private boolean shouldAuthenticate = true;
|
|
|
|
private Element streamFeatures;
|
2014-11-05 20:55:47 +00:00
|
|
|
private HashMap<String, List<String>> disco = new HashMap<>();
|
|
|
|
|
2014-03-10 18:22:13 +00:00
|
|
|
private String streamId = null;
|
2014-04-05 10:08:35 +00:00
|
|
|
private int smVersion = 3;
|
2014-11-05 20:55:47 +00:00
|
|
|
private SparseArray<String> messageReceipts = new SparseArray<>();
|
|
|
|
|
2014-08-19 13:06:50 +00:00
|
|
|
private boolean usingCompression = false;
|
2014-10-19 18:48:01 +00:00
|
|
|
private boolean usingEncryption = false;
|
2014-03-10 18:22:13 +00:00
|
|
|
private int stanzasReceived = 0;
|
|
|
|
private int stanzasSent = 0;
|
2014-08-19 13:06:50 +00:00
|
|
|
private long lastPaketReceived = 0;
|
|
|
|
private long lastPingSent = 0;
|
|
|
|
private long lastConnect = 0;
|
|
|
|
private long lastSessionStarted = 0;
|
2014-05-18 09:25:04 +00:00
|
|
|
private int attempt = 0;
|
2014-11-05 20:55:47 +00:00
|
|
|
private Hashtable<String, PacketReceived> packetCallbacks = new Hashtable<>();
|
2014-02-01 14:07:20 +00:00
|
|
|
private OnPresencePacketReceived presenceListener = null;
|
2014-03-27 01:02:59 +00:00
|
|
|
private OnJinglePacketReceived jingleListener = null;
|
2014-02-01 14:07:20 +00:00
|
|
|
private OnIqPacketReceived unregisteredIqListener = null;
|
|
|
|
private OnMessagePacketReceived messageListener = null;
|
2014-02-04 14:09:50 +00:00
|
|
|
private OnStatusChanged statusListener = null;
|
2014-03-14 19:43:54 +00:00
|
|
|
private OnBindListener bindListener = null;
|
2014-08-26 14:52:42 +00:00
|
|
|
private OnMessageAcknowledged acknowledgedListener = null;
|
2014-11-09 09:50:45 +00:00
|
|
|
private XmppConnectionService mXmppConnectionService = null;
|
2014-01-30 15:42:35 +00:00
|
|
|
|
2014-11-12 20:35:44 +00:00
|
|
|
private SaslMechanism saslMechanism;
|
2014-11-12 15:15:38 +00:00
|
|
|
|
2014-06-20 15:30:19 +00:00
|
|
|
public XmppConnection(Account account, XmppConnectionService service) {
|
2014-01-30 15:42:35 +00:00
|
|
|
this.account = account;
|
2014-08-31 14:28:21 +00:00
|
|
|
this.wakeLock = service.getPowerManager().newWakeLock(
|
2014-11-12 20:35:44 +00:00
|
|
|
PowerManager.PARTIAL_WAKE_LOCK, account.getJid().toBareJid().toString());
|
2014-01-30 15:42:35 +00:00
|
|
|
tagWriter = new TagWriter();
|
2014-11-09 09:50:45 +00:00
|
|
|
mXmppConnectionService = service;
|
2014-10-20 19:08:33 +00:00
|
|
|
applicationContext = service.getApplicationContext();
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-03-06 14:11:56 +00:00
|
|
|
|
2014-11-15 16:09:02 +00:00
|
|
|
protected void changeStatus(final Account.State nextStatus) {
|
2014-03-11 15:27:33 +00:00
|
|
|
if (account.getStatus() != nextStatus) {
|
2014-11-15 16:09:02 +00:00
|
|
|
if ((nextStatus == Account.State.OFFLINE)
|
|
|
|
&& (account.getStatus() != Account.State.CONNECTING)
|
|
|
|
&& (account.getStatus() != Account.State.ONLINE)
|
|
|
|
&& (account.getStatus() != Account.State.DISABLED)) {
|
2014-03-13 16:29:22 +00:00
|
|
|
return;
|
2014-11-15 16:09:02 +00:00
|
|
|
}
|
|
|
|
if (nextStatus == Account.State.ONLINE) {
|
2014-05-18 09:25:04 +00:00
|
|
|
this.attempt = 0;
|
|
|
|
}
|
2014-03-11 15:27:33 +00:00
|
|
|
account.setStatus(nextStatus);
|
|
|
|
if (statusListener != null) {
|
|
|
|
statusListener.onStatusChanged(account);
|
|
|
|
}
|
2014-03-06 02:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
|
|
|
|
protected void connect() {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting");
|
2014-08-19 13:06:50 +00:00
|
|
|
usingCompression = false;
|
2014-10-19 18:48:01 +00:00
|
|
|
usingEncryption = false;
|
2014-03-12 21:31:50 +00:00
|
|
|
lastConnect = SystemClock.elapsedRealtime();
|
2014-08-19 13:06:50 +00:00
|
|
|
lastPingSent = SystemClock.elapsedRealtime();
|
2014-05-18 09:25:04 +00:00
|
|
|
this.attempt++;
|
2014-01-30 15:42:35 +00:00
|
|
|
try {
|
2014-05-21 20:22:36 +00:00
|
|
|
shouldAuthenticate = shouldBind = !account
|
2014-11-12 20:35:44 +00:00
|
|
|
.isOptionSet(Account.OPTION_REGISTER);
|
2014-03-08 19:14:47 +00:00
|
|
|
tagReader = new XmlReader(wakeLock);
|
|
|
|
tagWriter = new TagWriter();
|
|
|
|
packetCallbacks.clear();
|
2014-11-15 16:09:02 +00:00
|
|
|
this.changeStatus(Account.State.CONNECTING);
|
2014-10-27 20:48:25 +00:00
|
|
|
Bundle result = DNSHelper.getSRVRecord(account.getServer());
|
|
|
|
ArrayList<Parcelable> values = result.getParcelableArrayList("values");
|
|
|
|
if ("timeout".equals(result.getString("error"))) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": dns timeout");
|
2014-11-15 16:09:02 +00:00
|
|
|
this.changeStatus(Account.State.OFFLINE);
|
2014-03-28 18:00:01 +00:00
|
|
|
return;
|
2014-10-27 20:48:25 +00:00
|
|
|
} else if (values != null) {
|
2014-11-03 09:03:45 +00:00
|
|
|
int i = 0;
|
|
|
|
boolean socketError = true;
|
|
|
|
while (socketError && values.size() > i) {
|
|
|
|
Bundle namePort = (Bundle) values.get(i);
|
|
|
|
try {
|
2014-11-09 14:05:02 +00:00
|
|
|
String srvRecordServer;
|
2014-11-12 20:35:44 +00:00
|
|
|
try {
|
|
|
|
srvRecordServer=IDN.toASCII(namePort.getString("name"));
|
|
|
|
} catch (final IllegalArgumentException e) {
|
|
|
|
// TODO: Handle me?`
|
|
|
|
srvRecordServer = "";
|
|
|
|
}
|
2014-11-03 09:03:45 +00:00
|
|
|
int srvRecordPort = namePort.getInt("port");
|
|
|
|
String srvIpServer = namePort.getString("ipv4");
|
|
|
|
InetSocketAddress addr;
|
|
|
|
if (srvIpServer != null) {
|
|
|
|
addr = new InetSocketAddress(srvIpServer, srvRecordPort);
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
2014-11-03 09:03:45 +00:00
|
|
|
+ ": using values from dns " + srvRecordServer
|
|
|
|
+ "[" + srvIpServer + "]:" + srvRecordPort);
|
|
|
|
} else {
|
|
|
|
addr = new InetSocketAddress(srvRecordServer, srvRecordPort);
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
2014-11-03 09:03:45 +00:00
|
|
|
+ ": using values from dns "
|
|
|
|
+ srvRecordServer + ":" + srvRecordPort);
|
2014-10-16 21:31:48 +00:00
|
|
|
}
|
2014-11-03 09:03:45 +00:00
|
|
|
socket = new Socket();
|
|
|
|
socket.connect(addr, 20000);
|
|
|
|
socketError = false;
|
|
|
|
} catch (UnknownHostException e) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
2014-11-03 09:03:45 +00:00
|
|
|
i++;
|
|
|
|
} catch (IOException e) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
2014-11-03 09:03:45 +00:00
|
|
|
i++;
|
2014-10-16 21:31:48 +00:00
|
|
|
}
|
2014-11-03 09:03:45 +00:00
|
|
|
}
|
|
|
|
if (socketError) {
|
2014-11-15 16:09:02 +00:00
|
|
|
this.changeStatus(Account.State.SERVER_NOT_FOUND);
|
2014-11-03 09:03:45 +00:00
|
|
|
if (wakeLock.isHeld()) {
|
|
|
|
try {
|
|
|
|
wakeLock.release();
|
2014-11-05 20:55:47 +00:00
|
|
|
} catch (final RuntimeException ignored) {
|
2014-11-03 09:03:45 +00:00
|
|
|
}
|
2014-10-27 20:48:25 +00:00
|
|
|
}
|
2014-11-03 09:03:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-27 20:48:25 +00:00
|
|
|
} else if (result.containsKey("error")
|
|
|
|
&& "nosrv".equals(result.getString("error", null))) {
|
2014-11-05 20:55:47 +00:00
|
|
|
socket = new Socket(account.getServer().getDomainpart(), 5222);
|
2014-10-04 12:33:14 +00:00
|
|
|
} else {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
2014-10-05 12:26:06 +00:00
|
|
|
+ ": timeout in DNS resolution");
|
2014-11-15 16:09:02 +00:00
|
|
|
changeStatus(Account.State.OFFLINE);
|
2014-10-04 12:33:14 +00:00
|
|
|
return;
|
2014-02-07 05:52:09 +00:00
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
OutputStream out = socket.getOutputStream();
|
|
|
|
tagWriter.setOutputStream(out);
|
|
|
|
InputStream in = socket.getInputStream();
|
|
|
|
tagReader.setInputStream(in);
|
|
|
|
tagWriter.beginDocument();
|
|
|
|
sendStartStream();
|
|
|
|
Tag nextTag;
|
|
|
|
while ((nextTag = tagReader.readTag()) != null) {
|
|
|
|
if (nextTag.isStart("stream")) {
|
|
|
|
processStream(nextTag);
|
2014-02-04 14:09:50 +00:00
|
|
|
break;
|
2014-01-30 15:42:35 +00:00
|
|
|
} else {
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG,
|
|
|
|
"found unexpected tag: " + nextTag.getName());
|
2014-02-01 00:25:56 +00:00
|
|
|
return;
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
if (socket.isConnected()) {
|
|
|
|
socket.close();
|
|
|
|
}
|
2014-02-01 00:25:56 +00:00
|
|
|
} catch (UnknownHostException e) {
|
2014-11-15 16:09:02 +00:00
|
|
|
this.changeStatus(Account.State.SERVER_NOT_FOUND);
|
2014-03-06 02:30:03 +00:00
|
|
|
if (wakeLock.isHeld()) {
|
2014-08-31 14:28:21 +00:00
|
|
|
try {
|
|
|
|
wakeLock.release();
|
2014-11-05 20:55:47 +00:00
|
|
|
} catch (final RuntimeException ignored) {
|
2014-08-31 14:28:21 +00:00
|
|
|
}
|
2014-03-06 02:30:03 +00:00
|
|
|
}
|
2014-11-12 20:35:44 +00:00
|
|
|
} catch (final IOException | XmlPullParserException e) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
2014-11-15 16:09:02 +00:00
|
|
|
this.changeStatus(Account.State.OFFLINE);
|
2014-03-06 02:30:03 +00:00
|
|
|
if (wakeLock.isHeld()) {
|
2014-08-31 14:28:21 +00:00
|
|
|
try {
|
|
|
|
wakeLock.release();
|
2014-11-05 20:55:47 +00:00
|
|
|
} catch (final RuntimeException ignored) {
|
2014-08-31 14:28:21 +00:00
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
}
|
2014-11-12 20:35:44 +00:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
|
2014-11-15 16:09:02 +00:00
|
|
|
this.changeStatus(Account.State.OFFLINE);
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG, "compression exception " + e.getMessage());
|
2014-04-03 16:16:14 +00:00
|
|
|
if (wakeLock.isHeld()) {
|
2014-08-31 14:28:21 +00:00
|
|
|
try {
|
|
|
|
wakeLock.release();
|
2014-11-05 20:55:47 +00:00
|
|
|
} catch (final RuntimeException ignored) {
|
2014-08-31 14:28:21 +00:00
|
|
|
}
|
2014-03-06 02:30:03 +00:00
|
|
|
}
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-02-01 00:25:56 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-06 02:30:03 +00:00
|
|
|
connect();
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
private void processStream(final Tag currentTag) throws XmlPullParserException,
|
2014-11-12 20:35:44 +00: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("compressed")) {
|
|
|
|
switchOverToZLib(nextTag);
|
|
|
|
} else if (nextTag.isStart("success")) {
|
|
|
|
final String challenge = tagReader.readElement(nextTag).getContent();
|
|
|
|
try {
|
|
|
|
saslMechanism.getResponse(challenge);
|
2014-11-15 13:48:40 +00:00
|
|
|
} catch (final SaslMechanism.AuthenticationException e) {
|
2014-11-12 20:35:44 +00:00
|
|
|
disconnect(true);
|
|
|
|
Log.e(Config.LOGTAG, String.valueOf(e));
|
|
|
|
}
|
2014-11-15 13:48:40 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
|
2014-11-15 14:42:40 +00:00
|
|
|
account.setKey(Account.PINNED_MECHANISM_KEY,
|
2014-11-15 16:09:02 +00:00
|
|
|
String.valueOf(saslMechanism.getPriority()));
|
2014-11-12 20:35:44 +00:00
|
|
|
tagReader.reset();
|
|
|
|
sendStartStream();
|
|
|
|
processStream(tagReader.readTag());
|
|
|
|
break;
|
|
|
|
} else if (nextTag.isStart("failure")) {
|
|
|
|
tagReader.readElement(nextTag);
|
2014-11-15 16:09:02 +00:00
|
|
|
changeStatus(Account.State.UNAUTHORIZED);
|
2014-11-12 20:35:44 +00: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 13:48:40 +00:00
|
|
|
} catch (final SaslMechanism.AuthenticationException e) {
|
2014-11-12 20:35:44 +00:00
|
|
|
// TODO: Send auth abort tag.
|
|
|
|
Log.e(Config.LOGTAG, e.toString());
|
|
|
|
}
|
|
|
|
tagWriter.writeElement(response);
|
|
|
|
} else if (nextTag.isStart("enabled")) {
|
|
|
|
Element enabled = tagReader.readElement(nextTag);
|
|
|
|
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;
|
|
|
|
RequestPacket r = new RequestPacket(smVersion);
|
|
|
|
tagWriter.writeStanzaAsync(r);
|
|
|
|
} else if (nextTag.isStart("resumed")) {
|
|
|
|
lastPaketReceived = SystemClock.elapsedRealtime();
|
|
|
|
Element resumed = tagReader.readElement(nextTag);
|
|
|
|
String h = resumed.getAttribute("h");
|
|
|
|
try {
|
|
|
|
int serverCount = Integer.parseInt(h);
|
|
|
|
if (serverCount != stanzasSent) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
|
|
|
+ ": session resumed with lost packages");
|
|
|
|
stanzasSent = serverCount;
|
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
|
|
|
+ ": session resumed");
|
|
|
|
}
|
|
|
|
if (acknowledgedListener != null) {
|
|
|
|
for (int i = 0; i < messageReceipts.size(); ++i) {
|
|
|
|
if (serverCount >= messageReceipts.keyAt(i)) {
|
|
|
|
acknowledgedListener.onMessageAcknowledged(
|
|
|
|
account, messageReceipts.valueAt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
messageReceipts.clear();
|
|
|
|
} catch (final NumberFormatException ignored) {
|
|
|
|
|
|
|
|
}
|
|
|
|
sendInitialPing();
|
|
|
|
|
|
|
|
} else if (nextTag.isStart("r")) {
|
|
|
|
tagReader.readElement(nextTag);
|
|
|
|
AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
|
|
|
|
tagWriter.writeStanzaAsync(ack);
|
|
|
|
} else if (nextTag.isStart("a")) {
|
|
|
|
Element ack = tagReader.readElement(nextTag);
|
|
|
|
lastPaketReceived = SystemClock.elapsedRealtime();
|
|
|
|
int serverSequence = Integer.parseInt(ack.getAttribute("h"));
|
|
|
|
String msgId = this.messageReceipts.get(serverSequence);
|
|
|
|
if (msgId != null) {
|
|
|
|
if (this.acknowledgedListener != null) {
|
|
|
|
this.acknowledgedListener.onMessageAcknowledged(
|
|
|
|
account, msgId);
|
|
|
|
}
|
|
|
|
this.messageReceipts.remove(serverSequence);
|
|
|
|
}
|
|
|
|
} else if (nextTag.isStart("failed")) {
|
|
|
|
tagReader.readElement(nextTag);
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": resumption failed");
|
|
|
|
streamId = null;
|
2014-11-15 16:09:02 +00:00
|
|
|
if (account.getStatus() != Account.State.ONLINE) {
|
2014-11-12 20:35:44 +00: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 16:09:02 +00:00
|
|
|
if (account.getStatus() == Account.State.ONLINE) {
|
|
|
|
account. setStatus(Account.State.OFFLINE);
|
2014-11-12 20:35:44 +00:00
|
|
|
if (statusListener != null) {
|
|
|
|
statusListener.onStatusChanged(account);
|
2014-08-27 12:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-10-05 12:26:06 +00:00
|
|
|
|
2014-10-05 08:46:31 +00:00
|
|
|
private void sendInitialPing() {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping");
|
2014-10-05 08:46:31 +00:00
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
2014-11-09 15:57:22 +00:00
|
|
|
iq.setFrom(account.getJid());
|
2014-10-05 08:46:31 +00:00
|
|
|
iq.addChild("ping", "urn:xmpp:ping");
|
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
2014-10-05 12:26:06 +00:00
|
|
|
|
2014-10-20 19:08:33 +00:00
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
|
2014-10-20 19:08:33 +00:00
|
|
|
+ ": online with resource " + account.getResource());
|
2014-11-15 16:09:02 +00:00
|
|
|
changeStatus(Account.State.ONLINE);
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
|
|
|
});
|
2014-10-05 08:46:31 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
|
|
|
private Element processPacket(Tag currentTag, int packetType)
|
2014-11-12 20:35:44 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2014-01-30 23:33:01 +00:00
|
|
|
Element element;
|
|
|
|
switch (packetType) {
|
2014-11-03 09:03:45 +00: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 15:42:35 +00:00
|
|
|
}
|
2014-01-30 23:33:01 +00:00
|
|
|
element.setAttributes(currentTag.getAttributes());
|
2014-01-30 15:42:35 +00:00
|
|
|
Tag nextTag = tagReader.readTag();
|
2014-08-31 14:28:21 +00:00
|
|
|
if (nextTag == null) {
|
2014-05-23 08:15:58 +00:00
|
|
|
throw new IOException("interrupted mid tag");
|
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
while (!nextTag.isEnd(element.getName())) {
|
2014-01-30 23:33:01 +00:00
|
|
|
if (!nextTag.isNo()) {
|
|
|
|
Element child = tagReader.readElement(nextTag);
|
2014-10-05 12:26:06 +00:00
|
|
|
String type = currentTag.getAttribute("type");
|
|
|
|
if (packetType == PACKET_IQ
|
|
|
|
&& "jingle".equals(child.getName())
|
|
|
|
&& ("set".equalsIgnoreCase(type) || "get"
|
2014-11-12 20:35:44 +00:00
|
|
|
.equalsIgnoreCase(type))) {
|
2014-03-27 01:02:59 +00:00
|
|
|
element = new JinglePacket();
|
|
|
|
element.setAttributes(currentTag.getAttributes());
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-01-30 23:33:01 +00:00
|
|
|
element.addChild(child);
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
nextTag = tagReader.readTag();
|
2014-08-31 14:28:21 +00:00
|
|
|
if (nextTag == null) {
|
2014-05-23 08:15:58 +00:00
|
|
|
throw new IOException("interrupted mid tag");
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-03-10 18:22:13 +00:00
|
|
|
++stanzasReceived;
|
2014-03-11 14:44:22 +00:00
|
|
|
lastPaketReceived = SystemClock.elapsedRealtime();
|
2014-01-30 23:33:01 +00:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2014-03-03 04:01:02 +00:00
|
|
|
private void processIq(Tag currentTag) throws XmlPullParserException,
|
2014-11-12 20:35:44 +00:00
|
|
|
IOException {
|
|
|
|
IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-11-12 20:35:44 +00:00
|
|
|
if (packet.getId() == null) {
|
|
|
|
return; // an iq packet without id is definitely invalid
|
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-11-12 20:35:44 +00:00
|
|
|
if (packet instanceof JinglePacket) {
|
|
|
|
if (this.jingleListener != null) {
|
|
|
|
this.jingleListener.onJinglePacketReceived(account,
|
|
|
|
(JinglePacket) packet);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (packetCallbacks.containsKey(packet.getId())) {
|
|
|
|
if (packetCallbacks.get(packet.getId()) instanceof OnIqPacketReceived) {
|
|
|
|
((OnIqPacketReceived) packetCallbacks.get(packet.getId()))
|
|
|
|
.onIqPacketReceived(account, packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
packetCallbacks.remove(packet.getId());
|
|
|
|
} else if ((packet.getType() == IqPacket.TYPE_GET || packet
|
|
|
|
.getType() == IqPacket.TYPE_SET)
|
|
|
|
&& this.unregisteredIqListener != null) {
|
|
|
|
this.unregisteredIqListener.onIqPacketReceived(account, packet);
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 23:33:01 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
|
|
|
private void processMessage(Tag currentTag) throws XmlPullParserException,
|
2014-11-12 20:35:44 +00:00
|
|
|
IOException {
|
|
|
|
MessagePacket packet = (MessagePacket) processPacket(currentTag,
|
|
|
|
PACKET_MESSAGE);
|
|
|
|
String id = packet.getAttribute("id");
|
|
|
|
if ((id != null) && (packetCallbacks.containsKey(id))) {
|
|
|
|
if (packetCallbacks.get(id) instanceof OnMessagePacketReceived) {
|
|
|
|
((OnMessagePacketReceived) packetCallbacks.get(id))
|
|
|
|
.onMessagePacketReceived(account, packet);
|
|
|
|
}
|
|
|
|
packetCallbacks.remove(id);
|
|
|
|
} else if (this.messageListener != null) {
|
|
|
|
this.messageListener.onMessagePacketReceived(account, packet);
|
|
|
|
}
|
2014-01-30 23:33:01 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
|
|
|
private void processPresence(Tag currentTag) throws XmlPullParserException,
|
2014-11-12 20:35:44 +00:00
|
|
|
IOException {
|
|
|
|
PresencePacket packet = (PresencePacket) processPacket(currentTag,
|
|
|
|
PACKET_PRESENCE);
|
|
|
|
String id = packet.getAttribute("id");
|
|
|
|
if ((id != null) && (packetCallbacks.containsKey(id))) {
|
|
|
|
if (packetCallbacks.get(id) instanceof OnPresencePacketReceived) {
|
|
|
|
((OnPresencePacketReceived) packetCallbacks.get(id))
|
|
|
|
.onPresencePacketReceived(account, packet);
|
|
|
|
}
|
|
|
|
packetCallbacks.remove(id);
|
|
|
|
} else if (this.presenceListener != null) {
|
|
|
|
this.presenceListener.onPresencePacketReceived(account, packet);
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 16:16:14 +00:00
|
|
|
private void sendCompressionZlib() throws IOException {
|
2014-04-05 07:45:15 +00:00
|
|
|
Element compress = new Element("compress");
|
|
|
|
compress.setAttribute("xmlns", "http://jabber.org/protocol/compress");
|
|
|
|
compress.addChild("method").setContent("zlib");
|
|
|
|
tagWriter.writeElement(compress);
|
2014-04-03 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
private void switchOverToZLib(final Tag currentTag)
|
2014-11-12 20:35:44 +00:00
|
|
|
throws XmlPullParserException, IOException,
|
|
|
|
NoSuchAlgorithmException {
|
|
|
|
tagReader.readTag(); // read tag close
|
|
|
|
tagWriter.setOutputStream(new ZLibOutputStream(tagWriter
|
|
|
|
.getOutputStream()));
|
|
|
|
tagReader
|
|
|
|
.setInputStream(new ZLibInputStream(tagReader.getInputStream()));
|
|
|
|
|
|
|
|
sendStartStream();
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": compression enabled");
|
|
|
|
usingCompression = true;
|
|
|
|
processStream(tagReader.readTag());
|
2014-04-03 16:16:14 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:22:13 +00:00
|
|
|
private void sendStartTLS() throws IOException {
|
2014-01-30 15:42:35 +00:00
|
|
|
Tag startTLS = Tag.empty("starttls");
|
|
|
|
startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls");
|
2014-02-04 14:09:50 +00:00
|
|
|
tagWriter.writeTag(startTLS);
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
|
2014-10-19 19:53:03 +00:00
|
|
|
private SharedPreferences getPreferences() {
|
2014-10-20 19:08:33 +00:00
|
|
|
return PreferenceManager
|
2014-11-12 20:35:44 +00:00
|
|
|
.getDefaultSharedPreferences(applicationContext);
|
2014-10-19 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean enableLegacySSL() {
|
2014-10-19 20:11:35 +00:00
|
|
|
return getPreferences().getBoolean("enable_legacy_ssl", false);
|
2014-10-19 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
private void switchOverToTls(final Tag currentTag) throws XmlPullParserException,
|
2014-11-12 20:35:44 +00:00
|
|
|
IOException {
|
|
|
|
tagReader.readTag();
|
|
|
|
try {
|
|
|
|
SSLContext sc = SSLContext.getInstance("TLS");
|
|
|
|
sc.init(null,
|
|
|
|
new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},
|
|
|
|
mXmppConnectionService.getRNG());
|
|
|
|
SSLSocketFactory factory = sc.getSocketFactory();
|
|
|
|
|
|
|
|
if (factory == null) {
|
|
|
|
throw new IOException("SSLSocketFactory was null");
|
|
|
|
}
|
2014-11-03 09:03:45 +00:00
|
|
|
|
2014-11-12 20:35:44 +00:00
|
|
|
final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier());
|
2014-11-08 19:52:02 +00:00
|
|
|
|
2014-11-12 20:35:44 +00:00
|
|
|
if (socket == null) {
|
|
|
|
throw new IOException("socket was null");
|
|
|
|
}
|
|
|
|
final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,
|
|
|
|
socket.getInetAddress().getHostAddress(), socket.getPort(),
|
|
|
|
true);
|
|
|
|
|
|
|
|
// Support all protocols except legacy SSL.
|
|
|
|
// The min SDK version prevents us having to worry about SSLv2. In
|
|
|
|
// future, this may be true of SSLv3 as well.
|
|
|
|
final String[] supportProtocols;
|
|
|
|
if (enableLegacySSL()) {
|
|
|
|
supportProtocols = sslSocket.getSupportedProtocols();
|
|
|
|
} else {
|
|
|
|
final List<String> supportedProtocols = new LinkedList<>(
|
|
|
|
Arrays.asList(sslSocket.getSupportedProtocols()));
|
|
|
|
supportedProtocols.remove("SSLv3");
|
|
|
|
supportProtocols = new String[supportedProtocols.size()];
|
|
|
|
supportedProtocols.toArray(supportProtocols);
|
|
|
|
}
|
|
|
|
sslSocket.setEnabledProtocols(supportProtocols);
|
|
|
|
|
|
|
|
if (verifier != null
|
|
|
|
&& !verifier.verify(account.getServer().getDomainpart(),
|
|
|
|
sslSocket.getSession())) {
|
|
|
|
sslSocket.close();
|
|
|
|
throw new IOException("host mismatch in TLS connection");
|
|
|
|
}
|
|
|
|
tagReader.setInputStream(sslSocket.getInputStream());
|
|
|
|
tagWriter.setOutputStream(sslSocket.getOutputStream());
|
|
|
|
sendStartStream();
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
|
|
|
+ ": TLS connection established");
|
|
|
|
usingEncryption = true;
|
|
|
|
processStream(tagReader.readTag());
|
|
|
|
sslSocket.close();
|
|
|
|
} catch (final NoSuchAlgorithmException | KeyManagementException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
|
|
|
|
private void processStreamFeatures(Tag currentTag)
|
2014-11-12 20:35:44 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2014-02-01 00:25:56 +00:00
|
|
|
this.streamFeatures = tagReader.readElement(currentTag);
|
2014-10-19 18:48:01 +00:00
|
|
|
if (this.streamFeatures.hasChild("starttls") && !usingEncryption) {
|
2014-02-01 00:25:56 +00:00
|
|
|
sendStartTLS();
|
2014-04-03 16:16:14 +00:00
|
|
|
} else if (compressionAvailable()) {
|
|
|
|
sendCompressionZlib();
|
2014-05-21 20:22:36 +00:00
|
|
|
} else if (this.streamFeatures.hasChild("register")
|
2014-10-20 19:08:33 +00:00
|
|
|
&& account.isOptionSet(Account.OPTION_REGISTER)
|
|
|
|
&& usingEncryption) {
|
2014-05-21 20:22:36 +00:00
|
|
|
sendRegistryRequest();
|
|
|
|
} else if (!this.streamFeatures.hasChild("register")
|
2014-10-19 21:14:17 +00:00
|
|
|
&& account.isOptionSet(Account.OPTION_REGISTER)) {
|
2014-11-15 16:09:02 +00:00
|
|
|
changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED);
|
2014-03-14 17:56:52 +00:00
|
|
|
disconnect(true);
|
2014-02-09 13:10:52 +00:00
|
|
|
} else if (this.streamFeatures.hasChild("mechanisms")
|
2014-10-19 18:48:01 +00:00
|
|
|
&& shouldAuthenticate && usingEncryption) {
|
2014-11-12 15:15:38 +00:00
|
|
|
final List<String> mechanisms = extractMechanisms(streamFeatures
|
2014-05-21 20:22:36 +00:00
|
|
|
.findChild("mechanisms"));
|
2014-11-12 20:35:44 +00:00
|
|
|
final Element auth = new Element("auth");
|
|
|
|
auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
|
2014-11-15 14:42:40 +00:00
|
|
|
if (mechanisms.contains("SCRAM-SHA-1")) {
|
2014-11-12 20:35:44 +00:00
|
|
|
saslMechanism = new ScramSha1(tagWriter, account, mXmppConnectionService.getRNG());
|
2014-11-15 14:42:40 +00:00
|
|
|
} else if (mechanisms.contains("DIGEST-MD5")) {
|
2014-11-12 20:35:44 +00:00
|
|
|
saslMechanism = new DigestMd5(tagWriter, account, mXmppConnectionService.getRNG());
|
2014-11-15 14:42:40 +00:00
|
|
|
} else if (mechanisms.contains("PLAIN")) {
|
2014-11-12 20:35:44 +00:00
|
|
|
saslMechanism = new Plain(tagWriter, account);
|
|
|
|
}
|
2014-11-15 14:42:40 +00:00
|
|
|
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?");
|
|
|
|
disconnect(true);
|
2014-11-15 16:19:42 +00:00
|
|
|
account.setStatus(Account.State.SECURITY_ERROR);
|
|
|
|
}
|
2014-11-15 14:42:40 +00:00
|
|
|
} catch (final JSONException e) {
|
|
|
|
Log.d(Config.LOGTAG, "Parse error while checking pinned auth mechanism");
|
|
|
|
}
|
|
|
|
Log.d(Config.LOGTAG, "Authenticating with " + saslMechanism.getMechanism());
|
|
|
|
auth.setAttribute("mechanism", saslMechanism.getMechanism());
|
|
|
|
if (!saslMechanism.getClientFirstMessage().isEmpty()) {
|
|
|
|
auth.setContent(saslMechanism.getClientFirstMessage());
|
|
|
|
}
|
|
|
|
tagWriter.writeElement(auth);
|
2014-11-12 20:35:44 +00:00
|
|
|
} else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:"
|
|
|
|
+ smVersion)
|
2014-05-21 20:22:36 +00:00
|
|
|
&& streamId != null) {
|
|
|
|
ResumePacket resume = new ResumePacket(this.streamId,
|
|
|
|
stanzasReceived, smVersion);
|
2014-03-10 18:22:13 +00:00
|
|
|
this.tagWriter.writeStanzaAsync(resume);
|
|
|
|
} else if (this.streamFeatures.hasChild("bind") && shouldBind) {
|
2014-02-01 00:25:56 +00:00
|
|
|
sendBindRequest();
|
2014-10-19 21:14:17 +00:00
|
|
|
} else {
|
2014-11-15 16:29:58 +00:00
|
|
|
account.setStatus(Account.State.INCOMPATIBLE_SERVER);
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
2014-10-20 19:08:33 +00:00
|
|
|
+ ": incompatible server. disconnecting");
|
2014-10-19 21:14:17 +00:00
|
|
|
disconnect(true);
|
2014-03-10 18:22:13 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
2014-04-03 16:16:14 +00:00
|
|
|
private boolean compressionAvailable() {
|
2014-05-21 20:22:36 +00:00
|
|
|
if (!this.streamFeatures.hasChild("compression",
|
2014-11-12 20:35:44 +00:00
|
|
|
"http://jabber.org/features/compress"))
|
2014-05-21 20:22:36 +00:00
|
|
|
return false;
|
|
|
|
if (!ZLibOutputStream.SUPPORTED)
|
|
|
|
return false;
|
|
|
|
if (!account.isOptionSet(Account.OPTION_USECOMPRESSION))
|
|
|
|
return false;
|
2014-04-03 16:16:14 +00:00
|
|
|
|
2014-05-21 20:22:36 +00:00
|
|
|
Element compression = this.streamFeatures.findChild("compression",
|
|
|
|
"http://jabber.org/features/compress");
|
2014-04-03 16:16:14 +00:00
|
|
|
for (Element child : compression.getChildren()) {
|
2014-05-21 20:22:36 +00:00
|
|
|
if (!"method".equals(child.getName()))
|
|
|
|
continue;
|
2014-04-03 16:16:14 +00:00
|
|
|
|
|
|
|
if ("zlib".equalsIgnoreCase(child.getContent())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-30 19:21:55 +00:00
|
|
|
private List<String> extractMechanisms(Element stream) {
|
2014-11-05 20:55:47 +00:00
|
|
|
ArrayList<String> mechanisms = new ArrayList<>(stream
|
2014-05-21 20:22:36 +00:00
|
|
|
.getChildren().size());
|
|
|
|
for (Element child : stream.getChildren()) {
|
2014-03-30 19:21:55 +00:00
|
|
|
mechanisms.add(child.getContent());
|
|
|
|
}
|
|
|
|
return mechanisms;
|
|
|
|
}
|
|
|
|
|
2014-03-14 17:56:52 +00:00
|
|
|
private void sendRegistryRequest() {
|
|
|
|
IqPacket register = new IqPacket(IqPacket.TYPE_GET);
|
|
|
|
register.query("jabber:iq:register");
|
|
|
|
register.setTo(account.getServer());
|
|
|
|
sendIqPacket(register, new OnIqPacketReceived() {
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-03-14 17:56:52 +00:00
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
|
|
|
Element instructions = packet.query().findChild("instructions");
|
2014-05-21 20:22:36 +00:00
|
|
|
if (packet.query().hasChild("username")
|
|
|
|
&& (packet.query().hasChild("password"))) {
|
2014-03-14 17:56:52 +00:00
|
|
|
IqPacket register = new IqPacket(IqPacket.TYPE_SET);
|
2014-05-21 20:22:36 +00:00
|
|
|
Element username = new Element("username")
|
2014-11-12 20:35:44 +00:00
|
|
|
.setContent(account.getUsername());
|
2014-05-21 20:22:36 +00:00
|
|
|
Element password = new Element("password")
|
2014-11-12 20:35:44 +00:00
|
|
|
.setContent(account.getPassword());
|
2014-03-20 14:49:53 +00:00
|
|
|
register.query("jabber:iq:register").addChild(username);
|
|
|
|
register.query().addChild(password);
|
2014-03-14 17:56:52 +00:00
|
|
|
sendIqPacket(register, new OnIqPacketReceived() {
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-03-14 17:56:52 +00:00
|
|
|
@Override
|
2014-05-21 20:22:36 +00:00
|
|
|
public void onIqPacketReceived(Account account,
|
2014-11-12 20:35:44 +00:00
|
|
|
IqPacket packet) {
|
2014-05-21 20:22:36 +00:00
|
|
|
if (packet.getType() == IqPacket.TYPE_RESULT) {
|
|
|
|
account.setOption(Account.OPTION_REGISTER,
|
|
|
|
false);
|
2014-11-15 16:09:02 +00:00
|
|
|
changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
|
2014-05-21 20:22:36 +00:00
|
|
|
} else if (packet.hasChild("error")
|
|
|
|
&& (packet.findChild("error")
|
2014-11-12 20:35:44 +00:00
|
|
|
.hasChild("conflict"))) {
|
2014-11-15 16:09:02 +00:00
|
|
|
changeStatus(Account.State.REGISTRATION_CONFLICT);
|
2014-03-14 17:56:52 +00:00
|
|
|
} else {
|
2014-11-15 16:09:02 +00:00
|
|
|
changeStatus(Account.State.REGISTRATION_FAILED);
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG, packet.toString());
|
2014-03-14 17:56:52 +00:00
|
|
|
}
|
|
|
|
disconnect(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-11-15 16:09:02 +00:00
|
|
|
changeStatus(Account.State.REGISTRATION_FAILED);
|
2014-03-14 17:56:52 +00:00
|
|
|
disconnect(true);
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
2014-05-21 20:22:36 +00:00
|
|
|
+ ": could not register. instructions are"
|
|
|
|
+ instructions.getContent());
|
2014-03-14 17:56:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
private void sendBindRequest() throws IOException {
|
2014-02-01 00:25:56 +00:00
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
|
2014-05-21 20:22:36 +00:00
|
|
|
iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind")
|
2014-11-12 20:35:44 +00:00
|
|
|
.addChild("resource").setContent(account.getResource());
|
2014-05-18 15:32:20 +00:00
|
|
|
this.sendUnboundIqPacket(iq, new OnIqPacketReceived() {
|
2014-02-01 00:25:56 +00:00
|
|
|
@Override
|
2014-02-01 14:07:20 +00:00
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2014-07-26 13:44:32 +00:00
|
|
|
Element bind = packet.findChild("bind");
|
2014-08-31 14:28:21 +00:00
|
|
|
if (bind != null) {
|
2014-11-05 20:55:47 +00:00
|
|
|
final Element jid = bind.findChild("jid");
|
2014-09-22 14:04:37 +00:00
|
|
|
if (jid != null && jid.getContent() != null) {
|
2014-11-12 20:35:44 +00:00
|
|
|
try {
|
|
|
|
account.setResource(Jid.fromString(jid.getContent()).getResourcepart());
|
|
|
|
} catch (final InvalidJidException e) {
|
|
|
|
// TODO: Handle the case where an external JID is technically invalid?
|
|
|
|
}
|
|
|
|
if (streamFeatures.hasChild("sm", "urn:xmpp:sm:3")) {
|
2014-07-26 13:44:32 +00:00
|
|
|
smVersion = 3;
|
|
|
|
EnablePacket enable = new EnablePacket(smVersion);
|
|
|
|
tagWriter.writeStanzaAsync(enable);
|
2014-08-26 14:52:42 +00:00
|
|
|
stanzasSent = 0;
|
2014-08-27 17:25:58 +00:00
|
|
|
messageReceipts.clear();
|
2014-08-31 14:28:21 +00:00
|
|
|
} else if (streamFeatures.hasChild("sm",
|
2014-11-12 20:35:44 +00:00
|
|
|
"urn:xmpp:sm:2")) {
|
2014-07-26 13:44:32 +00:00
|
|
|
smVersion = 2;
|
|
|
|
EnablePacket enable = new EnablePacket(smVersion);
|
|
|
|
tagWriter.writeStanzaAsync(enable);
|
2014-08-26 14:52:42 +00:00
|
|
|
stanzasSent = 0;
|
2014-08-27 17:25:58 +00:00
|
|
|
messageReceipts.clear();
|
2014-07-26 13:44:32 +00:00
|
|
|
}
|
|
|
|
sendServiceDiscoveryInfo(account.getServer());
|
|
|
|
sendServiceDiscoveryItems(account.getServer());
|
|
|
|
if (bindListener != null) {
|
|
|
|
bindListener.onBind(account);
|
|
|
|
}
|
2014-10-05 08:46:31 +00:00
|
|
|
sendInitialPing();
|
2014-07-26 13:44:32 +00:00
|
|
|
} else {
|
|
|
|
disconnect(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
disconnect(true);
|
2014-02-04 14:09:50 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
}
|
|
|
|
});
|
2014-05-17 19:57:44 +00:00
|
|
|
if (this.streamFeatures.hasChild("session")) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
2014-08-31 14:28:21 +00:00
|
|
|
+ ": sending deprecated session");
|
2014-05-17 19:57:44 +00:00
|
|
|
IqPacket startSession = new IqPacket(IqPacket.TYPE_SET);
|
2014-05-21 20:22:36 +00:00
|
|
|
startSession.addChild("session",
|
|
|
|
"urn:ietf:params:xml:ns:xmpp-session");
|
2014-05-18 15:32:20 +00:00
|
|
|
this.sendUnboundIqPacket(startSession, null);
|
2014-05-17 19:57:44 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
private void sendServiceDiscoveryInfo(final Jid server) {
|
|
|
|
final IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
|
|
|
iq.setTo(server.toDomainJid());
|
2014-03-15 03:59:18 +00:00
|
|
|
iq.query("http://jabber.org/protocol/disco#info");
|
2014-02-09 13:10:52 +00:00
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2014-11-05 20:55:47 +00:00
|
|
|
final List<Element> elements = packet.query().getChildren();
|
|
|
|
final List<String> features = new ArrayList<>();
|
2014-11-12 20:35:44 +00:00
|
|
|
for (Element element : elements) {
|
|
|
|
if (element.getName().equals("feature")) {
|
|
|
|
features.add(element.getAttribute("var"));
|
|
|
|
}
|
|
|
|
}
|
2014-11-05 20:55:47 +00:00
|
|
|
disco.put(server.toDomainJid().toString(), features);
|
|
|
|
|
|
|
|
if (account.getServer().equals(server.toDomainJid())) {
|
2014-04-08 21:15:55 +00:00
|
|
|
enableAdvancedStreamFeatures();
|
2014-02-09 13:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
private void enableAdvancedStreamFeatures() {
|
2014-08-09 08:20:16 +00:00
|
|
|
if (getFeatures().carbons()) {
|
2014-04-08 21:15:55 +00:00
|
|
|
sendEnableCarbons();
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
private void sendServiceDiscoveryItems(final Jid server) {
|
|
|
|
final IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
|
|
|
iq.setTo(server.toDomainJid());
|
2014-03-15 03:59:18 +00:00
|
|
|
iq.query("http://jabber.org/protocol/disco#items");
|
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2014-04-08 21:15:55 +00:00
|
|
|
List<Element> elements = packet.query().getChildren();
|
2014-11-12 20:35:44 +00:00
|
|
|
for (Element element : elements) {
|
|
|
|
if (element.getName().equals("item")) {
|
|
|
|
final String jid = element.getAttribute("jid");
|
|
|
|
try {
|
|
|
|
sendServiceDiscoveryInfo(Jid.fromString(jid).toDomainJid());
|
|
|
|
} catch (final InvalidJidException ignored) {
|
|
|
|
// TODO: Handle the case where an external JID is technically invalid?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-15 03:59:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-06 14:11:56 +00:00
|
|
|
|
2014-02-09 13:10:52 +00:00
|
|
|
private void sendEnableCarbons() {
|
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
|
2014-05-21 20:22:36 +00:00
|
|
|
iq.addChild("enable", "urn:xmpp:carbons:2");
|
2014-02-09 13:10:52 +00:00
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
2014-03-06 14:11:56 +00:00
|
|
|
|
2014-02-09 13:10:52 +00:00
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
|
|
|
if (!packet.hasChild("error")) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
2014-03-06 14:11:56 +00:00
|
|
|
+ ": successfully enabled carbons");
|
2014-02-09 13:10:52 +00:00
|
|
|
} else {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid()
|
2014-03-06 14:11:56 +00:00
|
|
|
+ ": error enableing carbons " + packet.toString());
|
2014-02-09 13:10:52 +00:00
|
|
|
}
|
2014-02-01 00:25:56 +00:00
|
|
|
}
|
|
|
|
});
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 21:58:37 +00:00
|
|
|
private void processStreamError(Tag currentTag)
|
2014-11-12 20:35:44 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2014-09-03 12:57:40 +00:00
|
|
|
Element streamError = tagReader.readElement(currentTag);
|
2014-09-08 21:58:37 +00:00
|
|
|
if (streamError != null && streamError.hasChild("conflict")) {
|
2014-11-05 20:55:47 +00:00
|
|
|
final String resource = account.getResource().split("\\.")[0];
|
2014-11-12 20:35:44 +00:00
|
|
|
account.setResource(resource + "." + nextRandomId());
|
|
|
|
Log.d(Config.LOGTAG,
|
2014-11-09 15:57:22 +00:00
|
|
|
account.getJid().toBareJid() + ": switching resource due to conflict ("
|
2014-11-12 20:35:44 +00:00
|
|
|
+ account.getResource() + ")");
|
2014-09-03 12:57:40 +00:00
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 18:22:13 +00:00
|
|
|
private void sendStartStream() throws IOException {
|
2014-02-07 15:50:29 +00:00
|
|
|
Tag stream = Tag.start("stream:stream");
|
2014-11-09 15:57:22 +00:00
|
|
|
stream.setAttribute("from", account.getJid().toBareJid().toString());
|
2014-11-05 20:55:47 +00:00
|
|
|
stream.setAttribute("to", account.getServer().toString());
|
2014-01-30 15:42:35 +00: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 14:09:50 +00:00
|
|
|
tagWriter.writeTag(stream);
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private String nextRandomId() {
|
2014-11-09 09:50:45 +00:00
|
|
|
return new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
2014-02-04 14:09:50 +00:00
|
|
|
public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) {
|
2014-05-21 20:22:36 +00:00
|
|
|
if (packet.getId() == null) {
|
2014-04-11 19:13:09 +00:00
|
|
|
String id = nextRandomId();
|
|
|
|
packet.setAttribute("id", id);
|
|
|
|
}
|
2014-11-09 15:57:22 +00:00
|
|
|
packet.setFrom(account.getJid());
|
2014-03-10 18:22:13 +00:00
|
|
|
this.sendPacket(packet, callback);
|
2014-02-01 00:25:56 +00:00
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-05-18 15:32:20 +00:00
|
|
|
public void sendUnboundIqPacket(IqPacket packet, OnIqPacketReceived callback) {
|
2014-05-21 20:22:36 +00:00
|
|
|
if (packet.getId() == null) {
|
2014-05-18 15:32:20 +00:00
|
|
|
String id = nextRandomId();
|
|
|
|
packet.setAttribute("id", id);
|
|
|
|
}
|
|
|
|
this.sendPacket(packet, callback);
|
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
|
|
|
public void sendMessagePacket(MessagePacket packet) {
|
2014-03-10 18:22:13 +00:00
|
|
|
this.sendPacket(packet, null);
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|
2014-03-06 14:11:56 +00:00
|
|
|
|
2014-02-09 13:10:52 +00:00
|
|
|
public void sendPresencePacket(PresencePacket packet) {
|
2014-03-10 18:22:13 +00:00
|
|
|
this.sendPacket(packet, null);
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-05-21 20:22:36 +00:00
|
|
|
private synchronized void sendPacket(final AbstractStanza packet,
|
2014-11-12 20:35:44 +00:00
|
|
|
PacketReceived callback) {
|
2014-08-31 14:28:21 +00:00
|
|
|
if (packet.getName().equals("iq") || packet.getName().equals("message")
|
|
|
|
|| packet.getName().equals("presence")) {
|
2014-08-26 14:52:42 +00:00
|
|
|
++stanzasSent;
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-03-10 18:22:13 +00:00
|
|
|
tagWriter.writeStanzaAsync(packet);
|
2014-08-31 14:28:21 +00:00
|
|
|
if (packet instanceof MessagePacket && packet.getId() != null
|
|
|
|
&& this.streamId != null) {
|
|
|
|
Log.d(Config.LOGTAG, "request delivery report for stanza "
|
|
|
|
+ stanzasSent);
|
2014-08-26 14:52:42 +00:00
|
|
|
this.messageReceipts.put(stanzasSent, packet.getId());
|
|
|
|
tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion));
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-03-03 04:01:02 +00:00
|
|
|
if (callback != null) {
|
2014-05-21 20:22:36 +00:00
|
|
|
if (packet.getId() == null) {
|
2014-03-10 18:22:13 +00:00
|
|
|
packet.setId(nextRandomId());
|
|
|
|
}
|
|
|
|
packetCallbacks.put(packet.getId(), callback);
|
2014-03-03 04:01:02 +00:00
|
|
|
}
|
2014-02-01 14:07:20 +00:00
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-03-11 14:44:22 +00:00
|
|
|
public void sendPing() {
|
|
|
|
if (streamFeatures.hasChild("sm")) {
|
2014-04-05 10:08:35 +00:00
|
|
|
tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
|
2014-03-11 14:44:22 +00:00
|
|
|
} else {
|
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
2014-11-09 15:57:22 +00:00
|
|
|
iq.setFrom(account.getJid());
|
2014-05-21 20:22:36 +00:00
|
|
|
iq.addChild("ping", "urn:xmpp:ping");
|
2014-03-11 14:44:22 +00:00
|
|
|
this.sendIqPacket(iq, null);
|
|
|
|
}
|
2014-08-19 13:06:50 +00:00
|
|
|
this.lastPingSent = SystemClock.elapsedRealtime();
|
2014-03-11 14:44:22 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
|
|
|
public void setOnMessagePacketReceivedListener(
|
|
|
|
OnMessagePacketReceived listener) {
|
2014-02-01 14:07:20 +00:00
|
|
|
this.messageListener = listener;
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
|
|
|
public void setOnUnregisteredIqPacketReceivedListener(
|
|
|
|
OnIqPacketReceived listener) {
|
2014-02-01 14:07:20 +00:00
|
|
|
this.unregisteredIqListener = listener;
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
|
|
|
public void setOnPresencePacketReceivedListener(
|
|
|
|
OnPresencePacketReceived listener) {
|
2014-02-01 14:07:20 +00:00
|
|
|
this.presenceListener = listener;
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
|
|
|
public void setOnJinglePacketReceivedListener(
|
|
|
|
OnJinglePacketReceived listener) {
|
2014-03-27 01:02:59 +00:00
|
|
|
this.jingleListener = listener;
|
2014-11-12 20:35:44 +00:00
|
|
|
}
|
2014-02-09 13:10:52 +00:00
|
|
|
|
2014-02-04 14:09:50 +00:00
|
|
|
public void setOnStatusChangedListener(OnStatusChanged listener) {
|
|
|
|
this.statusListener = listener;
|
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-03-14 19:43:54 +00:00
|
|
|
public void setOnBindListener(OnBindListener listener) {
|
|
|
|
this.bindListener = listener;
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-26 14:52:42 +00:00
|
|
|
public void setOnMessageAcknowledgeListener(OnMessageAcknowledged listener) {
|
|
|
|
this.acknowledgedListener = listener;
|
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
|
2014-03-10 18:22:13 +00:00
|
|
|
public void disconnect(boolean force) {
|
2014-11-09 15:57:22 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting");
|
2014-03-10 18:22:13 +00:00
|
|
|
try {
|
2014-05-21 20:22:36 +00:00
|
|
|
if (force) {
|
2014-03-10 18:22:13 +00:00
|
|
|
socket.close();
|
2014-03-12 21:31:50 +00:00
|
|
|
return;
|
2014-04-03 13:08:53 +00:00
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (tagWriter.isActive()) {
|
|
|
|
tagWriter.finish();
|
|
|
|
try {
|
|
|
|
while (!tagWriter.finished()) {
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG, "not yet finished");
|
2014-05-21 20:22:36 +00:00
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
tagWriter.writeTag(Tag.end("stream:stream"));
|
2014-10-08 10:21:58 +00:00
|
|
|
socket.close();
|
2014-05-21 20:22:36 +00:00
|
|
|
} catch (IOException e) {
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG,
|
|
|
|
"io exception during disconnect");
|
2014-05-21 20:22:36 +00:00
|
|
|
} catch (InterruptedException e) {
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG, "interrupted");
|
2014-05-21 20:22:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 08:37:49 +00:00
|
|
|
}).start();
|
2014-03-10 18:22:13 +00:00
|
|
|
} catch (IOException e) {
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG, "io exception during disconnect");
|
2014-03-10 18:22:13 +00:00
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-06-30 09:43:00 +00:00
|
|
|
public List<String> findDiscoItemsByFeature(String feature) {
|
2014-11-05 20:55:47 +00:00
|
|
|
final List<String> items = new ArrayList<>();
|
2014-07-11 17:48:02 +00:00
|
|
|
for (Entry<String, List<String>> cursor : disco.entrySet()) {
|
|
|
|
if (cursor.getValue().contains(feature)) {
|
|
|
|
items.add(cursor.getKey());
|
2014-05-21 20:22:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 09:43:00 +00:00
|
|
|
return items;
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-06-30 09:43:00 +00:00
|
|
|
public String findDiscoItemByFeature(String feature) {
|
|
|
|
List<String> items = findDiscoItemsByFeature(feature);
|
2014-08-31 14:28:21 +00:00
|
|
|
if (items.size() >= 1) {
|
2014-06-30 09:43:00 +00:00
|
|
|
return items.get(0);
|
|
|
|
}
|
2014-04-08 21:15:55 +00:00
|
|
|
return null;
|
2014-03-13 16:29:22 +00:00
|
|
|
}
|
2014-03-10 18:22:13 +00:00
|
|
|
|
|
|
|
public void r() {
|
2014-04-05 10:08:35 +00:00
|
|
|
this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
|
2014-03-10 18:22:13 +00:00
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-03-15 03:59:18 +00:00
|
|
|
public String getMucServer() {
|
2014-04-08 21:15:55 +00:00
|
|
|
return findDiscoItemByFeature("http://jabber.org/protocol/muc");
|
2014-03-15 03:59:18 +00:00
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-05-18 09:25:04 +00:00
|
|
|
public int getTimeToNextAttempt() {
|
2014-05-21 20:22:36 +00:00
|
|
|
int interval = (int) (25 * Math.pow(1.5, attempt));
|
2014-05-18 09:25:04 +00:00
|
|
|
int secondsSinceLast = (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
|
|
|
|
return interval - secondsSinceLast;
|
|
|
|
}
|
2014-05-21 20:22:36 +00:00
|
|
|
|
2014-05-18 09:25:04 +00:00
|
|
|
public int getAttempt() {
|
|
|
|
return this.attempt;
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-09 08:20:16 +00:00
|
|
|
public Features getFeatures() {
|
|
|
|
return this.features;
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-11-08 19:52:02 +00:00
|
|
|
public long getLastSessionEstablished() {
|
|
|
|
long diff;
|
|
|
|
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() {
|
|
|
|
return this.lastPaketReceived;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendActive() {
|
|
|
|
this.sendPacket(new ActivePacket(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendInactive() {
|
|
|
|
this.sendPacket(new InactivePacket(), null);
|
|
|
|
}
|
|
|
|
|
2014-08-09 08:20:16 +00:00
|
|
|
public class Features {
|
|
|
|
XmppConnection connection;
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-09 08:20:16 +00:00
|
|
|
public Features(XmppConnection connection) {
|
|
|
|
this.connection = connection;
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
private boolean hasDiscoFeature(final Jid server, final String feature) {
|
2014-11-12 20:35:44 +00:00
|
|
|
return connection.disco.containsKey(server.toDomainJid().toString()) &&
|
|
|
|
connection.disco.get(server.toDomainJid().toString()).contains(feature);
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-09 08:20:16 +00:00
|
|
|
public boolean carbons() {
|
|
|
|
return hasDiscoFeature(account.getServer(), "urn:xmpp:carbons:2");
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-09 08:20:16 +00:00
|
|
|
public boolean sm() {
|
2014-09-23 13:36:20 +00:00
|
|
|
return streamId != null;
|
2014-08-09 08:20:16 +00:00
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-26 15:43:44 +00:00
|
|
|
public boolean csi() {
|
2014-11-12 20:35:44 +00:00
|
|
|
return connection.streamFeatures != null && connection.streamFeatures.hasChild("csi", "urn:xmpp:csi:0");
|
2014-08-26 15:43:44 +00:00
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-09 08:20:16 +00:00
|
|
|
public boolean pubsub() {
|
2014-08-31 14:28:21 +00:00
|
|
|
return hasDiscoFeature(account.getServer(),
|
|
|
|
"http://jabber.org/protocol/pubsub#publish");
|
2014-08-09 08:20:16 +00:00
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-11-08 19:52:02 +00:00
|
|
|
public boolean mam() {
|
|
|
|
return hasDiscoFeature(account.getServer(), "urn:xmpp:mam:0");
|
|
|
|
}
|
|
|
|
|
2014-08-09 08:20:16 +00:00
|
|
|
public boolean rosterVersioning() {
|
2014-11-12 20:35:44 +00:00
|
|
|
return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
|
2014-08-09 08:20:16 +00:00
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-19 13:06:50 +00:00
|
|
|
public boolean streamhost() {
|
2014-08-31 14:28:21 +00:00
|
|
|
return connection
|
2014-11-12 20:35:44 +00:00
|
|
|
.findDiscoItemByFeature("http://jabber.org/protocol/bytestreams") != null;
|
2014-08-19 13:06:50 +00:00
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-08-19 13:06:50 +00:00
|
|
|
public boolean compression() {
|
|
|
|
return connection.usingCompression;
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|