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, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
207 fprintf(out, " set the mbox filename pattern for recursive search\n");
208 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
209 fprintf(out, " search for matching mails in the db file\n");
210 fprintf(out, " -d <db filename>, --db-file-generate <db filename>\n");
211 fprintf(out, " set the db filename for indexing\n");
212 fprintf(out, " -i, --index\n");
213 fprintf(out, " index mails\n");
214 fprintf(out, " -o <output filename>, --output <output filename>\n");
215 fprintf(out, " set the result file, use stdout if unset\n");
216 fprintf(out, " -a <search field>, --default-search <search field>\n");
217 fprintf(out, " set the default search field\n");
220 /*********************************************************************/
222 time_t time_for_past_day(int day) {
228 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
229 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
232 /*********************************************************************/
234 int ignore_entry(const char *name) {
236 strcmp(name, ".") == 0 ||
237 strcmp(name, "..") == 0 ||
238 (ignore_dot_files && name[0] == '.' && name[1] != '/');
241 int is_a_leading_from_line(char *mbox_line) {
246 The mbox man page in qmail documentation states:
248 > The reader should not attempt to take advantage of the fact
249 > that every From_ line (past the beginning of the file) is
250 > preceded by a blank line.
254 strncmp(mbox_line, "From ", 5) == 0 &&
255 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
258 int mbox_line_match_search(struct search_condition *condition,
259 int mbox_id, char *mbox_value) {
261 if(condition->field_id == ID_INTERVAL) {
262 if(mbox_id == ID_LEADING_LINE) {
268 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
269 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
272 return (t >= condition->interval_start &&
273 (condition->interval_stop == 0 ||
274 t <= condition->interval_stop));
282 (condition->field_id == mbox_id)
286 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
287 mbox_id == ID_FROM ||
291 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
297 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
301 void update_body_hits(char *mail_filename, int position_in_mail,
302 int nb_search_conditions, struct search_condition *search_conditions,
303 int nb_body_conditions,
307 char raw_mbox_line[BUFFER_SIZE];
313 mail_file = fopen(mail_filename, "r");
317 "mymail: Cannot open mbox '%s' for body scan.\n",
322 fseek(mail_file, position_in_mail, SEEK_SET);
324 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
325 while(nb_body_hits < nb_body_conditions) {
326 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
327 /* if(last_mbox_line_was_empty) { header = 0; } */
329 if(raw_mbox_line[0] == '\n') { header = 0; }
332 for(n = 0; n < nb_search_conditions; n++) {
333 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
335 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
343 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
344 (is_a_leading_from_line(raw_mbox_line)))
352 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
354 char raw_mbox_line[BUFFER_SIZE];
357 mail_file = fopen(mail_filename, "r");
361 "mymail: Cannot open mbox '%s' for mail extraction.\n",
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 = fopen(db_filename, "r");
409 "mymail: Cannot open \"%s\" for reading: %s\n",
415 /* First, check the db file leading line integrity */
417 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
418 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
420 "mymail: Header line in '%s' does not match the mymail db format.\n",
426 "mymail: Cannot read the header line in '%s'.\n",
431 /* Then parse the said db file */
433 current_position_in_mail = 0;
436 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
438 nb_body_conditions = 0;
439 for(n = 0; n < nb_search_conditions; n++) {
440 if(search_conditions[n].field_id == ID_BODY) {
441 nb_body_conditions++;
445 strcpy(current_mail_filename, "");
447 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
448 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
450 if(strcmp("mail", mbox_name) == 0) {
451 char position_in_file_string[TOKEN_BUFFER_SIZE];
453 if(current_mail_filename[0]) {
455 /* We first check all conditions but the body ones */
457 for(n = 0; n < nb_search_conditions &&
458 ((search_conditions[n].field_id == ID_BODY) ||
459 xor(hits[n], search_conditions[n].negation)); n++);
461 if(n == nb_search_conditions) {
463 /* Now check the body ones */
465 if(nb_body_conditions > 0) {
466 update_body_hits(current_mail_filename, current_position_in_mail,
467 nb_search_conditions, search_conditions,
472 nb_fulfilled_body_conditions = 0;
474 for(n = 0; n < nb_search_conditions; n++) {
475 if(search_conditions[n].field_id == ID_BODY &&
476 xor(hits[n], search_conditions[n].negation)) {
477 nb_fulfilled_body_conditions++;
481 if(nb_body_conditions == nb_fulfilled_body_conditions) {
482 nb_extracted_mails++;
483 extract_mail(current_mail_filename, current_position_in_mail, output_file);
488 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
490 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
491 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
492 current_position_in_mail = atol(position_in_file_string);
498 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
499 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
503 for(n = 0; n < nb_search_conditions; n++) {
504 hits[n] |= mbox_line_match_search(&search_conditions[n],
505 mbox_id, mbox_value);
517 return nb_extracted_mails;
520 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
521 int nb_search_conditions,
522 struct search_condition *search_conditions,
525 struct dirent *dir_e;
527 char subname[PATH_MAX + 1];
528 int nb_extracted_mails = 0;
530 if(lstat(entry_name, &sb) != 0) {
532 "mymail: Cannot stat \"%s\": %s\n",
538 dir = opendir(entry_name);
541 while((dir_e = readdir(dir))) {
542 if(!ignore_entry(dir_e->d_name)) {
543 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
544 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
545 nb_search_conditions, search_conditions,
553 const char *s = entry_name, *filename = entry_name;
554 while(*s) { if(*s == '/') { filename = s+1; } s++; }
556 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
557 nb_extracted_mails +=
558 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
562 return nb_extracted_mails;
565 /*********************************************************************/
567 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
568 char *raw_mbox_line, FILE *db_file) {
571 for(f = 0; f < nb_fields_to_parse; f++) {
572 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
573 fprintf(db_file, "%s %s\n",
574 field_names[fields_to_parse[f].id],
575 raw_mbox_line + matches.rm_eo);
580 void index_mbox(const char *mbox_filename,
581 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
583 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
584 char *end_of_full_line;
586 int in_header, new_header;
587 unsigned long int position_in_file;
589 file = fopen(mbox_filename, "r");
592 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
593 if(paranoid) { exit(EXIT_FAILURE); }
600 position_in_file = 0;
601 end_of_full_line = 0;
604 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
605 if(is_a_leading_from_line(raw_mbox_line)) {
608 "Got a ^\"From \" in the header in %s:%lu.\n",
609 mbox_filename, position_in_file);
610 fprintf(stderr, "%s", raw_mbox_line);
611 if(paranoid) { exit(EXIT_FAILURE); }
615 } else if(raw_mbox_line[0] == '\n') {
616 if(in_header) { in_header = 0; }
621 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
625 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
626 char *start = raw_mbox_line;
627 while(*start == ' ' || *start == '\t') start++;
628 *(end_of_full_line++) = ' ';
629 strcpy(end_of_full_line, start);
630 while(*end_of_full_line && *end_of_full_line != '\n') {
633 *end_of_full_line = '\0';
638 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
639 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
641 "Header line syntax error %s:%lu.\n",
642 mbox_filename, position_in_file);
643 fprintf(stderr, "%s", raw_mbox_line);
648 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
651 end_of_full_line = full_line;
652 strcpy(end_of_full_line, raw_mbox_line);
653 while(*end_of_full_line && *end_of_full_line != '\n') {
656 *end_of_full_line = '\0';
661 position_in_file += strlen(raw_mbox_line);
667 void recursive_index_mbox(FILE *db_file,
668 const char *entry_name, regex_t *mbox_filename_regexp,
669 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
671 struct dirent *dir_e;
673 char subname[PATH_MAX + 1];
675 if(lstat(entry_name, &sb) != 0) {
677 "mymail: Cannot stat \"%s\": %s\n",
683 dir = opendir(entry_name);
686 while((dir_e = readdir(dir))) {
687 if(!ignore_entry(dir_e->d_name)) {
688 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
689 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
690 nb_fields_to_parse, fields_to_parse);
695 const char *s = entry_name, *filename = s;
696 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
697 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
698 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
703 /*********************************************************************/
705 /* For long options that have no equivalent short option, use a
706 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
708 OPT_BASH_MODE = CHAR_MAX + 1
711 static struct option long_options[] = {
712 { "help", no_argument, 0, 'h' },
713 { "version", no_argument, 0, 'v' },
714 { "quiet", no_argument, 0, 'q' },
715 { "db-file-generate", 1, 0, 'd' },
716 { "db-pattern", 1, 0, 'p' },
717 { "db-root", 1, 0, 'r' },
718 { "db-list", 1, 0, 'l' },
719 { "mbox-pattern", 1, 0, 'm' },
720 { "search", 1, 0, 's' },
721 { "index", 0, 0, 'i' },
722 { "output", 1, 0, 'o' },
723 { "default-search", 1, 0, 'a' },
727 /*********************************************************************/
729 void init_condition(struct search_condition *condition, char *full_string) {
730 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
734 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
735 search_field = full_search_field;
737 if(search_field[0] == '!') {
739 condition->negation = 1;
741 condition->negation = 0;
746 if(strcmp(search_field, "8h") == 0) {
747 condition->field_id = ID_INTERVAL;
748 condition->interval_start = time(0) - 3600 * 8;
749 condition->interval_stop = 0;
752 else if(strcmp(search_field, "24h") == 0 ||
753 strcmp(search_field, "today") == 0) {
754 condition->field_id = ID_INTERVAL;
755 condition->interval_start = time(0) - 3600 * 24;
756 condition->interval_stop = 0;
759 else if(strcmp(search_field, "week") == 0) {
760 condition->field_id = ID_INTERVAL;
761 condition->interval_start = time(0) - 3600 * 24 * 7;
762 condition->interval_stop = 0;
765 else if(strcmp(search_field, "month") == 0) {
766 condition->field_id = ID_INTERVAL;
767 condition->interval_start = time(0) - 3600 * 24 * 31;
768 condition->interval_stop = 0;
771 else if(strcmp(search_field, "year") == 0) {
772 condition->field_id = ID_INTERVAL;
773 condition->interval_start = time(0) - 3600 * 24 * 365;
774 condition->interval_stop = 0;
779 else if(strcmp(search_field, "yesterday") == 0) {
780 condition->field_id = ID_INTERVAL;
781 condition->interval_start = time(0) - 2 * 3600 * 24;
782 condition->interval_stop = condition->interval_start + 3600 * 24;
787 else if(strcmp(search_field, "monday") == 0) {
788 condition->field_id = ID_INTERVAL;
789 condition->interval_start = time_for_past_day(1);
790 condition->interval_stop = condition->interval_start + 3600 * 24;
793 else if(strcmp(search_field, "tuesday") == 0) {
794 condition->field_id = ID_INTERVAL;
795 condition->interval_start = time_for_past_day(2);
796 condition->interval_stop = condition->interval_start + 3600 * 24;
799 else if(strcmp(search_field, "wednesday") == 0) {
800 condition->field_id = ID_INTERVAL;
801 condition->interval_start = time_for_past_day(3);
802 condition->interval_stop = condition->interval_start + 3600 * 24;
805 else if(strcmp(search_field, "thursday") == 0) {
806 condition->field_id = ID_INTERVAL;
807 condition->interval_start = time_for_past_day(4);
808 condition->interval_stop = condition->interval_start + 3600 * 24;
811 else if(strcmp(search_field, "friday") == 0) {
812 condition->field_id = ID_INTERVAL;
813 condition->interval_start = time_for_past_day(5);
814 condition->interval_stop = condition->interval_start + 3600 * 24;
817 else if(strcmp(search_field, "saturday") == 0) {
818 condition->field_id = ID_INTERVAL;
819 condition->interval_start = time_for_past_day(6);
820 condition->interval_stop = condition->interval_start + 3600 * 24;
823 else if(strcmp(search_field, "sunday") == 0) {
824 condition->field_id = ID_INTERVAL;
825 condition->interval_start = time_for_past_day(7);
826 condition->interval_stop = condition->interval_start + 3600 * 24;
831 /* header-related conditions */
833 condition->field_id = -1;
835 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
836 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
837 condition->field_id = m;
841 if(condition->field_id == -1) {
842 if(default_search_field) {
843 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
844 if(strncmp(field_names[m],
845 default_search_field, strlen(default_search_field)) == 0) {
846 condition->field_id = m;
849 string = full_string;
853 if(condition->field_id == -1) {
855 "mymail: Syntax error in field name \"%s\".\n",
860 if(regcomp(&condition->regexp,
864 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
866 field_names[condition->field_id]);
872 void free_condition(struct search_condition *condition) {
873 if(condition->field_id != ID_INTERVAL) {
874 regfree(&condition->regexp);
878 /*********************************************************************/
879 /*********************************************************************/
880 /*********************************************************************/
882 int main(int argc, char **argv) {
884 char *db_filename_regexp_string;
886 char *db_filename_list;
887 char *mbox_filename_regexp_string;
888 char output_filename[PATH_MAX + 1];
890 int error = 0, show_help = 0;
891 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
894 int nb_search_conditions;
895 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
897 if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP, 0)) {
899 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
906 db_filename_regexp_string = 0;
908 db_filename_list = 0;
909 mbox_filename_regexp_string = 0;
911 default_search_field = 0;
912 ignore_dot_files = 1;
914 setlocale(LC_ALL, "");
916 nb_search_conditions = 0;
918 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
919 long_options, NULL)) != -1) {
928 print_version(stdout);
941 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
944 db_filename = strdup(optarg);
948 if(db_filename_regexp_string) {
949 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
952 db_filename_regexp_string = strdup(optarg);
956 if(mbox_filename_regexp_string) {
957 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
960 mbox_filename_regexp_string = strdup(optarg);
964 strncpy(output_filename, optarg, PATH_MAX);
969 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
972 db_root_path = strdup(optarg);
976 if(db_filename_list) {
977 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
980 db_filename_list = strdup(optarg);
984 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
985 fprintf(stderr, "mymail: Too many search patterns.\n");
988 init_condition(&search_conditions[nb_search_conditions], optarg);
989 nb_search_conditions++;
993 default_search_field = optarg;
1002 /* Set all the values that may defined in the arguments, through
1003 environment variables, or hard-coded */
1006 char *default_db_filename = getenv("MYMAIL_DB_FILE");
1008 if(!default_db_filename) {
1009 default_db_filename = "mymail.db";
1012 db_filename = strdup(default_db_filename);
1015 if(!db_filename_regexp_string) {
1016 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
1018 if(!default_db_filename_regexp_string) {
1019 default_db_filename_regexp_string = "^mymail.db$";
1022 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
1026 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
1028 if(default_db_root_path) {
1029 db_root_path = strdup(default_db_root_path);
1033 if(!db_filename_list) {
1034 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
1036 if(default_db_filename_list) {
1037 db_filename_list = strdup(default_db_filename_list);
1041 if(!mbox_filename_regexp_string) {
1042 char *default_mbox_filename_regexp_string = getenv("MYMAIL_MBOX_PATTERN");
1044 if(default_mbox_filename_regexp_string) {
1045 mbox_filename_regexp_string = strdup(default_mbox_filename_regexp_string);
1049 /* Start the processing */
1052 print_usage(stderr);
1057 print_usage(stdout);
1065 regex_t mbox_filename_regexp_static;
1066 regex_t *mbox_filename_regexp;
1068 if(mbox_filename_regexp_string) {
1069 if(regcomp(&mbox_filename_regexp_static,
1070 mbox_filename_regexp_string,
1073 "mymail: Syntax error in regexp \"%s\".\n",
1074 mbox_filename_regexp_string);
1077 mbox_filename_regexp = &mbox_filename_regexp_static;
1079 mbox_filename_regexp = 0;
1082 db_file = fopen(db_filename, "w");
1086 "mymail: Cannot open \"%s\" for writing: %s\n",
1092 for(f = 0; f < nb_fields_to_parse; f++) {
1093 if(regcomp(&fields_to_parse[f].regexp,
1094 fields_to_parse[f].regexp_string,
1095 fields_to_parse[f].cflags)) {
1097 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1098 fields_to_parse[f].regexp_string,
1099 field_names[fields_to_parse[f].id]);
1104 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1106 while(optind < argc) {
1107 recursive_index_mbox(db_file,
1108 argv[optind], mbox_filename_regexp,
1109 nb_fields_to_parse, fields_to_parse);
1116 if(mbox_filename_regexp) {
1117 regfree(mbox_filename_regexp);
1120 for(f = 0; f < nb_fields_to_parse; f++) {
1121 regfree(&fields_to_parse[f].regexp);
1130 int nb_extracted_mails = 0;
1132 if(output_filename[0]) {
1133 output_file = fopen(output_filename, "w");
1137 "mymail: Cannot open result file \"%s\" for writing: %s\n",
1143 output_file = stdout;
1147 if(nb_search_conditions > 0) {
1149 /* Recursive search if db_root_path is set */
1152 regex_t db_filename_regexp;
1153 if(regcomp(&db_filename_regexp,
1154 db_filename_regexp_string,
1157 "mymail: Syntax error in regexp \"%s\".\n",
1158 db_filename_regexp_string);
1162 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1163 nb_search_conditions, search_conditions,
1166 regfree(&db_filename_regexp);
1169 /* Search in all db files listed in db_filename_list */
1171 if(db_filename_list) {
1172 char db_filename[PATH_MAX + 1];
1175 s = db_filename_list;
1178 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1180 if(db_filename[0]) {
1181 nb_extracted_mails +=
1182 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1187 /* Search in all db files listed in the command arguments */
1189 while(optind < argc) {
1190 nb_extracted_mails +=
1191 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1197 if(nb_extracted_mails > 0) {
1198 printf("Found %d matching mails.\n", nb_extracted_mails);
1200 printf("No matching mail found.\n");
1204 fflush(output_file);
1206 if(output_file != stdout) {
1207 fclose(output_file);
1211 for(n = 0; n < nb_search_conditions; n++) {
1212 free_condition(&search_conditions[n]);
1216 free(db_filename_regexp_string);
1218 free(db_filename_list);
1219 free(mbox_filename_regexp_string);
1221 regfree(&leading_from_line_regexp);