Account.java

  1package eu.siacs.conversations.entities;
  2
  3import java.security.interfaces.DSAPublicKey;
  4
  5import net.java.otr4j.crypto.OtrCryptoEngineImpl;
  6import net.java.otr4j.crypto.OtrCryptoException;
  7
  8import org.json.JSONException;
  9import org.json.JSONObject;
 10
 11import eu.siacs.conversations.crypto.OtrEngine;
 12import eu.siacs.conversations.xmpp.XmppConnection;
 13import android.content.ContentValues;
 14import android.content.Context;
 15import android.database.Cursor;
 16
 17public class Account  extends AbstractEntity{
 18
 19	private static final long serialVersionUID = 6174825093869578035L;
 20	
 21	public static final String TABLENAME = "accounts";
 22	
 23	public static final String USERNAME = "username";
 24	public static final String SERVER = "server";
 25	public static final String PASSWORD = "password";
 26	public static final String OPTIONS = "options";
 27	public static final String ROSTERVERSION = "rosterversion";
 28	public static final String KEYS = "keys";
 29	
 30	public static final int OPTION_USETLS = 0;
 31	public static final int OPTION_DISABLED = 1;
 32	public static final int OPTION_REGISTER = 2;
 33	
 34	public static final int STATUS_CONNECTING = 0;
 35	public static final int STATUS_DISABLED = -2;
 36	public static final int STATUS_OFFLINE = -1;
 37	public static final int STATUS_ONLINE = 1;
 38	public static final int STATUS_UNAUTHORIZED = 2;
 39	public static final int STATUS_NO_INTERNET = 3;
 40	public static final int STATUS_TLS_ERROR = 4;
 41	public static final int STATUS_SERVER_NOT_FOUND = 5;
 42
 43	public static final int STATUS_SERVER_REQUIRES_TLS = 6;
 44
 45	public static final int STATUS_REGISTRATION_FAILED = 7;
 46	public static final int STATUS_REGISTRATION_CONFLICT = 8;
 47	public static final int STATUS_REGISTRATION_SUCCESSFULL = 9;
 48	
 49	protected String username;
 50	protected String server;
 51	protected String password;
 52	protected int options = 0;
 53	protected String rosterVersion;
 54	protected String resource;
 55	protected int status = -1;
 56	protected JSONObject keys = new JSONObject();
 57	
 58	protected boolean online = false;
 59	
 60	transient OtrEngine otrEngine = null;
 61	transient XmppConnection xmppConnection = null;
 62
 63	private String otrFingerprint;
 64	
 65	public Account() {
 66		this.uuid = "0";
 67	}
 68	
 69	public Account(String username, String server, String password) {
 70		this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
 71	}
 72	public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) {
 73		this.uuid = uuid;
 74		this.username = username;
 75		this.server = server;
 76		this.password = password;
 77		this.options = options;
 78		this.rosterVersion = rosterVersion;
 79		try {
 80			this.keys = new JSONObject(keys);
 81		} catch (JSONException e) {
 82			
 83		}
 84	}
 85	
 86	public boolean isOptionSet(int option) {
 87		return ((options & (1 << option)) != 0);
 88	}
 89	
 90	public void setOption(int option, boolean value) {
 91		if (value) {
 92			this.options |= 1 << option;
 93		} else {
 94			this.options &= ~(1 << option);
 95		}
 96	}
 97	
 98	public String getUsername() {
 99		return username;
100	}
101
102	public void setUsername(String username) {
103		this.username = username;
104	}
105
106	public String getServer() {
107		return server;
108	}
109
110	public void setServer(String server) {
111		this.server = server;
112	}
113
114	public String getPassword() {
115		return password;
116	}
117
118	public void setPassword(String password) {
119		this.password = password;
120	}
121	
122	public void setStatus(int status) {
123		this.status = status;
124	}
125	
126	public int getStatus() {
127		if (isOptionSet(OPTION_DISABLED)) {
128			return STATUS_DISABLED;
129		} else {
130			return this.status;
131		}
132	}
133	
134	public void setResource(String resource) {
135		this.resource = resource;
136	}
137	
138	public String getJid() {
139		return username+"@"+server;
140	}
141	
142	public JSONObject getKeys() {
143		return keys;
144	}
145	
146	public String getSSLFingerprint() {
147		if (keys.has("ssl_cert")) {
148			try {
149				return keys.getString("ssl_cert");
150			} catch (JSONException e) {
151				return null;
152			}
153		} else {
154			return null;
155		}
156	}
157	
158	public void setSSLCertFingerprint(String fingerprint) {
159		this.setKey("ssl_cert", fingerprint);
160	}
161	
162	public boolean setKey(String keyName, String keyValue) {
163		try {
164			this.keys.put(keyName, keyValue);
165			return true;
166		} catch (JSONException e) {
167			return false;
168		}
169	}
170
171	@Override
172	public ContentValues getContentValues() {
173		ContentValues values = new ContentValues();
174		values.put(UUID,uuid);
175		values.put(USERNAME, username);
176		values.put(SERVER, server);
177		values.put(PASSWORD, password);
178		values.put(OPTIONS,options);
179		values.put(KEYS,this.keys.toString());
180		values.put(ROSTERVERSION,rosterVersion);
181		return values;
182	}
183	
184	public static Account fromCursor(Cursor cursor) {
185		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
186				cursor.getString(cursor.getColumnIndex(USERNAME)),
187				cursor.getString(cursor.getColumnIndex(SERVER)),
188				cursor.getString(cursor.getColumnIndex(PASSWORD)),
189				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
190				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
191				cursor.getString(cursor.getColumnIndex(KEYS))
192				);
193	}
194
195	
196	public OtrEngine getOtrEngine(Context context) {
197		if (otrEngine==null) {
198			otrEngine = new OtrEngine(context,this);
199		}
200		return this.otrEngine;
201	}
202
203	public XmppConnection getXmppConnection() {
204		return this.xmppConnection;
205	}
206
207	public void setXmppConnection(XmppConnection connection) {
208		this.xmppConnection = connection;
209	}
210
211	public String getFullJid() {
212		return this.getJid()+"/"+this.resource;
213	}
214	
215	public String getOtrFingerprint() {
216		if (this.otrFingerprint == null) {
217			try {
218				DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
219				StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
220				builder.insert(8, " ");
221				builder.insert(17, " ");
222				builder.insert(26, " ");
223				builder.insert(35, " ");
224				this.otrFingerprint = builder.toString();
225			} catch (OtrCryptoException e) {
226				
227			}
228		}
229		return this.otrFingerprint;
230	}
231
232	public String getRosterVersion() {
233		if (this.rosterVersion==null) {
234			return "";
235		} else {
236			return this.rosterVersion;
237		}
238	}
239	
240	public void setRosterVersion(String version) {
241		this.rosterVersion = version;
242	}
243}