-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathChatDelegate.swift
1295 lines (992 loc) · 47.2 KB
/
ChatDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Foundation
import SQLite3
import SwiftUI
import Photos
#if os(macOS)
import Contacts
#endif
let chat_columns = ["m.ROWID", "m.is_read", "m.is_from_me", "m.text", "m.item_type", "m.date_read", "m.date", "m.cache_has_attachments", "m.balloon_bundle_id", "m.share_direction", "c.chat_identifier", "c.display_name", "c.room_name"]
final class ChatDelegate {
let settings = Settings.shared()
private static var sharedDelegate: ChatDelegate = {
let delegate = ChatDelegate()
return delegate
}()
class func shared() -> ChatDelegate {
return sharedDelegate
}
final func createConnection(connection_string: String = Const.sms_db_address) -> OpaquePointer? {
/// This simply returns an opaque pointer to the database at `connection_string`, allowing for sqlite connections.
var db: OpaquePointer?
/// Open the database
let return_code = sqlite3_open_v2(connection_string, &db, SQLITE_OPEN_READONLY, nil)
if return_code != SQLITE_OK {
Const.log("WARNING: error opening database at \(connection_string): \(return_code)")
sqlite3_close(db)
db = nil
return db
}
sqlite3_busy_timeout(db, 20)
Const.log("opened database")
/// Return pointer to the database
return db
}
final func closeDatabase(_ db: inout OpaquePointer?) {
let close_code = sqlite3_close(db)
if close_code != SQLITE_OK {
Const.log("WARNING: error closing database. Errror: \(close_code)", warning: true)
}
db = nil
}
/// It's just too big to do one line
final func selectFromSql(
db: OpaquePointer?,
columns: [String],
table: String,
condition: String = "",
args: [Any] = [Any](),
num_items: Int = -1,
offset: Int = 0,
split_ids: Bool = false
) -> [[String:String]] {
/// This executes an SQL query, basically 'SELECT `column` from `table` `condition` LIMIT `offset`, `num_items`', on `db`
/// If you join tables, you may need to use stuff like `SELECT m.attr, h.otherattr from table m inner join othertable h;`.
/// If you leave `split_ids` as false, it will return the nested dicts with their original select columns, e.g. `m.attr` and `h.otherattr`
/// However, if you set it to true, it will return them without the table identifier, e.g. `attr` and `otherattr`. Be conscious of that.
/// You also must be very careful of inserting values into the condition: For every place that you want to insert a String or Int into the
/// condition (e.g. `LIKE "STRING"`), you need to place a `?` in the `condition` string, and place the Strings/Ints that need to replace them
/// into the `args` array.
/// For example, to do `... WHERE text LIKE "HELLO" AND ROWID IS 4`, you'll want `condition == "... WHERE text like ? AND ROWID IS ?`
/// and `args == ["HELLO", 4]`. This will allow them to be sanitized and inserted correctly.
/// Construct the query
var sqlString = "SELECT \(columns.joined(separator: ", ")) from \(table)"
if condition != "" {
sqlString += " \(condition)"
}
if num_items > 0 || offset != 0 {
sqlString += " LIMIT \(offset), \(num_items)"
}
sqlString += ";"
Const.log("full sql query: \(sqlString)")
var statement: OpaquePointer?
Const.log("opened statement")
var main_return = [[String:String]]()
/// Prepare the database for querying `sqlString`
if sqlite3_prepare_v2(db, sqlString, -1, &statement, nil) != SQLITE_OK {
if let errmsg = sqlite3_errmsg(db) {
Const.log("WARNING: error preparing select: \(errmsg)", warning: true)
}
return main_return
}
for (idx, arg) in args.enumerated() {
if let arg = arg as? Int {
sqlite3_bind_int(statement, Int32(idx + 1), Int32(arg))
} else if let arg = arg as? String {
sqlite3_bind_text(statement, Int32(idx + 1), NSString(string: arg).utf8String, -1, nil)
}
}
var i = 0
/// for each row, up to num_items rows
while sqlite3_step(statement) == SQLITE_ROW && (i < num_items || num_items <= 0) {
var minor_return = [String:String]()
/// for every column in the row
for j in 0..<columns.count {
var tiny_return = ""
/// Get the string for the specific column
if let tiny_return_cstring = sqlite3_column_text(statement, Int32(j)) {
tiny_return = String(cString: tiny_return_cstring)
}
/// add it to return value
let id = split_ids ? String(columns[j].split(separator: ".")[1]) : columns[j]
minor_return[id] = tiny_return
}
/// Add it to return value
main_return.append(minor_return)
i += 1
}
/// Finalize; has to be done after getting info.
if sqlite3_finalize(statement) != SQLITE_OK, let errmsg = sqlite3_errmsg(db) {
Const.log("WARNING: error finalizing prepared statement: \(errmsg)", warning: true)
}
statement = nil
Const.log("destroyed statement")
/// Since we passed `db` in as a parameter, we don't close the database here, but rather in the function where `db` was constructed
return main_return
}
final func parseTexts(_ texts: inout [[String:Any]], db: OpaquePointer?, contact_db: OpaquePointer?, is_group: Bool = false) {
Const.log("parsing texts with length \(texts.count)")
var names = [String:String]() /// key: id, val: display name.
let bad_bundles = ["com.apple.DigitalTouchBalloonProvider", "com.apple.Handwriting.HandwritingProvider"]
for i in 0..<texts.count {
texts[i]["text"] = (texts[i]["text"] as? String ?? "").replacingOccurrences(of: "\u{fffc}", with: "", options: NSString.CompareOptions.literal, range: nil)
/// Get details about attachments if there are any attachments associated with this message
if texts[i]["cache_has_attachments"] as? String == "1", let rowid = texts[i]["ROWID"] as? String {
texts[i]["attachments"] = getAttachmentFromMessage(mid: rowid)
}
/// Get the sender's name for this text if it is a group chat and it's not from me
if is_group, texts[i]["is_from_me"] as? String == "0", let id = texts[i]["id"] as? String, !id.isEmpty {
if let name = names[id] {
texts[i]["sender"] = name
} else {
let name = getDisplayNameWithDb(sms_db: db, contact_db: contact_db, chat_id: id, is_group: is_group)
texts[i]["sender"] = name
names[id] = name
}
}
if texts[i]["item_type"] as? String == "1" {
texts[i]["item_description"] = getJoinedChatDescription(texts[i]["other_handle"] as? String ?? "", "")
} else if texts[i]["group_action_type"] as? String == "1" {
texts[i]["item_description"] = "\(texts[i]["sender"] as? String ?? "") set the group image"
}
/// This checks if it has anything in the `payload_data` field, which would imply that it is a special message (e.g. rich link, gamepigeon message, etc).
/// However, it doesn't enter the `if` if the message is a handwritten message or a digital touch message, since I don't know how to parse those yet.
if (texts[i]["payload_data"] as? String)?.isEmpty ?? false,
let balloon_id = texts[i]["balloon_bundle_id"] as? String,
!bad_bundles.contains(balloon_id),
let rowid = texts[i]["ROWID"] as? String {
let link_info = getLinkInfo(rowid, db: db)
texts[i]["link_title"] = link_info["title"]
texts[i]["link_subtitle"] = link_info["subtitle"]
texts[i]["link_type"] = link_info["type"]
}
texts[i].removeValue(forKey: "payload_data")
if let serv = texts[i]["service"] as? String {
texts[i]["imessage"] = serv == "iMessage"
texts[i].removeValue(forKey: "service")
}
texts[i]["is_group"] = is_group
/// Change values that shouldn't be strings to the correct type
if let rowid = texts[i]["ROWID"] as? String {
texts[i]["ROWID"] = Int(rowid)
}
if let from_me = texts[i]["is_from_me"] as? String {
texts[i]["is_from_me"] = from_me == "1"
}
if let date = texts[i]["date"] as? String {
texts[i]["date"] = Int(date)
}
if let read = texts[i]["date_read"] as? String {
texts[i]["date_read"] = Int(read)
}
if let cache = texts[i]["cache_has_attachments"] as? String {
texts[i]["cache_has_attachments"] = cache == "1"
}
if let am_type = texts[i]["associated_message_type"] as? String {
texts[i]["associated_message_type"] = Int(am_type)
}
if let type = texts[i]["item_type"] as? String {
texts[i]["item_type"] = Int(type)
}
if let ga_type = texts[i]["group_action_type"] as? String {
texts[i]["group_action_type"] = Int(ga_type) ?? 0
}
}
}
final func parseChats(_ chats: [[String:Any]], db: OpaquePointer?, contact_db: OpaquePointer?) -> [[String:Any]] {
var ret = [[String:Any]]()
for chat in chats {
guard let chat_id = chat["c.chat_identifier"] as? String else {
continue
}
var new_chat = [String:Any]()
new_chat["has_unread"] = chat["m.is_from_me"] as? String ?? "" == "0" &&
chat["m.date_read"] as? String ?? "" == "0" &&
chat["m.text"] != nil &&
chat["m.is_read"] as? String ?? "" == "0" &&
chat["m.item_type"] as? String ?? "" == "0"
let latest_text = String((chat["m.text"] as? String ?? "").replacingOccurrences(of: "\u{fffc}", with: "", options: NSString.CompareOptions.literal, range: nil))
new_chat["latest_text"] = latest_text
let is_group = (chat["c.room_name"] as? String ?? "").count > 0
if let d_name = chat["c.display_name"] as? String, d_name.count > 0 {
new_chat["display_name"] = d_name
} else {
new_chat["display_name"] = getDisplayNameWithDb(sms_db: db, contact_db: contact_db, chat_id: chat_id, is_group: is_group)
}
if latest_text.isEmpty && chat["m.cache_has_attachments"] as? String != "0" {
let att = getAttachmentFromMessage(mid: chat["m.ROWID"] as? String ?? "")
if att.count != 0 && att[0].count == 2 && att[0]["mime_type"]?.count ?? 0 > 0 {
new_chat["latest_text"] = "Attachment: \(att[0]["mime_type"]?.split(separator: "/").first ?? "1 File")"
} else {
new_chat["latest_text"] = "Attachment: 1 file"
}
} else if chat["m.balloon_bundle_id"] as? String == "com.apple.DigitalTouchBalloonProvider" {
new_chat["latest_text"] = "Digital Touch Message"
} else if chat["m.balloon_bundle_id"] as? String == "com.apple.Handwriting.HandwritingProvider" {
new_chat["latest_text"] = "Handwritten Message"
} else if chat["m.item_type"] as? String == "4" {
new_chat["latest_text"] = chat["m.share_direction"] as? String == "1" ?
"\(new_chat["display_name"] as? String ?? "They") started sharing location with you" :
"You started sharing location with \(new_chat["display_name"] as? String ?? "them")"
}
new_chat["relative_time"] = Const.getRelativeTime(ts: Double(chat["m.date"] as? String ?? "0") ?? 0.0)
new_chat["is_group"] = is_group
new_chat["chat_identifier"] = chat_id
new_chat["time_marker"] = Int(chat["m.date"] as? String ?? "0")
new_chat["members"] = getGroupRecipientsWithDb(contact_db: contact_db, db: db, ci: chat_id)
.map { ["chat_identifier": $0.0, "display_name": $0.1 ]}
ret.append(new_chat)
}
return ret
}
final func getDisplayName(chat_id: String, is_group: Bool? = nil) -> String {
/// This does the same thing as getDisplayName, but doesn't take db as an argument. This allows for fetching of a single name,
/// when you don't need to get a lot of names at once.
Const.log("Getting display name for \(chat_id)")
/// Connect to contact database
var contact_db = createConnection(connection_string: Const.contacts_address)
var sms_db = createConnection()
if contact_db == nil || sms_db == nil { return "" }
/// get name
let name = getDisplayNameWithDb(sms_db: sms_db, contact_db: contact_db, chat_id: chat_id, is_group: is_group)
/// close
closeDatabase(&sms_db)
closeDatabase(&contact_db)
Const.log("destroyed dbs")
return name
}
final func getDisplayNameWithDb(sms_db: OpaquePointer?, contact_db: OpaquePointer?, chat_id: String, is_group: Bool? = nil) -> String {
/// Gets the first + last name of a contact with the phone number or email of `chat_id`
Const.log("Getting display name for \(chat_id) with db")
/// Support for group chats
if is_group == nil || is_group ?? false {
let check_name = selectFromSql(db: sms_db, columns: ["room_name", "display_name"], table: "chat", condition: "where chat_identifier is ?", args: [chat_id], num_items: 1)
if check_name.count > 0 && check_name[0]["room_name"]?.count ?? 0 > 0 {
if let display_name = check_name.first?["display_name"], !display_name.isEmpty {
return display_name
}
let recipients = getGroupRecipientsWithDb(contact_db: contact_db, db: sms_db, ci: chat_id)
return recipients.map(\.1).joined(separator: ", ")
}
}
/// For iOS, I extract info from the Address book database. I could use the Contacts framework, but it adds ~0.5 seconds per ~40 chats.
/// So this is is a bit more code, but it is significantly faster, and I have yet to come to a circumstance where it's less accurate.
/// I think it's definitely worth it.
#if os(iOS)
var display_name_array = [[String:String]]()
if chat_id.contains("@") { /// if an email
display_name_array = selectFromSql(db: contact_db, columns: ["c0First", "c1Last"], table: "ABPersonFullTextSearch_content", condition: "WHERE c17Email LIKE ?", args: ["%\(chat_id)%"])
} else if chat_id.contains("+") {
display_name_array = selectFromSql(db: contact_db, columns: ["c0First", "c1Last"], table: "ABPersonFullTextSearch_content", condition: "WHERE c16Phone LIKE ?", args: ["%\(chat_id)%"], num_items: 1)
} else {
display_name_array = selectFromSql(db: contact_db, columns: ["c0First", "c1Last"], table: "ABPersonFullTextSearch_content", condition: "WHERE c16Phone LIKE ? and c16Phone NOT LIKE \"%+%\"", args: ["%\(chat_id) "])
}
if display_name_array.count == 0 {
let unc = selectFromSql(db: sms_db, columns: ["uncanonicalized_id"], table: "handle", condition: "WHERE id is ?", args: [chat_id])
guard unc.count > 0, let ret = unc[0]["uncanonicalized_id"] else {
return chat_id
}
return ret.count == 0 ? chat_id : ret
}
/// combine first name and last name
let full_name: String = "\(display_name_array[0]["c0First"] ?? "no_first") \(display_name_array[0]["c1Last"] ?? "no_last")"
#elseif os(macOS)
let pred = chat_id.contains("@") ? CNContact.predicateForContacts(matchingEmailAddress: chat_id) : CNContact.predicateForContacts(matching: CNPhoneNumber(stringValue: chat_id))
let keys: [CNKeyDescriptor] = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey] as! [CNKeyDescriptor]
let store = CNContactStore()
var con_access = true
let dp_group = DispatchGroup()
dp_group.enter()
store.requestAccess(for: CNEntityType.contacts, completionHandler: { accepted, _ in
con_access = accepted
dp_group.leave()
})
dp_group.wait()
var full_name = chat_id
if con_access {
do {
let contacts = try store.unifiedContacts(matching: pred, keysToFetch: keys)
if contacts.count > 0 {
full_name = "\(contacts[0].givenName) \(contacts[0].familyName)"
}
} catch {
print("no... :(") /// will fix
}
}
#endif
Const.log("full name for \(chat_id) is \(full_name)")
return full_name
}
final func getGroupRecipientsWithDb(contact_db: OpaquePointer?, db: OpaquePointer?, ci: String) -> [(String, String)] {
// returns (chat_identifier, display_name)
Const.log("Getting group chat recipients for \(ci)")
let recipients = selectFromSql(db: db, columns: ["id"], table: "handle", condition: "WHERE ROWID in (SELECT handle_id from chat_handle_join WHERE chat_id in (SELECT ROWID from chat where chat_identifier is ?))", args: [ci])
var ret_val = [(String, String)]()
for i in recipients {
if let id = i["id"] {
/// get name for person
let ds = getDisplayNameWithDb(sms_db: db, contact_db: contact_db, chat_id: id, is_group: false)
ret_val.append((id, ds.count == 0 ? id : ds))
}
}
Const.log("Finished retrieving recipients for \(ci)")
return ret_val
}
final func loadMessages(num: String, num_items: Int, offset: Int = 0, from: Int = 0, surrounding: Int? = nil) -> [[String:Any]] {
/// This loads the latest `num_items` messages from/to `num`, offset by `offset`.
Const.log("getting messages for \(num)")
/// Create connection to text and contact databases
var db = createConnection()
var contact_db = createConnection(connection_string: Const.contacts_address)
if db == nil || contact_db == nil { return [[String:String]]() }
/// check if it's a group chat
let is_group = num.prefix(4) == "chat" && !num.contains("@") && num.count >= 20
var from_string: String = ""
var fixed_num = "?"
if num.contains(",") {
/// So that you can merge multiple conversations
fixed_num = [String](repeating: "?", count: num.split(separator: ",").count).joined(separator: " or chat_identifier is ")
}
if from != 0 {
from_string = " and m.is_from_me is \(from == 1 ? 1 : 0)"
}
var messages: [[String:Any]] = selectFromSql(
db: db,
columns: ["m.ROWID", "m.guid", "m.text", "m.subject", "m.is_from_me", "m.date", "m.date_read", "m.service", "m.cache_has_attachments", "m.balloon_bundle_id", "m.payload_data", "m.associated_message_guid", "m.associated_message_type", "m.item_type", "m.group_action_type", "m.other_handle", "h.id"],
table: "message m",
condition: "left join handle h on h.ROWID = m.handle_id where m.ROWID in (select message_id from chat_message_join where chat_id in (select ROWID from chat where chat_identifier is \(fixed_num)\(from_string))) order by m.date desc",
args: num.split(separator: ",").map({String($0)}),
num_items: num_items,
offset: offset,
split_ids: true
)
parseTexts(&messages, db: db, contact_db: contact_db, is_group: is_group)
/// close dbs
closeDatabase(&db)
closeDatabase(&contact_db)
Const.log("destroyed db; returning messages")
return messages
}
final func loadChats(num_to_load: Int, offset: Int = 0) -> [[String:Any]] {
/// This loads the most recent `num_to_load` conversations, offset by `offset`
Const.log("Getting \(String(num_to_load)) chats")
var pinned_chats: [String]? = nil
let dp_group = DispatchGroup()
if offset == 0 {
#if os(iOS)
if Const.getOSVersion() >= 14.0 {
DispatchQueue.global(qos: .default).async {
dp_group.enter()
pinned_chats = self.getPinnedChats()
dp_group.leave()
}
}
#elseif os(macOS)
if Const.getOSVersion() >= 10.15 {
pinned_chats = [String]() /// TEMPORARY. Will fix.
}
#endif
}
/// Create connections
var db = createConnection()
var contacts_db = createConnection(connection_string: Const.contacts_address)
if db == nil || contacts_db == nil { return [[String:Any]]() }
var addresses = [[String:String]]()
var chat_identifiers = [String:[String]]()
var checked = [String]()
let chats: [[String:Any]] = selectFromSql(
db: db,
columns: chat_columns,
table: "chat_message_join j",
condition: "inner join message m on j.message_id = m.ROWID inner join chat c on c.ROWID = j.chat_id where j.message_date in (select max(j.message_date) from chat_message_join j inner join chat c on c.ROWID = j.chat_id group by c.chat_identifier) order by j.message_date desc",
num_items: num_to_load,
offset: offset
)
inner: if settings.combine_contacts {
/// This `if` gets all the addresses associated with your contacts, parses them with regex to get all possible `chat_identifier`s,
/// then puts them into a nested array. Later on, the conversations check themselves with the nested arrays to find the associated `chat_identifier`s.
/// This section is probably not very optimized and probably also inaccurate. That's why it's optional.
addresses = selectFromSql(db: contacts_db, columns: ["c16Phone", "c17Email"], table: "ABPersonFullTextSearch_content")
let identifiers = selectFromSql(db: db, columns: ["chat_identifier"], table: "chat").map({$0["chat_identifier"] ?? ""})
for i in addresses {
var personal_addresses = i["c17Email"]?.split(separator: " ").map({String($0)}) ?? [String]()
let phone = i["c16Phone"] ?? ""
var checked_matches = personal_addresses
personal_addresses += phone.split(separator: " ").map({String($0)})
outerfor: for j in personal_addresses {
let len = (j.filter { "0"..."9" ~= $0}).count
if !(j.count >= 5 && (len == j.count || (len == j.count - 1 && j.first == "+"))) {
continue outerfor
}
for l in checked_matches {
if (l.contains(j)) {
continue outerfor
}
}
if identifiers.contains(j) {
checked_matches.append(j)
}
}
for j in checked_matches {
chat_identifiers[j] = checked_matches
}
}
}
var ret = parseChats(chats, db: db, contact_db: contacts_db)
dp_group.wait()
/// Just in case your pinned chats aren't in your most recent texts
if offset == 0, let pins = pinned_chats {
for chat in pins {
let details = getChatDetails(chat)
ret.append(details)
}
}
for idx in 0..<ret.count {
guard let chat_id = ret[idx]["chat_identifier"] as? String else {
continue
}
if let pins = pinned_chats {
ret[idx]["pinned"] = pins.contains(chat_id)
pinned_chats?.removeAll(where: { $0 == chat_id })
} else {
ret[idx]["pinned"] = false
}
if settings.combine_contacts {
if checked.contains(chat_id) { continue }
let these_addresses: [String] = chat_identifiers[chat_id] ?? [String]()
ret[idx]["addresses"] = these_addresses.joined(separator: ",")
for j in these_addresses {
checked.append(j)
}
} else {
ret[idx]["addresses"] = chat_id
}
}
/// close
closeDatabase(&contacts_db)
closeDatabase(&db)
Const.log("destroyed db")
return ret
}
final func getChatDetails(_ chat_id: String) -> [String:Any] {
var db = createConnection()
var contacts_db = createConnection(connection_string: Const.contacts_address)
if db == nil || contacts_db == nil { return [String:Any]() }
var pinned_chats: [String]? = [String]()
let dp_group = DispatchGroup()
if Const.getOSVersion() >= 14.0 {
DispatchQueue.global().async {
dp_group.enter()
pinned_chats = self.getPinnedChats()
dp_group.leave()
}
}
let chats: [[String:Any]] = selectFromSql(
db: db,
columns: chat_columns,
table: "chat_message_join j",
condition: "inner join message m on j.message_id = m.ROWID inner join chat c on c.ROWID = j.chat_id where c.chat_identifier is ?",
args: [chat_id],
num_items: 1
)
let arr = parseChats(chats, db: db, contact_db: contacts_db)
guard chats.count > 0, let chat_id = chats[0]["c.chat_identifier"] as? String, arr.count > 0 else {
return [String:Any]()
}
var ret = arr[0]
dp_group.wait()
ret["pinned"] = pinned_chats?.contains(chat_id)
closeDatabase(&db)
closeDatabase(&contacts_db)
return ret
}
final func returnImageData(chat_id: String) -> Data {
/// This does the same thing as returnImageDataDB, but without the `contact_db` and `image_db` as arguments, if you just need to get like 1 image
Const.log("Getting image data for chat_id \(chat_id)")
/// Make connections
var contact_db = createConnection(connection_string: Const.contacts_address)
#if os(iOS)
var image_db = createConnection(connection_string: Const.contact_images_address)
var sms_db = createConnection()
if contact_db == nil || image_db == nil || sms_db == nil {
return Data()
}
/// get image data with dbs as parameters
let return_val = returnImageDataDB(chat_id: chat_id, contact_db: contact_db!, image_db: image_db!, sms_db: sms_db!)
/// close
closeDatabase(&image_db)
closeDatabase(&sms_db)
#elseif os(macOS)
let image_name = selectFromSql(db: contact_db, columns: ["ZUNIQUEID"], table: "ZABCDRECORD", condition: "WHERE ZCONTACTINDEX IN (SELECT Z_PK from ZABCDCONTACTINDEX WHERE ZSTRINGFORINDEXING LIKE ?)", args: ["%\(chat_id)%"], num_items: 1)
let return_val: Data
if image_name.count == 0 || image_name[0].count == 0 {
return_val = NSImage(named: "profile")?.tiffRepresentation ?? Data()
} else {
let image_name_string = (image_name[0]["ZUNIQUEID"] ?? "").split(separator: ":")[0]
let image_path = URL(fileURLWithPath: Const.contact_images_address + image_name_string)
do {
return_val = try Data.init(contentsOf: image_path)
} catch {
return_val = NSImage(named: "profile")?.tiffRepresentation ?? Data()
}
}
#endif
closeDatabase(&contact_db)
return return_val
}
final func returnImageDataDB(chat_id: String, contact_db: OpaquePointer, image_db: OpaquePointer, sms_db: OpaquePointer) -> Data {
/// This returns the profile picture for someone with the phone number or email `chat_id` as pure image data
Const.log("Getting image data with db for chat_id \(chat_id)")
if chat_id == "default" {
return SMImage(named: "profile").parseableData() ?? Data()
}
if chat_id.prefix(4) == "chat" && !chat_id.contains("@") && chat_id.count >= 20 {
/// This is a kinda hacky workaround to get the image for a group
/// So basically, whenever a group image is set, a message is inserted into the SMS database with a `group_action_type` of 1 and 1 attachment
/// The attachment is the new profile picture for the group. So this just grabs the most recent message with 1 for the `group_action_type`,
/// gets the location of its associated attachment, and returns that.
let image_location = selectFromSql(
db: sms_db,
columns: ["filename"],
table: "attachment",
condition: "where ROWID in (select attachment_id from message_attachment_join where message_id in (select ROWID from message where group_action_type is 1 and cache_has_attachments is 1 and ROWID in (select message_id from chat_message_join where chat_id in (select ROWID from chat where chat_identifier is ?)) order by date DESC))",
args: [chat_id],
num_items: 1
)
guard image_location.count == 1, let image_url = image_location[0]["filename"] else {
return returnImageDataDB(chat_id: "default", contact_db: contact_db, image_db: image_db, sms_db: sms_db)
}
let image = SMImage(image_url.replacingOccurrences(of: "~", with: "/var/mobile"))
return image.parseableData() ?? Data()
}
var docid = [[String:String]]()
var fixed_num = chat_id
if chat_id.contains(",") {
/// Just turns it into a proper sql condition based on if the specific address is an email or number
let splits = chat_id.split(separator: ",").map({(String($0).contains("@") ? "c17Email" : "c16Phone") + " like ?"})
fixed_num = splits.joined(separator: " or ")
}
/// get docid. docid is the identifier that corresponds to each contact, allowing for us to get their image
if chat_id.contains(",") { /// Multiple addresses in one
docid = selectFromSql(db: contact_db, columns: ["docid"], table: "ABPersonFullTextSearch_content", condition: "WHERE \(fixed_num)", args: chat_id.split(separator: ",").map({"%\($0)%"}), num_items: 1)
} else if chat_id.contains("@") { /// if an email
docid = selectFromSql(db: contact_db, columns: ["docid"], table: "ABPersonFullTextSearch_content", condition: "WHERE c17Email LIKE ?", args: ["%\(chat_id)%"], num_items: 1)
} else if chat_id.contains("+") {
docid = selectFromSql(db: contact_db, columns: ["docid"], table: "ABPersonFullTextSearch_content", condition: "WHERE c16Phone LIKE ?", args: ["%\(chat_id)%"], num_items: 1)
} else {
docid = selectFromSql(db: contact_db, columns: ["docid"], table: "ABPersonFullTextSearch_content", condition: "WHERE c16Phone LIKE ? and c16Phone NOT LIKE \"%+%\"", args: ["%\(chat_id) "], num_items: 1)
}
guard docid.count > 0 else { return Data() }
/// each contact image is stored as a blob in the sqlite database, not as a file.
/// That's why we need a special query to get it, and can't use the selectFromSql function(s).
let sqlString = "SELECT data FROM ABThumbnailImage WHERE record_id is \(docid[0]["docid"] ?? "") order by format asc limit 1;"
var statement: OpaquePointer?
Const.log("opened statement")
if sqlite3_prepare_v2(image_db, sqlString, -1, &statement, nil) != SQLITE_OK {
if let errmsg = sqlite3_errmsg(image_db) {
Const.log("WARNING: error preparing select: \(errmsg)", warning: true)
}
return Data()
}
var jpgdata: Data = Data()
if sqlite3_step(statement) == SQLITE_ROW {
if let tiny_return_blob = sqlite3_column_blob(statement, 0) {
/// Getting the data
let len: Int32 = sqlite3_column_bytes(statement, 0)
let dat: NSData = NSData(bytes: tiny_return_blob, length: Int(len))
jpgdata = dat as Data
}
}
if sqlite3_finalize(statement) != SQLITE_OK {
if let errmsg = sqlite3_errmsg(image_db) {
Const.log("WARNING: error finalizing prepared statement: \(errmsg)", warning: true)
}
}
statement = nil
Const.log("destroyed statement")
return jpgdata
}
final func getAttachmentFromMessage(mid: String) -> [[String:String]] {
/// This returns the file path for all attachments associated with a certain message with the message_id of `mid`
Const.log("Getting attachment for mid \(mid)")
/// create connection, get attachments information
var db = createConnection()
if db == nil { return [[String:String]]() }
var files = selectFromSql(db: db, columns: ["filename", "mime_type"], table: "attachment", condition: "WHERE ROWID in (SELECT attachment_id from message_attachment_join WHERE message_id is ?)", args: [Int(mid) ?? 0])
for i in 0..<files.count {
files[i]["filename"] = String(files[i]["filename"]?.dropFirst(Const.attachment_address_prefix.count - Const.user_home_url.count + 2) ?? "")
}
closeDatabase(&db)
return files
}
final func getAttachmentType(path: String) -> String {
/// This gets the file type of the attachment at `path`
Const.log("Getting attachment type for @ \(path)")
var db = createConnection()
if db == nil { return "" }
let file_type_array = selectFromSql(db: db, columns: ["mime_type"], table: "attachment", condition: "WHERE filename like ?", args: ["%\(path)%"], num_items: 1)
let return_val = file_type_array.count > 0 ? file_type_array[0]["mime_type"] ?? "image/jpeg" : "image/jpeg"
closeDatabase(&db)
return return_val
}
final func getAttachmentDataFromPath(path: String) -> [Any] { /// [0] = data, [1] = mime_type
/// This returns the pure data of a file (attachment) at `path`
Const.log("Getting attachment data from path \(path)")
let parsed_path = path.replacingOccurrences(of: "/../", with: "/") /// To prevent LFI
let type = getAttachmentType(path: parsed_path)
var info: [Any] = [Data.init(capacity: 0), type]
do {
/// Pretty straightforward -- get data and return it
if (type == "image/heic") { /// Since they're used so much
let attachment_data = SMImage(Const.attachment_address_prefix + parsed_path).parseableData()
if attachment_data == nil {
Const.log("Failed to get JPEG data from HEIC Image at \(Const.attachment_address_prefix + parsed_path).", warning: true)
}
info[0] = attachment_data ?? Data() as Any
} else {
info[0] = try Data.init(contentsOf: URL(fileURLWithPath: Const.attachment_address_prefix + parsed_path))
}
} catch {
Const.log("WARNING: failed to load image for path \(Const.attachment_address_prefix + parsed_path)", warning: true)
}
return info
}
final func searchForString(term: String, case_sensitive: Bool = false, bridge_gaps: Bool = true, group_by_time: Bool = true) -> Any {
/// This gets all texts with `term` in them; `case_sensitive`, `bridge_gaps`, and `group_by_time` are customization options
/// Create Connections
var db = createConnection()
var contact_db = createConnection(connection_string: Const.contacts_address)
if db == nil || contact_db == nil { return [String:[[String:String]]]() }
/// Replacing percentages with escaped so that they don't act as wildcard characters
var upperTerm = term.replacingOccurrences(of: "%", with: "\\%")
/// replace spaces with wildcard characters if bridge_gaps == true
if bridge_gaps { upperTerm = upperTerm.split(separator: " ").joined(separator: "%") }
var return_texts = [String:[[String:Any]]]()
var texts: [[String:Any]] = selectFromSql(db: db, columns: ["c.chat_identifier", "c.display_name", "m.ROWID", "m.text", "m.service", "m.date", "m.cache_has_attachments"], table: "message m", condition: "inner join chat_message_join j on j.message_id = m.ROWID inner join chat c on j.chat_id = c.ROWID WHERE text like ? order by m.date desc", args: ["%\(upperTerm)%"], split_ids: true)
/// If case sensitive, remove those who don't exactly match. sqlite select is hardcoded case insensitive
if case_sensitive {
texts.removeAll { !(($0["text"] as? String)?.contains(term) ?? false) }
}
parseTexts(&texts, db: db, contact_db: contact_db)
if !group_by_time {
for i in texts {
/// get sender for this text
guard let chat = i["chat_identifier"] as? String else { continue }
/// Add this text onto the list of texts from this person that match term if grouping by person and not time
if return_texts[chat] == nil {
return_texts[chat] = [i]
} else {
return_texts[chat]?.append(i)
}
}
} else {
return_texts["texts"] = texts
}
return_texts["conversations"] = matchPartialAddressWithDB(term, db: contact_db)
/// close
closeDatabase(&db)
closeDatabase(&contact_db)
return return_texts
}
final func getPhotoList(num: Int = 40, offset: Int = 0, most_recent: Bool = true) -> [[String: Any]] {
/// This gets a list of the `num` (most_recent ? most recent : oldest) photos, offset by `offset`.
#if os(macOS)
guard #available(OSX 10.15, *) else { return [[String:Any]]() }
#endif
Const.log("Getting list of photos, num: \(num), offset: \(offset), most recent: \(most_recent ? "true" : "false")")
/// make sure that we have access to the photos library
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
var con = true;
PHPhotoLibrary.requestAuthorization({ auth in
if auth != PHAuthorizationStatus.authorized {
con = false
Const.log("App is not authorized to view photos. Please grant access.", warning: true)
}
})
guard con else { return [[String:Any]]() }
}
var ret_val = [[String:Any]].init(repeating: [String:Any](), count: num)
let fetchOptions = PHFetchOptions()
/// sort photos by most recent
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: !most_recent)]
fetchOptions.fetchLimit = num + offset
/// get images!
let result = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
let total = result.countOfAssets(with: PHAssetMediaType.image)
let group = DispatchGroup()
/// Here we iterate through the images and get their url
for i in offset..<total {
/// We have to use a dispatch group here to get all the image urls in order, since each uses an asynchronous callback to get them.
group.enter()
var local_result = [String:Any]()
let image = result.object(at: i)
/// Set up the options to grab the Image
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = { (adjustmeta: PHAdjustmentData) -> Bool in
return true
}
/// Get the url here in this callback
image.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
let url = contentEditingInput?.fullSizeImageURL as URL?
/// Insert all the values into the return
local_result["URL"] = url?.path.replacingOccurrences(of: Const.photo_address_prefix, with: "") ?? ""
local_result["is_favorite"] = image.isFavorite
/// Since we initialized `ret_val` to a set number of spaces, we can insert these at their correct index,
/// instead of just appending them (and thus messing up the order)
ret_val[i - offset] = local_result
/// Leave the dispatch grop that we entered at the beginning of this iteration
group.leave()
})
}
/// Wait until there is nobody left in the group. This will occur once every image has been iterated through, and the url has been grabbed.
group.wait()
return ret_val
}
final func getPhotoDatafromPath(path: String) -> Data {
/// This returns the pure data of a photo at `path`
Const.log("Getting photo data from path \(path)")
/// To prevent LFI
let parsed_path = path.replacingOccurrences(of: "\\/", with: "/").replacingOccurrences(of: "../", with: "")
let photo_data = SMImage(Const.photo_address_prefix + parsed_path).parseableData()
return photo_data ?? Data()
}
final func getLinkInfo(_ mid: String, db: OpaquePointer?) -> [String:String] {
/// This get a Rich Link's title text from the sql database. Was quite the pain to get working.
/// I'll expland it in the future to get like the subtitle and other stuff too
let sqlString = "SELECT payload_data from message where ROWID is \(mid);"
var statement: OpaquePointer?
var ret_dict = ["title": "Title", "subtitle": "Subtitle", "type": "Website"]
Const.log("Opened statement in getLinkTitle")
/// Prepare sql statement
var ret_code = sqlite3_prepare_v2(db, sqlString, -1, &statement, nil)
if ret_code != SQLITE_OK {
Const.log("WARNING: error preparing select: \(ret_code)", warning: true)
}
var data: NSData = NSData.init()
/// Get blob data
if sqlite3_step(statement) == SQLITE_ROW {
if let tiny_return_blob = sqlite3_column_blob(statement, 0) {
let len: Int32 = sqlite3_column_bytes(statement, 0)
data = NSData(bytes: tiny_return_blob, length: Int(len))
}
}
/// finalize sqlite statement
ret_code = sqlite3_finalize(statement)
if ret_code != SQLITE_OK {