108 lines
4.1 KiB
Java
108 lines
4.1 KiB
Java
package org.mercury_im.messenger.core;
|
|
|
|
import org.jivesoftware.smack.AbstractXMPPConnection;
|
|
import org.jivesoftware.smack.ConnectionListener;
|
|
import org.jivesoftware.smack.ReconnectionManager;
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
import org.jivesoftware.smack.chat2.ChatManager;
|
|
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
|
import org.jivesoftware.smack.iqrequest.IQRequestHandler;
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
import org.jivesoftware.smack.roster.Roster;
|
|
import org.jivesoftware.smackx.carbons.CarbonManager;
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
|
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
|
import org.jivesoftware.smackx.iqversion.packet.Version;
|
|
import org.jivesoftware.smackx.sid.StableUniqueStanzaIdManager;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
public class MercuryConnection implements ConnectionListener {
|
|
|
|
public static final String TAG = "Mercury";
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(MercuryConnection.class.getName());
|
|
|
|
protected final XMPPConnection connection;
|
|
protected final long accountId;
|
|
|
|
protected final ReconnectionManager reconnectionManager;
|
|
protected final Roster roster;
|
|
protected final ChatManager chatManager;
|
|
protected final CarbonManager carbonManager;
|
|
protected final StableUniqueStanzaIdManager stanzaIdManager;
|
|
protected final ServiceDiscoveryManager serviceDiscoveryManager;
|
|
|
|
public MercuryConnection(XMPPConnection connection, long accountId) {
|
|
this.connection = connection;
|
|
connection.addConnectionListener(this);
|
|
this.accountId = accountId;
|
|
|
|
reconnectionManager = ReconnectionManager.getInstanceFor((AbstractXMPPConnection) connection);
|
|
reconnectionManager.enableAutomaticReconnection();
|
|
|
|
this.roster = Roster.getInstanceFor(connection);
|
|
this.chatManager = ChatManager.getInstanceFor(connection);
|
|
this.carbonManager = CarbonManager.getInstanceFor(connection);
|
|
this.stanzaIdManager = StableUniqueStanzaIdManager.getInstanceFor(connection);
|
|
stanzaIdManager.enable();
|
|
|
|
this.serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
|
|
serviceDiscoveryManager.setIdentity(new DiscoverInfo.Identity("client", "Mercury", "phone"));
|
|
|
|
connection.registerIQRequestHandler(new AbstractIqRequestHandler(Version.ELEMENT, Version.NAMESPACE, IQ.Type.get, IQRequestHandler.Mode.async) {
|
|
@Override
|
|
public IQ handleIQRequest(IQ iqRequest) {
|
|
return Version.createResultFor(iqRequest, new Version("Mercury", "stealth", "Android"));
|
|
}
|
|
});
|
|
roster.setRosterLoadedAtLogin(true);
|
|
}
|
|
|
|
public XMPPConnection getConnection() {
|
|
return connection;
|
|
}
|
|
|
|
public void setPlainMessageHandler(PlainMessageHandler plainMessageHandler) {
|
|
carbonManager.addCarbonCopyReceivedListener(plainMessageHandler);
|
|
chatManager.addIncomingListener(plainMessageHandler);
|
|
chatManager.addOutgoingListener(plainMessageHandler);
|
|
}
|
|
|
|
public long getAccountId() {
|
|
return accountId;
|
|
}
|
|
|
|
public Roster getRoster() {
|
|
return roster;
|
|
}
|
|
|
|
@Override
|
|
public void connected(XMPPConnection connection) {
|
|
LOGGER.info("Connection " + getAccountId() + " connected.");
|
|
}
|
|
|
|
@Override
|
|
public void authenticated(XMPPConnection connection, boolean resumed) {
|
|
LOGGER.info("Connection " + getAccountId() + " authenticated (" + (resumed ? "resumed" : "first time") + ")");
|
|
if (connection == this.connection && !resumed) {
|
|
LOGGER.info("Enabling carbons!");
|
|
carbonManager.enableCarbonsAsync(exception -> {
|
|
LOGGER.severe("Could not enable carbons for connection " + accountId + ": " + exception.getMessage());
|
|
exception.printStackTrace();
|
|
});
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void connectionClosed() {
|
|
LOGGER.fine("Connection " + accountId + " closed.");
|
|
}
|
|
|
|
@Override
|
|
public void connectionClosedOnError(Exception e) {
|
|
LOGGER.severe("Connection " + accountId + " closed on error: " + e.getMessage());
|
|
}
|
|
|
|
}
|