3 * Copyright (c) 2013 Francois Fleuret
4 * Written by Francois Fleuret <francois@fleuret.org>
6 * This file is part of mymail.
8 * mymail is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
12 * mymail is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with mymail. If not, see <http://www.gnu.org/licenses/>.
24 This command is a dumb mail indexer. It can either (1) scan
25 directories containing mbox files, and create a db file containing
26 for each mail a list of fields computed from the header, or (2)
27 read such a db file and get all the mails matching regexp-defined
28 conditions on the fields, to create a resulting mbox file.
30 It is low-tech, simple, light and fast.
48 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
49 #define VERSION "0.9.2"
51 #define MAX_NB_SEARCH_CONDITIONS 32
53 #define BUFFER_SIZE 65536
54 #define TOKEN_BUFFER_SIZE 1024
56 #define LEADING_FROM_LINE_REGEXP_STRING "^From [^ ]* \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\) \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\) [ 123][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]\n$"
58 regex_t leading_from_line_regexp;
60 /* Global variables! */
64 char *default_search_field;
67 /********************************************************************/
82 static char *field_names[] = {
94 /********************************************************************/
96 struct search_condition {
100 time_t interval_start, interval_stop;
103 /********************************************************************/
105 struct parsable_field {
112 static struct parsable_field fields_to_parse[] = {
117 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
123 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
130 "^\\(to\\|cc\\|bcc\\): ",
131 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
138 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
145 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
150 /********************************************************************/
152 int xor(int a, int b) {
153 return (a && !b) || (!a && b);
156 char *parse_token(char *token_buffer, size_t token_buffer_size,
157 char separator, char *string) {
158 char *u = token_buffer;
159 while(u < token_buffer + token_buffer_size - 1 && *string &&
160 *string != separator) {
161 *(u++) = *(string++);
163 while(*string == separator) string++;
168 char *default_value(char *current_value,
169 const char *env_variable,
170 const char *hard_default_value) {
172 return current_value;
174 char *env_value = getenv(env_variable);
176 return strdup(env_value);
177 } else if(hard_default_value) {
178 return strdup(hard_default_value);
185 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
186 FILE *result = fopen(path, mode);
191 "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
192 path, comment, mode);
197 /*********************************************************************/
199 void print_version(FILE *out) {
200 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
203 void print_usage(FILE *out) {
205 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
207 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
209 fprintf(out, " -h, --help\n");
210 fprintf(out, " show this help\n");
211 fprintf(out, " -v, --version\n");
212 fprintf(out, " print the version number\n");
213 fprintf(out, " -q, --quiet\n");
214 fprintf(out, " do not print information during search\n");
215 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
216 fprintf(out, " set the db filename pattern for recursive search\n");
217 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
218 fprintf(out, " set the db root path for recursive search\n");
219 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
220 fprintf(out, " set the semicolon-separated list of db files for search\n");
221 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
222 fprintf(out, " set the mbox filename pattern for recursive search\n");
223 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
224 fprintf(out, " search for matching mails in the db file\n");
225 fprintf(out, " -d <db filename>, --db-file-generate <db filename>\n");
226 fprintf(out, " set the db filename for indexing\n");
227 fprintf(out, " -i, --index\n");
228 fprintf(out, " index mails\n");
229 fprintf(out, " -o <output filename>, --output <output filename>\n");
230 fprintf(out, " set the result file, use stdout if unset\n");
231 fprintf(out, " -a <search field>, --default-search <search field>\n");
232 fprintf(out, " set the default search field\n");
235 /*********************************************************************/
237 time_t time_for_past_day(int day) {
243 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
244 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
247 /*********************************************************************/
249 int ignore_entry(const char *name) {
251 strcmp(name, ".") == 0 ||
252 strcmp(name, "..") == 0 ||
253 (ignore_dot_files && name[0] == '.' && name[1] != '/');
256 int is_a_leading_from_line(char *mbox_line) {
261 The mbox man page in qmail documentation states:
263 > The reader should not attempt to take advantage of the fact
264 > that every From_ line (past the beginning of the file) is
265 > preceded by a blank line.
269 strncmp(mbox_line, "From ", 5) == 0 &&
270 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
273 int mbox_line_match_search(struct search_condition *condition,
274 int mbox_id, char *mbox_value) {
276 if(condition->field_id == ID_INTERVAL) {
277 if(mbox_id == ID_LEADING_LINE) {
283 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
284 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
287 return (t >= condition->interval_start &&
288 (condition->interval_stop == 0 ||
289 t <= condition->interval_stop));
297 (condition->field_id == mbox_id)
301 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
302 mbox_id == ID_FROM ||
306 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
312 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
316 void update_body_hits(char *mail_filename, int position_in_mail,
317 int nb_search_conditions, struct search_condition *search_conditions,
318 int nb_body_conditions,
322 char raw_mbox_line[BUFFER_SIZE];
328 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
330 fseek(mail_file, position_in_mail, SEEK_SET);
332 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
333 while(nb_body_hits < nb_body_conditions) {
334 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
335 /* if(last_mbox_line_was_empty) { header = 0; } */
337 if(raw_mbox_line[0] == '\n') { header = 0; }
340 for(n = 0; n < nb_search_conditions; n++) {
341 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
343 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
351 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
352 (is_a_leading_from_line(raw_mbox_line)))
360 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
362 char raw_mbox_line[BUFFER_SIZE];
365 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
366 fseek(mail_file, position_in_mail, SEEK_SET);
368 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
369 fprintf(output_file, "%s", raw_mbox_line);
371 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
372 (is_a_leading_from_line(raw_mbox_line))
375 fprintf(output_file, "%s", raw_mbox_line);
382 int search_in_db(const char *db_filename,
383 int nb_search_conditions,
384 struct search_condition *search_conditions,
387 int hits[MAX_NB_SEARCH_CONDITIONS];
388 char raw_db_line[BUFFER_SIZE];
389 char current_mail_filename[PATH_MAX + 1];
390 unsigned long int current_position_in_mail;
391 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
393 int already_written, m, n;
394 int nb_body_conditions, nb_fulfilled_body_conditions;
396 int nb_extracted_mails;
398 nb_extracted_mails = 0;
401 printf("Searching in '%s' ... ", db_filename);
405 db_file = safe_fopen(db_filename, "r", "index file for search");
407 /* First, check the db file leading line integrity */
409 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
410 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
412 "mymail: Header line in '%s' does not match the mymail db format.\n",
418 "mymail: Cannot read the header line in '%s'.\n",
423 /* Then parse the said db file */
425 current_position_in_mail = 0;
428 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
430 nb_body_conditions = 0;
431 for(n = 0; n < nb_search_conditions; n++) {
432 if(search_conditions[n].field_id == ID_BODY) {
433 nb_body_conditions++;
437 strcpy(current_mail_filename, "");
439 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
440 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
442 if(strcmp("mail", mbox_name) == 0) {
443 char position_in_file_string[TOKEN_BUFFER_SIZE];
445 if(current_mail_filename[0]) {
447 /* We first check all conditions but the body ones */
449 for(n = 0; n < nb_search_conditions &&
450 ((search_conditions[n].field_id == ID_BODY) ||
451 xor(hits[n], search_conditions[n].negation)); n++);
453 if(n == nb_search_conditions) {
455 /* Now check the body ones */
457 if(nb_body_conditions > 0) {
458 update_body_hits(current_mail_filename, current_position_in_mail,
459 nb_search_conditions, search_conditions,
464 nb_fulfilled_body_conditions = 0;
466 for(n = 0; n < nb_search_conditions; n++) {
467 if(search_conditions[n].field_id == ID_BODY &&
468 xor(hits[n], search_conditions[n].negation)) {
469 nb_fulfilled_body_conditions++;
473 if(nb_body_conditions == nb_fulfilled_body_conditions) {
474 nb_extracted_mails++;
475 extract_mail(current_mail_filename, current_position_in_mail, output_file);
480 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
482 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
483 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
484 current_position_in_mail = atol(position_in_file_string);
490 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
491 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
495 for(n = 0; n < nb_search_conditions; n++) {
496 hits[n] |= mbox_line_match_search(&search_conditions[n],
497 mbox_id, mbox_value);
509 return nb_extracted_mails;
512 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
513 int nb_search_conditions,
514 struct search_condition *search_conditions,
517 struct dirent *dir_e;
519 char subname[PATH_MAX + 1];
520 int nb_extracted_mails = 0;
522 if(lstat(entry_name, &sb) != 0) {
524 "mymail: Cannot stat \"%s\": %s\n",
530 dir = opendir(entry_name);
533 while((dir_e = readdir(dir))) {
534 if(!ignore_entry(dir_e->d_name)) {
535 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
536 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
537 nb_search_conditions, search_conditions,
545 const char *s = entry_name, *filename = entry_name;
546 while(*s) { if(*s == '/') { filename = s+1; } s++; }
548 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
549 nb_extracted_mails +=
550 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
554 return nb_extracted_mails;
557 /*********************************************************************/
559 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
560 char *raw_mbox_line, FILE *db_file) {
563 for(f = 0; f < nb_fields_to_parse; f++) {
564 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
565 fprintf(db_file, "%s %s\n",
566 field_names[fields_to_parse[f].id],
567 raw_mbox_line + matches.rm_eo);
572 void index_mbox(const char *mbox_filename,
573 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
575 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
576 char *end_of_full_line;
578 int in_header, new_header;
579 unsigned long int position_in_file;
581 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
586 position_in_file = 0;
587 end_of_full_line = 0;
590 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
591 if(is_a_leading_from_line(raw_mbox_line)) {
594 "Got a ^\"From \" in the header in %s:%lu.\n",
595 mbox_filename, position_in_file);
596 fprintf(stderr, "%s", raw_mbox_line);
597 if(paranoid) { exit(EXIT_FAILURE); }
601 } else if(raw_mbox_line[0] == '\n') {
602 if(in_header) { in_header = 0; }
607 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
611 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
612 char *start = raw_mbox_line;
613 while(*start == ' ' || *start == '\t') start++;
614 *(end_of_full_line++) = ' ';
615 strcpy(end_of_full_line, start);
616 while(*end_of_full_line && *end_of_full_line != '\n') {
619 *end_of_full_line = '\0';
624 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
625 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
627 "Header line syntax error %s:%lu.\n",
628 mbox_filename, position_in_file);
629 fprintf(stderr, "%s", raw_mbox_line);
634 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
637 end_of_full_line = full_line;
638 strcpy(end_of_full_line, raw_mbox_line);
639 while(*end_of_full_line && *end_of_full_line != '\n') {
642 *end_of_full_line = '\0';
647 position_in_file += strlen(raw_mbox_line);
653 void recursive_index_mbox(FILE *db_file,
654 const char *entry_name, regex_t *mbox_filename_regexp,
655 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
657 struct dirent *dir_e;
659 char subname[PATH_MAX + 1];
661 if(lstat(entry_name, &sb) != 0) {
663 "mymail: Cannot stat \"%s\": %s\n",
669 dir = opendir(entry_name);
672 while((dir_e = readdir(dir))) {
673 if(!ignore_entry(dir_e->d_name)) {
674 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
675 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
676 nb_fields_to_parse, fields_to_parse);
681 const char *s = entry_name, *filename = s;
682 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
683 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
684 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
689 /*********************************************************************/
691 /* For long options that have no equivalent short option, use a
692 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
694 OPT_BASH_MODE = CHAR_MAX + 1
697 static struct option long_options[] = {
698 { "help", no_argument, 0, 'h' },
699 { "version", no_argument, 0, 'v' },
700 { "quiet", no_argument, 0, 'q' },
701 { "db-file-generate", 1, 0, 'd' },
702 { "db-pattern", 1, 0, 'p' },
703 { "db-root", 1, 0, 'r' },
704 { "db-list", 1, 0, 'l' },
705 { "mbox-pattern", 1, 0, 'm' },
706 { "search", 1, 0, 's' },
707 { "index", 0, 0, 'i' },
708 { "output", 1, 0, 'o' },
709 { "default-search", 1, 0, 'a' },
713 /*********************************************************************/
715 void init_condition(struct search_condition *condition, char *full_string) {
716 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
720 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
721 search_field = full_search_field;
723 if(search_field[0] == '!') {
725 condition->negation = 1;
727 condition->negation = 0;
732 if(strcmp(search_field, "8h") == 0) {
733 condition->field_id = ID_INTERVAL;
734 condition->interval_start = time(0) - 3600 * 8;
735 condition->interval_stop = 0;
738 else if(strcmp(search_field, "24h") == 0 ||
739 strcmp(search_field, "today") == 0) {
740 condition->field_id = ID_INTERVAL;
741 condition->interval_start = time(0) - 3600 * 24;
742 condition->interval_stop = 0;
745 else if(strcmp(search_field, "week") == 0) {
746 condition->field_id = ID_INTERVAL;
747 condition->interval_start = time(0) - 3600 * 24 * 7;
748 condition->interval_stop = 0;
751 else if(strcmp(search_field, "month") == 0) {
752 condition->field_id = ID_INTERVAL;
753 condition->interval_start = time(0) - 3600 * 24 * 31;
754 condition->interval_stop = 0;
757 else if(strcmp(search_field, "year") == 0) {
758 condition->field_id = ID_INTERVAL;
759 condition->interval_start = time(0) - 3600 * 24 * 365;
760 condition->interval_stop = 0;
765 else if(strcmp(search_field, "yesterday") == 0) {
766 condition->field_id = ID_INTERVAL;
767 condition->interval_start = time(0) - 2 * 3600 * 24;
768 condition->interval_stop = condition->interval_start + 3600 * 24;
773 else if(strcmp(search_field, "monday") == 0) {
774 condition->field_id = ID_INTERVAL;
775 condition->interval_start = time_for_past_day(1);
776 condition->interval_stop = condition->interval_start + 3600 * 24;
779 else if(strcmp(search_field, "tuesday") == 0) {
780 condition->field_id = ID_INTERVAL;
781 condition->interval_start = time_for_past_day(2);
782 condition->interval_stop = condition->interval_start + 3600 * 24;
785 else if(strcmp(search_field, "wednesday") == 0) {
786 condition->field_id = ID_INTERVAL;
787 condition->interval_start = time_for_past_day(3);
788 condition->interval_stop = condition->interval_start + 3600 * 24;
791 else if(strcmp(search_field, "thursday") == 0) {
792 condition->field_id = ID_INTERVAL;
793 condition->interval_start = time_for_past_day(4);
794 condition->interval_stop = condition->interval_start + 3600 * 24;
797 else if(strcmp(search_field, "friday") == 0) {
798 condition->field_id = ID_INTERVAL;
799 condition->interval_start = time_for_past_day(5);
800 condition->interval_stop = condition->interval_start + 3600 * 24;
803 else if(strcmp(search_field, "saturday") == 0) {
804 condition->field_id = ID_INTERVAL;
805 condition->interval_start = time_for_past_day(6);
806 condition->interval_stop = condition->interval_start + 3600 * 24;
809 else if(strcmp(search_field, "sunday") == 0) {
810 condition->field_id = ID_INTERVAL;
811 condition->interval_start = time_for_past_day(7);
812 condition->interval_stop = condition->interval_start + 3600 * 24;
817 /* header-related conditions */
819 condition->field_id = -1;
821 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
822 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
823 condition->field_id = m;
827 if(condition->field_id == -1) {
828 if(default_search_field) {
829 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
830 if(strncmp(field_names[m],
831 default_search_field, strlen(default_search_field)) == 0) {
832 condition->field_id = m;
835 string = full_string;
839 if(condition->field_id == -1) {
841 "mymail: Syntax error in field name \"%s\".\n",
846 if(regcomp(&condition->regexp,
850 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
852 field_names[condition->field_id]);
858 void free_condition(struct search_condition *condition) {
859 if(condition->field_id != ID_INTERVAL) {
860 regfree(&condition->regexp);
864 /*********************************************************************/
865 /*********************************************************************/
866 /*********************************************************************/
868 int main(int argc, char **argv) {
870 char *db_filename_regexp_string;
872 char *db_filename_list;
873 char *mbox_filename_regexp_string;
874 char output_filename[PATH_MAX + 1];
876 int error = 0, show_help = 0;
877 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
880 int nb_search_conditions;
881 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
883 if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
885 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
892 db_filename_regexp_string = 0;
894 db_filename_list = 0;
895 mbox_filename_regexp_string = 0;
897 default_search_field = 0;
898 ignore_dot_files = 1;
900 setlocale(LC_ALL, "");
902 nb_search_conditions = 0;
904 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
905 long_options, NULL)) != -1) {
914 print_version(stdout);
927 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
930 db_filename = strdup(optarg);
934 if(db_filename_regexp_string) {
935 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
938 db_filename_regexp_string = strdup(optarg);
942 if(mbox_filename_regexp_string) {
943 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
946 mbox_filename_regexp_string = strdup(optarg);
950 strncpy(output_filename, optarg, PATH_MAX);
955 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
958 db_root_path = strdup(optarg);
962 if(db_filename_list) {
963 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
966 db_filename_list = strdup(optarg);
970 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
971 fprintf(stderr, "mymail: Too many search patterns.\n");
974 init_condition(&search_conditions[nb_search_conditions], optarg);
975 nb_search_conditions++;
979 default_search_field = optarg;
988 /* Set all the values that may defined in the arguments, through
989 environment variables, or hard-coded */
991 db_filename = default_value(db_filename,
995 db_filename_regexp_string = default_value(db_filename_regexp_string,
999 db_root_path = default_value(db_root_path,
1003 db_filename_list = default_value(db_filename_list,
1007 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1008 "MYMAIL_MBOX_PATTERN",
1011 /* Start the processing */
1014 print_usage(stderr);
1019 print_usage(stdout);
1027 regex_t mbox_filename_regexp_static;
1028 regex_t *mbox_filename_regexp;
1030 if(mbox_filename_regexp_string) {
1031 if(regcomp(&mbox_filename_regexp_static,
1032 mbox_filename_regexp_string,
1035 "mymail: Syntax error in regexp \"%s\".\n",
1036 mbox_filename_regexp_string);
1039 mbox_filename_regexp = &mbox_filename_regexp_static;
1041 mbox_filename_regexp = 0;
1044 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1046 for(f = 0; f < nb_fields_to_parse; f++) {
1047 if(regcomp(&fields_to_parse[f].regexp,
1048 fields_to_parse[f].regexp_string,
1049 fields_to_parse[f].cflags)) {
1051 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1052 fields_to_parse[f].regexp_string,
1053 field_names[fields_to_parse[f].id]);
1058 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1060 while(optind < argc) {
1061 recursive_index_mbox(db_file,
1062 argv[optind], mbox_filename_regexp,
1063 nb_fields_to_parse, fields_to_parse);
1070 if(mbox_filename_regexp) {
1071 regfree(mbox_filename_regexp);
1074 for(f = 0; f < nb_fields_to_parse; f++) {
1075 regfree(&fields_to_parse[f].regexp);
1084 int nb_extracted_mails = 0;
1086 if(output_filename[0]) {
1087 output_file = safe_fopen(output_filename, "w", "result mbox");
1089 output_file = stdout;
1093 if(nb_search_conditions > 0) {
1095 /* Recursive search if db_root_path is set */
1098 regex_t db_filename_regexp;
1099 if(regcomp(&db_filename_regexp,
1100 db_filename_regexp_string,
1103 "mymail: Syntax error in regexp \"%s\".\n",
1104 db_filename_regexp_string);
1108 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1109 nb_search_conditions, search_conditions,
1112 regfree(&db_filename_regexp);
1115 /* Search in all db files listed in db_filename_list */
1117 if(db_filename_list) {
1118 char db_filename[PATH_MAX + 1];
1121 s = db_filename_list;
1124 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1126 if(db_filename[0]) {
1127 nb_extracted_mails +=
1128 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1133 /* Search in all db files listed in the command arguments */
1135 while(optind < argc) {
1136 nb_extracted_mails +=
1137 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1143 if(nb_extracted_mails > 0) {
1144 printf("Found %d matching mails.\n", nb_extracted_mails);
1146 printf("No matching mail found.\n");
1150 fflush(output_file);
1152 if(output_file != stdout) {
1153 fclose(output_file);
1157 for(n = 0; n < nb_search_conditions; n++) {
1158 free_condition(&search_conditions[n]);
1162 free(db_filename_regexp_string);
1164 free(db_filename_list);
1165 free(mbox_filename_regexp_string);
1167 regfree(&leading_from_line_regexp);