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