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 { "month", 0, 24 * 31, -1, -1 },
831 { "trimester", 0, 24 * 92, -1, -1 },
832 { "year", 0, 24 * 365, -1, -1 },
834 { "yesterday", 1, -1, -1, -1 },
835 { "today", 1, -1, -1, 0 },
837 { "monday", 1, -1, -1, 1 },
838 { "tuesday", 1, -1, -1, 2 },
839 { "wednesday", 1, -1, -1, 3 },
840 { "thursday", 1, -1, -1, 4 },
841 { "friday", 1, -1, -1, 5 },
842 { "saturday", 1, -1, -1, 6 },
843 { "sunday", 1, -1, -1, 7 },
847 /*********************************************************************/
849 time_t time_for_past_day(int day) {
856 delta_day = (7 + tm->tm_wday - day) % 7;
860 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
863 void init_condition(struct search_condition *condition, const char *full_string,
864 const char *default_search_field) {
865 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
868 struct alias_node *a;
870 for(a = global_alias_list; a; a = a->next) {
871 if(strcmp(full_string, a->alias) == 0) {
872 full_string = a->value;
877 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
878 search_field = full_search_field;
880 if(search_field[0] == '!') {
882 condition->negation = 1;
884 condition->negation = 0;
887 condition->db_key = -1;
891 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
892 if(strcmp(time_criteria[k].label, search_field) == 0) {
893 condition->db_key = ID_TIME_INTERVAL;
894 if(time_criteria[k].day_criterion) {
895 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
896 condition->time_stop = condition->time_start + 3600 * 24;
898 condition->time_start = time(0) - 3600 * time_criteria[k].start_hour;
899 if(time_criteria[k].end_hour >= 0) {
900 condition->time_stop = time(0) - 3600 * time_criteria[k].end_hour;
902 condition->time_stop = 0;
910 if(condition->db_key == -1) {
912 /* No time condition matched, look for the search fields */
914 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
915 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
916 condition->db_key = m;
920 /* None match, if there is a default search field, re-run the search with it */
922 if(condition->db_key == -1) {
923 if(default_search_field) {
924 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
925 if(strncmp(field_keys[m],
926 default_search_field, strlen(default_search_field)) == 0) {
927 condition->db_key = m;
930 string = full_string;
931 if(string[0] == '!') { string++; }
935 if(condition->db_key == -1) {
937 "mymail: Syntax error in field key \"%s\".\n",
942 if(regcomp(&condition->db_value_regexp,
946 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
948 field_keys[condition->db_key]);
954 void free_condition(struct search_condition *condition) {
955 if(condition->db_key != ID_TIME_INTERVAL) {
956 regfree(&condition->db_value_regexp);
960 const char *eat_space(const char *s) {
961 while(*s == ' ' || *s == '\t') { s++; }
965 void read_rc_file(const char *rc_filename) {
966 char raw_line[BUFFER_SIZE];
967 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
974 rc_file = fopen(rc_filename, "r");
978 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
980 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
985 if(*s && *s != '#') {
986 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
988 if(strcmp(command, "alias") == 0) {
989 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
990 a->next = global_alias_list;
991 global_alias_list = a;
994 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
995 a->alias = strdup(tmp_token);
998 a->value = strdup(s);
1000 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
1006 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
1012 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
1026 /*********************************************************************/
1027 /*********************************************************************/
1028 /*********************************************************************/
1030 int main(int argc, char **argv) {
1031 char *db_filename = 0;
1032 char *db_filename_regexp_string = 0;
1033 char *db_root_path = 0;
1034 char *db_filename_list = 0;
1035 char *mbox_filename_regexp_string = 0;
1036 char *default_search_field;
1037 char output_filename[PATH_MAX + 1];
1038 char rc_filename[PATH_MAX + 1];
1039 int action_index = 0;
1040 int error = 0, show_help = 0;
1041 const unsigned int nb_fields_to_parse =
1042 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1045 unsigned int nb_search_conditions;
1046 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1047 struct alias_node *a, *b;
1049 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1051 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1055 if(getenv("MYMAILRC")) {
1056 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1057 } else if(getenv("HOME")) {
1058 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1060 rc_filename[0] = '\0';
1063 global_alias_list = 0;
1065 global_use_leading_time = 0;
1066 global_nb_mails_max = 250;
1068 default_search_field = 0;
1069 strncpy(output_filename, "", PATH_MAX);
1071 if(rc_filename[0]) {
1072 read_rc_file(rc_filename);
1077 #warning Test code added on 2013 May 02 11:17:01
1078 struct alias_node *a;
1079 for(a = global_alias_list; a; a = a->next) {
1080 printf ("ALIAS [%s] [%s]\n", a->alias, a->value);
1085 setlocale(LC_ALL, "");
1087 nb_search_conditions = 0;
1089 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
1090 long_options, NULL)) != -1) {
1099 print_version(stdout);
1107 global_use_leading_time = 1;
1116 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1119 db_filename = strdup(optarg);
1123 if(db_filename_regexp_string) {
1124 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1127 db_filename_regexp_string = strdup(optarg);
1131 if(mbox_filename_regexp_string) {
1132 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1135 mbox_filename_regexp_string = strdup(optarg);
1139 strncpy(output_filename, optarg, PATH_MAX);
1144 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1147 db_root_path = strdup(optarg);
1151 if(db_filename_list) {
1152 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1155 db_filename_list = strdup(optarg);
1159 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1160 fprintf(stderr, "mymail: Too many search patterns.\n");
1163 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1164 nb_search_conditions++;
1168 default_search_field = optarg;
1172 global_nb_mails_max = atoi(optarg);
1182 print_usage(stderr);
1187 print_usage(stdout);
1191 /* Set all the values that may defined in the arguments, through
1192 environment variables, or hard-coded */
1194 db_filename = default_value(db_filename,
1198 db_filename_regexp_string = default_value(db_filename_regexp_string,
1202 db_root_path = default_value(db_root_path,
1206 db_filename_list = default_value(db_filename_list,
1210 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1211 "MYMAIL_MBOX_PATTERN",
1218 regex_t mbox_filename_regexp_static;
1219 regex_t *mbox_filename_regexp;
1221 if(mbox_filename_regexp_string) {
1222 if(regcomp(&mbox_filename_regexp_static,
1223 mbox_filename_regexp_string,
1226 "mymail: Syntax error in regexp \"%s\".\n",
1227 mbox_filename_regexp_string);
1230 mbox_filename_regexp = &mbox_filename_regexp_static;
1232 mbox_filename_regexp = 0;
1235 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1237 for(f = 0; f < nb_fields_to_parse; f++) {
1238 if(regcomp(&fields_to_parse[f].regexp,
1239 fields_to_parse[f].regexp_string,
1240 fields_to_parse[f].cflags)) {
1242 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1243 fields_to_parse[f].regexp_string,
1244 field_keys[fields_to_parse[f].id]);
1250 "%s version_%s format_%d raw\n",
1251 MYMAIL_DB_MAGIC_TOKEN,
1253 MYMAIL_DB_FORMAT_VERSION);
1255 while(optind < argc) {
1256 recursive_index_mbox(db_file,
1257 argv[optind], mbox_filename_regexp,
1258 nb_fields_to_parse, fields_to_parse);
1265 if(mbox_filename_regexp) {
1266 regfree(mbox_filename_regexp);
1269 for(f = 0; f < nb_fields_to_parse; f++) {
1270 regfree(&fields_to_parse[f].regexp);
1279 int nb_extracted_mails = 0;
1281 if(output_filename[0]) {
1282 output_file = safe_fopen(output_filename, "w", "result mbox");
1284 output_file = stdout;
1288 if(nb_search_conditions > 0) {
1290 /* Recursive search if db_root_path is set */
1293 regex_t db_filename_regexp;
1294 if(regcomp(&db_filename_regexp,
1295 db_filename_regexp_string,
1298 "mymail: Syntax error in regexp \"%s\".\n",
1299 db_filename_regexp_string);
1303 nb_extracted_mails = recursive_search_in_db(db_root_path, &db_filename_regexp,
1305 nb_search_conditions, search_conditions,
1308 regfree(&db_filename_regexp);
1311 /* Search in all db files listed in db_filename_list */
1313 if(db_filename_list) {
1314 char db_filename[PATH_MAX + 1];
1317 s = db_filename_list;
1320 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1322 if(db_filename[0]) {
1323 nb_extracted_mails =
1324 search_in_db(db_filename,
1326 nb_search_conditions, search_conditions, output_file);
1331 /* Search in all db files listed in the command arguments */
1333 while(optind < argc) {
1334 nb_extracted_mails =
1335 search_in_db(argv[optind],
1337 nb_search_conditions, search_conditions, output_file);
1343 if(nb_extracted_mails > 0) {
1344 printf("Found %d matching mails.\n", nb_extracted_mails);
1346 printf("No matching mail found.\n");
1350 fflush(output_file);
1352 if(output_file != stdout) {
1353 fclose(output_file);
1357 for(n = 0; n < nb_search_conditions; n++) {
1358 free_condition(&search_conditions[n]);
1361 a = global_alias_list;
1371 free(db_filename_regexp_string);
1373 free(db_filename_list);
1374 free(mbox_filename_regexp_string);
1376 regfree(&global_leading_from_line_regexp);