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