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