forked from vandry/mairix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mairix.c
858 lines (774 loc) · 25.7 KB
/
mairix.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
/*
mairix - message index builder and finder for maildir folders.
**********************************************************************
* Copyright (C) Richard P. Curnow 2002,2003,2004,2005,2006,2007,2008
* Copyright (C) Sanjoy Mahajan 2005
* - mfolder validation code
* Copyright (C) James Cameron 2005
* Copyright (C) Paul Fox 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 "mairix.h"
#include "version.h"
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <unistd.h>
#include <ctype.h>
#include <locale.h>
#include <signal.h>
#include "imapinterface.h"
#ifdef TEST_OOM
int total_bytes=0;
#endif
int verbose = 0;
int do_hardlinks = 0;
static char *folder_base = NULL;
static char *maildir_folders = NULL;
static char *mh_folders = NULL;
static char *mboxen = NULL;
static char *imap_folders = NULL;
static char *imap_pipe = NULL;
static char *imap_server = NULL;
static char *imap_username = NULL;
static char *imap_password = NULL;
static char *mfolder = NULL;
static char *omit = NULL;
static char *database_path = NULL;
static enum folder_type output_folder_type = FT_MAILDIR;
static int skip_integrity_checks = 0;
static int follow_mbox_symlinks = 0;
static int sort_by_date = 0;
enum filetype {
M_NONE, M_FILE, M_DIR, M_OTHER
};
static enum filetype classify_file(char *name)/*{{{*/
{
struct stat sb;
if (stat(name, &sb) < 0) {
return M_NONE;
}
if (S_ISREG(sb.st_mode)) {
return M_FILE;
} else if (S_ISDIR(sb.st_mode)) {
return M_DIR;
} else {
return M_OTHER;
}
}
/*}}}*/
/*{{{ member of*/
/* returns 1 iff COMPLETE_MFOLDER (i.e. the match folder with
folder_base prepended if needed) matches one of the FOLDERS after
expanding the wildcards and recursion. Used to make sure that the
match folder will not overwrite a valuable mail file or
directory. */
int member_of (const char *complete_mfolder,
const char *folder_base,
const char *folders,
enum folder_type ft,
struct globber_array *omit_globs) {
char **raw_paths, **paths;
int n_raw_paths, n_paths, i;
if (!folders)
return 0;
split_on_colons(folders, &n_raw_paths, &raw_paths);
switch (ft) {
case FT_MAILDIR:
glob_and_expand_paths(folder_base, raw_paths, n_raw_paths, &paths, &n_paths, &maildir_traverse_methods, omit_globs);
break;
case FT_MH:
glob_and_expand_paths(folder_base, raw_paths, n_raw_paths, &paths, &n_paths, &mh_traverse_methods, omit_globs);
break;
case FT_MBOX:
glob_and_expand_paths(folder_base, raw_paths, n_raw_paths, &paths, &n_paths, &mbox_traverse_methods, omit_globs);
break;
case FT_RAW: /* cannot happen but to keep compiler happy */
case FT_EXCERPT:
case FT_IMAP:
break;
}
for (i=0; i<n_paths; i++) {
struct stat mfolder_sb, src_folder_sb; /* for checking inode numbers */
/* if the complete path names are the same, definitely a match */
if (strcmp (complete_mfolder, paths[i]) == 0)
return 1;
/* also a match if they point to the same file or directory but
via different routes (e.g. absolute path for one but path with
../.. for the other), so check inode numbers */
/* if cannot even get stat() info, probably not wrecking any mail
files or dirs, so continue, i.e. skip inode check. */
if (stat (complete_mfolder, &mfolder_sb) != 0 ||
stat (paths[i], &src_folder_sb) != 0)
continue;
if (mfolder_sb.st_ino == src_folder_sb.st_ino)
return 1;
}
free_string_array(n_raw_paths, &raw_paths);
free_string_array(n_paths, &paths);
return 0;
}
/*}}}*/
static char *copy_value(char *text)/*{{{*/
{
char *p;
char *result;
for (p = text; *p && (*p != '='); p++) ;
if (!*p) return NULL;
p++;
result = expand_string(p);
return result;
}
/*}}}*/
static void add_folders(char **folders, char *extra_folders)/*{{{*/
{
/* note : extra_pointers is stale after this routine exits. */
if (!*folders) {
*folders = extra_folders;
} else {
char *old_folders = *folders;
char *new_folders;
int old_len, extra_len;
old_len = strlen(old_folders);
extra_len = strlen(extra_folders);
new_folders = new_array(char, old_len + extra_len + 2);
strcpy(new_folders, old_folders);
strcpy(new_folders + old_len, ":");
strcpy(new_folders + old_len + 1, extra_folders);
*folders = new_folders;
free(old_folders);
}
}
/*}}}*/
static void parse_output_folder(char *p)/*{{{*/
{
char *temp;
temp = copy_value(p);
if (!strncasecmp(temp, "mh", 2)) {
output_folder_type = FT_MH;
} else if (!strncasecmp(temp, "maildir", 7)) {
output_folder_type = FT_MAILDIR;
} else if (!strncasecmp(temp, "raw", 3)) {
output_folder_type = FT_RAW;
} else if (!strncasecmp(temp, "excerpt", 3)) {
output_folder_type = FT_EXCERPT;
} else if (!strncasecmp(temp, "mbox", 4)) {
output_folder_type = FT_MBOX;
} else if (!strncasecmp(temp, "imap", 4)) {
output_folder_type = FT_IMAP;
}
else {
fprintf(stderr, "Unrecognized mformat <%s>\n", temp);
}
free(temp);
}
/*}}}*/
static void parse_rc_file(char *name)/*{{{*/
{
FILE *in;
char line[4096], *p;
int len, lineno;
int all_blank;
int used_default_name = 0;
if (!name) {
/* open default file */
struct passwd *pw;
char *home;
home = getenv("HOME");
if (!home) {
pw = getpwuid(getuid());
if (!pw) {
fprintf(stderr, "Cannot determine home directory\n");
exit(2);
}
home = pw->pw_dir;
}
name = new_array(char, strlen(home) + 12);
strcpy(name, home);
strcat(name, "/.mairixrc");
used_default_name = 1;
}
in = fopen(name, "r");
if (!in) {
fprintf(stderr, "Cannot open %s, exiting\n", name);
exit(2);
}
lineno = 0;
while(fgets(line, sizeof(line), in)) {
lineno++;
len = strlen(line);
if (len > sizeof(line) - 4) {
fprintf(stderr, "Line %d in %s too long, exiting\n", lineno, name);
exit(2);
}
if (line[len-1] == '\n') {
line[len-1] = '\0';
}
/* Strip trailing comments. */
for (p=line; *p && !strchr("#!;%", *p); p++) ;
if (*p) *p = '\0';
/* Discard blank lines */
all_blank = 1;
for (p=line; *p; p++) {
if (!isspace(*(unsigned char *)p)) {
all_blank = 0;
break;
}
}
if (all_blank) continue;
/* Now a real line to parse */
if (!strncasecmp(p, "base", 4)) folder_base = copy_value(p);
else if (!strncasecmp(p, "folders", 7)) {
fprintf(stderr, "'folders=' option in rc file is depracated, use 'maildir='\n");
add_folders(&maildir_folders, copy_value(p));
}
else if (!strncasecmp(p, "maildir=", 8)) add_folders(&maildir_folders, copy_value(p));
else if (!strncasecmp(p, "mh_folders=", 11)) {
fprintf(stderr, "'mh_folders=' option in rc file is depracated, use 'mh='\n");
add_folders(&mh_folders, copy_value(p));
}
else if (!strncasecmp(p, "mh=", 3)) add_folders(&mh_folders, copy_value(p));
else if (!strncasecmp(p, "mbox=", 5)) add_folders(&mboxen, copy_value(p));
else if (!strncasecmp(p, "imap=", 5)) add_folders(&imap_folders, copy_value(p));
else if (!strncasecmp(p, "imap_pipe=", 10)) imap_pipe = copy_value(p);
else if (!strncasecmp(p, "imap_server=", 12)) imap_server = copy_value(p);
else if (!strncasecmp(p, "imap_username=", 14)) imap_username = copy_value(p);
else if (!strncasecmp(p, "imap_password=", 14)) imap_password = copy_value(p);
else if (!strncasecmp(p, "follow_mbox_symlinks", 20)) follow_mbox_symlinks = 1;
else if (!strncasecmp(p, "omit=", 5)) add_folders(&omit, copy_value(p));
else if (!strncasecmp(p, "mformat=", 8)) {
parse_output_folder(p);
}
else if (!strncasecmp(p, "mfolder=", 8)) mfolder = copy_value(p);
else if (!strncasecmp(p, "database=", 9)) database_path = copy_value(p);
else if (!strncasecmp(p, "nochecks", 8)) skip_integrity_checks = 1;
else if (!strncasecmp(p, "sort=date+", 10)) sort_by_date = 1;
else {
if (verbose) {
fprintf(stderr, "Unrecognized option at line %d in %s\n", lineno, name);
}
}
}
fclose(in);
if (used_default_name) free(name);
}
/*}}}*/
static int message_compare(const void *a, const void *b)/*{{{*/
{
/* FIXME : Is this a sensible way to do this with mbox messages in the picture? */
struct msgpath *aa = (struct msgpath *) a;
struct msgpath *bb = (struct msgpath *) b;
if (aa->type < bb->type) return -1;
if (aa->type > bb->type) return 1;
return strcmp(aa->src.mpf.path, bb->src.mpf.path);
}
/*}}}*/
static void sort_message_list(struct msgpath_array *arr)/*{{{*/
{
if (arr->paths)
qsort(arr->paths, arr->n, sizeof(struct msgpath), message_compare);
}
/*}}}*/
static int compare_strings(const void *a, const void *b)/*{{{*/
{
const char **aa = (const char **) a;
const char **bb = (const char **) b;
return strcmp(*aa, *bb);
}
/*}}}*/
static int check_message_list_for_duplicates(struct msgpath_array *msgs)/*{{{*/
{
/* Caveat : only examines the file-per-message case */
char **sorted_paths, **sorted_imap;
int i, n, nn, imap_nn;
int result;
n = msgs->n;
sorted_paths = new_array(char *, n);
sorted_imap = new_array(char *, n);
for (i=0, nn=0, imap_nn=0; i<n; i++) {
switch (msgs->paths[i].type) {
case MTY_MBOX:
break;
case MTY_DEAD:
assert(0);
break;
case MTY_FILE:
sorted_paths[nn++] = msgs->paths[i].src.mpf.path;
break;
case MTY_IMAP:
sorted_imap[imap_nn++] = msgs->paths[i].src.mpf.path;
break;
}
}
qsort(sorted_paths, nn, sizeof(char *), compare_strings);
qsort(sorted_imap, imap_nn, sizeof(char *), compare_strings);
result = 0;
for (i=1; i<nn; i++) {
if (!strcmp(sorted_paths[i-1], sorted_paths[i])) {
result = 1;
break;
}
}
for (i=1; i<imap_nn; i++) {
if (!strcmp(sorted_imap[i-1], sorted_imap[i])) {
result = 1;
break;
}
}
free(sorted_paths);
free(sorted_imap);
return result;
}
/*}}}*/
static void emit_int(int x)/*{{{*/
{
char buf1[20], buf2[20];
char *p, *q;
int neg=0;
p = buf1;
*p = '0'; /* In case x is zero */
if (x < 0) {
neg = 1;
x = -x;
}
while (x) {
*p++ = '0' + (x % 10);
x /= 10;
}
p--;
q = buf2;
if (neg) *q++ = '-';
while (p >= buf1) {
*q++ = *p--;
}
write(2, buf2, q-buf2);
return;
}
/*}}}*/
void out_of_mem(char *file, int line, size_t size)/*{{{*/
{
/* Hairy coding ahead - can't use any [s]printf, itoa etc because
* those might try to use the heap! */
int filelen;
char *p;
static char msg1[] = "Out of memory (at ";
static char msg2[] = " bytes)\n";
/* Perhaps even strlen is unsafe in this situation? */
p = file;
while (*p) p++;
filelen = p - file;
write(2, msg1, sizeof(msg1)-1);
write(2, file, filelen);
write(2, ":", 1);
emit_int(line);
write(2, ", ", 2);
emit_int(size);
write(2, msg2, sizeof(msg2)-1);
exit(2);
}
/*}}}*/
void report_error(const char *str, const char *filename)/*{{{*/
{
if (filename) {
int len = strlen(str) + strlen(filename) + 4;
char *t;
t = new_array(char, len);
sprintf(t, "%s '%s'", str, filename);
perror(t);
free(t);
} else {
perror(str);
}
}
/*}}}*/
static void print_copyright(void)/*{{{*/
{
fprintf(stderr,
"mairix %s, Copyright (C) 2002-2010 Richard P. Curnow\n"
"mairix comes with ABSOLUTELY NO WARRANTY.\n"
"This is free software, and you are welcome to redistribute it\n"
"under certain conditions; see the GNU General Public License for details.\n\n",
PROGRAM_VERSION);
}
/*}}}*/
static void print_version(void)/*{{{*/
{
fprintf(stdout,
"mairix %s\n",
PROGRAM_VERSION);
}
/*}}}*/
static void handlesig(int signo)/*{{{*/
{
unlock_and_exit(7);
}
/*}}}*/
static void usage(void)/*{{{*/
{
print_copyright();
printf("mairix [-h] : Show help\n"
"mairix [-f <rcfile>] [-v] [-p] [-F] : Build index\n"
"mairix [-f <rcfile>] [-a] [-t] expr1 ... exprN : Run search\n"
"mairix [-f <rcfile>] -d : Dump database to stdout\n"
"-h : show this help\n"
"-f <rcfile> : use alternative rc file (default ~/.mairixrc)\n"
"-V : show version\n"
"-v : be verbose\n"
"-p : purge messages that no longer exist\n"
"-F : fast scan for maildir and MH folders (no mtime or size checks)\n"
"-a : add new matches to match folder (default : clear it first)\n"
"-x : display excerpt of message headers (default : use match folder)\n"
"-t : include all messages in same threads as matching messages\n"
"-o <mfolder> : override setting of mfolder from mairixrc file\n"
"-r : force raw output regardless of mformat setting in mairixrc file\n"
"-H : force hard links rather than symbolic ones\n"
"expr_i : search expression (all expr's AND'ed together):\n"
" word : match word in message body and major headers\n"
" t:word : match word in To: header\n"
" c:word : match word in Cc: header\n"
" f:word : match word in From: header\n"
" a:word : match word in To:, Cc: or From: headers (address)\n"
" s:word : match word in Subject: header\n"
" b:word : match word in message body\n"
" m:word : match word in Message-ID: header\n"
" n:word : match name of attachment within message\n"
" F:flags : match on message flags (s=seen,r=replied,f=flagged,-=negate)\n"
" p:substring : match substring of path\n"
" d:start-end : match date range\n"
" z:low-high : match messages in size range\n"
" bs:word : match word in Subject: header or body (or any other group of prefixes)\n"
" s:word1,word2 : match both words in Subject:\n"
" s:word1/word2 : match either word or both words in Subject:\n"
" s:~word : match messages not containing word in Subject:\n"
" s:substring= : match substring in any word in Subject:\n"
" s:^substring= : match left-anchored substring in any word in Subject:\n"
" s:substring=2 : match substring with <=2 errors in any word in Subject:\n"
"\n"
" (See documentation for more examples)\n"
);
}
/*}}}*/
/* Notes on folder management: {{{
Assumption is that the user wants to keep the 'mfolder' directories under a
common root with the real maildir folders. This allows a common value for
mutt's 'folder' variable => the '+' and '=' prefixes work better. This
means the indexer here can't just scan down all subdirectories of a single
ancestor, because it'll pick up its own mfolders. So, use environment
variables to tailor the folders.
MAIRIX_FOLDER_BASE is the common parent directory of the folders (aka
mutt's 'folder' variable)
MAIRIX_MAILDIR_FOLDERS, MAIRIX_MH_FOLDERS, MAIRIX_MBOXEN are
colon-separated lists of folders to index, with '...' after a
component meaning any maildir underneath it.
MAIRIX_MFOLDER is the folder to put the match data.
For example, if
MAIRIX_FOLDER_BASE = "/home/foobar/mail"
MAIRIX_FOLDERS = "inbox:lists...:action:archive..."
MAIRIX_MFOLDER = "mf"
then /home/foobar/mail/mf/{new,cur,tmp} contain the output of the search.
}}} */
int main (int argc, char **argv)/*{{{*/
{
struct msgpath_array *msgs;
struct database *db = NULL;
char *arg_rc_file_path = NULL;
char *arg_mfolder = NULL;
char *e;
int do_augment = 0;
int do_threads = 0;
int do_search = 0;
int do_purge = 0;
int any_updates = 0;
int any_purges = 0;
int do_help = 0;
int do_raw_output = 0;
int do_excerpt_output = 0;
int do_dump = 0;
int do_integrity_checks = 1;
int do_forced_unlock = 0;
int do_fast_index = 0;
int do_mbox_symlinks = 0;
struct imap_ll *imapc = NULL;
unsigned int forced_hash_key = CREATE_RANDOM_DATABASE_HASH;
struct globber_array *omit_globs;
int result;
setlocale(LC_CTYPE, "");
while (++argv, --argc) {
if (!*argv) {
break;
} else if (!strcmp(*argv, "-f") || !strcmp(*argv, "--rcfile")) {
++argv, --argc;
if (!argc) {
fprintf(stderr, "No filename given after -f argument\n");
exit(1);
}
arg_rc_file_path = *argv;
} else if (!strcmp(*argv, "-t") || !strcmp(*argv, "--threads")) {
do_search = 1;
do_threads = 1;
} else if (!strcmp(*argv, "-a") || !strcmp(*argv, "--augment")) {
do_search = 1;
do_augment = 1;
} else if (!strcmp(*argv, "-o") || !strcmp(*argv, "--mfolder")) {
++argv, --argc;
if (!argc) {
fprintf(stderr, "No folder name given after -o argument\n");
exit(1);
}
arg_mfolder = *argv;
} else if (!strcmp(*argv, "-p") || !strcmp(*argv, "--purge")) {
do_purge = 1;
} else if (!strcmp(*argv, "-d") || !strcmp(*argv, "--dump")) {
do_dump = 1;
} else if (!strcmp(*argv, "-r") || !strcmp(*argv, "--raw-output")) {
do_raw_output = 1;
} else if (!strcmp(*argv, "-x") || !strcmp(*argv, "--excerpt-output")) {
do_excerpt_output = 1;
} else if (!strcmp(*argv, "-H") || !strcmp(*argv, "--force-hardlinks")) {
do_hardlinks = 1;
} else if (!strcmp(*argv, "-Q") || !strcmp(*argv, "--no-integrity-checks")) {
do_integrity_checks = 0;
} else if (!strcmp(*argv, "--unlock")) {
do_forced_unlock = 1;
} else if (!strcmp(*argv, "-F") ||
!strcmp(*argv, "--fast-index")) {
do_fast_index = 1;
} else if (!strcmp(*argv, "--force-hash-key-new-database")) {
++argv, --argc;
if (!argc) {
fprintf(stderr, "No hash key given after --force-hash-key-new-database\n");
exit(1);
}
if ( 1 != sscanf(*argv, "%u", &forced_hash_key) )
{
fprintf(stderr, "Hash key given after --force-hash-key-new-database could not be parsed\n");
exit(1);
}
} else if (!strcmp(*argv, "-v") || !strcmp(*argv, "--verbose")) {
verbose = 1;
} else if (!strcmp(*argv, "-V") || !strcmp(*argv, "--version")) {
print_version();
exit(0);
} else if (!strcmp(*argv, "-h") ||
!strcmp(*argv, "--help")) {
do_help = 1;
} else if (!strcmp(*argv, "--")) {
/* End of args */
argc--;
argv++;
break;
} else if ((*argv)[0] == '-') {
fprintf(stderr, "Unrecognized option %s\n", *argv);
exit(3);
} else {
/* standard args start */
break;
}
}
if (do_help) {
usage();
exit(0);
}
if (verbose) {
print_copyright();
}
if (*argv) {
/* There are still args to process */
do_search = 1;
}
parse_rc_file(arg_rc_file_path);
if (getenv("MAIRIX_FOLDER_BASE")) {
folder_base = getenv("MAIRIX_FOLDER_BASE");
}
if (getenv("MAIRIX_MAILDIR_FOLDERS")) {
maildir_folders = getenv("MAIRIX_MAIDIR_FOLDERS");
}
if (getenv("MAIRIX_MH_FOLDERS")) {
mh_folders = getenv("MAIRIX_MH_FOLDERS");
}
if ((e = getenv("MAIRIX_MBOXEN"))) {
mboxen = e;
}
if (getenv("MAIRIX_MFOLDER")) {
mfolder = getenv("MAIRIX_MFOLDER");
}
if (getenv("MAIRIX_DATABASE")) {
database_path = getenv("MAIRIX_DATABASE");
}
if (arg_mfolder) {
mfolder = arg_mfolder;
}
if (skip_integrity_checks) {
do_integrity_checks = 0;
}
if (follow_mbox_symlinks) {
do_mbox_symlinks = 1;
}
if (!folder_base) {
fprintf(stderr, "No folder_base/MAIRIX_FOLDER_BASE set\n");
exit(2);
}
if (!database_path) {
fprintf(stderr, "No database/MAIRIX_DATABASE set\n");
exit(2);
}
if (do_raw_output) {
output_folder_type = FT_RAW;
} else if (do_excerpt_output) {
output_folder_type = FT_EXCERPT;
}
if (omit) {
omit_globs = colon_sep_string_to_globber_array(omit);
} else {
omit_globs = NULL;
}
/* Lock database.
* Prevent concurrent updates due to parallel indexing (e.g. due to stuck
* cron jobs).
* Prevent concurrent searching and indexing. */
signal(SIGHUP, handlesig);
signal(SIGINT, handlesig);
signal(SIGQUIT, handlesig);
lock_database(database_path, do_forced_unlock);
if (do_dump) {
dump_database(database_path);
result = 0;
} else if (do_search) {
int len;
char *complete_mfolder;
enum filetype ftype;
if (!mfolder) {
switch (output_folder_type) {
case FT_RAW:
case FT_EXCERPT:
break;
default:
fprintf(stderr, "No mfolder/MAIRIX_MFOLDER set\n");
unlock_and_exit(2);
}
mfolder = new_string("");
}
if (output_folder_type == FT_IMAP) {
complete_mfolder = new_string(mfolder);
} else {
/* complete_mfolder is needed by search_top() and member_of() so
compute it once here rather than in search_top() as well */
if ((mfolder[0] == '/') ||
((mfolder[0] == '.') && (mfolder[1] == '/'))) {
complete_mfolder = new_string(mfolder);
} else {
len = strlen(folder_base) + strlen(mfolder) + 2;
complete_mfolder = new_array(char, len);
strcpy(complete_mfolder, folder_base);
strcat(complete_mfolder, "/");
strcat(complete_mfolder, mfolder);
}
}
/* check whether mfolder output would destroy a mail folder or mbox */
switch (output_folder_type) {
case FT_RAW:
case FT_EXCERPT:
break;
case FT_IMAP:
/* the same check as below could be implemented in the future */
break;
default:
if ((member_of(complete_mfolder,folder_base, maildir_folders, FT_MAILDIR, omit_globs)||
member_of (complete_mfolder, folder_base, mh_folders, FT_MH, omit_globs) ||
member_of (complete_mfolder, folder_base, mboxen, FT_MBOX, omit_globs))) {
fprintf (stderr,
"You asked search results to go to the folder '%s'.\n"
"That folder appears to be one of the indexed mail folders!\n"
"For your own good, I refuse to output search results to an indexed mail folder.\n",
mfolder);
unlock_and_exit(3);
}
}
ftype = classify_file(database_path);
if (ftype != M_FILE) {
fprintf(stderr, "No database file '%s' is present.\nYou need to do an indexing run first.\n",
database_path);
unlock_and_exit(3);
}
result = search_top(do_threads, do_augment, database_path, complete_mfolder, argv, output_folder_type, verbose, imap_pipe, imap_server, imap_username, imap_password, sort_by_date);
} else {
enum filetype ftype;
if (imap_pipe && imap_server) {
fprintf(stderr, "specify one of imap_pipe or imap_server, not both\n");
unlock_and_exit(2);
}
if (imap_folders && (!(imap_pipe || imap_server))) {
fprintf(stderr, "If imap is given, imap_pipe OR imap_server is required\n");
imap_folders = NULL;
}
if (!maildir_folders && !mh_folders && !mboxen && !imap_folders) {
fprintf(stderr, "No [mh_]folders/mboxen/imap/MAIRIX_[MH_]FOLDERS set\n");
unlock_and_exit(2);
}
if (verbose) printf("Finding all currently existing messages...\n");
msgs = new_msgpath_array();
if (imap_folders) {
imapc = imap_start(imap_pipe, imap_server, imap_username, imap_password);
if (!imapc) unlock_and_exit(2);
build_imap_message_list(imap_folders, msgs, omit_globs, imapc);
}
if (maildir_folders) {
build_message_list(folder_base, maildir_folders, FT_MAILDIR, msgs, omit_globs);
}
if (mh_folders) {
build_message_list(folder_base, mh_folders, FT_MH, msgs, omit_globs);
}
sort_message_list(msgs);
/* The next call sorts the msgs array as part of looking for duplicates. */
if (check_message_list_for_duplicates(msgs)) {
fprintf(stderr, "Message list contains duplicates - check your 'folders' setting\n");
unlock_and_exit(2);
}
/* Try to open existing database */
ftype = classify_file(database_path);
if (ftype == M_FILE) {
if (verbose) printf("Reading existing database...\n");
db = new_database_from_file(database_path, do_integrity_checks);
if (verbose) printf("Loaded %d existing messages\n", db->n_msgs);
} else if (ftype == M_NONE) {
if (verbose) printf("Starting new database\n");
db = new_database( forced_hash_key );
} else {
fprintf(stderr, "database path %s is not a file; you can't put the database there\n", database_path);
unlock_and_exit(2);
}
build_mbox_lists(db, folder_base, mboxen, omit_globs, do_mbox_symlinks);
any_updates = update_database(db, msgs->paths, msgs->n, do_fast_index, imapc);
if (do_purge) {
any_purges = cull_dead_messages(db, do_integrity_checks);
}
if (any_updates || any_purges) {
/* For now write it every time. This is obviously the most reliable method. */
write_database(db, database_path, do_integrity_checks);
}
#if 0
get_db_stats(db);
#endif
free_database(db);
free_msgpath_array(msgs);
result = 0;
}
unlock_database();
return result;
}
/*}}}*/