1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
package de.pixart.messenger.entities;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.SystemClock;
import android.util.Pair;
import net.java.otr4j.crypto.OtrCryptoEngineImpl;
import net.java.otr4j.crypto.OtrCryptoException;
import org.json.JSONException;
import org.json.JSONObject;
import java.security.PublicKey;
import java.security.interfaces.DSAPublicKey;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import de.pixart.messenger.R;
import de.pixart.messenger.crypto.OtrService;
import de.pixart.messenger.crypto.PgpDecryptionService;
import de.pixart.messenger.crypto.axolotl.AxolotlService;
import de.pixart.messenger.crypto.axolotl.XmppAxolotlSession;
import de.pixart.messenger.services.XmppConnectionService;
import de.pixart.messenger.utils.XmppUri;
import de.pixart.messenger.xmpp.XmppConnection;
import de.pixart.messenger.xmpp.jid.InvalidJidException;
import de.pixart.messenger.xmpp.jid.Jid;
public class Account extends AbstractEntity {
public static final String TABLENAME = "accounts";
public static final String USERNAME = "username";
public static final String SERVER = "server";
public static final String PASSWORD = "password";
public static final String OPTIONS = "options";
public static final String ROSTERVERSION = "rosterversion";
public static final String KEYS = "keys";
public static final String AVATAR = "avatar";
public static final String DISPLAY_NAME = "display_name";
public static final String HOSTNAME = "hostname";
public static final String PORT = "port";
public static final String STATUS = "status";
public static final String STATUS_MESSAGE = "status_message";
public static final String PINNED_MECHANISM_KEY = "pinned_mechanism";
public static final int OPTION_USETLS = 0;
public static final int OPTION_DISABLED = 1;
public static final int OPTION_REGISTER = 2;
public static final int OPTION_USECOMPRESSION = 3;
public static final int OPTION_MAGIC_CREATE = 4;
public final HashSet<Pair<String, String>> inProgressDiscoFetches = new HashSet<>();
public boolean httpUploadAvailable(long filesize) {
return xmppConnection != null && xmppConnection.getFeatures().httpUpload(filesize);
}
public boolean httpUploadAvailable() {
return httpUploadAvailable(0);
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
public XmppConnection.Identity getServerIdentity() {
if (xmppConnection == null) {
return XmppConnection.Identity.UNKNOWN;
} else {
return xmppConnection.getServerIdentity();
}
}
public Contact getSelfContact() {
return getRoster().getContact(jid);
}
public boolean hasPendingPgpIntent(Conversation conversation) {
return pgpDecryptionService != null && pgpDecryptionService.hasPendingIntent(conversation);
}
public boolean isPgpDecryptionServiceConnected() {
return pgpDecryptionService != null && pgpDecryptionService.isConnected();
}
public boolean setShowErrorNotification(boolean newValue) {
boolean oldValue = showErrorNotification();
setKey("show_error", Boolean.toString(newValue));
return newValue != oldValue;
}
public boolean showErrorNotification() {
String key = getKey("show_error");
return key == null || Boolean.parseBoolean(key);
}
public enum State {
DISABLED,
OFFLINE,
CONNECTING,
ONLINE,
NO_INTERNET,
UNAUTHORIZED(true),
SERVER_NOT_FOUND(true),
REGISTRATION_FAILED(true),
REGISTRATION_CONFLICT(true),
REGISTRATION_SUCCESSFUL,
REGISTRATION_NOT_SUPPORTED(true),
SECURITY_ERROR(true),
INCOMPATIBLE_SERVER(true),
TOR_NOT_AVAILABLE(true),
BIND_FAILURE(true),
HOST_UNKNOWN(true),
REGISTRATION_PLEASE_WAIT(true),
STREAM_ERROR(true),
POLICY_VIOLATION(true),
REGISTRATION_PASSWORD_TOO_WEAK(true),
PAYMENT_REQUIRED(true),
MISSING_INTERNET_PERMISSION(true);
private final boolean isError;
public boolean isError() {
return this.isError;
}
State(final boolean isError) {
this.isError = isError;
}
State() {
this(false);
}
public int getReadableId() {
switch (this) {
case DISABLED:
return R.string.account_status_disabled;
case ONLINE:
return R.string.account_status_online;
case CONNECTING:
return R.string.account_status_connecting;
case OFFLINE:
return R.string.account_status_offline;
case UNAUTHORIZED:
return R.string.account_status_unauthorized;
case SERVER_NOT_FOUND:
return R.string.account_status_not_found;
case NO_INTERNET:
return R.string.account_status_no_internet;
case REGISTRATION_FAILED:
return R.string.account_status_regis_fail;
case REGISTRATION_CONFLICT:
return R.string.account_status_regis_conflict;
case REGISTRATION_SUCCESSFUL:
return R.string.account_status_regis_success;
case REGISTRATION_NOT_SUPPORTED:
return R.string.account_status_regis_not_sup;
case SECURITY_ERROR:
return R.string.account_status_security_error;
case INCOMPATIBLE_SERVER:
return R.string.account_status_incompatible_server;
case TOR_NOT_AVAILABLE:
return R.string.account_status_tor_unavailable;
case BIND_FAILURE:
return R.string.account_status_bind_failure;
case HOST_UNKNOWN:
return R.string.account_status_host_unknown;
case POLICY_VIOLATION:
return R.string.account_status_policy_violation;
case REGISTRATION_PLEASE_WAIT:
return R.string.registration_please_wait;
case REGISTRATION_PASSWORD_TOO_WEAK:
return R.string.registration_password_too_weak;
case STREAM_ERROR:
return R.string.account_status_stream_error;
case PAYMENT_REQUIRED:
return R.string.payment_required;
case MISSING_INTERNET_PERMISSION:
return R.string.missing_internet_permission;
default:
return R.string.account_status_unknown;
}
}
}
public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<>();
public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<>();
private static final String KEY_PGP_SIGNATURE = "pgp_signature";
private static final String KEY_PGP_ID = "pgp_id";
protected Jid jid;
protected String password;
protected int options = 0;
protected String rosterVersion;
protected State status = State.OFFLINE;
protected final JSONObject keys;
protected String avatar;
protected String displayName = null;
protected String hostname = null;
protected int port = 5222;
protected boolean online = false;
private OtrService mOtrService = null;
private AxolotlService axolotlService = null;
private PgpDecryptionService pgpDecryptionService = null;
private XmppConnection xmppConnection = null;
private long mEndGracePeriod = 0L;
private String otrFingerprint;
private final Roster roster = new Roster(this);
private List<Bookmark> bookmarks = new CopyOnWriteArrayList<>();
private final Collection<Jid> blocklist = new CopyOnWriteArraySet<>();
private Presence.Status presenceStatus = Presence.Status.ONLINE;
private String presenceStatusMessage = null;
public Account(final Jid jid, final String password) {
this(java.util.UUID.randomUUID().toString(), jid,
password, 0, null, "", null, null, null, 5222, Presence.Status.ONLINE, null);
}
private Account(final String uuid, final Jid jid,
final String password, final int options, final String rosterVersion, final String keys,
final String avatar, String displayName, String hostname, int port,
final Presence.Status status, String statusMessage) {
this.uuid = uuid;
this.jid = jid;
if (jid.isBareJid()) {
this.setResource("mobile");
}
this.password = password;
this.options = options;
this.rosterVersion = rosterVersion;
JSONObject tmp;
try {
tmp = new JSONObject(keys);
} catch (JSONException e) {
tmp = new JSONObject();
}
this.keys = tmp;
this.avatar = avatar;
this.displayName = displayName;
this.hostname = hostname;
this.port = port;
this.presenceStatus = status;
this.presenceStatusMessage = statusMessage;
}
public static Account fromCursor(final Cursor cursor) {
Jid jid = null;
try {
jid = Jid.fromParts(cursor.getString(cursor.getColumnIndex(USERNAME)),
cursor.getString(cursor.getColumnIndex(SERVER)), "mobile");
} catch (final InvalidJidException ignored) {
}
return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
jid,
cursor.getString(cursor.getColumnIndex(PASSWORD)),
cursor.getInt(cursor.getColumnIndex(OPTIONS)),
cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
cursor.getString(cursor.getColumnIndex(KEYS)),
cursor.getString(cursor.getColumnIndex(AVATAR)),
cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)),
cursor.getString(cursor.getColumnIndex(HOSTNAME)),
cursor.getInt(cursor.getColumnIndex(PORT)),
Presence.Status.fromShowString(cursor.getString(cursor.getColumnIndex(STATUS))),
cursor.getString(cursor.getColumnIndex(STATUS_MESSAGE)));
}
public boolean isOptionSet(final int option) {
return ((options & (1 << option)) != 0);
}
public void setOption(final int option, final boolean value) {
if (value) {
this.options |= 1 << option;
} else {
this.options &= ~(1 << option);
}
}
public String getUsername() {
return jid.getLocalpart();
}
public boolean setJid(final Jid next) {
final Jid prev = this.jid != null ? this.jid.toBareJid() : null;
this.jid = next;
return prev == null || (next != null && !prev.equals(next.toBareJid())
);
}
public Jid getServer() {
return jid.toDomainJid();
}
public String getPassword() {
return password;
}
public void setPassword(final String password) {
this.password = password;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getHostname() {
return this.hostname == null ? "" : this.hostname;
}
public boolean isOnion() {
final Jid server = getServer();
return server != null && server.toString().toLowerCase().endsWith(".onion");
}
public void setPort(int port) {
this.port = port;
}
public int getPort() {
return this.port;
}
public State getStatus() {
if (isOptionSet(OPTION_DISABLED)) {
return State.DISABLED;
} else {
return this.status;
}
}
public State getTrueStatus() {
return this.status;
}
public void setStatus(final State status) {
this.status = status;
}
public boolean errorStatus() {
return getStatus().isError();
}
public boolean hasErrorStatus() {
return getXmppConnection() != null
&& (getStatus().isError() || getStatus() == State.CONNECTING)
&& getXmppConnection().getAttempt() >= 3;
}
public void setPresenceStatus(Presence.Status status) {
this.presenceStatus = status;
}
public Presence.Status getPresenceStatus() {
return this.presenceStatus;
}
public void setPresenceStatusMessage(String message) {
this.presenceStatusMessage = message;
}
public String getPresenceStatusMessage() {
return this.presenceStatusMessage;
}
public String getResource() {
return jid.getResourcepart();
}
public boolean setResource(final String resource) {
final String oldResource = jid.getResourcepart();
if (oldResource == null || !oldResource.equals(resource)) {
try {
jid = Jid.fromParts(jid.getLocalpart(), jid.getDomainpart(), resource);
return true;
} catch (final InvalidJidException ignored) {
return true;
}
}
return false;
}
public Jid getJid() {
return jid;
}
public JSONObject getKeys() {
return keys;
}
public String getKey(final String name) {
synchronized (this.keys) {
return this.keys.optString(name, null);
}
}
public int getKeyAsInt(final String name, int defaultValue) {
String key = getKey(name);
try {
return key == null ? defaultValue : Integer.parseInt(key);
} catch (NumberFormatException e) {
return defaultValue;
}
}
public boolean setKey(final String keyName, final String keyValue) {
synchronized (this.keys) {
try {
this.keys.put(keyName, keyValue);
return true;
} catch (final JSONException e) {
return false;
}
}
}
public boolean setPrivateKeyAlias(String alias) {
return setKey("private_key_alias", alias);
}
public String getPrivateKeyAlias() {
return getKey("private_key_alias");
}
@Override
public ContentValues getContentValues() {
final ContentValues values = new ContentValues();
values.put(UUID, uuid);
values.put(USERNAME, jid.getLocalpart());
values.put(SERVER, jid.getDomainpart());
values.put(PASSWORD, password);
values.put(OPTIONS, options);
synchronized (this.keys) {
values.put(KEYS, this.keys.toString());
}
values.put(ROSTERVERSION, rosterVersion);
values.put(AVATAR, avatar);
values.put(DISPLAY_NAME, displayName);
values.put(HOSTNAME, hostname);
values.put(PORT, port);
values.put(STATUS, presenceStatus.toShowString());
values.put(STATUS_MESSAGE, presenceStatusMessage);
return values;
}
public AxolotlService getAxolotlService() {
return axolotlService;
}
public void initAccountServices(final XmppConnectionService context) {
this.mOtrService = new OtrService(context, this);
this.axolotlService = new AxolotlService(this, context);
this.pgpDecryptionService = new PgpDecryptionService(context);
if (xmppConnection != null) {
xmppConnection.addOnAdvancedStreamFeaturesAvailableListener(axolotlService);
}
}
public OtrService getOtrService() {
return this.mOtrService;
}
public PgpDecryptionService getPgpDecryptionService() {
return this.pgpDecryptionService;
}
public XmppConnection getXmppConnection() {
return this.xmppConnection;
}
public void setXmppConnection(final XmppConnection connection) {
this.xmppConnection = connection;
}
public String getOtrFingerprint() {
if (this.otrFingerprint == null) {
try {
if (this.mOtrService == null) {
return null;
}
final PublicKey publicKey = this.mOtrService.getPublicKey();
if (publicKey == null || !(publicKey instanceof DSAPublicKey)) {
return null;
}
this.otrFingerprint = new OtrCryptoEngineImpl().getFingerprint(publicKey).toLowerCase(Locale.US);
return this.otrFingerprint;
} catch (final OtrCryptoException ignored) {
return null;
}
} else {
return this.otrFingerprint;
}
}
public String getRosterVersion() {
if (this.rosterVersion == null) {
return "";
} else {
return this.rosterVersion;
}
}
public void setRosterVersion(final String version) {
this.rosterVersion = version;
}
public int countPresences() {
return this.getSelfContact().getPresences().size();
}
public String getPgpSignature() {
return getKey(KEY_PGP_SIGNATURE);
}
public boolean setPgpSignature(String signature) {
return setKey(KEY_PGP_SIGNATURE, signature);
}
public boolean unsetPgpSignature() {
synchronized (this.keys) {
return keys.remove(KEY_PGP_SIGNATURE) != null;
}
}
public long getPgpId() {
synchronized (this.keys) {
if (keys.has(KEY_PGP_ID)) {
try {
return keys.getLong(KEY_PGP_ID);
} catch (JSONException e) {
return 0;
}
} else {
return 0;
}
}
}
public boolean setPgpSignId(long pgpID) {
synchronized (this.keys) {
try {
keys.put(KEY_PGP_ID, pgpID);
} catch (JSONException e) {
return false;
}
return true;
}
}
public Roster getRoster() {
return this.roster;
}
public List<Bookmark> getBookmarks() {
return this.bookmarks;
}
public void setBookmarks(final List<Bookmark> bookmarks) {
this.bookmarks = bookmarks;
}
public boolean hasBookmarkFor(final Jid conferenceJid) {
for (final Bookmark bookmark : this.bookmarks) {
final Jid jid = bookmark.getJid();
if (jid != null && jid.equals(conferenceJid.toBareJid())) {
return true;
}
}
return false;
}
public boolean setAvatar(final String filename) {
if (this.avatar != null && this.avatar.equals(filename)) {
return false;
} else {
this.avatar = filename;
return true;
}
}
public String getAvatar() {
return this.avatar;
}
public void activateGracePeriod(long duration) {
this.mEndGracePeriod = SystemClock.elapsedRealtime() + duration;
}
public void deactivateGracePeriod() {
this.mEndGracePeriod = 0L;
}
public boolean inGracePeriod() {
return SystemClock.elapsedRealtime() < this.mEndGracePeriod;
}
public String getShareableUri() {
List<XmppUri.Fingerprint> fingerprints = this.getFingerprints();
String uri = "xmpp:" + this.getJid().toBareJid().toString();
if (fingerprints.size() > 0) {
StringBuilder builder = new StringBuilder(uri);
builder.append('?');
for (int i = 0; i < fingerprints.size(); ++i) {
XmppUri.FingerprintType type = fingerprints.get(i).type;
if (type == XmppUri.FingerprintType.OMEMO) {
builder.append(XmppUri.OMEMO_URI_PARAM);
builder.append(fingerprints.get(i).deviceId);
} else if (type == XmppUri.FingerprintType.OTR) {
builder.append(XmppUri.OTR_URI_PARAM);
}
builder.append('=');
builder.append(fingerprints.get(i).fingerprint);
if (i != fingerprints.size() - 1) {
builder.append(';');
}
}
return builder.toString();
} else {
return uri;
}
}
private List<XmppUri.Fingerprint> getFingerprints() {
ArrayList<XmppUri.Fingerprint> fingerprints = new ArrayList<>();
final String otr = this.getOtrFingerprint();
if (otr != null) {
fingerprints.add(new XmppUri.Fingerprint(XmppUri.FingerprintType.OTR, otr));
}
fingerprints.add(new XmppUri.Fingerprint(XmppUri.FingerprintType.OMEMO, axolotlService.getOwnFingerprint().substring(2), axolotlService.getOwnDeviceId()));
for (XmppAxolotlSession session : axolotlService.findOwnSessions()) {
if (session.getTrust().isVerified() && session.getTrust().isActive()) {
fingerprints.add(new XmppUri.Fingerprint(XmppUri.FingerprintType.OMEMO, session.getFingerprint().substring(2).replaceAll("\\s", ""), session.getRemoteAddress().getDeviceId()));
}
}
return fingerprints;
}
public boolean isBlocked(final ListItem contact) {
final Jid jid = contact.getJid();
return jid != null && (blocklist.contains(jid.toBareJid()) || blocklist.contains(jid.toDomainJid()));
}
public boolean isBlocked(final Jid jid) {
return jid != null && blocklist.contains(jid.toBareJid());
}
public Collection<Jid> getBlocklist() {
return this.blocklist;
}
public void clearBlocklist() {
getBlocklist().clear();
}
public boolean isOnlineAndConnected() {
return this.getStatus() == State.ONLINE && this.getXmppConnection() != null;
}
}
|