2014-02-28 12:46:01 -05:00
|
|
|
package eu.siacs.conversations.xmpp;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2014-03-06 14:03:35 -05:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.KeyStore;
|
|
|
|
import java.security.KeyStoreException;
|
2014-03-07 08:24:33 -05:00
|
|
|
import java.security.MessageDigest;
|
2014-03-06 14:03:35 -05:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
2014-01-30 10:42:35 -05:00
|
|
|
import java.security.SecureRandom;
|
2014-03-06 09:11:56 -05:00
|
|
|
import java.security.cert.CertPathValidatorException;
|
2014-03-06 14:03:35 -05:00
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
import java.security.cert.X509Certificate;
|
2014-03-14 23:59:18 -04:00
|
|
|
import java.util.ArrayList;
|
2014-04-08 17:15:55 -04:00
|
|
|
import java.util.HashMap;
|
2014-01-31 19:25:56 -05:00
|
|
|
import java.util.Hashtable;
|
2014-04-08 17:15:55 -04:00
|
|
|
import java.util.Iterator;
|
2014-02-09 08:10:52 -05:00
|
|
|
import java.util.List;
|
2014-04-08 17:15:55 -04:00
|
|
|
import java.util.Map.Entry;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
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.TrustManager;
|
|
|
|
import javax.net.ssl.TrustManagerFactory;
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
2014-02-07 00:52:09 -05:00
|
|
|
import android.os.Bundle;
|
2014-01-30 10:42:35 -05:00
|
|
|
import android.os.PowerManager;
|
2014-03-23 09:15:14 -04:00
|
|
|
import android.os.PowerManager.WakeLock;
|
2014-03-11 10:44:22 -04:00
|
|
|
import android.os.SystemClock;
|
2014-01-30 10:42:35 -05:00
|
|
|
import android.util.Log;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2014-06-20 11:30:19 -04:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2014-03-07 08:24:33 -05:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.utils.DNSHelper;
|
2014-04-03 12:16:14 -04:00
|
|
|
import eu.siacs.conversations.utils.zlib.ZLibOutputStream;
|
|
|
|
import eu.siacs.conversations.utils.zlib.ZLibInputStream;
|
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-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;
|
|
|
|
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 {
|
|
|
|
|
|
|
|
protected Account account;
|
|
|
|
private static final String LOGTAG = "xmppService";
|
|
|
|
|
2014-04-03 17:57:26 -04:00
|
|
|
private WakeLock wakeLock;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
2014-06-20 11:30:19 -04:00
|
|
|
private SecureRandom mRandom;
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-01-30 10:42:35 -05:00
|
|
|
private Socket socket;
|
|
|
|
private XmlReader tagReader;
|
|
|
|
private TagWriter tagWriter;
|
|
|
|
|
2014-01-31 19:25:56 -05:00
|
|
|
private boolean shouldBind = true;
|
|
|
|
private boolean shouldAuthenticate = true;
|
|
|
|
private Element streamFeatures;
|
2014-04-08 17:15:55 -04:00
|
|
|
private HashMap<String, List<String>> disco = new HashMap<String, List<String>>();
|
2014-05-20 04:48:07 -04: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;
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
private int stanzasReceived = 0;
|
|
|
|
private int stanzasSent = 0;
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-11 10:44:22 -04:00
|
|
|
public long lastPaketReceived = 0;
|
|
|
|
public long lastPingSent = 0;
|
2014-03-12 17:31:50 -04:00
|
|
|
public long lastConnect = 0;
|
|
|
|
public long lastSessionStarted = 0;
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-05-18 05:25:04 -04:00
|
|
|
private int attempt = 0;
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-01-30 18:33:01 -05:00
|
|
|
private static final int PACKET_IQ = 0;
|
|
|
|
private static final int PACKET_MESSAGE = 1;
|
|
|
|
private static final int PACKET_PRESENCE = 2;
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
private Hashtable<String, PacketReceived> packetCallbacks = new Hashtable<String, PacketReceived>();
|
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 OnTLSExceptionReceived tlsListener = null;
|
|
|
|
private OnBindListener bindListener = null;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
2014-06-20 11:30:19 -04:00
|
|
|
public XmppConnection(Account account, XmppConnectionService service) {
|
|
|
|
this.mRandom = service.getRNG();
|
2014-01-30 10:42:35 -05:00
|
|
|
this.account = account;
|
2014-06-20 11:30:19 -04:00
|
|
|
this.wakeLock = service.getPowerManager().newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
|
2014-05-21 16:22:36 -04:00
|
|
|
account.getJid());
|
2014-01-30 10:42:35 -05:00
|
|
|
tagWriter = new TagWriter();
|
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
2014-03-05 21:57:29 -05:00
|
|
|
protected void changeStatus(int nextStatus) {
|
2014-03-11 11:27:33 -04:00
|
|
|
if (account.getStatus() != nextStatus) {
|
2014-05-21 16:22:36 -04:00
|
|
|
if ((nextStatus == Account.STATUS_OFFLINE)
|
|
|
|
&& (account.getStatus() != Account.STATUS_CONNECTING)
|
|
|
|
&& (account.getStatus() != Account.STATUS_ONLINE)
|
|
|
|
&& (account.getStatus() != Account.STATUS_DISABLED)) {
|
2014-03-13 12:29:22 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-05-18 05:25:04 -04:00
|
|
|
if (nextStatus == Account.STATUS_ONLINE) {
|
|
|
|
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-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": connecting");
|
2014-03-12 17:31:50 -04:00
|
|
|
lastConnect = SystemClock.elapsedRealtime();
|
2014-05-18 05:25:04 -04:00
|
|
|
this.attempt++;
|
2014-01-30 10:42:35 -05:00
|
|
|
try {
|
2014-05-21 16:22:36 -04: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-03-05 21:57:29 -05:00
|
|
|
this.changeStatus(Account.STATUS_CONNECTING);
|
2014-02-07 00:52:09 -05:00
|
|
|
Bundle namePort = DNSHelper.getSRVRecord(account.getServer());
|
2014-03-28 14:00:01 -04:00
|
|
|
if ("timeout".equals(namePort.getString("error"))) {
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": dns timeout");
|
2014-03-28 14:00:01 -04:00
|
|
|
this.changeStatus(Account.STATUS_OFFLINE);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-07 00:52:09 -05:00
|
|
|
String srvRecordServer = namePort.getString("name");
|
2014-04-03 05:01:20 -04:00
|
|
|
String srvIpServer = namePort.getString("ipv4");
|
2014-02-07 00:52:09 -05:00
|
|
|
int srvRecordPort = namePort.getInt("port");
|
2014-02-09 08:10:52 -05:00
|
|
|
if (srvRecordServer != null) {
|
2014-04-03 05:01:20 -04:00
|
|
|
if (srvIpServer != null) {
|
|
|
|
Log.d(LOGTAG, account.getJid() + ": using values from dns "
|
2014-05-21 16:22:36 -04:00
|
|
|
+ srvRecordServer + "[" + srvIpServer + "]:"
|
|
|
|
+ srvRecordPort);
|
2014-04-03 05:01:20 -04:00
|
|
|
socket = new Socket(srvIpServer, srvRecordPort);
|
|
|
|
} else {
|
|
|
|
Log.d(LOGTAG, account.getJid() + ": using values from dns "
|
2014-05-21 16:22:36 -04:00
|
|
|
+ srvRecordServer + ":" + srvRecordPort);
|
2014-04-03 05:01:20 -04:00
|
|
|
socket = new Socket(srvRecordServer, srvRecordPort);
|
|
|
|
}
|
2014-02-07 00:52:09 -05:00
|
|
|
} else {
|
|
|
|
socket = new Socket(account.getServer(), 5222);
|
|
|
|
}
|
2014-01-30 10:42:35 -05: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 09:09:50 -05:00
|
|
|
break;
|
2014-01-30 10:42:35 -05:00
|
|
|
} else {
|
|
|
|
Log.d(LOGTAG, "found unexpected tag: " + nextTag.getName());
|
2014-01-31 19:25:56 -05:00
|
|
|
return;
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 09:09:50 -05:00
|
|
|
if (socket.isConnected()) {
|
|
|
|
socket.close();
|
|
|
|
}
|
2014-01-31 19:25:56 -05:00
|
|
|
} catch (UnknownHostException e) {
|
2014-03-05 21:57:29 -05:00
|
|
|
this.changeStatus(Account.STATUS_SERVER_NOT_FOUND);
|
2014-03-05 21:30:03 -05:00
|
|
|
if (wakeLock.isHeld()) {
|
2014-06-25 11:06:20 -04:00
|
|
|
try { wakeLock.release();} catch (RuntimeException re) {}
|
2014-03-05 21:30:03 -05:00
|
|
|
}
|
2014-01-31 19:25:56 -05:00
|
|
|
return;
|
2014-01-30 10:42:35 -05:00
|
|
|
} catch (IOException e) {
|
2014-03-07 08:24:33 -05:00
|
|
|
if (account.getStatus() != Account.STATUS_TLS_ERROR) {
|
|
|
|
this.changeStatus(Account.STATUS_OFFLINE);
|
|
|
|
}
|
2014-03-05 21:30:03 -05:00
|
|
|
if (wakeLock.isHeld()) {
|
2014-06-25 11:06:20 -04:00
|
|
|
try { wakeLock.release();} catch (RuntimeException re) {}
|
2014-02-04 09:09:50 -05:00
|
|
|
}
|
2014-03-05 21:30:03 -05:00
|
|
|
return;
|
2014-04-03 12:16:14 -04:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
this.changeStatus(Account.STATUS_OFFLINE);
|
|
|
|
Log.d(LOGTAG, "compression exception " + e.getMessage());
|
|
|
|
if (wakeLock.isHeld()) {
|
2014-06-25 11:06:20 -04:00
|
|
|
try { wakeLock.release();} catch (RuntimeException re) {}
|
2014-04-03 12:16:14 -04:00
|
|
|
}
|
|
|
|
return;
|
2014-01-31 19:25:56 -05:00
|
|
|
} catch (XmlPullParserException e) {
|
2014-03-05 21:57:29 -05:00
|
|
|
this.changeStatus(Account.STATUS_OFFLINE);
|
2014-02-09 08:10:52 -05:00
|
|
|
Log.d(LOGTAG, "xml exception " + e.getMessage());
|
2014-03-05 21:30:03 -05:00
|
|
|
if (wakeLock.isHeld()) {
|
2014-06-25 11:06:20 -04:00
|
|
|
try { wakeLock.release();} catch (RuntimeException re) {}
|
2014-03-05 21:30:03 -05:00
|
|
|
}
|
2014-01-31 19:25:56 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-01-31 19:25:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-05 21:30:03 -05:00
|
|
|
connect();
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void processStream(Tag currentTag) throws XmlPullParserException,
|
2014-04-03 12:16:14 -04:00
|
|
|
IOException, NoSuchAlgorithmException {
|
2014-02-04 09:09:50 -05:00
|
|
|
Tag nextTag = tagReader.readTag();
|
|
|
|
while ((nextTag != null) && (!nextTag.isEnd("stream"))) {
|
2014-01-30 10:42:35 -05:00
|
|
|
if (nextTag.isStart("error")) {
|
|
|
|
processStreamError(nextTag);
|
|
|
|
} else if (nextTag.isStart("features")) {
|
|
|
|
processStreamFeatures(nextTag);
|
2014-03-06 09:11:56 -05:00
|
|
|
if ((streamFeatures.getChildren().size() == 1)
|
|
|
|
&& (streamFeatures.hasChild("starttls"))
|
|
|
|
&& (!account.isOptionSet(Account.OPTION_USETLS))) {
|
|
|
|
changeStatus(Account.STATUS_SERVER_REQUIRES_TLS);
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
} else if (nextTag.isStart("proceed")) {
|
|
|
|
switchOverToTls(nextTag);
|
2014-04-03 12:16:14 -04:00
|
|
|
} else if (nextTag.isStart("compressed")) {
|
|
|
|
switchOverToZLib(nextTag);
|
2014-01-30 10:42:35 -05:00
|
|
|
} else if (nextTag.isStart("success")) {
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": logged in");
|
2014-01-30 10:42:35 -05:00
|
|
|
tagReader.readTag();
|
|
|
|
tagReader.reset();
|
|
|
|
sendStartStream();
|
|
|
|
processStream(tagReader.readTag());
|
2014-02-04 09:09:50 -05:00
|
|
|
break;
|
2014-02-09 08:10:52 -05:00
|
|
|
} else if (nextTag.isStart("failure")) {
|
2014-06-16 06:18:04 -04:00
|
|
|
tagReader.readElement(nextTag);
|
2014-03-06 09:11:56 -05:00
|
|
|
changeStatus(Account.STATUS_UNAUTHORIZED);
|
2014-03-30 15:21:55 -04:00
|
|
|
} else if (nextTag.isStart("challenge")) {
|
|
|
|
String challange = tagReader.readElement(nextTag).getContent();
|
2014-03-31 09:22:27 -04:00
|
|
|
Element response = new Element("response");
|
2014-05-21 16:22:36 -04:00
|
|
|
response.setAttribute("xmlns",
|
|
|
|
"urn:ietf:params:xml:ns:xmpp-sasl");
|
|
|
|
response.setContent(CryptoHelper.saslDigestMd5(account,
|
2014-06-20 11:30:19 -04:00
|
|
|
challange,mRandom));
|
2014-03-31 09:22:27 -04:00
|
|
|
tagWriter.writeElement(response);
|
2014-03-10 14:22:13 -04:00
|
|
|
} else if (nextTag.isStart("enabled")) {
|
|
|
|
this.stanzasSent = 0;
|
|
|
|
Element enabled = tagReader.readElement(nextTag);
|
|
|
|
if ("true".equals(enabled.getAttribute("resume"))) {
|
|
|
|
this.streamId = enabled.getAttribute("id");
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": stream managment("
|
|
|
|
+ smVersion + ") enabled (resumable)");
|
2014-03-10 14:22:13 -04:00
|
|
|
} else {
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": stream managment("
|
|
|
|
+ smVersion + ") enabled");
|
2014-03-10 14:22:13 -04:00
|
|
|
}
|
2014-03-12 17:31:50 -04:00
|
|
|
this.lastSessionStarted = SystemClock.elapsedRealtime();
|
2014-03-10 14:22:13 -04:00
|
|
|
this.stanzasReceived = 0;
|
2014-04-05 06:08:35 -04:00
|
|
|
RequestPacket r = new RequestPacket(smVersion);
|
2014-03-10 14:22:13 -04:00
|
|
|
tagWriter.writeStanzaAsync(r);
|
|
|
|
} else if (nextTag.isStart("resumed")) {
|
2014-05-20 05:05:03 -04:00
|
|
|
lastPaketReceived = SystemClock.elapsedRealtime();
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": session resumed");
|
2014-03-10 14:22:13 -04:00
|
|
|
tagReader.readElement(nextTag);
|
2014-03-15 10:13:35 -04:00
|
|
|
sendPing();
|
2014-03-10 14:22:13 -04:00
|
|
|
changeStatus(Account.STATUS_ONLINE);
|
|
|
|
} else if (nextTag.isStart("r")) {
|
|
|
|
tagReader.readElement(nextTag);
|
2014-05-21 16:22:36 -04:00
|
|
|
AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
|
2014-03-10 14:22:13 -04:00
|
|
|
tagWriter.writeStanzaAsync(ack);
|
|
|
|
} else if (nextTag.isStart("a")) {
|
|
|
|
Element ack = tagReader.readElement(nextTag);
|
2014-03-11 10:44:22 -04:00
|
|
|
lastPaketReceived = SystemClock.elapsedRealtime();
|
2014-03-10 14:22:13 -04:00
|
|
|
int serverSequence = Integer.parseInt(ack.getAttribute("h"));
|
2014-05-21 16:22:36 -04:00
|
|
|
if (serverSequence > this.stanzasSent) {
|
2014-03-10 14:22:13 -04:00
|
|
|
this.stanzasSent = serverSequence;
|
|
|
|
}
|
2014-03-11 16:56:37 -04:00
|
|
|
} else if (nextTag.isStart("failed")) {
|
2014-03-12 09:55:23 -04:00
|
|
|
tagReader.readElement(nextTag);
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": resumption failed");
|
2014-03-11 16:56:37 -04:00
|
|
|
streamId = null;
|
|
|
|
if (account.getStatus() != Account.STATUS_ONLINE) {
|
|
|
|
sendBindRequest();
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
} else if (nextTag.isStart("iq")) {
|
2014-02-01 09:07:20 -05:00
|
|
|
processIq(nextTag);
|
2014-01-30 18:33:01 -05:00
|
|
|
} else if (nextTag.isStart("message")) {
|
2014-02-01 09:07:20 -05:00
|
|
|
processMessage(nextTag);
|
2014-01-30 18:33:01 -05:00
|
|
|
} else if (nextTag.isStart("presence")) {
|
2014-02-01 09:07:20 -05:00
|
|
|
processPresence(nextTag);
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
2014-02-04 09:09:50 -05:00
|
|
|
nextTag = tagReader.readTag();
|
|
|
|
}
|
|
|
|
if (account.getStatus() == Account.STATUS_ONLINE) {
|
|
|
|
account.setStatus(Account.STATUS_OFFLINE);
|
2014-02-09 08:10:52 -05:00
|
|
|
if (statusListener != null) {
|
2014-02-04 09:09:50 -05:00
|
|
|
statusListener.onStatusChanged(account);
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
private Element processPacket(Tag currentTag, int packetType)
|
|
|
|
throws XmlPullParserException, IOException {
|
2014-01-30 18:33:01 -05:00
|
|
|
Element element;
|
|
|
|
switch (packetType) {
|
|
|
|
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-05-23 04:15:58 -04:00
|
|
|
if (nextTag==null) {
|
|
|
|
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()) {
|
|
|
|
Element child = tagReader.readElement(nextTag);
|
2014-05-21 16:22:36 -04:00
|
|
|
if ((packetType == PACKET_IQ)
|
|
|
|
&& ("jingle".equals(child.getName()))) {
|
2014-03-26 21:02:59 -04:00
|
|
|
element = new JinglePacket();
|
|
|
|
element.setAttributes(currentTag.getAttributes());
|
|
|
|
}
|
2014-01-30 18:33:01 -05:00
|
|
|
element.addChild(child);
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
nextTag = tagReader.readTag();
|
2014-05-23 04:15:58 -04:00
|
|
|
if (nextTag==null) {
|
|
|
|
throw new IOException("interrupted mid tag");
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
2014-03-10 14:22:13 -04:00
|
|
|
++stanzasReceived;
|
2014-03-11 10:44:22 -04:00
|
|
|
lastPaketReceived = SystemClock.elapsedRealtime();
|
2014-01-30 18:33:01 -05:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
private void processIq(Tag currentTag) throws XmlPullParserException,
|
2014-02-09 08:10:52 -05:00
|
|
|
IOException {
|
|
|
|
IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-04-03 09:24:11 -04:00
|
|
|
if (packet.getId() == null) {
|
2014-05-21 16:22:36 -04:00
|
|
|
return; // an iq packet without id is definitely invalid
|
2014-04-03 09:24:11 -04:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-26 21:02:59 -04:00
|
|
|
if (packet instanceof JinglePacket) {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (this.jingleListener != null) {
|
|
|
|
this.jingleListener.onJinglePacketReceived(account,
|
|
|
|
(JinglePacket) packet);
|
2014-03-26 21:02:59 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (packetCallbacks.containsKey(packet.getId())) {
|
|
|
|
if (packetCallbacks.get(packet.getId()) instanceof OnIqPacketReceived) {
|
|
|
|
((OnIqPacketReceived) packetCallbacks.get(packet.getId()))
|
|
|
|
.onIqPacketReceived(account, packet);
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-26 21:02:59 -04:00
|
|
|
packetCallbacks.remove(packet.getId());
|
|
|
|
} else if (this.unregisteredIqListener != null) {
|
|
|
|
this.unregisteredIqListener.onIqPacketReceived(account, packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-01-31 19:25:56 -05:00
|
|
|
}
|
2014-01-30 18:33:01 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
private void processMessage(Tag currentTag) throws XmlPullParserException,
|
|
|
|
IOException {
|
|
|
|
MessagePacket packet = (MessagePacket) processPacket(currentTag,
|
|
|
|
PACKET_MESSAGE);
|
2014-03-02 23:01:02 -05:00
|
|
|
String id = packet.getAttribute("id");
|
2014-03-06 09:11:56 -05:00
|
|
|
if ((id != null) && (packetCallbacks.containsKey(id))) {
|
2014-03-02 23:01:02 -05:00
|
|
|
if (packetCallbacks.get(id) instanceof OnMessagePacketReceived) {
|
2014-03-06 09:11:56 -05:00
|
|
|
((OnMessagePacketReceived) packetCallbacks.get(id))
|
|
|
|
.onMessagePacketReceived(account, packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
packetCallbacks.remove(id);
|
|
|
|
} else if (this.messageListener != null) {
|
2014-02-09 08:10:52 -05:00
|
|
|
this.messageListener.onMessagePacketReceived(account, packet);
|
2014-02-01 09:07:20 -05:00
|
|
|
}
|
2014-01-30 18:33:01 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
private void processPresence(Tag currentTag) throws XmlPullParserException,
|
|
|
|
IOException {
|
|
|
|
PresencePacket packet = (PresencePacket) processPacket(currentTag,
|
|
|
|
PACKET_PRESENCE);
|
2014-03-02 23:01:02 -05:00
|
|
|
String id = packet.getAttribute("id");
|
2014-03-06 09:11:56 -05:00
|
|
|
if ((id != null) && (packetCallbacks.containsKey(id))) {
|
2014-03-02 23:01:02 -05:00
|
|
|
if (packetCallbacks.get(id) instanceof OnPresencePacketReceived) {
|
2014-03-06 09:11:56 -05:00
|
|
|
((OnPresencePacketReceived) packetCallbacks.get(id))
|
|
|
|
.onPresencePacketReceived(account, packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
packetCallbacks.remove(id);
|
|
|
|
} else if (this.presenceListener != null) {
|
2014-02-09 08:10:52 -05:00
|
|
|
this.presenceListener.onPresencePacketReceived(account, packet);
|
2014-02-01 09:07:20 -05:00
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
2014-04-03 12:16:14 -04:00
|
|
|
private void sendCompressionZlib() throws IOException {
|
2014-04-05 03:45:15 -04: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 12:16:14 -04:00
|
|
|
}
|
|
|
|
|
2014-05-21 16:22:36 -04:00
|
|
|
private void switchOverToZLib(Tag currentTag)
|
|
|
|
throws XmlPullParserException, IOException,
|
|
|
|
NoSuchAlgorithmException {
|
2014-04-03 12:16:14 -04:00
|
|
|
tagReader.readTag(); // read tag close
|
|
|
|
|
2014-05-21 16:22:36 -04:00
|
|
|
tagWriter.setOutputStream(new ZLibOutputStream(tagWriter
|
|
|
|
.getOutputStream()));
|
|
|
|
tagReader
|
|
|
|
.setInputStream(new ZLibInputStream(tagReader.getInputStream()));
|
2014-04-03 12:16:14 -04:00
|
|
|
|
|
|
|
sendStartStream();
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": compression enabled");
|
2014-04-03 12:16:14 -04:00
|
|
|
processStream(tagReader.readTag());
|
|
|
|
}
|
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
private void sendStartTLS() throws IOException {
|
2014-01-30 10:42:35 -05:00
|
|
|
Tag startTLS = Tag.empty("starttls");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private void switchOverToTls(Tag currentTag) throws XmlPullParserException,
|
|
|
|
IOException {
|
2014-06-16 06:18:04 -04:00
|
|
|
tagReader.readTag();
|
2014-01-30 10:42:35 -05:00
|
|
|
try {
|
2014-03-06 14:03:35 -05:00
|
|
|
SSLContext sc = SSLContext.getInstance("TLS");
|
|
|
|
TrustManagerFactory tmf = TrustManagerFactory
|
|
|
|
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
|
|
|
try {
|
|
|
|
tmf.init((KeyStore) null);
|
|
|
|
} catch (KeyStoreException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
TrustManager[] trustManagers = tmf.getTrustManagers();
|
|
|
|
final X509TrustManager origTrustmanager = (X509TrustManager) trustManagers[0];
|
|
|
|
|
|
|
|
TrustManager[] wrappedTrustManagers = new TrustManager[] { new X509TrustManager() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void checkClientTrusted(X509Certificate[] chain,
|
|
|
|
String authType) throws CertificateException {
|
|
|
|
origTrustmanager.checkClientTrusted(chain, authType);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain,
|
|
|
|
String authType) throws CertificateException {
|
|
|
|
try {
|
|
|
|
origTrustmanager.checkServerTrusted(chain, authType);
|
|
|
|
} catch (CertificateException e) {
|
2014-03-07 08:24:33 -05:00
|
|
|
if (e.getCause() instanceof CertPathValidatorException) {
|
|
|
|
String sha;
|
|
|
|
try {
|
2014-05-21 16:22:36 -04:00
|
|
|
MessageDigest sha1 = MessageDigest
|
|
|
|
.getInstance("SHA1");
|
2014-03-07 08:24:33 -05:00
|
|
|
sha1.update(chain[0].getEncoded());
|
|
|
|
sha = CryptoHelper.bytesToHex(sha1.digest());
|
|
|
|
if (!sha.equals(account.getSSLFingerprint())) {
|
|
|
|
changeStatus(Account.STATUS_TLS_ERROR);
|
2014-05-21 16:22:36 -04:00
|
|
|
if (tlsListener != null) {
|
|
|
|
tlsListener.onTLSExceptionReceived(sha,
|
|
|
|
account);
|
2014-03-07 08:24:33 -05:00
|
|
|
}
|
|
|
|
throw new CertificateException();
|
|
|
|
}
|
|
|
|
} catch (NoSuchAlgorithmException e1) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new CertificateException();
|
|
|
|
}
|
2014-03-06 14:03:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
return origTrustmanager.getAcceptedIssuers();
|
|
|
|
}
|
|
|
|
|
|
|
|
} };
|
|
|
|
sc.init(null, wrappedTrustManagers, null);
|
|
|
|
SSLSocketFactory factory = sc.getSocketFactory();
|
|
|
|
SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,
|
2014-05-21 16:22:36 -04:00
|
|
|
socket.getInetAddress().getHostAddress(), socket.getPort(),
|
|
|
|
true);
|
2014-01-30 10:42:35 -05:00
|
|
|
tagReader.setInputStream(sslSocket.getInputStream());
|
|
|
|
tagWriter.setOutputStream(sslSocket.getOutputStream());
|
|
|
|
sendStartStream();
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": TLS connection established");
|
2014-01-30 10:42:35 -05:00
|
|
|
processStream(tagReader.readTag());
|
2014-02-04 09:09:50 -05:00
|
|
|
sslSocket.close();
|
2014-03-06 14:03:35 -05:00
|
|
|
} catch (NoSuchAlgorithmException e1) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-30 15:21:55 -04:00
|
|
|
private void sendSaslAuthPlain() throws IOException {
|
2014-03-09 07:58:29 -04:00
|
|
|
String saslString = CryptoHelper.saslPlain(account.getUsername(),
|
2014-01-30 10:42:35 -05:00
|
|
|
account.getPassword());
|
|
|
|
Element auth = new Element("auth");
|
|
|
|
auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
|
|
|
|
auth.setAttribute("mechanism", "PLAIN");
|
|
|
|
auth.setContent(saslString);
|
|
|
|
tagWriter.writeElement(auth);
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-30 15:21:55 -04:00
|
|
|
private void sendSaslAuthDigestMd5() throws IOException {
|
|
|
|
Element auth = new Element("auth");
|
|
|
|
auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
|
|
|
|
auth.setAttribute("mechanism", "DIGEST-MD5");
|
|
|
|
tagWriter.writeElement(auth);
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
|
|
|
|
private void processStreamFeatures(Tag currentTag)
|
|
|
|
throws XmlPullParserException, IOException {
|
2014-01-31 19:25:56 -05:00
|
|
|
this.streamFeatures = tagReader.readElement(currentTag);
|
2014-02-09 08:10:52 -05:00
|
|
|
if (this.streamFeatures.hasChild("starttls")
|
|
|
|
&& account.isOptionSet(Account.OPTION_USETLS)) {
|
2014-01-31 19:25:56 -05:00
|
|
|
sendStartTLS();
|
2014-04-03 12:16:14 -04:00
|
|
|
} else if (compressionAvailable()) {
|
|
|
|
sendCompressionZlib();
|
2014-05-21 16:22:36 -04:00
|
|
|
} else if (this.streamFeatures.hasChild("register")
|
|
|
|
&& (account.isOptionSet(Account.OPTION_REGISTER))) {
|
|
|
|
sendRegistryRequest();
|
|
|
|
} else if (!this.streamFeatures.hasChild("register")
|
|
|
|
&& (account.isOptionSet(Account.OPTION_REGISTER))) {
|
2014-03-14 13:56:52 -04:00
|
|
|
changeStatus(Account.STATUS_REGISTRATION_NOT_SUPPORTED);
|
|
|
|
disconnect(true);
|
2014-02-09 08:10:52 -05:00
|
|
|
} else if (this.streamFeatures.hasChild("mechanisms")
|
|
|
|
&& shouldAuthenticate) {
|
2014-05-21 16:22:36 -04:00
|
|
|
List<String> mechanisms = extractMechanisms(streamFeatures
|
|
|
|
.findChild("mechanisms"));
|
2014-03-30 15:21:55 -04:00
|
|
|
if (mechanisms.contains("PLAIN")) {
|
|
|
|
sendSaslAuthPlain();
|
|
|
|
} else if (mechanisms.contains("DIGEST-MD5")) {
|
|
|
|
sendSaslAuthDigestMd5();
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
} else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:"
|
|
|
|
+ smVersion)
|
|
|
|
&& streamId != null) {
|
|
|
|
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-03-10 14:22:13 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-04-03 12:16:14 -04:00
|
|
|
private boolean compressionAvailable() {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (!this.streamFeatures.hasChild("compression",
|
|
|
|
"http://jabber.org/features/compress"))
|
|
|
|
return false;
|
|
|
|
if (!ZLibOutputStream.SUPPORTED)
|
|
|
|
return false;
|
|
|
|
if (!account.isOptionSet(Account.OPTION_USECOMPRESSION))
|
|
|
|
return false;
|
2014-04-03 12:16:14 -04:00
|
|
|
|
2014-05-21 16:22:36 -04:00
|
|
|
Element compression = this.streamFeatures.findChild("compression",
|
|
|
|
"http://jabber.org/features/compress");
|
2014-04-03 12:16:14 -04:00
|
|
|
for (Element child : compression.getChildren()) {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (!"method".equals(child.getName()))
|
|
|
|
continue;
|
2014-04-03 12:16:14 -04:00
|
|
|
|
|
|
|
if ("zlib".equalsIgnoreCase(child.getContent())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-30 15:21:55 -04:00
|
|
|
private List<String> extractMechanisms(Element stream) {
|
2014-05-21 16:22:36 -04:00
|
|
|
ArrayList<String> mechanisms = new ArrayList<String>(stream
|
|
|
|
.getChildren().size());
|
|
|
|
for (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() {
|
|
|
|
IqPacket register = new IqPacket(IqPacket.TYPE_GET);
|
|
|
|
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
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
|
|
|
Element instructions = packet.query().findChild("instructions");
|
2014-05-21 16:22:36 -04:00
|
|
|
if (packet.query().hasChild("username")
|
|
|
|
&& (packet.query().hasChild("password"))) {
|
2014-03-14 13:56:52 -04:00
|
|
|
IqPacket register = new IqPacket(IqPacket.TYPE_SET);
|
2014-05-21 16:22:36 -04:00
|
|
|
Element username = new Element("username")
|
|
|
|
.setContent(account.getUsername());
|
|
|
|
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-05-21 16:22:36 -04:00
|
|
|
public void onIqPacketReceived(Account account,
|
|
|
|
IqPacket packet) {
|
|
|
|
if (packet.getType() == IqPacket.TYPE_RESULT) {
|
|
|
|
account.setOption(Account.OPTION_REGISTER,
|
|
|
|
false);
|
2014-03-14 13:56:52 -04:00
|
|
|
changeStatus(Account.STATUS_REGISTRATION_SUCCESSFULL);
|
2014-05-21 16:22:36 -04:00
|
|
|
} else if (packet.hasChild("error")
|
|
|
|
&& (packet.findChild("error")
|
|
|
|
.hasChild("conflict"))) {
|
2014-03-14 13:56:52 -04:00
|
|
|
changeStatus(Account.STATUS_REGISTRATION_CONFLICT);
|
|
|
|
} else {
|
|
|
|
changeStatus(Account.STATUS_REGISTRATION_FAILED);
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, packet.toString());
|
2014-03-14 13:56:52 -04:00
|
|
|
}
|
|
|
|
disconnect(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
changeStatus(Account.STATUS_REGISTRATION_FAILED);
|
|
|
|
disconnect(true);
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid()
|
|
|
|
+ ": could not register. instructions are"
|
|
|
|
+ instructions.getContent());
|
2014-03-14 13:56:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-30 10:42:35 -05:00
|
|
|
private void sendBindRequest() throws IOException {
|
2014-01-31 19:25:56 -05:00
|
|
|
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")
|
|
|
|
.addChild("resource").setContent(account.getResource());
|
2014-05-18 11:32:20 -04:00
|
|
|
this.sendUnboundIqPacket(iq, new OnIqPacketReceived() {
|
2014-01-31 19:25:56 -05:00
|
|
|
@Override
|
2014-02-01 09:07:20 -05:00
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2014-02-09 08:10:52 -05:00
|
|
|
String resource = packet.findChild("bind").findChild("jid")
|
|
|
|
.getContent().split("/")[1];
|
2014-02-04 09:09:50 -05:00
|
|
|
account.setResource(resource);
|
2014-05-21 16:22:36 -04:00
|
|
|
if (streamFeatures.hasChild("sm", "urn:xmpp:sm:3")) {
|
2014-04-05 06:08:35 -04:00
|
|
|
smVersion = 3;
|
|
|
|
EnablePacket enable = new EnablePacket(smVersion);
|
|
|
|
tagWriter.writeStanzaAsync(enable);
|
2014-05-21 16:22:36 -04:00
|
|
|
} else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) {
|
2014-04-05 06:08:35 -04:00
|
|
|
smVersion = 2;
|
|
|
|
EnablePacket enable = new EnablePacket(smVersion);
|
2014-03-10 14:22:13 -04:00
|
|
|
tagWriter.writeStanzaAsync(enable);
|
|
|
|
}
|
2014-04-08 17:15:55 -04:00
|
|
|
sendServiceDiscoveryInfo(account.getServer());
|
|
|
|
sendServiceDiscoveryItems(account.getServer());
|
2014-05-21 16:22:36 -04:00
|
|
|
if (bindListener != null) {
|
2014-03-15 10:13:35 -04:00
|
|
|
bindListener.onBind(account);
|
2014-02-04 09:09:50 -05:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-19 11:16:40 -04:00
|
|
|
changeStatus(Account.STATUS_ONLINE);
|
2014-02-09 08:10:52 -05:00
|
|
|
}
|
|
|
|
});
|
2014-05-17 15:57:44 -04:00
|
|
|
if (this.streamFeatures.hasChild("session")) {
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, account.getJid() + ": sending deprecated session");
|
2014-05-17 15:57:44 -04:00
|
|
|
IqPacket startSession = new IqPacket(IqPacket.TYPE_SET);
|
2014-05-21 16:22:36 -04:00
|
|
|
startSession.addChild("session",
|
|
|
|
"urn:ietf:params:xml:ns:xmpp-session");
|
2014-05-18 11:32:20 -04:00
|
|
|
this.sendUnboundIqPacket(startSession, null);
|
2014-05-17 15:57:44 -04:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
}
|
|
|
|
|
2014-04-08 17:15:55 -04:00
|
|
|
private void sendServiceDiscoveryInfo(final String server) {
|
2014-02-09 08:10:52 -05:00
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
2014-04-08 17:15:55 -04:00
|
|
|
iq.setTo(server);
|
2014-03-14 23:59:18 -04:00
|
|
|
iq.query("http://jabber.org/protocol/disco#info");
|
2014-02-09 08:10:52 -05:00
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2014-04-08 17:15:55 -04:00
|
|
|
List<Element> elements = packet.query().getChildren();
|
|
|
|
List<String> features = new ArrayList<String>();
|
|
|
|
for (int i = 0; i < elements.size(); ++i) {
|
|
|
|
if (elements.get(i).getName().equals("feature")) {
|
2014-05-21 16:22:36 -04:00
|
|
|
features.add(elements.get(i).getAttribute("var"));
|
2014-02-09 08:10:52 -05:00
|
|
|
}
|
2014-04-08 17:15:55 -04:00
|
|
|
}
|
|
|
|
disco.put(server, features);
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-04-08 17:15:55 -04:00
|
|
|
if (account.getServer().equals(server)) {
|
|
|
|
enableAdvancedStreamFeatures();
|
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() {
|
|
|
|
if (hasFeaturesCarbon()) {
|
|
|
|
sendEnableCarbons();
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-04-08 17:15:55 -04:00
|
|
|
private void sendServiceDiscoveryItems(final String server) {
|
2014-03-14 23:59:18 -04:00
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
2014-04-08 17:15:55 -04:00
|
|
|
iq.setTo(server);
|
2014-03-14 23:59:18 -04: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 17:15:55 -04:00
|
|
|
List<Element> elements = packet.query().getChildren();
|
|
|
|
for (int i = 0; i < elements.size(); ++i) {
|
|
|
|
if (elements.get(i).getName().equals("item")) {
|
2014-05-21 16:22:36 -04:00
|
|
|
String jid = elements.get(i).getAttribute("jid");
|
2014-04-08 17:15:55 -04:00
|
|
|
sendServiceDiscoveryInfo(jid);
|
2014-03-14 23:59:18 -04:00
|
|
|
}
|
2014-04-08 17:15:55 -04: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() {
|
|
|
|
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
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
|
|
|
if (!packet.hasChild("error")) {
|
2014-03-06 09:11:56 -05:00
|
|
|
Log.d(LOGTAG, account.getJid()
|
|
|
|
+ ": successfully enabled carbons");
|
2014-02-09 08:10:52 -05:00
|
|
|
} else {
|
2014-03-06 09:11:56 -05:00
|
|
|
Log.d(LOGTAG, account.getJid()
|
|
|
|
+ ": 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
|
|
|
}
|
|
|
|
|
|
|
|
private void processStreamError(Tag currentTag) {
|
|
|
|
Log.d(LOGTAG, "processStreamError");
|
|
|
|
}
|
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
private void sendStartStream() throws IOException {
|
2014-02-07 10:50:29 -05:00
|
|
|
Tag stream = Tag.start("stream:stream");
|
2014-01-30 10:42:35 -05:00
|
|
|
stream.setAttribute("from", account.getJid());
|
|
|
|
stream.setAttribute("to", account.getServer());
|
|
|
|
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-06-20 11:30:19 -04:00
|
|
|
return new BigInteger(50, mRandom).toString(32);
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-02-04 09:09:50 -05:00
|
|
|
public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (packet.getId() == null) {
|
2014-04-11 15:13:09 -04:00
|
|
|
String id = nextRandomId();
|
|
|
|
packet.setAttribute("id", id);
|
|
|
|
}
|
2014-04-03 09:08:53 -04:00
|
|
|
packet.setFrom(account.getFullJid());
|
2014-03-10 14:22:13 -04:00
|
|
|
this.sendPacket(packet, callback);
|
2014-01-31 19:25:56 -05:00
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-05-18 11:32:20 -04:00
|
|
|
public void sendUnboundIqPacket(IqPacket packet, OnIqPacketReceived callback) {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (packet.getId() == null) {
|
2014-05-18 11:32:20 -04:00
|
|
|
String id = nextRandomId();
|
|
|
|
packet.setAttribute("id", id);
|
|
|
|
}
|
|
|
|
this.sendPacket(packet, callback);
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void sendMessagePacket(MessagePacket packet) {
|
2014-03-10 14:22:13 -04:00
|
|
|
this.sendPacket(packet, null);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
|
|
|
public void sendMessagePacket(MessagePacket packet,
|
|
|
|
OnMessagePacketReceived callback) {
|
2014-03-10 14:22:13 -04:00
|
|
|
this.sendPacket(packet, callback);
|
2014-02-01 09:07:20 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void sendPresencePacket(PresencePacket packet) {
|
2014-03-10 14:22:13 -04:00
|
|
|
this.sendPacket(packet, null);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
public void sendPresencePacket(PresencePacket packet,
|
2014-03-06 09:11:56 -05:00
|
|
|
OnPresencePacketReceived callback) {
|
2014-03-10 14:22:13 -04:00
|
|
|
this.sendPacket(packet, callback);
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
|
|
|
private synchronized void sendPacket(final AbstractStanza packet,
|
|
|
|
PacketReceived callback) {
|
2014-03-11 10:44:22 -04:00
|
|
|
// TODO dont increment stanza count if packet = request packet or ack;
|
2014-03-10 14:22:13 -04:00
|
|
|
++stanzasSent;
|
|
|
|
tagWriter.writeStanzaAsync(packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
if (callback != null) {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (packet.getId() == null) {
|
2014-03-10 14:22:13 -04:00
|
|
|
packet.setId(nextRandomId());
|
|
|
|
}
|
|
|
|
packetCallbacks.put(packet.getId(), callback);
|
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 {
|
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
2014-03-20 10:49:53 -04:00
|
|
|
iq.setFrom(account.getFullJid());
|
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-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void setOnMessagePacketReceivedListener(
|
|
|
|
OnMessagePacketReceived listener) {
|
2014-02-01 09:07:20 -05:00
|
|
|
this.messageListener = listener;
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void setOnUnregisteredIqPacketReceivedListener(
|
|
|
|
OnIqPacketReceived listener) {
|
2014-02-01 09:07:20 -05:00
|
|
|
this.unregisteredIqListener = listener;
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void setOnPresencePacketReceivedListener(
|
|
|
|
OnPresencePacketReceived listener) {
|
2014-02-01 09:07:20 -05:00
|
|
|
this.presenceListener = listener;
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
|
|
|
public void setOnJinglePacketReceivedListener(
|
|
|
|
OnJinglePacketReceived listener) {
|
2014-03-26 21:02:59 -04:00
|
|
|
this.jingleListener = listener;
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-02-04 09:09:50 -05:00
|
|
|
public void setOnStatusChangedListener(OnStatusChanged listener) {
|
|
|
|
this.statusListener = listener;
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
|
|
|
public void setOnTLSExceptionReceivedListener(
|
|
|
|
OnTLSExceptionReceived listener) {
|
2014-03-07 08:24:33 -05:00
|
|
|
this.tlsListener = listener;
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-14 15:43:54 -04:00
|
|
|
public void setOnBindListener(OnBindListener listener) {
|
|
|
|
this.bindListener = listener;
|
|
|
|
}
|
2014-02-04 09:09:50 -05:00
|
|
|
|
2014-03-10 14:22:13 -04:00
|
|
|
public void disconnect(boolean force) {
|
2014-03-13 12:29:22 -04:00
|
|
|
changeStatus(Account.STATUS_OFFLINE);
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(LOGTAG, "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 {
|
|
|
|
while (!tagWriter.finished()) {
|
|
|
|
Log.d(LOGTAG, "not yet finished");
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
|
|
|
tagWriter.writeTag(Tag.end("stream:stream"));
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.d(LOGTAG, "io exception during disconnect");
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.d(LOGTAG, "interrupted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 04:37:49 -04:00
|
|
|
}).start();
|
2014-03-10 14:22:13 -04:00
|
|
|
} catch (IOException e) {
|
2014-05-21 16:22:36 -04:00
|
|
|
Log.d(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
|
|
|
|
2014-03-07 20:06:00 -05:00
|
|
|
public boolean hasFeatureRosterManagment() {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (this.streamFeatures == null) {
|
2014-03-07 20:06:00 -05:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return this.streamFeatures.hasChild("ver");
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-13 12:29:22 -04:00
|
|
|
public boolean hasFeatureStreamManagment() {
|
2014-05-21 16:22:36 -04:00
|
|
|
if (this.streamFeatures == null) {
|
2014-03-13 12:29:22 -04:00
|
|
|
return false;
|
|
|
|
} else {
|
2014-03-13 16:37:27 -04:00
|
|
|
return this.streamFeatures.hasChild("sm");
|
2014-03-13 12:29:22 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-13 12:29:22 -04:00
|
|
|
public boolean hasFeaturesCarbon() {
|
2014-04-08 17:15:55 -04:00
|
|
|
return hasDiscoFeature(account.getServer(), "urn:xmpp:carbons:2");
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-04-08 17:15:55 -04:00
|
|
|
public boolean hasDiscoFeature(String server, String feature) {
|
|
|
|
if (!disco.containsKey(server)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return disco.get(server).contains(feature);
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-06-30 05:43:00 -04:00
|
|
|
public List<String> findDiscoItemsByFeature(String feature) {
|
|
|
|
List<String> items = new ArrayList<String>();
|
2014-07-11 13:48:02 -04:00
|
|
|
for (Entry<String, List<String>> cursor : disco.entrySet()) {
|
|
|
|
if (cursor.getValue().contains(feature)) {
|
|
|
|
items.add(cursor.getKey());
|
2014-05-21 16:22:36 -04:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 05:43:00 -04:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String findDiscoItemByFeature(String feature) {
|
|
|
|
List<String> items = findDiscoItemsByFeature(feature);
|
|
|
|
if (items.size()>=1) {
|
|
|
|
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-03-12 17:31:50 -04:00
|
|
|
|
|
|
|
public int getReceivedStanzas() {
|
|
|
|
return this.stanzasReceived;
|
|
|
|
}
|
2014-05-21 16:22:36 -04:00
|
|
|
|
2014-03-12 17:31:50 -04:00
|
|
|
public int getSentStanzas() {
|
|
|
|
return this.stanzasSent;
|
|
|
|
}
|
2014-03-14 23:59:18 -04:00
|
|
|
|
|
|
|
public String getMucServer() {
|
2014-04-08 17:15:55 -04:00
|
|
|
return findDiscoItemByFeature("http://jabber.org/protocol/muc");
|
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-05-21 16:22:36 -04:00
|
|
|
int interval = (int) (25 * Math.pow(1.5, attempt));
|
2014-05-18 05:25:04 -04:00
|
|
|
int secondsSinceLast = (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
|
|
|
|
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-01-30 10:42:35 -05:00
|
|
|
}
|