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