forked from rc0/mairix
-
Notifications
You must be signed in to change notification settings - Fork 8
/
search.c
1553 lines (1436 loc) · 42.4 KB
/
search.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
/*
mairix - message index builder and finder for maildir folders.
**********************************************************************
* Copyright (C) Richard P. Curnow 2002,2003,2004,2005,2006
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
/* Lame fix for systems where NAME_MAX isn't defined after including the above
* set of .h files (Solaris, FreeBSD so far). Probably grossly oversized but
* it'll do. */
#if !defined(NAME_MAX)
#define NAME_MAX 4096
#endif
#include "mairix.h"
#include "reader.h"
#include "memmac.h"
#include "imapinterface.h"
static void mark_hits_in_table(struct read_db *db, struct toktable_db *tt, int hit_tok, char *hits)/*{{{*/
{
/* mark files containing matched token */
int idx;
struct int_list_reader ilr;
read_db_int_list_reader_init(&ilr, db, tt->enc_offsets[hit_tok]);
while (int_list_reader_read(&ilr, &idx)) {
assert(idx < db->n_msgs);
hits[idx] = 1;
}
}
/*}}}*/
static void mark_hits_in_table2(struct read_db *db, struct toktable2_db *tt, int hit_tok, char *hits)/*{{{*/
{
/* mark files containing matched token */
int idx;
struct int_list_reader ilr;
read_db_int_list_reader_init(&ilr, db, tt->enc1_offsets[hit_tok]);
while (int_list_reader_read(&ilr, &idx)) {
assert(idx < db->n_msgs);
hits[idx] = 1;
}
}
/*}}}*/
/* See "Fast text searching with errors, Sun Wu and Udi Manber, TR 91-11,
University of Arizona. I have been informed that this algorithm is NOT
patented. This implementation of it is entirely the work of Richard P.
Curnow - I haven't looked at any related source (webglimpse, agrep etc) in
writing this.
*/
static void build_match_vector(char *substring, unsigned long *a, unsigned long *hit)/*{{{*/
{
int len;
char *p;
int i;
len = strlen(substring);
if (len > 31 || len == 0) {
fprintf(stderr, "Can't match patterns longer than 31 characters or empty\n");
unlock_and_exit(2);
}
memset(a, 0xff, 256 * sizeof(unsigned long));
for (p=substring, i=0; *p; p++, i++) {
a[(unsigned int) *(unsigned char *)p] &= ~(1UL << i);
}
*hit = ~(1UL << (len-1));
return;
}
/*}}}*/
static int substring_match_0(unsigned long *a, unsigned long hit, int left_anchor, const char *token)/*{{{*/
{
int got_hit=0;
const char *p;
unsigned long r0;
unsigned long anchor, anchor1;
r0 = ~0;
got_hit = 0;
anchor = 0;
anchor1 = left_anchor ? 0x1 : 0x0;
for(p=token; *p; p++) {
int idx = (unsigned int) *(unsigned char *)p;
r0 = (r0<<1) | anchor | a[idx];
if (~(r0 | hit)) {
got_hit = 1;
break;
}
anchor = anchor1;
}
return got_hit;
}
/*}}}*/
static int substring_match_1(unsigned long *a, unsigned long hit, int left_anchor, const char *token)/*{{{*/
{
int got_hit=0;
const char *p;
unsigned long r0, r1, nr0;
unsigned long anchor, anchor1;
r0 = ~0;
r1 = r0<<1;
got_hit = 0;
anchor = 0;
anchor1 = left_anchor ? 0x1 : 0x0;
for(p=token; *p; p++) {
int idx = (unsigned int) *(unsigned char *)p;
nr0 = (r0<<1) | anchor | a[idx];
r1 = ((r1<<1) | anchor | a[idx]) & ((r0 & nr0) << 1) & r0;
r0 = nr0;
if (~((r0 & r1) | hit)) {
got_hit = 1;
break;
}
anchor = anchor1;
}
return got_hit;
}
/*}}}*/
static int substring_match_2(unsigned long *a, unsigned long hit, int left_anchor, const char *token)/*{{{*/
{
int got_hit=0;
const char *p;
unsigned long r0, r1, r2, nr0, nr1;
unsigned long anchor, anchor1;
r0 = ~0;
r1 = r0<<1;
r2 = r1<<1;
got_hit = 0;
anchor = 0;
anchor1 = left_anchor ? 0x1 : 0x0;
for(p=token; *p; p++) {
int idx = (unsigned int) *(unsigned char *)p;
nr0 = (r0<<1) | anchor | a[idx];
nr1 = ((r1<<1) | anchor | a[idx]) & ((r0 & nr0) << 1) & r0;
r2 = ((r2<<1) | anchor | a[idx]) & ((r1 & nr1) << 1) & r1;
r0 = nr0;
r1 = nr1;
if (~((r0 & r1 & r2) | hit)) {
got_hit = 1;
break;
}
anchor = anchor1;
}
return got_hit;
}
/*}}}*/
static int substring_match_3(unsigned long *a, unsigned long hit, int left_anchor, const char *token)/*{{{*/
{
int got_hit=0;
const char *p;
unsigned long r0, r1, r2, r3, nr0, nr1, nr2;
unsigned long anchor, anchor1;
r0 = ~0;
r1 = r0<<1;
r2 = r1<<1;
r3 = r2<<1;
got_hit = 0;
anchor = 0;
anchor1 = left_anchor ? 0x1 : 0x0;
for(p=token; *p; p++) {
int idx = (unsigned int) *(unsigned char *)p;
nr0 = (r0<<1) | anchor | a[idx];
nr1 = ((r1<<1) | anchor | a[idx]) & ((r0 & nr0) << 1) & r0;
nr2 = ((r2<<1) | anchor | a[idx]) & ((r1 & nr1) << 1) & r1;
r3 = ((r3<<1) | anchor | a[idx]) & ((r2 & nr2) << 1) & r2;
r0 = nr0;
r1 = nr1;
r2 = nr2;
if (~((r0 & r1 & r2 & r3) | hit)) {
got_hit = 1;
break;
}
anchor = anchor1;
}
return got_hit;
}
/*}}}*/
static int substring_match_general(unsigned long *a, unsigned long hit, int left_anchor, const char *token, int max_errors, unsigned long *r, unsigned long *nr)/*{{{*/
{
int got_hit=0;
const char *p;
int j;
unsigned long anchor, anchor1;
r[0] = ~0;
anchor = 0;
anchor1 = left_anchor ? 0x1 : 0x0;
for (j=1; j<=max_errors; j++) {
r[j] = r[j-1] << 1;
}
got_hit = 0;
for(p=token; *p; p++) {
int idx = (unsigned int) *(unsigned char *)p;
int d;
unsigned int compo;
compo = nr[0] = ((r[0]<<1) | anchor | a[idx]);
for (d=1; d<=max_errors; d++) {
nr[d] = ((r[d]<<1) | anchor | a[idx])
& ((r[d-1] & nr[d-1])<<1)
& r[d-1];
compo &= nr[d];
}
memcpy(r, nr, (1 + max_errors) * sizeof(unsigned long));
if (~(compo | hit)) {
got_hit = 1;
break;
}
anchor = anchor1;
}
return got_hit;
}
/*}}}*/
static void match_substring_in_table(struct read_db *db, struct toktable_db *tt, char *substring, int max_errors, int left_anchor, char *hits)/*{{{*/
{
int i, got_hit;
unsigned long a[256];
unsigned long *r=NULL, *nr=NULL;
unsigned long hit;
const char *token;
build_match_vector(substring, a, &hit);
got_hit = 0;
if (max_errors > 3) {
r = new_array(unsigned long, 1 + max_errors);
nr = new_array(unsigned long, 1 + max_errors);
}
for (i=0; i<tt->n; i++) {
token = get_db_token(db, tt->tok_offsets[i]);
if (!token) continue;
switch (max_errors) {
/* Optimise common cases for few errors to allow optimizer to keep bitmaps
* in registers */
case 0:
got_hit = substring_match_0(a, hit, left_anchor, token);
break;
case 1:
got_hit = substring_match_1(a, hit, left_anchor, token);
break;
case 2:
got_hit = substring_match_2(a, hit, left_anchor, token);
break;
case 3:
got_hit = substring_match_3(a, hit, left_anchor, token);
break;
default:
got_hit = substring_match_general(a, hit, left_anchor, token, max_errors, r, nr);
break;
}
if (got_hit) {
mark_hits_in_table(db, tt, i, hits);
}
}
if (r) free(r);
if (nr) free(nr);
}
/*}}}*/
static void match_substring_in_paths(struct read_db *db, char *substring, int max_errors, int left_anchor, char *hits)/*{{{*/
{
int i;
unsigned long a[256];
unsigned long *r=NULL, *nr=NULL;
unsigned long hit;
build_match_vector(substring, a, &hit);
if (max_errors > 3) {
r = new_array(unsigned long, 1 + max_errors);
nr = new_array(unsigned long, 1 + max_errors);
}
for (i=0; i<db->n_msgs; i++) {
char *token = NULL;
unsigned int mbix, msgix;
switch (rd_msg_type(db, i)) {
case DB_MSG_FILE:
token = db->data + db->path_offsets[i];
break;
case DB_MSG_MBOX:
decode_mbox_indices(db->data + db->path_offsets[i], &mbix, &msgix);
token = db->data + db->mbox_paths_table[mbix];
break;
case DB_MSG_DEAD:
hits[i] = 0; /* never match on dead paths */
goto next_message;
}
assert(token);
switch (max_errors) {
/* Optimise common cases for few errors to allow optimizer to keep bitmaps
* in registers */
case 0:
hits[i] = substring_match_0(a, hit, left_anchor, token);
break;
case 1:
hits[i] = substring_match_1(a, hit, left_anchor, token);
break;
case 2:
hits[i] = substring_match_2(a, hit, left_anchor, token);
break;
case 3:
hits[i] = substring_match_3(a, hit, left_anchor, token);
break;
default:
hits[i] = substring_match_general(a, hit, left_anchor, token, max_errors, r, nr);
break;
}
next_message:
(void) 0;
}
if (r) free(r);
if (nr) free(nr);
}
/*}}}*/
static void match_string_in_table(struct read_db *db, struct toktable_db *tt, char *key, char *hits)/*{{{*/
{
/* TODO : replace with binary search? */
int i;
const char *token;
for (i=0; i<tt->n; i++) {
token = get_db_token(db, tt->tok_offsets[i]);
if (token && !strcmp(key, token)) {
/* get all matching files */
mark_hits_in_table(db, tt, i, hits);
}
}
}
/*}}}*/
static void match_string_in_table2(struct read_db *db, struct toktable2_db *tt, char *key, char *hits)/*{{{*/
{
/* TODO : replace with binary search? */
int i;
const char *token;
for (i=0; i<tt->n; i++) {
token = get_db_token(db, tt->tok_offsets[i]);
if (!strcmp(key, token)) {
/* get all matching files */
mark_hits_in_table2(db, tt, i, hits);
}
}
}
/*}}}*/
static int parse_size_expr(char *x)/*{{{*/
{
int result;
int n;
if (1 == sscanf(x, "%d%n", &result, &n)) {
x += n;
switch (*x) {
case 'k':
case 'K':
result <<= 10;
break;
case 'm':
case 'M':
result <<= 20;
break;
default:
break;
}
return result;
} else {
fprintf(stderr, "Could not parse message size expression <%s>\n", x);
return -1;
}
}
/*}}}*/
static void parse_size_range(char *size_expr, int *has_start, int *start, int *has_end, int *end)/*{{{*/
{
char *x = size_expr;
char *dash;
int len;
if (*x == ':') x++;
len = strlen(x);
dash = strchr(x, '-');
*has_start = *has_end = 0;
if (dash) {
char *p, *q;
if (dash > x) {
char *s;
s = new_array(char, dash - x + 1);
for (p=s, q=x; q<dash; ) *p++ = *q++;
*p = 0;
*start = parse_size_expr(s);
*has_start = 1;
free(s);
}
if (dash[1]) { /* dash not at end of arg */
char *e;
e = new_array(char, (x + len) - dash);
for (p=e, q=dash+1; *q; ) *p++ = *q++;
*p = 0;
*end = parse_size_expr(e);
*has_end = 1;
free(e);
}
} else {
*has_start = 0;
*end = parse_size_expr(size_expr);
*has_end = 1;
}
return;
}
/*}}}*/
static void find_size_matches_in_table(struct read_db *db, char *size_expr, char *hits)/*{{{*/
{
int start, end;
int has_start, has_end, start_cond, end_cond;
int i;
start = end = -1; /* avoid compiler warning about uninitialised variables. */
parse_size_range(size_expr, &has_start, &start, &has_end, &end);
if (has_start && has_end) {
/* Allow user to put the endpoints in backwards */
if (start > end) {
int temp = start;
start = end;
end = temp;
}
}
for (i=0; i<db->n_msgs; i++) {
start_cond = has_start ? (db->size_table[i] > start) : 1;
end_cond = has_end ? (db->size_table[i] < end ) : 1;
if (start_cond && end_cond) {
hits[i] = 1;
}
}
}
/*}}}*/
static void find_date_matches_in_table(struct read_db *db, char *date_expr, char *hits)/*{{{*/
{
time_t start, end;
int has_start, has_end, start_cond, end_cond;
int i;
int status;
status = scan_date_string(date_expr, &start, &has_start, &end, &has_end);
if (status) {
unlock_and_exit (2);
}
if (has_start && has_end) {
/* Allow user to put the endpoints in backwards */
if (start > end) {
time_t temp = start;
start = end;
end = temp;
}
}
for (i=0; i<db->n_msgs; i++) {
start_cond = has_start ? (db->date_table[i] > start) : 1;
end_cond = has_end ? (db->date_table[i] < end ) : 1;
if (start_cond && end_cond) {
hits[i] = 1;
}
}
}
/*}}}*/
static void find_flag_matches_in_table(struct read_db *db, char *flag_expr, char *hits)/*{{{*/
{
int pos_seen, neg_seen;
int pos_replied, neg_replied;
int pos_flagged, neg_flagged;
int negate;
char *p;
int i;
negate = 0;
pos_seen = neg_seen = 0;
pos_replied = neg_replied = 0;
pos_flagged = neg_flagged = 0;
for (p=flag_expr; *p; p++) {
switch (*p) {
case '-':
negate = 1;
break;
case 's':
case 'S':
if (negate) neg_seen = 1;
else pos_seen = 1;
negate = 0;
break;
case 'r':
case 'R':
if (negate) neg_replied = 1;
else pos_replied = 1;
negate = 0;
break;
case 'f':
case 'F':
if (negate) neg_flagged = 1;
else pos_flagged = 1;
negate = 0;
break;
default:
fprintf(stderr, "Did not understand the character '%c' (0x%02x) in the flags argument F:%s\n",
isprint(*p) ? *p : '.',
(int) *(unsigned char *) p,
flag_expr);
break;
}
}
for (i=0; i<db->n_msgs; i++) {
if ((!pos_seen || (db->msg_type_and_flags[i] & FLAG_SEEN)) &&
(!neg_seen || !(db->msg_type_and_flags[i] & FLAG_SEEN)) &&
(!pos_replied || (db->msg_type_and_flags[i] & FLAG_REPLIED)) &&
(!neg_replied || !(db->msg_type_and_flags[i] & FLAG_REPLIED)) &&
(!pos_flagged || (db->msg_type_and_flags[i] & FLAG_FLAGGED)) &&
(!neg_flagged || !(db->msg_type_and_flags[i] & FLAG_FLAGGED))) {
hits[i] = 1;
}
}
}
/*}}}*/
static char *mk_maildir_path(int token, char *output_dir, int is_in_new,
int is_seen, int is_replied, int is_flagged)/*{{{*/
{
char *result;
char uniq_buf[48];
int len;
len = strlen(output_dir) + 64; /* oversize */
result = new_array(char, len + 1 + sizeof(":2,FRS"));
strcpy(result, output_dir);
strcat(result, is_in_new ? "/new/" : "/cur/");
sprintf(uniq_buf, "123456789.%d.mairix", token);
strcat(result, uniq_buf);
if (is_seen || is_replied || is_flagged) {
strcat(result, ":2,");
}
if (is_flagged) strcat(result, "F");
if (is_replied) strcat(result, "R");
if (is_seen) strcat(result, "S");
return result;
}
/*}}}*/
static char *mk_mh_path(int token, char *output_dir)/*{{{*/
{
char *result;
char uniq_buf[11];
int len;
len = strlen(output_dir) + 12; /* oversize */
result = new_array(char, len);
strcpy(result, output_dir);
strcat(result, "/");
sprintf(uniq_buf, "%d", token+1);
strcat(result, uniq_buf);
return result;
}
/*}}}*/
static int looks_like_maildir_new_p(const char *p)/*{{{*/
{
const char *s1, *s2;
s2 = p;
while (*s2) s2++;
while ((s2 > p) && (*s2 != '/')) s2--;
if (s2 <= p) return 0;
s1 = s2 - 1;
while ((s1 > p) && (*s1 != '/')) s1--;
if (s1 <= p) return 0;
if (!strncmp(s1, "/new/", 5)) {
return 1;
} else {
return 0;
}
}
/*}}}*/
static void create_symlink(char *link_target, char *new_link)/*{{{*/
{
if ((!do_hardlinks && symlink(link_target, new_link) < 0) || link(link_target, new_link)) {
if (verbose) {
perror("symlink");
fprintf(stderr, "Failed path <%s> -> <%s>\n", link_target, new_link);
}
}
}
/*}}}*/
static void mbox_terminate(const unsigned char *data, int len, FILE *out)/*{{{*/
{
if (len == 0)
fputs("\n", out);
else if (len == 1) {
if (data[0] != '\n')
fputs("\n", out);
}
else if (data[len-1] != '\n')
fputs("\n\n", out);
else if (data[len-2] != '\n')
fputs("\n", out);
}
/*}}}*/
static void append_file_to_mbox(const char *path, FILE *out)/*{{{*/
{
unsigned char *data;
int len;
create_ro_mapping(path, &data, &len, MAP_DECOMPRESS_IF_APPLICABLE);
if (data) {
fprintf(out, "From mairix@mairix Mon Jan 1 12:34:56 1970\n");
fprintf(out, "X-source-folder: %s\n", path);
fwrite (data, sizeof(unsigned char), len, out);
mbox_terminate(data, len, out);
free_ro_mapping(data, len);
}
return;
}
/*}}}*/
static void append_data_to_mbox(const char *data, size_t len, void *arg)/*{{{*/
{
FILE *out = (FILE *)arg;
fprintf(out, "From mairix@mairix Mon Jan 1 12:34:56 1970\n");
fwrite (data, sizeof(unsigned char), len, out);
fputc('\n', out);
return;
}
/*}}}*/
static void write_to_file(const char *data, size_t len, void *arg)/*{{{*/
{
FILE *fp;
if (!(fp = fopen((char *)arg, "w"))) {
fprintf(stderr, "Cannot write matched message to \"%s\": %s\n", (char *)arg, strerror(errno));
return;
}
fwrite(data, 1, len, fp);
fclose(fp);
}
/*}}}*/
static int had_failed_checksum;
static void get_validated_mbox_msg(struct read_db *db, int msg_index,/*{{{*/
int *mbox_index,
unsigned char **mbox_data, int *mbox_len,
unsigned char **msg_data, int *msg_len)
{
/* msg_data==NULL if checksum mismatches */
unsigned char *start;
checksum_t csum;
unsigned int mbi, msgi;
*msg_data = NULL;
*msg_len = 0;
decode_mbox_indices(db->data + db->path_offsets[msg_index], &mbi, &msgi);
*mbox_index = mbi;
create_ro_mapping(db->data + db->mbox_paths_table[mbi], mbox_data, mbox_len,
MAP_DECOMPRESS_IF_APPLICABLE);
if (!*mbox_data) return;
start = *mbox_data + db->mtime_table[msg_index];
/* Ensure that we don't run off the end of the mmap'd file */
if (db->mtime_table[msg_index] >= *mbox_len)
*msg_len = 0;
else if (db->mtime_table[msg_index] + db->size_table[msg_index] >= *mbox_len)
*msg_len = *mbox_len - db->mtime_table[msg_index];
else
*msg_len = db->size_table[msg_index];
compute_checksum((char *)start, *msg_len, &csum);
if (!memcmp((db->data + db->mbox_checksum_table[mbi] + (msgi * sizeof(checksum_t))), &csum, sizeof(checksum_t))) {
*msg_data = start;
} else {
had_failed_checksum = 1;
}
return;
}
/*}}}*/
static void append_mboxmsg_to_mbox(struct read_db *db, int msg_index, FILE *out)/*{{{*/
{
/* Need to common up code with try_copy_to_path */
unsigned char *mbox_start, *msg_start;
int mbox_len, msg_len;
int mbox_index;
get_validated_mbox_msg(db, msg_index, &mbox_index, &mbox_start, &mbox_len, &msg_start, &msg_len);
if (msg_start) {
/* Artificial from line, we don't have the envelope sender so this is
going to be artificial anyway. */
fprintf(out, "From mairix@mairix Mon Jan 1 12:34:56 1970\n");
fprintf(out, "X-source-folder: %s\n",
db->data + db->mbox_paths_table[mbox_index]);
fwrite(msg_start, sizeof(unsigned char), msg_len, out);
mbox_terminate(msg_start, msg_len, out);
}
if (mbox_start) {
free_ro_mapping(mbox_start, mbox_len);
}
}
/*}}}*/
static void try_copy_to_path(struct read_db *db, int msg_index, char *target_path)/*{{{*/
{
unsigned char *data;
int mbox_len, msg_len;
int mbi;
FILE *out;
unsigned char *start;
get_validated_mbox_msg(db, msg_index, &mbi, &data, &mbox_len, &start, &msg_len);
if (start) {
out = fopen(target_path, "wb");
if (out) {
fprintf(out, "X-source-folder: %s\n",
db->data + db->mbox_paths_table[mbi]);
fwrite(start, sizeof(char), msg_len?msg_len-1:0, out);
fclose(out);
}
}
if (data) {
free_ro_mapping(data, mbox_len);
}
return;
}
/*}}}*/
static struct msg_src *setup_mbox_msg_src(char *filename, off_t start, size_t len)/*{{{*/
{
static struct msg_src result;
result.type = MS_MBOX;
result.filename = filename;
result.start = start;
result.len = len;
return &result;
}
/*}}}*/
static void get_flags_from_file(struct read_db *db, int idx, int *is_seen, int *is_replied, int *is_flagged)
{
*is_seen = (db->msg_type_and_flags[idx] & FLAG_SEEN) ? 1 : 0;
*is_replied = (db->msg_type_and_flags[idx] & FLAG_REPLIED) ? 1 : 0;
*is_flagged = (db->msg_type_and_flags[idx] & FLAG_FLAGGED) ? 1 : 0;
}
static void string_tolower(char *str)
{
char *p;
for (p=str; *p; p++) {
*p = tolower(*(unsigned char *)p);
}
}
static struct read_db *qsortdb;
static int db_date_cmp(const void *va, const void *vb)
{
const int *ap = (const int *) va;
const int *bp = (const int *) vb;
return qsortdb->date_table[*ap] - qsortdb->date_table[*bp];
}
static int do_search(struct read_db *db, char **args, char *output_path, int show_threads, enum folder_type ft, int verbose, const char *imap_pipe, const char *imap_server, const char *imap_username, const char *imap_password, int please_clear, int sort_by_date)/*{{{*/
{
char *colon, *start_words;
int do_body, do_subject, do_from, do_to, do_cc, do_date, do_size;
int do_att_name;
int do_flags;
int do_path, do_msgid;
char *key;
char *hit0, *hit1, *hit2, *hit3;
int i;
int ii;
int n_hits;
int left_anchor;
int imap_tried = 0;
struct imap_ll *imapc;
int *results;
#define GET_IMAP if (!imap_tried) {\
imap_tried = 1;\
if (imap_pipe || imap_server) {\
imapc = imap_start(imap_pipe, imap_server, imap_username, imap_password);\
} else {\
fprintf(stderr, "[No IMAP settings]\n");\
imapc = NULL;\
}\
}
had_failed_checksum = 0;
hit0 = new_array(char, db->n_msgs);
hit1 = new_array(char, db->n_msgs);
hit2 = new_array(char, db->n_msgs);
hit3 = new_array(char, db->n_msgs);
/* Argument structure is
* x:tokena+tokenb,~tokenc,tokend+tokene
*
* + (and) binds more tightly than ,
* , (or) binds more tightly than separate args
*
*
* hit1 gathers the tokens and'ed with +
* hit2 gathers the tokens or'ed with ,
* hit3 gathers the separate args and'ed with <gap>
* */
/* Everything matches until proven otherwise */
memset(hit3, 1, db->n_msgs);
while (*args) {
/* key is a single argument, separate args are and-ed together */
key = *args++;
memset(hit2, 0, db->n_msgs);
memset(hit1, 1, db->n_msgs);
do_to = 0;
do_cc = 0;
do_from = 0;
do_subject = 0;
do_body = 0;
do_date = 0;
do_size = 0;
do_path = 0;
do_msgid = 0;
do_att_name = 0;
do_flags = 0;
colon = strchr(key, ':');
if (colon) {
char *p;
for (p=key; p<colon; p++) {
switch(*p) {
case 'b': do_body = 1; break;
case 's': do_subject = 1; break;
case 't': do_to = 1; break;
case 'c': do_cc = 1; break;
case 'f': do_from = 1; break;
case 'r': do_to = do_cc = 1; break;
case 'a': do_to = do_cc = do_from = 1; break;
case 'd': do_date = 1; break;
case 'z': do_size = 1; break;
case 'p': do_path = 1; break;
case 'm': do_msgid = 1; break;
case 'n': do_att_name = 1; break;
case 'F': do_flags = 1; break;
default: fprintf(stderr, "Unknown key type <%c>\n", *p); break;
}
}
if (do_msgid && (p-key) > 1) {
fprintf(stderr, "Message-ID key <m> can't be used with other keys\n");
unlock_and_exit(2);
}
start_words = 1 + colon;
} else {
do_body = do_subject = do_to = do_cc = do_from = 1;
start_words = key;
}
if (do_date || do_size || do_flags) {
memset(hit0, 0, db->n_msgs);
if (do_date) {
find_date_matches_in_table(db, start_words, hit0);
} else if (do_size) {
find_size_matches_in_table(db, start_words, hit0);
} else if (do_flags) {
find_flag_matches_in_table(db, start_words, hit0);
}
/* AND-combine match vectors */
for (i=0; i<db->n_msgs; i++) {
hit1[i] &= hit0[i];
}
} else if (do_msgid) {
char *lower_word = new_string(start_words);
string_tolower(lower_word);
memset(hit0, 0, db->n_msgs);
match_string_in_table2(db, &db->msg_ids, lower_word, hit0);
free(lower_word);
/* AND-combine match vectors */
for (i=0; i<db->n_msgs; i++) {
hit1[i] &= hit0[i];
}
} else {
/*{{{ Scan over separate words within this argument */
do {
/* / = 'or' separator
* , = 'and' separator */
char *orsep;
char *andsep;
char *word, *orig_word, *lower_word;
char *equal;
int negate;
int had_orsep;
int max_errors;
orsep = strchr(start_words, '/');
andsep = strchr(start_words, ',');
had_orsep = 0;
if (andsep && (!orsep || (andsep < orsep))) {
char *p, *q;
word = new_array(char, 1 + (andsep - start_words)); /* maybe oversize */
for (p=word, q=start_words; q < andsep; q++) {
if (!isspace(*(unsigned char *)q)) {
*p++ = *q;
}
}
*p = 0;
start_words = andsep + 1;
} else if (orsep) { /* comes before + if there's a + */
char *p, *q;
word = new_array(char, 1 + (orsep - start_words)); /* maybe oversize */
for (p=word, q=start_words; q < orsep; q++) {
if (!isspace(*(unsigned char *)q)) {
*p++ = *q;
}
}
*p = 0;
start_words = orsep + 1;
had_orsep = 1;
} else {
word = new_string(start_words);
while (*start_words) ++start_words;
}
orig_word = word;
if (word[0] == '~') {
negate = 1;
word++;
} else {
negate = 0;
}
if (word[0] == '^') {
left_anchor = 1;
word++;
} else {
left_anchor = 0;
}
equal = strchr(word, '=');
if (equal && (equal[1] == '\0' || isdigit(equal[1]))) {
*equal = 0;
max_errors = atoi(equal + 1);
/* Extend this to do anchoring etc */
} else {
equal = NULL;
max_errors = 0; /* keep GCC quiet */
}
/* Canonicalise search string to lowercase, since the database has all
* tokens handled that way. But not for path search! */
lower_word = new_string(word);
string_tolower(lower_word);
memset(hit0, 0, db->n_msgs);
if (equal) {