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 "^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 /********************************************************************/
170 /* malloc with error checking. */
172 void *safe_malloc(size_t n) {
176 "mymail: cannot allocate memory: %s\n", strerror(errno));
182 /*********************************************************************/
184 void print_version(FILE *out) {
185 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
188 void print_usage(FILE *out) {
190 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
192 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
194 fprintf(out, " -h, --help\n");
195 fprintf(out, " show this help\n");
196 fprintf(out, " -v, --version\n");
197 fprintf(out, " print the version number\n");
198 fprintf(out, " -q, --quiet\n");
199 fprintf(out, " do not print information during search\n");
200 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
201 fprintf(out, " set the db filename pattern for recursive search\n");
202 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
203 fprintf(out, " set the db root path for recursive search\n");
204 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
205 fprintf(out, " set the semicolon-separated list of db files for search\n");
206 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
207 fprintf(out, " search for matching mails in the db file\n");
208 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
209 fprintf(out, " set the db filename for indexing\n");
210 fprintf(out, " -i, --index\n");
211 fprintf(out, " index mails\n");
212 fprintf(out, " -o <output filename>, --output <output filename>\n");
213 fprintf(out, " set the result file, use stdout if unset\n");
214 fprintf(out, " -a <search field>, --default-search <search field>\n");
215 fprintf(out, " set the default search field\n");
218 /*********************************************************************/
220 time_t time_for_past_day(int day) {
226 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
227 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
230 /*********************************************************************/
232 int ignore_entry(const char *name) {
234 strcmp(name, ".") == 0 ||
235 strcmp(name, "..") == 0 ||
236 (ignore_dot_files && name[0] == '.' && name[1] != '/');
239 int is_a_leading_from_line(char *mbox_line) {
244 The mbox man page in qmail documentation states:
246 > The reader should not attempt to take advantage of the fact
247 > that every From_ line (past the beginning of the file) is
248 > preceded by a blank line.
252 strncmp(mbox_line, "From ", 5) == 0 &&
253 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
256 int mbox_line_match_search(struct search_condition *condition,
257 int mbox_id, char *mbox_value) {
259 if(condition->field_id == ID_INTERVAL) {
260 if(mbox_id == ID_LEADING_LINE) {
266 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
267 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
270 return (t >= condition->interval_start &&
271 (condition->interval_stop == 0 ||
272 t <= condition->interval_stop));
280 (condition->field_id == mbox_id)
284 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
285 mbox_id == ID_FROM ||
289 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
295 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
299 void update_body_hits(char *mail_filename, int position_in_mail,
300 int nb_search_conditions, struct search_condition *search_conditions,
301 int nb_body_conditions,
305 char raw_mbox_line[BUFFER_SIZE];
311 mail_file = fopen(mail_filename, "r");
315 "mymail: Cannot open mbox '%s' for body scan.\n",
320 fseek(mail_file, position_in_mail, SEEK_SET);
322 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
323 while(nb_body_hits < nb_body_conditions) {
324 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
325 /* if(last_mbox_line_was_empty) { header = 0; } */
327 if(raw_mbox_line[0] == '\n') { header = 0; }
330 for(n = 0; n < nb_search_conditions; n++) {
331 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
333 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
341 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
342 (is_a_leading_from_line(raw_mbox_line)))
350 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
352 char raw_mbox_line[BUFFER_SIZE];
355 mail_file = fopen(mail_filename, "r");
359 "mymail: Cannot open mbox '%s' for mail extraction.\n",
364 fseek(mail_file, position_in_mail, SEEK_SET);
366 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
367 fprintf(output_file, "%s", raw_mbox_line);
369 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
370 (is_a_leading_from_line(raw_mbox_line))
373 fprintf(output_file, "%s", raw_mbox_line);
380 int search_in_db(const char *db_filename,
381 int nb_search_conditions,
382 struct search_condition *search_conditions,
385 int hits[MAX_NB_SEARCH_CONDITIONS];
386 char raw_db_line[BUFFER_SIZE];
387 char current_mail_filename[PATH_MAX + 1];
388 unsigned long int current_position_in_mail;
389 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
391 int already_written, m, n;
392 int nb_body_conditions, nb_fulfilled_body_conditions;
394 int nb_extracted_mails;
396 nb_extracted_mails = 0;
399 printf("Searching in '%s' ... ", db_filename);
403 db_file = fopen(db_filename, "r");
407 "mymail: Cannot open \"%s\" for reading: %s\n",
413 /* First, check the db file leading line integrity */
415 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
416 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
418 "mymail: Header line in '%s' does not match the mymail db format.\n",
424 "mymail: Cannot read the header line in '%s'.\n",
429 /* Then parse the said db file */
431 current_position_in_mail = 0;
434 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
436 nb_body_conditions = 0;
437 for(n = 0; n < nb_search_conditions; n++) {
438 if(search_conditions[n].field_id == ID_BODY) {
439 nb_body_conditions++;
443 strcpy(current_mail_filename, "");
445 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
446 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
448 if(strcmp("mail", mbox_name) == 0) {
449 char position_in_file_string[TOKEN_BUFFER_SIZE];
451 if(current_mail_filename[0]) {
453 /* We first check all conditions but the body ones */
455 for(n = 0; n < nb_search_conditions &&
456 ((search_conditions[n].field_id == ID_BODY) ||
457 xor(hits[n], search_conditions[n].negation)); n++);
459 if(n == nb_search_conditions) {
461 /* Now check the body ones */
463 if(nb_body_conditions > 0) {
464 update_body_hits(current_mail_filename, current_position_in_mail,
465 nb_search_conditions, search_conditions,
470 nb_fulfilled_body_conditions = 0;
472 for(n = 0; n < nb_search_conditions; n++) {
473 if(search_conditions[n].field_id == ID_BODY &&
474 xor(hits[n], search_conditions[n].negation)) {
475 nb_fulfilled_body_conditions++;
479 if(nb_body_conditions == nb_fulfilled_body_conditions) {
480 nb_extracted_mails++;
481 extract_mail(current_mail_filename, current_position_in_mail, output_file);
486 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
488 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
489 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
490 current_position_in_mail = atol(position_in_file_string);
496 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
497 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
501 for(n = 0; n < nb_search_conditions; n++) {
502 hits[n] |= mbox_line_match_search(&search_conditions[n],
503 mbox_id, mbox_value);
515 return nb_extracted_mails;
518 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
519 int nb_search_conditions,
520 struct search_condition *search_conditions,
523 struct dirent *dir_e;
525 char subname[PATH_MAX + 1];
526 int nb_extracted_mails = 0;
528 if(lstat(entry_name, &sb) != 0) {
530 "mymail: Cannot stat \"%s\": %s\n",
536 dir = opendir(entry_name);
539 while((dir_e = readdir(dir))) {
540 if(!ignore_entry(dir_e->d_name)) {
541 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
542 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
543 nb_search_conditions, search_conditions,
551 const char *s = entry_name, *filename = entry_name;
552 while(*s) { if(*s == '/') { filename = s+1; } s++; }
554 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
555 nb_extracted_mails +=
556 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
560 return nb_extracted_mails;
563 /*********************************************************************/
565 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
566 char *raw_mbox_line, FILE *db_file) {
569 for(f = 0; f < nb_fields_to_parse; f++) {
570 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
571 fprintf(db_file, "%s %s\n",
572 field_names[fields_to_parse[f].id],
573 raw_mbox_line + matches.rm_eo);
578 void index_mbox(const char *mbox_filename,
579 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
581 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
582 char *end_of_full_line;
584 int in_header, new_header;
585 unsigned long int position_in_file;
587 file = fopen(mbox_filename, "r");
590 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
591 if(paranoid) { exit(EXIT_FAILURE); }
598 position_in_file = 0;
599 end_of_full_line = 0;
602 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
603 if(is_a_leading_from_line(raw_mbox_line)) {
606 "Got a ^\"From \" in the header in %s:%lu.\n",
607 mbox_filename, position_in_file);
608 fprintf(stderr, "%s", raw_mbox_line);
609 if(paranoid) { exit(EXIT_FAILURE); }
613 } else if(raw_mbox_line[0] == '\n') {
614 if(in_header) { in_header = 0; }
619 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
623 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
624 char *start = raw_mbox_line;
625 while(*start == ' ' || *start == '\t') start++;
626 *(end_of_full_line++) = ' ';
627 strcpy(end_of_full_line, start);
628 while(*end_of_full_line && *end_of_full_line != '\n') {
631 *end_of_full_line = '\0';
636 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
637 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
639 "Header line syntax error %s:%lu.\n",
640 mbox_filename, position_in_file);
641 fprintf(stderr, "%s", raw_mbox_line);
646 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
649 end_of_full_line = full_line;
650 strcpy(end_of_full_line, raw_mbox_line);
651 while(*end_of_full_line && *end_of_full_line != '\n') {
654 *end_of_full_line = '\0';
659 position_in_file += strlen(raw_mbox_line);
665 void recursive_index_mbox(FILE *db_file,
666 const char *entry_name,
667 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
669 struct dirent *dir_e;
671 char subname[PATH_MAX + 1];
673 if(lstat(entry_name, &sb) != 0) {
675 "mymail: Cannot stat \"%s\": %s\n",
681 dir = opendir(entry_name);
684 while((dir_e = readdir(dir))) {
685 if(!ignore_entry(dir_e->d_name)) {
686 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
687 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
692 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
696 /*********************************************************************/
698 /* For long options that have no equivalent short option, use a
699 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
701 OPT_BASH_MODE = CHAR_MAX + 1
704 static struct option long_options[] = {
705 { "help", no_argument, 0, 'h' },
706 { "version", no_argument, 0, 'v' },
707 { "quiet", no_argument, 0, 'q' },
708 { "db-file", 1, 0, 'd' },
709 { "db-pattern", 1, 0, 'p' },
710 { "db-root", 1, 0, 'r' },
711 { "db-list", 1, 0, 'l' },
712 { "search", 1, 0, 's' },
713 { "index", 0, 0, 'i' },
714 { "output", 1, 0, 'o' },
715 { "default-search", 1, 0, 'a' },
719 /*********************************************************************/
721 void init_condition(struct search_condition *condition, char *full_string) {
722 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
726 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
727 search_field = full_search_field;
729 if(search_field[0] == '!') {
731 condition->negation = 1;
733 condition->negation = 0;
738 if(strcmp(search_field, "8h") == 0) {
739 condition->field_id = ID_INTERVAL;
740 condition->interval_start = time(0) - 3600 * 8;
741 condition->interval_stop = 0;
744 else if(strcmp(search_field, "24h") == 0 ||
745 strcmp(search_field, "today") == 0) {
746 condition->field_id = ID_INTERVAL;
747 condition->interval_start = time(0) - 3600 * 24;
748 condition->interval_stop = 0;
751 else if(strcmp(search_field, "week") == 0) {
752 condition->field_id = ID_INTERVAL;
753 condition->interval_start = time(0) - 3600 * 24 * 7;
754 condition->interval_stop = 0;
757 else if(strcmp(search_field, "month") == 0) {
758 condition->field_id = ID_INTERVAL;
759 condition->interval_start = time(0) - 3600 * 24 * 31;
760 condition->interval_stop = 0;
763 else if(strcmp(search_field, "year") == 0) {
764 condition->field_id = ID_INTERVAL;
765 condition->interval_start = time(0) - 3600 * 24 * 365;
766 condition->interval_stop = 0;
771 else if(strcmp(search_field, "yesterday") == 0) {
772 condition->field_id = ID_INTERVAL;
773 condition->interval_start = time(0) - 2 * 3600 * 24;
774 condition->interval_stop = condition->interval_start + 3600 * 24;
779 else if(strcmp(search_field, "monday") == 0) {
780 condition->field_id = ID_INTERVAL;
781 condition->interval_start = time_for_past_day(1);
782 condition->interval_stop = condition->interval_start + 3600 * 24;
785 else if(strcmp(search_field, "tuesday") == 0) {
786 condition->field_id = ID_INTERVAL;
787 condition->interval_start = time_for_past_day(2);
788 condition->interval_stop = condition->interval_start + 3600 * 24;
791 else if(strcmp(search_field, "wednesday") == 0) {
792 condition->field_id = ID_INTERVAL;
793 condition->interval_start = time_for_past_day(3);
794 condition->interval_stop = condition->interval_start + 3600 * 24;
797 else if(strcmp(search_field, "thursday") == 0) {
798 condition->field_id = ID_INTERVAL;
799 condition->interval_start = time_for_past_day(4);
800 condition->interval_stop = condition->interval_start + 3600 * 24;
803 else if(strcmp(search_field, "friday") == 0) {
804 condition->field_id = ID_INTERVAL;
805 condition->interval_start = time_for_past_day(5);
806 condition->interval_stop = condition->interval_start + 3600 * 24;
809 else if(strcmp(search_field, "saturday") == 0) {
810 condition->field_id = ID_INTERVAL;
811 condition->interval_start = time_for_past_day(6);
812 condition->interval_stop = condition->interval_start + 3600 * 24;
815 else if(strcmp(search_field, "sunday") == 0) {
816 condition->field_id = ID_INTERVAL;
817 condition->interval_start = time_for_past_day(7);
818 condition->interval_stop = condition->interval_start + 3600 * 24;
823 /* header-related conditions */
825 condition->field_id = -1;
827 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
828 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
829 condition->field_id = m;
833 if(condition->field_id == -1) {
834 if(default_search_field) {
835 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
836 if(strncmp(field_names[m],
837 default_search_field, strlen(default_search_field)) == 0) {
838 condition->field_id = m;
841 string = full_string;
845 if(condition->field_id == -1) {
847 "mymail: Syntax error in field name \"%s\".\n",
852 if(regcomp(&condition->regexp,
856 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
858 field_names[condition->field_id]);
864 void free_condition(struct search_condition *condition) {
865 if(condition->field_id != ID_INTERVAL) {
866 regfree(&condition->regexp);
870 /*********************************************************************/
871 /*********************************************************************/
872 /*********************************************************************/
874 int main(int argc, char **argv) {
876 char *db_filename_regexp_string;
878 char *db_filename_list;
879 char output_filename[PATH_MAX + 1];
881 int error = 0, show_help = 0;
882 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
885 int nb_search_conditions;
887 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
889 if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP, 0)) {
891 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
898 db_filename_regexp_string = 0;
900 db_filename_list = 0;
902 default_search_field = 0;
903 ignore_dot_files = 1;
905 setlocale(LC_ALL, "");
907 nb_search_conditions = 0;
909 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
910 long_options, NULL)) != -1) {
919 print_version(stdout);
931 db_filename = strdup(optarg);
935 db_filename_regexp_string = strdup(optarg);
939 strncpy(output_filename, optarg, PATH_MAX);
943 db_root_path = strdup(optarg);
947 db_filename_list = strdup(optarg);
951 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
952 fprintf(stderr, "mymail: Too many search patterns.\n");
955 init_condition(&search_conditions[nb_search_conditions], optarg);
956 nb_search_conditions++;
960 default_search_field = optarg;
970 char *default_db_filename = getenv("MYMAIL_DB_FILE");
972 if(!default_db_filename) {
973 default_db_filename = "mymail.db";
976 db_filename = strdup(default_db_filename);
979 if(!db_filename_regexp_string) {
980 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
982 if(!default_db_filename_regexp_string) {
983 default_db_filename_regexp_string = "^mymail.db$";
986 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
990 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
992 if(default_db_root_path) {
993 db_root_path = strdup(default_db_root_path);
997 if(!db_filename_list) {
998 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
1000 if(default_db_filename_list) {
1001 db_filename_list = strdup(default_db_filename_list);
1005 if(output_filename[0]) {
1006 output_file = fopen(output_filename, "w");
1010 "mymail: Cannot open result file \"%s\" for writing: %s\n",
1016 output_file = stdout;
1021 print_usage(stderr);
1026 print_usage(stdout);
1033 db_file = fopen(db_filename, "w");
1037 "mymail: Cannot open \"%s\" for writing: %s\n",
1043 for(f = 0; f < nb_fields_to_parse; f++) {
1044 if(regcomp(&fields_to_parse[f].regexp,
1045 fields_to_parse[f].regexp_string,
1046 fields_to_parse[f].cflags)) {
1048 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1049 fields_to_parse[f].regexp_string,
1050 field_names[fields_to_parse[f].id]);
1055 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1057 while(optind < argc) {
1058 recursive_index_mbox(db_file,
1060 nb_fields_to_parse, fields_to_parse);
1067 for(f = 0; f < nb_fields_to_parse; f++) {
1068 regfree(&fields_to_parse[f].regexp);
1073 int nb_extracted_mails = 0;
1075 if(nb_search_conditions > 0) {
1077 /* Recursive search if db_root_path is set */
1080 regex_t db_filename_regexp;
1081 if(regcomp(&db_filename_regexp,
1082 db_filename_regexp_string,
1085 "mymail: Syntax error in regexp \"%s\".\n",
1086 db_filename_regexp_string);
1090 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1091 nb_search_conditions, search_conditions,
1094 regfree(&db_filename_regexp);
1097 /* Search in all db files listed in db_filename_list */
1099 if(db_filename_list) {
1100 char db_filename[PATH_MAX + 1];
1103 s = db_filename_list;
1106 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1108 if(db_filename[0]) {
1109 nb_extracted_mails +=
1110 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1115 /* Search in all db files listed in the command arguments */
1117 while(optind < argc) {
1118 nb_extracted_mails +=
1119 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1125 if(nb_extracted_mails > 0) {
1126 printf("Found %d matching mails.\n", nb_extracted_mails);
1128 printf("No matching mail found.\n");
1134 for(n = 0; n < nb_search_conditions; n++) {
1135 free_condition(&search_conditions[n]);
1138 if(output_file != stdout) {
1139 fflush(output_file);
1140 fclose(output_file);
1144 free(db_filename_regexp_string);
1146 free(db_filename_list);
1148 regfree(&leading_from_line_regexp);