Account.java

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