1package eu.siacs.conversations.persistance;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.DatabaseUtils;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.text.TextUtils;
10import android.os.Build;
11import android.os.Environment;
12import android.os.SystemClock;
13import android.util.Base64;
14import android.util.Log;
15
16import com.cheogram.android.WebxdcUpdate;
17
18import com.google.common.base.Stopwatch;
19import com.google.common.collect.Multimap;
20import com.google.common.collect.HashMultimap;
21
22import org.json.JSONException;
23import org.json.JSONObject;
24import org.whispersystems.libsignal.IdentityKey;
25import org.whispersystems.libsignal.IdentityKeyPair;
26import org.whispersystems.libsignal.InvalidKeyException;
27import org.whispersystems.libsignal.SignalProtocolAddress;
28import org.whispersystems.libsignal.state.PreKeyRecord;
29import org.whispersystems.libsignal.state.SessionRecord;
30import org.whispersystems.libsignal.state.SignedPreKeyRecord;
31
32import java.io.ByteArrayInputStream;
33import java.io.File;
34import java.io.IOException;
35import java.security.cert.CertificateEncodingException;
36import java.security.cert.CertificateException;
37import java.security.cert.CertificateFactory;
38import java.security.cert.X509Certificate;
39import java.util.ArrayList;
40import java.util.Collection;
41import java.util.HashMap;
42import java.util.Hashtable;
43import java.util.HashSet;
44import java.util.List;
45import java.util.Map;
46import java.util.Set;
47import java.util.UUID;
48import java.util.concurrent.CopyOnWriteArrayList;
49
50import io.ipfs.cid.Cid;
51
52import eu.siacs.conversations.Config;
53import eu.siacs.conversations.crypto.axolotl.AxolotlService;
54import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
55import eu.siacs.conversations.crypto.axolotl.SQLiteAxolotlStore;
56import eu.siacs.conversations.entities.Account;
57import eu.siacs.conversations.entities.Contact;
58import eu.siacs.conversations.entities.Conversation;
59import eu.siacs.conversations.entities.DownloadableFile;
60import eu.siacs.conversations.entities.Message;
61import eu.siacs.conversations.entities.MucOptions;
62import eu.siacs.conversations.entities.PresenceTemplate;
63import eu.siacs.conversations.entities.Roster;
64import eu.siacs.conversations.entities.ServiceDiscoveryResult;
65import eu.siacs.conversations.services.QuickConversationsService;
66import eu.siacs.conversations.services.ShortcutService;
67import eu.siacs.conversations.utils.CryptoHelper;
68import eu.siacs.conversations.utils.CursorUtils;
69import eu.siacs.conversations.utils.FtsUtils;
70import eu.siacs.conversations.utils.MimeUtils;
71import eu.siacs.conversations.utils.Resolver;
72import eu.siacs.conversations.xmpp.InvalidJid;
73import eu.siacs.conversations.xmpp.Jid;
74import eu.siacs.conversations.xmpp.mam.MamReference;
75
76public class DatabaseBackend extends SQLiteOpenHelper {
77
78 private static final String DATABASE_NAME = "history";
79 private static final int DATABASE_VERSION = 51;
80
81 private static boolean requiresMessageIndexRebuild = false;
82 private static DatabaseBackend instance = null;
83 private static final String CREATE_CONTATCS_STATEMENT = "create table "
84 + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
85 + Contact.SERVERNAME + " TEXT, " + Contact.SYSTEMNAME + " TEXT,"
86 + Contact.PRESENCE_NAME + " TEXT,"
87 + Contact.JID + " TEXT," + Contact.KEYS + " TEXT,"
88 + Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER,"
89 + Contact.SYSTEMACCOUNT + " NUMBER, " + Contact.AVATAR + " TEXT, "
90 + Contact.LAST_PRESENCE + " TEXT, " + Contact.LAST_TIME + " NUMBER, "
91 + Contact.RTP_CAPABILITY + " TEXT,"
92 + Contact.GROUPS + " TEXT, FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
93 + Account.TABLENAME + "(" + Account.UUID
94 + ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT + ", "
95 + Contact.JID + ") ON CONFLICT REPLACE);";
96
97 private static final String CREATE_DISCOVERY_RESULTS_STATEMENT = "create table "
98 + ServiceDiscoveryResult.TABLENAME + "("
99 + ServiceDiscoveryResult.HASH + " TEXT, "
100 + ServiceDiscoveryResult.VER + " TEXT, "
101 + ServiceDiscoveryResult.RESULT + " TEXT, "
102 + "UNIQUE(" + ServiceDiscoveryResult.HASH + ", "
103 + ServiceDiscoveryResult.VER + ") ON CONFLICT REPLACE);";
104
105 private static final String CREATE_PRESENCE_TEMPLATES_STATEMENT = "CREATE TABLE "
106 + PresenceTemplate.TABELNAME + "("
107 + PresenceTemplate.UUID + " TEXT, "
108 + PresenceTemplate.LAST_USED + " NUMBER,"
109 + PresenceTemplate.MESSAGE + " TEXT,"
110 + PresenceTemplate.STATUS + " TEXT,"
111 + "UNIQUE(" + PresenceTemplate.MESSAGE + "," + PresenceTemplate.STATUS + ") ON CONFLICT REPLACE);";
112
113 private static final String CREATE_PREKEYS_STATEMENT = "CREATE TABLE "
114 + SQLiteAxolotlStore.PREKEY_TABLENAME + "("
115 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
116 + SQLiteAxolotlStore.ID + " INTEGER, "
117 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
118 + SQLiteAxolotlStore.ACCOUNT
119 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
120 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
121 + SQLiteAxolotlStore.ID
122 + ") ON CONFLICT REPLACE"
123 + ");";
124
125 private static final String CREATE_SIGNED_PREKEYS_STATEMENT = "CREATE TABLE "
126 + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME + "("
127 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
128 + SQLiteAxolotlStore.ID + " INTEGER, "
129 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
130 + SQLiteAxolotlStore.ACCOUNT
131 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
132 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
133 + SQLiteAxolotlStore.ID
134 + ") ON CONFLICT REPLACE" +
135 ");";
136
137 private static final String CREATE_SESSIONS_STATEMENT = "CREATE TABLE "
138 + SQLiteAxolotlStore.SESSION_TABLENAME + "("
139 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
140 + SQLiteAxolotlStore.NAME + " TEXT, "
141 + SQLiteAxolotlStore.DEVICE_ID + " INTEGER, "
142 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
143 + SQLiteAxolotlStore.ACCOUNT
144 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
145 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
146 + SQLiteAxolotlStore.NAME + ", "
147 + SQLiteAxolotlStore.DEVICE_ID
148 + ") ON CONFLICT REPLACE"
149 + ");";
150
151 private static final String CREATE_IDENTITIES_STATEMENT = "CREATE TABLE "
152 + SQLiteAxolotlStore.IDENTITIES_TABLENAME + "("
153 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
154 + SQLiteAxolotlStore.NAME + " TEXT, "
155 + SQLiteAxolotlStore.OWN + " INTEGER, "
156 + SQLiteAxolotlStore.FINGERPRINT + " TEXT, "
157 + SQLiteAxolotlStore.CERTIFICATE + " BLOB, "
158 + SQLiteAxolotlStore.TRUST + " TEXT, "
159 + SQLiteAxolotlStore.ACTIVE + " NUMBER, "
160 + SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER,"
161 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
162 + SQLiteAxolotlStore.ACCOUNT
163 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
164 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
165 + SQLiteAxolotlStore.NAME + ", "
166 + SQLiteAxolotlStore.FINGERPRINT
167 + ") ON CONFLICT IGNORE"
168 + ");";
169
170 private static final String RESOLVER_RESULTS_TABLENAME = "resolver_results";
171
172 private static final String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "("
173 + Resolver.Result.DOMAIN + " TEXT,"
174 + Resolver.Result.HOSTNAME + " TEXT,"
175 + Resolver.Result.IP + " BLOB,"
176 + Resolver.Result.PRIORITY + " NUMBER,"
177 + Resolver.Result.DIRECT_TLS + " NUMBER,"
178 + Resolver.Result.AUTHENTICATED + " NUMBER,"
179 + Resolver.Result.PORT + " NUMBER,"
180 + "UNIQUE(" + Resolver.Result.DOMAIN + ") ON CONFLICT REPLACE"
181 + ");";
182
183 private static final String CREATE_MESSAGE_TIME_INDEX = "CREATE INDEX message_time_index ON " + Message.TABLENAME + "(" + Message.TIME_SENT + ")";
184 private static final String CREATE_MESSAGE_CONVERSATION_INDEX = "CREATE INDEX message_conversation_index ON " + Message.TABLENAME + "(" + Message.CONVERSATION + ")";
185 private static final String CREATE_MESSAGE_DELETED_INDEX = "CREATE INDEX message_deleted_index ON " + Message.TABLENAME + "(" + Message.DELETED + ")";
186 private static final String CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX = "CREATE INDEX message_file_path_index ON " + Message.TABLENAME + "(" + Message.RELATIVE_FILE_PATH + ")";
187 private static final String CREATE_MESSAGE_TYPE_INDEX = "CREATE INDEX message_type_index ON " + Message.TABLENAME + "(" + Message.TYPE + ")";
188
189 private static final String CREATE_MESSAGE_INDEX_TABLE = "CREATE VIRTUAL TABLE messages_index USING fts4 (uuid,body,notindexed=\"uuid\",content=\"" + Message.TABLENAME + "\",tokenize='unicode61')";
190 private static final String CREATE_MESSAGE_INSERT_TRIGGER = "CREATE TRIGGER after_message_insert AFTER INSERT ON " + Message.TABLENAME + " BEGIN INSERT INTO messages_index(rowid,uuid,body) VALUES(NEW.rowid,NEW.uuid,NEW.body); END;";
191 private static final String CREATE_MESSAGE_UPDATE_TRIGGER = "CREATE TRIGGER after_message_update UPDATE OF uuid,body ON " + Message.TABLENAME + " BEGIN UPDATE messages_index SET body=NEW.body,uuid=NEW.uuid WHERE rowid=OLD.rowid; END;";
192 private static final String CREATE_MESSAGE_DELETE_TRIGGER = "CREATE TRIGGER after_message_delete AFTER DELETE ON " + Message.TABLENAME + " BEGIN DELETE FROM messages_index WHERE rowid=OLD.rowid; END;";
193 private static final String COPY_PREEXISTING_ENTRIES = "INSERT INTO messages_index(messages_index) VALUES('rebuild');";
194
195 protected Context context;
196
197 private DatabaseBackend(Context context) {
198 super(context, DATABASE_NAME, null, DATABASE_VERSION);
199 this.context = context;
200 setWriteAheadLoggingEnabled(true);
201 }
202
203 private static ContentValues createFingerprintStatusContentValues(FingerprintStatus.Trust trust, boolean active) {
204 ContentValues values = new ContentValues();
205 values.put(SQLiteAxolotlStore.TRUST, trust.toString());
206 values.put(SQLiteAxolotlStore.ACTIVE, active ? 1 : 0);
207 return values;
208 }
209
210 public static boolean requiresMessageIndexRebuild() {
211 return requiresMessageIndexRebuild;
212 }
213
214 public void rebuildMessagesIndex() {
215 final SQLiteDatabase db = getWritableDatabase();
216 final Stopwatch stopwatch = Stopwatch.createStarted();
217 db.execSQL(COPY_PREEXISTING_ENTRIES);
218 Log.d(Config.LOGTAG,"rebuilt message index in "+ stopwatch.stop().toString());
219 }
220
221 public static synchronized DatabaseBackend getInstance(Context context) {
222 if (instance == null) {
223 instance = new DatabaseBackend(context);
224 }
225 return instance;
226 }
227
228 protected void cheogramMigrate(SQLiteDatabase db) {
229 db.beginTransaction();
230
231 try {
232 Cursor cursor = db.rawQuery("PRAGMA cheogram.user_version", null);
233 cursor.moveToNext();
234 int cheogramVersion = cursor.getInt(0);
235 cursor.close();
236
237 if(cheogramVersion < 1) {
238 // No cross-DB foreign keys unfortunately
239 db.execSQL(
240 "CREATE TABLE cheogram." + Message.TABLENAME + "(" +
241 Message.UUID + " TEXT PRIMARY KEY, " +
242 "subject TEXT" +
243 ")"
244 );
245 db.execSQL("PRAGMA cheogram.user_version = 1");
246 }
247
248 if(cheogramVersion < 2) {
249 db.execSQL(
250 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
251 "ADD COLUMN oobUri TEXT"
252 );
253 db.execSQL(
254 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
255 "ADD COLUMN fileParams TEXT"
256 );
257 db.execSQL("PRAGMA cheogram.user_version = 2");
258 }
259
260 if(cheogramVersion < 3) {
261 db.execSQL(
262 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
263 "ADD COLUMN payloads TEXT"
264 );
265 db.execSQL("PRAGMA cheogram.user_version = 3");
266 }
267
268 if(cheogramVersion < 4) {
269 db.execSQL(
270 "CREATE TABLE cheogram.cids (" +
271 "cid TEXT NOT NULL PRIMARY KEY," +
272 "path TEXT NOT NULL" +
273 ")"
274 );
275 db.execSQL("PRAGMA cheogram.user_version = 4");
276 }
277
278 if(cheogramVersion < 5) {
279 db.execSQL(
280 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
281 "ADD COLUMN timeReceived NUMBER"
282 );
283 db.execSQL("CREATE INDEX cheogram.message_time_received_index ON " + Message.TABLENAME + " (timeReceived)");
284 db.execSQL("PRAGMA cheogram.user_version = 5");
285 }
286
287 if(cheogramVersion < 6) {
288 db.execSQL(
289 "CREATE TABLE cheogram.blocked_media (" +
290 "cid TEXT NOT NULL PRIMARY KEY" +
291 ")"
292 );
293 db.execSQL("PRAGMA cheogram.user_version = 6");
294 }
295
296 if(cheogramVersion < 7) {
297 db.execSQL(
298 "ALTER TABLE cheogram.cids " +
299 "ADD COLUMN url TEXT"
300 );
301 db.execSQL("PRAGMA cheogram.user_version = 7");
302 }
303
304 if(cheogramVersion < 8) {
305 db.execSQL(
306 "CREATE TABLE cheogram.webxdc_updates (" +
307 "serial INTEGER PRIMARY KEY AUTOINCREMENT, " +
308 Message.CONVERSATION + " TEXT NOT NULL, " +
309 "sender TEXT NOT NULL, " +
310 "thread TEXT NOT NULL, " +
311 "threadParent TEXT, " +
312 "info TEXT, " +
313 "document TEXT, " +
314 "summary TEXT, " +
315 "payload TEXT" +
316 ")"
317 );
318 db.execSQL("CREATE INDEX cheogram.webxdc_index ON webxdc_updates (" + Message.CONVERSATION + ", thread)");
319 db.execSQL("PRAGMA cheogram.user_version = 8");
320 }
321
322 if(cheogramVersion < 9) {
323 db.execSQL(
324 "ALTER TABLE cheogram.webxdc_updates " +
325 "ADD COLUMN message_id TEXT"
326 );
327 db.execSQL("CREATE UNIQUE INDEX cheogram.webxdc_message_id_index ON webxdc_updates (" + Message.CONVERSATION + ", message_id)");
328 db.execSQL("PRAGMA cheogram.user_version = 9");
329 }
330
331 if(cheogramVersion < 10) {
332 db.execSQL(
333 "CREATE TABLE cheogram.muted_participants (" +
334 "muc_jid TEXT NOT NULL, " +
335 "occupant_id TEXT NOT NULL, " +
336 "nick TEXT NOT NULL," +
337 "PRIMARY KEY (muc_jid, occupant_id)" +
338 ")"
339 );
340 db.execSQL(
341 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
342 "ADD COLUMN occupant_id TEXT"
343 );
344 db.execSQL("PRAGMA cheogram.user_version = 10");
345 }
346
347 if(cheogramVersion < 11) {
348 if (Build.VERSION.SDK_INT >= 34) {
349 db.execSQL(
350 "ALTER TABLE cheogram.muted_participants " +
351 "DROP COLUMN nick"
352 );
353 } else {
354 db.execSQL("DROP TABLE cheogram.muted_participants");
355 db.execSQL(
356 "CREATE TABLE cheogram.muted_participants (" +
357 "muc_jid TEXT NOT NULL, " +
358 "occupant_id TEXT NOT NULL, " +
359 "PRIMARY KEY (muc_jid, occupant_id)" +
360 ")"
361 );
362 }
363 db.execSQL("PRAGMA cheogram.user_version = 11");
364 }
365
366 db.setTransactionSuccessful();
367 } finally {
368 db.endTransaction();
369 }
370 }
371
372 @Override
373 public void onConfigure(SQLiteDatabase db) {
374 db.execSQL("PRAGMA foreign_keys=ON");
375 db.rawQuery("PRAGMA secure_delete=ON", null).close();
376 db.execSQL("ATTACH DATABASE ? AS cheogram", new Object[]{context.getDatabasePath("cheogram").getPath()});
377 cheogramMigrate(db);
378 }
379
380 @Override
381 public void onCreate(SQLiteDatabase db) {
382 db.execSQL("create table " + Account.TABLENAME + "(" + Account.UUID + " TEXT PRIMARY KEY,"
383 + Account.USERNAME + " TEXT,"
384 + Account.SERVER + " TEXT,"
385 + Account.PASSWORD + " TEXT,"
386 + Account.DISPLAY_NAME + " TEXT, "
387 + Account.STATUS + " TEXT,"
388 + Account.STATUS_MESSAGE + " TEXT,"
389 + Account.ROSTERVERSION + " TEXT,"
390 + Account.OPTIONS + " NUMBER, "
391 + Account.AVATAR + " TEXT, "
392 + Account.KEYS + " TEXT, "
393 + Account.HOSTNAME + " TEXT, "
394 + Account.RESOURCE + " TEXT,"
395 + Account.PINNED_MECHANISM + " TEXT,"
396 + Account.PINNED_CHANNEL_BINDING + " TEXT,"
397 + Account.FAST_MECHANISM + " TEXT,"
398 + Account.FAST_TOKEN + " TEXT,"
399 + Account.PORT + " NUMBER DEFAULT 5222)");
400 db.execSQL("create table " + Conversation.TABLENAME + " ("
401 + Conversation.UUID + " TEXT PRIMARY KEY, " + Conversation.NAME
402 + " TEXT, " + Conversation.CONTACT + " TEXT, "
403 + Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACTJID
404 + " TEXT, " + Conversation.CREATED + " NUMBER, "
405 + Conversation.STATUS + " NUMBER, " + Conversation.MODE
406 + " NUMBER, " + Conversation.ATTRIBUTES + " TEXT, FOREIGN KEY("
407 + Conversation.ACCOUNT + ") REFERENCES " + Account.TABLENAME
408 + "(" + Account.UUID + ") ON DELETE CASCADE);");
409 db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID
410 + " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, "
411 + Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART
412 + " TEXT, " + Message.TRUE_COUNTERPART + " TEXT,"
413 + Message.BODY + " TEXT, " + Message.ENCRYPTION + " NUMBER, "
414 + Message.STATUS + " NUMBER," + Message.TYPE + " NUMBER, "
415 + Message.RELATIVE_FILE_PATH + " TEXT, "
416 + Message.SERVER_MSG_ID + " TEXT, "
417 + Message.FINGERPRINT + " TEXT, "
418 + Message.CARBON + " INTEGER, "
419 + Message.EDITED + " TEXT, "
420 + Message.READ + " NUMBER DEFAULT 1, "
421 + Message.OOB + " INTEGER, "
422 + Message.ERROR_MESSAGE + " TEXT,"
423 + Message.READ_BY_MARKERS + " TEXT,"
424 + Message.MARKABLE + " NUMBER DEFAULT 0,"
425 + Message.DELETED + " NUMBER DEFAULT 0,"
426 + Message.BODY_LANGUAGE + " TEXT,"
427 + Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY("
428 + Message.CONVERSATION + ") REFERENCES "
429 + Conversation.TABLENAME + "(" + Conversation.UUID
430 + ") ON DELETE CASCADE);");
431 db.execSQL(CREATE_MESSAGE_TIME_INDEX);
432 db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
433 db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
434 db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
435 db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
436 db.execSQL(CREATE_CONTATCS_STATEMENT);
437 db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
438 db.execSQL(CREATE_SESSIONS_STATEMENT);
439 db.execSQL(CREATE_PREKEYS_STATEMENT);
440 db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
441 db.execSQL(CREATE_IDENTITIES_STATEMENT);
442 db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
443 db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
444 db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
445 db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
446 db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
447 db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
448 }
449
450 @Override
451 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
452 if (oldVersion < 2 && newVersion >= 2) {
453 db.execSQL("update " + Account.TABLENAME + " set "
454 + Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
455 }
456 if (oldVersion < 3 && newVersion >= 3) {
457 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
458 + Message.TYPE + " NUMBER");
459 }
460 if (oldVersion < 5 && newVersion >= 5) {
461 db.execSQL("DROP TABLE " + Contact.TABLENAME);
462 db.execSQL(CREATE_CONTATCS_STATEMENT);
463 db.execSQL("UPDATE " + Account.TABLENAME + " SET "
464 + Account.ROSTERVERSION + " = NULL");
465 }
466 if (oldVersion < 6 && newVersion >= 6) {
467 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
468 + Message.TRUE_COUNTERPART + " TEXT");
469 }
470 if (oldVersion < 7 && newVersion >= 7) {
471 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
472 + Message.REMOTE_MSG_ID + " TEXT");
473 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
474 + Contact.AVATAR + " TEXT");
475 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN "
476 + Account.AVATAR + " TEXT");
477 }
478 if (oldVersion < 8 && newVersion >= 8) {
479 db.execSQL("ALTER TABLE " + Conversation.TABLENAME + " ADD COLUMN "
480 + Conversation.ATTRIBUTES + " TEXT");
481 }
482 if (oldVersion < 9 && newVersion >= 9) {
483 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
484 + Contact.LAST_TIME + " NUMBER");
485 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
486 + Contact.LAST_PRESENCE + " TEXT");
487 }
488 if (oldVersion < 10 && newVersion >= 10) {
489 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
490 + Message.RELATIVE_FILE_PATH + " TEXT");
491 }
492 if (oldVersion < 11 && newVersion >= 11) {
493 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
494 + Contact.GROUPS + " TEXT");
495 db.execSQL("delete from " + Contact.TABLENAME);
496 db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
497 }
498 if (oldVersion < 12 && newVersion >= 12) {
499 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
500 + Message.SERVER_MSG_ID + " TEXT");
501 }
502 if (oldVersion < 13 && newVersion >= 13) {
503 db.execSQL("delete from " + Contact.TABLENAME);
504 db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
505 }
506 if (oldVersion < 14 && newVersion >= 14) {
507 canonicalizeJids(db);
508 }
509 if (oldVersion < 15 && newVersion >= 15) {
510 recreateAxolotlDb(db);
511 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
512 + Message.FINGERPRINT + " TEXT");
513 }
514 if (oldVersion < 16 && newVersion >= 16) {
515 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
516 + Message.CARBON + " INTEGER");
517 }
518 if (oldVersion < 19 && newVersion >= 19) {
519 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.DISPLAY_NAME + " TEXT");
520 }
521 if (oldVersion < 20 && newVersion >= 20) {
522 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.HOSTNAME + " TEXT");
523 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PORT + " NUMBER DEFAULT 5222");
524 }
525 if (oldVersion < 26 && newVersion >= 26) {
526 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS + " TEXT");
527 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS_MESSAGE + " TEXT");
528 }
529 if (oldVersion < 40 && newVersion >= 40) {
530 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.RESOURCE + " TEXT");
531 }
532 /* Any migrations that alter the Account table need to happen BEFORE this migration, as it
533 * depends on account de-serialization.
534 */
535 if (oldVersion < 17 && newVersion >= 17 && newVersion < 31) {
536 List<Account> accounts = getAccounts(db);
537 for (Account account : accounts) {
538 String ownDeviceIdString = account.getKey(SQLiteAxolotlStore.JSONKEY_REGISTRATION_ID);
539 if (ownDeviceIdString == null) {
540 continue;
541 }
542 int ownDeviceId = Integer.valueOf(ownDeviceIdString);
543 SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().asBareJid().toString(), ownDeviceId);
544 deleteSession(db, account, ownAddress);
545 IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
546 if (identityKeyPair != null) {
547 String[] selectionArgs = {
548 account.getUuid(),
549 CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize())
550 };
551 ContentValues values = new ContentValues();
552 values.put(SQLiteAxolotlStore.TRUSTED, 2);
553 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
554 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
555 + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
556 selectionArgs);
557 } else {
558 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not load own identity key pair");
559 }
560 }
561 }
562 if (oldVersion < 18 && newVersion >= 18) {
563 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ + " NUMBER DEFAULT 1");
564 }
565
566 if (oldVersion < 21 && newVersion >= 21) {
567 List<Account> accounts = getAccounts(db);
568 for (Account account : accounts) {
569 account.unsetPgpSignature();
570 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
571 + "=?", new String[]{account.getUuid()});
572 }
573 }
574
575 if (oldVersion >= 15 && oldVersion < 22 && newVersion >= 22) {
576 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.CERTIFICATE);
577 }
578
579 if (oldVersion < 23 && newVersion >= 23) {
580 db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
581 }
582
583 if (oldVersion < 24 && newVersion >= 24) {
584 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.EDITED + " TEXT");
585 }
586
587 if (oldVersion < 25 && newVersion >= 25) {
588 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.OOB + " INTEGER");
589 }
590
591 if (oldVersion < 26 && newVersion >= 26) {
592 db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
593 }
594
595 if (oldVersion < 27 && newVersion >= 27) {
596 db.execSQL("DELETE FROM " + ServiceDiscoveryResult.TABLENAME);
597 }
598
599 if (oldVersion < 28 && newVersion >= 28) {
600 canonicalizeJids(db);
601 }
602
603 if (oldVersion < 29 && newVersion >= 29) {
604 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.ERROR_MESSAGE + " TEXT");
605 }
606 if (oldVersion >= 15 && oldVersion < 31 && newVersion >= 31) {
607 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.TRUST + " TEXT");
608 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.ACTIVE + " NUMBER");
609 HashMap<Integer, ContentValues> migration = new HashMap<>();
610 migration.put(0, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
611 migration.put(1, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
612 migration.put(2, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, true));
613 migration.put(3, createFingerprintStatusContentValues(FingerprintStatus.Trust.COMPROMISED, false));
614 migration.put(4, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
615 migration.put(5, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
616 migration.put(6, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, false));
617 migration.put(7, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, true));
618 migration.put(8, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, false));
619 for (Map.Entry<Integer, ContentValues> entry : migration.entrySet()) {
620 String whereClause = SQLiteAxolotlStore.TRUSTED + "=?";
621 String[] where = {String.valueOf(entry.getKey())};
622 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, entry.getValue(), whereClause, where);
623 }
624
625 }
626 if (oldVersion >= 15 && oldVersion < 32 && newVersion >= 32) {
627 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER");
628 ContentValues defaults = new ContentValues();
629 defaults.put(SQLiteAxolotlStore.LAST_ACTIVATION, System.currentTimeMillis());
630 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, defaults, null, null);
631 }
632 if (oldVersion >= 15 && oldVersion < 33 && newVersion >= 33) {
633 String whereClause = SQLiteAxolotlStore.OWN + "=1";
634 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED, true), whereClause, null);
635 }
636
637 if (oldVersion < 34 && newVersion >= 34) {
638 db.execSQL(CREATE_MESSAGE_TIME_INDEX);
639
640 final File oldPicturesDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Conversations/");
641 final File oldFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/");
642 final File newFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Files/");
643 final File newVideosDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Videos/");
644 if (oldPicturesDirectory.exists() && oldPicturesDirectory.isDirectory()) {
645 final File newPicturesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Images/");
646 newPicturesDirectory.getParentFile().mkdirs();
647 if (oldPicturesDirectory.renameTo(newPicturesDirectory)) {
648 Log.d(Config.LOGTAG, "moved " + oldPicturesDirectory.getAbsolutePath() + " to " + newPicturesDirectory.getAbsolutePath());
649 }
650 }
651 if (oldFilesDirectory.exists() && oldFilesDirectory.isDirectory()) {
652 newFilesDirectory.mkdirs();
653 newVideosDirectory.mkdirs();
654 final File[] files = oldFilesDirectory.listFiles();
655 if (files == null) {
656 return;
657 }
658 for (File file : files) {
659 if (file.getName().equals(".nomedia")) {
660 if (file.delete()) {
661 Log.d(Config.LOGTAG, "deleted nomedia file in " + oldFilesDirectory.getAbsolutePath());
662 }
663 } else if (file.isFile()) {
664 final String name = file.getName();
665 boolean isVideo = false;
666 int start = name.lastIndexOf('.') + 1;
667 if (start < name.length()) {
668 String mime = MimeUtils.guessMimeTypeFromExtension(name.substring(start));
669 isVideo = mime != null && mime.startsWith("video/");
670 }
671 File dst = new File((isVideo ? newVideosDirectory : newFilesDirectory).getAbsolutePath() + "/" + file.getName());
672 if (file.renameTo(dst)) {
673 Log.d(Config.LOGTAG, "moved " + file + " to " + dst);
674 }
675 }
676 }
677 }
678 }
679 if (oldVersion < 35 && newVersion >= 35) {
680 db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
681 }
682 if (oldVersion < 36 && newVersion >= 36) {
683 List<Account> accounts = getAccounts(db);
684 for (Account account : accounts) {
685 account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true);
686 account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, false);
687 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
688 + "=?", new String[]{account.getUuid()});
689 }
690 }
691
692 if (oldVersion < 37 && newVersion >= 37) {
693 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ_BY_MARKERS + " TEXT");
694 }
695
696 if (oldVersion < 38 && newVersion >= 38) {
697 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.MARKABLE + " NUMBER DEFAULT 0");
698 }
699
700 if (oldVersion < 39 && newVersion >= 39) {
701 db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
702 }
703
704 if (QuickConversationsService.isQuicksy() && oldVersion < 43 && newVersion >= 43) {
705 List<Account> accounts = getAccounts(db);
706 for (Account account : accounts) {
707 account.setOption(Account.OPTION_MAGIC_CREATE, true);
708 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
709 + "=?", new String[]{account.getUuid()});
710 }
711 }
712
713 if (oldVersion < 44 && newVersion >= 44) {
714 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.DELETED + " NUMBER DEFAULT 0");
715 db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
716 db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
717 db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
718 }
719
720 if (oldVersion < 45 && newVersion >= 45) {
721 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE);
722 }
723
724 if (oldVersion < 46 && newVersion >= 46) {
725 final long start = SystemClock.elapsedRealtime();
726 db.rawQuery("PRAGMA secure_delete = FALSE", null).close();
727 db.execSQL("update " + Message.TABLENAME + " set " + Message.EDITED + "=NULL");
728 db.rawQuery("PRAGMA secure_delete=ON", null).close();
729 final long diff = SystemClock.elapsedRealtime() - start;
730 Log.d(Config.LOGTAG, "deleted old edit information in " + diff + "ms");
731 }
732 if (oldVersion < 47 && newVersion >= 47) {
733 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.PRESENCE_NAME + " TEXT");
734 }
735 if (oldVersion < 48 && newVersion >= 48) {
736 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.RTP_CAPABILITY + " TEXT");
737 }
738 if (oldVersion < 49 && newVersion >= 49) {
739 db.beginTransaction();
740 db.execSQL("DROP TRIGGER IF EXISTS after_message_insert;");
741 db.execSQL("DROP TRIGGER IF EXISTS after_message_update;");
742 db.execSQL("DROP TRIGGER IF EXISTS after_message_delete;");
743 db.execSQL("DROP TABLE IF EXISTS messages_index;");
744 // a hack that should not be necessary, but
745 // there was at least one occurence when SQLite failed at this
746 db.execSQL("DROP TABLE IF EXISTS messages_index_docsize;");
747 db.execSQL("DROP TABLE IF EXISTS messages_index_segdir;");
748 db.execSQL("DROP TABLE IF EXISTS messages_index_segments;");
749 db.execSQL("DROP TABLE IF EXISTS messages_index_stat;");
750 db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
751 db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
752 db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
753 db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
754 db.setTransactionSuccessful();
755 db.endTransaction();
756 requiresMessageIndexRebuild = true;
757 }
758 if (oldVersion < 50 && newVersion >= 50) {
759 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PINNED_MECHANISM + " TEXT");
760 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PINNED_CHANNEL_BINDING + " TEXT");
761 }
762 if (oldVersion < 51 && newVersion >= 51) {
763 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.FAST_MECHANISM + " TEXT");
764 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.FAST_TOKEN + " TEXT");
765 }
766 }
767
768 private void canonicalizeJids(SQLiteDatabase db) {
769 // migrate db to new, canonicalized JID domainpart representation
770
771 // Conversation table
772 Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME, new String[0]);
773 while (cursor.moveToNext()) {
774 String newJid;
775 try {
776 newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))).toString();
777 } catch (IllegalArgumentException ignored) {
778 Log.e(Config.LOGTAG, "Failed to migrate Conversation CONTACTJID "
779 + cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))
780 + ": " + ignored + ". Skipping...");
781 continue;
782 }
783
784 final String[] updateArgs = {
785 newJid,
786 cursor.getString(cursor.getColumnIndex(Conversation.UUID)),
787 };
788 db.execSQL("update " + Conversation.TABLENAME
789 + " set " + Conversation.CONTACTJID + " = ? "
790 + " where " + Conversation.UUID + " = ?", updateArgs);
791 }
792 cursor.close();
793
794 // Contact table
795 cursor = db.rawQuery("select * from " + Contact.TABLENAME, new String[0]);
796 while (cursor.moveToNext()) {
797 String newJid;
798 try {
799 newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Contact.JID))).toString();
800 } catch (final IllegalArgumentException e) {
801 Log.e(Config.LOGTAG, "Failed to migrate Contact JID "
802 + cursor.getString(cursor.getColumnIndex(Contact.JID))
803 + ": Skipping...", e);
804 continue;
805 }
806
807 final String[] updateArgs = {
808 newJid,
809 cursor.getString(cursor.getColumnIndex(Contact.ACCOUNT)),
810 cursor.getString(cursor.getColumnIndex(Contact.JID)),
811 };
812 db.execSQL("update " + Contact.TABLENAME
813 + " set " + Contact.JID + " = ? "
814 + " where " + Contact.ACCOUNT + " = ? "
815 + " AND " + Contact.JID + " = ?", updateArgs);
816 }
817 cursor.close();
818
819 // Account table
820 cursor = db.rawQuery("select * from " + Account.TABLENAME, new String[0]);
821 while (cursor.moveToNext()) {
822 String newServer;
823 try {
824 newServer = Jid.of(
825 cursor.getString(cursor.getColumnIndex(Account.USERNAME)),
826 cursor.getString(cursor.getColumnIndex(Account.SERVER)),
827 null
828 ).getDomain().toEscapedString();
829 } catch (IllegalArgumentException ignored) {
830 Log.e(Config.LOGTAG, "Failed to migrate Account SERVER "
831 + cursor.getString(cursor.getColumnIndex(Account.SERVER))
832 + ": " + ignored + ". Skipping...");
833 continue;
834 }
835
836 String[] updateArgs = {
837 newServer,
838 cursor.getString(cursor.getColumnIndex(Account.UUID)),
839 };
840 db.execSQL("update " + Account.TABLENAME
841 + " set " + Account.SERVER + " = ? "
842 + " where " + Account.UUID + " = ?", updateArgs);
843 }
844 cursor.close();
845 }
846
847 public DownloadableFile getFileForCid(Cid cid) {
848 if (cid == null) return null;
849
850 SQLiteDatabase db = this.getReadableDatabase();
851 Cursor cursor = db.query("cheogram.cids", new String[]{"path"}, "cid=?", new String[]{cid.toString()}, null, null, null);
852 DownloadableFile f = null;
853 if (cursor.moveToNext()) {
854 f = new DownloadableFile(cursor.getString(0));
855 }
856 cursor.close();
857 return f;
858 }
859
860 public String getUrlForCid(Cid cid) {
861 SQLiteDatabase db = this.getReadableDatabase();
862 Cursor cursor = db.query("cheogram.cids", new String[]{"url"}, "cid=?", new String[]{cid.toString()}, null, null, null);
863 String url = null;
864 if (cursor.moveToNext()) {
865 url = cursor.getString(0);
866 }
867 cursor.close();
868 return url;
869 }
870
871 public void saveCid(Cid cid, File file) {
872 saveCid(cid, file, null);
873 }
874
875 public void saveCid(Cid cid, File file, String url) {
876 SQLiteDatabase db = this.getWritableDatabase();
877 ContentValues cv = new ContentValues();
878 cv.put("cid", cid.toString());
879 if (file != null) cv.put("path", file.getAbsolutePath());
880 if (url != null) cv.put("url", url);
881 if (db.update("cheogram.cids", cv, "cid=?", new String[]{cid.toString()}) < 1) {
882 db.insertWithOnConflict("cheogram.cids", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
883 }
884 }
885
886 public void blockMedia(Cid cid) {
887 SQLiteDatabase db = this.getWritableDatabase();
888 ContentValues cv = new ContentValues();
889 cv.put("cid", cid.toString());
890 db.insertWithOnConflict("cheogram.blocked_media", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
891 }
892
893 public boolean isBlockedMedia(Cid cid) {
894 SQLiteDatabase db = this.getReadableDatabase();
895 Cursor cursor = db.query("cheogram.blocked_media", new String[]{"count(*)"}, "cid=?", new String[]{cid.toString()}, null, null, null);
896 boolean is = false;
897 if (cursor.moveToNext()) {
898 is = cursor.getInt(0) > 0;
899 }
900 cursor.close();
901 return is;
902 }
903
904 public void clearBlockedMedia() {
905 SQLiteDatabase db = this.getWritableDatabase();
906 db.execSQL("DELETE FROM cheogram.blocked_media");
907 }
908
909 public Multimap<String, String> loadMutedMucUsers() {
910 Multimap<String, String> result = HashMultimap.create();
911 SQLiteDatabase db = this.getReadableDatabase();
912 Cursor cursor = db.query("cheogram.muted_participants", new String[]{"muc_jid", "occupant_id"}, null, null, null, null, null);
913 while (cursor.moveToNext()) {
914 result.put(cursor.getString(0), cursor.getString(1));
915 }
916 cursor.close();
917 return result;
918 }
919
920 public boolean muteMucUser(MucOptions.User user) {
921 if (user.getMuc() == null || user.getOccupantId() == null) return false;
922
923 SQLiteDatabase db = this.getWritableDatabase();
924 ContentValues cv = new ContentValues();
925 cv.put("muc_jid", user.getMuc().toString());
926 cv.put("occupant_id", user.getOccupantId());
927 db.insertWithOnConflict("cheogram.muted_participants", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
928
929 return true;
930 }
931
932 public boolean unmuteMucUser(MucOptions.User user) {
933 if (user.getMuc() == null || user.getOccupantId() == null) return false;
934
935 SQLiteDatabase db = this.getWritableDatabase();
936 String where = "muc_jid=? AND occupant_id=?";
937 String[] whereArgs = {user.getMuc().toString(), user.getOccupantId()};
938 db.delete("cheogram.muted_participants", where, whereArgs);
939
940 return true;
941 }
942
943 public void insertWebxdcUpdate(final WebxdcUpdate update) {
944 SQLiteDatabase db = this.getWritableDatabase();
945 db.insertWithOnConflict("cheogram.webxdc_updates", null, update.getContentValues(), SQLiteDatabase.CONFLICT_IGNORE);
946 }
947
948 public WebxdcUpdate findLastWebxdcUpdate(Message message) {
949 if (message.getThread() == null) {
950 Log.w(Config.LOGTAG, "WebXDC message with no thread!");
951 return null;
952 }
953
954 SQLiteDatabase db = this.getReadableDatabase();
955 String[] selectionArgs = {message.getConversation().getUuid(), message.getThread().getContent()};
956 Cursor cursor = db.query("cheogram.webxdc_updates", null,
957 Message.CONVERSATION + "=? AND thread=?",
958 selectionArgs, null, null, "serial ASC");
959 WebxdcUpdate update = null;
960 if (cursor.moveToLast()) {
961 update = new WebxdcUpdate(cursor, cursor.getLong(cursor.getColumnIndex("serial")));
962 }
963 cursor.close();
964 return update;
965 }
966
967 public List<WebxdcUpdate> findWebxdcUpdates(Message message, long serial) {
968 SQLiteDatabase db = this.getReadableDatabase();
969 String[] selectionArgs = {message.getConversation().getUuid(), message.getThread().getContent(), String.valueOf(serial)};
970 Cursor cursor = db.query("cheogram.webxdc_updates", null,
971 Message.CONVERSATION + "=? AND thread=? AND serial>?",
972 selectionArgs, null, null, "serial ASC");
973 long maxSerial = 0;
974 if (cursor.moveToLast()) {
975 maxSerial = cursor.getLong(cursor.getColumnIndex("serial"));
976 }
977 cursor.moveToFirst();
978 cursor.moveToPrevious();
979
980 List<WebxdcUpdate> updates = new ArrayList<>();
981 while (cursor.moveToNext()) {
982 updates.add(new WebxdcUpdate(cursor, maxSerial));
983 }
984 cursor.close();
985 return updates;
986 }
987
988 public void createConversation(Conversation conversation) {
989 SQLiteDatabase db = this.getWritableDatabase();
990 db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
991 }
992
993 public void createMessage(Message message) {
994 SQLiteDatabase db = this.getWritableDatabase();
995 db.insert(Message.TABLENAME, null, message.getContentValues());
996 db.insert("cheogram." + Message.TABLENAME, null, message.getCheogramContentValues());
997 }
998
999 public void createAccount(Account account) {
1000 SQLiteDatabase db = this.getWritableDatabase();
1001 db.insert(Account.TABLENAME, null, account.getContentValues());
1002 }
1003
1004 public void insertDiscoveryResult(ServiceDiscoveryResult result) {
1005 SQLiteDatabase db = this.getWritableDatabase();
1006 db.insert(ServiceDiscoveryResult.TABLENAME, null, result.getContentValues());
1007 }
1008
1009 public ServiceDiscoveryResult findDiscoveryResult(final String hash, final String ver) {
1010 SQLiteDatabase db = this.getReadableDatabase();
1011 String[] selectionArgs = {hash, ver};
1012 Cursor cursor = db.query(ServiceDiscoveryResult.TABLENAME, null,
1013 ServiceDiscoveryResult.HASH + "=? AND " + ServiceDiscoveryResult.VER + "=?",
1014 selectionArgs, null, null, null);
1015 if (cursor.getCount() == 0) {
1016 cursor.close();
1017 return null;
1018 }
1019 cursor.moveToFirst();
1020
1021 ServiceDiscoveryResult result = null;
1022 try {
1023 result = new ServiceDiscoveryResult(cursor);
1024 } catch (JSONException e) { /* result is still null */ }
1025
1026 cursor.close();
1027 return result;
1028 }
1029
1030 public void saveResolverResult(String domain, Resolver.Result result) {
1031 SQLiteDatabase db = this.getWritableDatabase();
1032 ContentValues contentValues = result.toContentValues();
1033 contentValues.put(Resolver.Result.DOMAIN, domain);
1034 db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
1035 }
1036
1037 public synchronized Resolver.Result findResolverResult(String domain) {
1038 SQLiteDatabase db = this.getReadableDatabase();
1039 String where = Resolver.Result.DOMAIN + "=?";
1040 String[] whereArgs = {domain};
1041 final Cursor cursor = db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
1042 Resolver.Result result = null;
1043 if (cursor != null) {
1044 try {
1045 if (cursor.moveToFirst()) {
1046 result = Resolver.Result.fromCursor(cursor);
1047 }
1048 } catch (Exception e) {
1049 Log.d(Config.LOGTAG, "unable to find cached resolver result in database " + e.getMessage());
1050 return null;
1051 } finally {
1052 cursor.close();
1053 }
1054 }
1055 return result;
1056 }
1057
1058 public void insertPresenceTemplate(PresenceTemplate template) {
1059 SQLiteDatabase db = this.getWritableDatabase();
1060 String whereToDelete = PresenceTemplate.MESSAGE + "=?";
1061 String[] whereToDeleteArgs = {template.getStatusMessage()};
1062 db.delete(PresenceTemplate.TABELNAME, whereToDelete, whereToDeleteArgs);
1063 db.delete(PresenceTemplate.TABELNAME, PresenceTemplate.UUID + " not in (select " + PresenceTemplate.UUID + " from " + PresenceTemplate.TABELNAME + " order by " + PresenceTemplate.LAST_USED + " desc limit 9)", null);
1064 db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues());
1065 }
1066
1067 public List<PresenceTemplate> getPresenceTemplates() {
1068 ArrayList<PresenceTemplate> templates = new ArrayList<>();
1069 SQLiteDatabase db = this.getReadableDatabase();
1070 Cursor cursor = db.query(PresenceTemplate.TABELNAME, null, null, null, null, null, PresenceTemplate.LAST_USED + " desc");
1071 while (cursor.moveToNext()) {
1072 templates.add(PresenceTemplate.fromCursor(cursor));
1073 }
1074 cursor.close();
1075 return templates;
1076 }
1077
1078 public CopyOnWriteArrayList<Conversation> getConversations(int status) {
1079 CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<>();
1080 SQLiteDatabase db = this.getReadableDatabase();
1081 String[] selectionArgs = {Integer.toString(status)};
1082 Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME
1083 + " where " + Conversation.STATUS + " = ? and " + Conversation.CONTACTJID + " is not null order by "
1084 + Conversation.CREATED + " desc", selectionArgs);
1085 while (cursor.moveToNext()) {
1086 final Conversation conversation = Conversation.fromCursor(cursor);
1087 if (conversation.getJid() instanceof InvalidJid) {
1088 continue;
1089 }
1090 list.add(conversation);
1091 }
1092 cursor.close();
1093 return list;
1094 }
1095
1096 public Message getMessage(Conversation conversation, String uuid) {
1097 ArrayList<Message> list = new ArrayList<>();
1098 SQLiteDatabase db = this.getReadableDatabase();
1099 Cursor cursor;
1100 cursor = db.rawQuery(
1101 "SELECT * FROM " + Message.TABLENAME + " " +
1102 "LEFT JOIN cheogram." + Message.TABLENAME +
1103 " USING (" + Message.UUID + ")" +
1104 "WHERE " + Message.UUID + "=?",
1105 new String[]{uuid}
1106 );
1107 while (cursor.moveToNext()) {
1108 try {
1109 return Message.fromCursor(cursor, conversation);
1110 } catch (Exception e) {
1111 Log.e(Config.LOGTAG, "unable to restore message");
1112 }
1113 }
1114 cursor.close();
1115 return null;
1116 }
1117
1118 public ArrayList<Message> getMessages(Conversation conversations, int limit) {
1119 return getMessages(conversations, limit, -1);
1120 }
1121
1122 public Map<String, Message> getMessageFuzzyIds(Conversation conversation, Collection<String> ids) {
1123 final var result = new Hashtable<String, Message>();
1124 if (ids.size() < 1) return result;
1125 final ArrayList<String> params = new ArrayList<>();
1126 final ArrayList<String> template = new ArrayList<>();
1127 for (final var id : ids) {
1128 template.add("?");
1129 }
1130 params.addAll(ids);
1131 params.addAll(ids);
1132 params.addAll(ids);
1133 ArrayList<Message> list = new ArrayList<>();
1134 SQLiteDatabase db = this.getReadableDatabase();
1135 Cursor cursor;
1136 cursor = db.rawQuery(
1137 "SELECT * FROM " + Message.TABLENAME + " " +
1138 "LEFT JOIN cheogram." + Message.TABLENAME +
1139 " USING (" + Message.UUID + ")" +
1140 "WHERE " + Message.UUID + " IN (" + TextUtils.join(",", template) + ") OR " + Message.SERVER_MSG_ID + " IN (" + TextUtils.join(",", template) + ") OR " + Message.REMOTE_MSG_ID + " IN (" + TextUtils.join(",", template) + ")",
1141 params.toArray(new String[0])
1142 );
1143
1144 while (cursor.moveToNext()) {
1145 try {
1146 final var m = Message.fromCursor(cursor, conversation);
1147 if (ids.contains(m.getUuid())) result.put(m.getUuid(), m);
1148 if (ids.contains(m.getServerMsgId())) result.put(m.getServerMsgId(), m);
1149 if (ids.contains(m.getRemoteMsgId())) result.put(m.getRemoteMsgId(), m);
1150 } catch (Exception e) {
1151 Log.e(Config.LOGTAG, "unable to restore message");
1152 }
1153 }
1154 cursor.close();
1155 return result;
1156 }
1157
1158 public ArrayList<Message> getMessages(Conversation conversation, int limit, long timestamp) {
1159 ArrayList<Message> list = new ArrayList<>();
1160 SQLiteDatabase db = this.getReadableDatabase();
1161 Cursor cursor;
1162 if (timestamp == -1) {
1163 String[] selectionArgs = {conversation.getUuid()};
1164 cursor = db.rawQuery(
1165 "SELECT * FROM " + Message.TABLENAME + " " +
1166 "LEFT JOIN cheogram." + Message.TABLENAME +
1167 " USING (" + Message.UUID + ")" +
1168 " WHERE " + Message.UUID + " IN (" +
1169 "SELECT " + Message.UUID + " FROM " + Message.TABLENAME +
1170 " WHERE " + Message.CONVERSATION + "=? " +
1171 "ORDER BY " + Message.TIME_SENT + " DESC " +
1172 "LIMIT " + String.valueOf(limit) + ") " +
1173 "ORDER BY " + Message.TIME_SENT + " DESC ",
1174 selectionArgs
1175 );
1176 } else {
1177 String[] selectionArgs = {conversation.getUuid(),
1178 Long.toString(timestamp)};
1179 cursor = db.rawQuery(
1180 "SELECT * FROM " + Message.TABLENAME + " " +
1181 "LEFT JOIN cheogram." + Message.TABLENAME +
1182 " USING (" + Message.UUID + ")" +
1183 " WHERE " + Message.UUID + " IN (" +
1184 "SELECT " + Message.UUID + " FROM " + Message.TABLENAME +
1185 " WHERE " + Message.CONVERSATION + "=? AND " +
1186 Message.TIME_SENT + "<? " +
1187 "ORDER BY " + Message.TIME_SENT + " DESC " +
1188 "LIMIT " + String.valueOf(limit) + ") " +
1189 "ORDER BY " + Message.TIME_SENT + " DESC ",
1190 selectionArgs
1191 );
1192 }
1193 CursorUtils.upgradeCursorWindowSize(cursor);
1194 final Multimap<String, Message> waitingForReplies = HashMultimap.create();
1195 final var replyIds = new HashSet<String>();
1196 while (cursor.moveToNext()) {
1197 try {
1198 final var m = Message.fromCursor(cursor, conversation);
1199 final var reply = m.getReply();
1200 if (reply != null && reply.getAttribute("id") != null) { // Guard against busted replies
1201 replyIds.add(reply.getAttribute("id"));
1202 waitingForReplies.put(reply.getAttribute("id"), m);
1203 }
1204 list.add(0, m);
1205 } catch (Exception e) {
1206 Log.e(Config.LOGTAG, "unable to restore message", e);
1207 }
1208 }
1209 for (final var parent : getMessageFuzzyIds(conversation, replyIds).entrySet()) {
1210 for (final var m : waitingForReplies.get(parent.getKey())) {
1211 m.setInReplyTo(parent.getValue());
1212 }
1213 }
1214 cursor.close();
1215 return list;
1216 }
1217
1218 public Cursor getMessageSearchCursor(final List<String> term, final String uuid) {
1219 final SQLiteDatabase db = this.getReadableDatabase();
1220 final StringBuilder SQL = new StringBuilder();
1221 final String[] selectionArgs;
1222 SQL.append("SELECT " + Message.TABLENAME + ".*," + Conversation.TABLENAME + "." + Conversation.CONTACTJID + "," + Conversation.TABLENAME + "." + Conversation.ACCOUNT + "," + Conversation.TABLENAME + "." + Conversation.MODE + " FROM " + Message.TABLENAME + " JOIN " + Conversation.TABLENAME + " ON " + Message.TABLENAME + "." + Message.CONVERSATION + "=" + Conversation.TABLENAME + "." + Conversation.UUID + " JOIN messages_index ON messages_index.rowid=messages.rowid WHERE " + Message.ENCRYPTION + " NOT IN(" + Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE + "," + Message.ENCRYPTION_PGP + "," + Message.ENCRYPTION_DECRYPTION_FAILED + "," + Message.ENCRYPTION_AXOLOTL_FAILED + ") AND " + Message.TYPE + " IN(" + Message.TYPE_TEXT + "," + Message.TYPE_PRIVATE + ") AND messages_index.body MATCH ?");
1223 if (uuid == null) {
1224 selectionArgs = new String[]{FtsUtils.toMatchString(term)};
1225 } else {
1226 selectionArgs = new String[]{FtsUtils.toMatchString(term), uuid};
1227 SQL.append(" AND " + Conversation.TABLENAME + '.' + Conversation.UUID + "=?");
1228 }
1229 SQL.append(" ORDER BY " + Message.TIME_SENT + " DESC limit " + Config.MAX_SEARCH_RESULTS);
1230 Log.d(Config.LOGTAG, "search term: " + FtsUtils.toMatchString(term));
1231 return db.rawQuery(SQL.toString(), selectionArgs);
1232 }
1233
1234 public List<String> markFileAsDeleted(final File file, final boolean internal) {
1235 SQLiteDatabase db = this.getReadableDatabase();
1236 String selection;
1237 String[] selectionArgs;
1238 if (internal) {
1239 final String name = file.getName();
1240 if (name.endsWith(".pgp")) {
1241 selection = "(" + Message.RELATIVE_FILE_PATH + " IN(?,?) OR (" + Message.RELATIVE_FILE_PATH + "=? and encryption in(1,4))) and type in (1,2,5)";
1242 selectionArgs = new String[]{file.getAbsolutePath(), name, name.substring(0, name.length() - 4)};
1243 } else {
1244 selection = Message.RELATIVE_FILE_PATH + " IN(?,?) and type in (1,2,5)";
1245 selectionArgs = new String[]{file.getAbsolutePath(), name};
1246 }
1247 } else {
1248 selection = Message.RELATIVE_FILE_PATH + "=? and type in (1,2,5)";
1249 selectionArgs = new String[]{file.getAbsolutePath()};
1250 }
1251 final List<String> uuids = new ArrayList<>();
1252 Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID}, selection, selectionArgs, null, null, null);
1253 while (cursor != null && cursor.moveToNext()) {
1254 uuids.add(cursor.getString(0));
1255 }
1256 if (cursor != null) {
1257 cursor.close();
1258 }
1259 markFileAsDeleted(uuids);
1260 return uuids;
1261 }
1262
1263 public void markFileAsDeleted(List<String> uuids) {
1264 SQLiteDatabase db = this.getReadableDatabase();
1265 final ContentValues contentValues = new ContentValues();
1266 final String where = Message.UUID + "=?";
1267 contentValues.put(Message.DELETED, 1);
1268 db.beginTransaction();
1269 for (String uuid : uuids) {
1270 db.update(Message.TABLENAME, contentValues, where, new String[]{uuid});
1271 }
1272 db.setTransactionSuccessful();
1273 db.endTransaction();
1274 }
1275
1276 public void markFilesAsChanged(List<FilePathInfo> files) {
1277 SQLiteDatabase db = this.getReadableDatabase();
1278 final String where = Message.UUID + "=?";
1279 db.beginTransaction();
1280 for (FilePathInfo info : files) {
1281 final ContentValues contentValues = new ContentValues();
1282 contentValues.put(Message.DELETED, info.deleted ? 1 : 0);
1283 db.update(Message.TABLENAME, contentValues, where, new String[]{info.uuid.toString()});
1284 }
1285 db.setTransactionSuccessful();
1286 db.endTransaction();
1287 }
1288
1289 public List<FilePathInfo> getFilePathInfo() {
1290 final SQLiteDatabase db = this.getReadableDatabase();
1291 final Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID, Message.RELATIVE_FILE_PATH, Message.DELETED}, "type in (1,2,5) and " + Message.RELATIVE_FILE_PATH + " is not null", null, null, null, null);
1292 final List<FilePathInfo> list = new ArrayList<>();
1293 while (cursor != null && cursor.moveToNext()) {
1294 list.add(new FilePathInfo(cursor.getString(0), cursor.getString(1), cursor.getInt(2) > 0));
1295 }
1296 if (cursor != null) {
1297 cursor.close();
1298 }
1299 return list;
1300 }
1301
1302 public List<FilePath> getRelativeFilePaths(String account, Jid jid, int limit) {
1303 SQLiteDatabase db = this.getReadableDatabase();
1304 final String SQL = "select uuid,relativeFilePath from messages where type in (1,2,5) and deleted=0 and " + Message.RELATIVE_FILE_PATH + " is not null and conversationUuid=(select uuid from conversations where accountUuid=? and (contactJid=? or contactJid like ?)) order by timeSent desc";
1305 final String[] args = {account, jid.toString(), jid.toString() + "/%"};
1306 Cursor cursor = db.rawQuery(SQL + (limit > 0 ? " limit " + limit : ""), args);
1307 List<FilePath> filesPaths = new ArrayList<>();
1308 while (cursor.moveToNext()) {
1309 filesPaths.add(new FilePath(cursor.getString(0), cursor.getString(1)));
1310 }
1311 cursor.close();
1312 return filesPaths;
1313 }
1314
1315 public static class FilePath {
1316 public final UUID uuid;
1317 public final String path;
1318
1319 private FilePath(String uuid, String path) {
1320 this.uuid = UUID.fromString(uuid);
1321 this.path = path;
1322 }
1323 }
1324
1325 public static class FilePathInfo extends FilePath {
1326 public boolean deleted;
1327
1328 private FilePathInfo(String uuid, String path, boolean deleted) {
1329 super(uuid, path);
1330 this.deleted = deleted;
1331 }
1332
1333 public boolean setDeleted(boolean deleted) {
1334 final boolean changed = deleted != this.deleted;
1335 this.deleted = deleted;
1336 return changed;
1337 }
1338 }
1339
1340 public Conversation findConversation(final Account account, final Jid contactJid) {
1341 SQLiteDatabase db = this.getReadableDatabase();
1342 String[] selectionArgs = {account.getUuid(),
1343 contactJid.asBareJid().toString() + "/%",
1344 contactJid.asBareJid().toString()
1345 };
1346 try(final Cursor cursor = db.query(Conversation.TABLENAME, null,
1347 Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID
1348 + " like ? OR " + Conversation.CONTACTJID + "=?)", selectionArgs, null, null, null)) {
1349 if (cursor.getCount() == 0) {
1350 return null;
1351 }
1352 cursor.moveToFirst();
1353 final Conversation conversation = Conversation.fromCursor(cursor);
1354 if (conversation.getJid() instanceof InvalidJid) {
1355 return null;
1356 }
1357 return conversation;
1358 }
1359 }
1360
1361 public void updateConversation(final Conversation conversation) {
1362 final SQLiteDatabase db = this.getWritableDatabase();
1363 final String[] args = {conversation.getUuid()};
1364 db.update(Conversation.TABLENAME, conversation.getContentValues(),
1365 Conversation.UUID + "=?", args);
1366 }
1367
1368 public List<Account> getAccounts() {
1369 SQLiteDatabase db = this.getReadableDatabase();
1370 return getAccounts(db);
1371 }
1372
1373 public List<Jid> getAccountJids(final boolean enabledOnly) {
1374 final SQLiteDatabase db = this.getReadableDatabase();
1375 final List<Jid> jids = new ArrayList<>();
1376 final String[] columns = new String[]{Account.USERNAME, Account.SERVER};
1377 final String where = enabledOnly ? "not options & (1 <<1)" : null;
1378 try (final Cursor cursor = db.query(Account.TABLENAME, columns, where, null, null, null, null)) {
1379 while (cursor != null && cursor.moveToNext()) {
1380 jids.add(Jid.of(cursor.getString(0), cursor.getString(1), null));
1381 }
1382 } catch (final Exception e) {
1383 return jids;
1384 }
1385 return jids;
1386 }
1387
1388 private List<Account> getAccounts(SQLiteDatabase db) {
1389 final List<Account> list = new ArrayList<>();
1390 try (final Cursor cursor =
1391 db.query(Account.TABLENAME, null, null, null, null, null, null)) {
1392 while (cursor != null && cursor.moveToNext()) {
1393 list.add(Account.fromCursor(cursor));
1394 }
1395 }
1396 return list;
1397 }
1398
1399 public boolean updateAccount(Account account) {
1400 SQLiteDatabase db = this.getWritableDatabase();
1401 String[] args = {account.getUuid()};
1402 final int rows = db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", args);
1403 return rows == 1;
1404 }
1405
1406 public boolean deleteAccount(Account account) {
1407 SQLiteDatabase db = this.getWritableDatabase();
1408 String[] args = {account.getUuid()};
1409 final int rows = db.delete(Account.TABLENAME, Account.UUID + "=?", args);
1410 return rows == 1;
1411 }
1412
1413 public boolean updateMessage(Message message, boolean includeBody) {
1414 SQLiteDatabase db = this.getWritableDatabase();
1415 String[] args = {message.getUuid()};
1416 ContentValues contentValues = message.getContentValues();
1417 contentValues.remove(Message.UUID);
1418 if (!includeBody) {
1419 contentValues.remove(Message.BODY);
1420 }
1421 return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1 &&
1422 db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
1423 }
1424
1425 public boolean updateMessage(Message message, String uuid) {
1426 SQLiteDatabase db = this.getWritableDatabase();
1427 String[] args = {uuid};
1428 return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1 &&
1429 db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
1430 }
1431
1432
1433 public boolean deleteMessage(String uuid) {
1434 SQLiteDatabase db = this.getWritableDatabase();
1435 String[] args = {uuid};
1436 return db.delete(Message.TABLENAME, Message.UUID + "=?", args) == 1 &&
1437 db.delete("cheogram." + Message.TABLENAME, Message.UUID + "=?", args) == 1;
1438 }
1439
1440 public void readRoster(Roster roster) {
1441 final SQLiteDatabase db = this.getReadableDatabase();
1442 final String[] args = {roster.getAccount().getUuid()};
1443 try (final Cursor cursor =
1444 db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?", args, null, null, null)) {
1445 while (cursor.moveToNext()) {
1446 roster.initContact(Contact.fromCursor(cursor));
1447 }
1448 }
1449 }
1450
1451 public void writeRoster(final Roster roster) {
1452 long start = SystemClock.elapsedRealtime();
1453 final Account account = roster.getAccount();
1454 final SQLiteDatabase db = this.getWritableDatabase();
1455 db.beginTransaction();
1456 for (Contact contact : roster.getContacts()) {
1457 if (contact.getOption(Contact.Options.IN_ROSTER) || contact.hasAvatarOrPresenceName() || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) {
1458 db.insert(Contact.TABLENAME, null, contact.getContentValues());
1459 } else {
1460 String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
1461 String[] whereArgs = {account.getUuid(), contact.getJid().toString()};
1462 db.delete(Contact.TABLENAME, where, whereArgs);
1463 }
1464 }
1465 db.setTransactionSuccessful();
1466 db.endTransaction();
1467 account.setRosterVersion(roster.getVersion());
1468 updateAccount(account);
1469 long duration = SystemClock.elapsedRealtime() - start;
1470 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": persisted roster in " + duration + "ms");
1471 }
1472
1473 public void deleteMessagesInConversation(Conversation conversation) {
1474 long start = SystemClock.elapsedRealtime();
1475 final SQLiteDatabase db = this.getWritableDatabase();
1476 db.beginTransaction();
1477 final String[] args = {conversation.getUuid()};
1478 int num = db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
1479 db.delete("cheogram.webxdc_updates", Message.CONVERSATION + "=?", args);
1480 db.setTransactionSuccessful();
1481 db.endTransaction();
1482 Log.d(Config.LOGTAG, "deleted " + num + " messages for " + conversation.getJid().asBareJid() + " in " + (SystemClock.elapsedRealtime() - start) + "ms");
1483 }
1484
1485 public void expireOldMessages(long timestamp) {
1486 final String[] args = {String.valueOf(timestamp)};
1487 SQLiteDatabase db = this.getReadableDatabase();
1488 db.beginTransaction();
1489 db.delete(Message.TABLENAME, "timeSent<?", args);
1490 db.delete("cheogram.messages", "timeReceived<?", args);
1491 db.setTransactionSuccessful();
1492 db.endTransaction();
1493 }
1494
1495 public MamReference getLastMessageReceived(Account account) {
1496 Cursor cursor = null;
1497 try {
1498 SQLiteDatabase db = this.getReadableDatabase();
1499 String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) and (conversations.mode=0 or (messages.serverMsgId not null and messages.type=4)) order by messages.timesent desc limit 1";
1500 String[] args = {account.getUuid()};
1501 cursor = db.rawQuery(sql, args);
1502 if (cursor.getCount() == 0) {
1503 return null;
1504 } else {
1505 cursor.moveToFirst();
1506 return new MamReference(cursor.getLong(0), cursor.getString(1));
1507 }
1508 } catch (Exception e) {
1509 return null;
1510 } finally {
1511 if (cursor != null) {
1512 cursor.close();
1513 }
1514 }
1515 }
1516
1517 public long getLastTimeFingerprintUsed(Account account, String fingerprint) {
1518 String SQL = "select messages.timeSent from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and messages.axolotl_fingerprint=? order by messages.timesent desc limit 1";
1519 String[] args = {account.getUuid(), fingerprint};
1520 Cursor cursor = getReadableDatabase().rawQuery(SQL, args);
1521 long time;
1522 if (cursor.moveToFirst()) {
1523 time = cursor.getLong(0);
1524 } else {
1525 time = 0;
1526 }
1527 cursor.close();
1528 return time;
1529 }
1530
1531 public MamReference getLastClearDate(Account account) {
1532 SQLiteDatabase db = this.getReadableDatabase();
1533 String[] columns = {Conversation.ATTRIBUTES};
1534 String selection = Conversation.ACCOUNT + "=?";
1535 String[] args = {account.getUuid()};
1536 Cursor cursor = db.query(Conversation.TABLENAME, columns, selection, args, null, null, null);
1537 MamReference maxClearDate = new MamReference(0);
1538 while (cursor.moveToNext()) {
1539 try {
1540 final JSONObject o = new JSONObject(cursor.getString(0));
1541 maxClearDate = MamReference.max(maxClearDate, MamReference.fromAttribute(o.getString(Conversation.ATTRIBUTE_LAST_CLEAR_HISTORY)));
1542 } catch (Exception e) {
1543 //ignored
1544 }
1545 }
1546 cursor.close();
1547 return maxClearDate;
1548 }
1549
1550 private Cursor getCursorForSession(Account account, SignalProtocolAddress contact) {
1551 final SQLiteDatabase db = this.getReadableDatabase();
1552 String[] selectionArgs = {account.getUuid(),
1553 contact.getName(),
1554 Integer.toString(contact.getDeviceId())};
1555 return db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1556 null,
1557 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1558 + SQLiteAxolotlStore.NAME + " = ? AND "
1559 + SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1560 selectionArgs,
1561 null, null, null);
1562 }
1563
1564 public SessionRecord loadSession(Account account, SignalProtocolAddress contact) {
1565 SessionRecord session = null;
1566 Cursor cursor = getCursorForSession(account, contact);
1567 if (cursor.getCount() != 0) {
1568 cursor.moveToFirst();
1569 try {
1570 session = new SessionRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1571 } catch (IOException e) {
1572 cursor.close();
1573 throw new AssertionError(e);
1574 }
1575 }
1576 cursor.close();
1577 return session;
1578 }
1579
1580 public List<Integer> getSubDeviceSessions(Account account, SignalProtocolAddress contact) {
1581 final SQLiteDatabase db = this.getReadableDatabase();
1582 return getSubDeviceSessions(db, account, contact);
1583 }
1584
1585 private List<Integer> getSubDeviceSessions(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1586 List<Integer> devices = new ArrayList<>();
1587 String[] columns = {SQLiteAxolotlStore.DEVICE_ID};
1588 String[] selectionArgs = {account.getUuid(),
1589 contact.getName()};
1590 Cursor cursor = db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1591 columns,
1592 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1593 + SQLiteAxolotlStore.NAME + " = ?",
1594 selectionArgs,
1595 null, null, null);
1596
1597 while (cursor.moveToNext()) {
1598 devices.add(cursor.getInt(
1599 cursor.getColumnIndex(SQLiteAxolotlStore.DEVICE_ID)));
1600 }
1601
1602 cursor.close();
1603 return devices;
1604 }
1605
1606 public List<String> getKnownSignalAddresses(Account account) {
1607 List<String> addresses = new ArrayList<>();
1608 String[] colums = {"DISTINCT " + SQLiteAxolotlStore.NAME};
1609 String[] selectionArgs = {account.getUuid()};
1610 Cursor cursor = getReadableDatabase().query(SQLiteAxolotlStore.SESSION_TABLENAME,
1611 colums,
1612 SQLiteAxolotlStore.ACCOUNT + " = ?",
1613 selectionArgs,
1614 null, null, null
1615 );
1616 while (cursor.moveToNext()) {
1617 addresses.add(cursor.getString(0));
1618 }
1619 cursor.close();
1620 return addresses;
1621 }
1622
1623 public boolean containsSession(Account account, SignalProtocolAddress contact) {
1624 Cursor cursor = getCursorForSession(account, contact);
1625 int count = cursor.getCount();
1626 cursor.close();
1627 return count != 0;
1628 }
1629
1630 public void storeSession(Account account, SignalProtocolAddress contact, SessionRecord session) {
1631 SQLiteDatabase db = this.getWritableDatabase();
1632 ContentValues values = new ContentValues();
1633 values.put(SQLiteAxolotlStore.NAME, contact.getName());
1634 values.put(SQLiteAxolotlStore.DEVICE_ID, contact.getDeviceId());
1635 values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(session.serialize(), Base64.DEFAULT));
1636 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1637 db.insert(SQLiteAxolotlStore.SESSION_TABLENAME, null, values);
1638 }
1639
1640 public void deleteSession(Account account, SignalProtocolAddress contact) {
1641 SQLiteDatabase db = this.getWritableDatabase();
1642 deleteSession(db, account, contact);
1643 }
1644
1645 private void deleteSession(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1646 String[] args = {account.getUuid(),
1647 contact.getName(),
1648 Integer.toString(contact.getDeviceId())};
1649 db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1650 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1651 + SQLiteAxolotlStore.NAME + " = ? AND "
1652 + SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1653 args);
1654 }
1655
1656 public void deleteAllSessions(Account account, SignalProtocolAddress contact) {
1657 SQLiteDatabase db = this.getWritableDatabase();
1658 String[] args = {account.getUuid(), contact.getName()};
1659 db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1660 SQLiteAxolotlStore.ACCOUNT + "=? AND "
1661 + SQLiteAxolotlStore.NAME + " = ?",
1662 args);
1663 }
1664
1665 private Cursor getCursorForPreKey(Account account, int preKeyId) {
1666 SQLiteDatabase db = this.getReadableDatabase();
1667 String[] columns = {SQLiteAxolotlStore.KEY};
1668 String[] selectionArgs = {account.getUuid(), Integer.toString(preKeyId)};
1669 Cursor cursor = db.query(SQLiteAxolotlStore.PREKEY_TABLENAME,
1670 columns,
1671 SQLiteAxolotlStore.ACCOUNT + "=? AND "
1672 + SQLiteAxolotlStore.ID + "=?",
1673 selectionArgs,
1674 null, null, null);
1675
1676 return cursor;
1677 }
1678
1679 public PreKeyRecord loadPreKey(Account account, int preKeyId) {
1680 PreKeyRecord record = null;
1681 Cursor cursor = getCursorForPreKey(account, preKeyId);
1682 if (cursor.getCount() != 0) {
1683 cursor.moveToFirst();
1684 try {
1685 record = new PreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1686 } catch (IOException e) {
1687 throw new AssertionError(e);
1688 }
1689 }
1690 cursor.close();
1691 return record;
1692 }
1693
1694 public boolean containsPreKey(Account account, int preKeyId) {
1695 Cursor cursor = getCursorForPreKey(account, preKeyId);
1696 int count = cursor.getCount();
1697 cursor.close();
1698 return count != 0;
1699 }
1700
1701 public void storePreKey(Account account, PreKeyRecord record) {
1702 SQLiteDatabase db = this.getWritableDatabase();
1703 ContentValues values = new ContentValues();
1704 values.put(SQLiteAxolotlStore.ID, record.getId());
1705 values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1706 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1707 db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values);
1708 }
1709
1710 public int deletePreKey(Account account, int preKeyId) {
1711 SQLiteDatabase db = this.getWritableDatabase();
1712 String[] args = {account.getUuid(), Integer.toString(preKeyId)};
1713 return db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1714 SQLiteAxolotlStore.ACCOUNT + "=? AND "
1715 + SQLiteAxolotlStore.ID + "=?",
1716 args);
1717 }
1718
1719 private Cursor getCursorForSignedPreKey(Account account, int signedPreKeyId) {
1720 SQLiteDatabase db = this.getReadableDatabase();
1721 String[] columns = {SQLiteAxolotlStore.KEY};
1722 String[] selectionArgs = {account.getUuid(), Integer.toString(signedPreKeyId)};
1723 Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1724 columns,
1725 SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
1726 selectionArgs,
1727 null, null, null);
1728
1729 return cursor;
1730 }
1731
1732 public SignedPreKeyRecord loadSignedPreKey(Account account, int signedPreKeyId) {
1733 SignedPreKeyRecord record = null;
1734 Cursor cursor = getCursorForSignedPreKey(account, signedPreKeyId);
1735 if (cursor.getCount() != 0) {
1736 cursor.moveToFirst();
1737 try {
1738 record = new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1739 } catch (IOException e) {
1740 throw new AssertionError(e);
1741 }
1742 }
1743 cursor.close();
1744 return record;
1745 }
1746
1747 public List<SignedPreKeyRecord> loadSignedPreKeys(Account account) {
1748 List<SignedPreKeyRecord> prekeys = new ArrayList<>();
1749 SQLiteDatabase db = this.getReadableDatabase();
1750 String[] columns = {SQLiteAxolotlStore.KEY};
1751 String[] selectionArgs = {account.getUuid()};
1752 Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1753 columns,
1754 SQLiteAxolotlStore.ACCOUNT + "=?",
1755 selectionArgs,
1756 null, null, null);
1757
1758 while (cursor.moveToNext()) {
1759 try {
1760 prekeys.add(new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT)));
1761 } catch (IOException ignored) {
1762 }
1763 }
1764 cursor.close();
1765 return prekeys;
1766 }
1767
1768 public int getSignedPreKeysCount(Account account) {
1769 String[] columns = {"count(" + SQLiteAxolotlStore.KEY + ")"};
1770 String[] selectionArgs = {account.getUuid()};
1771 SQLiteDatabase db = this.getReadableDatabase();
1772 Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1773 columns,
1774 SQLiteAxolotlStore.ACCOUNT + "=?",
1775 selectionArgs,
1776 null, null, null);
1777 final int count;
1778 if (cursor.moveToFirst()) {
1779 count = cursor.getInt(0);
1780 } else {
1781 count = 0;
1782 }
1783 cursor.close();
1784 return count;
1785 }
1786
1787 public boolean containsSignedPreKey(Account account, int signedPreKeyId) {
1788 Cursor cursor = getCursorForPreKey(account, signedPreKeyId);
1789 int count = cursor.getCount();
1790 cursor.close();
1791 return count != 0;
1792 }
1793
1794 public void storeSignedPreKey(Account account, SignedPreKeyRecord record) {
1795 SQLiteDatabase db = this.getWritableDatabase();
1796 ContentValues values = new ContentValues();
1797 values.put(SQLiteAxolotlStore.ID, record.getId());
1798 values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1799 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1800 db.insert(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, null, values);
1801 }
1802
1803 public void deleteSignedPreKey(Account account, int signedPreKeyId) {
1804 SQLiteDatabase db = this.getWritableDatabase();
1805 String[] args = {account.getUuid(), Integer.toString(signedPreKeyId)};
1806 db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1807 SQLiteAxolotlStore.ACCOUNT + "=? AND "
1808 + SQLiteAxolotlStore.ID + "=?",
1809 args);
1810 }
1811
1812 private Cursor getIdentityKeyCursor(Account account, String name, boolean own) {
1813 final SQLiteDatabase db = this.getReadableDatabase();
1814 return getIdentityKeyCursor(db, account, name, own);
1815 }
1816
1817 private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, boolean own) {
1818 return getIdentityKeyCursor(db, account, name, own, null);
1819 }
1820
1821 private Cursor getIdentityKeyCursor(Account account, String fingerprint) {
1822 final SQLiteDatabase db = this.getReadableDatabase();
1823 return getIdentityKeyCursor(db, account, fingerprint);
1824 }
1825
1826 private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String fingerprint) {
1827 return getIdentityKeyCursor(db, account, null, null, fingerprint);
1828 }
1829
1830 private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, Boolean own, String fingerprint) {
1831 String[] columns = {SQLiteAxolotlStore.TRUST,
1832 SQLiteAxolotlStore.ACTIVE,
1833 SQLiteAxolotlStore.LAST_ACTIVATION,
1834 SQLiteAxolotlStore.KEY};
1835 ArrayList<String> selectionArgs = new ArrayList<>(4);
1836 selectionArgs.add(account.getUuid());
1837 String selectionString = SQLiteAxolotlStore.ACCOUNT + " = ?";
1838 if (name != null) {
1839 selectionArgs.add(name);
1840 selectionString += " AND " + SQLiteAxolotlStore.NAME + " = ?";
1841 }
1842 if (fingerprint != null) {
1843 selectionArgs.add(fingerprint);
1844 selectionString += " AND " + SQLiteAxolotlStore.FINGERPRINT + " = ?";
1845 }
1846 if (own != null) {
1847 selectionArgs.add(own ? "1" : "0");
1848 selectionString += " AND " + SQLiteAxolotlStore.OWN + " = ?";
1849 }
1850 Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1851 columns,
1852 selectionString,
1853 selectionArgs.toArray(new String[selectionArgs.size()]),
1854 null, null, null);
1855
1856 return cursor;
1857 }
1858
1859 public IdentityKeyPair loadOwnIdentityKeyPair(Account account) {
1860 SQLiteDatabase db = getReadableDatabase();
1861 return loadOwnIdentityKeyPair(db, account);
1862 }
1863
1864 private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
1865 String name = account.getJid().asBareJid().toString();
1866 IdentityKeyPair identityKeyPair = null;
1867 Cursor cursor = getIdentityKeyCursor(db, account, name, true);
1868 if (cursor.getCount() != 0) {
1869 cursor.moveToFirst();
1870 try {
1871 identityKeyPair = new IdentityKeyPair(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1872 } catch (InvalidKeyException e) {
1873 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1874 }
1875 }
1876 cursor.close();
1877
1878 return identityKeyPair;
1879 }
1880
1881 public Set<IdentityKey> loadIdentityKeys(Account account, String name) {
1882 return loadIdentityKeys(account, name, null);
1883 }
1884
1885 public Set<IdentityKey> loadIdentityKeys(Account account, String name, FingerprintStatus status) {
1886 Set<IdentityKey> identityKeys = new HashSet<>();
1887 Cursor cursor = getIdentityKeyCursor(account, name, false);
1888
1889 while (cursor.moveToNext()) {
1890 if (status != null && !FingerprintStatus.fromCursor(cursor).equals(status)) {
1891 continue;
1892 }
1893 try {
1894 String key = cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY));
1895 if (key != null) {
1896 identityKeys.add(new IdentityKey(Base64.decode(key, Base64.DEFAULT), 0));
1897 } else {
1898 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Missing key (possibly preverified) in database for account" + account.getJid().asBareJid() + ", address: " + name);
1899 }
1900 } catch (InvalidKeyException e) {
1901 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1902 }
1903 }
1904 cursor.close();
1905
1906 return identityKeys;
1907 }
1908
1909 public long numTrustedKeys(Account account, String name) {
1910 SQLiteDatabase db = getReadableDatabase();
1911 String[] args = {
1912 account.getUuid(),
1913 name,
1914 FingerprintStatus.Trust.TRUSTED.toString(),
1915 FingerprintStatus.Trust.VERIFIED.toString(),
1916 FingerprintStatus.Trust.VERIFIED_X509.toString()
1917 };
1918 return DatabaseUtils.queryNumEntries(db, SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1919 SQLiteAxolotlStore.ACCOUNT + " = ?"
1920 + " AND " + SQLiteAxolotlStore.NAME + " = ?"
1921 + " AND (" + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ?)"
1922 + " AND " + SQLiteAxolotlStore.ACTIVE + " > 0",
1923 args
1924 );
1925 }
1926
1927 private void storeIdentityKey(Account account, String name, boolean own, String fingerprint, String base64Serialized, FingerprintStatus status) {
1928 SQLiteDatabase db = this.getWritableDatabase();
1929 ContentValues values = new ContentValues();
1930 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1931 values.put(SQLiteAxolotlStore.NAME, name);
1932 values.put(SQLiteAxolotlStore.OWN, own ? 1 : 0);
1933 values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1934 values.put(SQLiteAxolotlStore.KEY, base64Serialized);
1935 values.putAll(status.toContentValues());
1936 String where = SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.NAME + "=? AND " + SQLiteAxolotlStore.FINGERPRINT + " =?";
1937 String[] whereArgs = {account.getUuid(), name, fingerprint};
1938 int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, where, whereArgs);
1939 if (rows == 0) {
1940 db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1941 }
1942 }
1943
1944 public void storePreVerification(Account account, String name, String fingerprint, FingerprintStatus status) {
1945 SQLiteDatabase db = this.getWritableDatabase();
1946 ContentValues values = new ContentValues();
1947 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1948 values.put(SQLiteAxolotlStore.NAME, name);
1949 values.put(SQLiteAxolotlStore.OWN, 0);
1950 values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1951 values.putAll(status.toContentValues());
1952 db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1953 }
1954
1955 public FingerprintStatus getFingerprintStatus(Account account, String fingerprint) {
1956 Cursor cursor = getIdentityKeyCursor(account, fingerprint);
1957 final FingerprintStatus status;
1958 if (cursor.getCount() > 0) {
1959 cursor.moveToFirst();
1960 status = FingerprintStatus.fromCursor(cursor);
1961 } else {
1962 status = null;
1963 }
1964 cursor.close();
1965 return status;
1966 }
1967
1968 public boolean setIdentityKeyTrust(Account account, String fingerprint, FingerprintStatus fingerprintStatus) {
1969 SQLiteDatabase db = this.getWritableDatabase();
1970 return setIdentityKeyTrust(db, account, fingerprint, fingerprintStatus);
1971 }
1972
1973 private boolean setIdentityKeyTrust(SQLiteDatabase db, Account account, String fingerprint, FingerprintStatus status) {
1974 String[] selectionArgs = {
1975 account.getUuid(),
1976 fingerprint
1977 };
1978 int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, status.toContentValues(),
1979 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1980 + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1981 selectionArgs);
1982 return rows == 1;
1983 }
1984
1985 public boolean setIdentityKeyCertificate(Account account, String fingerprint, X509Certificate x509Certificate) {
1986 SQLiteDatabase db = this.getWritableDatabase();
1987 String[] selectionArgs = {
1988 account.getUuid(),
1989 fingerprint
1990 };
1991 try {
1992 ContentValues values = new ContentValues();
1993 values.put(SQLiteAxolotlStore.CERTIFICATE, x509Certificate.getEncoded());
1994 return db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
1995 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1996 + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1997 selectionArgs) == 1;
1998 } catch (CertificateEncodingException e) {
1999 Log.d(Config.LOGTAG, "could not encode certificate");
2000 return false;
2001 }
2002 }
2003
2004 public X509Certificate getIdentityKeyCertifcate(Account account, String fingerprint) {
2005 SQLiteDatabase db = this.getReadableDatabase();
2006 String[] selectionArgs = {
2007 account.getUuid(),
2008 fingerprint
2009 };
2010 String[] colums = {SQLiteAxolotlStore.CERTIFICATE};
2011 String selection = SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.FINGERPRINT + " = ? ";
2012 Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME, colums, selection, selectionArgs, null, null, null);
2013 if (cursor.getCount() < 1) {
2014 return null;
2015 } else {
2016 cursor.moveToFirst();
2017 byte[] certificate = cursor.getBlob(cursor.getColumnIndex(SQLiteAxolotlStore.CERTIFICATE));
2018 cursor.close();
2019 if (certificate == null || certificate.length == 0) {
2020 return null;
2021 }
2022 try {
2023 CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
2024 return (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(certificate));
2025 } catch (CertificateException e) {
2026 Log.d(Config.LOGTAG, "certificate exception " + e.getMessage());
2027 return null;
2028 }
2029 }
2030 }
2031
2032 public void storeIdentityKey(Account account, String name, IdentityKey identityKey, FingerprintStatus status) {
2033 storeIdentityKey(account, name, false, CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()), Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT), status);
2034 }
2035
2036 public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) {
2037 storeIdentityKey(account, account.getJid().asBareJid().toString(), true, CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize()), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT), FingerprintStatus.createActiveVerified(false));
2038 }
2039
2040
2041 private void recreateAxolotlDb(SQLiteDatabase db) {
2042 Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + ">>> (RE)CREATING AXOLOTL DATABASE <<<");
2043 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SESSION_TABLENAME);
2044 db.execSQL(CREATE_SESSIONS_STATEMENT);
2045 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.PREKEY_TABLENAME);
2046 db.execSQL(CREATE_PREKEYS_STATEMENT);
2047 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME);
2048 db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
2049 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.IDENTITIES_TABLENAME);
2050 db.execSQL(CREATE_IDENTITIES_STATEMENT);
2051 }
2052
2053 public void wipeAxolotlDb(Account account) {
2054 String accountName = account.getUuid();
2055 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ">>> WIPING AXOLOTL DATABASE FOR ACCOUNT " + accountName + " <<<");
2056 SQLiteDatabase db = this.getWritableDatabase();
2057 String[] deleteArgs = {
2058 accountName
2059 };
2060 db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
2061 SQLiteAxolotlStore.ACCOUNT + " = ?",
2062 deleteArgs);
2063 db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
2064 SQLiteAxolotlStore.ACCOUNT + " = ?",
2065 deleteArgs);
2066 db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2067 SQLiteAxolotlStore.ACCOUNT + " = ?",
2068 deleteArgs);
2069 db.delete(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2070 SQLiteAxolotlStore.ACCOUNT + " = ?",
2071 deleteArgs);
2072 }
2073
2074 public List<ShortcutService.FrequentContact> getFrequentContacts(int days) {
2075 SQLiteDatabase db = this.getReadableDatabase();
2076 final String SQL = "select " + Conversation.TABLENAME + "." + Conversation.ACCOUNT + "," + Conversation.TABLENAME + "." + Conversation.CONTACTJID + " from " + Conversation.TABLENAME + " join " + Message.TABLENAME + " on conversations.uuid=messages.conversationUuid where messages.status!=0 and carbon==0 and conversations.mode=0 and messages.timeSent>=? group by conversations.uuid order by count(body) desc limit 4;";
2077 String[] whereArgs = new String[]{String.valueOf(System.currentTimeMillis() - (Config.MILLISECONDS_IN_DAY * days))};
2078 Cursor cursor = db.rawQuery(SQL, whereArgs);
2079 ArrayList<ShortcutService.FrequentContact> contacts = new ArrayList<>();
2080 while (cursor.moveToNext()) {
2081 try {
2082 contacts.add(new ShortcutService.FrequentContact(cursor.getString(0), Jid.of(cursor.getString(1))));
2083 } catch (Exception e) {
2084 Log.d(Config.LOGTAG, e.getMessage());
2085 }
2086 }
2087 cursor.close();
2088 return contacts;
2089 }
2090}