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_NO_INTERNET = 2;
 39	public static final int STATUS_UNAUTHORIZED = 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	public static final int STATUS_REGISTRATION_NOT_SUPPORTED = 10;
 49	
 50	protected String username;
 51	protected String server;
 52	protected String password;
 53	protected int options = 0;
 54	protected String rosterVersion;
 55	protected String resource = "mobile";
 56	protected int status = -1;
 57	protected JSONObject keys = new JSONObject();
 58	
 59	protected boolean online = false;
 60	
 61	transient OtrEngine otrEngine = null;
 62	transient XmppConnection xmppConnection = null;
 63	transient protected Presences presences = new Presences();
 64
 65	private String otrFingerprint;
 66	
 67	public Account() {
 68		this.uuid = "0";
 69	}
 70	
 71	public Account(String username, String server, String password) {
 72		this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
 73	}
 74	public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) {
 75		this.uuid = uuid;
 76		this.username = username;
 77		this.server = server;
 78		this.password = password;
 79		this.options = options;
 80		this.rosterVersion = rosterVersion;
 81		try {
 82			this.keys = new JSONObject(keys);
 83		} catch (JSONException e) {
 84			
 85		}
 86	}
 87	
 88	public boolean isOptionSet(int option) {
 89		return ((options & (1 << option)) != 0);
 90	}
 91	
 92	public void setOption(int option, boolean value) {
 93		if (value) {
 94			this.options |= 1 << option;
 95		} else {
 96			this.options &= ~(1 << option);
 97		}
 98	}
 99	
100	public String getUsername() {
101		return username;
102	}
103
104	public void setUsername(String username) {
105		this.username = username;
106	}
107
108	public String getServer() {
109		return server;
110	}
111
112	public void setServer(String server) {
113		this.server = server;
114	}
115
116	public String getPassword() {
117		return password;
118	}
119
120	public void setPassword(String password) {
121		this.password = password;
122	}
123	
124	public void setStatus(int status) {
125		this.status = status;
126	}
127	
128	public int getStatus() {
129		if (isOptionSet(OPTION_DISABLED)) {
130			return STATUS_DISABLED;
131		} else {
132			return this.status;
133		}
134	}
135	
136	public boolean hasErrorStatus() {
137		return getStatus() > STATUS_NO_INTERNET;
138	}
139	
140	public void setResource(String resource) {
141		this.resource = resource;
142	}
143	
144	public String getResource() {
145		return this.resource;
146	}
147	
148	public String getJid() {
149		return username+"@"+server;
150	}
151	
152	public JSONObject getKeys() {
153		return keys;
154	}
155	
156	public String getSSLFingerprint() {
157		if (keys.has("ssl_cert")) {
158			try {
159				return keys.getString("ssl_cert");
160			} catch (JSONException e) {
161				return null;
162			}
163		} else {
164			return null;
165		}
166	}
167	
168	public void setSSLCertFingerprint(String fingerprint) {
169		this.setKey("ssl_cert", fingerprint);
170	}
171	
172	public boolean setKey(String keyName, String keyValue) {
173		try {
174			this.keys.put(keyName, keyValue);
175			return true;
176		} catch (JSONException e) {
177			return false;
178		}
179	}
180
181	@Override
182	public ContentValues getContentValues() {
183		ContentValues values = new ContentValues();
184		values.put(UUID,uuid);
185		values.put(USERNAME, username);
186		values.put(SERVER, server);
187		values.put(PASSWORD, password);
188		values.put(OPTIONS,options);
189		values.put(KEYS,this.keys.toString());
190		values.put(ROSTERVERSION,rosterVersion);
191		return values;
192	}
193	
194	public static Account fromCursor(Cursor cursor) {
195		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
196				cursor.getString(cursor.getColumnIndex(USERNAME)),
197				cursor.getString(cursor.getColumnIndex(SERVER)),
198				cursor.getString(cursor.getColumnIndex(PASSWORD)),
199				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
200				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
201				cursor.getString(cursor.getColumnIndex(KEYS))
202				);
203	}
204
205	
206	public OtrEngine getOtrEngine(Context context) {
207		if (otrEngine==null) {
208			otrEngine = new OtrEngine(context,this);
209		}
210		return this.otrEngine;
211	}
212
213	public XmppConnection getXmppConnection() {
214		return this.xmppConnection;
215	}
216
217	public void setXmppConnection(XmppConnection connection) {
218		this.xmppConnection = connection;
219	}
220
221	public String getFullJid() {
222		return this.getJid()+"/"+this.resource;
223	}
224	
225	public String getOtrFingerprint() {
226		if (this.otrFingerprint == null) {
227			try {
228				DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
229				if (pubkey == null) {
230					return null;
231				}
232				StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
233				builder.insert(8, " ");
234				builder.insert(17, " ");
235				builder.insert(26, " ");
236				builder.insert(35, " ");
237				this.otrFingerprint = builder.toString();
238			} catch (OtrCryptoException e) {
239				
240			}
241		}
242		return this.otrFingerprint;
243	}
244
245	public String getRosterVersion() {
246		if (this.rosterVersion==null) {
247			return "";
248		} else {
249			return this.rosterVersion;
250		}
251	}
252	
253	public void setRosterVersion(String version) {
254		this.rosterVersion = version;
255	}
256
257	public String getOtrFingerprint(Context applicationContext) {
258		this.getOtrEngine(applicationContext);
259		return this.getOtrFingerprint();
260	}
261	
262	public void updatePresence(String resource, int status) {
263		this.presences.updatePresence(resource, status);
264	}
265
266	public void removePresence(String resource) {
267		this.presences.removePresence(resource);
268	}
269	
270	public void clearPresences() {
271		this.presences = new Presences();
272	}
273
274	public int countPresences() {
275		return this.presences.size();
276	}
277}