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