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 mymail is a simple mail indexer. It can:
26 (1) scan mbox files, and create a db file containing for each mail a
27 list of fields computed from its header.
29 (2) read such a db file, gets all the mails matching regexp-defined
30 conditions on the fields, and generates a resulting mbox file.
32 It is low-tech, simple, light and fast.
50 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
51 #define MYMAIL_VERSION "0.9.9"
53 #define MYMAIL_DB_FORMAT_VERSION 1
55 #define MAX_NB_SEARCH_CONDITIONS 32
57 #define BUFFER_SIZE 65536
58 #define TOKEN_BUFFER_SIZE 1024
60 #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$"
62 /********************************************************************/
66 struct alias_node *next;
69 /* Global variables! */
72 int global_use_leading_time;
73 int global_nb_mails_max;
74 regex_t global_leading_from_line_regexp;
75 struct alias_node *global_alias_list;
77 /********************************************************************/
95 static char *field_keys[] = {
110 /********************************************************************/
112 struct search_condition {
114 regex_t db_value_regexp;
116 time_t time_start, time_stop;
119 /********************************************************************/
121 struct parsable_field {
128 static struct parsable_field fields_to_parse[] = {
133 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
139 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
140 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
146 "^\\(to\\|cc\\|bcc\\): ",
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 }
161 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
168 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
174 "^\\(in-reply-to\\|references\\): ",
175 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
180 /********************************************************************/
182 int xor(int a, int b) {
183 return (a && !b) || (!a && b);
186 const char *parse_token(char *token_buffer, size_t token_buffer_size,
187 char separator, const char *string) {
188 char *u = token_buffer;
190 while(*string == separator) { string++; }
192 while(u < token_buffer + token_buffer_size - 1 && *string && *string != separator) {
193 *(u++) = *(string++);
196 while(*string == separator) { string++; }
202 char *default_value(char *current_value,
203 const char *env_variable,
204 const char *hard_default_value) {
206 return current_value;
208 char *env_value = getenv(env_variable);
210 return strdup(env_value);
211 } else if(hard_default_value) {
212 return strdup(hard_default_value);
219 /********************************************************************/
221 void *safe_malloc(size_t n) {
225 "mymail: cannot allocate memory: %s\n", strerror(errno));
231 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
232 FILE *result = fopen(path, mode);
237 "mymail: Cannot open file '%s' (%s) with mode \"%s\": %s\n",
244 /*********************************************************************/
246 void print_version(FILE *out) {
247 fprintf(out, "mymail version %s (%s)\n", MYMAIL_VERSION, UNAME);
250 void print_usage(FILE *out) {
252 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
254 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
256 fprintf(out, " -h, --help\n");
257 fprintf(out, " show this help\n");
258 fprintf(out, " -v, --version\n");
259 fprintf(out, " print the version number\n");
260 fprintf(out, " -q, --quiet\n");
261 fprintf(out, " do not print information during search\n");
262 fprintf(out, " -t, --use-leading-time\n");
263 fprintf(out, " use the time stamp from the leading line of each mail and not the Date:\n");
264 fprintf(out, " field\n");
265 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
266 fprintf(out, " set the db filename pattern for recursive search\n");
267 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
268 fprintf(out, " set the db root path for recursive search\n");
269 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
270 fprintf(out, " set the semicolon-separated list of db files for search\n");
271 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
272 fprintf(out, " set the mbox filename pattern for recursive search\n");
273 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
274 fprintf(out, " search for matching mails in the db file\n");
275 fprintf(out, " -d <db filename>, --db-file-output <db filename>\n");
276 fprintf(out, " set the db filename for indexing\n");
277 fprintf(out, " -i, --index\n");
278 fprintf(out, " index mails\n");
279 fprintf(out, " -o <output filename>, --output <output filename>\n");
280 fprintf(out, " set the result file, use stdout if unset\n");
281 fprintf(out, " -n <max number of mails>, --nb-mails-max <max number of mails>\n");
282 fprintf(out, " set the maximum number of mails to extract\n");
283 fprintf(out, " -a <search field>, --default-search <search field>\n");
284 fprintf(out, " set the default search field\n");
287 /*********************************************************************/
289 int ignore_entry(const char *name) {
291 strcmp(name, ".") == 0 ||
292 strcmp(name, "..") == 0 ||
293 (name[0] == '.' && name[1] != '/');
296 int is_a_leading_from_line(char *mbox_line) {
298 strncmp(mbox_line, "From ", 5) == 0 &&
299 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
302 int db_line_match_search(struct search_condition *condition,
303 int db_key, const char *db_value) {
307 (condition->db_key == db_key)
311 (condition->db_key == ID_PARTICIPANT && (db_key == ID_LEADING_LINE ||
316 (condition->db_key == ID_FROM && db_key == ID_LEADING_LINE)
320 (condition->db_key == ID_THREAD_ID && (db_key == ID_MAIL_ID ||
321 db_key == ID_REFERENCE_ID))
326 regexec(&condition->db_value_regexp, db_value, 0, 0, 0) == 0;
329 void update_body_hits(char *mail_filename, int position_in_mail,
330 int nb_search_conditions, struct search_condition *search_conditions,
331 int nb_body_conditions,
335 char raw_mbox_line[BUFFER_SIZE];
341 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
343 fseek(mail_file, position_in_mail, SEEK_SET);
345 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
346 while(nb_body_hits < nb_body_conditions) {
347 if(raw_mbox_line[0] == '\n') { header = 0; }
350 for(n = 0; n < nb_search_conditions; n++) {
351 if(search_conditions[n].db_key == ID_BODY && !hits[n]) {
353 (regexec(&search_conditions[n].db_value_regexp, raw_mbox_line, 0, 0, 0) == 0);
361 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
362 (is_a_leading_from_line(raw_mbox_line)))
370 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
372 char raw_mbox_line[BUFFER_SIZE];
375 /* printf("Extract\n"); */
377 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
378 fseek(mail_file, position_in_mail, SEEK_SET);
380 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
381 fprintf(output_file, "%s", raw_mbox_line);
383 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
384 is_a_leading_from_line(raw_mbox_line))
386 fprintf(output_file, "%s", raw_mbox_line);
393 int check_full_mail_match(char *current_mail_filename,
395 int nb_search_conditions,
396 struct search_condition *search_conditions,
397 int nb_body_conditions,
399 int current_position_in_mail) {
400 int n, nb_fulfilled_body_conditions;
402 for(n = 0; n < nb_search_conditions; n++) {
403 if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
404 hits[n] = (mail_time >= search_conditions[n].time_start &&
405 (search_conditions[n].time_stop == 0 ||
406 mail_time <= search_conditions[n].time_stop));
410 /* We first check all conditions but the body ones */
412 for(n = 0; n < nb_search_conditions &&
413 ((search_conditions[n].db_key == ID_BODY) ||
414 xor(hits[n], search_conditions[n].negation)); n++);
416 if(n == nb_search_conditions) {
418 /* Now check the body ones */
420 nb_fulfilled_body_conditions = 0;
422 if(nb_body_conditions > 0) {
423 update_body_hits(current_mail_filename, current_position_in_mail,
424 nb_search_conditions, search_conditions,
428 for(n = 0; n < nb_search_conditions; n++) {
429 if(search_conditions[n].db_key == ID_BODY &&
430 xor(hits[n], search_conditions[n].negation)) {
431 nb_fulfilled_body_conditions++;
435 return nb_body_conditions == nb_fulfilled_body_conditions;
441 /* We use the mail leading line time by default, and if we should and
442 can, we update with the Date: field */
444 void update_time(int db_key, const char *db_value, time_t *t) {
448 memset(&tm, 0, sizeof(struct tm));
450 if(db_key == ID_LEADING_LINE) {
452 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
453 /* printf("From %s", db_value); */
454 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
457 if(!global_use_leading_time) {
458 if(db_key == ID_DATE) {
459 if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
460 strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
461 /* printf("Date: %s", db_value); */
469 int search_in_db(const char *db_filename,
470 int nb_extracted_mails,
471 int nb_search_conditions,
472 struct search_condition *search_conditions,
476 char raw_db_line[BUFFER_SIZE];
477 char current_mail_filename[PATH_MAX + 1];
478 char db_key_string[TOKEN_BUFFER_SIZE];
479 char position_in_file_string[TOKEN_BUFFER_SIZE];
480 unsigned long int current_position_in_mail;
481 const char *db_value;
483 int hits[MAX_NB_SEARCH_CONDITIONS];
484 int nb_body_conditions, need_time;
490 printf("Searching in '%s' ... ", db_filename);
494 db_file = safe_fopen(db_filename, "r", "index file for search");
496 /* First, check the db file leading line integrity */
498 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
499 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
501 "mymail: Header line in '%s' does not match the mymail db format.\n",
507 "mymail: Cannot read the header line in '%s'.\n",
512 /* Then parse the said db file */
514 current_position_in_mail = 0;
516 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
518 nb_body_conditions = 0;
522 for(n = 0; n < nb_search_conditions; n++) {
523 if(search_conditions[n].db_key == ID_BODY) {
524 nb_body_conditions++;
526 else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
531 strcpy(current_mail_filename, "");
533 while(nb_extracted_mails < global_nb_mails_max &&
534 fgets(raw_db_line, BUFFER_SIZE, db_file)) {
537 char *s = raw_db_line;
538 while(*s && *s != '\n') { s++; }
541 db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
543 if(strcmp("mail", db_key_string) == 0) {
544 if(current_mail_filename[0]) {
545 if(check_full_mail_match(current_mail_filename,
547 nb_search_conditions, search_conditions,
548 nb_body_conditions, hits, current_position_in_mail)) {
549 extract_mail(current_mail_filename, current_position_in_mail, output_file);
550 nb_extracted_mails++;
554 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
555 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
556 strncpy(current_mail_filename, db_value, PATH_MAX + 1);
557 current_position_in_mail = atol(position_in_file_string);
562 for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
563 if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
568 for(n = 0; n < nb_search_conditions; n++) {
569 hits[n] |= db_line_match_search(&search_conditions[n],
574 update_time(db_key, db_value, &mail_time);
579 if(nb_extracted_mails < global_nb_mails_max &&
580 current_mail_filename[0] &&
581 check_full_mail_match(current_mail_filename,
583 nb_search_conditions, search_conditions,
584 nb_body_conditions, hits, current_position_in_mail)) {
585 extract_mail(current_mail_filename, current_position_in_mail, output_file);
586 nb_extracted_mails++;
596 return nb_extracted_mails;
599 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
600 int nb_extracted_mails,
601 int nb_search_conditions,
602 struct search_condition *search_conditions,
605 struct dirent *dir_e;
607 char subname[PATH_MAX + 1];
609 if(lstat(entry_name, &sb) != 0) {
611 "mymail: Cannot stat \"%s\": %s\n",
617 /* printf("recursive_search_in_db %s\n", entry_name); */
619 dir = opendir(entry_name);
622 while((dir_e = readdir(dir)) &&
623 nb_extracted_mails < global_nb_mails_max) {
624 if(!ignore_entry(dir_e->d_name)) {
625 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
626 nb_extracted_mails = recursive_search_in_db(subname, db_filename_regexp,
628 nb_search_conditions, search_conditions,
636 const char *s = entry_name, *filename = entry_name;
637 while(*s) { if(*s == '/') { filename = s+1; } s++; }
639 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
641 search_in_db(entry_name,
643 nb_search_conditions, search_conditions, output_file);
647 return nb_extracted_mails;
650 /*********************************************************************/
652 void index_one_mbox_line(unsigned int nb_fields_to_parse,
653 struct parsable_field *fields_to_parse,
654 char *raw_mbox_line, FILE *db_file) {
657 for(f = 0; f < nb_fields_to_parse; f++) {
658 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
659 fprintf(db_file, "%s %s\n",
660 field_keys[fields_to_parse[f].id],
661 raw_mbox_line + matches.rm_eo);
666 void index_mbox(const char *mbox_filename,
667 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
669 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
670 char *end_of_full_line;
672 int in_header, new_header;
673 unsigned long int position_in_file;
675 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
680 position_in_file = 0;
681 end_of_full_line = 0;
684 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
685 if(is_a_leading_from_line(raw_mbox_line)) {
686 /* This starts a new mail */
689 "Got a ^\"From \" in the header in %s:%lu.\n",
690 mbox_filename, position_in_file);
691 fprintf(stderr, "%s", raw_mbox_line);
694 /* printf("LEADING_LINE %s", raw_mbox_line); */
698 } else if(raw_mbox_line[0] == '\n') {
701 /* We leave the header, index the current line */
703 /* printf("INDEX %s\n", full_line); */
704 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
706 end_of_full_line = full_line;
707 *end_of_full_line = '\0';
713 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
717 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
718 /* Continuation of a line */
719 char *start = raw_mbox_line;
720 while(*start == ' ' || *start == '\t') start++;
721 *(end_of_full_line++) = ' ';
722 strcpy(end_of_full_line, start);
723 while(*end_of_full_line && *end_of_full_line != '\n') {
726 *end_of_full_line = '\0';
730 /* Start a new header line, not a continuation */
733 /* printf("INDEX %s\n", full_line); */
734 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
737 end_of_full_line = full_line;
738 strcpy(end_of_full_line, raw_mbox_line);
739 while(*end_of_full_line && *end_of_full_line != '\n') {
742 *end_of_full_line = '\0';
747 position_in_file += strlen(raw_mbox_line);
753 void recursive_index_mbox(FILE *db_file,
754 const char *entry_name, regex_t *mbox_filename_regexp,
755 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
757 struct dirent *dir_e;
759 char subname[PATH_MAX + 1];
761 if(lstat(entry_name, &sb) != 0) {
763 "mymail: Cannot stat \"%s\": %s\n",
769 dir = opendir(entry_name);
772 while((dir_e = readdir(dir))) {
773 if(!ignore_entry(dir_e->d_name)) {
774 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
775 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
776 nb_fields_to_parse, fields_to_parse);
781 const char *s = entry_name, *filename = s;
782 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
783 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
784 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
789 /*********************************************************************/
791 /* For long options that have no equivalent short option, use a
792 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
794 OPT_BASH_MODE = CHAR_MAX + 1
797 static struct option long_options[] = {
798 { "help", no_argument, 0, 'h' },
799 { "version", no_argument, 0, 'v' },
800 { "quiet", no_argument, 0, 'q' },
801 { "use-leading-time", no_argument, 0, 't' },
802 { "db-file-output", 1, 0, 'd' },
803 { "db-pattern", 1, 0, 'p' },
804 { "db-root", 1, 0, 'r' },
805 { "db-list", 1, 0, 'l' },
806 { "mbox-pattern", 1, 0, 'm' },
807 { "search", 1, 0, 's' },
808 { "index", 0, 0, 'i' },
809 { "output", 1, 0, 'o' },
810 { "default-search", 1, 0, 'a' },
811 { "nb-mails-max", 1, 0, 'n' },
815 struct time_criterion {
818 int start_hour, end_hour;
822 /*********************************************************************/
824 static struct time_criterion time_criteria[] = {
826 { "8h", 0, 8, -1, -1 },
827 { "24h", 0, 24, -1, -1 },
828 { "48h", 0, 48, -1, -1 },
829 { "week", 0, 24 * 7, -1, -1 },
830 { "2weeks", 0, 24 * 14, -1, -1 },
831 { "month", 0, 24 * 31, -1, -1 },
832 { "trimester", 0, 24 * 92, -1, -1 },
833 { "year", 0, 24 * 365, -1, -1 },
835 { "yesterday", 1, -1, -1, -1 },
836 { "today", 1, -1, -1, 0 },
838 { "monday", 1, -1, -1, 1 },
839 { "tuesday", 1, -1, -1, 2 },
840 { "wednesday", 1, -1, -1, 3 },
841 { "thursday", 1, -1, -1, 4 },
842 { "friday", 1, -1, -1, 5 },
843 { "saturday", 1, -1, -1, 6 },
844 { "sunday", 1, -1, -1, 7 },
848 /*********************************************************************/
850 time_t time_for_past_day(int day) {
857 delta_day = (7 + tm->tm_wday - day) % 7;
861 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
864 void init_condition(struct search_condition *condition, const char *full_string,
865 const char *default_search_field) {
866 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
869 struct alias_node *a;
871 for(a = global_alias_list; a; a = a->next) {
872 if(strcmp(full_string, a->alias) == 0) {
873 full_string = a->value;
878 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
879 search_field = full_search_field;
881 if(search_field[0] == '!') {
883 condition->negation = 1;
885 condition->negation = 0;
888 condition->db_key = -1;
892 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
893 if(strcmp(time_criteria[k].label, search_field) == 0) {
894 condition->db_key = ID_TIME_INTERVAL;
895 if(time_criteria[k].day_criterion) {
896 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
897 condition->time_stop = condition->time_start + 3600 * 24;
899 condition->time_start = time(0) - 3600 * time_criteria[k].start_hour;
900 if(time_criteria[k].end_hour >= 0) {
901 condition->time_stop = time(0) - 3600 * time_criteria[k].end_hour;
903 condition->time_stop = 0;
911 if(condition->db_key == -1) {
913 /* No time condition matched, look for the search fields */
915 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
916 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
917 condition->db_key = m;
921 /* None match, if there is a default search field, re-run the search with it */
923 if(condition->db_key == -1) {
924 if(default_search_field) {
925 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
926 if(strncmp(field_keys[m],
927 default_search_field, strlen(default_search_field)) == 0) {
928 condition->db_key = m;
931 string = full_string;
932 if(string[0] == '!') { string++; }
936 if(condition->db_key == -1) {
938 "mymail: Syntax error in field key \"%s\".\n",
943 if(regcomp(&condition->db_value_regexp,
947 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
949 field_keys[condition->db_key]);
955 void free_condition(struct search_condition *condition) {
956 if(condition->db_key != ID_TIME_INTERVAL) {
957 regfree(&condition->db_value_regexp);
961 const char *eat_space(const char *s) {
962 while(*s == ' ' || *s == '\t') { s++; }
966 void read_rc_file(const char *rc_filename) {
967 char raw_line[BUFFER_SIZE];
968 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
975 rc_file = fopen(rc_filename, "r");
979 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
981 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
986 if(*s && *s != '#') {
987 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
989 if(strcmp(command, "alias") == 0) {
990 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
991 a->next = global_alias_list;
992 global_alias_list = a;
995 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
996 a->alias = strdup(tmp_token);
999 a->value = strdup(s);
1001 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
1007 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
1013 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
1027 /*********************************************************************/
1028 /*********************************************************************/
1029 /*********************************************************************/
1031 int main(int argc, char **argv) {
1032 char *db_filename = 0;
1033 char *db_filename_regexp_string = 0;
1034 char *db_root_path = 0;
1035 char *db_filename_list = 0;
1036 char *mbox_filename_regexp_string = 0;
1037 char *default_search_field;
1038 char output_filename[PATH_MAX + 1];
1039 char rc_filename[PATH_MAX + 1];
1040 int action_index = 0;
1041 int error = 0, show_help = 0;
1042 const unsigned int nb_fields_to_parse =
1043 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1046 unsigned int nb_search_conditions;
1047 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1048 struct alias_node *a, *b;
1050 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1052 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1056 if(getenv("MYMAILRC")) {
1057 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1058 } else if(getenv("HOME")) {
1059 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1061 rc_filename[0] = '\0';
1064 global_alias_list = 0;
1066 global_use_leading_time = 0;
1067 global_nb_mails_max = 250;
1069 default_search_field = 0;
1070 strncpy(output_filename, "", PATH_MAX);
1072 if(rc_filename[0]) {
1073 read_rc_file(rc_filename);
1078 #warning Test code added on 2013 May 02 11:17:01
1079 struct alias_node *a;
1080 for(a = global_alias_list; a; a = a->next) {
1081 printf ("ALIAS [%s] [%s]\n", a->alias, a->value);
1086 setlocale(LC_ALL, "");
1088 nb_search_conditions = 0;
1090 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
1091 long_options, NULL)) != -1) {
1100 print_version(stdout);
1108 global_use_leading_time = 1;
1117 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1120 db_filename = strdup(optarg);
1124 if(db_filename_regexp_string) {
1125 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1128 db_filename_regexp_string = strdup(optarg);
1132 if(mbox_filename_regexp_string) {
1133 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1136 mbox_filename_regexp_string = strdup(optarg);
1140 strncpy(output_filename, optarg, PATH_MAX);
1145 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1148 db_root_path = strdup(optarg);
1152 if(db_filename_list) {
1153 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1156 db_filename_list = strdup(optarg);
1160 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1161 fprintf(stderr, "mymail: Too many search patterns.\n");
1164 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1165 nb_search_conditions++;
1169 default_search_field = optarg;
1173 global_nb_mails_max = atoi(optarg);
1183 print_usage(stderr);
1188 print_usage(stdout);
1192 /* Set all the values that may defined in the arguments, through
1193 environment variables, or hard-coded */
1195 db_filename = default_value(db_filename,
1199 db_filename_regexp_string = default_value(db_filename_regexp_string,
1203 db_root_path = default_value(db_root_path,
1207 db_filename_list = default_value(db_filename_list,
1211 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1212 "MYMAIL_MBOX_PATTERN",
1219 regex_t mbox_filename_regexp_static;
1220 regex_t *mbox_filename_regexp;
1222 if(mbox_filename_regexp_string) {
1223 if(regcomp(&mbox_filename_regexp_static,
1224 mbox_filename_regexp_string,
1227 "mymail: Syntax error in regexp \"%s\".\n",
1228 mbox_filename_regexp_string);
1231 mbox_filename_regexp = &mbox_filename_regexp_static;
1233 mbox_filename_regexp = 0;
1236 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1238 for(f = 0; f < nb_fields_to_parse; f++) {
1239 if(regcomp(&fields_to_parse[f].regexp,
1240 fields_to_parse[f].regexp_string,
1241 fields_to_parse[f].cflags)) {
1243 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1244 fields_to_parse[f].regexp_string,
1245 field_keys[fields_to_parse[f].id]);
1251 "%s version_%s format_%d raw\n",
1252 MYMAIL_DB_MAGIC_TOKEN,
1254 MYMAIL_DB_FORMAT_VERSION);
1256 while(optind < argc) {
1257 recursive_index_mbox(db_file,
1258 argv[optind], mbox_filename_regexp,
1259 nb_fields_to_parse, fields_to_parse);
1266 if(mbox_filename_regexp) {
1267 regfree(mbox_filename_regexp);
1270 for(f = 0; f < nb_fields_to_parse; f++) {
1271 regfree(&fields_to_parse[f].regexp);
1280 int nb_extracted_mails = 0;
1282 if(output_filename[0]) {
1283 output_file = safe_fopen(output_filename, "w", "result mbox");
1285 output_file = stdout;
1289 if(nb_search_conditions > 0) {
1291 /* Recursive search if db_root_path is set */
1294 regex_t db_filename_regexp;
1295 if(regcomp(&db_filename_regexp,
1296 db_filename_regexp_string,
1299 "mymail: Syntax error in regexp \"%s\".\n",
1300 db_filename_regexp_string);
1304 nb_extracted_mails = recursive_search_in_db(db_root_path, &db_filename_regexp,
1306 nb_search_conditions, search_conditions,
1309 regfree(&db_filename_regexp);
1312 /* Search in all db files listed in db_filename_list */
1314 if(db_filename_list) {
1315 char db_filename[PATH_MAX + 1];
1318 s = db_filename_list;
1321 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1323 if(db_filename[0]) {
1324 nb_extracted_mails =
1325 search_in_db(db_filename,
1327 nb_search_conditions, search_conditions, output_file);
1332 /* Search in all db files listed in the command arguments */
1334 while(optind < argc) {
1335 nb_extracted_mails =
1336 search_in_db(argv[optind],
1338 nb_search_conditions, search_conditions, output_file);
1344 if(nb_extracted_mails > 0) {
1345 printf("Found %d matching mails.\n", nb_extracted_mails);
1347 printf("No matching mail found.\n");
1351 fflush(output_file);
1353 if(output_file != stdout) {
1354 fclose(output_file);
1358 for(n = 0; n < nb_search_conditions; n++) {
1359 free_condition(&search_conditions[n]);
1362 a = global_alias_list;
1372 free(db_filename_regexp_string);
1374 free(db_filename_list);
1375 free(mbox_filename_regexp_string);
1377 regfree(&global_leading_from_line_regexp);