2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2014-11-09 15:20:36 +00:00
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.os.SystemClock;
|
2018-03-05 17:30:40 +00:00
|
|
|
import android.util.Log;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2021-09-05 14:29:02 +00:00
|
|
|
import com.google.common.base.Strings;
|
2022-09-14 10:49:15 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2021-09-05 14:29:02 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2016-11-17 21:28:45 +00:00
|
|
|
import java.util.ArrayList;
|
2014-12-21 20:43:58 +00:00
|
|
|
import java.util.Collection;
|
2019-09-28 00:23:15 +00:00
|
|
|
import java.util.HashMap;
|
2016-02-03 09:40:02 +00:00
|
|
|
import java.util.HashSet;
|
2014-11-09 15:20:36 +00:00
|
|
|
import java.util.List;
|
2019-09-28 00:23:15 +00:00
|
|
|
import java.util.Map;
|
2019-01-03 13:07:03 +00:00
|
|
|
import java.util.Set;
|
2014-12-21 20:43:58 +00:00
|
|
|
import java.util.concurrent.CopyOnWriteArraySet;
|
2014-11-09 15:20:36 +00:00
|
|
|
|
2018-05-04 10:18:31 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2018-05-04 10:18:31 +00:00
|
|
|
import eu.siacs.conversations.crypto.PgpDecryptionService;
|
2015-06-25 14:56:34 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.AxolotlService;
|
2016-11-17 21:28:45 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
|
2022-09-15 10:22:05 +00:00
|
|
|
import eu.siacs.conversations.crypto.sasl.ChannelBinding;
|
2022-10-15 18:53:59 +00:00
|
|
|
import eu.siacs.conversations.crypto.sasl.ChannelBindingMechanism;
|
2022-10-15 16:56:31 +00:00
|
|
|
import eu.siacs.conversations.crypto.sasl.HashedToken;
|
|
|
|
import eu.siacs.conversations.crypto.sasl.HashedTokenSha256;
|
|
|
|
import eu.siacs.conversations.crypto.sasl.HashedTokenSha512;
|
2022-09-15 10:22:05 +00:00
|
|
|
import eu.siacs.conversations.crypto.sasl.SaslMechanism;
|
|
|
|
import eu.siacs.conversations.crypto.sasl.ScramPlusMechanism;
|
2019-01-25 09:07:02 +00:00
|
|
|
import eu.siacs.conversations.services.AvatarService;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2019-01-25 09:07:02 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2016-11-17 21:28:45 +00:00
|
|
|
import eu.siacs.conversations.utils.XmppUri;
|
2021-01-23 08:25:34 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.xmpp.XmppConnection;
|
2020-05-02 07:50:17 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.RtpCapability;
|
2014-11-05 20:55:47 +00:00
|
|
|
|
2019-01-25 09:07:02 +00:00
|
|
|
public class Account extends AbstractEntity implements AvatarService.Avatarable {
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2018-09-27 15:39:49 +00:00
|
|
|
public static final String TABLENAME = "accounts";
|
|
|
|
|
|
|
|
public static final String USERNAME = "username";
|
|
|
|
public static final String SERVER = "server";
|
|
|
|
public static final String PASSWORD = "password";
|
|
|
|
public static final String OPTIONS = "options";
|
|
|
|
public static final String ROSTERVERSION = "rosterversion";
|
|
|
|
public static final String KEYS = "keys";
|
|
|
|
public static final String AVATAR = "avatar";
|
|
|
|
public static final String DISPLAY_NAME = "display_name";
|
|
|
|
public static final String HOSTNAME = "hostname";
|
|
|
|
public static final String PORT = "port";
|
|
|
|
public static final String STATUS = "status";
|
|
|
|
public static final String STATUS_MESSAGE = "status_message";
|
|
|
|
public static final String RESOURCE = "resource";
|
2022-09-15 10:22:05 +00:00
|
|
|
public static final String PINNED_MECHANISM = "pinned_mechanism";
|
|
|
|
public static final String PINNED_CHANNEL_BINDING = "pinned_channel_binding";
|
2022-10-15 16:56:31 +00:00
|
|
|
public static final String FAST_MECHANISM = "fast_mechanism";
|
|
|
|
public static final String FAST_TOKEN = "fast_token";
|
2018-09-27 15:39:49 +00:00
|
|
|
|
|
|
|
public static final int OPTION_DISABLED = 1;
|
|
|
|
public static final int OPTION_REGISTER = 2;
|
|
|
|
public static final int OPTION_MAGIC_CREATE = 4;
|
|
|
|
public static final int OPTION_REQUIRES_ACCESS_MODE_CHANGE = 5;
|
|
|
|
public static final int OPTION_LOGGED_IN_SUCCESSFULLY = 6;
|
|
|
|
public static final int OPTION_HTTP_UPLOAD_AVAILABLE = 7;
|
2022-09-25 13:18:45 +00:00
|
|
|
public static final int OPTION_UNVERIFIED = 8;
|
2020-01-09 19:10:19 +00:00
|
|
|
public static final int OPTION_FIXED_USERNAME = 9;
|
2022-09-25 12:13:04 +00:00
|
|
|
public static final int OPTION_QUICKSTART_AVAILABLE = 10;
|
2022-09-15 10:22:05 +00:00
|
|
|
|
2018-09-27 15:39:49 +00:00
|
|
|
private static final String KEY_PGP_SIGNATURE = "pgp_signature";
|
|
|
|
private static final String KEY_PGP_ID = "pgp_id";
|
2022-09-15 10:22:05 +00:00
|
|
|
private static final String KEY_PINNED_MECHANISM = "pinned_mechanism";
|
|
|
|
public static final String KEY_PRE_AUTH_REGISTRATION_TOKEN = "pre_auth_registration";
|
|
|
|
|
2018-09-27 15:39:49 +00:00
|
|
|
protected final JSONObject keys;
|
|
|
|
private final Roster roster = new Roster(this);
|
|
|
|
private final Collection<Jid> blocklist = new CopyOnWriteArraySet<>();
|
2019-06-30 19:57:37 +00:00
|
|
|
public final Set<Conversation> pendingConferenceJoins = new HashSet<>();
|
|
|
|
public final Set<Conversation> pendingConferenceLeaves = new HashSet<>();
|
2019-06-18 16:09:44 +00:00
|
|
|
public final Set<Conversation> inProgressConferenceJoins = new HashSet<>();
|
2019-06-30 17:54:07 +00:00
|
|
|
public final Set<Conversation> inProgressConferencePings = new HashSet<>();
|
2018-09-27 15:39:49 +00:00
|
|
|
protected Jid jid;
|
|
|
|
protected String password;
|
|
|
|
protected int options = 0;
|
|
|
|
protected State status = State.OFFLINE;
|
2018-10-29 11:00:25 +00:00
|
|
|
private State lastErrorStatus = State.OFFLINE;
|
2018-09-27 15:39:49 +00:00
|
|
|
protected String resource;
|
|
|
|
protected String avatar;
|
|
|
|
protected String hostname = null;
|
|
|
|
protected int port = 5222;
|
|
|
|
protected boolean online = false;
|
|
|
|
private String rosterVersion;
|
|
|
|
private String displayName = null;
|
|
|
|
private AxolotlService axolotlService = null;
|
|
|
|
private PgpDecryptionService pgpDecryptionService = null;
|
|
|
|
private XmppConnection xmppConnection = null;
|
|
|
|
private long mEndGracePeriod = 0L;
|
2019-09-28 00:23:15 +00:00
|
|
|
private final Map<Jid, Bookmark> bookmarks = new HashMap<>();
|
2022-09-15 10:22:05 +00:00
|
|
|
private Presence.Status presenceStatus;
|
|
|
|
private String presenceStatusMessage;
|
|
|
|
private String pinnedMechanism;
|
|
|
|
private String pinnedChannelBinding;
|
2022-10-15 16:56:31 +00:00
|
|
|
private String fastMechanism;
|
|
|
|
private String fastToken;
|
2018-09-27 15:39:49 +00:00
|
|
|
|
|
|
|
public Account(final Jid jid, final String password) {
|
2022-10-15 16:56:31 +00:00
|
|
|
this(
|
|
|
|
java.util.UUID.randomUUID().toString(),
|
|
|
|
jid,
|
|
|
|
password,
|
|
|
|
0,
|
|
|
|
null,
|
|
|
|
"",
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
5222,
|
|
|
|
Presence.Status.ONLINE,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Account(
|
|
|
|
final String uuid,
|
|
|
|
final Jid jid,
|
|
|
|
final String password,
|
|
|
|
final int options,
|
|
|
|
final String rosterVersion,
|
|
|
|
final String keys,
|
|
|
|
final String avatar,
|
|
|
|
String displayName,
|
|
|
|
String hostname,
|
|
|
|
int port,
|
|
|
|
final Presence.Status status,
|
|
|
|
String statusMessage,
|
|
|
|
final String pinnedMechanism,
|
|
|
|
final String pinnedChannelBinding,
|
|
|
|
final String fastMechanism,
|
|
|
|
final String fastToken) {
|
2018-09-27 15:39:49 +00:00
|
|
|
this.uuid = uuid;
|
|
|
|
this.jid = jid;
|
|
|
|
this.password = password;
|
|
|
|
this.options = options;
|
|
|
|
this.rosterVersion = rosterVersion;
|
|
|
|
JSONObject tmp;
|
|
|
|
try {
|
|
|
|
tmp = new JSONObject(keys);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
tmp = new JSONObject();
|
|
|
|
}
|
|
|
|
this.keys = tmp;
|
|
|
|
this.avatar = avatar;
|
|
|
|
this.displayName = displayName;
|
|
|
|
this.hostname = hostname;
|
|
|
|
this.port = port;
|
|
|
|
this.presenceStatus = status;
|
|
|
|
this.presenceStatusMessage = statusMessage;
|
2022-09-15 10:22:05 +00:00
|
|
|
this.pinnedMechanism = pinnedMechanism;
|
|
|
|
this.pinnedChannelBinding = pinnedChannelBinding;
|
2022-10-15 16:56:31 +00:00
|
|
|
this.fastMechanism = fastMechanism;
|
|
|
|
this.fastToken = fastToken;
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Account fromCursor(final Cursor cursor) {
|
|
|
|
final Jid jid;
|
|
|
|
try {
|
2022-09-15 10:22:05 +00:00
|
|
|
final String resource = cursor.getString(cursor.getColumnIndexOrThrow(RESOURCE));
|
2022-10-15 16:56:31 +00:00
|
|
|
jid =
|
|
|
|
Jid.of(
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(USERNAME)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(SERVER)),
|
|
|
|
resource == null || resource.trim().isEmpty() ? null : resource);
|
2022-09-15 10:22:05 +00:00
|
|
|
} catch (final IllegalArgumentException e) {
|
2022-10-15 16:56:31 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(USERNAME))
|
|
|
|
+ "@"
|
|
|
|
+ cursor.getString(cursor.getColumnIndexOrThrow(SERVER)));
|
2022-09-15 10:22:05 +00:00
|
|
|
throw new AssertionError(e);
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
2022-10-15 16:56:31 +00:00
|
|
|
return new Account(
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(UUID)),
|
2018-09-27 15:39:49 +00:00
|
|
|
jid,
|
2022-09-14 15:51:22 +00:00
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(PASSWORD)),
|
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(OPTIONS)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(ROSTERVERSION)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(KEYS)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(AVATAR)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(DISPLAY_NAME)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(HOSTNAME)),
|
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(PORT)),
|
2022-10-15 16:56:31 +00:00
|
|
|
Presence.Status.fromShowString(
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(STATUS))),
|
2022-09-15 10:22:05 +00:00
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(STATUS_MESSAGE)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(PINNED_MECHANISM)),
|
2022-10-15 16:56:31 +00:00
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(PINNED_CHANNEL_BINDING)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(FAST_MECHANISM)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(FAST_TOKEN)));
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 15:51:22 +00:00
|
|
|
public boolean httpUploadAvailable(long size) {
|
|
|
|
return xmppConnection != null && xmppConnection.getFeatures().httpUpload(size);
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean httpUploadAvailable() {
|
|
|
|
return isOptionSet(OPTION_HTTP_UPLOAD_AVAILABLE) || httpUploadAvailable(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDisplayName() {
|
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDisplayName(String displayName) {
|
|
|
|
this.displayName = displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Contact getSelfContact() {
|
|
|
|
return getRoster().getContact(jid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPendingPgpIntent(Conversation conversation) {
|
|
|
|
return pgpDecryptionService != null && pgpDecryptionService.hasPendingIntent(conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isPgpDecryptionServiceConnected() {
|
|
|
|
return pgpDecryptionService != null && pgpDecryptionService.isConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setShowErrorNotification(boolean newValue) {
|
|
|
|
boolean oldValue = showErrorNotification();
|
|
|
|
setKey("show_error", Boolean.toString(newValue));
|
|
|
|
return newValue != oldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean showErrorNotification() {
|
|
|
|
String key = getKey("show_error");
|
|
|
|
return key == null || Boolean.parseBoolean(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return !isOptionSet(Account.OPTION_DISABLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOptionSet(final int option) {
|
|
|
|
return ((options & (1 << option)) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setOption(final int option, final boolean value) {
|
|
|
|
final int before = this.options;
|
|
|
|
if (value) {
|
|
|
|
this.options |= 1 << option;
|
|
|
|
} else {
|
|
|
|
this.options &= ~(1 << option);
|
|
|
|
}
|
|
|
|
return before != this.options;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsername() {
|
|
|
|
return jid.getEscapedLocal();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setJid(final Jid next) {
|
|
|
|
final Jid previousFull = this.jid;
|
|
|
|
final Jid prev = this.jid != null ? this.jid.asBareJid() : null;
|
|
|
|
final boolean changed = prev == null || (next != null && !prev.equals(next.asBareJid()));
|
|
|
|
if (changed) {
|
|
|
|
final AxolotlService oldAxolotlService = this.axolotlService;
|
|
|
|
if (oldAxolotlService != null) {
|
|
|
|
oldAxolotlService.destroy();
|
|
|
|
this.jid = next;
|
|
|
|
this.axolotlService = oldAxolotlService.makeNew();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.jid = next;
|
|
|
|
return next != null && !next.equals(previousFull);
|
|
|
|
}
|
|
|
|
|
2020-05-17 08:24:46 +00:00
|
|
|
public Jid getDomain() {
|
2018-09-27 15:39:49 +00:00
|
|
|
return jid.getDomain();
|
|
|
|
}
|
|
|
|
|
2020-05-17 08:24:46 +00:00
|
|
|
public String getServer() {
|
|
|
|
return jid.getDomain().toEscapedString();
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:39:49 +00:00
|
|
|
public String getPassword() {
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPassword(final String password) {
|
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getHostname() {
|
2021-09-05 14:29:02 +00:00
|
|
|
return Strings.nullToEmpty(this.hostname);
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setHostname(String hostname) {
|
|
|
|
this.hostname = hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOnion() {
|
|
|
|
final String server = getServer();
|
|
|
|
return server != null && server.endsWith(".onion");
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPort() {
|
|
|
|
return this.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPort(int port) {
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public State getStatus() {
|
|
|
|
if (isOptionSet(OPTION_DISABLED)) {
|
|
|
|
return State.DISABLED;
|
|
|
|
} else {
|
|
|
|
return this.status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 11:00:25 +00:00
|
|
|
public State getLastErrorStatus() {
|
|
|
|
return this.lastErrorStatus;
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:39:49 +00:00
|
|
|
public void setStatus(final State status) {
|
|
|
|
this.status = status;
|
2018-11-21 15:45:38 +00:00
|
|
|
if (status.isError || status == State.ONLINE) {
|
2018-10-29 11:00:25 +00:00
|
|
|
this.lastErrorStatus = status;
|
|
|
|
}
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 10:22:05 +00:00
|
|
|
public void setPinnedMechanism(final SaslMechanism mechanism) {
|
|
|
|
this.pinnedMechanism = mechanism.getMechanism();
|
2022-10-15 18:53:59 +00:00
|
|
|
if (mechanism instanceof ChannelBindingMechanism) {
|
2022-10-15 16:56:31 +00:00
|
|
|
this.pinnedChannelBinding =
|
2022-10-15 18:53:59 +00:00
|
|
|
((ChannelBindingMechanism) mechanism).getChannelBinding().toString();
|
2022-10-15 16:56:31 +00:00
|
|
|
} else {
|
|
|
|
this.pinnedChannelBinding = null;
|
2022-09-15 10:22:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:56:31 +00:00
|
|
|
public void setFastToken(final HashedToken.Mechanism mechanism, final String token) {
|
|
|
|
this.fastMechanism = mechanism.name();
|
|
|
|
this.fastToken = token;
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:44:05 +00:00
|
|
|
public void resetFastToken() {
|
|
|
|
this.fastMechanism = null;
|
|
|
|
this.fastToken = null;
|
|
|
|
}
|
|
|
|
|
2022-09-15 10:22:05 +00:00
|
|
|
public void resetPinnedMechanism() {
|
|
|
|
this.pinnedMechanism = null;
|
|
|
|
this.pinnedChannelBinding = null;
|
|
|
|
setKey(Account.KEY_PINNED_MECHANISM, String.valueOf(-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPinnedMechanismPriority() {
|
|
|
|
final int fallback = getKeyAsInt(KEY_PINNED_MECHANISM, -1);
|
|
|
|
if (Strings.isNullOrEmpty(this.pinnedMechanism)) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
final SaslMechanism saslMechanism = getPinnedMechanism();
|
|
|
|
if (saslMechanism == null) {
|
|
|
|
return fallback;
|
|
|
|
} else {
|
|
|
|
return saslMechanism.getPriority();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:56:31 +00:00
|
|
|
private SaslMechanism getPinnedMechanism() {
|
2022-09-15 10:22:05 +00:00
|
|
|
final String mechanism = Strings.nullToEmpty(this.pinnedMechanism);
|
|
|
|
final ChannelBinding channelBinding = ChannelBinding.get(this.pinnedChannelBinding);
|
|
|
|
return new SaslMechanism.Factory(this).of(mechanism, channelBinding);
|
|
|
|
}
|
|
|
|
|
2022-10-15 18:53:59 +00:00
|
|
|
public HashedToken getFastMechanism() {
|
2022-10-15 16:56:31 +00:00
|
|
|
final HashedToken.Mechanism fastMechanism = HashedToken.Mechanism.ofOrNull(this.fastMechanism);
|
|
|
|
final String token = this.fastToken;
|
|
|
|
if (fastMechanism == null || Strings.isNullOrEmpty(token)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (fastMechanism.hashFunction.equals("SHA-256")) {
|
|
|
|
return new HashedTokenSha256(this, fastMechanism.channelBinding);
|
|
|
|
} else if (fastMechanism.hashFunction.equals("SHA-512")) {
|
|
|
|
return new HashedTokenSha512(this, fastMechanism.channelBinding);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SaslMechanism getQuickStartMechanism() {
|
|
|
|
final HashedToken hashedTokenMechanism = getFastMechanism();
|
|
|
|
if (hashedTokenMechanism != null) {
|
|
|
|
return hashedTokenMechanism;
|
|
|
|
}
|
|
|
|
return getPinnedMechanism();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFastToken() {
|
|
|
|
return this.fastToken;
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:39:49 +00:00
|
|
|
public State getTrueStatus() {
|
|
|
|
return this.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean errorStatus() {
|
|
|
|
return getStatus().isError();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasErrorStatus() {
|
|
|
|
return getXmppConnection() != null
|
|
|
|
&& (getStatus().isError() || getStatus() == State.CONNECTING)
|
|
|
|
&& getXmppConnection().getAttempt() >= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Presence.Status getPresenceStatus() {
|
|
|
|
return this.presenceStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPresenceStatus(Presence.Status status) {
|
|
|
|
this.presenceStatus = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPresenceStatusMessage() {
|
|
|
|
return this.presenceStatusMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPresenceStatusMessage(String message) {
|
|
|
|
this.presenceStatusMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getResource() {
|
|
|
|
return jid.getResource();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setResource(final String resource) {
|
|
|
|
this.jid = this.jid.withResource(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Jid getJid() {
|
|
|
|
return jid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JSONObject getKeys() {
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getKey(final String name) {
|
|
|
|
synchronized (this.keys) {
|
|
|
|
return this.keys.optString(name, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyAsInt(final String name, int defaultValue) {
|
|
|
|
String key = getKey(name);
|
|
|
|
try {
|
|
|
|
return key == null ? defaultValue : Integer.parseInt(key);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setKey(final String keyName, final String keyValue) {
|
|
|
|
synchronized (this.keys) {
|
|
|
|
try {
|
|
|
|
this.keys.put(keyName, keyValue);
|
|
|
|
return true;
|
|
|
|
} catch (final JSONException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 10:22:05 +00:00
|
|
|
public void setPrivateKeyAlias(final String alias) {
|
|
|
|
setKey("private_key_alias", alias);
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPrivateKeyAlias() {
|
|
|
|
return getKey("private_key_alias");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ContentValues getContentValues() {
|
|
|
|
final ContentValues values = new ContentValues();
|
|
|
|
values.put(UUID, uuid);
|
|
|
|
values.put(USERNAME, jid.getLocal());
|
2020-05-17 08:24:46 +00:00
|
|
|
values.put(SERVER, jid.getDomain().toEscapedString());
|
2018-09-27 15:39:49 +00:00
|
|
|
values.put(PASSWORD, password);
|
|
|
|
values.put(OPTIONS, options);
|
|
|
|
synchronized (this.keys) {
|
|
|
|
values.put(KEYS, this.keys.toString());
|
|
|
|
}
|
|
|
|
values.put(ROSTERVERSION, rosterVersion);
|
|
|
|
values.put(AVATAR, avatar);
|
|
|
|
values.put(DISPLAY_NAME, displayName);
|
|
|
|
values.put(HOSTNAME, hostname);
|
|
|
|
values.put(PORT, port);
|
|
|
|
values.put(STATUS, presenceStatus.toShowString());
|
|
|
|
values.put(STATUS_MESSAGE, presenceStatusMessage);
|
|
|
|
values.put(RESOURCE, jid.getResource());
|
2022-09-15 10:22:05 +00:00
|
|
|
values.put(PINNED_MECHANISM, pinnedMechanism);
|
|
|
|
values.put(PINNED_CHANNEL_BINDING, pinnedChannelBinding);
|
2022-10-15 16:56:31 +00:00
|
|
|
values.put(FAST_MECHANISM, this.fastMechanism);
|
|
|
|
values.put(FAST_TOKEN, this.fastToken);
|
2018-09-27 15:39:49 +00:00
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AxolotlService getAxolotlService() {
|
|
|
|
return axolotlService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initAccountServices(final XmppConnectionService context) {
|
|
|
|
this.axolotlService = new AxolotlService(this, context);
|
|
|
|
this.pgpDecryptionService = new PgpDecryptionService(context);
|
|
|
|
if (xmppConnection != null) {
|
|
|
|
xmppConnection.addOnAdvancedStreamFeaturesAvailableListener(axolotlService);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PgpDecryptionService getPgpDecryptionService() {
|
|
|
|
return this.pgpDecryptionService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XmppConnection getXmppConnection() {
|
|
|
|
return this.xmppConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setXmppConnection(final XmppConnection connection) {
|
|
|
|
this.xmppConnection = connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRosterVersion() {
|
|
|
|
if (this.rosterVersion == null) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return this.rosterVersion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRosterVersion(final String version) {
|
|
|
|
this.rosterVersion = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int countPresences() {
|
|
|
|
return this.getSelfContact().getPresences().size();
|
|
|
|
}
|
|
|
|
|
2020-05-02 07:50:17 +00:00
|
|
|
public int activeDevicesWithRtpCapability() {
|
|
|
|
int i = 0;
|
2022-10-15 16:56:31 +00:00
|
|
|
for (Presence presence : getSelfContact().getPresences().getPresences()) {
|
2020-05-02 07:50:17 +00:00
|
|
|
if (RtpCapability.check(presence) != RtpCapability.Capability.NONE) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:39:49 +00:00
|
|
|
public String getPgpSignature() {
|
|
|
|
return getKey(KEY_PGP_SIGNATURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setPgpSignature(String signature) {
|
|
|
|
return setKey(KEY_PGP_SIGNATURE, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean unsetPgpSignature() {
|
|
|
|
synchronized (this.keys) {
|
|
|
|
return keys.remove(KEY_PGP_SIGNATURE) != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getPgpId() {
|
|
|
|
synchronized (this.keys) {
|
|
|
|
if (keys.has(KEY_PGP_ID)) {
|
|
|
|
try {
|
|
|
|
return keys.getLong(KEY_PGP_ID);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setPgpSignId(long pgpID) {
|
|
|
|
synchronized (this.keys) {
|
|
|
|
try {
|
|
|
|
if (pgpID == 0) {
|
|
|
|
keys.remove(KEY_PGP_ID);
|
|
|
|
} else {
|
|
|
|
keys.put(KEY_PGP_ID, pgpID);
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Roster getRoster() {
|
|
|
|
return this.roster;
|
|
|
|
}
|
|
|
|
|
2019-09-28 00:23:15 +00:00
|
|
|
public Collection<Bookmark> getBookmarks() {
|
2022-09-14 10:49:15 +00:00
|
|
|
synchronized (this.bookmarks) {
|
|
|
|
return ImmutableList.copyOf(this.bookmarks.values());
|
|
|
|
}
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 10:49:15 +00:00
|
|
|
public void setBookmarks(final Map<Jid, Bookmark> bookmarks) {
|
2019-09-28 00:23:15 +00:00
|
|
|
synchronized (this.bookmarks) {
|
|
|
|
this.bookmarks.clear();
|
|
|
|
this.bookmarks.putAll(bookmarks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 10:49:15 +00:00
|
|
|
public void putBookmark(final Bookmark bookmark) {
|
2019-09-28 00:23:15 +00:00
|
|
|
synchronized (this.bookmarks) {
|
|
|
|
this.bookmarks.put(bookmark.getJid(), bookmark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeBookmark(Bookmark bookmark) {
|
|
|
|
synchronized (this.bookmarks) {
|
|
|
|
this.bookmarks.remove(bookmark.getJid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeBookmark(Jid jid) {
|
|
|
|
synchronized (this.bookmarks) {
|
|
|
|
this.bookmarks.remove(jid);
|
|
|
|
}
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:07:03 +00:00
|
|
|
public Set<Jid> getBookmarkedJids() {
|
2019-09-28 00:23:15 +00:00
|
|
|
synchronized (this.bookmarks) {
|
|
|
|
return new HashSet<>(this.bookmarks.keySet());
|
2019-01-03 13:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 15:04:42 +00:00
|
|
|
public Bookmark getBookmark(final Jid jid) {
|
2019-09-28 00:23:15 +00:00
|
|
|
synchronized (this.bookmarks) {
|
|
|
|
return this.bookmarks.get(jid.asBareJid());
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setAvatar(final String filename) {
|
|
|
|
if (this.avatar != null && this.avatar.equals(filename)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
this.avatar = filename;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAvatar() {
|
|
|
|
return this.avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void activateGracePeriod(final long duration) {
|
|
|
|
if (duration > 0) {
|
|
|
|
this.mEndGracePeriod = SystemClock.elapsedRealtime() + duration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deactivateGracePeriod() {
|
|
|
|
this.mEndGracePeriod = 0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean inGracePeriod() {
|
|
|
|
return SystemClock.elapsedRealtime() < this.mEndGracePeriod;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getShareableUri() {
|
|
|
|
List<XmppUri.Fingerprint> fingerprints = this.getFingerprints();
|
|
|
|
String uri = "xmpp:" + this.getJid().asBareJid().toEscapedString();
|
|
|
|
if (fingerprints.size() > 0) {
|
|
|
|
return XmppUri.getFingerprintUri(uri, fingerprints, ';');
|
|
|
|
} else {
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getShareableLink() {
|
|
|
|
List<XmppUri.Fingerprint> fingerprints = this.getFingerprints();
|
2022-10-15 16:56:31 +00:00
|
|
|
String uri =
|
|
|
|
"https://conversations.im/i/"
|
|
|
|
+ XmppUri.lameUrlEncode(this.getJid().asBareJid().toEscapedString());
|
2018-09-27 15:39:49 +00:00
|
|
|
if (fingerprints.size() > 0) {
|
|
|
|
return XmppUri.getFingerprintUri(uri, fingerprints, '&');
|
|
|
|
} else {
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<XmppUri.Fingerprint> getFingerprints() {
|
|
|
|
ArrayList<XmppUri.Fingerprint> fingerprints = new ArrayList<>();
|
|
|
|
if (axolotlService == null) {
|
|
|
|
return fingerprints;
|
|
|
|
}
|
2022-10-15 16:56:31 +00:00
|
|
|
fingerprints.add(
|
|
|
|
new XmppUri.Fingerprint(
|
|
|
|
XmppUri.FingerprintType.OMEMO,
|
|
|
|
axolotlService.getOwnFingerprint().substring(2),
|
|
|
|
axolotlService.getOwnDeviceId()));
|
2018-09-27 15:39:49 +00:00
|
|
|
for (XmppAxolotlSession session : axolotlService.findOwnSessions()) {
|
|
|
|
if (session.getTrust().isVerified() && session.getTrust().isActive()) {
|
2022-10-15 16:56:31 +00:00
|
|
|
fingerprints.add(
|
|
|
|
new XmppUri.Fingerprint(
|
|
|
|
XmppUri.FingerprintType.OMEMO,
|
|
|
|
session.getFingerprint().substring(2).replaceAll("\\s", ""),
|
|
|
|
session.getRemoteAddress().getDeviceId()));
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fingerprints;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBlocked(final ListItem contact) {
|
|
|
|
final Jid jid = contact.getJid();
|
2022-10-15 16:56:31 +00:00
|
|
|
return jid != null
|
|
|
|
&& (blocklist.contains(jid.asBareJid()) || blocklist.contains(jid.getDomain()));
|
2018-09-27 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBlocked(final Jid jid) {
|
|
|
|
return jid != null && blocklist.contains(jid.asBareJid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Jid> getBlocklist() {
|
|
|
|
return this.blocklist;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearBlocklist() {
|
|
|
|
getBlocklist().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOnlineAndConnected() {
|
|
|
|
return this.getStatus() == State.ONLINE && this.getXmppConnection() != null;
|
|
|
|
}
|
|
|
|
|
2019-01-25 09:07:02 +00:00
|
|
|
@Override
|
|
|
|
public int getAvatarBackgroundColor() {
|
|
|
|
return UIHelper.getColorForName(jid.asBareJid().toString());
|
|
|
|
}
|
|
|
|
|
2020-08-31 11:05:10 +00:00
|
|
|
@Override
|
|
|
|
public String getAvatarName() {
|
|
|
|
throw new IllegalStateException("This method should not be called");
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:39:49 +00:00
|
|
|
public enum State {
|
|
|
|
DISABLED(false, false),
|
|
|
|
OFFLINE(false),
|
|
|
|
CONNECTING(false),
|
|
|
|
ONLINE(false),
|
|
|
|
NO_INTERNET(false),
|
|
|
|
UNAUTHORIZED,
|
2022-06-14 06:39:55 +00:00
|
|
|
TEMPORARY_AUTH_FAILURE,
|
2018-09-27 15:39:49 +00:00
|
|
|
SERVER_NOT_FOUND,
|
|
|
|
REGISTRATION_SUCCESSFUL(false),
|
|
|
|
REGISTRATION_FAILED(true, false),
|
|
|
|
REGISTRATION_WEB(true, false),
|
|
|
|
REGISTRATION_CONFLICT(true, false),
|
|
|
|
REGISTRATION_NOT_SUPPORTED(true, false),
|
|
|
|
REGISTRATION_PLEASE_WAIT(true, false),
|
2022-10-15 16:56:31 +00:00
|
|
|
REGISTRATION_INVALID_TOKEN(true, false),
|
2018-09-27 15:39:49 +00:00
|
|
|
REGISTRATION_PASSWORD_TOO_WEAK(true, false),
|
|
|
|
TLS_ERROR,
|
2021-04-30 07:53:19 +00:00
|
|
|
TLS_ERROR_DOMAIN,
|
2018-09-27 15:39:49 +00:00
|
|
|
INCOMPATIBLE_SERVER,
|
2022-08-29 16:53:34 +00:00
|
|
|
INCOMPATIBLE_CLIENT,
|
2018-09-27 15:39:49 +00:00
|
|
|
TOR_NOT_AVAILABLE,
|
|
|
|
DOWNGRADE_ATTACK,
|
|
|
|
SESSION_FAILURE,
|
|
|
|
BIND_FAILURE,
|
|
|
|
HOST_UNKNOWN,
|
|
|
|
STREAM_ERROR,
|
|
|
|
STREAM_OPENING_ERROR,
|
|
|
|
POLICY_VIOLATION,
|
|
|
|
PAYMENT_REQUIRED,
|
|
|
|
MISSING_INTERNET_PERMISSION(false);
|
|
|
|
|
|
|
|
private final boolean isError;
|
|
|
|
private final boolean attemptReconnect;
|
|
|
|
|
|
|
|
State(final boolean isError) {
|
|
|
|
this(isError, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
State(final boolean isError, final boolean reconnect) {
|
|
|
|
this.isError = isError;
|
|
|
|
this.attemptReconnect = reconnect;
|
|
|
|
}
|
|
|
|
|
|
|
|
State() {
|
|
|
|
this(true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isError() {
|
|
|
|
return this.isError;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAttemptReconnect() {
|
|
|
|
return this.attemptReconnect;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getReadableId() {
|
|
|
|
switch (this) {
|
|
|
|
case DISABLED:
|
|
|
|
return R.string.account_status_disabled;
|
|
|
|
case ONLINE:
|
|
|
|
return R.string.account_status_online;
|
|
|
|
case CONNECTING:
|
|
|
|
return R.string.account_status_connecting;
|
|
|
|
case OFFLINE:
|
|
|
|
return R.string.account_status_offline;
|
|
|
|
case UNAUTHORIZED:
|
|
|
|
return R.string.account_status_unauthorized;
|
|
|
|
case SERVER_NOT_FOUND:
|
|
|
|
return R.string.account_status_not_found;
|
|
|
|
case NO_INTERNET:
|
|
|
|
return R.string.account_status_no_internet;
|
|
|
|
case REGISTRATION_FAILED:
|
|
|
|
return R.string.account_status_regis_fail;
|
|
|
|
case REGISTRATION_WEB:
|
|
|
|
return R.string.account_status_regis_web;
|
|
|
|
case REGISTRATION_CONFLICT:
|
|
|
|
return R.string.account_status_regis_conflict;
|
|
|
|
case REGISTRATION_SUCCESSFUL:
|
|
|
|
return R.string.account_status_regis_success;
|
|
|
|
case REGISTRATION_NOT_SUPPORTED:
|
|
|
|
return R.string.account_status_regis_not_sup;
|
2020-01-09 13:13:05 +00:00
|
|
|
case REGISTRATION_INVALID_TOKEN:
|
|
|
|
return R.string.account_status_regis_invalid_token;
|
2018-09-27 15:39:49 +00:00
|
|
|
case TLS_ERROR:
|
|
|
|
return R.string.account_status_tls_error;
|
2021-04-30 07:53:19 +00:00
|
|
|
case TLS_ERROR_DOMAIN:
|
|
|
|
return R.string.account_status_tls_error_domain;
|
2018-09-27 15:39:49 +00:00
|
|
|
case INCOMPATIBLE_SERVER:
|
|
|
|
return R.string.account_status_incompatible_server;
|
2022-08-29 16:53:34 +00:00
|
|
|
case INCOMPATIBLE_CLIENT:
|
|
|
|
return R.string.account_status_incompatible_client;
|
2018-09-27 15:39:49 +00:00
|
|
|
case TOR_NOT_AVAILABLE:
|
|
|
|
return R.string.account_status_tor_unavailable;
|
|
|
|
case BIND_FAILURE:
|
|
|
|
return R.string.account_status_bind_failure;
|
|
|
|
case SESSION_FAILURE:
|
|
|
|
return R.string.session_failure;
|
|
|
|
case DOWNGRADE_ATTACK:
|
|
|
|
return R.string.sasl_downgrade;
|
|
|
|
case HOST_UNKNOWN:
|
|
|
|
return R.string.account_status_host_unknown;
|
|
|
|
case POLICY_VIOLATION:
|
|
|
|
return R.string.account_status_policy_violation;
|
|
|
|
case REGISTRATION_PLEASE_WAIT:
|
|
|
|
return R.string.registration_please_wait;
|
|
|
|
case REGISTRATION_PASSWORD_TOO_WEAK:
|
|
|
|
return R.string.registration_password_too_weak;
|
|
|
|
case STREAM_ERROR:
|
|
|
|
return R.string.account_status_stream_error;
|
|
|
|
case STREAM_OPENING_ERROR:
|
|
|
|
return R.string.account_status_stream_opening_error;
|
|
|
|
case PAYMENT_REQUIRED:
|
|
|
|
return R.string.payment_required;
|
|
|
|
case MISSING_INTERNET_PERMISSION:
|
|
|
|
return R.string.missing_internet_permission;
|
2022-06-14 06:39:55 +00:00
|
|
|
case TEMPORARY_AUTH_FAILURE:
|
|
|
|
return R.string.account_status_temporary_auth_failure;
|
2018-09-27 15:39:49 +00:00
|
|
|
default:
|
|
|
|
return R.string.account_status_unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|