127 lines
4.9 KiB
Java
127 lines
4.9 KiB
Java
package org.mercury_im.messenger;
|
|
|
|
import org.jivesoftware.smack.SmackConfiguration;
|
|
import org.jivesoftware.smack.SmackException;
|
|
import org.jivesoftware.smack.XMPPException;
|
|
import org.jivesoftware.smack.roster.Roster;
|
|
import org.jivesoftware.smack.roster.RosterEntry;
|
|
import org.jxmpp.jid.BareJid;
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
|
import org.mercury_im.messenger.data.repository.Repositories;
|
|
import org.mercury_im.messenger.entity.Account;
|
|
import org.mercury_im.messenger.entity.contact.Peer;
|
|
import org.mercury_im.messenger.exception.ConnectionNotFoundException;
|
|
import org.mercury_im.messenger.exception.ContactAlreadyAddedException;
|
|
import org.mercury_im.messenger.xmpp.MercuryConnection;
|
|
import org.mercury_im.messenger.xmpp.MercuryConnectionManager;
|
|
|
|
import java.util.UUID;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import javax.inject.Inject;
|
|
import javax.inject.Singleton;
|
|
|
|
import io.reactivex.Completable;
|
|
import io.reactivex.Single;
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
@Singleton
|
|
public class Messenger {
|
|
|
|
public static final String TAG = "MercuryIM";
|
|
private static final Logger LOGGER = Logger.getLogger(Messenger.class.getName());
|
|
|
|
private final MercuryConnectionManager connectionManager;
|
|
private final Repositories repositories;
|
|
|
|
private CompositeDisposable disposable = new CompositeDisposable();
|
|
|
|
static {
|
|
SmackConfiguration.DEBUG = true;
|
|
}
|
|
|
|
@Inject
|
|
public Messenger(Repositories repositories, MercuryConnectionManager connectionManager) {
|
|
this.repositories = repositories;
|
|
this.connectionManager = connectionManager;
|
|
performInitialLogin();
|
|
}
|
|
|
|
public MercuryConnectionManager getConnectionManager() {
|
|
return connectionManager;
|
|
}
|
|
|
|
public void performInitialLogin() {
|
|
LOGGER.log(Level.INFO, "Perform initial login.");
|
|
disposable.add(repositories.getAccountRepository().observeAllAccounts().firstOrError()
|
|
.subscribeOn(Schedulers.io())
|
|
.subscribe(connectionManager::registerConnections));
|
|
}
|
|
|
|
public Account createAccount(String username, String service, String password) {
|
|
Account account = new Account();
|
|
account.setAddress(username + "@" + service);
|
|
account.setPassword(password);
|
|
return account;
|
|
}
|
|
|
|
|
|
public Completable addContact(UUID accountId, String contactAddress) {
|
|
return Completable.fromAction(() -> doAddContact(accountId, contactAddress));
|
|
}
|
|
|
|
private void doAddContact(UUID accountId, String contactAddress)
|
|
throws ConnectionNotFoundException, XmppStringprepException, ContactAlreadyAddedException,
|
|
SmackException.NotLoggedInException, XMPPException.XMPPErrorException,
|
|
SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
|
|
MercuryConnection connection = getConnectionManager().getConnection(accountId);
|
|
if (connection == null) {
|
|
throw new ConnectionNotFoundException(accountId);
|
|
}
|
|
|
|
EntityBareJid jid = JidCreate.entityBareFrom(contactAddress);
|
|
Roster roster = Roster.getInstanceFor(connection.getConnection());
|
|
if (roster.getEntry(jid) != null) {
|
|
throw new ContactAlreadyAddedException(jid);
|
|
}
|
|
|
|
if (roster.isSubscriptionPreApprovalSupported()) {
|
|
LOGGER.log(Level.INFO, "Pre-Approval supported.");
|
|
try {
|
|
roster.preApproveAndCreateEntry(jid, null, null);
|
|
} catch (SmackException.FeatureNotSupportedException e) {
|
|
throw new AssertionError("pre-approval failed even though the feature is announced.");
|
|
}
|
|
} else {
|
|
roster.createItemAndRequestSubscription(jid, null, null);
|
|
}
|
|
}
|
|
|
|
public Completable deleteContact(Peer contact) {
|
|
return Completable.fromAction(() -> doDeleteContact(contact));
|
|
}
|
|
|
|
private void doDeleteContact(Peer contact)
|
|
throws ConnectionNotFoundException, XmppStringprepException,
|
|
SmackException.NotLoggedInException, XMPPException.XMPPErrorException,
|
|
SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
|
|
MercuryConnection connection = getConnectionManager().getConnection(contact.getAccount().getId());
|
|
if (connection == null) {
|
|
throw new ConnectionNotFoundException(contact.getAccount().getId());
|
|
}
|
|
|
|
Roster roster = Roster.getInstanceFor(connection.getConnection());
|
|
EntityBareJid jid = JidCreate.entityBareFrom(contact.getAddress());
|
|
RosterEntry entry = roster.getEntry(jid);
|
|
if (entry != null) {
|
|
roster.removeEntry(entry);
|
|
} else {
|
|
throw new IllegalStateException("Contact " + jid.toString() + " not in roster!");
|
|
}
|
|
}
|
|
}
|