-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMessage.m
777 lines (642 loc) · 28.9 KB
/
Message.m
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
//
// Message.m
// ringring.io
//
// Created by Peter Kosztolanyi on 08/02/2014.
//
//
#import "Message.h"
#import "Contact.h"
#import "AddressBookMap.h"
#import "LinphoneHelper.h"
@implementation Message
@synthesize chatId;
@synthesize refChatId;
@synthesize messageDirection;
@synthesize messageType;
@synthesize text;
@synthesize sipMessage;
@synthesize receivedDate;
@synthesize openedDate;
@synthesize messageState;
@synthesize expiryTime;
@synthesize indexPath;
// Generic workflow:
// -----------------
// 1) Alice sends a new message - "TYPE:TXT;CHAT_ID:123;EXPIRY:600;MESSAGE:Hello Bob"
// 2) Bob receives the message and saves it into his local
// database
// 3) Bob sends an ack - "TYPE:ACK;CHAT_ID:123;EXPIRY:0;MESSAGE:md5sum(Hello Bob)"
// 4) Alice receives the ack and updates her message status
// to Received if the received MD5 sum is correct
// 5) Bob opens the message and sends a read confirmation - "TYPE:CON;CHAT_ID:123;EXPIRY:0;MESSAGE:md5sum(Hello Bon)"
// 6) Alice receives the conf and updates her message
// status to Read if the received MD5 sum is correct
- (id)initWithContact:(Contact *)aContact
withMessageDirection:(MessageDirection)aMessageDirection
withMessageType:(MessageType)aMessageType
withChatID:(NSNumber *)aChatID
withRefChatId:(NSNumber *)aRefChatId
withExpiryTime:(NSNumber *)anExpiryTime
withText:(NSString *)aText
{
self = [super initWithContact:aContact];
if (self != nil) {
self.messageDirection = aMessageDirection;
self.messageType = aMessageType;
self.chatId = aChatID;
self.refChatId = aRefChatId;
self.expiryTime = anExpiryTime;
self.text = aText;
self.receivedDate = [NSDate date];
self.openedDate = nil;
self.messageState = MessageStateIdle;
[NSNumber numberWithInteger:0];
[self refreshSipMessage];
}
return self;
}
- (id)initWithEmail:(NSString *)anEmail
withMessageDirection:(MessageDirection)aMessageDirection
withMessageType:(MessageType)aMessageType
withChatId:(NSNumber *)aChatID
withRefChatId:(NSNumber *)aRefChatId
withExpiryTime:(NSNumber *)anExpiryTime
withText:(NSString *)aText
{
Contact *contact = nil;
// Try to get contact from the addressbook by email
contact = [AddressBookMap getContactWithEmail:anEmail];
// No contact found; create a default one
if (contact == nil) {
contact = [[Contact alloc] initWithDefault:anEmail];
}
self = [self initWithContact:contact
withMessageDirection:aMessageDirection
withMessageType:aMessageType
withChatID:aChatID
withRefChatId:aRefChatId
withExpiryTime:anExpiryTime
withText:aText];
return self;
}
- (id)initWithContact:(Contact *)aContact
withMessageDirection:(MessageDirection)aMessageDirection
withSipMessage:(NSString *)aSipMessage
{
self = [self initWithContact:aContact
withMessageDirection:aMessageDirection
withMessageType:UnknownMessage
withChatID:nil
withRefChatId:nil
withExpiryTime:nil
withText:nil];
// Extract message properties from sipMessage
if (self != nil && [Message isValidSipMessage:aSipMessage]) {
// Sample sipMessage: "TYPE:TXT;CHAT_ID:11;REF_CHAT_ID:123;EXPIRY_TIME;MESSAGE:Hello Bob"
NSArray *sipTextComponents = [aSipMessage componentsSeparatedByString:@";"];
NSString *sipTextComponent;
NSRange range;
// Extract Message Type
sipTextComponent = [sipTextComponents objectAtIndex:0];
range = [sipTextComponent rangeOfString:@":"];
NSString *sMessageType = [sipTextComponent substringFromIndex:range.location + 1];
if ([sMessageType isEqualToString:@"TXT"]) {
self.messageType = TextMessage;
}
else if([sMessageType isEqualToString:@"ACK"]) {
self.messageType = AckMessage;
}
else if([sMessageType isEqualToString:@"OPN"]) {
self.messageType = OpenedMessage;
}
else {
self.messageType = UnknownMessage;
}
// Extract Chat Id
sipTextComponent = [sipTextComponents objectAtIndex:1];
range = [sipTextComponent rangeOfString:@":"];
NSString *sChatId = [[sipTextComponents objectAtIndex:1] substringFromIndex:range.location + 1];
NSScanner *scannerChatId = [NSScanner scannerWithString:sChatId];
int dChatId;
if ([scannerChatId scanInt:&dChatId] && scannerChatId.scanLocation == sChatId.length) {
self.chatId = [NSNumber numberWithInt:dChatId];
}
// Extract Ref Chat Id
sipTextComponent = [sipTextComponents objectAtIndex:2];
range = [sipTextComponent rangeOfString:@":"];
NSString *sRefChatId = [[sipTextComponents objectAtIndex:2] substringFromIndex:range.location + 1];
NSScanner *scannerRefChatId = [NSScanner scannerWithString:sRefChatId];
int dRefChatId;
if ([scannerRefChatId scanInt:&dRefChatId] && scannerRefChatId.scanLocation == sRefChatId.length) {
self.refChatId = [NSNumber numberWithInt:dRefChatId];
}
// Extract Expiry Time
sipTextComponent = [sipTextComponents objectAtIndex:3];
range = [sipTextComponent rangeOfString:@":"];
NSString *sExpiryTime = [[sipTextComponents objectAtIndex:3] substringFromIndex:range.location + 1];
NSScanner *scannerExpiryTime = [NSScanner scannerWithString:sExpiryTime];
int dExpiryTime;
if ([scannerExpiryTime scanInt:&dExpiryTime] && scannerExpiryTime.scanLocation == sExpiryTime.length) {
self.expiryTime = [NSNumber numberWithInt:dExpiryTime];
}
// Extract Text
sipTextComponent = [sipTextComponents objectAtIndex:4];
range = [sipTextComponent rangeOfString:@":"];
NSString *sText = [sipTextComponent substringFromIndex:range.location + 1];
self.text = sText;
[self refreshSipMessage];
}
return self;
}
- (id)initWithEmail:(NSString *)anEmail
withMessageDirection:(MessageDirection)aMessageDirection
withSipMessage:(NSString *)aSipMessage
{
Contact *contact = nil;
// Try to get contact from the addressbook by email
contact = [AddressBookMap getContactWithEmail:anEmail];
// No contact found; create a default one
if (contact == nil) {
contact = [[Contact alloc] initWithDefault:anEmail];
}
self = [self initWithContact:contact
withMessageDirection:aMessageDirection
withSipMessage:aSipMessage];
return self;
}
- (id)initWithSqlStatement:(sqlite3_stmt *)sqlStatement
{
self = [self initWithEmail:[NSString stringWithUTF8String: (const char*) sqlite3_column_text(sqlStatement, 2)]
withMessageDirection:[[NSNumber numberWithInt:sqlite3_column_int(sqlStatement, 3)] intValue]
withMessageType:[[NSNumber numberWithInt:sqlite3_column_int(sqlStatement, 4)] intValue]
withChatId:[NSNumber numberWithInt:sqlite3_column_int(sqlStatement, 0)]
withRefChatId:[NSNumber numberWithInt:sqlite3_column_int(sqlStatement, 1)]
withExpiryTime:[NSNumber numberWithInt:sqlite3_column_int(sqlStatement, 9)]
withText:[NSString stringWithUTF8String: (const char*) sqlite3_column_text(sqlStatement, 5)]];
if (self != nil) {
self.receivedDate = [NSDate dateWithTimeIntervalSince1970:sqlite3_column_int(sqlStatement, 6)];
self.openedDate = [NSDate dateWithTimeIntervalSince1970:sqlite3_column_int(sqlStatement, 7)];
self.messageState = [[NSNumber numberWithInt:sqlite3_column_int(sqlStatement, 8)] intValue];
[self refreshSipMessage];
}
if (!self.openedDate || ![self.openedDate compare:[NSDate dateWithTimeIntervalSince1970:0]]) {
self.hasUnreadMessages = YES;
}
return self;
}
- (void)save
{
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return;
}
const char *sql = "INSERT INTO chat (ref_id, contact_email, direction, type, text, received_date, opened_date, state, expiry_time) VALUES (@REF_ID, @CONTACT_EMAIL, @DIRECTION, @TYPE, @TEXT, @RECEIVED_DATE, @OPENED_DATE, @STATE, @EXPIRY_TIME)";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)];
return;
}
// Prepare statement
sqlite3_bind_int(sqlStatement, 1, [self.refChatId intValue]);
sqlite3_bind_text(sqlStatement, 2, [self.email UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_int(sqlStatement, 3, self.messageDirection);
sqlite3_bind_int(sqlStatement, 4, self.messageType);
sqlite3_bind_text(sqlStatement, 5, [self.text UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_double(sqlStatement, 6, [self.receivedDate timeIntervalSince1970]);
sqlite3_bind_double(sqlStatement, 7, [self.openedDate timeIntervalSince1970]);
sqlite3_bind_int(sqlStatement, 8, self.messageState);
sqlite3_bind_int(sqlStatement, 9, [self.expiryTime intValue]);
if (sqlite3_step(sqlStatement) != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
sqlite3_finalize(sqlStatement);
}
self.chatId = [[NSNumber alloc] initWithLong:(long)sqlite3_last_insert_rowid(database)];
sqlite3_finalize(sqlStatement);
[self refreshSipMessage];
}
- (void)update
{
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return;
}
const char *sql = "UPDATE chat SET ref_id=@REF_ID, contact_email=@CONTACT_EMAIL, direction=@DIRECTION, type=@TYPE, text=@TEXT, received_date=@RECEIVED_DATE, opened_date=@OPENED_DATE, state=@STATE, expiry_time=@EXPIRY_TIME WHERE id=@ID";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)];
return;
}
// Prepare statement
sqlite3_bind_int(sqlStatement, 1, [self.refChatId intValue]);
sqlite3_bind_text(sqlStatement, 2, [self.email UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_int(sqlStatement, 3, self.messageDirection);
sqlite3_bind_int(sqlStatement, 4, self.messageType);
sqlite3_bind_text(sqlStatement, 5, [self.text UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_double(sqlStatement, 6, [self.receivedDate timeIntervalSince1970]);
sqlite3_bind_double(sqlStatement, 7, [self.openedDate timeIntervalSince1970]);
sqlite3_bind_int(sqlStatement, 8, self.messageState);
sqlite3_bind_int(sqlStatement, 9, [self.expiryTime intValue]);
sqlite3_bind_int(sqlStatement, 10, [self.chatId intValue]);
if (sqlite3_step(sqlStatement) != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
sqlite3_finalize(sqlStatement);
return;
}
sqlite3_finalize(sqlStatement);
}
- (void)delete
{
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return;
}
const char *sql = "DELETE FROM chat WHERE id=@ID";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)];
return;
}
// Prepare statement
sqlite3_bind_int(sqlStatement, 1, [chatId intValue]);
if (sqlite3_step(sqlStatement) != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
sqlite3_finalize(sqlStatement);
return;
}
sqlite3_finalize(sqlStatement);
}
+ (bool)isValidSipMessage:(NSString *)aSipMessage
{
NSString *pattern = @"TYPE:.+;CHAT_ID:.+;REF_CHAT_ID:.+;EXPIRY_TIME:[0-9]+;MESSAGE:.*";
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
return [myTest evaluateWithObject: aSipMessage];
}
+ (NSMutableArray *)listMessageLog
{
NSMutableArray *messages = [NSMutableArray array];
// Connect to database
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return messages;
}
// Select the latest message from every email address
const char *sql = "SELECT id, ref_id, contact_email, direction, type, text, received_date, opened_date, state, expiry_time FROM chat WHERE id IN (SELECT MAX(id) FROM chat GROUP BY contact_email) ORDER BY received_date ASC";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't execute the query: %s (%s)", sql, sqlite3_errmsg(database)];
return messages;
}
int err;
while ((err = sqlite3_step(sqlStatement)) == SQLITE_ROW) {
Message *message = [[Message alloc] initWithSqlStatement:sqlStatement];
[messages addObject:message];
}
if (err != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
return messages;
}
sqlite3_finalize(sqlStatement);
return messages;
}
+ (NSMutableArray *)listMessages:(NSString *)email
{
NSMutableArray *messages = [NSMutableArray array];
// Connect to database
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return messages;
}
// Select every message from the remote email address
const char *sql = "SELECT id, ref_id, contact_email, direction, type, text, received_date, opened_date, state, expiry_time FROM chat WHERE contact_email=@EMAIL ORDER BY received_date ASC";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't execute the query: %s (%s)", sql, sqlite3_errmsg(database)];
return messages;
}
// Prepare statement
sqlite3_bind_text(sqlStatement, 1, [email UTF8String], -1, SQLITE_STATIC);
int err;
while ((err = sqlite3_step(sqlStatement)) == SQLITE_ROW) {
Message *message = [[Message alloc] initWithSqlStatement:sqlStatement];
// Send opened message if not yet sent
if (message.messageDirection == IncomingMessage && message.messageState == MessageStateIdle) {
// Prepare OPN message with MD5 checksum of the incoming message
Message *opnMessage = [[Message alloc] initWithEmail:message.email
withMessageDirection:OutgoingMessage
withMessageType:OpenedMessage
withChatId:message.chatId
withRefChatId:message.refChatId
withExpiryTime:message.expiryTime
withText:[LinphoneHelper MD5String:message.text]];
// Save onscreen message status to Opened
message.messageState = MessageStateOpened;
message.openedDate = [NSDate date];
[message update];
// Send OPN
LinphoneChatRoom *chatRoom = linphone_core_create_chat_room([LinphoneManager getLc], [[LinphoneHelper emailToSipUser:email] UTF8String]);
LinphoneChatMessage *conMsg = linphone_chat_room_create_message(chatRoom, [opnMessage.sipMessage UTF8String]);
linphone_chat_room_send_message2(chatRoom, conMsg, nil, (__bridge void *)(self));
}
[messages addObject:message];
}
if (err != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
return messages;
}
sqlite3_finalize(sqlStatement);
return messages;
}
+ (Message *)getMessage:(NSNumber *)chatId
{
Message *message = nil;
// Connect to database
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return message;
}
// Select every message from the remote email address
const char *sql = "SELECT id, ref_id, contact_email, direction, type, text, received_date, opened_date, state, expiry_time FROM chat WHERE id = @CHAT_ID";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't execute the query: %s (%s)", sql, sqlite3_errmsg(database)];
return message;
}
// Prepare statement
sqlite3_bind_int(sqlStatement, 1, [chatId intValue]);
int err;
while ((err = sqlite3_step(sqlStatement)) == SQLITE_ROW) {
message = [[Message alloc] initWithSqlStatement:sqlStatement];
}
if (err != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
return message;
}
sqlite3_finalize(sqlStatement);
return message;
}
+ (NSNumber *)getUnreadMessages
{
NSNumber *unreadMessages = [[NSNumber alloc] initWithInt:0];
// Connect to database
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return unreadMessages;
}
// Select every message from the remote email address
const char *sql = "SELECT COUNT(*) FROM chat WHERE direction = @DIRECTION AND opened_date = 0";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't execute the query: %s (%s)", sql, sqlite3_errmsg(database)];
return unreadMessages;
}
// Prepare statement
sqlite3_bind_int(sqlStatement, 1, (int)IncomingMessage);
int err;
while ((err = sqlite3_step(sqlStatement)) == SQLITE_ROW) {
unreadMessages = [NSNumber numberWithInt:sqlite3_column_int(sqlStatement, 0)];
}
if (err != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
return unreadMessages;
}
sqlite3_finalize(sqlStatement);
return unreadMessages;
}
+ (void)markAllAsRead:(NSString *)email
{
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return;
}
const char *sql = "UPDATE chat SET opened_date = @OPENED_DATE WHERE opened_date = 0";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)];
return;
}
// Prepare statement
sqlite3_bind_double(sqlStatement, 1, [[NSDate date] timeIntervalSince1970]);
// sqlite3_bind_text(sqlStatement, 2, [email UTF8String], -1, SQLITE_STATIC);
if (sqlite3_step(sqlStatement) != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
sqlite3_finalize(sqlStatement);
return;
}
sqlite3_finalize(sqlStatement);
}
+ (void)deleteMessagesBeforeDate:(NSDate *)date
{
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return;
}
const char *sql = "DELETE FROM chat WHERE opened_date != 0 AND opened_date <= @DATE";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)];
return;
}
// Prepare statement
sqlite3_bind_double(sqlStatement, 1, [date timeIntervalSince1970]);
if (sqlite3_step(sqlStatement) != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
sqlite3_finalize(sqlStatement);
return;
}
sqlite3_finalize(sqlStatement);
}
+ (void)deleteMessagesWithContactEmail:(NSString *)contactEmail
{
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return;
}
const char *sql = "DELETE FROM chat WHERE contact_email = @EMAIL";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)];
return;
}
// Prepare statement
sqlite3_bind_text(sqlStatement, 1, [contactEmail UTF8String], -1, SQLITE_STATIC);
if (sqlite3_step(sqlStatement) != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
sqlite3_finalize(sqlStatement);
return;
}
sqlite3_finalize(sqlStatement);
}
+ (void)updateExpiredMessages
{
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return;
}
const char *sql = "UPDATE chat SET state = @STATE WHERE expiry_time != 0 AND opened_date != 0 AND opened_date + expiry_time <= @NOW";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)];
return;
}
// Prepare statement
sqlite3_bind_int(sqlStatement, 1, (int)MessageStateExpired);
sqlite3_bind_double(sqlStatement, 2, [[NSDate date] timeIntervalSince1970]);
if (sqlite3_step(sqlStatement) != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
sqlite3_finalize(sqlStatement);
return;
}
sqlite3_finalize(sqlStatement);
}
+ (void)deleteExpiredMessages
{
sqlite3* database = [[LinphoneManager instance] database];
if(database == NULL) {
[LinphoneHelper logc:LinphoneLoggerError format:"Database not ready"];
return;
}
const char *sql = "DELETE FROM chat WHERE state = @STATE";
sqlite3_stmt *sqlStatement;
if (sqlite3_prepare_v2(database, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
[LinphoneHelper logc:LinphoneLoggerError format:"Can't prepare the query: %s (%s)", sql, sqlite3_errmsg(database)];
return;
}
// Prepare statement
sqlite3_bind_int(sqlStatement, 1, (int)MessageStateExpired);
if (sqlite3_step(sqlStatement) != SQLITE_DONE) {
[LinphoneHelper logc:LinphoneLoggerError format:"Error during execution of query: %s (%s)", sql, sqlite3_errmsg(database)];
sqlite3_finalize(sqlStatement);
return;
}
sqlite3_finalize(sqlStatement);
}
- (void)refreshSipMessage
{
NSMutableString *sipMessageGen = [NSMutableString string];
[sipMessageGen appendFormat:@"TYPE:%@;", [Message messageTypeToString:self.messageType]];
[sipMessageGen appendFormat:@"CHAT_ID:%@;", [self chatId]];
[sipMessageGen appendFormat:@"REF_CHAT_ID:%@;", [self refChatId]];
[sipMessageGen appendFormat:@"EXPIRY_TIME:%@;", [self expiryTime]];
[sipMessageGen appendFormat:@"MESSAGE:%@", [self text]];
self.sipMessage = sipMessageGen;
}
+ (NSString *)messageDirectionToString:(MessageDirection)messageDirection
{
NSString *messageDirectionString;
switch ((MessageDirection)messageDirection) {
case NoMessage:
messageDirectionString = @"NoMessage";
break;
case IncomingMessage:
messageDirectionString = @"Incoming";
break;
case OutgoingMessage:
messageDirectionString = @"Outgoing";
break;
}
return messageDirectionString;
}
+ (NSString *)messageTypeToString:(MessageType)messageType
{
NSString *messageTypeToString;
switch ((MessageType)messageType) {
case UnknownMessage:
messageTypeToString = @"UNK";
break;
case AckMessage:
messageTypeToString = @"ACK";
break;
case TextMessage:
messageTypeToString = @"TXT";
break;
case OpenedMessage:
messageTypeToString = @"OPN";
break;
}
return messageTypeToString;
}
+ (NSString *)messageStateToString:(MessageState)messageState
{
NSString *messageStateToString;
switch ((MessageState)messageState) {
case MessageStateIdle:
messageStateToString = NSLocalizedString(@"Idle", nil);
break;
case MessageStateInProgress:
messageStateToString = NSLocalizedString(@"In Progress", nil);
break;
case MessageStateDelivered:
messageStateToString = NSLocalizedString(@"Delivered", nil);
break;
case MessageStateNotDelivered:
messageStateToString = NSLocalizedString(@"Not Delivered", nil);
break;
case MessageStateReceived:
messageStateToString = NSLocalizedString(@"Received", nil);
break;
case MessageStateNotReceived:
messageStateToString = NSLocalizedString(@"No feedback", nil);
break;
case MessageStateWaitingtoOpen:
messageStateToString = NSLocalizedString(@"Waiting to Open", nil);
break;
case MessageStateOpened:
messageStateToString = NSLocalizedString(@"Seen", nil);
break;
case MessageStateExpired:
messageStateToString = NSLocalizedString(@"Expired", nil);
break;
}
return messageStateToString;
}
- (NSString *)description {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
return [NSString stringWithFormat:@"chatId : %@\n\
refChatId : %@\n\
email : [%@]\n\
firstName : [%@]\n\
lastName : [%@]\n\
fullName : [%@]\n\
image : %@\n\
hasUnreadMessages: %@\n\
messageDirection : %@\n\
messageType : %@\n\
text : [%@]\n\
sipMessage : [%@]\n\
receivedDate : %@\n\
openedDate : %@\n\
state : %@\n\
expiryTime : %@\n\
indexPath : %@",
[self chatId],
[self refChatId],
[self email],
[self firstName],
[self lastName],
[self fullName],
[self image]?@"Found":@"Not found",
[self hasUnreadMessages]?@"YES":@"NO",
[Message messageDirectionToString:self.messageDirection],
[Message messageTypeToString:self.messageType],
[self text],
[self sipMessage],
[formatter stringFromDate:[self receivedDate]],
[formatter stringFromDate:[self openedDate]],
[Message messageStateToString:self.messageState],
[self expiryTime],
[[self indexPath] description]];
}
@end