Account.java

  1package eu.siacs.conversations.entities;
  2
  3import java.security.interfaces.DSAPublicKey;
  4import java.util.List;
  5import java.util.Locale;
  6import java.util.concurrent.CopyOnWriteArrayList;
  7
  8import net.java.otr4j.crypto.OtrCryptoEngineImpl;
  9import net.java.otr4j.crypto.OtrCryptoException;
 10
 11import org.json.JSONException;
 12import org.json.JSONObject;
 13
 14import eu.siacs.conversations.R;
 15import eu.siacs.conversations.crypto.OtrEngine;
 16import eu.siacs.conversations.persistance.FileBackend;
 17import eu.siacs.conversations.utils.UIHelper;
 18import eu.siacs.conversations.xmpp.XmppConnection;
 19import android.content.ContentValues;
 20import android.content.Context;
 21import android.database.Cursor;
 22import android.graphics.Bitmap;
 23
 24public class Account extends AbstractEntity {
 25
 26	public static final String TABLENAME = "accounts";
 27
 28	public static final String USERNAME = "username";
 29	public static final String SERVER = "server";
 30	public static final String PASSWORD = "password";
 31	public static final String OPTIONS = "options";
 32	public static final String ROSTERVERSION = "rosterversion";
 33	public static final String KEYS = "keys";
 34	public static final String AVATAR = "avatar";
 35
 36	public static final int OPTION_USETLS = 0;
 37	public static final int OPTION_DISABLED = 1;
 38	public static final int OPTION_REGISTER = 2;
 39	public static final int OPTION_USECOMPRESSION = 3;
 40
 41	public static final int STATUS_CONNECTING = 0;
 42	public static final int STATUS_DISABLED = -2;
 43	public static final int STATUS_OFFLINE = -1;
 44	public static final int STATUS_ONLINE = 1;
 45	public static final int STATUS_NO_INTERNET = 2;
 46	public static final int STATUS_UNAUTHORIZED = 3;
 47	public static final int STATUS_SERVER_NOT_FOUND = 5;
 48
 49	public static final int STATUS_REGISTRATION_FAILED = 7;
 50	public static final int STATUS_REGISTRATION_CONFLICT = 8;
 51	public static final int STATUS_REGISTRATION_SUCCESSFULL = 9;
 52	public static final int STATUS_REGISTRATION_NOT_SUPPORTED = 10;
 53
 54	protected String username;
 55	protected String server;
 56	protected String password;
 57	protected int options = 0;
 58	protected String rosterVersion;
 59	protected String resource = "mobile";
 60	protected int status = -1;
 61	protected JSONObject keys = new JSONObject();
 62	protected String avatar;
 63
 64	protected boolean online = false;
 65
 66	transient OtrEngine otrEngine = null;
 67	transient XmppConnection xmppConnection = null;
 68	transient protected Presences presences = new Presences();
 69
 70	private String otrFingerprint;
 71
 72	private Roster roster = null;
 73
 74	private List<Bookmark> bookmarks = new CopyOnWriteArrayList<Bookmark>();
 75
 76	public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<Conversation>();
 77	public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<Conversation>();
 78
 79	public Account() {
 80		this.uuid = "0";
 81	}
 82
 83	public Account(String username, String server, String password) {
 84		this(java.util.UUID.randomUUID().toString(), username, server,
 85				password, 0, null, "", null);
 86	}
 87
 88	public Account(String uuid, String username, String server,
 89			String password, int options, String rosterVersion, String keys,
 90			String avatar) {
 91		this.uuid = uuid;
 92		this.username = username;
 93		this.server = server;
 94		this.password = password;
 95		this.options = options;
 96		this.rosterVersion = rosterVersion;
 97		try {
 98			this.keys = new JSONObject(keys);
 99		} catch (JSONException e) {
100
101		}
102		this.avatar = avatar;
103	}
104
105	public boolean isOptionSet(int option) {
106		return ((options & (1 << option)) != 0);
107	}
108
109	public void setOption(int option, boolean value) {
110		if (value) {
111			this.options |= 1 << option;
112		} else {
113			this.options &= ~(1 << option);
114		}
115	}
116
117	public String getUsername() {
118		return username;
119	}
120
121	public void setUsername(String username) {
122		this.username = username;
123	}
124
125	public String getServer() {
126		return server;
127	}
128
129	public void setServer(String server) {
130		this.server = server;
131	}
132
133	public String getPassword() {
134		return password;
135	}
136
137	public void setPassword(String password) {
138		this.password = password;
139	}
140
141	public void setStatus(int status) {
142		this.status = status;
143	}
144
145	public int getStatus() {
146		if (isOptionSet(OPTION_DISABLED)) {
147			return STATUS_DISABLED;
148		} else {
149			return this.status;
150		}
151	}
152
153	public boolean errorStatus() {
154		int s = getStatus();
155		return (s == STATUS_REGISTRATION_FAILED
156				|| s == STATUS_REGISTRATION_CONFLICT
157				|| s == STATUS_REGISTRATION_NOT_SUPPORTED
158				|| s == STATUS_SERVER_NOT_FOUND || s == STATUS_UNAUTHORIZED);
159	}
160
161	public boolean hasErrorStatus() {
162		return getStatus() > STATUS_NO_INTERNET
163				&& (getXmppConnection().getAttempt() >= 2);
164	}
165
166	public void setResource(String resource) {
167		this.resource = resource;
168	}
169
170	public String getResource() {
171		return this.resource;
172	}
173
174	public String getJid() {
175		return username.toLowerCase(Locale.getDefault()) + "@"
176				+ server.toLowerCase(Locale.getDefault());
177	}
178
179	public JSONObject getKeys() {
180		return keys;
181	}
182
183	public String getSSLFingerprint() {
184		if (keys.has("ssl_cert")) {
185			try {
186				return keys.getString("ssl_cert");
187			} catch (JSONException e) {
188				return null;
189			}
190		} else {
191			return null;
192		}
193	}
194
195	public void setSSLCertFingerprint(String fingerprint) {
196		this.setKey("ssl_cert", fingerprint);
197	}
198
199	public boolean setKey(String keyName, String keyValue) {
200		try {
201			this.keys.put(keyName, keyValue);
202			return true;
203		} catch (JSONException e) {
204			return false;
205		}
206	}
207
208	@Override
209	public ContentValues getContentValues() {
210		ContentValues values = new ContentValues();
211		values.put(UUID, uuid);
212		values.put(USERNAME, username);
213		values.put(SERVER, server);
214		values.put(PASSWORD, password);
215		values.put(OPTIONS, options);
216		values.put(KEYS, this.keys.toString());
217		values.put(ROSTERVERSION, rosterVersion);
218		values.put(AVATAR, avatar);
219		return values;
220	}
221
222	public static Account fromCursor(Cursor cursor) {
223		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
224				cursor.getString(cursor.getColumnIndex(USERNAME)),
225				cursor.getString(cursor.getColumnIndex(SERVER)),
226				cursor.getString(cursor.getColumnIndex(PASSWORD)),
227				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
228				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
229				cursor.getString(cursor.getColumnIndex(KEYS)),
230				cursor.getString(cursor.getColumnIndex(AVATAR)));
231	}
232
233	public OtrEngine getOtrEngine(Context context) {
234		if (otrEngine == null) {
235			otrEngine = new OtrEngine(context, this);
236		}
237		return this.otrEngine;
238	}
239
240	public XmppConnection getXmppConnection() {
241		return this.xmppConnection;
242	}
243
244	public void setXmppConnection(XmppConnection connection) {
245		this.xmppConnection = connection;
246	}
247
248	public String getFullJid() {
249		return this.getJid() + "/" + this.resource;
250	}
251
252	public String getOtrFingerprint() {
253		if (this.otrFingerprint == null) {
254			try {
255				DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine
256						.getPublicKey();
257				if (pubkey == null) {
258					return null;
259				}
260				StringBuilder builder = new StringBuilder(
261						new OtrCryptoEngineImpl().getFingerprint(pubkey));
262				builder.insert(8, " ");
263				builder.insert(17, " ");
264				builder.insert(26, " ");
265				builder.insert(35, " ");
266				this.otrFingerprint = builder.toString();
267			} catch (OtrCryptoException e) {
268
269			}
270		}
271		return this.otrFingerprint;
272	}
273
274	public String getRosterVersion() {
275		if (this.rosterVersion == null) {
276			return "";
277		} else {
278			return this.rosterVersion;
279		}
280	}
281
282	public void setRosterVersion(String version) {
283		this.rosterVersion = version;
284	}
285
286	public String getOtrFingerprint(Context applicationContext) {
287		this.getOtrEngine(applicationContext);
288		return this.getOtrFingerprint();
289	}
290
291	public void updatePresence(String resource, int status) {
292		this.presences.updatePresence(resource, status);
293	}
294
295	public void removePresence(String resource) {
296		this.presences.removePresence(resource);
297	}
298
299	public void clearPresences() {
300		this.presences = new Presences();
301	}
302
303	public int countPresences() {
304		return this.presences.size();
305	}
306
307	public String getPgpSignature() {
308		if (keys.has("pgp_signature")) {
309			try {
310				return keys.getString("pgp_signature");
311			} catch (JSONException e) {
312				return null;
313			}
314		} else {
315			return null;
316		}
317	}
318
319	public Roster getRoster() {
320		if (this.roster == null) {
321			this.roster = new Roster(this);
322		}
323		return this.roster;
324	}
325
326	public void setBookmarks(List<Bookmark> bookmarks) {
327		this.bookmarks = bookmarks;
328	}
329
330	public List<Bookmark> getBookmarks() {
331		return this.bookmarks;
332	}
333
334	public boolean hasBookmarkFor(String conferenceJid) {
335		for (Bookmark bmark : this.bookmarks) {
336			if (bmark.getJid().equals(conferenceJid)) {
337				return true;
338			}
339		}
340		return false;
341	}
342
343	public Bitmap getImage(Context context, int size) {
344		if (this.avatar != null) {
345			Bitmap bm = FileBackend.getAvatar(this.avatar, size, context);
346			if (bm == null) {
347				return UIHelper.getContactPicture(getJid(), size, context,
348						false);
349			} else {
350				return bm;
351			}
352		} else {
353			return UIHelper.getContactPicture(getJid(), size, context, false);
354		}
355	}
356
357	public boolean setAvatar(String filename) {
358		if (this.avatar != null && this.avatar.equals(filename)) {
359			return false;
360		} else {
361			this.avatar = filename;
362			return true;
363		}
364	}
365
366	public String getAvatar() {
367		return this.avatar;
368	}
369
370	public int getReadableStatusId() {
371		switch (getStatus()) {
372
373		case Account.STATUS_DISABLED:
374			return R.string.account_status_disabled;
375		case Account.STATUS_ONLINE:
376			return R.string.account_status_online;
377		case Account.STATUS_CONNECTING:
378			return R.string.account_status_connecting;
379		case Account.STATUS_OFFLINE:
380			return R.string.account_status_offline;
381		case Account.STATUS_UNAUTHORIZED:
382			return R.string.account_status_unauthorized;
383		case Account.STATUS_SERVER_NOT_FOUND:
384			return R.string.account_status_not_found;
385		case Account.STATUS_NO_INTERNET:
386			return R.string.account_status_no_internet;
387		case Account.STATUS_REGISTRATION_FAILED:
388			return R.string.account_status_regis_fail;
389		case Account.STATUS_REGISTRATION_CONFLICT:
390			return R.string.account_status_regis_conflict;
391		case Account.STATUS_REGISTRATION_SUCCESSFULL:
392			return R.string.account_status_regis_success;
393		case Account.STATUS_REGISTRATION_NOT_SUPPORTED:
394			return R.string.account_status_regis_not_sup;
395		default:
396			return R.string.account_status_unknown;
397		}
398	}
399}