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