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 MYMAIL_VERSION "0.9.8"
51 #define MYMAIL_DB_FORMAT_VERSION 1
53 #define MAX_NB_SEARCH_CONDITIONS 32
55 #define BUFFER_SIZE 65536
56 #define TOKEN_BUFFER_SIZE 1024
58 #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\\) [ 0123][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]\n$"
60 /* Global variables! */
63 int global_use_leading_time;
65 regex_t global_leading_from_line_regexp;
67 /********************************************************************/
82 static char *field_keys[] = {
94 /********************************************************************/
96 struct search_condition {
98 regex_t db_value_regexp;
100 time_t time_start, time_stop;
103 /********************************************************************/
107 struct alias_node *next;
110 struct alias_node *global_alias_list;
112 /********************************************************************/
114 struct parsable_field {
121 static struct parsable_field fields_to_parse[] = {
126 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
132 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
133 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
139 "^\\(to\\|cc\\|bcc\\): ",
140 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
147 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
154 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
159 /********************************************************************/
161 int xor(int a, int b) {
162 return (a && !b) || (!a && b);
165 const char *parse_token(char *token_buffer, size_t token_buffer_size,
166 char separator, const char *string) {
167 char *u = token_buffer;
169 while(*string == separator) { string++; }
171 while(u < token_buffer + token_buffer_size - 1 && *string && *string != separator) {
172 *(u++) = *(string++);
175 while(*string == separator) { string++; }
181 char *default_value(char *current_value,
182 const char *env_variable,
183 const char *hard_default_value) {
185 return current_value;
187 char *env_value = getenv(env_variable);
189 return strdup(env_value);
190 } else if(hard_default_value) {
191 return strdup(hard_default_value);
198 /********************************************************************/
200 void *safe_malloc(size_t n) {
204 "selector: cannot allocate memory: %s\n", strerror(errno));
210 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
211 FILE *result = fopen(path, mode);
216 "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
217 path, comment, mode);
222 /*********************************************************************/
224 void print_version(FILE *out) {
225 fprintf(out, "mymail version %s (%s)\n", MYMAIL_VERSION, UNAME);
228 void print_usage(FILE *out) {
230 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
232 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
234 fprintf(out, " -h, --help\n");
235 fprintf(out, " show this help\n");
236 fprintf(out, " -v, --version\n");
237 fprintf(out, " print the version number\n");
238 fprintf(out, " -q, --quiet\n");
239 fprintf(out, " do not print information during search\n");
240 fprintf(out, " -t, --use-leading-time\n");
241 fprintf(out, " use the time stamp from the leading line of each mail and not the Date:\n");
242 fprintf(out, " field\n");
243 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
244 fprintf(out, " set the db filename pattern for recursive search\n");
245 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
246 fprintf(out, " set the db root path for recursive search\n");
247 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
248 fprintf(out, " set the semicolon-separated list of db files for search\n");
249 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
250 fprintf(out, " set the mbox filename pattern for recursive search\n");
251 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
252 fprintf(out, " search for matching mails in the db file\n");
253 fprintf(out, " -d <db filename>, --db-file-output <db filename>\n");
254 fprintf(out, " set the db filename for indexing\n");
255 fprintf(out, " -i, --index\n");
256 fprintf(out, " index mails\n");
257 fprintf(out, " -o <output filename>, --output <output filename>\n");
258 fprintf(out, " set the result file, use stdout if unset\n");
259 fprintf(out, " -a <search field>, --default-search <search field>\n");
260 fprintf(out, " set the default search field\n");
263 /*********************************************************************/
265 int ignore_entry(const char *name) {
267 strcmp(name, ".") == 0 ||
268 strcmp(name, "..") == 0 ||
269 (name[0] == '.' && name[1] != '/');
272 int is_a_leading_from_line(char *mbox_line) {
274 strncmp(mbox_line, "From ", 5) == 0 &&
275 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
278 int db_line_match_search(struct search_condition *condition,
279 int db_key, const char *db_value) {
283 (condition->db_key == db_key)
287 (condition->db_key == ID_PARTICIPANT && (db_key == ID_LEADING_LINE ||
292 (condition->db_key == ID_FROM && db_key == ID_LEADING_LINE)
298 regexec(&condition->db_value_regexp, db_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 = safe_fopen(mail_filename, "r", "mbox for body scan");
315 fseek(mail_file, position_in_mail, SEEK_SET);
317 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
318 while(nb_body_hits < nb_body_conditions) {
319 if(raw_mbox_line[0] == '\n') { header = 0; }
322 for(n = 0; n < nb_search_conditions; n++) {
323 if(search_conditions[n].db_key == ID_BODY && !hits[n]) {
325 (regexec(&search_conditions[n].db_value_regexp, raw_mbox_line, 0, 0, 0) == 0);
333 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
334 (is_a_leading_from_line(raw_mbox_line)))
342 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
344 char raw_mbox_line[BUFFER_SIZE];
347 /* printf("Extract\n"); */
349 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
350 fseek(mail_file, position_in_mail, SEEK_SET);
352 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
353 fprintf(output_file, "%s", raw_mbox_line);
355 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
356 is_a_leading_from_line(raw_mbox_line))
358 fprintf(output_file, "%s", raw_mbox_line);
365 int check_full_mail_match(char *current_mail_filename,
367 int nb_search_conditions,
368 struct search_condition *search_conditions,
369 int nb_body_conditions,
371 int current_position_in_mail) {
372 int n, nb_fulfilled_body_conditions;
374 for(n = 0; n < nb_search_conditions; n++) {
375 if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
376 hits[n] = (mail_time >= search_conditions[n].time_start &&
377 (search_conditions[n].time_stop == 0 ||
378 mail_time <= search_conditions[n].time_stop));
382 /* We first check all conditions but the body ones */
384 for(n = 0; n < nb_search_conditions &&
385 ((search_conditions[n].db_key == ID_BODY) ||
386 xor(hits[n], search_conditions[n].negation)); n++);
388 if(n == nb_search_conditions) {
390 /* Now check the body ones */
392 nb_fulfilled_body_conditions = 0;
394 if(nb_body_conditions > 0) {
395 update_body_hits(current_mail_filename, current_position_in_mail,
396 nb_search_conditions, search_conditions,
400 for(n = 0; n < nb_search_conditions; n++) {
401 if(search_conditions[n].db_key == ID_BODY &&
402 xor(hits[n], search_conditions[n].negation)) {
403 nb_fulfilled_body_conditions++;
407 return nb_body_conditions == nb_fulfilled_body_conditions;
413 /* We use the mail leading line time by default, and if we should and
414 can, we update with the Date: field */
416 void update_time(int db_key, const char *db_value, time_t *t) {
420 memset(&tm, 0, sizeof(struct tm));
422 if(db_key == ID_LEADING_LINE) {
424 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
425 /* printf("From %s", db_value); */
426 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
429 if(!global_use_leading_time) {
430 if(db_key == ID_DATE) {
431 if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
432 strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
433 /* printf("Date: %s", db_value); */
441 int search_in_db(const char *db_filename,
442 int nb_search_conditions,
443 struct search_condition *search_conditions,
447 char raw_db_line[BUFFER_SIZE];
448 char current_mail_filename[PATH_MAX + 1];
449 char db_key_string[TOKEN_BUFFER_SIZE];
450 char position_in_file_string[TOKEN_BUFFER_SIZE];
451 unsigned long int current_position_in_mail;
452 const char *db_value;
454 int hits[MAX_NB_SEARCH_CONDITIONS];
455 int nb_body_conditions, need_time;
456 int nb_extracted_mails;
461 nb_extracted_mails = 0;
464 printf("Searching in '%s' ... ", db_filename);
468 db_file = safe_fopen(db_filename, "r", "index file for search");
470 /* First, check the db file leading line integrity */
472 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
473 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
475 "mymail: Header line in '%s' does not match the mymail db format.\n",
481 "mymail: Cannot read the header line in '%s'.\n",
486 /* Then parse the said db file */
488 current_position_in_mail = 0;
490 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
492 nb_body_conditions = 0;
496 for(n = 0; n < nb_search_conditions; n++) {
497 if(search_conditions[n].db_key == ID_BODY) {
498 nb_body_conditions++;
500 else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
505 strcpy(current_mail_filename, "");
507 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
508 db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
510 if(strcmp("mail", db_key_string) == 0) {
511 if(current_mail_filename[0]) {
512 if(check_full_mail_match(current_mail_filename,
514 nb_search_conditions, search_conditions,
515 nb_body_conditions, hits, current_position_in_mail)) {
516 extract_mail(current_mail_filename, current_position_in_mail, output_file);
517 nb_extracted_mails++;
521 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
522 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
523 db_value = parse_token(current_mail_filename, PATH_MAX+1, '\n', db_value);
524 current_position_in_mail = atol(position_in_file_string);
529 for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
530 if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
535 for(n = 0; n < nb_search_conditions; n++) {
536 hits[n] |= db_line_match_search(&search_conditions[n],
541 update_time(db_key, db_value, &mail_time);
546 if(current_mail_filename[0]) {
547 if(check_full_mail_match(current_mail_filename,
549 nb_search_conditions, search_conditions,
550 nb_body_conditions, hits, current_position_in_mail)) {
551 extract_mail(current_mail_filename, current_position_in_mail, output_file);
552 nb_extracted_mails++;
563 return nb_extracted_mails;
566 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
567 int nb_search_conditions,
568 struct search_condition *search_conditions,
571 struct dirent *dir_e;
573 char subname[PATH_MAX + 1];
574 int nb_extracted_mails = 0;
576 if(lstat(entry_name, &sb) != 0) {
578 "mymail: Cannot stat \"%s\": %s\n",
584 /* printf("recursive_search_in_db %s\n", entry_name); */
586 dir = opendir(entry_name);
589 while((dir_e = readdir(dir))) {
590 if(!ignore_entry(dir_e->d_name)) {
591 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
592 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
593 nb_search_conditions, search_conditions,
601 const char *s = entry_name, *filename = entry_name;
602 while(*s) { if(*s == '/') { filename = s+1; } s++; }
604 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
605 nb_extracted_mails +=
606 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
610 return nb_extracted_mails;
613 /*********************************************************************/
615 void index_one_mbox_line(unsigned int nb_fields_to_parse,
616 struct parsable_field *fields_to_parse,
617 char *raw_mbox_line, FILE *db_file) {
620 for(f = 0; f < nb_fields_to_parse; f++) {
621 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
622 fprintf(db_file, "%s %s\n",
623 field_keys[fields_to_parse[f].id],
624 raw_mbox_line + matches.rm_eo);
629 void index_mbox(const char *mbox_filename,
630 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
632 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
633 char *end_of_full_line;
635 int in_header, new_header;
636 unsigned long int position_in_file;
638 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
643 position_in_file = 0;
644 end_of_full_line = 0;
647 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
648 if(is_a_leading_from_line(raw_mbox_line)) {
649 /* This starts a new mail */
652 "Got a ^\"From \" in the header in %s:%lu.\n",
653 mbox_filename, position_in_file);
654 fprintf(stderr, "%s", raw_mbox_line);
657 /* printf("LEADING_LINE %s", raw_mbox_line); */
661 } else if(raw_mbox_line[0] == '\n') {
664 /* We leave the header, index the current line */
666 /* printf("INDEX %s\n", full_line); */
667 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
669 end_of_full_line = full_line;
670 *end_of_full_line = '\0';
676 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
680 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
681 /* Continuation of a line */
682 char *start = raw_mbox_line;
683 while(*start == ' ' || *start == '\t') start++;
684 *(end_of_full_line++) = ' ';
685 strcpy(end_of_full_line, start);
686 while(*end_of_full_line && *end_of_full_line != '\n') {
689 *end_of_full_line = '\0';
693 /* Start a new header line, not a continuation */
696 /* printf("INDEX %s\n", full_line); */
697 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
700 end_of_full_line = full_line;
701 strcpy(end_of_full_line, raw_mbox_line);
702 while(*end_of_full_line && *end_of_full_line != '\n') {
705 *end_of_full_line = '\0';
710 position_in_file += strlen(raw_mbox_line);
716 void recursive_index_mbox(FILE *db_file,
717 const char *entry_name, regex_t *mbox_filename_regexp,
718 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
720 struct dirent *dir_e;
722 char subname[PATH_MAX + 1];
724 if(lstat(entry_name, &sb) != 0) {
726 "mymail: Cannot stat \"%s\": %s\n",
732 dir = opendir(entry_name);
735 while((dir_e = readdir(dir))) {
736 if(!ignore_entry(dir_e->d_name)) {
737 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
738 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
739 nb_fields_to_parse, fields_to_parse);
744 const char *s = entry_name, *filename = s;
745 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
746 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
747 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
752 /*********************************************************************/
754 /* For long options that have no equivalent short option, use a
755 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
757 OPT_BASH_MODE = CHAR_MAX + 1
760 static struct option long_options[] = {
761 { "help", no_argument, 0, 'h' },
762 { "version", no_argument, 0, 'v' },
763 { "quiet", no_argument, 0, 'q' },
764 { "use-leading-time", no_argument, 0, 't' },
765 { "db-file-output", 1, 0, 'd' },
766 { "db-pattern", 1, 0, 'p' },
767 { "db-root", 1, 0, 'r' },
768 { "db-list", 1, 0, 'l' },
769 { "mbox-pattern", 1, 0, 'm' },
770 { "search", 1, 0, 's' },
771 { "index", 0, 0, 'i' },
772 { "output", 1, 0, 'o' },
773 { "default-search", 1, 0, 'a' },
777 struct time_criterion {
780 int start_hour, end_hour;
784 /*********************************************************************/
786 static struct time_criterion time_criteria[] = {
788 { "8h", 0, 8, -1, -1 },
789 { "24h", 0, 24, -1, -1 },
790 { "48h", 0, 48, -1, -1 },
791 { "week", 0, 24 * 7, -1, -1 },
792 { "month", 0, 24 * 31, -1, -1 },
793 { "trimester", 0, 24 * 92, -1, -1 },
794 { "year", 0, 24 * 365, -1, -1 },
796 { "yesterday", 1, -1, -1, -1 },
797 { "today", 1, -1, -1, 0 },
799 { "monday", 1, -1, -1, 1 },
800 { "tuesday", 1, -1, -1, 2 },
801 { "wednesday", 1, -1, -1, 3 },
802 { "thursday", 1, -1, -1, 4 },
803 { "friday", 1, -1, -1, 5 },
804 { "saturday", 1, -1, -1, 6 },
805 { "sunday", 1, -1, -1, 7 },
809 /*********************************************************************/
811 time_t time_for_past_day(int day) {
818 delta_day = (7 + tm->tm_wday - day) % 7;
822 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
825 void init_condition(struct search_condition *condition, const char *full_string,
826 const char *default_search_field) {
827 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
830 struct alias_node *a;
832 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
833 search_field = full_search_field;
835 if(search_field[0] == '!') {
837 condition->negation = 1;
839 condition->negation = 0;
842 for(a = global_alias_list; a; a = a->next) {
843 if(strcmp(search_field, a->alias) == 0) {
844 search_field = a->value;
849 condition->db_key = -1;
853 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
854 if(strcmp(time_criteria[k].label, search_field) == 0) {
855 condition->db_key = ID_TIME_INTERVAL;
856 if(time_criteria[k].day_criterion) {
857 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
858 condition->time_stop = condition->time_start + 3600 * 24;
860 condition->time_start = time(0) - 3600 * time_criteria[k].start_hour;
861 if(time_criteria[k].end_hour >= 0) {
862 condition->time_stop = time(0) - 3600 * time_criteria[k].end_hour;
864 condition->time_stop = 0;
872 if(condition->db_key == -1) {
874 /* No time condition matched, look for the search fields */
876 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
877 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
878 condition->db_key = m;
882 /* None match, if there is a default search field, re-run the search with it */
884 if(condition->db_key == -1) {
885 if(default_search_field) {
886 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
887 if(strncmp(field_keys[m],
888 default_search_field, strlen(default_search_field)) == 0) {
889 condition->db_key = m;
892 string = full_string;
893 if(string[0] == '!') { string++; }
897 if(condition->db_key == -1) {
899 "mymail: Syntax error in field key \"%s\".\n",
904 if(regcomp(&condition->db_value_regexp,
908 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
910 field_keys[condition->db_key]);
916 void free_condition(struct search_condition *condition) {
917 if(condition->db_key != ID_TIME_INTERVAL) {
918 regfree(&condition->db_value_regexp);
922 const char *eat_space(const char *s) {
923 while(*s == ' ' || *s == '\t') { s++; }
927 void read_rc_file(const char *rc_filename) {
928 char raw_line[BUFFER_SIZE];
929 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
936 rc_file = fopen(rc_filename, "r");
940 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
942 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
947 if(*s && *s != '#') {
948 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
949 if(strcmp(command, "alias") == 0) {
950 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
951 a->next = global_alias_list;
952 global_alias_list = a;
955 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
956 a->alias = strdup(tmp_token);
959 a->value = strdup(s);
961 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
967 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
973 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
987 /*********************************************************************/
988 /*********************************************************************/
989 /*********************************************************************/
991 int main(int argc, char **argv) {
992 char *db_filename = 0;
993 char *db_filename_regexp_string = 0;
994 char *db_root_path = 0;
995 char *db_filename_list = 0;
996 char *mbox_filename_regexp_string = 0;
997 char *default_search_field;
998 char output_filename[PATH_MAX + 1];
999 char rc_filename[PATH_MAX + 1];
1000 int action_index = 0;
1001 int error = 0, show_help = 0;
1002 const unsigned int nb_fields_to_parse =
1003 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1006 unsigned int nb_search_conditions;
1007 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1008 struct alias_node *a, *b;
1010 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1012 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1016 if(getenv("MYMAILRC")) {
1017 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1018 } else if(getenv("HOME")) {
1019 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1021 rc_filename[0] = '\0';
1024 global_alias_list = 0;
1026 global_use_leading_time = 0;
1027 default_search_field = 0;
1028 strncpy(output_filename, "", PATH_MAX);
1030 if(rc_filename[0]) {
1031 read_rc_file(rc_filename);
1034 setlocale(LC_ALL, "");
1036 nb_search_conditions = 0;
1038 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
1039 long_options, NULL)) != -1) {
1048 print_version(stdout);
1056 global_use_leading_time = 1;
1065 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1068 db_filename = strdup(optarg);
1072 if(db_filename_regexp_string) {
1073 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1076 db_filename_regexp_string = strdup(optarg);
1080 if(mbox_filename_regexp_string) {
1081 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1084 mbox_filename_regexp_string = strdup(optarg);
1088 strncpy(output_filename, optarg, PATH_MAX);
1093 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1096 db_root_path = strdup(optarg);
1100 if(db_filename_list) {
1101 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1104 db_filename_list = strdup(optarg);
1108 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1109 fprintf(stderr, "mymail: Too many search patterns.\n");
1112 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1113 nb_search_conditions++;
1117 default_search_field = optarg;
1126 /* Set all the values that may defined in the arguments, through
1127 environment variables, or hard-coded */
1129 db_filename = default_value(db_filename,
1133 db_filename_regexp_string = default_value(db_filename_regexp_string,
1137 db_root_path = default_value(db_root_path,
1141 db_filename_list = default_value(db_filename_list,
1145 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1146 "MYMAIL_MBOX_PATTERN",
1149 /* Start the processing */
1152 print_usage(stderr);
1157 print_usage(stdout);
1165 regex_t mbox_filename_regexp_static;
1166 regex_t *mbox_filename_regexp;
1168 if(mbox_filename_regexp_string) {
1169 if(regcomp(&mbox_filename_regexp_static,
1170 mbox_filename_regexp_string,
1173 "mymail: Syntax error in regexp \"%s\".\n",
1174 mbox_filename_regexp_string);
1177 mbox_filename_regexp = &mbox_filename_regexp_static;
1179 mbox_filename_regexp = 0;
1182 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1184 for(f = 0; f < nb_fields_to_parse; f++) {
1185 if(regcomp(&fields_to_parse[f].regexp,
1186 fields_to_parse[f].regexp_string,
1187 fields_to_parse[f].cflags)) {
1189 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1190 fields_to_parse[f].regexp_string,
1191 field_keys[fields_to_parse[f].id]);
1197 "%s version_%s format_%d raw\n",
1198 MYMAIL_DB_MAGIC_TOKEN,
1200 MYMAIL_DB_FORMAT_VERSION);
1202 while(optind < argc) {
1203 recursive_index_mbox(db_file,
1204 argv[optind], mbox_filename_regexp,
1205 nb_fields_to_parse, fields_to_parse);
1212 if(mbox_filename_regexp) {
1213 regfree(mbox_filename_regexp);
1216 for(f = 0; f < nb_fields_to_parse; f++) {
1217 regfree(&fields_to_parse[f].regexp);
1226 int nb_extracted_mails = 0;
1228 if(output_filename[0]) {
1229 output_file = safe_fopen(output_filename, "w", "result mbox");
1231 output_file = stdout;
1235 if(nb_search_conditions > 0) {
1237 /* Recursive search if db_root_path is set */
1240 regex_t db_filename_regexp;
1241 if(regcomp(&db_filename_regexp,
1242 db_filename_regexp_string,
1245 "mymail: Syntax error in regexp \"%s\".\n",
1246 db_filename_regexp_string);
1250 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1251 nb_search_conditions, search_conditions,
1254 regfree(&db_filename_regexp);
1257 /* Search in all db files listed in db_filename_list */
1259 if(db_filename_list) {
1260 char db_filename[PATH_MAX + 1];
1263 s = db_filename_list;
1266 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1268 if(db_filename[0]) {
1269 nb_extracted_mails +=
1270 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1275 /* Search in all db files listed in the command arguments */
1277 while(optind < argc) {
1278 nb_extracted_mails +=
1279 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1285 if(nb_extracted_mails > 0) {
1286 printf("Found %d matching mails.\n", nb_extracted_mails);
1288 printf("No matching mail found.\n");
1292 fflush(output_file);
1294 if(output_file != stdout) {
1295 fclose(output_file);
1299 for(n = 0; n < nb_search_conditions; n++) {
1300 free_condition(&search_conditions[n]);
1303 a = global_alias_list;
1313 free(db_filename_regexp_string);
1315 free(db_filename_list);
1316 free(mbox_filename_regexp_string);
1318 regfree(&global_leading_from_line_regexp);