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