package asmack.org.jivesoftware.smack; import asmack.com.kenai.jbosh.BOSHClientConnEvent; import asmack.com.kenai.jbosh.BOSHException; import asmack.com.kenai.jbosh.BOSHMessageEvent; import asmack.com.kenai.jbosh.D; import asmack.com.kenai.jbosh.F; import asmack.com.kenai.jbosh.u; import asmack.com.kenai.jbosh.v; import asmack.com.kenai.jbosh.w; import asmack.com.kenai.jbosh.x; import asmack.com.kenai.jbosh.y; import asmack.org.jivesoftware.smack.Connection; import asmack.org.jivesoftware.smack.debugger.SmackDebugger; import asmack.org.jivesoftware.smack.packet.Packet; import asmack.org.jivesoftware.smack.packet.Presence; import asmack.org.jivesoftware.smack.packet.XMPPError; import asmack.org.jivesoftware.smack.util.StringUtils; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; import java.io.Reader; import java.io.Writer; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; public class BOSHConnection extends Connection { public static final String BOSH_URI = "http://jabber.org/protocol/httpbind"; public static final String XMPP_BOSH_NS = "urn:xmpp:xbosh"; private boolean anonymous; protected String authID; private boolean authenticated; private u client; private final BOSHConfiguration config; private boolean connected; private boolean done; private boolean isFirstInitialization; private ExecutorService listenerExecutor; private Thread readerConsumer; private PipedWriter readerPipe; private Roster roster; protected String sessionID; private String user; private boolean wasAuthenticated; private class BOSHConnectionListener implements w { private final BOSHConnection connection; public BOSHConnectionListener(BOSHConnection bOSHConnection) { this.connection = bOSHConnection; } @Override // asmack.com.kenai.jbosh.w public void connectionEvent(BOSHClientConnEvent bOSHClientConnEvent) { try { if (bOSHClientConnEvent.isConnected()) { BOSHConnection.this.connected = true; if (BOSHConnection.this.isFirstInitialization) { BOSHConnection.this.isFirstInitialization = false; for (ConnectionCreationListener connectionCreationListener : Connection.getConnectionCreationListeners()) { connectionCreationListener.connectionCreated(this.connection); } } else { try { if (BOSHConnection.this.wasAuthenticated) { this.connection.login(BOSHConnection.this.config.getUsername(), BOSHConnection.this.config.getPassword(), BOSHConnection.this.config.getResource(), BOSHConnection.this.config.getFeature()); } for (ConnectionListener connectionListener : BOSHConnection.this.getConnectionListeners()) { connectionListener.reconnectionSuccessful(); } } catch (XMPPException e2) { for (ConnectionListener connectionListener2 : BOSHConnection.this.getConnectionListeners()) { connectionListener2.reconnectionFailed(e2); } } } } else { if (bOSHClientConnEvent.isError()) { try { bOSHClientConnEvent.getCause(); } catch (Exception e3) { BOSHConnection.this.notifyConnectionError(e3); } } BOSHConnection.this.connected = false; } synchronized (this.connection) { this.connection.notifyAll(); } } catch (Throwable th) { synchronized (this.connection) { this.connection.notifyAll(); throw th; } } } } /* access modifiers changed from: private */ public class ListenerNotification implements Runnable { private Packet packet; public ListenerNotification(Packet packet2) { this.packet = packet2; } public void run() { for (Connection.ListenerWrapper listenerWrapper : BOSHConnection.this.recvListeners.values()) { listenerWrapper.notifyListener(this.packet); } } } public BOSHConnection(BOSHConfiguration bOSHConfiguration) { super(bOSHConfiguration); this.connected = false; this.authenticated = false; this.anonymous = false; this.isFirstInitialization = true; this.wasAuthenticated = false; this.done = false; this.authID = null; this.sessionID = null; this.user = null; this.roster = null; this.config = bOSHConfiguration; } public BOSHConnection(boolean z, String str, int i, String str2, String str3) { super(new BOSHConfiguration(z, str, i, str2, str3)); this.connected = false; this.authenticated = false; this.anonymous = false; this.isFirstInitialization = true; this.wasAuthenticated = false; this.done = false; this.authID = null; this.sessionID = null; this.user = null; this.roster = null; this.config = (BOSHConfiguration) getConfiguration(); } private void setWasAuthenticated(boolean z) { if (!this.wasAuthenticated) { this.wasAuthenticated = z; } } @Override // asmack.org.jivesoftware.smack.Connection public void connect() throws XMPPException { if (!this.connected) { this.done = false; try { if (this.client != null) { this.client.a(); this.client = null; } this.saslAuthentication.init(); this.sessionID = null; this.authID = null; v.a a2 = v.a.a(this.config.getURI(), this.config.getServiceName()); if (this.config.isProxyEnabled()) { a2.a(this.config.getProxyAddress(), this.config.getProxyPort()); } this.client = u.a(a2.a()); this.listenerExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { /* class asmack.org.jivesoftware.smack.BOSHConnection.AnonymousClass1 */ public Thread newThread(Runnable runnable) { Thread thread = new Thread(runnable, "Smack Listener Processor (" + BOSHConnection.this.connectionCounterValue + ")"); thread.setDaemon(true); return thread; } }); this.client.a(new BOSHConnectionListener(this)); this.client.a(new BOSHPacketReader(this)); if (this.config.isDebuggerEnabled()) { initDebugger(); if (this.isFirstInitialization) { if (this.debugger.getReaderListener() != null) { addPacketListener(this.debugger.getReaderListener(), null); } if (this.debugger.getWriterListener() != null) { addPacketSendingListener(this.debugger.getWriterListener(), null); } } } u uVar = this.client; F.a d2 = F.d(); d2.a("xmpp", XMPP_BOSH_NS); d2.a(D.a(XMPP_BOSH_NS, "version", "xmpp"), "1.0"); uVar.b(d2.a()); synchronized (this) { long currentTimeMillis = System.currentTimeMillis() + ((long) (SmackConfiguration.getPacketReplyTimeout() * 6)); while (!this.connected && System.currentTimeMillis() < currentTimeMillis) { try { wait(Math.abs(currentTimeMillis - System.currentTimeMillis())); } catch (InterruptedException unused) { } } } if (!this.connected && !this.done) { this.done = true; String str = "Timeout reached for the connection to " + getHost() + ":" + getPort() + "."; throw new XMPPException(str, new XMPPError(XMPPError.Condition.remote_server_timeout, str)); } } catch (Exception e2) { throw new XMPPException("Can't connect to " + getServiceName(), e2); } } else { throw new IllegalStateException("Already connected to a server."); } } @Override // asmack.org.jivesoftware.smack.Connection public void disconnect(Presence presence) { if (this.connected) { shutdown(presence); Roster roster2 = this.roster; if (roster2 != null) { roster2.cleanup(); this.roster = null; } this.sendListeners.clear(); this.recvListeners.clear(); this.collectors.clear(); this.interceptors.clear(); this.wasAuthenticated = false; this.isFirstInitialization = true; for (ConnectionListener connectionListener : getConnectionListeners()) { try { connectionListener.connectionClosed(); } catch (Exception e2) { e2.printStackTrace(); } } } } @Override // asmack.org.jivesoftware.smack.Connection public String getConnectionID() { if (!this.connected) { return null; } String str = this.authID; return str != null ? str : this.sessionID; } @Override // asmack.org.jivesoftware.smack.Connection public Roster getRoster() { if (this.roster == null) { return null; } if (!this.config.isRosterLoadedAtLogin()) { this.roster.reload(); } Roster roster2 = this.roster; if (!roster2.rosterInitialized) { try { synchronized (roster2) { long packetReplyTimeout = (long) SmackConfiguration.getPacketReplyTimeout(); long currentTimeMillis = System.currentTimeMillis(); while (true) { if (this.roster.rosterInitialized) { break; } else if (packetReplyTimeout <= 0) { break; } else { this.roster.wait(packetReplyTimeout); long currentTimeMillis2 = System.currentTimeMillis(); packetReplyTimeout -= currentTimeMillis2 - currentTimeMillis; currentTimeMillis = currentTimeMillis2; } } } } catch (InterruptedException unused) { } } return this.roster; } @Override // asmack.org.jivesoftware.smack.Connection public String getUser() { return this.user; } /* access modifiers changed from: protected */ @Override // asmack.org.jivesoftware.smack.Connection public void initDebugger() { this.writer = new Writer() { /* class asmack.org.jivesoftware.smack.BOSHConnection.AnonymousClass2 */ @Override // java.io.Closeable, java.io.Writer, java.lang.AutoCloseable public void close() { } @Override // java.io.Writer, java.io.Flushable public void flush() { } @Override // java.io.Writer public void write(char[] cArr, int i, int i2) { } }; try { this.readerPipe = new PipedWriter(); this.reader = new PipedReader(this.readerPipe); } catch (IOException unused) { } super.initDebugger(); this.client.a(new y() { /* class asmack.org.jivesoftware.smack.BOSHConnection.AnonymousClass3 */ @Override // asmack.com.kenai.jbosh.y public void responseReceived(BOSHMessageEvent bOSHMessageEvent) { if (bOSHMessageEvent.getBody() != null) { try { BOSHConnection.this.readerPipe.write(bOSHMessageEvent.getBody().c()); BOSHConnection.this.readerPipe.flush(); } catch (Exception unused) { } } } }); this.client.a(new x() { /* class asmack.org.jivesoftware.smack.BOSHConnection.AnonymousClass4 */ @Override // asmack.com.kenai.jbosh.x public void requestSent(BOSHMessageEvent bOSHMessageEvent) { if (bOSHMessageEvent.getBody() != null) { try { BOSHConnection.this.writer.write(bOSHMessageEvent.getBody().c()); } catch (Exception unused) { } } } }); this.readerConsumer = new Thread() { /* class asmack.org.jivesoftware.smack.BOSHConnection.AnonymousClass5 */ private int bufferLength = 1024; private Thread thread = this; public void run() { try { char[] cArr = new char[this.bufferLength]; while (BOSHConnection.this.readerConsumer == this.thread) { if (!BOSHConnection.this.done) { BOSHConnection.this.reader.read(cArr, 0, this.bufferLength); } else { return; } } } catch (IOException unused) { } } }; this.readerConsumer.setDaemon(true); this.readerConsumer.start(); } @Override // asmack.org.jivesoftware.smack.Connection public boolean isAnonymous() { return this.anonymous; } @Override // asmack.org.jivesoftware.smack.Connection public boolean isAuthenticated() { return this.authenticated; } @Override // asmack.org.jivesoftware.smack.Connection public boolean isConnected() { return this.connected; } @Override // asmack.org.jivesoftware.smack.Connection public boolean isSecureConnection() { return false; } @Override // asmack.org.jivesoftware.smack.Connection public boolean isUsingCompression() { return false; } @Override // asmack.org.jivesoftware.smack.Connection public void login(String str, String str2, String str3, String str4) throws XMPPException { SmackDebugger smackDebugger; if (!isConnected()) { throw new IllegalStateException("Not connected to server."); } else if (!this.authenticated) { String trim = str.toLowerCase().trim(); String authenticate = (!this.config.isSASLAuthenticationEnabled() || !this.saslAuthentication.hasNonAnonymousAuthentication()) ? new NonSASLAuthentication(this).authenticate(trim, str2, str3, str4) : str2 != null ? this.saslAuthentication.authenticate(trim, str2, str3, str4) : this.saslAuthentication.authenticate(trim, str3, str4, this.config.getCallbackHandler()); if (authenticate != null) { this.user = authenticate; this.config.setServiceName(StringUtils.parseServer(authenticate)); } else { this.user = String.valueOf(trim) + "@" + getServiceName(); if (str3 != null) { this.user = String.valueOf(this.user) + "/" + str3; } } if (this.roster == null) { RosterStorage rosterStorage = this.rosterStorage; if (rosterStorage == null) { this.roster = new Roster(this); } else { this.roster = new Roster(this, rosterStorage); } } if (this.config.isRosterLoadedAtLogin()) { this.roster.reload(); } if (this.config.isSendPresence()) { sendPacket(new Presence(Presence.Type.available)); } this.authenticated = true; this.anonymous = false; this.config.setLoginInfo(trim, str2, str3, str4); if (this.config.isDebuggerEnabled() && (smackDebugger = this.debugger) != null) { smackDebugger.userHasLogged(this.user); } } else { throw new IllegalStateException("Already logged in to server."); } } @Override // asmack.org.jivesoftware.smack.Connection public void loginAnonymously() throws XMPPException { SmackDebugger smackDebugger; if (!isConnected()) { throw new IllegalStateException("Not connected to server."); } else if (!this.authenticated) { String authenticateAnonymously = (!this.config.isSASLAuthenticationEnabled() || !this.saslAuthentication.hasAnonymousAuthentication()) ? new NonSASLAuthentication(this).authenticateAnonymously() : this.saslAuthentication.authenticateAnonymously(); this.user = authenticateAnonymously; this.config.setServiceName(StringUtils.parseServer(authenticateAnonymously)); this.roster = null; if (this.config.isSendPresence()) { sendPacket(new Presence(Presence.Type.available)); } this.authenticated = true; this.anonymous = true; if (this.config.isDebuggerEnabled() && (smackDebugger = this.debugger) != null) { smackDebugger.userHasLogged(this.user); } } else { throw new IllegalStateException("Already logged in to server."); } } /* access modifiers changed from: protected */ public void notifyConnectionError(Exception exc) { shutdown(new Presence(Presence.Type.unavailable)); exc.printStackTrace(); for (ConnectionListener connectionListener : getConnectionListeners()) { try { connectionListener.connectionClosedOnError(exc); } catch (Exception e2) { e2.printStackTrace(); } } } /* access modifiers changed from: protected */ public void processPacket(Packet packet) { if (packet != null) { for (PacketCollector packetCollector : getPacketCollectors()) { packetCollector.processPacket(packet); } this.listenerExecutor.submit(new ListenerNotification(packet)); } } /* access modifiers changed from: protected */ public void send(F f2) throws BOSHException { if (!this.connected) { throw new IllegalStateException("Not connected to a server!"); } else if (f2 != null) { if (this.sessionID != null) { F.a e2 = f2.e(); e2.a(D.a(BOSH_URI, "sid"), this.sessionID); f2 = e2.a(); } this.client.b(f2); } else { throw new NullPointerException("Body mustn't be null!"); } } @Override // asmack.org.jivesoftware.smack.Connection public void sendPacket(Packet packet) { if (!isConnected()) { throw new IllegalStateException("Not connected to server."); } else if (packet == null) { throw new NullPointerException("Packet is null."); } else if (!this.done) { firePacketInterceptors(packet); try { F.a d2 = F.d(); d2.a(packet.toXML()); send(d2.a()); firePacketSendingListeners(packet); } catch (BOSHException e2) { e2.printStackTrace(); } } } @Override // asmack.org.jivesoftware.smack.Connection public void setRosterStorage(RosterStorage rosterStorage) throws IllegalStateException { if (this.roster == null) { this.rosterStorage = rosterStorage; return; } throw new IllegalStateException("Roster is already initialized"); } /* access modifiers changed from: protected */ public void shutdown(Presence presence) { setWasAuthenticated(this.authenticated); this.authID = null; this.sessionID = null; this.done = true; this.authenticated = false; this.connected = false; this.isFirstInitialization = false; try { u uVar = this.client; F.a d2 = F.d(); d2.a("xmpp", XMPP_BOSH_NS); d2.a(presence.toXML()); uVar.a(d2.a()); Thread.sleep(150); } catch (Exception unused) { } PipedWriter pipedWriter = this.readerPipe; if (pipedWriter != null) { try { pipedWriter.close(); } catch (Throwable unused2) { } this.reader = null; } Reader reader = this.reader; if (reader != null) { try { reader.close(); } catch (Throwable unused3) { } this.reader = null; } Writer writer = this.writer; if (writer != null) { try { writer.close(); } catch (Throwable unused4) { } this.writer = null; } ExecutorService executorService = this.listenerExecutor; if (executorService != null) { executorService.shutdown(); } this.readerConsumer = null; } }