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