Conversations/src/eu/siacs/conversations/entities/Account.java

291 lines
7.0 KiB
Java
Raw Normal View History

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