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 long mEndGracePeriod = 0L;
120 private String otrFingerprint;
121 private final Roster roster = new Roster(this);
122 private List<Bookmark> bookmarks = new CopyOnWriteArrayList<>();
123
124 public Account() {
125 this.uuid = "0";
126 }
127
128 public Account(final Jid jid, final String password) {
129 this(java.util.UUID.randomUUID().toString(), jid,
130 password, 0, null, "", null);
131 }
132
133 public Account(final String uuid, final Jid jid,
134 final String password, final int options, final String rosterVersion, final String keys,
135 final String avatar) {
136 this.uuid = uuid;
137 this.jid = jid;
138 if (jid.isBareJid()) {
139 this.setResource("mobile");
140 }
141 this.password = password;
142 this.options = options;
143 this.rosterVersion = rosterVersion;
144 try {
145 this.keys = new JSONObject(keys);
146 } catch (final JSONException ignored) {
147
148 }
149 this.avatar = avatar;
150 }
151
152 public static Account fromCursor(Cursor cursor) {
153 Jid jid = null;
154 try {
155 jid = Jid.fromParts(cursor.getString(cursor.getColumnIndex(USERNAME)),
156 cursor.getString(cursor.getColumnIndex(SERVER)), "mobile");
157 } catch (final InvalidJidException ignored) {
158 }
159 return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
160 jid,
161 cursor.getString(cursor.getColumnIndex(PASSWORD)),
162 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
163 cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
164 cursor.getString(cursor.getColumnIndex(KEYS)),
165 cursor.getString(cursor.getColumnIndex(AVATAR)));
166 }
167
168 public boolean isOptionSet(int option) {
169 return ((options & (1 << option)) != 0);
170 }
171
172 public void setOption(int option, boolean value) {
173 if (value) {
174 this.options |= 1 << option;
175 } else {
176 this.options &= ~(1 << option);
177 }
178 }
179
180 public String getUsername() {
181 return jid.getLocalpart();
182 }
183
184 public void setUsername(final String username) throws InvalidJidException {
185 jid = Jid.fromParts(username, jid.getDomainpart(), jid.getResourcepart());
186 }
187
188 public Jid getServer() {
189 return jid.toDomainJid();
190 }
191
192 public void setServer(final String server) throws InvalidJidException {
193 jid = Jid.fromParts(jid.getLocalpart(), server, jid.getResourcepart());
194 }
195
196 public String getPassword() {
197 return password;
198 }
199
200 public void setPassword(final String password) {
201 this.password = password;
202 }
203
204 public State getStatus() {
205 if (isOptionSet(OPTION_DISABLED)) {
206 return State.DISABLED;
207 } else {
208 return this.status;
209 }
210 }
211
212 public void setStatus(final State status) {
213 this.status = status;
214 }
215
216 public boolean errorStatus() {
217 return getStatus().isError();
218 }
219
220 public boolean hasErrorStatus() {
221 return getXmppConnection() != null && getStatus().isError() && getXmppConnection().getAttempt() >= 2;
222 }
223
224 public String getResource() {
225 return jid.getResourcepart();
226 }
227
228 public void setResource(final String resource) {
229 try {
230 jid = Jid.fromParts(jid.getLocalpart(), jid.getDomainpart(), resource);
231 } catch (final InvalidJidException ignored) {
232 }
233 }
234
235 public Jid getJid() {
236 return jid;
237 }
238
239 public JSONObject getKeys() {
240 return keys;
241 }
242
243 public String getSSLFingerprint() {
244 if (keys.has("ssl_cert")) {
245 try {
246 return keys.getString("ssl_cert");
247 } catch (JSONException e) {
248 return null;
249 }
250 } else {
251 return null;
252 }
253 }
254
255 public void setSSLCertFingerprint(String fingerprint) {
256 this.setKey("ssl_cert", fingerprint);
257 }
258
259 public boolean setKey(String keyName, String keyValue) {
260 try {
261 this.keys.put(keyName, keyValue);
262 return true;
263 } catch (JSONException e) {
264 return false;
265 }
266 }
267
268 @Override
269 public ContentValues getContentValues() {
270 ContentValues values = new ContentValues();
271 values.put(UUID, uuid);
272 values.put(USERNAME, jid.getLocalpart());
273 values.put(SERVER, jid.getDomainpart());
274 values.put(PASSWORD, password);
275 values.put(OPTIONS, options);
276 values.put(KEYS, this.keys.toString());
277 values.put(ROSTERVERSION, rosterVersion);
278 values.put(AVATAR, avatar);
279 return values;
280 }
281
282 public void initOtrEngine(XmppConnectionService context) {
283 this.otrEngine = new OtrEngine(context, this);
284 }
285
286 public OtrEngine getOtrEngine() {
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 if (this.otrEngine == null) {
302 return null;
303 }
304 DSAPublicKey publicKey = (DSAPublicKey) this.otrEngine.getPublicKey();
305 if (publicKey == null) {
306 return null;
307 }
308 this.otrFingerprint = new OtrCryptoEngineImpl().getFingerprint(publicKey);
309 return this.otrFingerprint;
310 } catch (final OtrCryptoException ignored) {
311 return null;
312 }
313 } else {
314 return this.otrFingerprint;
315 }
316 }
317
318 public String getRosterVersion() {
319 if (this.rosterVersion == null) {
320 return "";
321 } else {
322 return this.rosterVersion;
323 }
324 }
325
326 public void setRosterVersion(String version) {
327 this.rosterVersion = version;
328 }
329
330 public int countPresences() {
331 return this.getRoster().getContact(this.getJid().toBareJid()).getPresences().size();
332 }
333
334 public String getPgpSignature() {
335 if (keys.has("pgp_signature")) {
336 try {
337 return keys.getString("pgp_signature");
338 } catch (JSONException e) {
339 return null;
340 }
341 } else {
342 return null;
343 }
344 }
345
346 public Roster getRoster() {
347 return this.roster;
348 }
349
350 public List<Bookmark> getBookmarks() {
351 return this.bookmarks;
352 }
353
354 public void setBookmarks(List<Bookmark> bookmarks) {
355 this.bookmarks = bookmarks;
356 }
357
358 public boolean hasBookmarkFor(final Jid conferenceJid) {
359 for (Bookmark bookmark : this.bookmarks) {
360 final Jid jid = bookmark.getJid();
361 if (jid != null && jid.equals(conferenceJid.toBareJid())) {
362 return true;
363 }
364 }
365 return false;
366 }
367
368 public boolean setAvatar(String filename) {
369 if (this.avatar != null && this.avatar.equals(filename)) {
370 return false;
371 } else {
372 this.avatar = filename;
373 return true;
374 }
375 }
376
377 public String getAvatar() {
378 return this.avatar;
379 }
380
381 public void activateGracePeriod() {
382 this.mEndGracePeriod = SystemClock.elapsedRealtime()
383 + (Config.CARBON_GRACE_PERIOD * 1000);
384 }
385
386 public void deactivateGracePeriod() {
387 this.mEndGracePeriod = 0L;
388 }
389
390 public boolean inGracePeriod() {
391 return SystemClock.elapsedRealtime() < this.mEndGracePeriod;
392 }
393
394 public String getShareableUri() {
395 String fingerprint = this.getOtrFingerprint();
396 if (fingerprint != null) {
397 return "xmpp:" + this.getJid().toBareJid().toString() + "?otr-fingerprint="+fingerprint;
398 } else {
399 return "xmpp:" + this.getJid().toBareJid().toString();
400 }
401 }
402}