2014-02-28 17:46:01 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
2014-01-24 22:58:51 +00:00
|
|
|
|
2014-02-16 15:32:15 +00:00
|
|
|
import java.security.interfaces.DSAPublicKey;
|
|
|
|
|
|
|
|
import net.java.otr4j.crypto.OtrCryptoEngineImpl;
|
|
|
|
import net.java.otr4j.crypto.OtrCryptoException;
|
|
|
|
|
2014-02-13 22:40:08 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.crypto.OtrEngine;
|
|
|
|
import eu.siacs.conversations.xmpp.XmppConnection;
|
2014-01-25 18:33:12 +00:00
|
|
|
import android.content.ContentValues;
|
2014-02-13 22:40:08 +00:00
|
|
|
import android.content.Context;
|
2014-01-28 18:21:54 +00:00
|
|
|
import android.database.Cursor;
|
2014-01-24 22:58:51 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
public class Account extends AbstractEntity{
|
2014-01-24 22:58:51 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
private static final long serialVersionUID = 6174825093869578035L;
|
|
|
|
|
2014-01-28 18:21:54 +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";
|
2014-02-02 15:05:15 +00:00
|
|
|
public static final String OPTIONS = "options";
|
|
|
|
public static final String ROSTERVERSION = "rosterversion";
|
2014-02-13 22:40:08 +00:00
|
|
|
public static final String KEYS = "keys";
|
2014-01-28 18:21:54 +00:00
|
|
|
|
2014-02-02 16:53:34 +00:00
|
|
|
public static final int OPTION_USETLS = 0;
|
2014-02-04 20:44:16 +00:00
|
|
|
public static final int OPTION_DISABLED = 1;
|
2014-03-13 02:52:41 +00:00
|
|
|
public static final int OPTION_REGISTER = 2;
|
2014-04-03 16:16:14 +00:00
|
|
|
public static final int OPTION_USECOMPRESSION = 3;
|
2014-02-02 16:53:34 +00:00
|
|
|
|
2014-03-06 02:30:03 +00:00
|
|
|
public static final int STATUS_CONNECTING = 0;
|
|
|
|
public static final int STATUS_DISABLED = -2;
|
|
|
|
public static final int STATUS_OFFLINE = -1;
|
2014-02-04 14:09:50 +00:00
|
|
|
public static final int STATUS_ONLINE = 1;
|
2014-04-03 07:21:45 +00:00
|
|
|
public static final int STATUS_NO_INTERNET = 2;
|
|
|
|
public static final int STATUS_UNAUTHORIZED = 3;
|
2014-02-04 14:09:50 +00:00
|
|
|
public static final int STATUS_TLS_ERROR = 4;
|
|
|
|
public static final int STATUS_SERVER_NOT_FOUND = 5;
|
2014-03-06 14:11:56 +00:00
|
|
|
|
|
|
|
public static final int STATUS_SERVER_REQUIRES_TLS = 6;
|
2014-03-13 16:29:22 +00:00
|
|
|
|
|
|
|
public static final int STATUS_REGISTRATION_FAILED = 7;
|
|
|
|
public static final int STATUS_REGISTRATION_CONFLICT = 8;
|
|
|
|
public static final int STATUS_REGISTRATION_SUCCESSFULL = 9;
|
2014-03-14 17:56:52 +00:00
|
|
|
public static final int STATUS_REGISTRATION_NOT_SUPPORTED = 10;
|
2014-02-04 14:09:50 +00:00
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
protected String username;
|
|
|
|
protected String server;
|
|
|
|
protected String password;
|
2014-02-10 14:24:34 +00:00
|
|
|
protected int options = 0;
|
2014-02-02 15:05:15 +00:00
|
|
|
protected String rosterVersion;
|
2014-03-21 21:14:52 +00:00
|
|
|
protected String resource = "mobile";
|
2014-03-06 02:30:03 +00:00
|
|
|
protected int status = -1;
|
2014-02-13 22:40:08 +00:00
|
|
|
protected JSONObject keys = new JSONObject();
|
2014-01-28 18:21:54 +00:00
|
|
|
|
|
|
|
protected boolean online = false;
|
|
|
|
|
2014-02-13 22:40:08 +00:00
|
|
|
transient OtrEngine otrEngine = null;
|
|
|
|
transient XmppConnection xmppConnection = null;
|
2014-03-19 15:16:40 +00:00
|
|
|
transient protected Presences presences = new Presences();
|
2014-02-16 15:32:15 +00:00
|
|
|
|
|
|
|
private String otrFingerprint;
|
2014-02-13 22:40:08 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
public Account() {
|
2014-01-28 18:21:54 +00:00
|
|
|
this.uuid = "0";
|
|
|
|
}
|
|
|
|
|
|
|
|
public Account(String username, String server, String password) {
|
2014-02-13 22:40:08 +00:00
|
|
|
this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-02-13 22:40:08 +00:00
|
|
|
public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) {
|
2014-01-28 18:21:54 +00:00
|
|
|
this.uuid = uuid;
|
|
|
|
this.username = username;
|
|
|
|
this.server = server;
|
|
|
|
this.password = password;
|
2014-02-02 15:05:15 +00:00
|
|
|
this.options = options;
|
|
|
|
this.rosterVersion = rosterVersion;
|
2014-02-13 22:40:08 +00:00
|
|
|
try {
|
|
|
|
this.keys = new JSONObject(keys);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
|
|
|
|
2014-02-02 16:53:34 +00:00
|
|
|
public boolean isOptionSet(int option) {
|
|
|
|
return ((options & (1 << option)) != 0);
|
|
|
|
}
|
|
|
|
|
2014-02-04 20:44:16 +00:00
|
|
|
public void setOption(int option, boolean value) {
|
|
|
|
if (value) {
|
2014-02-21 18:14:03 +00:00
|
|
|
this.options |= 1 << option;
|
2014-02-04 20:44:16 +00:00
|
|
|
} else {
|
2014-02-21 18:14:03 +00:00
|
|
|
this.options &= ~(1 << option);
|
2014-02-04 20:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
public String getUsername() {
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUsername(String username) {
|
|
|
|
this.username = username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getServer() {
|
|
|
|
return server;
|
2014-01-25 18:33:12 +00:00
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
|
|
|
|
public void setServer(String server) {
|
|
|
|
this.server = server;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPassword(String password) {
|
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
|
2014-02-04 14:09:50 +00:00
|
|
|
public void setStatus(int status) {
|
|
|
|
this.status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getStatus() {
|
2014-02-04 20:44:16 +00:00
|
|
|
if (isOptionSet(OPTION_DISABLED)) {
|
|
|
|
return STATUS_DISABLED;
|
|
|
|
} else {
|
|
|
|
return this.status;
|
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 07:21:45 +00:00
|
|
|
public boolean hasErrorStatus() {
|
|
|
|
return getStatus() > STATUS_NO_INTERNET;
|
|
|
|
}
|
|
|
|
|
2014-02-04 14:09:50 +00:00
|
|
|
public void setResource(String resource) {
|
|
|
|
this.resource = resource;
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 21:14:52 +00:00
|
|
|
public String getResource() {
|
|
|
|
return this.resource;
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
public String getJid() {
|
|
|
|
return username+"@"+server;
|
|
|
|
}
|
2014-02-13 22:40:08 +00:00
|
|
|
|
|
|
|
public JSONObject getKeys() {
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2014-03-07 13:24:33 +00:00
|
|
|
public String getSSLFingerprint() {
|
|
|
|
if (keys.has("ssl_cert")) {
|
|
|
|
try {
|
|
|
|
return keys.getString("ssl_cert");
|
|
|
|
} catch (JSONException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSSLCertFingerprint(String fingerprint) {
|
|
|
|
this.setKey("ssl_cert", fingerprint);
|
|
|
|
}
|
|
|
|
|
2014-02-27 23:22:56 +00:00
|
|
|
public boolean setKey(String keyName, String keyValue) {
|
|
|
|
try {
|
|
|
|
this.keys.put(keyName, keyValue);
|
|
|
|
return true;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-13 22:40:08 +00:00
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
@Override
|
|
|
|
public ContentValues getContentValues() {
|
2014-01-28 18:21:54 +00:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(UUID,uuid);
|
|
|
|
values.put(USERNAME, username);
|
|
|
|
values.put(SERVER, server);
|
|
|
|
values.put(PASSWORD, password);
|
2014-02-04 20:44:16 +00:00
|
|
|
values.put(OPTIONS,options);
|
2014-02-13 22:40:08 +00:00
|
|
|
values.put(KEYS,this.keys.toString());
|
|
|
|
values.put(ROSTERVERSION,rosterVersion);
|
2014-01-28 18:21:54 +00:00
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Account fromCursor(Cursor cursor) {
|
|
|
|
return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
|
|
|
|
cursor.getString(cursor.getColumnIndex(USERNAME)),
|
|
|
|
cursor.getString(cursor.getColumnIndex(SERVER)),
|
2014-02-02 15:05:15 +00:00
|
|
|
cursor.getString(cursor.getColumnIndex(PASSWORD)),
|
|
|
|
cursor.getInt(cursor.getColumnIndex(OPTIONS)),
|
2014-02-13 22:40:08 +00:00
|
|
|
cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
|
|
|
|
cursor.getString(cursor.getColumnIndex(KEYS))
|
2014-02-02 15:05:15 +00:00
|
|
|
);
|
2014-01-24 22:58:51 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 22:40:08 +00:00
|
|
|
|
|
|
|
public OtrEngine getOtrEngine(Context context) {
|
|
|
|
if (otrEngine==null) {
|
|
|
|
otrEngine = new OtrEngine(context,this);
|
|
|
|
}
|
|
|
|
return this.otrEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XmppConnection getXmppConnection() {
|
|
|
|
return this.xmppConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setXmppConnection(XmppConnection connection) {
|
|
|
|
this.xmppConnection = connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFullJid() {
|
|
|
|
return this.getJid()+"/"+this.resource;
|
|
|
|
}
|
2014-02-16 15:32:15 +00:00
|
|
|
|
|
|
|
public String getOtrFingerprint() {
|
|
|
|
if (this.otrFingerprint == null) {
|
|
|
|
try {
|
|
|
|
DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
|
2014-03-14 20:36:38 +00:00
|
|
|
if (pubkey == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-16 15:32:15 +00:00
|
|
|
StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
|
|
|
|
builder.insert(8, " ");
|
|
|
|
builder.insert(17, " ");
|
|
|
|
builder.insert(26, " ");
|
|
|
|
builder.insert(35, " ");
|
|
|
|
this.otrFingerprint = builder.toString();
|
|
|
|
} catch (OtrCryptoException e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.otrFingerprint;
|
|
|
|
}
|
2014-02-19 00:35:23 +00:00
|
|
|
|
|
|
|
public String getRosterVersion() {
|
|
|
|
if (this.rosterVersion==null) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return this.rosterVersion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRosterVersion(String version) {
|
|
|
|
this.rosterVersion = version;
|
|
|
|
}
|
2014-03-14 20:36:38 +00:00
|
|
|
|
|
|
|
public String getOtrFingerprint(Context applicationContext) {
|
|
|
|
this.getOtrEngine(applicationContext);
|
|
|
|
return this.getOtrFingerprint();
|
|
|
|
}
|
2014-03-19 15:16:40 +00:00
|
|
|
|
|
|
|
public void updatePresence(String resource, int status) {
|
|
|
|
this.presences.updatePresence(resource, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removePresence(String resource) {
|
|
|
|
this.presences.removePresence(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearPresences() {
|
|
|
|
this.presences = new Presences();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int countPresences() {
|
|
|
|
return this.presences.size();
|
|
|
|
}
|
2014-01-24 22:58:51 +00:00
|
|
|
}
|