diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-04-02 22:53:01 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-04-02 23:09:05 +0200 |
commit | 621217973065960e3347262f507d7c3b39326464 (patch) | |
tree | eba1fdd5ca5da1e7e5a41c8a4466749e04332f98 /src/main/java/de/pixart/messenger/xmpp | |
parent | 95521e80c1c029b02eddf5799cd23d0268ae4a1c (diff) |
migrate to xmpp-addr
Diffstat (limited to 'src/main/java/de/pixart/messenger/xmpp')
12 files changed, 152 insertions, 439 deletions
diff --git a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java index 154627afe..9d8dd48ee 100644 --- a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java @@ -84,8 +84,6 @@ import de.pixart.messenger.xml.TagWriter; import de.pixart.messenger.xml.XmlReader; import de.pixart.messenger.xmpp.forms.Data; import de.pixart.messenger.xmpp.forms.Field; -import de.pixart.messenger.xmpp.jid.InvalidJidException; -import de.pixart.messenger.xmpp.jid.Jid; import de.pixart.messenger.xmpp.jingle.OnJinglePacketReceived; import de.pixart.messenger.xmpp.jingle.stanzas.JinglePacket; import de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza; @@ -99,6 +97,7 @@ import de.pixart.messenger.xmpp.stanzas.streammgmt.AckPacket; import de.pixart.messenger.xmpp.stanzas.streammgmt.EnablePacket; import de.pixart.messenger.xmpp.stanzas.streammgmt.RequestPacket; import de.pixart.messenger.xmpp.stanzas.streammgmt.ResumePacket; +import rocks.xmpp.addr.Jid; public class XmppConnection implements Runnable { @@ -184,7 +183,7 @@ public class XmppConnection implements Runnable { public XmppConnection(final Account account, final XmppConnectionService service) { this.account = account; - final String tag = account.getJid().toBareJid().toPreppedString(); + final String tag = account.getJid().asBareJid().toString(); mXmppConnectionService = service; } @@ -192,7 +191,7 @@ public class XmppConnection implements Runnable { synchronized (this) { this.mThread = Thread.currentThread(); if (this.mThread.isInterrupted()) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": not changing status to " + nextStatus + " because thread was interrupted"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": not changing status to " + nextStatus + " because thread was interrupted"); return; } if (account.getStatus() != nextStatus) { @@ -235,7 +234,7 @@ public class XmppConnection implements Runnable { if (mXmppConnectionService.areMessagesInitialized()) { mXmppConnectionService.resetSendingToWaiting(account); } - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting"); + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": connecting"); features.encryptionEnabled = false; inSmacksSession = false; isBound = false; @@ -252,7 +251,7 @@ public class XmppConnection implements Runnable { Integer Port = XMPP_Port[new Random().nextInt(XMPP_Port.length)]; localSocket = new Socket(); try { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": connect to " + Config.XMPP_IP + ":" + Port); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": connect to " + Config.XMPP_IP + ":" + Port); localSocket.connect(new InetSocketAddress(Config.XMPP_IP, Port), Config.SOCKET_TIMEOUT * 1000); } catch (IOException e) { throw new UnknownHostException(); @@ -260,7 +259,7 @@ public class XmppConnection implements Runnable { try { startXmpp(localSocket); } catch (InterruptedException e) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream"); + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": thread was interrupted before beginning stream"); return; } catch (Exception e) { throw new IOException(e.getMessage()); @@ -273,12 +272,12 @@ public class XmppConnection implements Runnable { destination = account.getHostname(); this.verifiedHostname = destination; } - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": connect to " + destination + " via Tor"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": connect to " + destination + " via Tor"); localSocket = SocksSocketFactory.createSocketOverTor(destination, account.getPort()); try { startXmpp(localSocket); } catch (InterruptedException e) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream"); + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": thread was interrupted before beginning stream"); return; } catch (Exception e) { throw new IOException(e.getMessage()); @@ -296,9 +295,9 @@ public class XmppConnection implements Runnable { localSocket = tlsFactoryVerifier.factory.createSocket(); localSocket.connect(address, Config.SOCKET_TIMEOUT * 1000); final SSLSession session = ((SSLSocket) localSocket).getSession(); - final String domain = account.getJid().getDomainpart(); + final String domain = account.getJid().getDomain(); if (!tlsFactoryVerifier.verifier.verify(domain, this.verifiedHostname, session)) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS certificate verification failed"); throw new StateChangingException(Account.State.TLS_ERROR); } } catch (KeyManagementException e) { @@ -314,7 +313,7 @@ public class XmppConnection implements Runnable { try { startXmpp(localSocket); } catch (InterruptedException e) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream"); + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": thread was interrupted before beginning stream"); return; } catch (Exception e) { throw new IOException(e.getMessage()); @@ -329,20 +328,20 @@ public class XmppConnection implements Runnable { try { startXmpp(localSocket); } catch (InterruptedException e) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream"); + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": thread was interrupted before beginning stream"); return; } catch (Exception e) { throw new IOException(e.getMessage()); } } else { - final String domain = account.getJid().getDomainpart(); - List<Resolver.Result> results = Resolver.resolve(account.getJid().getDomainpart()); + final String domain = account.getJid().getDomain(); + List<Resolver.Result> results = Resolver.resolve(account.getJid().getDomain()); Resolver.Result storedBackupResult; if (!Thread.currentThread().isInterrupted()) { storedBackupResult = mXmppConnectionService.databaseBackend.findResolverResult(domain); if (storedBackupResult != null && !results.contains(storedBackupResult)) { results.add(storedBackupResult); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": loaded backup resolver result from db: " + storedBackupResult); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": loaded backup resolver result from db: " + storedBackupResult); } } else { storedBackupResult = null; @@ -350,7 +349,7 @@ public class XmppConnection implements Runnable { for (Iterator<Resolver.Result> iterator = results.iterator(); iterator.hasNext(); ) { final Resolver.Result result = iterator.next(); if (Thread.currentThread().isInterrupted()) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": Thread was interrupted"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Thread was interrupted"); return; } try { @@ -360,12 +359,12 @@ public class XmppConnection implements Runnable { final InetSocketAddress addr; if (result.getIp() != null) { addr = new InetSocketAddress(result.getIp(), result.getPort()); - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": using values from dns " + result.getHostname().toString() + "/" + result.getIp().getHostAddress() + ":" + result.getPort() + " tls: " + features.encryptionEnabled); } else { addr = new InetSocketAddress(IDN.toASCII(result.getHostname().toString()), result.getPort()); - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": using values from dns " + result.getHostname().toString() + ":" + result.getPort() + " tls: " + features.encryptionEnabled); } @@ -382,13 +381,13 @@ public class XmppConnection implements Runnable { } SSLSocketHelper.setSecurity((SSLSocket) localSocket); - SSLSocketHelper.setSNIHost(tlsFactoryVerifier.factory, (SSLSocket) localSocket, account.getServer().getDomainpart()); + SSLSocketHelper.setSNIHost(tlsFactoryVerifier.factory, (SSLSocket) localSocket, account.getServer().getDomain()); SSLSocketHelper.setAlpnProtocol(tlsFactoryVerifier.factory, (SSLSocket) localSocket, "xmpp-client"); localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000); - if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomainpart(), verifiedHostname, ((SSLSocket) localSocket).getSession())) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed"); + if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomain(), verifiedHostname, ((SSLSocket) localSocket).getSession())) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS certificate verification failed"); if (!iterator.hasNext()) { throw new StateChangingException(Account.State.TLS_ERROR); } @@ -406,10 +405,10 @@ public class XmppConnection implements Runnable { } catch (final StateChangingException e) { throw e; } catch (InterruptedException e) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream"); + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": thread was interrupted before beginning stream"); return; } catch (final Throwable e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage() + "(" + e.getClass().getName() + ")"); + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": " + e.getMessage() + "(" + e.getClass().getName() + ")"); if (!iterator.hasNext()) { throw new UnknownHostException(); } @@ -428,14 +427,14 @@ public class XmppConnection implements Runnable { } catch (final SocksSocketFactory.SocksProxyNotFoundException e) { this.changeStatus(Account.State.TOR_NOT_AVAILABLE); } catch (final IOException | XmlPullParserException | NoSuchAlgorithmException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage()); + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": " + e.getMessage()); this.changeStatus(Account.State.OFFLINE); this.attempt = Math.max(0, this.attempt - 1); } finally { if (!Thread.currentThread().isInterrupted()) { forceCloseSocket(); } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": not force closing socket because thread was interrupted"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": not force closing socket because thread was interrupted"); } } } @@ -472,7 +471,7 @@ public class XmppConnection implements Runnable { } else { keyManager = null; } - String domain = account.getJid().getDomainpart(); + String domain = account.getJid().getDomain(); sc.init(keyManager, new X509TrustManager[]{mInteractive ? trustManager.getInteractive(domain) : trustManager.getNonInteractive(domain)}, mXmppConnectionService.getRNG()); final SSLSocketFactory factory = sc.getSocketFactory(); final DomainHostnameVerifier verifier = trustManager.wrapHostnameVerifier(new XmppDomainVerifier(), mInteractive); @@ -483,7 +482,7 @@ public class XmppConnection implements Runnable { public void run() { synchronized (this) { if (Thread.currentThread().isInterrupted()) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": aborting connect because thread was interrupted"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": aborting connect because thread was interrupted"); return; } forceCloseSocket(); @@ -510,7 +509,7 @@ public class XmppConnection implements Runnable { Log.e(Config.LOGTAG, String.valueOf(e)); throw new StateChangingException(Account.State.UNAUTHORIZED); } - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in"); + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": logged in"); account.setKey(Account.PINNED_MECHANISM_KEY, String.valueOf(saslMechanism.getPriority())); tagReader.reset(); @@ -560,11 +559,11 @@ public class XmppConnection implements Runnable { final Element enabled = tagReader.readElement(nextTag); if ("true".equals(enabled.getAttribute("resume"))) { this.streamId = enabled.getAttribute("id"); - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": stream management(" + smVersion + ") enabled (resumable)"); } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": stream management(" + smVersion + ") enabled"); } this.stanzasReceived = 0; @@ -583,11 +582,11 @@ public class XmppConnection implements Runnable { synchronized (this.mStanzaQueue) { final int serverCount = Integer.parseInt(h); if (serverCount < stanzasSent) { - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": session resumed with lost packages"); stanzasSent = serverCount; } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed"); + Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": session resumed"); } acknowledgeStanzaUpTo(serverCount); for (int i = 0; i < this.mStanzaQueue.size(); ++i) { @@ -600,7 +599,7 @@ public class XmppConnection implements Runnable { if (packet instanceof MessagePacket) { MessagePacket message = (MessagePacket) packet; mXmppConnectionService.markMessage(account, - message.getTo().toBareJid(), + message.getTo().asBareJid(), message.getId(), Message.STATUS_UNSEND); } @@ -608,12 +607,12 @@ public class XmppConnection implements Runnable { } } catch (final NumberFormatException ignored) { } - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": online with resource " + account.getResource()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": online with resource " + account.getResource()); changeStatus(Account.State.ONLINE); } else if (nextTag.isStart("r")) { tagReader.readElement(nextTag); if (Config.EXTENDED_SM_LOGGING) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": acknowledging stanza #" + this.stanzasReceived); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": acknowledging stanza #" + this.stanzasReceived); } final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion); tagWriter.writeStanzaAsync(ack); @@ -622,7 +621,7 @@ public class XmppConnection implements Runnable { synchronized (NotificationService.CATCHUP_LOCK) { if (mWaitingForSmCatchup.compareAndSet(true, false)) { int count = mSmCatchupMessageCounter.get(); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": SM catchup complete (" + count + ")"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": SM catchup complete (" + count + ")"); accountUiNeedsRefresh = true; if (count > 0) { mXmppConnectionService.getNotificationService().finishBacklog(true, account); @@ -640,18 +639,18 @@ public class XmppConnection implements Runnable { acknowledgeStanzaUpTo(serverSequence); } } catch (NumberFormatException | NullPointerException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server send ack without sequence number"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server send ack without sequence number"); } } else if (nextTag.isStart("failed")) { Element failed = tagReader.readElement(nextTag); try { final int serverCount = Integer.parseInt(failed.getAttribute("h")); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed but server acknowledged stanza #" + serverCount); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed but server acknowledged stanza #" + serverCount); synchronized (this.mStanzaQueue) { acknowledgeStanzaUpTo(serverCount); } } catch (NumberFormatException | NullPointerException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed"); } resetStreamId(); sendBindRequest(); @@ -676,7 +675,7 @@ public class XmppConnection implements Runnable { for (int i = 0; i < mStanzaQueue.size(); ++i) { if (serverCount >= mStanzaQueue.keyAt(i)) { if (Config.EXTENDED_SM_LOGGING) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server acknowledged stanza #" + mStanzaQueue.keyAt(i)); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server acknowledged stanza #" + mStanzaQueue.keyAt(i)); } AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i); if (stanza instanceof MessagePacket && acknowledgedListener != null) { @@ -735,7 +734,7 @@ public class XmppConnection implements Runnable { if (inSmacksSession) { ++stanzasReceived; } else if (features.sm()) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": not counting stanza(" + element.getClass().getSimpleName() + "). Not in smacks session."); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": not counting stanza(" + element.getClass().getSimpleName() + "). Not in smacks session."); } lastPacketReceived = SystemClock.elapsedRealtime(); if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) { @@ -766,14 +765,14 @@ public class XmppConnection implements Runnable { callback = packetCallbackDuple.second; packetCallbacks.remove(packet.getId()); } else { - Log.e(Config.LOGTAG, account.getJid().toBareJid().toString() + ": ignoring spoofed iq packet"); + Log.e(Config.LOGTAG, account.getJid().asBareJid().toString() + ": ignoring spoofed iq packet"); } } else { if (packet.getFrom() != null && packet.getFrom().equals(packetCallbackDuple.first.getTo())) { callback = packetCallbackDuple.second; packetCallbacks.remove(packet.getId()); } else { - Log.e(Config.LOGTAG, account.getJid().toBareJid().toString() + ": ignoring spoofed iq packet"); + Log.e(Config.LOGTAG, account.getJid().asBareJid().toString() + ": ignoring spoofed iq packet"); } } } else if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) { @@ -824,14 +823,14 @@ public class XmppConnection implements Runnable { SSLSocketHelper.setSecurity(sslSocket); - if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomainpart(), this.verifiedHostname, sslSocket.getSession())) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed"); + if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomain(), this.verifiedHostname, sslSocket.getSession())) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS certificate verification failed"); throw new StateChangingException(Account.State.TLS_ERROR); } tagReader.setInputStream(sslSocket.getInputStream()); tagWriter.setOutputStream(sslSocket.getOutputStream()); sendStartStream(); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS connection established"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS connection established"); features.encryptionEnabled = true; final Tag tag = tagReader.readTag(); if (tag != null && tag.isStart("stream")) { @@ -841,7 +840,7 @@ public class XmppConnection implements Runnable { } sslSocket.close(); } catch (final NoSuchAlgorithmException | KeyManagementException e1) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": TLS certificate verification failed"); throw new StateChangingException(Account.State.TLS_ERROR); } } @@ -864,7 +863,7 @@ public class XmppConnection implements Runnable { authenticate(); } else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:" + smVersion) && streamId != null) { if (Config.EXTENDED_SM_LOGGING) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resuming after stanza #" + stanzasReceived); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resuming after stanza #" + stanzasReceived); } final ResumePacket resume = new ResumePacket(this.streamId, stanzasReceived, smVersion); this.mSmCatchupMessageCounter.set(0); @@ -889,7 +888,7 @@ public class XmppConnection implements Runnable { saslMechanism = new ScramSha256(tagWriter, account, mXmppConnectionService.getRNG()); } else if (mechanisms.contains("SCRAM-SHA-1")) { saslMechanism = new ScramSha1(tagWriter, account, mXmppConnectionService.getRNG()); - } else if (mechanisms.contains("PLAIN") && !account.getJid().getDomainpart().equals("nimbuzz.com")) { + } else if (mechanisms.contains("PLAIN") && !account.getJid().getDomain().equals("nimbuzz.com")) { saslMechanism = new Plain(tagWriter, account); } else if (mechanisms.contains("DIGEST-MD5")) { saslMechanism = new DigestMd5(tagWriter, account, mXmppConnectionService.getRNG()); @@ -946,7 +945,7 @@ public class XmppConnection implements Runnable { final Element password = new Element("password").setContent(account.getPassword()); register.query("jabber:iq:register").addChild(username); register.query().addChild(password); - register.setFrom(account.getJid().toBareJid()); + register.setFrom(account.getJid().asBareJid()); sendUnmodifiedIqPacket(register, registrationResponseListener, true); } else if (query.hasChild("x", Namespace.DATA)) { final Data data = Data.parse(query.findChild("x", Namespace.DATA)); @@ -1035,7 +1034,7 @@ public class XmppConnection implements Runnable { try { mXmppConnectionService.restoredFromDatabaseLatch.await(); } catch (InterruptedException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": interrupted while waiting for DB restore during bind"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": interrupted while waiting for DB restore during bind"); return; } clearIqCallbacks(); @@ -1055,13 +1054,13 @@ public class XmppConnection implements Runnable { final Element jid = bind.findChild("jid"); if (jid != null && jid.getContent() != null) { try { - Jid assignedJid = Jid.fromString(jid.getContent()); - if (!account.getJid().getDomainpart().equals(assignedJid.getDomainpart())) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server tried to re-assign domain to " + assignedJid.getDomainpart()); + Jid assignedJid = Jid.of(jid.getContent()); + if (!account.getJid().getDomain().equals(assignedJid.getDomain())) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server tried to re-assign domain to " + assignedJid.getDomain()); throw new StateChangingError(Account.State.BIND_FAILURE); } if (account.setJid(assignedJid)) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": jid changed during bind. updating database"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": jid changed during bind. updating database"); mXmppConnectionService.databaseBackend.updateAccount(account); } if (streamFeatures.hasChild("session") @@ -1071,8 +1070,8 @@ public class XmppConnection implements Runnable { sendPostBindInitialization(); } return; - } catch (final InvalidJidException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server reported invalid jid (" + jid.getContent() + ") on bind"); + } catch (final IllegalArgumentException e) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server reported invalid jid (" + jid.getContent() + ") on bind"); } } else { Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure. (no jid)"); @@ -1095,7 +1094,7 @@ public class XmppConnection implements Runnable { if (this.packetCallbacks.size() == 0) { return; } - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": clearing " + this.packetCallbacks.size() + " iq callbacks"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": clearing " + this.packetCallbacks.size() + " iq callbacks"); final Iterator<Pair<IqPacket, OnIqPacketReceived>> iterator = this.packetCallbacks.values().iterator(); while (iterator.hasNext()) { Pair<IqPacket, OnIqPacketReceived> entry = iterator.next(); @@ -1107,22 +1106,22 @@ public class XmppConnection implements Runnable { try { callback.onIqPacketReceived(account, failurePacket); } catch (StateChangingError error) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": caught StateChangingError(" + error.state.toString() + ") while clearing callbacks"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": caught StateChangingError(" + error.state.toString() + ") while clearing callbacks"); //ignore } } - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": done clearing iq callbacks. " + this.packetCallbacks.size() + " left"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": done clearing iq callbacks. " + this.packetCallbacks.size() + " left"); } public void sendDiscoTimeout() { if (mWaitForDisco.compareAndSet(true, false)) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": finalizing bind after disco timeout"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": finalizing bind after disco timeout"); finalizeBind(); } } private void sendStartSession() { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending legacy session to outdated server"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending legacy session to outdated server"); final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET); startSession.addChild("session", "urn:ietf:params:xml:ns:xmpp-session"); this.sendUnmodifiedIqPacket(startSession, (account, packet) -> { @@ -1154,10 +1153,10 @@ public class XmppConnection implements Runnable { synchronized (this.disco) { this.disco.clear(); } - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": starting service discovery"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": starting service discovery"); mPendingServiceDiscoveries.set(0); - if (smVersion == 0 || Patches.DISCO_EXCEPTIONS.contains(account.getJid().getDomainpart())) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": do not wait for service discovery"); + if (smVersion == 0 || Patches.DISCO_EXCEPTIONS.contains(account.getJid().getDomain())) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": do not wait for service discovery"); mWaitForDisco.set(false); } else { mWaitForDisco.set(true); @@ -1178,10 +1177,10 @@ public class XmppConnection implements Runnable { if (discoveryResult == null) { sendServiceDiscoveryInfo(account.getServer()); } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server caps came from cache"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server caps came from cache"); disco.put(account.getServer(), discoveryResult); } - sendServiceDiscoveryInfo(account.getJid().toBareJid()); + sendServiceDiscoveryInfo(account.getJid().asBareJid()); if (!requestDiscoItemsFirst) { sendServiceDiscoveryItems(account.getServer()); } @@ -1210,13 +1209,13 @@ public class XmppConnection implements Runnable { } disco.put(jid, result); advancedStreamFeaturesLoaded = disco.containsKey(account.getServer()) - && disco.containsKey(account.getJid().toBareJid()); + && disco.containsKey(account.getJid().asBareJid()); } - if (advancedStreamFeaturesLoaded && (jid.equals(account.getServer()) || jid.equals(account.getJid().toBareJid()))) { + if (advancedStreamFeaturesLoaded && (jid.equals(account.getServer()) || jid.equals(account.getJid().asBareJid()))) { enableAdvancedStreamFeatures(); } } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not query disco info for " + jid.toString()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not query disco info for " + jid.toString()); } if (packet.getType() != IqPacket.TYPE.TIMEOUT) { if (mPendingServiceDiscoveries.decrementAndGet() == 0 @@ -1229,7 +1228,7 @@ public class XmppConnection implements Runnable { } private void finalizeBind() { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": online with resource " + account.getResource()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": online with resource " + account.getResource()); if (bindListener != null) { bindListener.onBind(account); } @@ -1241,7 +1240,7 @@ public class XmppConnection implements Runnable { sendEnableCarbons(); } if (getFeatures().blocking() && !features.blockListRequested) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": Requesting block list"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": Requesting block list"); this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser()); } for (final OnAdvancedStreamFeaturesLoaded listener : advancedStreamFeaturesLoadedListeners) { @@ -1252,7 +1251,7 @@ public class XmppConnection implements Runnable { private void sendServiceDiscoveryItems(final Jid server) { mPendingServiceDiscoveries.incrementAndGet(); final IqPacket iq = new IqPacket(IqPacket.TYPE.GET); - iq.setTo(server.toDomainJid()); + iq.setTo(Jid.ofDomain(server.getDomain())); iq.query("http://jabber.org/protocol/disco#items"); this.sendIqPacket(iq, new OnIqPacketReceived() { @@ -1273,7 +1272,7 @@ public class XmppConnection implements Runnable { sendServiceDiscoveryInfo(jid); } } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not query disco items of " + server); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not query disco items of " + server); } if (packet.getType() != IqPacket.TYPE.TIMEOUT) { if (mPendingServiceDiscoveries.decrementAndGet() == 0 @@ -1293,11 +1292,11 @@ public class XmppConnection implements Runnable { @Override public void onIqPacketReceived(final Account account, final IqPacket packet) { if (!packet.hasChild("error")) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": successfully enabled carbons"); features.carbonsEnabled = true; } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error enableing carbons " + packet.toString()); } } @@ -1311,14 +1310,14 @@ public class XmppConnection implements Runnable { } if (streamError.hasChild("conflict")) { account.setResource(createNewResource()); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": switching resource due to conflict (" + account.getResource() + ")"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": switching resource due to conflict (" + account.getResource() + ")"); throw new IOException(); } else if (streamError.hasChild("host-unknown")) { throw new StateChangingException(Account.State.HOST_UNKNOWN); } else if (streamError.hasChild("policy-violation")) { throw new StateChangingException(Account.State.POLICY_VIOLATION); } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": stream error " + streamError.toString()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": stream error " + streamError.toString()); throw new StateChangingException(Account.State.STREAM_ERROR); } } @@ -1385,7 +1384,7 @@ public class XmppConnection implements Runnable { if (force || isBound) { tagWriter.writeStanzaAsync(packet); } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + " do not write stanza to unbound stream " + packet.toString()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + " do not write stanza to unbound stream " + packet.toString()); } if (packet instanceof AbstractAcknowledgeableStanza) { AbstractAcknowledgeableStanza stanza = (AbstractAcknowledgeableStanza) packet; @@ -1399,7 +1398,7 @@ public class XmppConnection implements Runnable { this.mStanzaQueue.append(stanzasSent, stanza); if (stanza instanceof MessagePacket && stanza.getId() != null && inSmacksSession) { if (Config.EXTENDED_SM_LOGGING) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": requesting ack for message stanza #" + stanzasSent); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": requesting ack for message stanza #" + stanzasSent); } tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion)); } @@ -1460,10 +1459,10 @@ public class XmppConnection implements Runnable { try { socket.close(); } catch (IOException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": io exception " + e.getMessage() + " during force close"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": io exception " + e.getMessage() + " during force close"); } } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": socket was null during force close"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": socket was null during force close"); } } @@ -1475,7 +1474,7 @@ public class XmppConnection implements Runnable { public void disconnect(final boolean force) { interrupt(); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force=" + Boolean.valueOf(force)); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": disconnecting force=" + Boolean.valueOf(force)); if (force) { forceCloseSocket(); } else { @@ -1486,19 +1485,19 @@ public class XmppConnection implements Runnable { final CountDownLatch streamCountDownLatch = this.mStreamCountDownLatch; try { currentTagWriter.await(1, TimeUnit.SECONDS); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": closing stream"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": closing stream"); currentTagWriter.writeTag(Tag.end("stream:stream")); if (streamCountDownLatch != null) { if (streamCountDownLatch.await(1, TimeUnit.SECONDS)) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": remote ended stream"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": remote ended stream"); } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": remote has not closed socket. force closing"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": remote has not closed socket. force closing"); } } } catch (InterruptedException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": interrupted while gracefully closing stream"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": interrupted while gracefully closing stream"); } catch (final IOException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": io exception during disconnect (" + e.getMessage() + ")"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": io exception during disconnect (" + e.getMessage() + ")"); } finally { FileBackend.close(currentSocket); } @@ -1611,7 +1610,7 @@ public class XmppConnection implements Runnable { public Identity getServerIdentity() { synchronized (this.disco) { - ServiceDiscoveryResult result = disco.get(account.getJid().toDomainJid()); + ServiceDiscoveryResult result = disco.get(Jid.ofDomain(account.getJid().getDomain())); if (result == null) { return Identity.UNKNOWN; } @@ -1764,38 +1763,38 @@ public class XmppConnection implements Runnable { public boolean pep() { synchronized (XmppConnection.this.disco) { - ServiceDiscoveryResult info = disco.get(account.getJid().toBareJid()); + ServiceDiscoveryResult info = disco.get(account.getJid().asBareJid()); return info != null && info.hasIdentity("pubsub", "pep"); } } public boolean pepPersistent() { synchronized (XmppConnection.this.disco) { - ServiceDiscoveryResult info = disco.get(account.getJid().toBareJid()); + ServiceDiscoveryResult info = disco.get(account.getJid().asBareJid()); return info != null && info.getFeatures().contains("http://jabber.org/protocol/pubsub#persistent-items"); } } public boolean pepPublishOptions() { - return hasDiscoFeature(account.getJid().toBareJid(),Namespace.PUBSUB_PUBLISH_OPTIONS); + return hasDiscoFeature(account.getJid().asBareJid(),Namespace.PUBSUB_PUBLISH_OPTIONS); } public boolean pepOmemoWhitelisted() { - return hasDiscoFeature(account.getJid().toBareJid(), AxolotlService.PEP_OMEMO_WHITELISTED); + return hasDiscoFeature(account.getJid().asBareJid(), AxolotlService.PEP_OMEMO_WHITELISTED); } public boolean mam() { - return hasDiscoFeature(account.getJid().toBareJid(), Namespace.MAM) - || hasDiscoFeature(account.getJid().toBareJid(), Namespace.MAM_LEGACY); + return hasDiscoFeature(account.getJid().asBareJid(), Namespace.MAM) + || hasDiscoFeature(account.getJid().asBareJid(), Namespace.MAM_LEGACY); } public boolean mamLegacy() { - return !hasDiscoFeature(account.getJid().toBareJid(), Namespace.MAM) - && hasDiscoFeature(account.getJid().toBareJid(), Namespace.MAM_LEGACY); + return !hasDiscoFeature(account.getJid().asBareJid(), Namespace.MAM) + && hasDiscoFeature(account.getJid().asBareJid(), Namespace.MAM_LEGACY); } public boolean push() { - return hasDiscoFeature(account.getJid().toBareJid(), "urn:xmpp:push:0") + return hasDiscoFeature(account.getJid().asBareJid(), "urn:xmpp:push:0") || hasDiscoFeature(account.getServer(), "urn:xmpp:push:0"); } @@ -1818,7 +1817,7 @@ public class XmppConnection implements Runnable { if (filesize <= maxsize) { return true; } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": http upload is not available for files with size " + filesize + " (max is " + maxsize + ")"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": http upload is not available for files with size " + filesize + " (max is " + maxsize + ")"); return false; } } catch (Exception e) { @@ -1844,7 +1843,7 @@ public class XmppConnection implements Runnable { } public boolean stanzaIds() { - return hasDiscoFeature(account.getJid().toBareJid(), Namespace.STANZA_IDS); + return hasDiscoFeature(account.getJid().asBareJid(), Namespace.STANZA_IDS); } } } diff --git a/src/main/java/de/pixart/messenger/xmpp/jid/InvalidJidException.java b/src/main/java/de/pixart/messenger/xmpp/jid/InvalidJidException.java deleted file mode 100644 index fa1b396d4..000000000 --- a/src/main/java/de/pixart/messenger/xmpp/jid/InvalidJidException.java +++ /dev/null @@ -1,49 +0,0 @@ -package de.pixart.messenger.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"; - public final static String IS_NULL = "JID can not be NULL"; - - /** - * 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/de/pixart/messenger/xmpp/jid/Jid.java b/src/main/java/de/pixart/messenger/xmpp/jid/Jid.java deleted file mode 100644 index af21019b8..000000000 --- a/src/main/java/de/pixart/messenger/xmpp/jid/Jid.java +++ /dev/null @@ -1,253 +0,0 @@ -package de.pixart.messenger.xmpp.jid; - -import android.util.LruCache; - -import net.java.otr4j.session.SessionID; - -import java.net.IDN; - -import de.pixart.messenger.Config; -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 static LruCache<String, Jid> cache = new LruCache<>(1024); - - private final String localpart; - private final String domainpart; - private final String resourcepart; - - private static final char[] JID_ESCAPING_CHARS = {' ','"','&','\'','/',':','<','>','@','\\'}; - - // It's much more efficient to store the full 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 getUnescapedLocalpart() { - if (localpart == null || !localpart.contains("\\")) { - return localpart; - } else { - String localpart = this.localpart; - for(char c : JID_ESCAPING_CHARS) { - localpart = localpart.replace(String.format ("\\%02x", (int)c),String.valueOf(c)); - } - return localpart; - } - } - - public String getDomainpart() { - return IDN.toUnicode(domainpart); - } - - public String getResourcepart() { - return resourcepart; - } - - public static Jid fromSessionID(final 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 Jid.fromString(jid, false); - } - - public static Jid fromString(final String jid, final boolean safe) throws InvalidJidException { - return new Jid(jid, safe); - } - - 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, false); - } - - private Jid(final String jid, final boolean safe) throws InvalidJidException { - if (jid == null) throw new InvalidJidException(InvalidJidException.IS_NULL); - - Jid fromCache = Jid.cache.get(jid); - if (fromCache != null) { - displayjid = fromCache.displayjid; - localpart = fromCache.localpart; - domainpart = fromCache.domainpart; - resourcepart = fromCache.resourcepart; - return; - } - - // 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); - } - - // Go ahead and check if the localpart or resourcepart is empty. - if (jid.startsWith("@") || (jid.endsWith("@") && slashCount == 0) || jid.startsWith("/") || (jid.endsWith("/") && slashCount < 2)) { - throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER); - } - - String finaljid; - - final int domainpartStart; - final int atLoc = jid.indexOf("@"); - final int slashLoc = jid.indexOf("/"); - // If there is no "@" in the JID (eg. "example.net" or "example.net/resource") - // or there are one or more "@" signs but they're all in the resourcepart (eg. "example.net/@/rp@"): - if (atCount == 0 || (atCount > 0 && slashLoc != -1 && atLoc > slashLoc)) { - localpart = ""; - finaljid = ""; - domainpartStart = 0; - } else { - final String lp = jid.substring(0, atLoc); - try { - localpart = Config.DISABLE_STRING_PREP || safe ? lp : 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 + "@"; - } - - final String dp; - if (slashCount > 0) { - final String rp = jid.substring(slashLoc + 1, jid.length()); - try { - resourcepart = Config.DISABLE_STRING_PREP || safe ? rp : 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); - } - try { - dp = IDN.toUnicode(Stringprep.nameprep(jid.substring(domainpartStart, slashLoc)), IDN.USE_STD3_ASCII_RULES); - } catch (final StringprepException e) { - throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e); - } - finaljid = finaljid + dp + "/" + rp; - } else { - resourcepart = ""; - try { - dp = IDN.toUnicode(Stringprep.nameprep(jid.substring(domainpartStart, jid.length())), IDN.USE_STD3_ASCII_RULES); - } catch (final StringprepException e) { - throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e); - } - 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); - } - - Jid.cache.put(jid, this); - - this.displayjid = finaljid; - } - - public Jid toBareJid() { - try { - return resourcepart.isEmpty() ? this : fromParts(localpart, domainpart, ""); - } catch (final InvalidJidException e) { - // This should never happen. - throw new AssertionError("Jid " + this.toString() + " invalid"); - } - } - - public Jid toDomainJid() { - try { - return resourcepart.isEmpty() && localpart.isEmpty() ? this : fromString(getDomainpart()); - } catch (final InvalidJidException e) { - // This should never happen. - throw new AssertionError("Jid " + this.toString() + " invalid"); - } - } - - @Override - public String toString() { - return displayjid; - } - - public String toPreppedString() { - String out; - if (hasLocalpart()) { - out = localpart + '@' + domainpart; - } else { - out = domainpart; - } - if (!resourcepart.isEmpty()) { - out += '/' + resourcepart; - } - return out; - } - - @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(); - } - - public boolean isDomainJid() { - return !this.hasLocalpart(); - } -} diff --git a/src/main/java/de/pixart/messenger/xmpp/jid/OtrJidHelper.java b/src/main/java/de/pixart/messenger/xmpp/jid/OtrJidHelper.java new file mode 100644 index 000000000..4710b9532 --- /dev/null +++ b/src/main/java/de/pixart/messenger/xmpp/jid/OtrJidHelper.java @@ -0,0 +1,16 @@ +package de.pixart.messenger.xmpp.jid; + +import net.java.otr4j.session.SessionID; + +import rocks.xmpp.addr.Jid; + +public final class OtrJidHelper { + + public static Jid fromSessionID(final SessionID id) throws IllegalArgumentException { + if (id.getUserID().isEmpty()) { + return Jid.of(id.getAccountID()); + } else { + return Jid.of(id.getAccountID() + "/" + id.getUserID()); + } + } +} diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleCandidate.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleCandidate.java index 6f87db473..9f163d5d9 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleCandidate.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleCandidate.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.List; import de.pixart.messenger.xml.Element; -import de.pixart.messenger.xmpp.jid.Jid; +import rocks.xmpp.addr.Jid; public class JingleCandidate { diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java index f655a4241..3b83857e7 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java @@ -37,11 +37,11 @@ import de.pixart.messenger.services.XmppConnectionService; import de.pixart.messenger.utils.CryptoHelper; import de.pixart.messenger.xml.Element; import de.pixart.messenger.xmpp.OnIqPacketReceived; -import de.pixart.messenger.xmpp.jid.Jid; import de.pixart.messenger.xmpp.jingle.stanzas.Content; import de.pixart.messenger.xmpp.jingle.stanzas.JinglePacket; import de.pixart.messenger.xmpp.jingle.stanzas.Reason; import de.pixart.messenger.xmpp.stanzas.IqPacket; +import rocks.xmpp.addr.Jid; public class JingleConnection implements Transferable { private final SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US); @@ -108,7 +108,7 @@ public class JingleConnection implements Transferable { public void onFileTransmitted(DownloadableFile file) { if (responding()) { if (expectedHash.length > 0 && !Arrays.equals(expectedHash, file.getSha1Sum())) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": hashes did not match"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": hashes did not match"); } sendSuccess(); mXmppConnectionService.getFileBackend().updateFileParams(message); @@ -338,7 +338,7 @@ public class JingleConnection implements Transferable { private void upgradeNamespace() { Jid jid = this.message.getCounterpart(); - String resource = jid != null ? jid.getResourcepart() : null; + String resource = jid != null ? jid.getResource() : null; if (resource != null) { Presence presence = this.account.getRoster().getContact(jid).getPresences().getPresences().get(resource); ServiceDiscoveryResult result = presence != null ? presence.getServiceDiscoveryResult() : null; @@ -357,7 +357,7 @@ public class JingleConnection implements Transferable { this.mJingleStatus = JINGLE_STATUS_INITIATED; Conversation conversation = this.mXmppConnectionService .findOrCreateConversation(account, - packet.getFrom().toBareJid(), false, true); + packet.getFrom().asBareJid(), false, true); this.message = new Message(conversation, "", Message.ENCRYPTION_NONE); this.message.setStatus(Message.STATUS_RECEIVED); this.mStatus = Transferable.STATUS_OFFER; @@ -386,7 +386,7 @@ public class JingleConnection implements Transferable { if (fileOffer != null) { Element encrypted = fileOffer.findChild("encrypted", AxolotlService.PEP_PREFIX); if (encrypted != null) { - this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().toBareJid()); + this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().asBareJid()); } Element fileSize = fileOffer.findChild("size"); Element fileNameElement = fileOffer.findChild("name"); @@ -495,7 +495,7 @@ public class JingleConnection implements Transferable { if (message.getEncryption() == Message.ENCRYPTION_OTR) { Conversation conversation = this.message.getConversation(); if (!this.mXmppConnectionService.renewSymmetricKey(conversation)) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not set symmetric key"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not set symmetric key"); cancel(); } this.file.setKeyAndIv(conversation.getSymmetricKey()); @@ -527,7 +527,7 @@ public class JingleConnection implements Transferable { @Override public void onIqPacketReceived(Account account, IqPacket packet) { if (packet.getType() == IqPacket.TYPE.RESULT) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": other party received offer"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": other party received offer"); if (mJingleStatus == JINGLE_STATUS_OFFERED) { mJingleStatus = JINGLE_STATUS_INITIATED; mXmppConnectionService.markMessage(message, Message.STATUS_OFFERED); @@ -709,7 +709,7 @@ public class JingleConnection implements Transferable { if (connection.getCandidate().isOurs()) { final String sid; if (ftVersion == Content.Version.FT_3) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": use session ID instead of transport ID to activate proxy"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": use session ID instead of transport ID to activate proxy"); sid = getSessionId(); } else { sid = getTransportId(); @@ -805,7 +805,7 @@ public class JingleConnection implements Transferable { } private void sendFallbackToIbb() { - Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": sending fallback to ibb"); + Log.d(Config.LOGTAG, account.getJid().asBareJid()+ ": sending fallback to ibb"); JinglePacket packet = this.bootstrapPacket("transport-replace"); Content content = new Content(this.contentCreator, this.contentName); this.transportId = this.mJingleConnectionManager.nextRandomId(); @@ -850,7 +850,7 @@ public class JingleConnection implements Transferable { @Override public void onIqPacketReceived(Account account, IqPacket packet) { if (packet.getType() == IqPacket.TYPE.RESULT) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + " recipient ACKed our transport-accept. creating ibb"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + " recipient ACKed our transport-accept. creating ibb"); transport.connect(onIbbTransportConnected); } } @@ -896,7 +896,7 @@ public class JingleConnection implements Transferable { this.message.setTransferable(null); this.mJingleConnectionManager.finishConnection(this); } else { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": received session-terminate/success while responding"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received session-terminate/success while responding"); } } diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnectionManager.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnectionManager.java index 8c1cee0fd..de0ce1ea0 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnectionManager.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnectionManager.java @@ -18,9 +18,9 @@ import de.pixart.messenger.services.XmppConnectionService; import de.pixart.messenger.utils.Namespace; import de.pixart.messenger.xml.Element; import de.pixart.messenger.xmpp.OnIqPacketReceived; -import de.pixart.messenger.xmpp.jid.Jid; import de.pixart.messenger.xmpp.jingle.stanzas.JinglePacket; import de.pixart.messenger.xmpp.stanzas.IqPacket; +import rocks.xmpp.addr.Jid; public class JingleConnectionManager extends AbstractConnectionManager { private List<JingleConnection> connections = new CopyOnWriteArrayList<>(); @@ -87,7 +87,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { listener.onPrimaryCandidateFound(false, null); return; } - if (!this.primaryCandidates.containsKey(account.getJid().toBareJid())) { + if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) { final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS); if (proxy != null) { IqPacket iq = new IqPacket(IqPacket.TYPE.GET); @@ -108,7 +108,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { candidate.setType(JingleCandidate.TYPE_PROXY); candidate.setJid(proxy); candidate.setPriority(655360 + 65535); - primaryCandidates.put(account.getJid().toBareJid(), candidate); + primaryCandidates.put(account.getJid().asBareJid(), candidate); listener.onPrimaryCandidateFound(true, candidate); } catch (final NumberFormatException e) { listener.onPrimaryCandidateFound(false, null); @@ -125,7 +125,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { } else { listener.onPrimaryCandidateFound(true, - this.primaryCandidates.get(account.getJid().toBareJid())); + this.primaryCandidates.get(account.getJid().asBareJid())); } } diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleInbandTransport.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleInbandTransport.java index 61a87c2b9..151e9409f 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleInbandTransport.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleInbandTransport.java @@ -16,8 +16,8 @@ import de.pixart.messenger.entities.DownloadableFile; import de.pixart.messenger.persistance.FileBackend; import de.pixart.messenger.xml.Element; import de.pixart.messenger.xmpp.OnIqPacketReceived; -import de.pixart.messenger.xmpp.jid.Jid; import de.pixart.messenger.xmpp.stanzas.IqPacket; +import rocks.xmpp.addr.Jid; public class JingleInbandTransport extends JingleTransport { @@ -102,13 +102,13 @@ public class JingleInbandTransport extends JingleTransport { digest.reset(); this.fileOutputStream = connection.getFileOutputStream(); if (this.fileOutputStream == null) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not create output stream"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not create output stream"); callback.onFileTransferAborted(); return; } this.remainingSize = this.fileSize = file.getExpectedSize(); } catch (final NoSuchAlgorithmException | IOException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + " " + e.getMessage()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + " " + e.getMessage()); callback.onFileTransferAborted(); } } @@ -125,7 +125,7 @@ public class JingleInbandTransport extends JingleTransport { this.digest.reset(); fileInputStream = connection.getFileInputStream(); if (fileInputStream == null) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could no create input stream"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could no create input stream"); callback.onFileTransferAborted(); return; } @@ -134,7 +134,7 @@ public class JingleInbandTransport extends JingleTransport { } } catch (NoSuchAlgorithmException e) { callback.onFileTransferAborted(); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": " + e.getMessage()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + e.getMessage()); } } @@ -195,7 +195,7 @@ public class JingleInbandTransport extends JingleTransport { fileInputStream.close(); } } catch (IOException e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": io exception during sendNextBlock() " + e.getMessage()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": io exception during sendNextBlock() " + e.getMessage()); FileBackend.close(fileInputStream); this.onFileTransmissionStatusChanged.onFileTransferAborted(); } @@ -219,7 +219,7 @@ public class JingleInbandTransport extends JingleTransport { connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100)); } } catch (Exception e) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": " + e.getMessage()); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + e.getMessage()); FileBackend.close(fileOutputStream); this.onFileTransmissionStatusChanged.onFileTransferAborted(); } @@ -245,7 +245,7 @@ public class JingleInbandTransport extends JingleTransport { this.connected = false; this.account.getXmppConnection().sendIqPacket( packet.generateResponse(IqPacket.TYPE.RESULT), null); - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": received ibb close"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received ibb close"); } else { Log.d(Config.LOGTAG, payload.toString()); // TODO some sort of exception diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleSocks5Transport.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleSocks5Transport.java index 595f17e28..946849858 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleSocks5Transport.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleSocks5Transport.java @@ -37,7 +37,7 @@ public class JingleSocks5Transport extends JingleTransport { MessageDigest mDigest = MessageDigest.getInstance("SHA-1"); StringBuilder destBuilder = new StringBuilder(); if (jingleConnection.getFtVersion() == Content.Version.FT_3) { - Log.d(Config.LOGTAG, this.connection.getAccount().getJid().toBareJid() + ": using session Id instead of transport Id for proxy destination"); + Log.d(Config.LOGTAG, this.connection.getAccount().getJid().asBareJid() + ": using session Id instead of transport Id for proxy destination"); destBuilder.append(jingleConnection.getSessionId()); } else { destBuilder.append(jingleConnection.getTransportId()); @@ -97,7 +97,7 @@ public class JingleSocks5Transport extends JingleTransport { digest.reset(); fileInputStream = connection.getFileInputStream(); if (fileInputStream == null) { - Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": could not create input stream"); + Log.d(Config.LOGTAG, connection.getAccount().getJid().asBareJid() + ": could not create input stream"); callback.onFileTransferAborted(); return; } @@ -117,7 +117,7 @@ public class JingleSocks5Transport extends JingleTransport { callback.onFileTransmitted(file); } } catch (Exception e) { - Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": " + e.getMessage()); + Log.d(Config.LOGTAG, connection.getAccount().getJid().asBareJid() + ": " + e.getMessage()); callback.onFileTransferAborted(); } finally { FileBackend.close(fileInputStream); @@ -144,7 +144,7 @@ public class JingleSocks5Transport extends JingleTransport { fileOutputStream = connection.getFileOutputStream(); if (fileOutputStream == null) { callback.onFileTransferAborted(); - Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": could not create output stream"); + Log.d(Config.LOGTAG, connection.getAccount().getJid().asBareJid() + ": could not create output stream"); return; } double size = file.getExpectedSize(); @@ -155,7 +155,7 @@ public class JingleSocks5Transport extends JingleTransport { count = inputStream.read(buffer); if (count == -1) { callback.onFileTransferAborted(); - Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": file ended prematurely with " + remainingSize + " bytes remaining"); + Log.d(Config.LOGTAG, connection.getAccount().getJid().asBareJid() + ": file ended prematurely with " + remainingSize + " bytes remaining"); return; } else { fileOutputStream.write(buffer, 0, count); @@ -169,7 +169,7 @@ public class JingleSocks5Transport extends JingleTransport { file.setSha1Sum(digest.digest()); callback.onFileTransmitted(file); } catch (Exception e) { - Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": " + e.getMessage()); + Log.d(Config.LOGTAG, connection.getAccount().getJid().asBareJid() + ": " + e.getMessage()); callback.onFileTransferAborted(); } finally { wakeLock.release(); diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/stanzas/JinglePacket.java b/src/main/java/de/pixart/messenger/xmpp/jingle/stanzas/JinglePacket.java index 3f7cc8e5b..8d16a1c6e 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/stanzas/JinglePacket.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/stanzas/JinglePacket.java @@ -3,8 +3,8 @@ package de.pixart.messenger.xmpp.jingle.stanzas; import android.util.Base64; import de.pixart.messenger.xml.Element; -import de.pixart.messenger.xmpp.jid.Jid; import de.pixart.messenger.xmpp.stanzas.IqPacket; +import rocks.xmpp.addr.Jid; public class JinglePacket extends IqPacket { Content content = null; diff --git a/src/main/java/de/pixart/messenger/xmpp/pep/Avatar.java b/src/main/java/de/pixart/messenger/xmpp/pep/Avatar.java index 05b41c610..2521d09bc 100644 --- a/src/main/java/de/pixart/messenger/xmpp/pep/Avatar.java +++ b/src/main/java/de/pixart/messenger/xmpp/pep/Avatar.java @@ -3,7 +3,7 @@ package de.pixart.messenger.xmpp.pep; import android.util.Base64; import de.pixart.messenger.xml.Element; -import de.pixart.messenger.xmpp.jid.Jid; +import rocks.xmpp.addr.Jid; public class Avatar { diff --git a/src/main/java/de/pixart/messenger/xmpp/stanzas/AbstractStanza.java b/src/main/java/de/pixart/messenger/xmpp/stanzas/AbstractStanza.java index 8d1b24ef8..9e26439c2 100644 --- a/src/main/java/de/pixart/messenger/xmpp/stanzas/AbstractStanza.java +++ b/src/main/java/de/pixart/messenger/xmpp/stanzas/AbstractStanza.java @@ -2,7 +2,7 @@ package de.pixart.messenger.xmpp.stanzas; import de.pixart.messenger.entities.Account; import de.pixart.messenger.xml.Element; -import de.pixart.messenger.xmpp.jid.Jid; +import rocks.xmpp.addr.Jid; public class AbstractStanza extends Element { @@ -33,18 +33,18 @@ public class AbstractStanza extends Element { public boolean fromServer(final Account account) { return getFrom() == null || getFrom().equals(account.getServer()) - || getFrom().equals(account.getJid().toBareJid()) + || getFrom().equals(account.getJid().asBareJid()) || getFrom().equals(account.getJid()); } public boolean toServer(final Account account) { return getTo() == null || getTo().equals(account.getServer()) - || getTo().equals(account.getJid().toBareJid()) + || getTo().equals(account.getJid().asBareJid()) || getTo().equals(account.getJid()); } public boolean fromAccount(final Account account) { - return getFrom() != null && getFrom().toBareJid().equals(account.getJid().toBareJid()); + return getFrom() != null && getFrom().asBareJid().equals(account.getJid().asBareJid()); } } |