aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/xmpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/eu/siacs/conversations/xmpp')
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java826
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jid/InvalidJidException.java48
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jid/Jid.java191
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java31
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java280
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java28
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jingle/JingleInbandTransport.java65
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jingle/JingleSocks5Transport.java26
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jingle/JingleTransport.java2
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java5
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/pep/Avatar.java4
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java33
12 files changed, 986 insertions, 553 deletions
diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
index d59a302d..9e6b8baf 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
@@ -12,6 +12,8 @@ import android.util.Log;
import android.util.SparseArray;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
+import org.json.JSONException;
+import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
@@ -40,9 +42,12 @@ import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import eu.siacs.conversations.Config;
+import eu.siacs.conversations.crypto.sasl.DigestMd5;
+import eu.siacs.conversations.crypto.sasl.Plain;
+import eu.siacs.conversations.crypto.sasl.SaslMechanism;
+import eu.siacs.conversations.crypto.sasl.ScramSha1;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.services.XmppConnectionService;
-import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.DNSHelper;
import eu.siacs.conversations.utils.zlib.ZLibInputStream;
import eu.siacs.conversations.utils.zlib.ZLibOutputStream;
@@ -50,6 +55,8 @@ import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xml.Tag;
import eu.siacs.conversations.xml.TagWriter;
import eu.siacs.conversations.xml.XmlReader;
+import eu.siacs.conversations.xmpp.jid.InvalidJidException;
+import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
@@ -78,12 +85,16 @@ public class XmppConnection implements Runnable {
private boolean shouldBind = true;
private boolean shouldAuthenticate = true;
private Element streamFeatures;
- private HashMap<String, List<String>> disco = new HashMap<String, List<String>>();
+ private HashMap<String, List<String>> disco = new HashMap<>();
+
private String streamId = null;
private int smVersion = 3;
- private SparseArray<String> messageReceipts = new SparseArray<String>();
- private boolean usingCompression = false;
- private boolean usingEncryption = false;
+ private SparseArray<String> messageReceipts = new SparseArray<>();
+
+ private boolean enabledCompression = false;
+ private boolean enabledEncryption = false;
+ private boolean enabledCarbons = false;
+
private int stanzasReceived = 0;
private int stanzasSent = 0;
private long lastPaketReceived = 0;
@@ -91,7 +102,7 @@ public class XmppConnection implements Runnable {
private long lastConnect = 0;
private long lastSessionStarted = 0;
private int attempt = 0;
- private Hashtable<String, PacketReceived> packetCallbacks = new Hashtable<String, PacketReceived>();
+ private Hashtable<String, PacketReceived> packetCallbacks = new Hashtable<>();
private OnPresencePacketReceived presenceListener = null;
private OnJinglePacketReceived jingleListener = null;
private OnIqPacketReceived unregisteredIqListener = null;
@@ -101,24 +112,26 @@ public class XmppConnection implements Runnable {
private OnMessageAcknowledged acknowledgedListener = null;
private XmppConnectionService mXmppConnectionService = null;
+ private SaslMechanism saslMechanism;
+
public XmppConnection(Account account, XmppConnectionService service) {
this.account = account;
this.wakeLock = service.getPowerManager().newWakeLock(
- PowerManager.PARTIAL_WAKE_LOCK, account.getJid());
+ PowerManager.PARTIAL_WAKE_LOCK, account.getJid().toBareJid().toString());
tagWriter = new TagWriter();
mXmppConnectionService = service;
applicationContext = service.getApplicationContext();
}
- protected void changeStatus(int nextStatus) {
+ protected void changeStatus(final Account.State nextStatus) {
if (account.getStatus() != nextStatus) {
- if ((nextStatus == Account.STATUS_OFFLINE)
- && (account.getStatus() != Account.STATUS_CONNECTING)
- && (account.getStatus() != Account.STATUS_ONLINE)
- && (account.getStatus() != Account.STATUS_DISABLED)) {
+ if ((nextStatus == Account.State.OFFLINE)
+ && (account.getStatus() != Account.State.CONNECTING)
+ && (account.getStatus() != Account.State.ONLINE)
+ && (account.getStatus() != Account.State.DISABLED)) {
return;
}
- if (nextStatus == Account.STATUS_ONLINE) {
+ if (nextStatus == Account.State.ONLINE) {
this.attempt = 0;
}
account.setStatus(nextStatus);
@@ -129,24 +142,24 @@ public class XmppConnection implements Runnable {
}
protected void connect() {
- Log.d(Config.LOGTAG, account.getJid() + ": connecting");
- usingCompression = false;
- usingEncryption = false;
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting");
+ enabledCompression = false;
+ enabledEncryption = false;
lastConnect = SystemClock.elapsedRealtime();
lastPingSent = SystemClock.elapsedRealtime();
this.attempt++;
try {
shouldAuthenticate = shouldBind = !account
- .isOptionSet(Account.OPTION_REGISTER);
+ .isOptionSet(Account.OPTION_REGISTER);
tagReader = new XmlReader(wakeLock);
tagWriter = new TagWriter();
packetCallbacks.clear();
- this.changeStatus(Account.STATUS_CONNECTING);
+ this.changeStatus(Account.State.CONNECTING);
Bundle result = DNSHelper.getSRVRecord(account.getServer());
ArrayList<Parcelable> values = result.getParcelableArrayList("values");
if ("timeout".equals(result.getString("error"))) {
- Log.d(Config.LOGTAG, account.getJid() + ": dns timeout");
- this.changeStatus(Account.STATUS_OFFLINE);
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": dns timeout");
+ this.changeStatus(Account.State.OFFLINE);
return;
} else if (values != null) {
int i = 0;
@@ -154,18 +167,24 @@ public class XmppConnection implements Runnable {
while (socketError && values.size() > i) {
Bundle namePort = (Bundle) values.get(i);
try {
- String srvRecordServer = namePort.getString("name");
+ String srvRecordServer;
+ try {
+ srvRecordServer=IDN.toASCII(namePort.getString("name"));
+ } catch (final IllegalArgumentException e) {
+ // TODO: Handle me?`
+ srvRecordServer = "";
+ }
int srvRecordPort = namePort.getInt("port");
String srvIpServer = namePort.getString("ipv4");
InetSocketAddress addr;
if (srvIpServer != null) {
addr = new InetSocketAddress(srvIpServer, srvRecordPort);
- Log.d(Config.LOGTAG, account.getJid()
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
+ ": using values from dns " + srvRecordServer
+ "[" + srvIpServer + "]:" + srvRecordPort);
} else {
addr = new InetSocketAddress(srvRecordServer, srvRecordPort);
- Log.d(Config.LOGTAG, account.getJid()
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
+ ": using values from dns "
+ srvRecordServer + ":" + srvRecordPort);
}
@@ -173,30 +192,30 @@ public class XmppConnection implements Runnable {
socket.connect(addr, 20000);
socketError = false;
} catch (UnknownHostException e) {
- Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
i++;
} catch (IOException e) {
- Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
i++;
}
}
if (socketError) {
- this.changeStatus(Account.STATUS_SERVER_NOT_FOUND);
+ this.changeStatus(Account.State.SERVER_NOT_FOUND);
if (wakeLock.isHeld()) {
try {
wakeLock.release();
- } catch (RuntimeException re) {
+ } catch (final RuntimeException ignored) {
}
}
return;
}
} else if (result.containsKey("error")
&& "nosrv".equals(result.getString("error", null))) {
- socket = new Socket(account.getServer(), 5222);
+ socket = new Socket(account.getServer().getDomainpart(), 5222);
} else {
- Log.d(Config.LOGTAG, account.getJid()
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
+ ": timeout in DNS resolution");
- changeStatus(Account.STATUS_OFFLINE);
+ changeStatus(Account.State.OFFLINE);
return;
}
OutputStream out = socket.getOutputStream();
@@ -220,45 +239,32 @@ public class XmppConnection implements Runnable {
socket.close();
}
} catch (UnknownHostException e) {
- this.changeStatus(Account.STATUS_SERVER_NOT_FOUND);
+ this.changeStatus(Account.State.SERVER_NOT_FOUND);
if (wakeLock.isHeld()) {
try {
wakeLock.release();
- } catch (RuntimeException re) {
+ } catch (final RuntimeException ignored) {
}
}
- return;
- } catch (IOException e) {
- Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
- this.changeStatus(Account.STATUS_OFFLINE);
+ } catch (final IOException | XmlPullParserException e) {
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
+ this.changeStatus(Account.State.OFFLINE);
if (wakeLock.isHeld()) {
try {
wakeLock.release();
- } catch (RuntimeException re) {
+ } catch (final RuntimeException ignored) {
}
}
- return;
} catch (NoSuchAlgorithmException e) {
- Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
- this.changeStatus(Account.STATUS_OFFLINE);
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
+ this.changeStatus(Account.State.OFFLINE);
Log.d(Config.LOGTAG, "compression exception " + e.getMessage());
if (wakeLock.isHeld()) {
try {
wakeLock.release();
- } catch (RuntimeException re) {
+ } catch (final RuntimeException ignored) {
}
}
- return;
- } catch (XmlPullParserException e) {
- Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
- this.changeStatus(Account.STATUS_OFFLINE);
- if (wakeLock.isHeld()) {
- try {
- wakeLock.release();
- } catch (RuntimeException re) {
- }
- }
- return;
}
}
@@ -268,137 +274,151 @@ public class XmppConnection implements Runnable {
connect();
}
- private void processStream(Tag currentTag) throws XmlPullParserException,
- 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")) {
- Log.d(Config.LOGTAG, account.getJid() + ": logged in");
- tagReader.readTag();
- tagReader.reset();
- sendStartStream();
- processStream(tagReader.readTag());
- break;
- } else if (nextTag.isStart("failure")) {
- tagReader.readElement(nextTag);
- changeStatus(Account.STATUS_UNAUTHORIZED);
- } else if (nextTag.isStart("challenge")) {
- String challange = tagReader.readElement(nextTag).getContent();
- Element response = new Element("response");
- response.setAttribute("xmlns",
- "urn:ietf:params:xml:ns:xmpp-sasl");
- response.setContent(CryptoHelper.saslDigestMd5(account,
- challange, mXmppConnectionService.getRNG()));
- 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()
- + ": stream managment(" + smVersion
- + ") enabled (resumable)");
- } else {
- Log.d(Config.LOGTAG, account.getJid()
- + ": 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()
- + ": session resumed with lost packages");
- stanzasSent = serverCount;
- } else {
- Log.d(Config.LOGTAG, account.getJid()
- + ": session resumed");
- }
- if (acknowledgedListener != null) {
- for (int i = 0; i < messageReceipts.size(); ++i) {
- if (serverCount >= messageReceipts.keyAt(i)) {
- acknowledgedListener.onMessageAcknowledged(
- account, messageReceipts.valueAt(i));
+ private void processStream(final Tag currentTag) throws XmlPullParserException,
+ 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);
+ } catch (final SaslMechanism.AuthenticationException e) {
+ disconnect(true);
+ Log.e(Config.LOGTAG, String.valueOf(e));
+ }
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
+ account.setKey(Account.PINNED_MECHANISM_KEY,
+ String.valueOf(saslMechanism.getPriority()));
+ tagReader.reset();
+ sendStartStream();
+ processStream(tagReader.readTag());
+ break;
+ } else if (nextTag.isStart("failure")) {
+ tagReader.readElement(nextTag);
+ changeStatus(Account.State.UNAUTHORIZED);
+ } 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));
+ } catch (final SaslMechanism.AuthenticationException e) {
+ // 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) {
+
+ }
+ sendServiceDiscoveryInfo(account.getServer());
+ sendServiceDiscoveryItems(account.getServer());
+ 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;
+ if (account.getStatus() != Account.State.ONLINE) {
+ 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();
+ }
+ if (account.getStatus() == Account.State.ONLINE) {
+ account. setStatus(Account.State.OFFLINE);
+ if (statusListener != null) {
+ statusListener.onStatusChanged(account);
}
}
- }
- messageReceipts.clear();
- } catch (NumberFormatException e) {
-
- }
- 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() + ": resumption failed");
- streamId = null;
- if (account.getStatus() != Account.STATUS_ONLINE) {
- 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();
- }
- if (account.getStatus() == Account.STATUS_ONLINE) {
- account.setStatus(Account.STATUS_OFFLINE);
- if (statusListener != null) {
- statusListener.onStatusChanged(account);
- }
- }
}
private void sendInitialPing() {
- Log.d(Config.LOGTAG, account.getJid() + ": sending intial ping");
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping");
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
- iq.setFrom(account.getFullJid());
+ iq.setFrom(account.getJid());
iq.addChild("ping", "urn:xmpp:ping");
this.sendIqPacket(iq, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
- Log.d(Config.LOGTAG, account.getJid()
+ Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
+ ": online with resource " + account.getResource());
- changeStatus(Account.STATUS_ONLINE);
+ changeStatus(Account.State.ONLINE);
}
});
}
private Element processPacket(Tag currentTag, int packetType)
- throws XmlPullParserException, IOException {
+ throws XmlPullParserException, IOException {
Element element;
switch (packetType) {
case PACKET_IQ:
@@ -425,10 +445,10 @@ public class XmppConnection implements Runnable {
if (packetType == PACKET_IQ
&& "jingle".equals(child.getName())
&& ("set".equalsIgnoreCase(type) || "get"
- .equalsIgnoreCase(type))) {
+ .equalsIgnoreCase(type))) {
element = new JinglePacket();
element.setAttributes(currentTag.getAttributes());
- }
+ }
element.addChild(child);
}
nextTag = tagReader.readTag();
@@ -442,64 +462,64 @@ public class XmppConnection implements Runnable {
}
private void processIq(Tag currentTag) throws XmlPullParserException,
- IOException {
- IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
+ IOException {
+ IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
- if (packet.getId() == null) {
- return; // an iq packet without id is definitely invalid
- }
-
- 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);
- }
+ if (packet.getId() == null) {
+ return; // an iq packet without id is definitely invalid
+ }
- packetCallbacks.remove(packet.getId());
- } else if ((packet.getType() == IqPacket.TYPE_GET || packet
- .getType() == IqPacket.TYPE_SET)
- && this.unregisteredIqListener != null) {
- this.unregisteredIqListener.onIqPacketReceived(account, packet);
- }
- }
+ 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);
+ }
+ }
}
private void processMessage(Tag currentTag) throws XmlPullParserException,
- 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);
- }
+ 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);
+ }
}
private void processPresence(Tag currentTag) throws XmlPullParserException,
- 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);
- }
+ 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);
+ }
}
private void sendCompressionZlib() throws IOException {
@@ -509,19 +529,19 @@ public class XmppConnection implements Runnable {
tagWriter.writeElement(compress);
}
- private void switchOverToZLib(Tag currentTag)
- 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() + ": compression enabled");
- usingCompression = true;
- processStream(tagReader.readTag());
+ private void switchOverToZLib(final Tag currentTag)
+ 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");
+ enabledCompression = true;
+ processStream(tagReader.readTag());
}
private void sendStartTLS() throws IOException {
@@ -532,119 +552,124 @@ public class XmppConnection implements Runnable {
private SharedPreferences getPreferences() {
return PreferenceManager
- .getDefaultSharedPreferences(applicationContext);
+ .getDefaultSharedPreferences(applicationContext);
}
private boolean enableLegacySSL() {
return getPreferences().getBoolean("enable_legacy_ssl", false);
}
- private void switchOverToTls(Tag currentTag) throws XmlPullParserException,
- 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");
- }
-
- HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier());
-
- if (socket == null || socket.isClosed()) {
- throw new IOException("socket null or closed");
- }
- final InetAddress address = socket.getInetAddress();
- if (address == null) {
- throw new IOException("socket address was null");
- }
-
- final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true);
+ private void switchOverToTls(final Tag currentTag) throws XmlPullParserException,
+ 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");
+ }
- // 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<String>(
- Arrays.asList(sslSocket.getSupportedProtocols()));
- supportedProtocols.remove("SSLv3");
- supportProtocols = new String[supportedProtocols.size()];
- supportedProtocols.toArray(supportProtocols);
- }
- sslSocket.setEnabledProtocols(supportProtocols);
+ final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier());
- if (verifier != null
- && !verifier.verify(account.getServer(),
- 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()
- + ": TLS connection established");
- usingEncryption = true;
- processStream(tagReader.readTag());
- sslSocket.close();
- } catch (NoSuchAlgorithmException e1) {
- e1.printStackTrace();
- } catch (KeyManagementException e) {
- e.printStackTrace();
- }
- }
+ if (socket == null || socket.isClosed()) {
+ throw new IOException("socket null or closed");
+ }
+ final InetAddress address = socket.getInetAddress();
+ if (address == null) {
+ throw new IOException("socket address was null");
+ }
- private void sendSaslAuthPlain() throws IOException {
- String saslString = CryptoHelper.saslPlain(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);
- }
+ final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true);
- 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);
+ // 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())) {
+ Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
+ disconnect(true);
+ changeStatus(Account.State.SECURITY_ERROR);
+ }
+ tagReader.setInputStream(sslSocket.getInputStream());
+ tagWriter.setOutputStream(sslSocket.getOutputStream());
+ sendStartStream();
+ Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ + ": TLS connection established");
+ enabledEncryption = true;
+ processStream(tagReader.readTag());
+ sslSocket.close();
+ } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
+ e1.printStackTrace();
+ }
}
private void processStreamFeatures(Tag currentTag)
- throws XmlPullParserException, IOException {
+ throws XmlPullParserException, IOException {
this.streamFeatures = tagReader.readElement(currentTag);
- if (this.streamFeatures.hasChild("starttls") && !usingEncryption) {
+ if (this.streamFeatures.hasChild("starttls") && !enabledEncryption) {
sendStartTLS();
} else if (compressionAvailable()) {
sendCompressionZlib();
} else if (this.streamFeatures.hasChild("register")
&& account.isOptionSet(Account.OPTION_REGISTER)
- && usingEncryption) {
+ && enabledEncryption) {
sendRegistryRequest();
} else if (!this.streamFeatures.hasChild("register")
&& account.isOptionSet(Account.OPTION_REGISTER)) {
- changeStatus(Account.STATUS_REGISTRATION_NOT_SUPPORTED);
+ changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED);
disconnect(true);
} else if (this.streamFeatures.hasChild("mechanisms")
- && shouldAuthenticate && usingEncryption) {
- List<String> mechanisms = extractMechanisms(streamFeatures
+ && shouldAuthenticate && enabledEncryption) {
+ final List<String> mechanisms = extractMechanisms(streamFeatures
.findChild("mechanisms"));
- if (mechanisms.contains("PLAIN")) {
- sendSaslAuthPlain();
+ final Element auth = new Element("auth");
+ auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
+ if (mechanisms.contains("SCRAM-SHA-1")) {
+ saslMechanism = new ScramSha1(tagWriter, account, mXmppConnectionService.getRNG());
} else if (mechanisms.contains("DIGEST-MD5")) {
- sendSaslAuthDigestMd5();
+ saslMechanism = new DigestMd5(tagWriter, account, mXmppConnectionService.getRNG());
+ } else if (mechanisms.contains("PLAIN")) {
+ saslMechanism = new Plain(tagWriter, account);
+ }
+ 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);
+ changeStatus(Account.State.SECURITY_ERROR);
+ }
+ } catch (final JSONException e) {
+ Log.d(Config.LOGTAG, "Parse error while checking pinned auth mechanism");
}
+ Log.d(Config.LOGTAG,account.getJid().toString()+": Authenticating with " + saslMechanism.getMechanism());
+ auth.setAttribute("mechanism", saslMechanism.getMechanism());
+ if (!saslMechanism.getClientFirstMessage().isEmpty()) {
+ auth.setContent(saslMechanism.getClientFirstMessage());
+ }
+ tagWriter.writeElement(auth);
} else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:"
- + smVersion)
+ + smVersion)
&& streamId != null) {
ResumePacket resume = new ResumePacket(this.streamId,
stanzasReceived, smVersion);
@@ -652,15 +677,14 @@ public class XmppConnection implements Runnable {
} else if (this.streamFeatures.hasChild("bind") && shouldBind) {
sendBindRequest();
} else {
- Log.d(Config.LOGTAG, account.getJid()
- + ": incompatible server. disconnecting");
disconnect(true);
+ changeStatus(Account.State.INCOMPATIBLE_SERVER);
}
}
private boolean compressionAvailable() {
if (!this.streamFeatures.hasChild("compression",
- "http://jabber.org/features/compress"))
+ "http://jabber.org/features/compress"))
return false;
if (!ZLibOutputStream.SUPPORTED)
return false;
@@ -681,7 +705,7 @@ public class XmppConnection implements Runnable {
}
private List<String> extractMechanisms(Element stream) {
- ArrayList<String> mechanisms = new ArrayList<String>(stream
+ ArrayList<String> mechanisms = new ArrayList<>(stream
.getChildren().size());
for (Element child : stream.getChildren()) {
mechanisms.add(child.getContent());
@@ -702,35 +726,35 @@ public class XmppConnection implements Runnable {
&& (packet.query().hasChild("password"))) {
IqPacket register = new IqPacket(IqPacket.TYPE_SET);
Element username = new Element("username")
- .setContent(account.getUsername());
+ .setContent(account.getUsername());
Element password = new Element("password")
- .setContent(account.getPassword());
+ .setContent(account.getPassword());
register.query("jabber:iq:register").addChild(username);
register.query().addChild(password);
sendIqPacket(register, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account,
- IqPacket packet) {
+ IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_RESULT) {
account.setOption(Account.OPTION_REGISTER,
false);
- changeStatus(Account.STATUS_REGISTRATION_SUCCESSFULL);
+ changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
} else if (packet.hasChild("error")
&& (packet.findChild("error")
- .hasChild("conflict"))) {
- changeStatus(Account.STATUS_REGISTRATION_CONFLICT);
+ .hasChild("conflict"))) {
+ changeStatus(Account.State.REGISTRATION_CONFLICT);
} else {
- changeStatus(Account.STATUS_REGISTRATION_FAILED);
+ changeStatus(Account.State.REGISTRATION_FAILED);
Log.d(Config.LOGTAG, packet.toString());
}
disconnect(true);
}
});
} else {
- changeStatus(Account.STATUS_REGISTRATION_FAILED);
+ changeStatus(Account.State.REGISTRATION_FAILED);
disconnect(true);
- Log.d(Config.LOGTAG, account.getJid()
+ Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": could not register. instructions are"
+ instructions.getContent());
}
@@ -741,15 +765,19 @@ public class XmppConnection implements Runnable {
private void sendBindRequest() throws IOException {
IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind")
- .addChild("resource").setContent(account.getResource());
+ .addChild("resource").setContent(account.getResource());
this.sendUnboundIqPacket(iq, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
Element bind = packet.findChild("bind");
if (bind != null) {
- Element jid = bind.findChild("jid");
+ final Element jid = bind.findChild("jid");
if (jid != null && jid.getContent() != null) {
- account.setResource(jid.getContent().split("/", 2)[1]);
+ 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")) {
smVersion = 3;
EnablePacket enable = new EnablePacket(smVersion);
@@ -757,13 +785,15 @@ public class XmppConnection implements Runnable {
stanzasSent = 0;
messageReceipts.clear();
} else if (streamFeatures.hasChild("sm",
- "urn:xmpp:sm:2")) {
+ "urn:xmpp:sm:2")) {
smVersion = 2;
EnablePacket enable = new EnablePacket(smVersion);
tagWriter.writeStanzaAsync(enable);
stanzasSent = 0;
messageReceipts.clear();
}
+ enabledCarbons = false;
+ disco.clear();
sendServiceDiscoveryInfo(account.getServer());
sendServiceDiscoveryItems(account.getServer());
if (bindListener != null) {
@@ -779,7 +809,7 @@ public class XmppConnection implements Runnable {
}
});
if (this.streamFeatures.hasChild("session")) {
- Log.d(Config.LOGTAG, account.getJid()
+ Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": sending deprecated session");
IqPacket startSession = new IqPacket(IqPacket.TYPE_SET);
startSession.addChild("session",
@@ -788,49 +818,61 @@ public class XmppConnection implements Runnable {
}
}
- private void sendServiceDiscoveryInfo(final String server) {
- IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
- iq.setTo(server);
- iq.query("http://jabber.org/protocol/disco#info");
- this.sendIqPacket(iq, new OnIqPacketReceived() {
+ private void sendServiceDiscoveryInfo(final Jid server) {
+ if (disco.containsKey(server.toDomainJid().toString())) {
+ if (account.getServer().equals(server.toDomainJid())) {
+ enableAdvancedStreamFeatures();
+ }
+ } else {
+ final IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
+ iq.setTo(server.toDomainJid());
+ iq.query("http://jabber.org/protocol/disco#info");
+ this.sendIqPacket(iq, new OnIqPacketReceived() {
- @Override
- public void onIqPacketReceived(Account account, IqPacket packet) {
- 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")) {
- features.add(elements.get(i).getAttribute("var"));
+ @Override
+ public void onIqPacketReceived(Account account, IqPacket packet) {
+ final List<Element> elements = packet.query().getChildren();
+ final List<String> features = new ArrayList<>();
+ for (Element element : elements) {
+ if (element.getName().equals("feature")) {
+ features.add(element.getAttribute("var"));
+ }
}
- }
- disco.put(server, features);
+ disco.put(server.toDomainJid().toString(), features);
- if (account.getServer().equals(server)) {
- enableAdvancedStreamFeatures();
+ if (account.getServer().equals(server.toDomainJid())) {
+ enableAdvancedStreamFeatures();
+ }
}
- }
- });
+ });
+ }
}
private void enableAdvancedStreamFeatures() {
if (getFeatures().carbons()) {
- sendEnableCarbons();
+ if (!enabledCarbons) {
+ sendEnableCarbons();
+ }
}
}
- private void sendServiceDiscoveryItems(final String server) {
- IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
- iq.setTo(server);
+ private void sendServiceDiscoveryItems(final Jid server) {
+ final IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
+ iq.setTo(server.toDomainJid());
iq.query("http://jabber.org/protocol/disco#items");
this.sendIqPacket(iq, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
List<Element> elements = packet.query().getChildren();
- for (int i = 0; i < elements.size(); ++i) {
- if (elements.get(i).getName().equals("item")) {
- String jid = elements.get(i).getAttribute("jid");
- sendServiceDiscoveryInfo(jid);
+ 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?
+ }
}
}
}
@@ -845,10 +887,11 @@ public class XmppConnection implements Runnable {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (!packet.hasChild("error")) {
- Log.d(Config.LOGTAG, account.getJid()
+ Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": successfully enabled carbons");
+ enabledCarbons = true;
} else {
- Log.d(Config.LOGTAG, account.getJid()
+ Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": error enableing carbons " + packet.toString());
}
}
@@ -856,21 +899,21 @@ public class XmppConnection implements Runnable {
}
private void processStreamError(Tag currentTag)
- throws XmlPullParserException, IOException {
+ throws XmlPullParserException, IOException {
Element streamError = tagReader.readElement(currentTag);
if (streamError != null && streamError.hasChild("conflict")) {
- String resource = account.getResource().split("\\.")[0];
+ final String resource = account.getResource().split("\\.")[0];
account.setResource(resource + "." + nextRandomId());
Log.d(Config.LOGTAG,
- account.getJid() + ": switching resource due to conflict ("
- + account.getResource() + ")");
+ account.getJid().toBareJid() + ": switching resource due to conflict ("
+ + account.getResource() + ")");
}
}
private void sendStartStream() throws IOException {
Tag stream = Tag.start("stream:stream");
- stream.setAttribute("from", account.getJid());
- stream.setAttribute("to", account.getServer());
+ stream.setAttribute("from", account.getJid().toBareJid().toString());
+ stream.setAttribute("to", account.getServer().toString());
stream.setAttribute("version", "1.0");
stream.setAttribute("xml:lang", "en");
stream.setAttribute("xmlns", "jabber:client");
@@ -887,7 +930,7 @@ public class XmppConnection implements Runnable {
String id = nextRandomId();
packet.setAttribute("id", id);
}
- packet.setFrom(account.getFullJid());
+ packet.setFrom(account.getJid());
this.sendPacket(packet, callback);
}
@@ -908,11 +951,11 @@ public class XmppConnection implements Runnable {
}
private synchronized void sendPacket(final AbstractStanza packet,
- PacketReceived callback) {
+ PacketReceived callback) {
if (packet.getName().equals("iq") || packet.getName().equals("message")
|| packet.getName().equals("presence")) {
++stanzasSent;
- }
+ }
tagWriter.writeStanzaAsync(packet);
if (packet instanceof MessagePacket && packet.getId() != null
&& this.streamId != null) {
@@ -920,7 +963,7 @@ public class XmppConnection implements Runnable {
+ stanzasSent);
this.messageReceipts.put(stanzasSent, packet.getId());
tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion));
- }
+ }
if (callback != null) {
if (packet.getId() == null) {
packet.setId(nextRandomId());
@@ -934,7 +977,7 @@ public class XmppConnection implements Runnable {
tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
} else {
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
- iq.setFrom(account.getFullJid());
+ iq.setFrom(account.getJid());
iq.addChild("ping", "urn:xmpp:ping");
this.sendIqPacket(iq, null);
}
@@ -944,22 +987,22 @@ public class XmppConnection implements Runnable {
public void setOnMessagePacketReceivedListener(
OnMessagePacketReceived listener) {
this.messageListener = listener;
- }
+ }
public void setOnUnregisteredIqPacketReceivedListener(
OnIqPacketReceived listener) {
this.unregisteredIqListener = listener;
- }
+ }
public void setOnPresencePacketReceivedListener(
OnPresencePacketReceived listener) {
this.presenceListener = listener;
- }
+ }
public void setOnJinglePacketReceivedListener(
OnJinglePacketReceived listener) {
this.jingleListener = listener;
- }
+ }
public void setOnStatusChangedListener(OnStatusChanged listener) {
this.statusListener = listener;
@@ -974,7 +1017,7 @@ public class XmppConnection implements Runnable {
}
public void disconnect(boolean force) {
- Log.d(Config.LOGTAG, account.getJid() + ": disconnecting");
+ Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting");
try {
if (force) {
socket.close();
@@ -1008,7 +1051,7 @@ public class XmppConnection implements Runnable {
}
public List<String> findDiscoItemsByFeature(String feature) {
- List<String> items = new ArrayList<String>();
+ final List<String> items = new ArrayList<>();
for (Entry<String, List<String>> cursor : disco.entrySet()) {
if (cursor.getValue().contains(feature)) {
items.add(cursor.getKey());
@@ -1084,11 +1127,9 @@ public class XmppConnection implements Runnable {
this.connection = connection;
}
- private boolean hasDiscoFeature(String server, String feature) {
- if (!connection.disco.containsKey(server)) {
- return false;
- }
- return connection.disco.get(server).contains(feature);
+ private boolean hasDiscoFeature(final Jid server, final String feature) {
+ return connection.disco.containsKey(server.toDomainJid().toString()) &&
+ connection.disco.get(server.toDomainJid().toString()).contains(feature);
}
public boolean carbons() {
@@ -1100,12 +1141,7 @@ public class XmppConnection implements Runnable {
}
public boolean csi() {
- if (connection.streamFeatures == null) {
- return false;
- } else {
- return connection.streamFeatures.hasChild("csi",
- "urn:xmpp:csi:0");
- }
+ return connection.streamFeatures != null && connection.streamFeatures.hasChild("csi", "urn:xmpp:csi:0");
}
public boolean pubsub() {
@@ -1118,20 +1154,16 @@ public class XmppConnection implements Runnable {
}
public boolean rosterVersioning() {
- if (connection.streamFeatures == null) {
- return false;
- } else {
- return connection.streamFeatures.hasChild("ver");
- }
+ return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
}
public boolean streamhost() {
return connection
- .findDiscoItemByFeature("http://jabber.org/protocol/bytestreams") != null;
+ .findDiscoItemByFeature("http://jabber.org/protocol/bytestreams") != null;
}
public boolean compression() {
- return connection.usingCompression;
+ return connection.enabledCompression;
}
}
}
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jid/InvalidJidException.java b/src/main/java/eu/siacs/conversations/xmpp/jid/InvalidJidException.java
new file mode 100644
index 00000000..f1855263
--- /dev/null
+++ b/src/main/java/eu/siacs/conversations/xmpp/jid/InvalidJidException.java
@@ -0,0 +1,48 @@
+package eu.siacs.conversations.xmpp.jid;
+
+public class InvalidJidException extends Exception {
+
+ // This is probably not the "Java way", but the "Java way" means we'd have a ton of extra tiny,
+ // annoying classes floating around. I like this.
+ public final static String INVALID_LENGTH = "JID must be between 0 and 3071 characters";
+ public final static String INVALID_PART_LENGTH = "JID part must be between 0 and 1023 characters";
+ public final static String INVALID_CHARACTER = "JID contains an invalid character";
+ public final static String STRINGPREP_FAIL = "The STRINGPREP operation has failed for the given JID";
+
+ /**
+ * Constructs a new {@code Exception} that includes the current stack trace.
+ */
+ public InvalidJidException() {
+ }
+
+ /**
+ * Constructs a new {@code Exception} with the current stack trace and the
+ * specified detail message.
+ *
+ * @param detailMessage the detail message for this exception.
+ */
+ public InvalidJidException(final String detailMessage) {
+ super(detailMessage);
+ }
+
+ /**
+ * Constructs a new {@code Exception} with the current stack trace, the
+ * specified detail message and the specified cause.
+ *
+ * @param detailMessage the detail message for this exception.
+ * @param throwable the cause of this exception.
+ */
+ public InvalidJidException(final String detailMessage, final Throwable throwable) {
+ super(detailMessage, throwable);
+ }
+
+ /**
+ * Constructs a new {@code Exception} with the current stack trace and the
+ * specified cause.
+ *
+ * @param throwable the cause of this exception.
+ */
+ public InvalidJidException(final Throwable throwable) {
+ super(throwable);
+ }
+}
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jid/Jid.java b/src/main/java/eu/siacs/conversations/xmpp/jid/Jid.java
new file mode 100644
index 00000000..ebf8a6ed
--- /dev/null
+++ b/src/main/java/eu/siacs/conversations/xmpp/jid/Jid.java
@@ -0,0 +1,191 @@
+package eu.siacs.conversations.xmpp.jid;
+
+import net.java.otr4j.session.SessionID;
+
+import java.net.IDN;
+
+import gnu.inet.encoding.Stringprep;
+import gnu.inet.encoding.StringprepException;
+
+/**
+ * The `Jid' class provides an immutable representation of a JID.
+ */
+public final class Jid {
+
+ private final String localpart;
+ private final String domainpart;
+ private final String resourcepart;
+
+ // It's much more efficient to store the ful JID as well as the parts instead of figuring them
+ // all out every time (since some characters are displayed but aren't used for comparisons).
+ private final String displayjid;
+
+ public String getLocalpart() {
+ return localpart;
+ }
+
+ public String getDomainpart() {
+ return IDN.toUnicode(domainpart);
+ }
+
+ public String getResourcepart() {
+ return resourcepart;
+ }
+
+ public static Jid fromSessionID(SessionID id) throws InvalidJidException{
+ if (id.getUserID().isEmpty()) {
+ return Jid.fromString(id.getAccountID());
+ } else {
+ return Jid.fromString(id.getAccountID()+"/"+id.getUserID());
+ }
+ }
+
+ public static Jid fromString(final String jid) throws InvalidJidException {
+ return new Jid(jid);
+ }
+
+ public static Jid fromParts(final String localpart,
+ final String domainpart,
+ final String resourcepart) throws InvalidJidException {
+ String out;
+ if (localpart == null || localpart.isEmpty()) {
+ out = domainpart;
+ } else {
+ out = localpart + "@" + domainpart;
+ }
+ if (resourcepart != null && !resourcepart.isEmpty()) {
+ out = out + "/" + resourcepart;
+ }
+ return new Jid(out);
+ }
+
+ private Jid(final String jid) throws InvalidJidException {
+ // Hackish Android way to count the number of chars in a string... should work everywhere.
+ final int atCount = jid.length() - jid.replace("@", "").length();
+ final int slashCount = jid.length() - jid.replace("/", "").length();
+
+ // Throw an error if there's anything obvious wrong with the JID...
+ if (jid.isEmpty() || jid.length() > 3071) {
+ throw new InvalidJidException(InvalidJidException.INVALID_LENGTH);
+ }
+ if (atCount > 1 || slashCount > 1 ||
+ jid.startsWith("@") || jid.endsWith("@") ||
+ jid.startsWith("/") || jid.endsWith("/")) {
+ throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER);
+ }
+
+ String finaljid;
+
+ final int domainpartStart;
+ if (atCount == 1) {
+ final int atLoc = jid.indexOf("@");
+ final String lp = jid.substring(0, atLoc);
+ try {
+ localpart = Stringprep.nodeprep(lp);
+ } catch (final StringprepException e) {
+ throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
+ }
+ if (localpart.isEmpty() || localpart.length() > 1023) {
+ throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
+ }
+ domainpartStart = atLoc + 1;
+ finaljid = lp + "@";
+ } else {
+ localpart = "";
+ finaljid = "";
+ domainpartStart = 0;
+ }
+
+ final String dp;
+ if (slashCount == 1) {
+ final int slashLoc = jid.indexOf("/");
+ final String rp = jid.substring(slashLoc + 1, jid.length());
+ try {
+ resourcepart = Stringprep.resourceprep(rp);
+ } catch (final StringprepException e) {
+ throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
+ }
+ if (resourcepart.isEmpty() || resourcepart.length() > 1023) {
+ throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
+ }
+ dp = IDN.toUnicode(jid.substring(domainpartStart, slashLoc), IDN.USE_STD3_ASCII_RULES);
+ finaljid = finaljid + dp + "/" + rp;
+ } else {
+ resourcepart = "";
+ dp = IDN.toUnicode(jid.substring(domainpartStart, jid.length()),
+ IDN.USE_STD3_ASCII_RULES);
+ finaljid = finaljid + dp;
+ }
+
+ // Remove trailing "." before storing the domain part.
+ if (dp.endsWith(".")) {
+ try {
+ domainpart = IDN.toASCII(dp.substring(0, dp.length() - 1), IDN.USE_STD3_ASCII_RULES);
+ } catch (final IllegalArgumentException e) {
+ throw new InvalidJidException(e);
+ }
+ } else {
+ try {
+ domainpart = IDN.toASCII(dp, IDN.USE_STD3_ASCII_RULES);
+ } catch (final IllegalArgumentException e) {
+ throw new InvalidJidException(e);
+ }
+ }
+
+ // TODO: Find a proper domain validation library; validate individual parts, separators, etc.
+ if (domainpart.isEmpty() || domainpart.length() > 1023) {
+ throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
+ }
+
+ this.displayjid = finaljid;
+ }
+
+ public Jid toBareJid() {
+ try {
+ return resourcepart.isEmpty() ? this : fromParts(localpart, domainpart, "");
+ } catch (final InvalidJidException e) {
+ // This should never happen.
+ return null;
+ }
+ }
+
+ public Jid toDomainJid() {
+ try {
+ return resourcepart.isEmpty() && localpart.isEmpty() ? this : fromString(getDomainpart());
+ } catch (final InvalidJidException e) {
+ // This should never happen.
+ return null;
+ }
+ }
+
+ @Override
+ public String toString() {
+ return displayjid;
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ final Jid jid = (Jid) o;
+
+ return jid.hashCode() == this.hashCode();
+ }
+
+ @Override
+ public int hashCode() {
+ int result = localpart.hashCode();
+ result = 31 * result + domainpart.hashCode();
+ result = 31 * result + resourcepart.hashCode();
+ return result;
+ }
+
+ public boolean hasLocalpart() {
+ return !localpart.isEmpty();
+ }
+
+ public boolean isBareJid() {
+ return this.resourcepart.isEmpty();
+ }
+}
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java
index 3e7c7b68..281ea3ca 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import eu.siacs.conversations.xml.Element;
+import eu.siacs.conversations.xmpp.jid.Jid;
public class JingleCandidate {
@@ -17,7 +18,7 @@ public class JingleCandidate {
private String host;
private int port;
private int type;
- private String jid;
+ private Jid jid;
private int priority;
public JingleCandidate(String cid, boolean ours) {
@@ -37,11 +38,11 @@ public class JingleCandidate {
return this.host;
}
- public void setJid(String jid) {
+ public void setJid(final Jid jid) {
this.jid = jid;
}
- public String getJid() {
+ public Jid getJid() {
return this.jid;
}
@@ -58,13 +59,17 @@ public class JingleCandidate {
}
public void setType(String type) {
- if ("proxy".equals(type)) {
- this.type = TYPE_PROXY;
- } else if ("direct".equals(type)) {
- this.type = TYPE_DIRECT;
- } else {
- this.type = TYPE_UNKNOWN;
- }
+ switch (type) {
+ case "proxy":
+ this.type = TYPE_PROXY;
+ break;
+ case "direct":
+ this.type = TYPE_DIRECT;
+ break;
+ default:
+ this.type = TYPE_UNKNOWN;
+ break;
+ }
}
public void setPriority(int i) {
@@ -93,7 +98,7 @@ public class JingleCandidate {
}
public static List<JingleCandidate> parse(List<Element> canditates) {
- List<JingleCandidate> parsedCandidates = new ArrayList<JingleCandidate>();
+ List<JingleCandidate> parsedCandidates = new ArrayList<>();
for (Element c : canditates) {
parsedCandidates.add(JingleCandidate.parse(c));
}
@@ -104,7 +109,7 @@ public class JingleCandidate {
JingleCandidate parsedCandidate = new JingleCandidate(
candidate.getAttribute("cid"), false);
parsedCandidate.setHost(candidate.getAttribute("host"));
- parsedCandidate.setJid(candidate.getAttribute("jid"));
+ parsedCandidate.setJid(candidate.getAttributeAsJid("jid"));
parsedCandidate.setType(candidate.getAttribute("type"));
parsedCandidate.setPriority(Integer.parseInt(candidate
.getAttribute("priority")));
@@ -118,7 +123,7 @@ public class JingleCandidate {
element.setAttribute("cid", this.getCid());
element.setAttribute("host", this.getHost());
element.setAttribute("port", Integer.toString(this.getPort()));
- element.setAttribute("jid", this.getJid());
+ element.setAttribute("jid", this.getJid().toString());
element.setAttribute("priority", Integer.toString(this.getPriority()));
if (this.getType() == TYPE_DIRECT) {
element.setAttribute("type", "direct");
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java
index 6b9ca9aa..3a1ba778 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java
@@ -1,5 +1,6 @@
package eu.siacs.conversations.xmpp.jingle;
+import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -9,18 +10,20 @@ import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import android.content.Intent;
-import android.graphics.BitmapFactory;
import android.net.Uri;
+import android.os.SystemClock;
import android.util.Log;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile;
+import eu.siacs.conversations.entities.DownloadablePlaceholder;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
+import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
@@ -28,9 +31,6 @@ import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class JingleConnection implements Downloadable {
- private final String[] extensions = { "webp", "jpeg", "jpg", "png" };
- private final String[] cryptoExtensions = { "pgp", "gpg", "otr" };
-
private JingleConnectionManager mJingleConnectionManager;
private XmppConnectionService mXmppConnectionService;
@@ -45,14 +45,14 @@ public class JingleConnection implements Downloadable {
private int ibbBlockSize = 4096;
private int mJingleStatus = -1;
- private int mStatus = -1;
+ private int mStatus = Downloadable.STATUS_UNKNOWN;
private Message message;
private String sessionId;
private Account account;
- private String initiator;
- private String responder;
- private List<JingleCandidate> candidates = new ArrayList<JingleCandidate>();
- private ConcurrentHashMap<String, JingleSocks5Transport> connections = new ConcurrentHashMap<String, JingleSocks5Transport>();
+ private Jid initiator;
+ private Jid responder;
+ private List<JingleCandidate> candidates = new ArrayList<>();
+ private ConcurrentHashMap<String, JingleSocks5Transport> connections = new ConcurrentHashMap<>();
private String transportId;
private Element fileOffer;
@@ -61,6 +61,9 @@ public class JingleConnection implements Downloadable {
private String contentName;
private String contentCreator;
+ private int mProgress = 0;
+ private long mLastGuiRefresh = 0;
+
private boolean receivedCandidate = false;
private boolean sentCandidate = false;
@@ -73,7 +76,7 @@ public class JingleConnection implements Downloadable {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_ERROR) {
- cancel();
+ fail();
}
}
};
@@ -82,23 +85,21 @@ public class JingleConnection implements Downloadable {
@Override
public void onFileTransmitted(DownloadableFile file) {
- if (responder.equals(account.getFullJid())) {
+ if (responder.equals(account.getJid())) {
sendSuccess();
if (acceptedAutomatically) {
message.markUnread();
JingleConnection.this.mXmppConnectionService
.getNotificationService().push(message);
}
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(file.getAbsolutePath(), options);
- int imageHeight = options.outHeight;
- int imageWidth = options.outWidth;
- message.setBody(Long.toString(file.getSize()) + '|'
- + imageWidth + '|' + imageHeight);
+ mXmppConnectionService.getFileBackend().updateFileParams(message);
mXmppConnectionService.databaseBackend.createMessage(message);
mXmppConnectionService.markMessage(message,
Message.STATUS_RECEIVED);
+ } else {
+ if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
+ file.delete();
+ }
}
Log.d(Config.LOGTAG,
"sucessfully transmitted file:" + file.getAbsolutePath());
@@ -113,7 +114,7 @@ public class JingleConnection implements Downloadable {
@Override
public void onFileTransferAborted() {
JingleConnection.this.sendCancel();
- JingleConnection.this.cancel();
+ JingleConnection.this.fail();
}
};
@@ -121,7 +122,7 @@ public class JingleConnection implements Downloadable {
@Override
public void success() {
- if (initiator.equals(account.getFullJid())) {
+ if (initiator.equals(account.getJid())) {
Log.d(Config.LOGTAG, "we were initiating. sending file");
transport.send(file, onFileTransmissionSatusChanged);
} else {
@@ -150,7 +151,7 @@ public class JingleConnection implements Downloadable {
return this.account;
}
- public String getCounterPart() {
+ public Jid getCounterPart() {
return this.message.getCounterpart();
}
@@ -160,14 +161,14 @@ public class JingleConnection implements Downloadable {
Reason reason = packet.getReason();
if (reason != null) {
if (reason.hasChild("cancel")) {
- this.cancel();
+ this.fail();
} else if (reason.hasChild("success")) {
this.receiveSuccess();
} else {
- this.cancel();
+ this.fail();
}
} else {
- this.cancel();
+ this.fail();
}
} else if (packet.isAction("session-accept")) {
returnResult = receiveAccept(packet);
@@ -202,8 +203,10 @@ public class JingleConnection implements Downloadable {
this.contentCreator = "initiator";
this.contentName = this.mJingleConnectionManager.nextRandomId();
this.message = message;
+ this.message.setDownloadable(this);
+ this.mStatus = Downloadable.STATUS_UPLOADING;
this.account = message.getConversation().getAccount();
- this.initiator = this.account.getFullJid();
+ this.initiator = this.account.getJid();
this.responder = this.message.getCounterpart();
this.sessionId = this.mJingleConnectionManager.nextRandomId();
if (this.candidates.size() > 0) {
@@ -254,17 +257,16 @@ public class JingleConnection implements Downloadable {
this.mJingleStatus = JINGLE_STATUS_INITIATED;
Conversation conversation = this.mXmppConnectionService
.findOrCreateConversation(account,
- packet.getFrom().split("/", 2)[0], false);
+ packet.getFrom().toBareJid(), false);
this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
this.message.setStatus(Message.STATUS_RECEIVED);
- this.message.setType(Message.TYPE_IMAGE);
this.mStatus = Downloadable.STATUS_OFFER;
this.message.setDownloadable(this);
- String[] fromParts = packet.getFrom().split("/", 2);
- this.message.setPresence(fromParts[1]);
+ final Jid from = packet.getFrom();
+ this.message.setCounterpart(from);
this.account = account;
this.initiator = packet.getFrom();
- this.responder = this.account.getFullJid();
+ this.responder = this.account.getJid();
this.sessionId = packet.getSessionId();
Content content = packet.getJingleContent();
this.contentCreator = content.getAttribute("creator");
@@ -277,75 +279,83 @@ public class JingleConnection implements Downloadable {
Element fileSize = fileOffer.findChild("size");
Element fileNameElement = fileOffer.findChild("name");
if (fileNameElement != null) {
- boolean supportedFile = false;
String[] filename = fileNameElement.getContent()
.toLowerCase(Locale.US).split("\\.");
- if (Arrays.asList(this.extensions).contains(
+ if (Arrays.asList(VALID_IMAGE_EXTENSIONS).contains(
filename[filename.length - 1])) {
- supportedFile = true;
- } else if (Arrays.asList(this.cryptoExtensions).contains(
+ message.setType(Message.TYPE_IMAGE);
+ } else if (Arrays.asList(VALID_CRYPTO_EXTENSIONS).contains(
filename[filename.length - 1])) {
if (filename.length == 3) {
- if (Arrays.asList(this.extensions).contains(
+ if (Arrays.asList(VALID_IMAGE_EXTENSIONS).contains(
filename[filename.length - 2])) {
- supportedFile = true;
- if (filename[filename.length - 1].equals("otr")) {
- Log.d(Config.LOGTAG, "receiving otr file");
- this.message
- .setEncryption(Message.ENCRYPTION_OTR);
- } else {
- this.message
- .setEncryption(Message.ENCRYPTION_PGP);
- }
+ message.setType(Message.TYPE_IMAGE);
+ } else {
+ message.setType(Message.TYPE_FILE);
+ }
+ if (filename[filename.length - 1].equals("otr")) {
+ message.setEncryption(Message.ENCRYPTION_OTR);
+ } else {
+ message.setEncryption(Message.ENCRYPTION_PGP);
}
}
+ } else {
+ message.setType(Message.TYPE_FILE);
}
- if (supportedFile) {
- long size = Long.parseLong(fileSize.getContent());
- message.setBody(Long.toString(size));
- conversation.add(message);
- mXmppConnectionService.updateConversationUi();
- if (size <= this.mJingleConnectionManager
- .getAutoAcceptFileSize()) {
- Log.d(Config.LOGTAG, "auto accepting file from "
- + packet.getFrom());
- this.acceptedAutomatically = true;
- this.sendAccept();
- } else {
- message.markUnread();
- Log.d(Config.LOGTAG,
- "not auto accepting new file offer with size: "
- + size
- + " allowed size:"
- + this.mJingleConnectionManager
- .getAutoAcceptFileSize());
- this.mXmppConnectionService.getNotificationService()
- .push(message);
- }
- this.file = this.mXmppConnectionService.getFileBackend()
- .getFile(message, false);
- if (message.getEncryption() == Message.ENCRYPTION_OTR) {
- byte[] key = conversation.getSymmetricKey();
- if (key == null) {
- this.sendCancel();
- this.cancel();
- return;
- } else {
- this.file.setKey(key);
+ if (message.getType() == Message.TYPE_FILE) {
+ String suffix = "";
+ if (!fileNameElement.getContent().isEmpty()) {
+ String parts[] = fileNameElement.getContent().split("/");
+ suffix = parts[parts.length - 1];
+ if (message.getEncryption() == Message.ENCRYPTION_OTR && suffix.endsWith(".otr")) {
+ suffix = suffix.substring(0,suffix.length() - 4);
+ } else if (message.getEncryption() == Message.ENCRYPTION_PGP && (suffix.endsWith(".pgp") || suffix.endsWith(".gpg"))) {
+ suffix = suffix.substring(0,suffix.length() - 4);
}
}
- this.file.setExpectedSize(size);
+ message.setRelativeFilePath(message.getUuid()+"_"+suffix);
+ }
+ long size = Long.parseLong(fileSize.getContent());
+ message.setBody(Long.toString(size));
+ conversation.add(message);
+ mXmppConnectionService.updateConversationUi();
+ if (size <= this.mJingleConnectionManager
+ .getAutoAcceptFileSize()) {
+ Log.d(Config.LOGTAG, "auto accepting file from "
+ + packet.getFrom());
+ this.acceptedAutomatically = true;
+ this.sendAccept();
} else {
- this.sendCancel();
- this.cancel();
+ message.markUnread();
+ Log.d(Config.LOGTAG,
+ "not auto accepting new file offer with size: "
+ + size
+ + " allowed size:"
+ + this.mJingleConnectionManager
+ .getAutoAcceptFileSize());
+ this.mXmppConnectionService.getNotificationService()
+ .push(message);
}
+ this.file = this.mXmppConnectionService.getFileBackend()
+ .getFile(message, false);
+ if (message.getEncryption() == Message.ENCRYPTION_OTR) {
+ byte[] key = conversation.getSymmetricKey();
+ if (key == null) {
+ this.sendCancel();
+ this.fail();
+ return;
+ } else {
+ this.file.setKey(key);
+ }
+ }
+ this.file.setExpectedSize(size);
} else {
this.sendCancel();
- this.cancel();
+ this.fail();
}
} else {
this.sendCancel();
- this.cancel();
+ this.fail();
}
}
@@ -353,7 +363,7 @@ public class JingleConnection implements Downloadable {
this.mXmppConnectionService.markMessage(this.message, Message.STATUS_OFFERED);
JinglePacket packet = this.bootstrapPacket("session-initiate");
Content content = new Content(this.contentCreator, this.contentName);
- if (message.getType() == Message.TYPE_IMAGE) {
+ if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
content.setTransportId(this.transportId);
this.file = this.mXmppConnectionService.getFileBackend().getFile(
message, false);
@@ -375,7 +385,7 @@ public class JingleConnection implements Downloadable {
}
private List<Element> getCandidatesAsElements() {
- List<Element> elements = new ArrayList<Element>();
+ List<Element> elements = new ArrayList<>();
for (JingleCandidate c : this.candidates) {
elements.add(c.toElement());
}
@@ -443,7 +453,7 @@ public class JingleConnection implements Downloadable {
private JinglePacket bootstrapPacket(String action) {
JinglePacket packet = new JinglePacket();
packet.setAction(action);
- packet.setFrom(account.getFullJid());
+ packet.setFrom(account.getJid());
packet.setTo(this.message.getCounterpart());
packet.setSessionId(this.sessionId);
packet.setInitiator(this.initiator);
@@ -484,7 +494,7 @@ public class JingleConnection implements Downloadable {
} else {
Log.d(Config.LOGTAG, "activated connection not found");
this.sendCancel();
- this.cancel();
+ this.fail();
}
}
return true;
@@ -531,8 +541,8 @@ public class JingleConnection implements Downloadable {
this.transport = connection;
if (connection == null) {
Log.d(Config.LOGTAG, "could not find suitable candidate");
- this.disconnect();
- if (this.initiator.equals(account.getFullJid())) {
+ this.disconnectSocks5Connections();
+ if (this.initiator.equals(account.getJid())) {
this.sendFallbackToIbb();
}
} else {
@@ -547,7 +557,7 @@ public class JingleConnection implements Downloadable {
activation.query("http://jabber.org/protocol/bytestreams")
.setAttribute("sid", this.getSessionId());
activation.query().addChild("activate")
- .setContent(this.getCounterPart());
+ .setContent(this.getCounterPart().toString());
this.account.getXmppConnection().sendIqPacket(activation,
new OnIqPacketReceived() {
@@ -570,7 +580,7 @@ public class JingleConnection implements Downloadable {
+ " was a proxy. waiting for other party to activate");
}
} else {
- if (initiator.equals(account.getFullJid())) {
+ if (initiator.equals(account.getJid())) {
Log.d(Config.LOGTAG, "we were initiating. sending file");
connection.send(file, onFileTransmissionSatusChanged);
} else {
@@ -600,7 +610,7 @@ public class JingleConnection implements Downloadable {
} else if (connection.getCandidate().getPriority() == currentConnection
.getCandidate().getPriority()) {
// Log.d(Config.LOGTAG,"found two candidates with same priority");
- if (initiator.equals(account.getFullJid())) {
+ if (initiator.equals(account.getJid())) {
if (currentConnection.getCandidate().isOurs()) {
connection = currentConnection;
}
@@ -622,7 +632,7 @@ public class JingleConnection implements Downloadable {
reason.addChild("success");
packet.setReason(reason);
this.sendJinglePacket(packet);
- this.disconnect();
+ this.disconnectSocks5Connections();
this.mJingleStatus = JINGLE_STATUS_FINISHED;
this.message.setStatus(Message.STATUS_RECEIVED);
this.message.setDownloadable(null);
@@ -653,8 +663,7 @@ public class JingleConnection implements Downloadable {
}
}
this.transportId = packet.getJingleContent().getTransportId();
- this.transport = new JingleInbandTransport(this.account,
- this.responder, this.transportId, this.ibbBlockSize);
+ this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
this.transport.receive(file, onFileTransmissionSatusChanged);
JinglePacket answer = bootstrapPacket("transport-accept");
Content content = new Content("initiator", "a-file-offer");
@@ -676,8 +685,7 @@ public class JingleConnection implements Downloadable {
this.ibbBlockSize = bs;
}
}
- this.transport = new JingleInbandTransport(this.account,
- this.responder, this.transportId, this.ibbBlockSize);
+ this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
this.transport.connect(new OnTransportConnected() {
@Override
@@ -701,20 +709,51 @@ public class JingleConnection implements Downloadable {
this.mJingleStatus = JINGLE_STATUS_FINISHED;
this.mXmppConnectionService.markMessage(this.message,
Message.STATUS_SEND);
- this.disconnect();
+ this.disconnectSocks5Connections();
+ if (this.transport != null && this.transport instanceof JingleInbandTransport) {
+ this.transport.disconnect();
+ }
+ this.message.setDownloadable(null);
this.mJingleConnectionManager.finishConnection(this);
}
public void cancel() {
- this.mJingleStatus = JINGLE_STATUS_CANCELED;
- this.disconnect();
+ this.disconnectSocks5Connections();
+ if (this.transport != null && this.transport instanceof JingleInbandTransport) {
+ this.transport.disconnect();
+ }
+ this.sendCancel();
+ this.mJingleConnectionManager.finishConnection(this);
+ if (this.responder.equals(account.getJid())) {
+ this.message.setDownloadable(new DownloadablePlaceholder(Downloadable.STATUS_FAILED));
+ if (this.file!=null) {
+ file.delete();
+ }
+ this.mXmppConnectionService.updateConversationUi();
+ } else {
+ this.mXmppConnectionService.markMessage(this.message,
+ Message.STATUS_SEND_FAILED);
+ this.message.setDownloadable(null);
+ }
+ }
+
+ private void fail() {
+ this.mJingleStatus = JINGLE_STATUS_FAILED;
+ this.disconnectSocks5Connections();
+ if (this.transport != null && this.transport instanceof JingleInbandTransport) {
+ this.transport.disconnect();
+ }
if (this.message != null) {
- if (this.responder.equals(account.getFullJid())) {
- this.mStatus = Downloadable.STATUS_FAILED;
+ if (this.responder.equals(account.getJid())) {
+ this.message.setDownloadable(new DownloadablePlaceholder(Downloadable.STATUS_FAILED));
+ if (this.file!=null) {
+ file.delete();
+ }
this.mXmppConnectionService.updateConversationUi();
} else {
this.mXmppConnectionService.markMessage(this.message,
Message.STATUS_SEND_FAILED);
+ this.message.setDownloadable(null);
}
}
this.mJingleConnectionManager.finishConnection(this);
@@ -763,7 +802,7 @@ public class JingleConnection implements Downloadable {
});
}
- private void disconnect() {
+ private void disconnectSocks5Connections() {
Iterator<Entry<String, JingleSocks5Transport>> it = this.connections
.entrySet().iterator();
while (it.hasNext()) {
@@ -810,11 +849,11 @@ public class JingleConnection implements Downloadable {
this.sendJinglePacket(packet);
}
- public String getInitiator() {
+ public Jid getInitiator() {
return this.initiator;
}
- public String getResponder() {
+ public Jid getResponder() {
return this.responder;
}
@@ -855,6 +894,14 @@ public class JingleConnection implements Downloadable {
return null;
}
+ public void updateProgress(int i) {
+ this.mProgress = i;
+ if (SystemClock.elapsedRealtime() - this.mLastGuiRefresh > Config.PROGRESS_UI_UPDATE_INTERVAL) {
+ this.mLastGuiRefresh = SystemClock.elapsedRealtime();
+ mXmppConnectionService.updateConversationUi();
+ }
+ }
+
interface OnProxyActivated {
public void success();
@@ -870,7 +917,7 @@ public class JingleConnection implements Downloadable {
}
public boolean start() {
- if (account.getStatus() == Account.STATUS_ONLINE) {
+ if (account.getStatus() == Account.State.ONLINE) {
if (mJingleStatus == JINGLE_STATUS_INITIATED) {
new Thread(new Runnable() {
@@ -899,4 +946,29 @@ public class JingleConnection implements Downloadable {
return 0;
}
}
+
+ @Override
+ public int getProgress() {
+ return this.mProgress;
+ }
+
+ @Override
+ public String getMimeType() {
+ if (this.message.getType() == Message.TYPE_FILE) {
+ String mime = null;
+ String path = this.message.getRelativeFilePath();
+ if (path != null && !this.message.getRelativeFilePath().isEmpty()) {
+ mime = URLConnection.guessContentTypeFromName(this.message.getRelativeFilePath());
+ if (mime!=null) {
+ return mime;
+ } else {
+ return "";
+ }
+ } else {
+ return "";
+ }
+ } else {
+ return "image/webp";
+ }
+ }
}
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java
index d937146a..72c960d8 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java
@@ -14,13 +14,15 @@ import eu.siacs.conversations.services.AbstractConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
+import eu.siacs.conversations.xmpp.jid.InvalidJidException;
+import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class JingleConnectionManager extends AbstractConnectionManager {
- private List<JingleConnection> connections = new CopyOnWriteArrayList<JingleConnection>();
+ private List<JingleConnection> connections = new CopyOnWriteArrayList<>();
- private HashMap<String, JingleCandidate> primaryCandidates = new HashMap<String, JingleCandidate>();
+ private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
@SuppressLint("TrulyRandom")
private SecureRandom random = new SecureRandom();
@@ -61,7 +63,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
return connection;
}
- public JingleConnection createNewConnection(JinglePacket packet) {
+ public JingleConnection createNewConnection(final JinglePacket packet) {
JingleConnection connection = new JingleConnection(this);
this.connections.add(connection);
return connection;
@@ -73,13 +75,17 @@ public class JingleConnectionManager extends AbstractConnectionManager {
public void getPrimaryCandidate(Account account,
final OnPrimaryCandidateFound listener) {
- if (!this.primaryCandidates.containsKey(account.getJid())) {
+ if (Config.NO_PROXY_LOOKUP) {
+ listener.onPrimaryCandidateFound(false, null);
+ return;
+ }
+ if (!this.primaryCandidates.containsKey(account.getJid().toBareJid())) {
String xmlns = "http://jabber.org/protocol/bytestreams";
final String proxy = account.getXmppConnection()
.findDiscoItemByFeature(xmlns);
if (proxy != null) {
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
- iq.setTo(proxy);
+ iq.setAttribute("to", proxy);
iq.query(xmlns);
account.getXmppConnection().sendIqPacket(iq,
new OnIqPacketReceived() {
@@ -101,9 +107,13 @@ public class JingleConnectionManager extends AbstractConnectionManager {
.getAttribute("port")));
candidate
.setType(JingleCandidate.TYPE_PROXY);
- candidate.setJid(proxy);
- candidate.setPriority(655360 + 65535);
- primaryCandidates.put(account.getJid(),
+ try {
+ candidate.setJid(Jid.fromString(proxy));
+ } catch (final InvalidJidException e) {
+ candidate.setJid(null);
+ }
+ candidate.setPriority(655360 + 65535);
+ primaryCandidates.put(account.getJid().toBareJid(),
candidate);
listener.onPrimaryCandidateFound(true,
candidate);
@@ -119,7 +129,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
} else {
listener.onPrimaryCandidateFound(true,
- this.primaryCandidates.get(account.getJid()));
+ this.primaryCandidates.get(account.getJid().toBareJid()));
}
}
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleInbandTransport.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleInbandTransport.java
index cc1e92f6..04b225d0 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleInbandTransport.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleInbandTransport.java
@@ -8,17 +8,19 @@ import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import android.util.Base64;
+
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
+import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class JingleInbandTransport extends JingleTransport {
private Account account;
- private String counterpart;
+ private Jid counterpart;
private int blockSize;
private int bufferSize;
private int seq = 0;
@@ -26,11 +28,15 @@ public class JingleInbandTransport extends JingleTransport {
private boolean established = false;
+ private boolean connected = true;
+
private DownloadableFile file;
+ private JingleConnection connection;
private InputStream fileInputStream = null;
- private OutputStream fileOutputStream;
- private long remainingSize;
+ private OutputStream fileOutputStream = null;
+ private long remainingSize = 0;
+ private long fileSize = 0;
private MessageDigest digest;
private OnFileTransmissionStatusChanged onFileTransmissionStatusChanged;
@@ -38,16 +44,16 @@ public class JingleInbandTransport extends JingleTransport {
private OnIqPacketReceived onAckReceived = new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
- if (packet.getType() == IqPacket.TYPE_RESULT) {
+ if (connected && packet.getType() == IqPacket.TYPE_RESULT) {
sendNextBlock();
}
}
};
- public JingleInbandTransport(Account account, String counterpart,
- String sid, int blocksize) {
- this.account = account;
- this.counterpart = counterpart;
+ public JingleInbandTransport(final JingleConnection connection, final String sid, final int blocksize) {
+ this.connection = connection;
+ this.account = connection.getAccount();
+ this.counterpart = connection.getCounterPart();
this.blockSize = blocksize;
this.bufferSize = blocksize / 4;
this.sessionId = sid;
@@ -60,7 +66,7 @@ public class JingleInbandTransport extends JingleTransport {
open.setAttribute("sid", this.sessionId);
open.setAttribute("stanza", "iq");
open.setAttribute("block-size", Integer.toString(this.blockSize));
-
+ this.connected = true;
this.account.getXmppConnection().sendIqPacket(iq,
new OnIqPacketReceived() {
@@ -91,13 +97,11 @@ public class JingleInbandTransport extends JingleTransport {
callback.onFileTransferAborted();
return;
}
- this.remainingSize = file.getExpectedSize();
- } catch (NoSuchAlgorithmException e) {
- callback.onFileTransferAborted();
- } catch (IOException e) {
+ this.remainingSize = this.fileSize = file.getExpectedSize();
+ } catch (final NoSuchAlgorithmException | IOException e) {
callback.onFileTransferAborted();
}
- }
+ }
@Override
public void send(DownloadableFile file,
@@ -105,6 +109,8 @@ public class JingleInbandTransport extends JingleTransport {
this.onFileTransmissionStatusChanged = callback;
this.file = file;
try {
+ this.remainingSize = this.file.getSize();
+ this.fileSize = this.remainingSize;
this.digest = MessageDigest.getInstance("SHA-1");
this.digest.reset();
fileInputStream = this.file.createInputStream();
@@ -112,12 +118,33 @@ public class JingleInbandTransport extends JingleTransport {
callback.onFileTransferAborted();
return;
}
- this.sendNextBlock();
+ if (this.connected) {
+ this.sendNextBlock();
+ }
} catch (NoSuchAlgorithmException e) {
callback.onFileTransferAborted();
}
}
+ @Override
+ public void disconnect() {
+ this.connected = false;
+ if (this.fileOutputStream != null) {
+ try {
+ this.fileOutputStream.close();
+ } catch (IOException e) {
+
+ }
+ }
+ if (this.fileInputStream != null) {
+ try {
+ this.fileInputStream.close();
+ } catch (IOException e) {
+
+ }
+ }
+ }
+
private void sendNextBlock() {
byte[] buffer = new byte[this.bufferSize];
try {
@@ -127,6 +154,7 @@ public class JingleInbandTransport extends JingleTransport {
fileInputStream.close();
this.onFileTransmissionStatusChanged.onFileTransmitted(file);
} else {
+ this.remainingSize -= count;
this.digest.update(buffer);
String base64 = Base64.encodeToString(buffer, Base64.NO_WRAP);
IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
@@ -141,6 +169,7 @@ public class JingleInbandTransport extends JingleTransport {
this.account.getXmppConnection().sendIqPacket(iq,
this.onAckReceived);
this.seq++;
+ connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
}
} catch (IOException e) {
this.onFileTransmissionStatusChanged.onFileTransferAborted();
@@ -156,6 +185,7 @@ public class JingleInbandTransport extends JingleTransport {
}
this.remainingSize -= buffer.length;
+
this.fileOutputStream.write(buffer);
this.digest.update(buffer);
@@ -164,6 +194,8 @@ public class JingleInbandTransport extends JingleTransport {
fileOutputStream.flush();
fileOutputStream.close();
this.onFileTransmissionStatusChanged.onFileTransmitted(file);
+ } else {
+ connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
}
} catch (IOException e) {
this.onFileTransmissionStatusChanged.onFileTransferAborted();
@@ -174,13 +206,14 @@ public class JingleInbandTransport extends JingleTransport {
if (payload.getName().equals("open")) {
if (!established) {
established = true;
+ connected = true;
this.account.getXmppConnection().sendIqPacket(
packet.generateRespone(IqPacket.TYPE_RESULT), null);
} else {
this.account.getXmppConnection().sendIqPacket(
packet.generateRespone(IqPacket.TYPE_ERROR), null);
}
- } else if (payload.getName().equals("data")) {
+ } else if (connected && payload.getName().equals("data")) {
this.receiveNextBlock(payload.getContent());
this.account.getXmppConnection().sendIqPacket(
packet.generateRespone(IqPacket.TYPE_RESULT), null);
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleSocks5Transport.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleSocks5Transport.java
index 1da2f0cd..c3419580 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleSocks5Transport.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleSocks5Transport.java
@@ -15,6 +15,7 @@ import eu.siacs.conversations.utils.CryptoHelper;
public class JingleSocks5Transport extends JingleTransport {
private JingleCandidate candidate;
+ private JingleConnection connection;
private String destination;
private OutputStream outputStream;
private InputStream inputStream;
@@ -25,16 +26,17 @@ public class JingleSocks5Transport extends JingleTransport {
public JingleSocks5Transport(JingleConnection jingleConnection,
JingleCandidate candidate) {
this.candidate = candidate;
+ this.connection = jingleConnection;
try {
MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
StringBuilder destBuilder = new StringBuilder();
destBuilder.append(jingleConnection.getSessionId());
if (candidate.isOurs()) {
- destBuilder.append(jingleConnection.getAccount().getFullJid());
+ destBuilder.append(jingleConnection.getAccount().getJid());
destBuilder.append(jingleConnection.getCounterPart());
} else {
destBuilder.append(jingleConnection.getCounterPart());
- destBuilder.append(jingleConnection.getAccount().getFullJid());
+ destBuilder.append(jingleConnection.getAccount().getJid());
}
mDigest.reset();
this.destination = CryptoHelper.bytesToHex(mDigest
@@ -102,11 +104,15 @@ public class JingleSocks5Transport extends JingleTransport {
callback.onFileTransferAborted();
return;
}
+ long size = file.getSize();
+ long transmitted = 0;
int count;
byte[] buffer = new byte[8192];
while ((count = fileInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);
digest.update(buffer, 0, count);
+ transmitted += count;
+ connection.updateProgress((int) ((((double) transmitted) / size) * 100));
}
outputStream.flush();
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
@@ -151,6 +157,7 @@ public class JingleSocks5Transport extends JingleTransport {
callback.onFileTransferAborted();
return;
}
+ double size = file.getExpectedSize();
long remainingSize = file.getExpectedSize();
byte[] buffer = new byte[8192];
int count = buffer.length;
@@ -164,6 +171,7 @@ public class JingleSocks5Transport extends JingleTransport {
digest.update(buffer, 0, count);
remainingSize -= count;
}
+ connection.updateProgress((int) (((size - remainingSize) / size) * 100));
}
fileOutputStream.flush();
fileOutputStream.close();
@@ -189,6 +197,20 @@ public class JingleSocks5Transport extends JingleTransport {
}
public void disconnect() {
+ if (this.outputStream != null) {
+ try {
+ this.outputStream.close();
+ } catch (IOException e) {
+
+ }
+ }
+ if (this.inputStream != null) {
+ try {
+ this.inputStream.close();
+ } catch (IOException e) {
+
+ }
+ }
if (this.socket != null) {
try {
this.socket.close();
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleTransport.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleTransport.java
index 1374e61c..e832d3f5 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleTransport.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleTransport.java
@@ -10,4 +10,6 @@ public abstract class JingleTransport {
public abstract void send(final DownloadableFile file,
final OnFileTransmissionStatusChanged callback);
+
+ public abstract void disconnect();
}
diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java
index 77a73643..4f73a83a 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java
@@ -1,6 +1,7 @@
package eu.siacs.conversations.xmpp.jingle.stanzas;
import eu.siacs.conversations.xml.Element;
+import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class JinglePacket extends IqPacket {
@@ -85,8 +86,8 @@ public class JinglePacket extends IqPacket {
return this.jingle.getAttribute("action");
}
- public void setInitiator(String initiator) {
- this.jingle.setAttribute("initiator", initiator);
+ public void setInitiator(final Jid initiator) {
+ this.jingle.setAttribute("initiator", initiator.toString());
}
public boolean isAction(String action) {
diff --git a/src/main/java/eu/siacs/conversations/xmpp/pep/Avatar.java b/src/main/java/eu/siacs/conversations/xmpp/pep/Avatar.java
index 154fadf6..9f5ac988 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/pep/Avatar.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/pep/Avatar.java
@@ -1,6 +1,8 @@
package eu.siacs.conversations.xmpp.pep;
import eu.siacs.conversations.xml.Element;
+import eu.siacs.conversations.xmpp.jid.Jid;
+
import android.util.Base64;
public class Avatar {
@@ -10,7 +12,7 @@ public class Avatar {
public int height;
public int width;
public long size;
- public String owner;
+ public Jid owner;
public byte[] getImageAsBytes() {
return Base64.decode(image, Base64.DEFAULT);
diff --git a/src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java b/src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java
index eef41c79..eade220a 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/stanzas/AbstractStanza.java
@@ -1,6 +1,8 @@
package eu.siacs.conversations.xmpp.stanzas;
import eu.siacs.conversations.xml.Element;
+import eu.siacs.conversations.xmpp.jid.InvalidJidException;
+import eu.siacs.conversations.xmpp.jid.Jid;
public class AbstractStanza extends Element {
@@ -8,27 +10,40 @@ public class AbstractStanza extends Element {
super(name);
}
- public String getTo() {
- return getAttribute("to");
+ public Jid getTo() {
+ try {
+ return Jid.fromString(getAttribute("to"));
+ } catch (final InvalidJidException e) {
+ return null;
+ }
}
- public String getFrom() {
- return getAttribute("from");
+ public Jid getFrom() {
+ String from = getAttribute("from");
+ if (from == null) {
+ return null;
+ } else {
+ try {
+ return Jid.fromString(from);
+ } catch (final InvalidJidException e) {
+ return null;
+ }
+ }
}
public String getId() {
return this.getAttribute("id");
}
- public void setTo(String to) {
- setAttribute("to", to);
+ public void setTo(final Jid to) {
+ setAttribute("to", to.toString());
}
- public void setFrom(String from) {
- setAttribute("from", from);
+ public void setFrom(final Jid from) {
+ setAttribute("from", from.toString());
}
- public void setId(String id) {
+ public void setId(final String id) {
setAttribute("id", id);
}
}