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.6"
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! */
61 int global_use_leading_time;
63 regex_t global_leading_from_line_regexp;
65 /********************************************************************/
80 static char *field_keys[] = {
92 /********************************************************************/
94 struct search_condition {
98 time_t interval_start, interval_stop;
101 /********************************************************************/
103 struct parsable_field {
110 static struct parsable_field fields_to_parse[] = {
115 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
121 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
122 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
128 "^\\(to\\|cc\\|bcc\\): ",
129 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
136 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
143 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
148 /********************************************************************/
150 int xor(int a, int b) {
151 return (a && !b) || (!a && b);
154 const char *parse_token(char *token_buffer, size_t token_buffer_size,
155 char separator, const char *string) {
156 char *u = token_buffer;
157 while(*string == separator) { string++; }
158 while(u < token_buffer + token_buffer_size - 1 && *string &&
159 *string != separator) {
160 *(u++) = *(string++);
162 while(*string == separator) { string++; }
167 char *default_value(char *current_value,
168 const char *env_variable,
169 const char *hard_default_value) {
171 return current_value;
173 char *env_value = getenv(env_variable);
175 return strdup(env_value);
176 } else if(hard_default_value) {
177 return strdup(hard_default_value);
184 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
185 FILE *result = fopen(path, mode);
190 "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
191 path, comment, mode);
196 /*********************************************************************/
198 void print_version(FILE *out) {
199 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
202 void print_usage(FILE *out) {
204 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
206 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
208 fprintf(out, " -h, --help\n");
209 fprintf(out, " show this help\n");
210 fprintf(out, " -v, --version\n");
211 fprintf(out, " print the version number\n");
212 fprintf(out, " -q, --quiet\n");
213 fprintf(out, " do not print information during search\n");
214 fprintf(out, " -t, --use-leading-time\n");
215 fprintf(out, " use the time stamp from the leading line of each mail and not the Date:\n");
216 fprintf(out, " field\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 int ignore_entry(const char *name) {
241 strcmp(name, ".") == 0 ||
242 strcmp(name, "..") == 0 ||
243 (name[0] == '.' && name[1] != '/');
246 int is_a_leading_from_line(char *mbox_line) {
248 strncmp(mbox_line, "From ", 5) == 0 &&
249 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
252 int db_line_match_search(struct search_condition *condition,
253 int mbox_id, const char *mbox_value) {
257 (condition->field_id == mbox_id)
261 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
262 mbox_id == ID_FROM ||
266 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
272 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
275 void update_body_hits(char *mail_filename, int position_in_mail,
276 int nb_search_conditions, struct search_condition *search_conditions,
277 int nb_body_conditions,
281 char raw_mbox_line[BUFFER_SIZE];
287 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
289 fseek(mail_file, position_in_mail, SEEK_SET);
291 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
292 while(nb_body_hits < nb_body_conditions) {
293 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
294 /* if(last_mbox_line_was_empty) { header = 0; } */
296 if(raw_mbox_line[0] == '\n') { header = 0; }
299 for(n = 0; n < nb_search_conditions; n++) {
300 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
302 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
310 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
311 (is_a_leading_from_line(raw_mbox_line)))
319 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
321 char raw_mbox_line[BUFFER_SIZE];
324 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
325 fseek(mail_file, position_in_mail, SEEK_SET);
327 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
328 fprintf(output_file, "%s", raw_mbox_line);
330 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
331 (is_a_leading_from_line(raw_mbox_line))
334 fprintf(output_file, "%s", raw_mbox_line);
341 int check_full_mail_match(char *current_mail_filename,
343 int nb_search_conditions,
344 struct search_condition *search_conditions,
345 int nb_body_conditions,
347 int current_position_in_mail) {
348 int n, nb_fulfilled_body_conditions;
350 for(n = 0; n < nb_search_conditions; n++) {
351 if(search_conditions[n].field_id == ID_TIME_INTERVAL) {
352 hits[n] = (mail_time >= search_conditions[n].interval_start &&
353 (search_conditions[n].interval_stop == 0 ||
354 mail_time <= search_conditions[n].interval_stop));
358 /* We first check all conditions but the body ones */
360 for(n = 0; n < nb_search_conditions &&
361 ((search_conditions[n].field_id == ID_BODY) ||
362 xor(hits[n], search_conditions[n].negation)); n++);
364 if(n == nb_search_conditions) {
366 /* Now check the body ones */
368 nb_fulfilled_body_conditions = 0;
370 if(nb_body_conditions > 0) {
371 update_body_hits(current_mail_filename, current_position_in_mail,
372 nb_search_conditions, search_conditions,
376 for(n = 0; n < nb_search_conditions; n++) {
377 if(search_conditions[n].field_id == ID_BODY &&
378 xor(hits[n], search_conditions[n].negation)) {
379 nb_fulfilled_body_conditions++;
383 return nb_body_conditions == nb_fulfilled_body_conditions;
389 /* We use the mail leading line time by default, and if we should and
390 can, we update with the Date: field */
392 void update_time(int db_id, const char *db_value, time_t *t) {
396 if(db_id == ID_LEADING_LINE) {
398 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
399 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
402 if(!global_use_leading_time) {
403 if(db_id == ID_DATE) {
404 if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
405 strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
413 int search_in_db(const char *db_filename,
414 int nb_search_conditions,
415 struct search_condition *search_conditions,
419 char raw_db_line[BUFFER_SIZE];
420 char current_mail_filename[PATH_MAX + 1];
421 char db_key[TOKEN_BUFFER_SIZE];
422 char position_in_file_string[TOKEN_BUFFER_SIZE];
423 unsigned long int current_position_in_mail;
424 const char *db_value;
426 int hits[MAX_NB_SEARCH_CONDITIONS];
427 int nb_body_conditions, need_time;
428 int nb_extracted_mails;
433 nb_extracted_mails = 0;
436 printf("Searching in '%s' ... ", db_filename);
440 db_file = safe_fopen(db_filename, "r", "index file for search");
442 /* First, check the db file leading line integrity */
444 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
445 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
447 "mymail: Header line in '%s' does not match the mymail db format.\n",
453 "mymail: Cannot read the header line in '%s'.\n",
458 /* Then parse the said db file */
460 current_position_in_mail = 0;
462 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
464 nb_body_conditions = 0;
468 for(n = 0; n < nb_search_conditions; n++) {
469 if(search_conditions[n].field_id == ID_BODY) {
470 nb_body_conditions++;
472 else if(search_conditions[n].field_id == ID_TIME_INTERVAL) {
477 strcpy(current_mail_filename, "");
479 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
480 db_value = parse_token(db_key, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
482 if(strcmp("mail", db_key) == 0) {
483 if(current_mail_filename[0]) {
484 if(check_full_mail_match(current_mail_filename,
486 nb_search_conditions, search_conditions,
487 nb_body_conditions, hits, current_position_in_mail)) {
488 extract_mail(current_mail_filename, current_position_in_mail, output_file);
489 nb_extracted_mails++;
493 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
494 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
495 db_value = parse_token(current_mail_filename, PATH_MAX+1, '\n', db_value);
496 current_position_in_mail = atol(position_in_file_string);
501 for(m = 0; (m < MAX_ID) && db_id == -1; m++) {
502 if(strncmp(field_keys[m], db_key, strlen(db_key)) == 0) {
507 for(n = 0; n < nb_search_conditions; n++) {
508 hits[n] |= db_line_match_search(&search_conditions[n],
513 update_time(db_id, db_value, &mail_time);
518 if(current_mail_filename[0]) {
519 if(check_full_mail_match(current_mail_filename,
521 nb_search_conditions, search_conditions,
522 nb_body_conditions, hits, current_position_in_mail)) {
523 extract_mail(current_mail_filename, current_position_in_mail, output_file);
524 nb_extracted_mails++;
535 return nb_extracted_mails;
538 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
539 int nb_search_conditions,
540 struct search_condition *search_conditions,
543 struct dirent *dir_e;
545 char subname[PATH_MAX + 1];
546 int nb_extracted_mails = 0;
548 if(lstat(entry_name, &sb) != 0) {
550 "mymail: Cannot stat \"%s\": %s\n",
556 /* printf("recursive_search_in_db %s\n", entry_name); */
558 dir = opendir(entry_name);
561 while((dir_e = readdir(dir))) {
562 if(!ignore_entry(dir_e->d_name)) {
563 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
564 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
565 nb_search_conditions, search_conditions,
573 const char *s = entry_name, *filename = entry_name;
574 while(*s) { if(*s == '/') { filename = s+1; } s++; }
576 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
577 nb_extracted_mails +=
578 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
582 return nb_extracted_mails;
585 /*********************************************************************/
587 void index_one_mbox_line(unsigned int nb_fields_to_parse,
588 struct parsable_field *fields_to_parse,
589 char *raw_mbox_line, FILE *db_file) {
592 for(f = 0; f < nb_fields_to_parse; f++) {
593 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
594 fprintf(db_file, "%s %s\n",
595 field_keys[fields_to_parse[f].id],
596 raw_mbox_line + matches.rm_eo);
601 void index_mbox(const char *mbox_filename,
602 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
604 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
605 char *end_of_full_line;
607 int in_header, new_header;
608 unsigned long int position_in_file;
610 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
615 position_in_file = 0;
616 end_of_full_line = 0;
619 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
620 if(is_a_leading_from_line(raw_mbox_line)) {
621 /* This starts a new mail */
624 "Got a ^\"From \" in the header in %s:%lu.\n",
625 mbox_filename, position_in_file);
626 fprintf(stderr, "%s", raw_mbox_line);
629 /* printf("LEADING_LINE %s", raw_mbox_line); */
633 } else if(raw_mbox_line[0] == '\n') {
636 /* We leave the header, index the current line */
638 /* printf("INDEX %s\n", full_line); */
639 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
641 end_of_full_line = full_line;
642 *end_of_full_line = '\0';
648 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
652 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
653 /* Continuation of a line */
654 char *start = raw_mbox_line;
655 while(*start == ' ' || *start == '\t') start++;
656 *(end_of_full_line++) = ' ';
657 strcpy(end_of_full_line, start);
658 while(*end_of_full_line && *end_of_full_line != '\n') {
661 *end_of_full_line = '\0';
665 /* Start a new header line, not a continuation */
668 /* printf("INDEX %s\n", full_line); */
669 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
672 end_of_full_line = full_line;
673 strcpy(end_of_full_line, raw_mbox_line);
674 while(*end_of_full_line && *end_of_full_line != '\n') {
677 *end_of_full_line = '\0';
682 position_in_file += strlen(raw_mbox_line);
688 void recursive_index_mbox(FILE *db_file,
689 const char *entry_name, regex_t *mbox_filename_regexp,
690 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
692 struct dirent *dir_e;
694 char subname[PATH_MAX + 1];
696 if(lstat(entry_name, &sb) != 0) {
698 "mymail: Cannot stat \"%s\": %s\n",
704 dir = opendir(entry_name);
707 while((dir_e = readdir(dir))) {
708 if(!ignore_entry(dir_e->d_name)) {
709 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
710 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
711 nb_fields_to_parse, fields_to_parse);
716 const char *s = entry_name, *filename = s;
717 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
718 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
719 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
724 /*********************************************************************/
726 /* For long options that have no equivalent short option, use a
727 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
729 OPT_BASH_MODE = CHAR_MAX + 1
732 static struct option long_options[] = {
733 { "help", no_argument, 0, 'h' },
734 { "version", no_argument, 0, 'v' },
735 { "quiet", no_argument, 0, 'q' },
736 { "use-leading-time", no_argument, 0, 't' },
737 { "db-file-generate", 1, 0, 'd' },
738 { "db-pattern", 1, 0, 'p' },
739 { "db-root", 1, 0, 'r' },
740 { "db-list", 1, 0, 'l' },
741 { "mbox-pattern", 1, 0, 'm' },
742 { "search", 1, 0, 's' },
743 { "index", 0, 0, 'i' },
744 { "output", 1, 0, 'o' },
745 { "default-search", 1, 0, 'a' },
749 struct time_criterion {
752 int start_hour, end_hour;
756 /*********************************************************************/
758 static struct time_criterion time_criteria[] = {
760 { "8h", 0, 8, -1, -1 },
761 { "24h", 0, 24, -1, -1 },
762 { "48h", 0, 48, -1, -1 },
763 { "week", 0, 24 * 7, -1, -1 },
764 { "month", 0, 24 * 31, -1, -1 },
765 { "year", 0, 24 * 365, -1, -1 },
767 { "yesterday", 1, -1, -1, -1 },
768 { "today", 1, -1, -1, 0 },
770 { "monday", 1, -1, -1, 1 },
771 { "tuesday", 1, -1, -1, 2 },
772 { "wednesday", 1, -1, -1, 3 },
773 { "thursday", 1, -1, -1, 4 },
774 { "friday", 1, -1, -1, 5 },
775 { "saturday", 1, -1, -1, 6 },
776 { "sunday", 1, -1, -1, 7 },
780 /*********************************************************************/
782 time_t time_for_past_day(int day) {
789 delta_day = (7 + tm->tm_wday - day) % 7;
793 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
796 void init_condition(struct search_condition *condition, const char *full_string,
797 const char *default_search_field) {
798 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
802 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
803 search_field = full_search_field;
805 if(search_field[0] == '!') {
807 condition->negation = 1;
809 condition->negation = 0;
812 condition->field_id = -1;
816 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
817 if(strcmp(time_criteria[k].label, search_field) == 0) {
818 condition->field_id = ID_TIME_INTERVAL;
819 if(time_criteria[k].day_criterion) {
820 condition->interval_start = time_for_past_day(time_criteria[k].past_week_day);
821 condition->interval_stop = condition->interval_start + 3600 * 24;
823 condition->interval_start = time(0) - 3600 * time_criteria[k].start_hour;
824 if(time_criteria[k].end_hour >= 0) {
825 condition->interval_stop = time(0) - 3600 * time_criteria[k].end_hour;
827 condition->interval_stop = 0;
834 if(condition->field_id == -1) {
836 /* No time condition matched, look for the search fields */
838 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
839 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
840 condition->field_id = m;
844 /* None match, if there is a default search field, re-run the search with it */
846 if(condition->field_id == -1) {
847 if(default_search_field) {
848 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
849 if(strncmp(field_keys[m],
850 default_search_field, strlen(default_search_field)) == 0) {
851 condition->field_id = m;
854 string = full_string;
855 if(string[0] == '!') { string++; }
859 if(condition->field_id == -1) {
861 "mymail: Syntax error in field key \"%s\".\n",
866 if(regcomp(&condition->regexp,
870 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
872 field_keys[condition->field_id]);
878 void free_condition(struct search_condition *condition) {
879 if(condition->field_id != ID_TIME_INTERVAL) {
880 regfree(&condition->regexp);
884 /*********************************************************************/
885 /*********************************************************************/
886 /*********************************************************************/
888 int main(int argc, char **argv) {
889 char *db_filename = 0;
890 char *db_filename_regexp_string = 0;
891 char *db_root_path = 0;
892 char *db_filename_list = 0;
893 char *mbox_filename_regexp_string = 0;
894 char *default_search_field;
895 char output_filename[PATH_MAX + 1];
896 int action_index = 0;
897 int error = 0, show_help = 0;
898 const unsigned int nb_fields_to_parse =
899 sizeof(fields_to_parse) / sizeof(struct parsable_field);
902 unsigned int nb_search_conditions;
903 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
905 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
907 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
912 global_use_leading_time = 0;
913 default_search_field = 0;
914 strncpy(output_filename, "", PATH_MAX);
916 setlocale(LC_ALL, "");
918 nb_search_conditions = 0;
920 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
921 long_options, NULL)) != -1) {
930 print_version(stdout);
938 global_use_leading_time = 1;
947 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
950 db_filename = strdup(optarg);
954 if(db_filename_regexp_string) {
955 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
958 db_filename_regexp_string = strdup(optarg);
962 if(mbox_filename_regexp_string) {
963 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
966 mbox_filename_regexp_string = strdup(optarg);
970 strncpy(output_filename, optarg, PATH_MAX);
975 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
978 db_root_path = strdup(optarg);
982 if(db_filename_list) {
983 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
986 db_filename_list = strdup(optarg);
990 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
991 fprintf(stderr, "mymail: Too many search patterns.\n");
994 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
995 nb_search_conditions++;
999 default_search_field = optarg;
1008 /* Set all the values that may defined in the arguments, through
1009 environment variables, or hard-coded */
1011 db_filename = default_value(db_filename,
1015 db_filename_regexp_string = default_value(db_filename_regexp_string,
1019 db_root_path = default_value(db_root_path,
1023 db_filename_list = default_value(db_filename_list,
1027 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1028 "MYMAIL_MBOX_PATTERN",
1031 /* Start the processing */
1034 print_usage(stderr);
1039 print_usage(stdout);
1047 regex_t mbox_filename_regexp_static;
1048 regex_t *mbox_filename_regexp;
1050 if(mbox_filename_regexp_string) {
1051 if(regcomp(&mbox_filename_regexp_static,
1052 mbox_filename_regexp_string,
1055 "mymail: Syntax error in regexp \"%s\".\n",
1056 mbox_filename_regexp_string);
1059 mbox_filename_regexp = &mbox_filename_regexp_static;
1061 mbox_filename_regexp = 0;
1064 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1066 for(f = 0; f < nb_fields_to_parse; f++) {
1067 if(regcomp(&fields_to_parse[f].regexp,
1068 fields_to_parse[f].regexp_string,
1069 fields_to_parse[f].cflags)) {
1071 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1072 fields_to_parse[f].regexp_string,
1073 field_keys[fields_to_parse[f].id]);
1078 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1080 while(optind < argc) {
1081 recursive_index_mbox(db_file,
1082 argv[optind], mbox_filename_regexp,
1083 nb_fields_to_parse, fields_to_parse);
1090 if(mbox_filename_regexp) {
1091 regfree(mbox_filename_regexp);
1094 for(f = 0; f < nb_fields_to_parse; f++) {
1095 regfree(&fields_to_parse[f].regexp);
1104 int nb_extracted_mails = 0;
1106 if(output_filename[0]) {
1107 output_file = safe_fopen(output_filename, "w", "result mbox");
1109 output_file = stdout;
1113 if(nb_search_conditions > 0) {
1115 /* Recursive search if db_root_path is set */
1118 regex_t db_filename_regexp;
1119 if(regcomp(&db_filename_regexp,
1120 db_filename_regexp_string,
1123 "mymail: Syntax error in regexp \"%s\".\n",
1124 db_filename_regexp_string);
1128 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1129 nb_search_conditions, search_conditions,
1132 regfree(&db_filename_regexp);
1135 /* Search in all db files listed in db_filename_list */
1137 if(db_filename_list) {
1138 char db_filename[PATH_MAX + 1];
1141 s = db_filename_list;
1144 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1146 if(db_filename[0]) {
1147 nb_extracted_mails +=
1148 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1153 /* Search in all db files listed in the command arguments */
1155 while(optind < argc) {
1156 nb_extracted_mails +=
1157 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1163 if(nb_extracted_mails > 0) {
1164 printf("Found %d matching mails.\n", nb_extracted_mails);
1166 printf("No matching mail found.\n");
1170 fflush(output_file);
1172 if(output_file != stdout) {
1173 fclose(output_file);
1177 for(n = 0; n < nb_search_conditions; n++) {
1178 free_condition(&search_conditions[n]);
1182 free(db_filename_regexp_string);
1184 free(db_filename_list);
1185 free(mbox_filename_regexp_string);
1187 regfree(&global_leading_from_line_regexp);