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