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 /********************************************************************/
187 /* malloc with error checking. */
189 void *safe_malloc(size_t n) {
193 "mymail: cannot allocate memory: %s\n", strerror(errno));
199 /*********************************************************************/
201 void print_version(FILE *out) {
202 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
205 void print_usage(FILE *out) {
207 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
209 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
211 fprintf(out, " -h, --help\n");
212 fprintf(out, " show this help\n");
213 fprintf(out, " -v, --version\n");
214 fprintf(out, " print the version number\n");
215 fprintf(out, " -q, --quiet\n");
216 fprintf(out, " do not print information during search\n");
217 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
218 fprintf(out, " set the db filename pattern for recursive search\n");
219 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
220 fprintf(out, " set the db root path for recursive search\n");
221 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
222 fprintf(out, " set the semicolon-separated list of db files for search\n");
223 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
224 fprintf(out, " set the mbox filename pattern for recursive search\n");
225 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
226 fprintf(out, " search for matching mails in the db file\n");
227 fprintf(out, " -d <db filename>, --db-file-generate <db filename>\n");
228 fprintf(out, " set the db filename for indexing\n");
229 fprintf(out, " -i, --index\n");
230 fprintf(out, " index mails\n");
231 fprintf(out, " -o <output filename>, --output <output filename>\n");
232 fprintf(out, " set the result file, use stdout if unset\n");
233 fprintf(out, " -a <search field>, --default-search <search field>\n");
234 fprintf(out, " set the default search field\n");
237 /*********************************************************************/
239 time_t time_for_past_day(int day) {
245 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
246 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
249 /*********************************************************************/
251 int ignore_entry(const char *name) {
253 strcmp(name, ".") == 0 ||
254 strcmp(name, "..") == 0 ||
255 (ignore_dot_files && name[0] == '.' && name[1] != '/');
258 int is_a_leading_from_line(char *mbox_line) {
263 The mbox man page in qmail documentation states:
265 > The reader should not attempt to take advantage of the fact
266 > that every From_ line (past the beginning of the file) is
267 > preceded by a blank line.
271 strncmp(mbox_line, "From ", 5) == 0 &&
272 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
275 int mbox_line_match_search(struct search_condition *condition,
276 int mbox_id, char *mbox_value) {
278 if(condition->field_id == ID_INTERVAL) {
279 if(mbox_id == ID_LEADING_LINE) {
285 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
286 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
289 return (t >= condition->interval_start &&
290 (condition->interval_stop == 0 ||
291 t <= condition->interval_stop));
299 (condition->field_id == mbox_id)
303 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
304 mbox_id == ID_FROM ||
308 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
314 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
318 void update_body_hits(char *mail_filename, int position_in_mail,
319 int nb_search_conditions, struct search_condition *search_conditions,
320 int nb_body_conditions,
324 char raw_mbox_line[BUFFER_SIZE];
330 mail_file = fopen(mail_filename, "r");
334 "mymail: Cannot open mbox '%s' for body scan.\n",
339 fseek(mail_file, position_in_mail, SEEK_SET);
341 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
342 while(nb_body_hits < nb_body_conditions) {
343 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
344 /* if(last_mbox_line_was_empty) { header = 0; } */
346 if(raw_mbox_line[0] == '\n') { header = 0; }
349 for(n = 0; n < nb_search_conditions; n++) {
350 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
352 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
360 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
361 (is_a_leading_from_line(raw_mbox_line)))
369 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
371 char raw_mbox_line[BUFFER_SIZE];
374 mail_file = fopen(mail_filename, "r");
378 "mymail: Cannot open mbox '%s' for mail extraction.\n",
383 fseek(mail_file, position_in_mail, SEEK_SET);
385 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
386 fprintf(output_file, "%s", raw_mbox_line);
388 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
389 (is_a_leading_from_line(raw_mbox_line))
392 fprintf(output_file, "%s", raw_mbox_line);
399 int search_in_db(const char *db_filename,
400 int nb_search_conditions,
401 struct search_condition *search_conditions,
404 int hits[MAX_NB_SEARCH_CONDITIONS];
405 char raw_db_line[BUFFER_SIZE];
406 char current_mail_filename[PATH_MAX + 1];
407 unsigned long int current_position_in_mail;
408 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
410 int already_written, m, n;
411 int nb_body_conditions, nb_fulfilled_body_conditions;
413 int nb_extracted_mails;
415 nb_extracted_mails = 0;
418 printf("Searching in '%s' ... ", db_filename);
422 db_file = fopen(db_filename, "r");
426 "mymail: Cannot open \"%s\" for reading: %s\n",
432 /* First, check the db file leading line integrity */
434 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
435 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
437 "mymail: Header line in '%s' does not match the mymail db format.\n",
443 "mymail: Cannot read the header line in '%s'.\n",
448 /* Then parse the said db file */
450 current_position_in_mail = 0;
453 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
455 nb_body_conditions = 0;
456 for(n = 0; n < nb_search_conditions; n++) {
457 if(search_conditions[n].field_id == ID_BODY) {
458 nb_body_conditions++;
462 strcpy(current_mail_filename, "");
464 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
465 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
467 if(strcmp("mail", mbox_name) == 0) {
468 char position_in_file_string[TOKEN_BUFFER_SIZE];
470 if(current_mail_filename[0]) {
472 /* We first check all conditions but the body ones */
474 for(n = 0; n < nb_search_conditions &&
475 ((search_conditions[n].field_id == ID_BODY) ||
476 xor(hits[n], search_conditions[n].negation)); n++);
478 if(n == nb_search_conditions) {
480 /* Now check the body ones */
482 if(nb_body_conditions > 0) {
483 update_body_hits(current_mail_filename, current_position_in_mail,
484 nb_search_conditions, search_conditions,
489 nb_fulfilled_body_conditions = 0;
491 for(n = 0; n < nb_search_conditions; n++) {
492 if(search_conditions[n].field_id == ID_BODY &&
493 xor(hits[n], search_conditions[n].negation)) {
494 nb_fulfilled_body_conditions++;
498 if(nb_body_conditions == nb_fulfilled_body_conditions) {
499 nb_extracted_mails++;
500 extract_mail(current_mail_filename, current_position_in_mail, output_file);
505 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
507 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
508 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
509 current_position_in_mail = atol(position_in_file_string);
515 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
516 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
520 for(n = 0; n < nb_search_conditions; n++) {
521 hits[n] |= mbox_line_match_search(&search_conditions[n],
522 mbox_id, mbox_value);
534 return nb_extracted_mails;
537 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
538 int nb_search_conditions,
539 struct search_condition *search_conditions,
542 struct dirent *dir_e;
544 char subname[PATH_MAX + 1];
545 int nb_extracted_mails = 0;
547 if(lstat(entry_name, &sb) != 0) {
549 "mymail: Cannot stat \"%s\": %s\n",
555 dir = opendir(entry_name);
558 while((dir_e = readdir(dir))) {
559 if(!ignore_entry(dir_e->d_name)) {
560 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
561 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
562 nb_search_conditions, search_conditions,
570 const char *s = entry_name, *filename = entry_name;
571 while(*s) { if(*s == '/') { filename = s+1; } s++; }
573 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
574 nb_extracted_mails +=
575 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
579 return nb_extracted_mails;
582 /*********************************************************************/
584 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
585 char *raw_mbox_line, FILE *db_file) {
588 for(f = 0; f < nb_fields_to_parse; f++) {
589 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
590 fprintf(db_file, "%s %s\n",
591 field_names[fields_to_parse[f].id],
592 raw_mbox_line + matches.rm_eo);
597 void index_mbox(const char *mbox_filename,
598 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
600 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
601 char *end_of_full_line;
603 int in_header, new_header;
604 unsigned long int position_in_file;
606 file = fopen(mbox_filename, "r");
609 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
610 if(paranoid) { exit(EXIT_FAILURE); }
617 position_in_file = 0;
618 end_of_full_line = 0;
621 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
622 if(is_a_leading_from_line(raw_mbox_line)) {
625 "Got a ^\"From \" in the header in %s:%lu.\n",
626 mbox_filename, position_in_file);
627 fprintf(stderr, "%s", raw_mbox_line);
628 if(paranoid) { exit(EXIT_FAILURE); }
632 } else if(raw_mbox_line[0] == '\n') {
633 if(in_header) { in_header = 0; }
638 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
642 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
643 char *start = raw_mbox_line;
644 while(*start == ' ' || *start == '\t') start++;
645 *(end_of_full_line++) = ' ';
646 strcpy(end_of_full_line, start);
647 while(*end_of_full_line && *end_of_full_line != '\n') {
650 *end_of_full_line = '\0';
655 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
656 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
658 "Header line syntax error %s:%lu.\n",
659 mbox_filename, position_in_file);
660 fprintf(stderr, "%s", raw_mbox_line);
665 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
668 end_of_full_line = full_line;
669 strcpy(end_of_full_line, raw_mbox_line);
670 while(*end_of_full_line && *end_of_full_line != '\n') {
673 *end_of_full_line = '\0';
678 position_in_file += strlen(raw_mbox_line);
684 void recursive_index_mbox(FILE *db_file,
685 const char *entry_name, regex_t *mbox_filename_regexp,
686 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
688 struct dirent *dir_e;
690 char subname[PATH_MAX + 1];
692 if(lstat(entry_name, &sb) != 0) {
694 "mymail: Cannot stat \"%s\": %s\n",
700 dir = opendir(entry_name);
703 while((dir_e = readdir(dir))) {
704 if(!ignore_entry(dir_e->d_name)) {
705 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
706 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
707 nb_fields_to_parse, fields_to_parse);
712 const char *s = entry_name, *filename = s;
713 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
714 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
715 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
720 /*********************************************************************/
722 /* For long options that have no equivalent short option, use a
723 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
725 OPT_BASH_MODE = CHAR_MAX + 1
728 static struct option long_options[] = {
729 { "help", no_argument, 0, 'h' },
730 { "version", no_argument, 0, 'v' },
731 { "quiet", no_argument, 0, 'q' },
732 { "db-file-generate", 1, 0, 'd' },
733 { "db-pattern", 1, 0, 'p' },
734 { "db-root", 1, 0, 'r' },
735 { "db-list", 1, 0, 'l' },
736 { "mbox-pattern", 1, 0, 'm' },
737 { "search", 1, 0, 's' },
738 { "index", 0, 0, 'i' },
739 { "output", 1, 0, 'o' },
740 { "default-search", 1, 0, 'a' },
744 /*********************************************************************/
746 void init_condition(struct search_condition *condition, char *full_string) {
747 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
751 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
752 search_field = full_search_field;
754 if(search_field[0] == '!') {
756 condition->negation = 1;
758 condition->negation = 0;
763 if(strcmp(search_field, "8h") == 0) {
764 condition->field_id = ID_INTERVAL;
765 condition->interval_start = time(0) - 3600 * 8;
766 condition->interval_stop = 0;
769 else if(strcmp(search_field, "24h") == 0 ||
770 strcmp(search_field, "today") == 0) {
771 condition->field_id = ID_INTERVAL;
772 condition->interval_start = time(0) - 3600 * 24;
773 condition->interval_stop = 0;
776 else if(strcmp(search_field, "week") == 0) {
777 condition->field_id = ID_INTERVAL;
778 condition->interval_start = time(0) - 3600 * 24 * 7;
779 condition->interval_stop = 0;
782 else if(strcmp(search_field, "month") == 0) {
783 condition->field_id = ID_INTERVAL;
784 condition->interval_start = time(0) - 3600 * 24 * 31;
785 condition->interval_stop = 0;
788 else if(strcmp(search_field, "year") == 0) {
789 condition->field_id = ID_INTERVAL;
790 condition->interval_start = time(0) - 3600 * 24 * 365;
791 condition->interval_stop = 0;
796 else if(strcmp(search_field, "yesterday") == 0) {
797 condition->field_id = ID_INTERVAL;
798 condition->interval_start = time(0) - 2 * 3600 * 24;
799 condition->interval_stop = condition->interval_start + 3600 * 24;
804 else if(strcmp(search_field, "monday") == 0) {
805 condition->field_id = ID_INTERVAL;
806 condition->interval_start = time_for_past_day(1);
807 condition->interval_stop = condition->interval_start + 3600 * 24;
810 else if(strcmp(search_field, "tuesday") == 0) {
811 condition->field_id = ID_INTERVAL;
812 condition->interval_start = time_for_past_day(2);
813 condition->interval_stop = condition->interval_start + 3600 * 24;
816 else if(strcmp(search_field, "wednesday") == 0) {
817 condition->field_id = ID_INTERVAL;
818 condition->interval_start = time_for_past_day(3);
819 condition->interval_stop = condition->interval_start + 3600 * 24;
822 else if(strcmp(search_field, "thursday") == 0) {
823 condition->field_id = ID_INTERVAL;
824 condition->interval_start = time_for_past_day(4);
825 condition->interval_stop = condition->interval_start + 3600 * 24;
828 else if(strcmp(search_field, "friday") == 0) {
829 condition->field_id = ID_INTERVAL;
830 condition->interval_start = time_for_past_day(5);
831 condition->interval_stop = condition->interval_start + 3600 * 24;
834 else if(strcmp(search_field, "saturday") == 0) {
835 condition->field_id = ID_INTERVAL;
836 condition->interval_start = time_for_past_day(6);
837 condition->interval_stop = condition->interval_start + 3600 * 24;
840 else if(strcmp(search_field, "sunday") == 0) {
841 condition->field_id = ID_INTERVAL;
842 condition->interval_start = time_for_past_day(7);
843 condition->interval_stop = condition->interval_start + 3600 * 24;
848 /* header-related conditions */
850 condition->field_id = -1;
852 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
853 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
854 condition->field_id = m;
858 if(condition->field_id == -1) {
859 if(default_search_field) {
860 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
861 if(strncmp(field_names[m],
862 default_search_field, strlen(default_search_field)) == 0) {
863 condition->field_id = m;
866 string = full_string;
870 if(condition->field_id == -1) {
872 "mymail: Syntax error in field name \"%s\".\n",
877 if(regcomp(&condition->regexp,
881 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
883 field_names[condition->field_id]);
889 void free_condition(struct search_condition *condition) {
890 if(condition->field_id != ID_INTERVAL) {
891 regfree(&condition->regexp);
895 /*********************************************************************/
896 /*********************************************************************/
897 /*********************************************************************/
899 int main(int argc, char **argv) {
901 char *db_filename_regexp_string;
903 char *db_filename_list;
904 char *mbox_filename_regexp_string;
905 char output_filename[PATH_MAX + 1];
907 int error = 0, show_help = 0;
908 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
911 int nb_search_conditions;
912 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
914 if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
916 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
923 db_filename_regexp_string = 0;
925 db_filename_list = 0;
926 mbox_filename_regexp_string = 0;
928 default_search_field = 0;
929 ignore_dot_files = 1;
931 setlocale(LC_ALL, "");
933 nb_search_conditions = 0;
935 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
936 long_options, NULL)) != -1) {
945 print_version(stdout);
958 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
961 db_filename = strdup(optarg);
965 if(db_filename_regexp_string) {
966 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
969 db_filename_regexp_string = strdup(optarg);
973 if(mbox_filename_regexp_string) {
974 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
977 mbox_filename_regexp_string = strdup(optarg);
981 strncpy(output_filename, optarg, PATH_MAX);
986 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
989 db_root_path = strdup(optarg);
993 if(db_filename_list) {
994 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
997 db_filename_list = strdup(optarg);
1001 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1002 fprintf(stderr, "mymail: Too many search patterns.\n");
1005 init_condition(&search_conditions[nb_search_conditions], optarg);
1006 nb_search_conditions++;
1010 default_search_field = optarg;
1019 /* Set all the values that may defined in the arguments, through
1020 environment variables, or hard-coded */
1022 db_filename = default_value(db_filename,
1026 db_filename_regexp_string = default_value(db_filename_regexp_string,
1030 db_root_path = default_value(db_root_path,
1034 db_filename_list = default_value(db_filename_list,
1038 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1039 "MYMAIL_MBOX_PATTERN",
1042 /* Start the processing */
1045 print_usage(stderr);
1050 print_usage(stdout);
1058 regex_t mbox_filename_regexp_static;
1059 regex_t *mbox_filename_regexp;
1061 if(mbox_filename_regexp_string) {
1062 if(regcomp(&mbox_filename_regexp_static,
1063 mbox_filename_regexp_string,
1066 "mymail: Syntax error in regexp \"%s\".\n",
1067 mbox_filename_regexp_string);
1070 mbox_filename_regexp = &mbox_filename_regexp_static;
1072 mbox_filename_regexp = 0;
1075 db_file = fopen(db_filename, "w");
1079 "mymail: Cannot open \"%s\" for writing: %s\n",
1085 for(f = 0; f < nb_fields_to_parse; f++) {
1086 if(regcomp(&fields_to_parse[f].regexp,
1087 fields_to_parse[f].regexp_string,
1088 fields_to_parse[f].cflags)) {
1090 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1091 fields_to_parse[f].regexp_string,
1092 field_names[fields_to_parse[f].id]);
1097 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1099 while(optind < argc) {
1100 recursive_index_mbox(db_file,
1101 argv[optind], mbox_filename_regexp,
1102 nb_fields_to_parse, fields_to_parse);
1109 if(mbox_filename_regexp) {
1110 regfree(mbox_filename_regexp);
1113 for(f = 0; f < nb_fields_to_parse; f++) {
1114 regfree(&fields_to_parse[f].regexp);
1123 int nb_extracted_mails = 0;
1125 if(output_filename[0]) {
1126 output_file = fopen(output_filename, "w");
1130 "mymail: Cannot open result file \"%s\" for writing: %s\n",
1136 output_file = stdout;
1140 if(nb_search_conditions > 0) {
1142 /* Recursive search if db_root_path is set */
1145 regex_t db_filename_regexp;
1146 if(regcomp(&db_filename_regexp,
1147 db_filename_regexp_string,
1150 "mymail: Syntax error in regexp \"%s\".\n",
1151 db_filename_regexp_string);
1155 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1156 nb_search_conditions, search_conditions,
1159 regfree(&db_filename_regexp);
1162 /* Search in all db files listed in db_filename_list */
1164 if(db_filename_list) {
1165 char db_filename[PATH_MAX + 1];
1168 s = db_filename_list;
1171 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1173 if(db_filename[0]) {
1174 nb_extracted_mails +=
1175 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1180 /* Search in all db files listed in the command arguments */
1182 while(optind < argc) {
1183 nb_extracted_mails +=
1184 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1190 if(nb_extracted_mails > 0) {
1191 printf("Found %d matching mails.\n", nb_extracted_mails);
1193 printf("No matching mail found.\n");
1197 fflush(output_file);
1199 if(output_file != stdout) {
1200 fclose(output_file);
1204 for(n = 0; n < nb_search_conditions; n++) {
1205 free_condition(&search_conditions[n]);
1209 free(db_filename_regexp_string);
1211 free(db_filename_list);
1212 free(mbox_filename_regexp_string);
1214 regfree(&leading_from_line_regexp);