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-02-09 08:10:52 -05:00
|
|
|
import java.util.HashSet;
|
2014-01-31 19:25:56 -05:00
|
|
|
import java.util.Hashtable;
|
2014-02-09 08:10:52 -05:00
|
|
|
import java.util.List;
|
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;
|
|
|
|
import android.util.Log;
|
2014-02-28 12:46:01 -05:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
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;
|
|
|
|
import eu.siacs.conversations.utils.SASL;
|
|
|
|
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-01-30 10:42:35 -05:00
|
|
|
|
|
|
|
public class XmppConnection implements Runnable {
|
|
|
|
|
|
|
|
protected Account account;
|
|
|
|
private static final String LOGTAG = "xmppService";
|
|
|
|
|
|
|
|
private PowerManager.WakeLock wakeLock;
|
|
|
|
|
|
|
|
private SecureRandom random = new SecureRandom();
|
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-02-09 08:10:52 -05:00
|
|
|
private HashSet<String> discoFeatures = new HashSet<String>();
|
|
|
|
|
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;
|
|
|
|
private OnIqPacketReceived unregisteredIqListener = null;
|
|
|
|
private OnMessagePacketReceived messageListener = null;
|
2014-02-04 09:09:50 -05:00
|
|
|
private OnStatusChanged statusListener = null;
|
2014-03-07 08:24:33 -05:00
|
|
|
private OnTLSExceptionReceived tlsListener;
|
2014-01-30 10:42:35 -05:00
|
|
|
|
|
|
|
public XmppConnection(Account account, PowerManager pm) {
|
|
|
|
this.account = account;
|
|
|
|
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
|
|
|
|
"XmppConnection");
|
|
|
|
tagReader = new XmlReader(wakeLock);
|
|
|
|
tagWriter = new TagWriter();
|
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
2014-03-05 21:57:29 -05:00
|
|
|
protected void changeStatus(int nextStatus) {
|
|
|
|
account.setStatus(nextStatus);
|
|
|
|
if (statusListener != null) {
|
|
|
|
statusListener.onStatusChanged(account);
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
|
|
|
|
protected void connect() {
|
2014-03-06 09:11:56 -05:00
|
|
|
Log.d(LOGTAG, "connecting");
|
2014-01-30 10:42:35 -05:00
|
|
|
try {
|
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());
|
|
|
|
String srvRecordServer = namePort.getString("name");
|
|
|
|
int srvRecordPort = namePort.getInt("port");
|
2014-02-09 08:10:52 -05:00
|
|
|
if (srvRecordServer != null) {
|
|
|
|
Log.d(LOGTAG, account.getJid() + ": using values from dns "
|
|
|
|
+ srvRecordServer + ":" + srvRecordPort);
|
|
|
|
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()) {
|
|
|
|
wakeLock.release();
|
|
|
|
}
|
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()) {
|
|
|
|
wakeLock.release();
|
2014-02-04 09:09:50 -05:00
|
|
|
}
|
2014-03-05 21:30:03 -05: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()) {
|
|
|
|
wakeLock.release();
|
|
|
|
}
|
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-02-09 08:10:52 -05:00
|
|
|
Log.d(LOGTAG, "end run");
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void processStream(Tag currentTag) throws XmlPullParserException,
|
|
|
|
IOException {
|
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);
|
|
|
|
} else if (nextTag.isStart("success")) {
|
2014-02-09 08:10:52 -05:00
|
|
|
Log.d(LOGTAG, account.getJid()
|
2014-03-07 20:06:00 -05:00
|
|
|
+ ": 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-02-04 09:09:50 -05:00
|
|
|
Element failure = tagReader.readElement(nextTag);
|
2014-03-06 09:11:56 -05:00
|
|
|
changeStatus(Account.STATUS_UNAUTHORIZED);
|
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
|
|
|
} else {
|
|
|
|
Log.d(LOGTAG, "found unexpected tag: " + nextTag.getName()
|
|
|
|
+ " as child of " + currentTag.getName());
|
|
|
|
}
|
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-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);
|
|
|
|
element.addChild(child);
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
nextTag = tagReader.readTag();
|
|
|
|
}
|
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-03-02 23:01:02 -05:00
|
|
|
if (packetCallbacks.containsKey(packet.getId())) {
|
|
|
|
if (packetCallbacks.get(packet.getId()) instanceof OnIqPacketReceived) {
|
2014-03-06 09:11:56 -05:00
|
|
|
((OnIqPacketReceived) packetCallbacks.get(packet.getId()))
|
|
|
|
.onIqPacketReceived(account, packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
2014-03-02 23:01:02 -05:00
|
|
|
packetCallbacks.remove(packet.getId());
|
2014-02-01 09:07:20 -05:00
|
|
|
} else if (this.unregisteredIqListener != null) {
|
2014-02-09 08:10:52 -05:00
|
|
|
this.unregisteredIqListener.onIqPacketReceived(account, packet);
|
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-02-04 09:09:50 -05:00
|
|
|
private void sendStartTLS() {
|
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 {
|
|
|
|
Tag nextTag = tagReader.readTag(); // should be proceed end tag
|
|
|
|
try {
|
2014-03-06 14:03:35 -05:00
|
|
|
SSLContext sc = SSLContext.getInstance("TLS");
|
|
|
|
TrustManagerFactory tmf = TrustManagerFactory
|
|
|
|
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
|
|
|
// Initialise the TMF as you normally would, for example:
|
|
|
|
// tmf.in
|
|
|
|
try {
|
|
|
|
tmf.init((KeyStore) null);
|
|
|
|
} catch (KeyStoreException e1) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
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 {
|
|
|
|
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
|
|
|
|
sha1.update(chain[0].getEncoded());
|
|
|
|
sha = CryptoHelper.bytesToHex(sha1.digest());
|
|
|
|
if (!sha.equals(account.getSSLFingerprint())) {
|
|
|
|
changeStatus(Account.STATUS_TLS_ERROR);
|
|
|
|
if (tlsListener!=null) {
|
|
|
|
tlsListener.onTLSExceptionReceived(sha,account);
|
|
|
|
}
|
|
|
|
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-03-07 08:24:33 -05: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-03-07 20:06:00 -05: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendSaslAuth() throws IOException, XmlPullParserException {
|
|
|
|
String saslString = SASL.plain(account.getUsername(),
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-09 08:10:52 -05:00
|
|
|
} else if (this.streamFeatures.hasChild("mechanisms")
|
|
|
|
&& shouldAuthenticate) {
|
2014-01-31 19:25:56 -05:00
|
|
|
sendSaslAuth();
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
if (this.streamFeatures.hasChild("bind") && shouldBind) {
|
2014-01-31 19:25:56 -05:00
|
|
|
sendBindRequest();
|
|
|
|
if (this.streamFeatures.hasChild("session")) {
|
|
|
|
IqPacket startSession = new IqPacket(IqPacket.TYPE_SET);
|
|
|
|
Element session = new Element("session");
|
2014-02-09 08:10:52 -05:00
|
|
|
session.setAttribute("xmlns",
|
|
|
|
"urn:ietf:params:xml:ns:xmpp-session");
|
2014-01-31 19:25:56 -05:00
|
|
|
session.setContent("");
|
|
|
|
startSession.addChild(session);
|
|
|
|
sendIqPacket(startSession, null);
|
|
|
|
tagWriter.writeElement(startSession);
|
|
|
|
}
|
|
|
|
Element presence = new Element("presence");
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-01-31 19:25:56 -05:00
|
|
|
tagWriter.writeElement(presence);
|
2014-01-30 18:33:01 -05: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-01-30 10:42:35 -05:00
|
|
|
Element bind = new Element("bind");
|
2014-02-09 08:10:52 -05:00
|
|
|
bind.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-bind");
|
2014-03-08 21:24:59 -05:00
|
|
|
Element resource = new Element("resource");
|
|
|
|
resource.setContent("Conversations");
|
|
|
|
bind.addChild(resource);
|
2014-01-30 10:42:35 -05:00
|
|
|
iq.addChild(bind);
|
2014-02-09 08:10:52 -05:00
|
|
|
this.sendIqPacket(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);
|
|
|
|
account.setStatus(Account.STATUS_ONLINE);
|
2014-02-09 08:10:52 -05:00
|
|
|
if (statusListener != null) {
|
2014-02-04 09:09:50 -05:00
|
|
|
statusListener.onStatusChanged(account);
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
sendServiceDiscovery();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendServiceDiscovery() {
|
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
|
|
|
iq.setAttribute("to", account.getServer());
|
|
|
|
Element query = new Element("query");
|
|
|
|
query.setAttribute("xmlns", "http://jabber.org/protocol/disco#info");
|
|
|
|
iq.addChild(query);
|
|
|
|
this.sendIqPacket(iq, new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
|
|
|
if (packet.hasChild("query")) {
|
|
|
|
List<Element> elements = packet.findChild("query")
|
|
|
|
.getChildren();
|
|
|
|
for (int i = 0; i < elements.size(); ++i) {
|
|
|
|
if (elements.get(i).getName().equals("feature")) {
|
|
|
|
discoFeatures.add(elements.get(i).getAttribute(
|
|
|
|
"var"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (discoFeatures.contains("urn:xmpp:carbons:2")) {
|
|
|
|
sendEnableCarbons();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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);
|
|
|
|
Element enable = new Element("enable");
|
|
|
|
enable.setAttribute("xmlns", "urn:xmpp:carbons:2");
|
|
|
|
iq.addChild(enable);
|
|
|
|
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-02-04 09:09:50 -05:00
|
|
|
private void sendStartStream() {
|
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() {
|
|
|
|
return new BigInteger(50, random).toString(32);
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
2014-02-04 09:09:50 -05:00
|
|
|
public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) {
|
2014-01-31 19:25:56 -05:00
|
|
|
String id = nextRandomId();
|
2014-02-09 08:10:52 -05:00
|
|
|
packet.setAttribute("id", id);
|
2014-01-31 19:25:56 -05:00
|
|
|
tagWriter.writeElement(packet);
|
|
|
|
if (callback != null) {
|
2014-03-02 23:01:02 -05:00
|
|
|
packetCallbacks.put(id, callback);
|
2014-01-31 19:25:56 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void sendMessagePacket(MessagePacket packet) {
|
2014-03-02 23:01:02 -05:00
|
|
|
this.sendMessagePacket(packet, null);
|
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
|
|
|
public void sendMessagePacket(MessagePacket packet,
|
|
|
|
OnMessagePacketReceived callback) {
|
2014-03-02 23:01:02 -05:00
|
|
|
String id = nextRandomId();
|
|
|
|
packet.setAttribute("id", id);
|
2014-02-01 09:07:20 -05:00
|
|
|
tagWriter.writeElement(packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
if (callback != null) {
|
|
|
|
packetCallbacks.put(id, callback);
|
|
|
|
}
|
2014-02-01 09:07:20 -05:00
|
|
|
}
|
2014-02-09 08:10:52 -05:00
|
|
|
|
|
|
|
public void sendPresencePacket(PresencePacket packet) {
|
2014-03-02 23:01:02 -05:00
|
|
|
this.sendPresencePacket(packet, null);
|
|
|
|
}
|
2014-03-06 09:11:56 -05:00
|
|
|
|
|
|
|
public PresencePacket sendPresencePacket(PresencePacket packet,
|
|
|
|
OnPresencePacketReceived callback) {
|
2014-03-02 23:01:02 -05:00
|
|
|
String id = nextRandomId();
|
|
|
|
packet.setAttribute("id", id);
|
2014-02-01 09:07:20 -05:00
|
|
|
tagWriter.writeElement(packet);
|
2014-03-02 23:01:02 -05:00
|
|
|
if (callback != null) {
|
|
|
|
packetCallbacks.put(id, callback);
|
|
|
|
}
|
2014-03-03 22:09:15 -05:00
|
|
|
return packet;
|
2014-02-01 09:07:20 -05:00
|
|
|
}
|
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-02-09 08:10:52 -05:00
|
|
|
|
2014-02-04 09:09:50 -05:00
|
|
|
public void setOnStatusChangedListener(OnStatusChanged listener) {
|
|
|
|
this.statusListener = listener;
|
|
|
|
}
|
2014-03-07 08:24:33 -05:00
|
|
|
|
|
|
|
public void setOnTLSExceptionReceivedListener(OnTLSExceptionReceived listener) {
|
|
|
|
this.tlsListener = listener;
|
|
|
|
}
|
2014-02-04 09:09:50 -05:00
|
|
|
|
|
|
|
public void disconnect() {
|
2014-02-13 17:40:08 -05:00
|
|
|
tagWriter.writeTag(Tag.end("stream:stream"));
|
2014-02-04 09:09:50 -05:00
|
|
|
}
|
2014-03-07 20:06:00 -05:00
|
|
|
|
|
|
|
public boolean hasFeatureRosterManagment() {
|
|
|
|
if (this.streamFeatures==null) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return this.streamFeatures.hasChild("ver");
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 10:42:35 -05:00
|
|
|
}
|