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.5"
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 /* Global variables! */
62 regex_t global_leading_from_line_regexp;
64 /********************************************************************/
79 static char *field_keys[] = {
91 /********************************************************************/
93 struct search_condition {
97 time_t interval_start, interval_stop;
100 /********************************************************************/
102 struct parsable_field {
109 static struct parsable_field fields_to_parse[] = {
114 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
120 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
121 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
127 "^\\(to\\|cc\\|bcc\\): ",
128 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
135 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
142 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
147 /********************************************************************/
149 int xor(int a, int b) {
150 return (a && !b) || (!a && b);
153 const char *parse_token(char *token_buffer, size_t token_buffer_size,
154 char separator, const char *string) {
155 char *u = token_buffer;
156 while(*string == separator) { string++; }
157 while(u < token_buffer + token_buffer_size - 1 && *string &&
158 *string != separator) {
159 *(u++) = *(string++);
161 while(*string == separator) { string++; }
166 char *default_value(char *current_value,
167 const char *env_variable,
168 const char *hard_default_value) {
170 return current_value;
172 char *env_value = getenv(env_variable);
174 return strdup(env_value);
175 } else if(hard_default_value) {
176 return strdup(hard_default_value);
183 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
184 FILE *result = fopen(path, mode);
189 "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
190 path, comment, mode);
195 /*********************************************************************/
197 void print_version(FILE *out) {
198 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
201 void print_usage(FILE *out) {
203 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
205 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
207 fprintf(out, " -h, --help\n");
208 fprintf(out, " show this help\n");
209 fprintf(out, " -v, --version\n");
210 fprintf(out, " print the version number\n");
211 fprintf(out, " -q, --quiet\n");
212 fprintf(out, " do not print information during search\n");
213 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
214 fprintf(out, " set the db filename pattern for recursive search\n");
215 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
216 fprintf(out, " set the db root path for recursive search\n");
217 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
218 fprintf(out, " set the semicolon-separated list of db files for search\n");
219 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
220 fprintf(out, " set the mbox filename pattern for recursive search\n");
221 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
222 fprintf(out, " search for matching mails in the db file\n");
223 fprintf(out, " -d <db filename>, --db-file-generate <db filename>\n");
224 fprintf(out, " set the db filename for indexing\n");
225 fprintf(out, " -i, --index\n");
226 fprintf(out, " index mails\n");
227 fprintf(out, " -o <output filename>, --output <output filename>\n");
228 fprintf(out, " set the result file, use stdout if unset\n");
229 fprintf(out, " -a <search field>, --default-search <search field>\n");
230 fprintf(out, " set the default search field\n");
233 /*********************************************************************/
235 int ignore_entry(const char *name) {
237 strcmp(name, ".") == 0 ||
238 strcmp(name, "..") == 0 ||
239 (name[0] == '.' && name[1] != '/');
242 int is_a_leading_from_line(char *mbox_line) {
244 strncmp(mbox_line, "From ", 5) == 0 &&
245 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
248 int mbox_line_match_search(struct search_condition *condition,
249 int mbox_id, const char *mbox_value) {
251 if(condition->field_id == ID_INTERVAL) {
252 if(mbox_id == ID_LEADING_LINE) {
258 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
259 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
262 return (t >= condition->interval_start &&
263 (condition->interval_stop == 0 ||
264 t <= condition->interval_stop));
272 (condition->field_id == mbox_id)
276 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
277 mbox_id == ID_FROM ||
281 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
287 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
291 void update_body_hits(char *mail_filename, int position_in_mail,
292 int nb_search_conditions, struct search_condition *search_conditions,
293 int nb_body_conditions,
297 char raw_mbox_line[BUFFER_SIZE];
303 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
305 fseek(mail_file, position_in_mail, SEEK_SET);
307 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
308 while(nb_body_hits < nb_body_conditions) {
309 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
310 /* if(last_mbox_line_was_empty) { header = 0; } */
312 if(raw_mbox_line[0] == '\n') { header = 0; }
315 for(n = 0; n < nb_search_conditions; n++) {
316 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
318 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
326 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
327 (is_a_leading_from_line(raw_mbox_line)))
335 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
337 char raw_mbox_line[BUFFER_SIZE];
340 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
341 fseek(mail_file, position_in_mail, SEEK_SET);
343 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
344 fprintf(output_file, "%s", raw_mbox_line);
346 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
347 (is_a_leading_from_line(raw_mbox_line))
350 fprintf(output_file, "%s", raw_mbox_line);
357 int search_in_db(const char *db_filename,
358 int nb_search_conditions,
359 struct search_condition *search_conditions,
362 int hits[MAX_NB_SEARCH_CONDITIONS];
363 char raw_db_line[BUFFER_SIZE];
364 char current_mail_filename[PATH_MAX + 1];
365 unsigned long int current_position_in_mail;
366 char mbox_key[TOKEN_BUFFER_SIZE];
367 const char *mbox_value;
369 int already_written, m, n;
370 int nb_body_conditions, nb_fulfilled_body_conditions;
372 int nb_extracted_mails;
374 nb_extracted_mails = 0;
377 printf("Searching in '%s' ... ", db_filename);
381 db_file = safe_fopen(db_filename, "r", "index file for search");
383 /* First, check the db file leading line integrity */
385 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
386 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
388 "mymail: Header line in '%s' does not match the mymail db format.\n",
394 "mymail: Cannot read the header line in '%s'.\n",
399 /* Then parse the said db file */
401 current_position_in_mail = 0;
404 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
406 nb_body_conditions = 0;
407 for(n = 0; n < nb_search_conditions; n++) {
408 if(search_conditions[n].field_id == ID_BODY) {
409 nb_body_conditions++;
413 strcpy(current_mail_filename, "");
415 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
416 mbox_value = parse_token(mbox_key, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
418 if(strcmp("mail", mbox_key) == 0) {
419 char position_in_file_string[TOKEN_BUFFER_SIZE];
421 if(current_mail_filename[0]) {
423 /* We first check all conditions but the body ones */
425 for(n = 0; n < nb_search_conditions &&
426 ((search_conditions[n].field_id == ID_BODY) ||
427 xor(hits[n], search_conditions[n].negation)); n++);
429 if(n == nb_search_conditions) {
431 /* Now check the body ones */
433 nb_fulfilled_body_conditions = 0;
435 if(nb_body_conditions > 0) {
436 update_body_hits(current_mail_filename, current_position_in_mail,
437 nb_search_conditions, search_conditions,
441 for(n = 0; n < nb_search_conditions; n++) {
442 if(search_conditions[n].field_id == ID_BODY &&
443 xor(hits[n], search_conditions[n].negation)) {
444 nb_fulfilled_body_conditions++;
449 if(nb_body_conditions == nb_fulfilled_body_conditions) {
450 nb_extracted_mails++;
451 extract_mail(current_mail_filename, current_position_in_mail, output_file);
456 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
458 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
459 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
460 current_position_in_mail = atol(position_in_file_string);
466 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
467 if(strncmp(field_keys[m], mbox_key, strlen(mbox_key)) == 0) {
471 for(n = 0; n < nb_search_conditions; n++) {
472 hits[n] |= mbox_line_match_search(&search_conditions[n],
473 mbox_id, mbox_value);
485 return nb_extracted_mails;
488 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
489 int nb_search_conditions,
490 struct search_condition *search_conditions,
493 struct dirent *dir_e;
495 char subname[PATH_MAX + 1];
496 int nb_extracted_mails = 0;
498 if(lstat(entry_name, &sb) != 0) {
500 "mymail: Cannot stat \"%s\": %s\n",
506 /* printf("recursive_search_in_db %s\n", entry_name); */
508 dir = opendir(entry_name);
511 while((dir_e = readdir(dir))) {
512 if(!ignore_entry(dir_e->d_name)) {
513 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
514 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
515 nb_search_conditions, search_conditions,
523 const char *s = entry_name, *filename = entry_name;
524 while(*s) { if(*s == '/') { filename = s+1; } s++; }
526 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
527 nb_extracted_mails +=
528 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
532 return nb_extracted_mails;
535 /*********************************************************************/
537 void index_one_mbox_line(unsigned int nb_fields_to_parse,
538 struct parsable_field *fields_to_parse,
539 char *raw_mbox_line, FILE *db_file) {
542 for(f = 0; f < nb_fields_to_parse; f++) {
543 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
544 fprintf(db_file, "%s %s\n",
545 field_keys[fields_to_parse[f].id],
546 raw_mbox_line + matches.rm_eo);
551 void index_mbox(const char *mbox_filename,
552 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
554 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
555 char *end_of_full_line;
557 int in_header, new_header;
558 unsigned long int position_in_file;
560 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
565 position_in_file = 0;
566 end_of_full_line = 0;
569 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
570 if(is_a_leading_from_line(raw_mbox_line)) {
571 /* This starts a new mail */
574 "Got a ^\"From \" in the header in %s:%lu.\n",
575 mbox_filename, position_in_file);
576 fprintf(stderr, "%s", raw_mbox_line);
579 /* printf("LEADING_LINE %s", raw_mbox_line); */
583 } else if(raw_mbox_line[0] == '\n') {
586 /* We leave the header, index the current line */
588 /* printf("INDEX %s\n", full_line); */
589 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
591 end_of_full_line = full_line;
592 *end_of_full_line = '\0';
598 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
602 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
603 /* Continuation of a line */
604 char *start = raw_mbox_line;
605 while(*start == ' ' || *start == '\t') start++;
606 *(end_of_full_line++) = ' ';
607 strcpy(end_of_full_line, start);
608 while(*end_of_full_line && *end_of_full_line != '\n') {
611 *end_of_full_line = '\0';
615 /* Start a new header line, not a continuation */
618 /* printf("INDEX %s\n", full_line); */
619 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
622 end_of_full_line = full_line;
623 strcpy(end_of_full_line, raw_mbox_line);
624 while(*end_of_full_line && *end_of_full_line != '\n') {
627 *end_of_full_line = '\0';
632 position_in_file += strlen(raw_mbox_line);
638 void recursive_index_mbox(FILE *db_file,
639 const char *entry_name, regex_t *mbox_filename_regexp,
640 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
642 struct dirent *dir_e;
644 char subname[PATH_MAX + 1];
646 if(lstat(entry_name, &sb) != 0) {
648 "mymail: Cannot stat \"%s\": %s\n",
654 dir = opendir(entry_name);
657 while((dir_e = readdir(dir))) {
658 if(!ignore_entry(dir_e->d_name)) {
659 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
660 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
661 nb_fields_to_parse, fields_to_parse);
666 const char *s = entry_name, *filename = s;
667 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
668 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
669 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
674 /*********************************************************************/
676 /* For long options that have no equivalent short option, use a
677 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
679 OPT_BASH_MODE = CHAR_MAX + 1
682 static struct option long_options[] = {
683 { "help", no_argument, 0, 'h' },
684 { "version", no_argument, 0, 'v' },
685 { "quiet", no_argument, 0, 'q' },
686 { "db-file-generate", 1, 0, 'd' },
687 { "db-pattern", 1, 0, 'p' },
688 { "db-root", 1, 0, 'r' },
689 { "db-list", 1, 0, 'l' },
690 { "mbox-pattern", 1, 0, 'm' },
691 { "search", 1, 0, 's' },
692 { "index", 0, 0, 'i' },
693 { "output", 1, 0, 'o' },
694 { "default-search", 1, 0, 'a' },
698 struct time_criterion {
700 int start_hour, end_hour;
704 /*********************************************************************/
706 static struct time_criterion time_criteria[] = {
709 { "today", 24, -1, -1 },
710 { "24h", 24, -1, -1 },
711 { "48h", 48, -1, -1 },
712 { "week", 24 * 7, -1, -1 },
713 { "month", 24 * 31, -1, -1 },
714 { "year", 24 * 365, -1, -1 },
716 { "yesterday", 48, 24, -1 },
718 { "monday", -1, -1, 1 },
719 { "tuesday", -1, -1, 2 },
720 { "wednesday", -1, -1, 3 },
721 { "thursday", -1, -1, 4 },
722 { "friday", -1, -1, 5 },
723 { "saturday", -1, -1, 6 },
724 { "sunday", -1, -1, 7 },
728 /*********************************************************************/
730 time_t time_for_past_day(int day) {
736 delta_day = (7 + tm->tm_wday - day) % 7;
737 if(delta_day == 0) { delta_day = 7; }
738 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
741 void init_condition(struct search_condition *condition, const char *full_string,
742 const char *default_search_field) {
743 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
747 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
748 search_field = full_search_field;
750 if(search_field[0] == '!') {
752 condition->negation = 1;
754 condition->negation = 0;
757 condition->field_id = -1;
761 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
762 if(strcmp(time_criteria[k].label, search_field) == 0) {
763 condition->field_id = ID_INTERVAL;
764 if(time_criteria[k].past_week_day < 0) {
765 condition->interval_start = time(0) - 3600 * time_criteria[k].start_hour;
766 if(time_criteria[k].end_hour >= 0) {
767 condition->interval_stop = time(0) - 3600 * time_criteria[k].end_hour;
769 condition->interval_stop = 0;
772 condition->interval_start = time_for_past_day(time_criteria[k].past_week_day);
773 condition->interval_stop = condition->interval_start + 3600 * 24;
779 if(condition->field_id == -1) {
781 /* No time condition matched, look for the search fields */
783 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
784 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
785 condition->field_id = m;
789 /* None match, if there is a default search field, re-run the search with it */
791 if(condition->field_id == -1) {
792 if(default_search_field) {
793 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
794 if(strncmp(field_keys[m],
795 default_search_field, strlen(default_search_field)) == 0) {
796 condition->field_id = m;
799 string = full_string;
800 if(string[0] == '!') { string++; }
804 if(condition->field_id == -1) {
806 "mymail: Syntax error in field key \"%s\".\n",
811 if(regcomp(&condition->regexp,
815 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
817 field_keys[condition->field_id]);
823 void free_condition(struct search_condition *condition) {
824 if(condition->field_id != ID_INTERVAL) {
825 regfree(&condition->regexp);
829 /*********************************************************************/
830 /*********************************************************************/
831 /*********************************************************************/
833 int main(int argc, char **argv) {
834 char *db_filename = 0;
835 char *db_filename_regexp_string = 0;
836 char *db_root_path = 0;
837 char *db_filename_list = 0;
838 char *mbox_filename_regexp_string = 0;
839 char *default_search_field;
840 char output_filename[PATH_MAX + 1];
841 int action_index = 0;
842 int error = 0, show_help = 0;
843 const unsigned int nb_fields_to_parse =
844 sizeof(fields_to_parse) / sizeof(struct parsable_field);
847 unsigned int nb_search_conditions;
848 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
850 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
852 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
857 default_search_field = 0;
858 strncpy(output_filename, "", PATH_MAX);
860 setlocale(LC_ALL, "");
862 nb_search_conditions = 0;
864 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
865 long_options, NULL)) != -1) {
874 print_version(stdout);
887 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
890 db_filename = strdup(optarg);
894 if(db_filename_regexp_string) {
895 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
898 db_filename_regexp_string = strdup(optarg);
902 if(mbox_filename_regexp_string) {
903 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
906 mbox_filename_regexp_string = strdup(optarg);
910 strncpy(output_filename, optarg, PATH_MAX);
915 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
918 db_root_path = strdup(optarg);
922 if(db_filename_list) {
923 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
926 db_filename_list = strdup(optarg);
930 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
931 fprintf(stderr, "mymail: Too many search patterns.\n");
934 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
935 nb_search_conditions++;
939 default_search_field = optarg;
948 /* Set all the values that may defined in the arguments, through
949 environment variables, or hard-coded */
951 db_filename = default_value(db_filename,
955 db_filename_regexp_string = default_value(db_filename_regexp_string,
959 db_root_path = default_value(db_root_path,
963 db_filename_list = default_value(db_filename_list,
967 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
968 "MYMAIL_MBOX_PATTERN",
971 /* Start the processing */
987 regex_t mbox_filename_regexp_static;
988 regex_t *mbox_filename_regexp;
990 if(mbox_filename_regexp_string) {
991 if(regcomp(&mbox_filename_regexp_static,
992 mbox_filename_regexp_string,
995 "mymail: Syntax error in regexp \"%s\".\n",
996 mbox_filename_regexp_string);
999 mbox_filename_regexp = &mbox_filename_regexp_static;
1001 mbox_filename_regexp = 0;
1004 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1006 for(f = 0; f < nb_fields_to_parse; f++) {
1007 if(regcomp(&fields_to_parse[f].regexp,
1008 fields_to_parse[f].regexp_string,
1009 fields_to_parse[f].cflags)) {
1011 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1012 fields_to_parse[f].regexp_string,
1013 field_keys[fields_to_parse[f].id]);
1018 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1020 while(optind < argc) {
1021 recursive_index_mbox(db_file,
1022 argv[optind], mbox_filename_regexp,
1023 nb_fields_to_parse, fields_to_parse);
1030 if(mbox_filename_regexp) {
1031 regfree(mbox_filename_regexp);
1034 for(f = 0; f < nb_fields_to_parse; f++) {
1035 regfree(&fields_to_parse[f].regexp);
1044 int nb_extracted_mails = 0;
1046 if(output_filename[0]) {
1047 output_file = safe_fopen(output_filename, "w", "result mbox");
1049 output_file = stdout;
1053 if(nb_search_conditions > 0) {
1055 /* Recursive search if db_root_path is set */
1058 regex_t db_filename_regexp;
1059 if(regcomp(&db_filename_regexp,
1060 db_filename_regexp_string,
1063 "mymail: Syntax error in regexp \"%s\".\n",
1064 db_filename_regexp_string);
1068 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1069 nb_search_conditions, search_conditions,
1072 regfree(&db_filename_regexp);
1075 /* Search in all db files listed in db_filename_list */
1077 if(db_filename_list) {
1078 char db_filename[PATH_MAX + 1];
1081 s = db_filename_list;
1084 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1086 if(db_filename[0]) {
1087 nb_extracted_mails +=
1088 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1093 /* Search in all db files listed in the command arguments */
1095 while(optind < argc) {
1096 nb_extracted_mails +=
1097 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1103 if(nb_extracted_mails > 0) {
1104 printf("Found %d matching mails.\n", nb_extracted_mails);
1106 printf("No matching mail found.\n");
1110 fflush(output_file);
1112 if(output_file != stdout) {
1113 fclose(output_file);
1117 for(n = 0; n < nb_search_conditions; n++) {
1118 free_condition(&search_conditions[n]);
1122 free(db_filename_regexp_string);
1124 free(db_filename_list);
1125 free(mbox_filename_regexp_string);
1127 regfree(&global_leading_from_line_regexp);