forked from dbmail/dbmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm_mailboxstate.c
1459 lines (1214 loc) · 35.1 KB
/
dm_mailboxstate.c
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
/*
Copyright (c) 2004-2012 NFG Net Facilities Group BV support@nfg.nl
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string.h>
#include "dbmail.h"
#include "dm_mailboxstate.h"
#include "dm_db.h"
#define THIS_MODULE "MailboxState"
/*
*/
extern DBParam_T db_params;
extern Mempool_T small_pool;
extern const char *imap_flag_desc_escaped[];
#define DBPFX db_params.pfx
#define T MailboxState_T
static void db_getmailbox_seq(T M, Connection_T c);
static void db_getmailbox_permission(T M, Connection_T c);
static void state_load_metadata(T M, Connection_T c);
static void MailboxState_setMsginfo(T M, GTree *msginfo);
/* */
static void MailboxState_uid_msn_new(T M)
{
if (M->msn) g_tree_destroy(M->msn);
M->msn = g_tree_new_full((GCompareDataFunc)ucmpdata,NULL,NULL,NULL);
if (M->ids) g_tree_destroy(M->ids);
M->ids = g_tree_new_full((GCompareDataFunc)ucmpdata,NULL,NULL,(GDestroyNotify)g_free);
}
static void MessageInfo_free(MessageInfo *m)
{
g_list_destroy(m->keywords);
g_free(m);
}
static T state_load_messages(T M, Connection_T c, gboolean coldLoad)
{
unsigned nrows = 0, i = 0, j;
struct timeval before, after;
const char *query_result;
uint64_t tempId;
MessageInfo *result;
GTree *msginfo;
uint64_t *uid, id = 0;
ResultSet_T r;
PreparedStatement_T stmt;
Field_T frag;
INIT_QUERY;
int idsAdded = 0;
char filterCondition[96]; memset(filterCondition,0,96);
int mailbox_sync_deleted = config_get_value_default_int("mailbox_sync_deleted", "IMAP", 1);
int mailbox_sync_batch_size= config_get_value_default_int("mailbox_sync_batch_size", "IMAP", 64);
/* the initialization should be done elsewhere, see ols MailboxState_new and MailboxState_update */
msginfo=MailboxState_getMsginfo(M);
uint64_t seq=MailboxState_getSeq(M);
if (coldLoad){
//msginfo = g_tree_new_full((GCompareDataFunc)ucmpdata, NULL,(GDestroyNotify)g_free,(GDestroyNotify)MessageInfo_free);
TRACE(TRACE_DEBUG, "SEQ Cold Load [ %" PRIu64 " ]", seq);
snprintf(filterCondition,64-1,"/*SEQ New*/ AND m.status < %d ", MESSAGE_STATUS_DELETE);
}else{
uint64_t state_seq=M->state_seq;
//msginfo=MailboxState_getMsginfo(M);
TRACE(TRACE_DEBUG, "SEQ RENEW [ %" PRIu64 " %" PRIu64 " ]",state_seq , seq);
//MailboxState_uid_msn_new(M);
/* use seq to select only changed elements, event this which are deleted*/
snprintf(filterCondition,64-1,"/*SEQ ReNew*/ AND m.seq >= %" PRIu64 "-1 AND m.status <= %d ", state_seq, MESSAGE_STATUS_DELETE );
}
date2char_str("internal_date", &frag);
snprintf(query, DEF_QUERYSIZE-1,
"SELECT seen_flag, answered_flag, deleted_flag, flagged_flag, "
"draft_flag, recent_flag, %s, rfcsize, seq, m.message_idnr, status, m.physmessage_id "
"FROM %smessages m "
"LEFT JOIN %sphysmessage p ON p.id = m.physmessage_id "
"WHERE m.mailbox_idnr = ? %s "
"ORDER BY m.seq DESC",
frag,
DBPFX, DBPFX,
filterCondition);
stmt = db_stmt_prepare(c, query);
db_stmt_set_u64(stmt, 1, M->id);
gettimeofday(&before, NULL);
r = db_stmt_query(stmt);
gettimeofday(&after, NULL);
log_query_time(query,before,after);
i = 0;
gettimeofday(&before, NULL);
while (db_result_next(r)) {
i++;
id = db_result_get_u64(r, IMAP_NFLAGS + 3);
/* reset */
idsAdded=0;
if (coldLoad){
/* new element*/
result = g_new0(MessageInfo,1);
uid = g_new0(uint64_t,1);
*uid = id;
idsAdded=1;
result->expunge=0;
result->expunged=0;
//TRACE(TRACE_DEBUG, "SEQ CREATED %ld",id);
}else{
/* soft renew, so search */
result = g_tree_lookup(msginfo, &id);
if (result == NULL){
/* check deletion */
if (db_result_get_int(r, IMAP_NFLAGS + 4)>=MESSAGE_STATUS_DELETE){
// item is already deleted and is not in tree, so do not even add it.
continue;
}
/* not found so create*/
result = g_new0(MessageInfo,1);
uid = g_new0(uint64_t,1);
*uid = id;
idsAdded=1;
result->expunge=0;
result->expunged=0;
}else{
/* initialize uid, result is not null */
uid = g_new0(uint64_t,1);
*uid = id;
//TRACE(TRACE_DEBUG, "SEQ FOUND %ld",id);
}
}
/* id */
result->uid = id;
/* mailbox_id */
result->mailbox_id = M->id;
/* flags */
for (j = 0; j < IMAP_NFLAGS; j++)
result->flags[j] = db_result_get_bool(r,j);
/* internal date */
query_result = db_result_get(r,IMAP_NFLAGS);
strncpy(result->internaldate,
(query_result) ? query_result :
"01-Jan-1970 00:00:01 +0100",
IMAP_INTERNALDATE_LEN-1);
/* rfcsize */
result->rfcsize = db_result_get_u64(r,IMAP_NFLAGS + 1);
/* modseq */
result->seq = db_result_get_u64(r,IMAP_NFLAGS + 2);
/* status */
result->status = db_result_get_int(r, IMAP_NFLAGS + 4);
/* physmessage_id */
result->phys_id = db_result_get_int(r, IMAP_NFLAGS + 5);
if (result->flags[IMAP_FLAG_DELETED]==1 && result->status < MESSAGE_STATUS_DELETE){
TRACE(TRACE_DEBUG, "DESYNC Meessage marked as deleted but not deleted [ %" PRIu64 " ] consider using `mailbox_sync_deleted`", *uid);
if (mailbox_sync_deleted==2 && mailbox_sync_batch_size>0){
db_set_message_status(id,MESSAGE_STATUS_DELETE);
result->status=MESSAGE_STATUS_DELETE;
mailbox_sync_batch_size--;
TRACE(TRACE_DEBUG, "DESYNC marked as deleted[ %" PRIu64 " ]", *uid);
}
}
if (result->status >= MESSAGE_STATUS_DELETE || result->flags[IMAP_FLAG_DELETED]==1 || result->expunged==1 || result->expunge>=1){
result->expunge ++;
if (result->expunged == 1){
//TRACE(TRACE_DEBUG, "SEQ Remove MSG EXPUNGED [ %" PRIu64 " ]", *uid);
/* result does not need to be freed, it is freed in tree remove */
g_tree_remove(msginfo, uid);
g_free(uid);
continue;
}else{
//TRACE(TRACE_DEBUG, "SEQ Remove MSG EXPUNGING [ %" PRIu64 " expunge flag %d, was expunged %d]", *uid, result->expunge, result->expunged);
}
}
if (idsAdded==1){
//TRACE(TRACE_DEBUG, "SEQ ADDED %ld",id);
/* it's new */
g_tree_insert(msginfo, uid, result);
}else{
/* no need, result was updated */
/* free uid, is it always allocated */
g_free(uid);
}
}
gettimeofday(&after, NULL);
log_query_time("Parsing State ",before,after);
if (! i) { // empty mailbox
/* update the state seq */
M->state_seq = seq;
MailboxState_setMsginfo(M, msginfo);
return M;
}
db_con_clear(c);
memset(query, 0, sizeof(query));
snprintf(query, DEF_QUERYSIZE-1,
"SELECT k.message_idnr, k.keyword FROM %skeywords k "
"LEFT JOIN %smessages m ON k.message_idnr=m.message_idnr "
"WHERE m.mailbox_idnr = ? %s "
"order by m.message_idnr "
,
DBPFX, DBPFX,
filterCondition);
nrows = 0;
stmt = db_stmt_prepare(c, query);
db_stmt_set_u64(stmt, 1, M->id);
r = db_stmt_query(stmt);
gettimeofday(&before, NULL);
tempId=0;
while (db_result_next(r)) {
nrows++;
id = db_result_get_u64(r,0);
const char * keyword = db_result_get(r,1);
if (strlen(keyword)>0){
// TRACE(TRACE_INFO, "Keyword line [%d %s]", nrows, keyword);
/* use tempId a temporary store the id of the item in order to avoid unnecessary lookups */
if ( tempId!=id || tempId==0 ){
result = g_tree_lookup(msginfo, &id);
tempId=id;
}
if ( result != NULL ){
result->keywords = g_list_append(result->keywords, g_strdup(keyword));
/*
GList *kL = g_tree_keys(M->keywords);
GString *kStr = g_list_join(kL," ");
TRACE(TRACE_INFO, "MSG Keyworkds [%s]",kStr->str);
g_string_free(kStr,TRUE);
g_list_free(g_list_first(kL));
*/
}
}
}
db_con_clear(c);
TRACE(TRACE_DEBUG, "SEQ Keywords [ %" PRIu64 " ]", nrows);
if (! nrows) TRACE(TRACE_DEBUG, "no keywords");
gettimeofday(&after, NULL);
log_query_time("Parsing Keywords ",before,after);
/* update the state seq */
M->state_seq = seq;
TRACE(TRACE_DEBUG, "SEQ STATE [ %" PRIu64 " %" PRIu64 " ]", M->state_seq , seq);
/* on both cases can use the same function due to some checks at MailboxState_setMsgInfo*/
MailboxState_setMsginfo(M, msginfo);
return M;
}
gboolean _compare_data(gconstpointer a, gconstpointer b, gpointer UNUSED data)
{
return strcmp((const char *)a,(const char *)b);
}
T MailboxState_new(Mempool_T pool, uint64_t id)
{
T M; Connection_T c;
volatile int t = DM_SUCCESS;
gboolean freepool = FALSE;
if (! pool) {
pool = mempool_open();
freepool = TRUE;
}
M = mempool_pop(pool, sizeof(*M));
M->pool = pool;
M->freepool = freepool;
if (! id) return M;
M->id = id;
M->recent_queue = g_tree_new((GCompareFunc)ucmp);
M->keywords = g_tree_new_full((GCompareDataFunc)_compare_data,NULL,g_free,NULL);
M->msginfo = g_tree_new_full((GCompareDataFunc)ucmpdata, NULL,(GDestroyNotify)g_free,(GDestroyNotify)MessageInfo_free);
M->differential_iterations = 0;
c = db_con_get();
TRY
db_begin_transaction(c); // we need read-committed isolation
state_load_metadata(M, c);
state_load_messages(M, c, true);
db_commit_transaction(c);
CATCH(SQLException)
LOG_SQLERROR;
db_rollback_transaction(c);
t = DM_EQUERY;
FINALLY
db_con_close(c);
END_TRY;
if (t == DM_EQUERY) {
TRACE(TRACE_ERR, "Error opening mailbox");
MailboxState_free(&M);
}
return M;
}
/**
* Update only the mailbox.
* @param M
* @return
*/
T MailboxState_update(Mempool_T pool, T OldM)
{
T M; Connection_T c;
volatile int t = DM_SUCCESS;
gboolean freepool = FALSE;
uint64_t id;
/* differential mode, evaluate max iterations */
int mailbox_diffential_max_iterations = config_get_value_default_int("mailbox_update_strategy_2_max_iterations", "IMAP", 300);
if (mailbox_diffential_max_iterations > 0 && OldM->differential_iterations >= mailbox_diffential_max_iterations-1 ){
TRACE(TRACE_DEBUG, "Strategy differential mode override due to max iterations, see config [IMAP] mailbox_update_strategy_2_max_iterations");
return MailboxState_new(pool, OldM->id);
}
if (! pool) {
pool = mempool_open();
freepool = TRUE;
}
id = OldM->id;
M = mempool_pop(pool, sizeof(*M));
M->pool = pool;
M->freepool = freepool;
if (! id) return M;
M->id = id;
M->recent_queue = g_tree_new((GCompareFunc)ucmp);
M->keywords = g_tree_new_full((GCompareDataFunc)_compare_data,NULL,g_free,NULL);
M->msginfo = g_tree_new_full((GCompareDataFunc)ucmpdata, NULL,(GDestroyNotify)g_free,(GDestroyNotify)MessageInfo_free);
// increase differential iterations in order to apply mailbox_update_strategy_2_max_iterations
M->differential_iterations = OldM->differential_iterations + 1;
// copy state_seq
M->state_seq = OldM->state_seq;
TRACE(TRACE_DEBUG, "Strategy SEQ UPDATE, iterations %d", M->differential_iterations);
//M->ids = g_tree_new_full((GCompareDataFunc)_compare_data,NULL,g_free,NULL);
//M->msn = g_tree_new_full((GCompareDataFunc)_compare_data,NULL,g_free,NULL);
//g_tree_merge(M->recent, OldM->recent, IST_SUBSEARCH_OR);
//g_tree_merge(M->msginfo, OldM->msginfo, IST_SUBSEARCH_OR);
g_tree_copy_MessageInfo(M->msginfo,OldM->msginfo);
//no need, those keywords will be populated at metadata
//@todo change this behaviour in state_load_metadata
//g_tree_copy_String(M->keywords,OldM->keywords);
//MailboxState_remap(M);
/* reset the sequence */
MailboxState_resetSeq(OldM);
//uint64_t seq = MailboxState_getSeq(OldM);
c = db_con_get();
TRY
db_begin_transaction(c); // we need read-committed isolation
state_load_metadata(M, c);
state_load_messages(M, c, false); // do a soft refresh
db_commit_transaction(c);
CATCH(SQLException)
LOG_SQLERROR;
db_rollback_transaction(c);
t = DM_EQUERY;
FINALLY
db_con_close(c);
END_TRY;
if (t == DM_EQUERY) {
TRACE(TRACE_ERR, "SEQ Error opening mailbox");
MailboxState_free(&M);
}
return M;
}
void MailboxState_remap(T M)
{
GList *ids = NULL;
uint64_t *uid, *msn = NULL, rows = 1;
MessageInfo *msginfo;
MailboxState_uid_msn_new(M);
ids = g_tree_keys(M->msginfo);
ids = g_list_first(ids);
while (ids) {
uid = (uint64_t *)ids->data;
msginfo = g_tree_lookup(M->msginfo, uid);
if (msginfo->status < MESSAGE_STATUS_DELETE) {
msn = g_new0(uint64_t,1);
*msn = msginfo->msn = rows++;
g_tree_insert(M->ids, uid, msn);
g_tree_insert(M->msn, msn, uid);
}
if (! g_list_next(ids)) break;
ids = g_list_next(ids);
}
g_list_free(g_list_first(ids));
}
GTree * MailboxState_getMsginfo(T M)
{
return M->msginfo;
}
static void MailboxState_setMsginfo(T M, GTree *msginfo)
{
GTree *oldmsginfo = M->msginfo;
M->msginfo = msginfo;
MailboxState_remap(M);
if (oldmsginfo){
/* might be situations when old msginfo is the same as the new */
if (msginfo != oldmsginfo)
g_tree_destroy(oldmsginfo);
}
}
void MailboxState_addMsginfo(T M, uint64_t uid, MessageInfo *msginfo)
{
uint64_t *id = g_new0(uint64_t,1);
*id = uid;
g_tree_insert(M->msginfo, id, msginfo);
if (msginfo->flags[IMAP_FLAG_RECENT] == 1) {
M->seq--; // force resync
M->recent++;
}
MailboxState_build_recent(M);
MailboxState_remap(M);
}
int MailboxState_removeUid(T M, uint64_t uid)
{
MessageInfo *msginfo = g_tree_lookup(M->msginfo, &uid);
if (! msginfo) {
TRACE(TRACE_WARNING,"trying to remove unknown UID [%" PRIu64 "]", uid);
return DM_EGENERAL;
}
msginfo->status = MESSAGE_STATUS_DELETE;
M->exists--;
MailboxState_remap(M);
return DM_SUCCESS;
}
GTree * MailboxState_getIds(T M)
{
return M->ids;
}
GTree * MailboxState_getMsn(T M)
{
return M->msn;
}
void MailboxState_setId(T M, uint64_t id)
{
M->id = id;
}
uint64_t MailboxState_getId(T M)
{
return M->id;
}
uint64_t MailboxState_getSeq(T M)
{
if (! M->seq) {
Connection_T c = db_con_get();
TRY
db_getmailbox_seq(M, c);
CATCH(SQLException)
LOG_SQLERROR;
FINALLY
db_con_close(c);
END_TRY;
}
return M->seq;
}
/**
* Reset the sequence stored at structure level.
* Subsequent call for MailboxState_getSeq will be required to retrieve the the sequence
* @param M
* @return void
*/
void MailboxState_resetSeq(T M)
{
M->seq=0;
}
/**
* Reset the sequence stored at structure level and store the new seq from db
* Internally it will use MailboxState_getSeq
* @param M
* @return void
*/
uint64_t MailboxState_resyncSeq(T M)
{
M->seq=0;
return MailboxState_getSeq(M);
}
unsigned MailboxState_getExists(T M)
{
int real = g_tree_nnodes(M->ids);
if (real > (int)M->exists) {
TRACE(TRACE_DEBUG, "[%" PRIu64 "] exists [%u] -> [%d]",
M->id, M->exists, real);
M->exists = (unsigned)real;
}
return M->exists;
}
void MailboxState_setExists(T M, unsigned exists)
{
TRACE(TRACE_DEBUG, "[%" PRIu64 "] exists [%u] -> [%u]",
M->id, M->exists, exists);
M->exists = exists;
}
unsigned MailboxState_getRecent(T M)
{
return M->recent;
}
uint64_t MailboxState_getUidnext(T M)
{
return M->uidnext;
}
void MailboxState_setOwner(T M, uint64_t owner_id)
{
M->owner_id = owner_id;
}
uint64_t MailboxState_getOwner(T M)
{
return M->owner_id;
}
void MailboxState_setPermission(T M, int permission)
{
M->permission = permission;
}
unsigned MailboxState_getPermission(T M)
{
if (! M->permission) {
Connection_T c = db_con_get();
TRY
db_getmailbox_permission(M, c);
CATCH(SQLException)
LOG_SQLERROR;
FINALLY
db_con_close(c);
END_TRY;
}
return M->permission;
}
void MailboxState_setName(T M, const char *name)
{
String_T old = M->name;
M->name = p_string_new(M->pool, name);
if (old)
p_string_free(old, TRUE);
}
const char * MailboxState_getName(T M)
{
return p_string_str(M->name);
}
void MailboxState_setIsUsers(T M, gboolean t)
{
M->is_users = t;
}
gboolean MailboxState_isUsers(T M)
{
return M->is_users;
}
void MailboxState_setIsPublic(T M, gboolean t)
{
M->is_public = t;
}
gboolean MailboxState_isPublic(T M)
{
return M->is_public;
}
gboolean MailboxState_hasKeyword(T M, const char *keyword)
{
if (g_tree_lookup(M->keywords, (gpointer)keyword))
return TRUE;
return FALSE;
}
void MailboxState_addKeyword(T M, const char *keyword)
{
char *kw = g_strdup(keyword);
g_tree_insert(M->keywords, kw, kw);
}
void MailboxState_setNoSelect(T M, gboolean no_select)
{
M->no_select = no_select;
}
gboolean MailboxState_noSelect(T M)
{
return M->no_select;
}
void MailboxState_setNoChildren(T M, gboolean no_children)
{
M->no_children = no_children;
}
gboolean MailboxState_noChildren(T M)
{
return M->no_children;
}
gboolean MailboxState_noInferiors(T M)
{
return M->no_inferiors;
}
unsigned MailboxState_getUnseen(T M)
{
return M->unseen;
}
struct filter_range_helper {
gboolean uid;
uint64_t min;
uint64_t max;
GTree *a;
};
static int filter_range(gpointer key, gpointer value, gpointer data)
{
uint64_t *k, *v;
struct filter_range_helper *d = (struct filter_range_helper *)data;
if (*(uint64_t *)key < d->min) return FALSE; // skip
if (*(uint64_t *)key > d->max) return TRUE; // done
k = mempool_pop(small_pool, sizeof(uint64_t));
v = mempool_pop(small_pool, sizeof(uint64_t));
*k = *(uint64_t *)key;
*v = *(uint64_t *)value;
if (d->uid)
g_tree_insert(d->a, k, v);
else
g_tree_insert(d->a, v, k);
return FALSE;
}
static void find_range(GTree *c, uint64_t l, uint64_t r, GTree *a, gboolean uid)
{
struct filter_range_helper data;
data.uid = uid;
data.min = l;
data.max = r;
data.a = a;
g_tree_foreach(c, (GTraverseFunc)filter_range, &data);
}
GTree * MailboxState_get_set(MailboxState_T M, const char *set, gboolean uid)
{
GTree *inset, *a, *b;
GList *sets = NULL;
GString *t;
uint64_t lo = 0, hi = 0;
gboolean error = FALSE;
if (uid)
inset = MailboxState_getIds(M);
else
inset = MailboxState_getMsn(M);
a = g_tree_new_full((GCompareDataFunc)ucmpdata,NULL, (GDestroyNotify)uint64_free, (GDestroyNotify)uint64_free);
b = g_tree_new_full((GCompareDataFunc)ucmpdata,NULL, (GDestroyNotify)uint64_free, (GDestroyNotify)uint64_free);
if (! uid) {
lo = 1;
hi = MailboxState_getExists(M);
} else {
GList *ids = g_tree_keys(inset);
if (ids) {
ids = g_list_last(ids);
hi = *((uint64_t *)ids->data);
ids = g_list_first(ids);
lo = *((uint64_t *)ids->data);
g_list_free(g_list_first(ids));
}
}
t = g_string_new(set);
sets = g_string_split(t, ",");
g_string_free(t,TRUE);
sets = g_list_first(sets);
while(sets) {
uint64_t l = 0, r = 0;
char *rest = (char *)sets->data;
if (strlen(rest) < 1) break;
if (g_tree_nnodes(inset) == 0) { // empty box
if (rest[0] == '*') {
uint64_t *k = mempool_pop(small_pool, sizeof(uint64_t));
uint64_t *v = mempool_pop(small_pool, sizeof(uint64_t));
*k = 1;
*v = MailboxState_getUidnext(M);
g_tree_insert(b, k, v);
} else {
if (! (l = dm_strtoull(sets->data, &rest, 10))) {
error = TRUE;
break;
}
if (rest[0]) {
if (rest[0] != ':') {
error = TRUE;
break;
}
rest++;
if ((rest[0] != '*') && (! dm_strtoull(rest, NULL, 10))) {
error = TRUE;
break;
}
}
uint64_t *k = mempool_pop(small_pool, sizeof(uint64_t));
uint64_t *v = mempool_pop(small_pool, sizeof(uint64_t));
*k = 1;
*v = MailboxState_getUidnext(M);
g_tree_insert(b, k, v);
}
} else {
if (rest[0] == '*') {
l = hi;
r = l;
if (strlen(rest) > 1)
rest++;
} else {
if (! (l = dm_strtoull(sets->data,&rest,10))) {
error = TRUE;
break;
}
if (l == 0xffffffff) l = hi; // outlook
l = max(l,lo);
r = l;
}
if (rest[0]==':') {
if (strlen(rest)>1) rest++;
if (rest[0] == '*') r = hi;
else {
if (! (r = dm_strtoull(rest,NULL,10))) {
error = TRUE;
break;
}
if (r == 0xffffffff) r = hi; // outlook
}
if (!r) break;
}
if (! (l && r)) break;
find_range(inset, min(l,r), max(l,r), a, uid);
if (g_tree_merge(b,a,IST_SUBSEARCH_OR)) {
error = TRUE;
TRACE(TRACE_ERR, "cannot compare null trees");
break;
}
}
if (! g_list_next(sets)) break;
sets = g_list_next(sets);
}
g_list_destroy(sets);
if (a) g_tree_destroy(a);
if (error) {
g_tree_destroy(b);
b = NULL;
TRACE(TRACE_DEBUG, "return NULL");
}
return b;
}
static gboolean _free_recent_queue(gpointer key, gpointer UNUSED value, gpointer data)
{
T M = (T)data;
mempool_push(M->pool, key, sizeof(uint64_t));
return FALSE;
}
void MailboxState_free(T *M)
{
T s = *M;
if (s->name)
p_string_free(s->name, TRUE);
g_tree_destroy(s->keywords);
s->keywords = NULL;
if (s->msn) g_tree_destroy(s->msn);
s->msn = NULL;
if (s->ids) g_tree_destroy(s->ids);
s->ids = NULL;
if (s->msginfo) g_tree_destroy(s->msginfo);
s->msginfo = NULL;
if (s->recent_queue) {
g_tree_foreach(s->recent_queue, (GTraverseFunc)_free_recent_queue, s);
g_tree_destroy(s->recent_queue);
}
s->recent_queue = NULL;
gboolean freepool = s->freepool;
Mempool_T pool = s->pool;
mempool_push(pool, s, sizeof(*s));
if (freepool) {
mempool_close(&pool);
}
s = NULL;
}
void db_getmailbox_permission(T M, Connection_T c)
{
ResultSet_T r;
PreparedStatement_T stmt;
g_return_if_fail(M->id);
stmt = db_stmt_prepare(c,
"SELECT permission FROM %smailboxes WHERE mailbox_idnr = ?",
DBPFX);
db_stmt_set_u64(stmt, 1, M->id);
r = db_stmt_query(stmt);
if (db_result_next(r))
M->permission = db_result_get_int(r, 0);
}
static void db_getmailbox_info(T M, Connection_T c)
{
/* query mailbox for LIST results */
ResultSet_T r;
char *mbxname, *name, *pattern;
struct mailbox_match *mailbox_like = NULL;
GString *fqname, *qs;
int i=0, prml;
PreparedStatement_T stmt;
stmt = db_stmt_prepare(c,
"SELECT "
"CASE WHEN user_id IS NULL THEN 0 ELSE 1 END, " // subscription
"owner_idnr, name, no_select, no_inferiors "
"FROM %smailboxes b LEFT OUTER JOIN %ssubscription s ON "
"b.mailbox_idnr = s.mailbox_id WHERE b.mailbox_idnr = ?",
DBPFX, DBPFX);
db_stmt_set_u64(stmt, 1, M->id);
r = db_stmt_query(stmt);
if (db_result_next(r)) {
/* subsciption */
M->is_subscribed = db_result_get_bool(r, i++);
/* owner_idnr */
M->owner_id = db_result_get_u64(r, i++);
/* name */
name = g_strdup(db_result_get(r,i++));
if (MATCH(name, "INBOX")) {
M->is_inbox = TRUE;
M->is_subscribed = TRUE;
}
mbxname = mailbox_add_namespace(name, M->owner_id, M->owner_id);
fqname = g_string_new(mbxname);
fqname = g_string_truncate(fqname,IMAP_MAX_MAILBOX_NAMELEN);
MailboxState_setName(M, fqname->str);
g_string_free(fqname,TRUE);
g_free(mbxname);
/* no_select */
M->no_select=db_result_get_bool(r,i++);
/* no_inferior */
M->no_inferiors=db_result_get_bool(r,i++);
/* no_children search pattern*/
pattern = g_strdup_printf("%s/%%", name);
mailbox_like = mailbox_match_new(pattern);
g_free(pattern);
g_free(name);
}
db_con_clear(c);
qs = g_string_new("");
g_string_printf(qs, "SELECT COUNT(*) AS nr_children FROM %smailboxes WHERE owner_idnr = ? ", DBPFX);
if (mailbox_like && mailbox_like->insensitive)
g_string_append_printf(qs, "AND name %s ? ", db_get_sql(SQL_INSENSITIVE_LIKE));
if (mailbox_like && mailbox_like->sensitive)
g_string_append_printf(qs, "AND name %s ? ", db_get_sql(SQL_SENSITIVE_LIKE));
stmt = db_stmt_prepare(c, qs->str);
prml = 1;
db_stmt_set_u64(stmt, prml++, M->owner_id);
if (mailbox_like && mailbox_like->insensitive)
db_stmt_set_str(stmt, prml++, mailbox_like->insensitive);
if (mailbox_like && mailbox_like->sensitive)
db_stmt_set_str(stmt, prml++, mailbox_like->sensitive);
r = db_stmt_query(stmt);
if (db_result_next(r)) {
int nr_children = db_result_get_int(r,0);
M->no_children=nr_children ? 0 : 1;
} else {
M->no_children=1;
}
mailbox_match_free(mailbox_like);