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 int global_discard_mail_from_the_future;
75 regex_t global_leading_from_line_regexp;
76 struct alias_node *global_alias_list;
77 time_t global_current_time;
79 /********************************************************************/
97 static char *field_keys[] = {
112 /********************************************************************/
114 struct search_condition {
116 regex_t db_value_regexp;
118 time_t time_start, time_stop;
121 /********************************************************************/
123 struct parsable_field {
130 static struct parsable_field fields_to_parse[] = {
135 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
141 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
142 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
148 "^\\(to\\|cc\\|bcc\\): ",
149 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
156 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
163 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
170 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
176 "^\\(in-reply-to\\|references\\): ",
177 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
182 /********************************************************************/
184 int xor(int a, int b) {
185 return (a && !b) || (!a && b);
188 const char *parse_token(char *token_buffer, size_t token_buffer_size,
189 char separator, const char *string) {
190 char *u = token_buffer;
192 while(*string == separator) { string++; }
194 while(u < token_buffer + token_buffer_size - 1 && *string && *string != separator) {
195 *(u++) = *(string++);
198 while(*string == separator) { string++; }
204 char *default_value(char *current_value,
205 const char *env_variable,
206 const char *hard_default_value) {
208 return current_value;
210 char *env_value = getenv(env_variable);
212 return strdup(env_value);
213 } else if(hard_default_value) {
214 return strdup(hard_default_value);
221 /********************************************************************/
223 void *safe_malloc(size_t n) {
227 "mymail: cannot allocate memory: %s\n", strerror(errno));
233 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
234 FILE *result = fopen(path, mode);
239 "mymail: Cannot open file '%s' (%s) with mode \"%s\": %s\n",
246 /*********************************************************************/
248 void print_version(FILE *out) {
249 fprintf(out, "mymail version %s (%s)\n", MYMAIL_VERSION, UNAME);
252 void print_usage(FILE *out) {
254 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
256 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
258 fprintf(out, " -h, --help\n");
259 fprintf(out, " show this help\n");
260 fprintf(out, " -v, --version\n");
261 fprintf(out, " print the version number\n");
262 fprintf(out, " -q, --quiet\n");
263 fprintf(out, " do not print information during search\n");
264 fprintf(out, " -t, --use-leading-time\n");
265 fprintf(out, " use the time stamp from the leading line of each mail and not the Date:\n");
266 fprintf(out, " field\n");
267 fprintf(out, " -f, --do-not-discard-mails-from-the-future\n");
268 fprintf(out, " do not ignore mails with a date more than 24h in the future\n");
269 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
270 fprintf(out, " set the db filename pattern for recursive search\n");
271 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
272 fprintf(out, " set the db root path for recursive search\n");
273 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
274 fprintf(out, " set the semicolon-separated list of db files for search\n");
275 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
276 fprintf(out, " set the mbox filename pattern for recursive search\n");
277 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
278 fprintf(out, " search for matching mails in the db file\n");
279 fprintf(out, " -d <db filename>, --db-file-output <db filename>\n");
280 fprintf(out, " set the db filename for indexing\n");
281 fprintf(out, " -i, --index\n");
282 fprintf(out, " index mails\n");
283 fprintf(out, " -o <output filename>, --output <output filename>\n");
284 fprintf(out, " set the result file, use stdout if unset\n");
285 fprintf(out, " -n <max number of mails>, --nb-mails-max <max number of mails>\n");
286 fprintf(out, " set the maximum number of mails to extract\n");
287 fprintf(out, " -a <search field>, --default-search <search field>\n");
288 fprintf(out, " set the default search field\n");
292 /*********************************************************************/
294 int ignore_entry(const char *name) {
296 strcmp(name, ".") == 0 ||
297 strcmp(name, "..") == 0 ||
298 (name[0] == '.' && name[1] != '/');
301 int is_a_leading_from_line(char *mbox_line) {
303 strncmp(mbox_line, "From ", 5) == 0 &&
304 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
307 int db_line_match_search(struct search_condition *condition,
308 int db_key, const char *db_value) {
312 (condition->db_key == db_key)
316 (condition->db_key == ID_PARTICIPANT && (db_key == ID_LEADING_LINE ||
321 (condition->db_key == ID_FROM && db_key == ID_LEADING_LINE)
325 (condition->db_key == ID_THREAD_ID && (db_key == ID_MAIL_ID ||
326 db_key == ID_REFERENCE_ID))
331 regexec(&condition->db_value_regexp, db_value, 0, 0, 0) == 0;
334 void update_body_hits(char *mail_filename, int position_in_mail,
335 int nb_search_conditions, struct search_condition *search_conditions,
336 int nb_body_conditions,
340 char raw_mbox_line[BUFFER_SIZE];
346 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
348 fseek(mail_file, position_in_mail, SEEK_SET);
350 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
351 while(nb_body_hits < nb_body_conditions) {
352 if(raw_mbox_line[0] == '\n') { header = 0; }
355 for(n = 0; n < nb_search_conditions; n++) {
356 if(search_conditions[n].db_key == ID_BODY && !hits[n]) {
358 (regexec(&search_conditions[n].db_value_regexp, raw_mbox_line, 0, 0, 0) == 0);
366 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
367 (is_a_leading_from_line(raw_mbox_line)))
375 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
377 char raw_mbox_line[BUFFER_SIZE];
380 /* printf("Extract\n"); */
382 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
383 fseek(mail_file, position_in_mail, SEEK_SET);
385 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
386 fprintf(output_file, "%s", raw_mbox_line);
388 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
389 is_a_leading_from_line(raw_mbox_line))
391 fprintf(output_file, "%s", raw_mbox_line);
398 int check_full_mail_match(char *current_mail_filename,
400 int nb_search_conditions,
401 struct search_condition *search_conditions,
402 int nb_body_conditions,
404 int current_position_in_mail) {
405 int n, nb_fulfilled_body_conditions;
407 for(n = 0; n < nb_search_conditions; n++) {
408 if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
409 hits[n] = (mail_time >= search_conditions[n].time_start &&
410 (search_conditions[n].time_stop == 0 ||
411 mail_time <= search_conditions[n].time_stop));
415 /* We first check all conditions but the body ones */
417 for(n = 0; n < nb_search_conditions &&
418 ((search_conditions[n].db_key == ID_BODY) ||
419 xor(hits[n], search_conditions[n].negation)); n++);
421 if(n == nb_search_conditions) {
423 /* Now check the body ones */
425 nb_fulfilled_body_conditions = 0;
427 if(nb_body_conditions > 0) {
428 update_body_hits(current_mail_filename, current_position_in_mail,
429 nb_search_conditions, search_conditions,
433 for(n = 0; n < nb_search_conditions; n++) {
434 if(search_conditions[n].db_key == ID_BODY &&
435 xor(hits[n], search_conditions[n].negation)) {
436 nb_fulfilled_body_conditions++;
440 return nb_body_conditions == nb_fulfilled_body_conditions;
446 /* We use the mail leading line time by default, and if we should and
447 can, we update with the Date: field */
449 void update_time(int db_key, const char *db_value, time_t *t) {
453 memset(&tm, 0, sizeof(struct tm));
455 if(db_key == ID_LEADING_LINE) {
457 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
458 /* printf("From %s", db_value); */
459 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
462 if(!global_use_leading_time) {
463 if(db_key == ID_DATE) {
464 if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
465 strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
466 /* printf("Date: %s", db_value); */
474 int search_in_db(const char *db_filename,
475 int nb_extracted_mails,
476 int nb_search_conditions,
477 struct search_condition *search_conditions,
481 char raw_db_line[BUFFER_SIZE];
482 char current_mail_filename[PATH_MAX + 1];
483 char db_key_string[TOKEN_BUFFER_SIZE];
484 char position_in_file_string[TOKEN_BUFFER_SIZE];
485 unsigned long int current_position_in_mail;
486 const char *db_value;
488 int hits[MAX_NB_SEARCH_CONDITIONS];
489 int nb_body_conditions, need_time;
495 printf("Searching in '%s' ... ", db_filename);
499 db_file = safe_fopen(db_filename, "r", "index file for search");
501 /* First, check the db file leading line integrity */
503 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
504 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
506 "mymail: Header line in '%s' does not match the mymail db format.\n",
512 "mymail: Cannot read the header line in '%s'.\n",
517 /* Then parse the said db file */
519 current_position_in_mail = 0;
521 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
523 nb_body_conditions = 0;
524 need_time = global_discard_mail_from_the_future;
527 for(n = 0; n < nb_search_conditions; n++) {
528 if(search_conditions[n].db_key == ID_BODY) {
529 nb_body_conditions++;
531 else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
536 strcpy(current_mail_filename, "");
538 while(nb_extracted_mails < global_nb_mails_max &&
539 fgets(raw_db_line, BUFFER_SIZE, db_file)) {
542 char *s = raw_db_line;
543 while(*s && *s != '\n') { s++; }
546 db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
548 if(strcmp("mail", db_key_string) == 0) {
549 if(current_mail_filename[0] &&
550 (!global_discard_mail_from_the_future || mail_time < global_current_time + 3600 * 24) &&
551 check_full_mail_match(current_mail_filename,
553 nb_search_conditions, search_conditions,
554 nb_body_conditions, hits, current_position_in_mail)) {
555 extract_mail(current_mail_filename, current_position_in_mail, output_file);
556 nb_extracted_mails++;
559 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
560 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
561 strncpy(current_mail_filename, db_value, PATH_MAX + 1);
562 current_position_in_mail = atol(position_in_file_string);
567 for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
568 if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
573 for(n = 0; n < nb_search_conditions; n++) {
574 hits[n] |= db_line_match_search(&search_conditions[n],
579 update_time(db_key, db_value, &mail_time);
584 /* printf("global_discard_mail_from_the_future = %d\n",
585 global_discard_mail_from_the_future); */
587 if(nb_extracted_mails < global_nb_mails_max &&
588 current_mail_filename[0] &&
589 (!global_discard_mail_from_the_future || mail_time < global_current_time + 3600 * 24) &&
590 check_full_mail_match(current_mail_filename,
592 nb_search_conditions, search_conditions,
593 nb_body_conditions, hits, current_position_in_mail)) {
594 extract_mail(current_mail_filename, current_position_in_mail, output_file);
595 nb_extracted_mails++;
605 return nb_extracted_mails;
608 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
609 int nb_extracted_mails,
610 int nb_search_conditions,
611 struct search_condition *search_conditions,
614 struct dirent *dir_e;
616 char subname[PATH_MAX + 1];
618 if(lstat(entry_name, &sb) != 0) {
620 "mymail: Cannot stat \"%s\": %s\n",
626 /* printf("recursive_search_in_db %s\n", entry_name); */
628 dir = opendir(entry_name);
631 while((dir_e = readdir(dir)) &&
632 nb_extracted_mails < global_nb_mails_max) {
633 if(!ignore_entry(dir_e->d_name)) {
634 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
635 nb_extracted_mails = recursive_search_in_db(subname, db_filename_regexp,
637 nb_search_conditions, search_conditions,
645 const char *s = entry_name, *filename = entry_name;
646 while(*s) { if(*s == '/') { filename = s+1; } s++; }
648 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
650 search_in_db(entry_name,
652 nb_search_conditions, search_conditions, output_file);
656 return nb_extracted_mails;
659 /*********************************************************************/
661 void index_one_mbox_line(unsigned int nb_fields_to_parse,
662 struct parsable_field *fields_to_parse,
663 char *raw_mbox_line, FILE *db_file) {
666 for(f = 0; f < nb_fields_to_parse; f++) {
667 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
668 fprintf(db_file, "%s %s\n",
669 field_keys[fields_to_parse[f].id],
670 raw_mbox_line + matches.rm_eo);
675 void index_mbox(const char *mbox_filename,
676 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
678 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
679 char *end_of_full_line;
681 int in_header, new_header;
682 unsigned long int position_in_file;
684 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
689 position_in_file = 0;
690 end_of_full_line = 0;
693 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
694 if(is_a_leading_from_line(raw_mbox_line)) {
695 /* This starts a new mail */
698 "Got a ^\"From \" in the header in %s:%lu.\n",
699 mbox_filename, position_in_file);
700 fprintf(stderr, "%s", raw_mbox_line);
703 /* printf("LEADING_LINE %s", raw_mbox_line); */
707 } else if(raw_mbox_line[0] == '\n') {
710 /* We leave the header, index the current line */
712 /* printf("INDEX %s\n", full_line); */
713 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
715 end_of_full_line = full_line;
716 *end_of_full_line = '\0';
722 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
726 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
727 /* Continuation of a line */
728 char *start = raw_mbox_line;
729 while(*start == ' ' || *start == '\t') start++;
730 *(end_of_full_line++) = ' ';
731 strcpy(end_of_full_line, start);
732 while(*end_of_full_line && *end_of_full_line != '\n') {
735 *end_of_full_line = '\0';
739 /* Start a new header line, not a continuation */
742 /* printf("INDEX %s\n", full_line); */
743 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
746 end_of_full_line = full_line;
747 strcpy(end_of_full_line, raw_mbox_line);
748 while(*end_of_full_line && *end_of_full_line != '\n') {
751 *end_of_full_line = '\0';
756 position_in_file += strlen(raw_mbox_line);
762 void recursive_index_mbox(FILE *db_file,
763 const char *entry_name, regex_t *mbox_filename_regexp,
764 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
766 struct dirent *dir_e;
768 char subname[PATH_MAX + 1];
770 if(lstat(entry_name, &sb) != 0) {
772 "mymail: Cannot stat \"%s\": %s\n",
778 dir = opendir(entry_name);
781 while((dir_e = readdir(dir))) {
782 if(!ignore_entry(dir_e->d_name)) {
783 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
784 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
785 nb_fields_to_parse, fields_to_parse);
790 const char *s = entry_name, *filename = s;
791 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
792 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
793 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
798 /*********************************************************************/
800 /* For long options that have no equivalent short option, use a
801 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
803 OPT_BASH_MODE = CHAR_MAX + 1
806 static struct option long_options[] = {
807 { "help", no_argument, 0, 'h' },
808 { "version", no_argument, 0, 'v' },
809 { "quiet", no_argument, 0, 'q' },
810 { "use-leading-time", no_argument, 0, 't' },
811 { "do-not-discard-mails-from-the-future", no_argument, 0, 'f' },
812 { "db-file-output", 1, 0, 'd' },
813 { "db-pattern", 1, 0, 'p' },
814 { "db-root", 1, 0, 'r' },
815 { "db-list", 1, 0, 'l' },
816 { "mbox-pattern", 1, 0, 'm' },
817 { "search", 1, 0, 's' },
818 { "index", 0, 0, 'i' },
819 { "output", 1, 0, 'o' },
820 { "default-search", 1, 0, 'a' },
821 { "nb-mails-max", 1, 0, 'n' },
825 struct time_criterion {
828 int start_hour, end_hour;
832 /*********************************************************************/
834 static struct time_criterion time_criteria[] = {
836 { "1h", 0, 1, -1, -1 },
837 { "8h", 0, 8, -1, -1 },
838 { "24h", 0, 24, -1, -1 },
839 { "48h", 0, 48, -1, -1 },
840 { "week", 0, 24 * 7, -1, -1 },
841 { "2weeks", 0, 24 * 14, -1, -1 },
842 { "month", 0, 24 * 31, -1, -1 },
843 { "trimester", 0, 24 * 92, -1, -1 },
844 { "year", 0, 24 * 365, -1, -1 },
846 { "yesterday", 1, -1, -1, -1 },
847 { "today", 1, -1, -1, 0 },
849 { "monday", 1, -1, -1, 1 },
850 { "tuesday", 1, -1, -1, 2 },
851 { "wednesday", 1, -1, -1, 3 },
852 { "thursday", 1, -1, -1, 4 },
853 { "friday", 1, -1, -1, 5 },
854 { "saturday", 1, -1, -1, 6 },
855 { "sunday", 1, -1, -1, 7 },
859 /*********************************************************************/
861 time_t time_for_past_day(int day) {
864 tm = localtime(&global_current_time);
866 delta_day = (7 + tm->tm_wday - day) % 7;
870 return global_current_time - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
873 void init_condition(struct search_condition *condition, const char *full_string,
874 const char *default_search_field) {
875 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
878 struct alias_node *a;
880 for(a = global_alias_list; a; a = a->next) {
881 if(strcmp(full_string, a->alias) == 0) {
882 full_string = a->value;
887 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
888 search_field = full_search_field;
890 if(search_field[0] == '!') {
892 condition->negation = 1;
894 condition->negation = 0;
897 condition->db_key = -1;
901 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
902 if(strcmp(time_criteria[k].label, search_field) == 0) {
903 condition->db_key = ID_TIME_INTERVAL;
904 if(time_criteria[k].day_criterion) {
905 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
906 condition->time_stop = condition->time_start + 3600 * 24;
908 condition->time_start = global_current_time - 3600 * time_criteria[k].start_hour;
909 if(time_criteria[k].end_hour >= 0) {
910 condition->time_stop = global_current_time - 3600 * time_criteria[k].end_hour;
912 condition->time_stop = 0;
920 if(condition->db_key == -1) {
922 /* No time condition matched, look for the search fields */
924 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
925 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
926 condition->db_key = m;
930 /* None match, if there is a default search field, re-run the search with it */
932 if(condition->db_key == -1) {
933 if(default_search_field) {
934 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
935 if(strncmp(field_keys[m],
936 default_search_field, strlen(default_search_field)) == 0) {
937 condition->db_key = m;
940 string = full_string;
941 if(string[0] == '!') { string++; }
945 if(condition->db_key == -1) {
947 "mymail: Syntax error in field key \"%s\".\n",
952 if(regcomp(&condition->db_value_regexp,
956 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
958 field_keys[condition->db_key]);
964 void free_condition(struct search_condition *condition) {
965 if(condition->db_key != ID_TIME_INTERVAL) {
966 regfree(&condition->db_value_regexp);
970 const char *eat_space(const char *s) {
971 while(*s == ' ' || *s == '\t') { s++; }
975 void read_rc_file(const char *rc_filename) {
976 char raw_line[BUFFER_SIZE];
977 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
984 rc_file = fopen(rc_filename, "r");
988 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
990 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
995 if(*s && *s != '#') {
996 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
998 if(strcmp(command, "alias") == 0) {
999 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
1000 a->next = global_alias_list;
1001 global_alias_list = a;
1004 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
1005 a->alias = strdup(tmp_token);
1008 a->value = strdup(s);
1010 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
1016 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
1022 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
1036 /*********************************************************************/
1037 /*********************************************************************/
1038 /*********************************************************************/
1040 int main(int argc, char **argv) {
1041 char *db_filename = 0;
1042 char *db_filename_regexp_string = 0;
1043 char *db_root_path = 0;
1044 char *db_filename_list = 0;
1045 char *mbox_filename_regexp_string = 0;
1046 char *default_search_field;
1047 char output_filename[PATH_MAX + 1];
1048 char rc_filename[PATH_MAX + 1];
1049 int action_index = 0;
1050 int error = 0, show_help = 0;
1051 const unsigned int nb_fields_to_parse =
1052 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1055 unsigned int nb_search_conditions;
1056 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1057 struct alias_node *a, *b;
1059 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1061 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1065 if(getenv("MYMAILRC")) {
1066 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1067 } else if(getenv("HOME")) {
1068 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1070 rc_filename[0] = '\0';
1073 global_alias_list = 0;
1075 global_use_leading_time = 0;
1076 global_nb_mails_max = 250;
1077 global_discard_mail_from_the_future = 1;
1078 global_current_time = time(0);
1080 default_search_field = 0;
1081 strncpy(output_filename, "", PATH_MAX);
1083 if(rc_filename[0]) {
1084 read_rc_file(rc_filename);
1089 #warning Test code added on 2013 May 02 11:17:01
1090 struct alias_node *a;
1091 for(a = global_alias_list; a; a = a->next) {
1092 printf ("ALIAS [%s] [%s]\n", a->alias, a->value);
1097 setlocale(LC_ALL, "");
1099 nb_search_conditions = 0;
1101 while ((c = getopt_long(argc, argv, "hvqtfip:s:d:r:l:o:a:m:",
1102 long_options, NULL)) != -1) {
1111 print_version(stdout);
1119 global_use_leading_time = 1;
1123 global_discard_mail_from_the_future = 0;
1132 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1135 db_filename = strdup(optarg);
1139 if(db_filename_regexp_string) {
1140 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1143 db_filename_regexp_string = strdup(optarg);
1147 if(mbox_filename_regexp_string) {
1148 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1151 mbox_filename_regexp_string = strdup(optarg);
1155 strncpy(output_filename, optarg, PATH_MAX);
1160 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1163 db_root_path = strdup(optarg);
1167 if(db_filename_list) {
1168 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1171 db_filename_list = strdup(optarg);
1175 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1176 fprintf(stderr, "mymail: Too many search patterns.\n");
1179 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1180 nb_search_conditions++;
1184 default_search_field = optarg;
1188 global_nb_mails_max = atoi(optarg);
1198 print_usage(stderr);
1203 print_usage(stdout);
1207 /* Set all the values that may defined in the arguments, through
1208 environment variables, or hard-coded */
1210 db_filename = default_value(db_filename,
1214 db_filename_regexp_string = default_value(db_filename_regexp_string,
1218 db_root_path = default_value(db_root_path,
1222 db_filename_list = default_value(db_filename_list,
1226 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1227 "MYMAIL_MBOX_PATTERN",
1234 regex_t mbox_filename_regexp_static;
1235 regex_t *mbox_filename_regexp;
1237 if(mbox_filename_regexp_string) {
1238 if(regcomp(&mbox_filename_regexp_static,
1239 mbox_filename_regexp_string,
1242 "mymail: Syntax error in regexp \"%s\".\n",
1243 mbox_filename_regexp_string);
1246 mbox_filename_regexp = &mbox_filename_regexp_static;
1248 mbox_filename_regexp = 0;
1251 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1253 for(f = 0; f < nb_fields_to_parse; f++) {
1254 if(regcomp(&fields_to_parse[f].regexp,
1255 fields_to_parse[f].regexp_string,
1256 fields_to_parse[f].cflags)) {
1258 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1259 fields_to_parse[f].regexp_string,
1260 field_keys[fields_to_parse[f].id]);
1266 "%s version_%s format_%d raw\n",
1267 MYMAIL_DB_MAGIC_TOKEN,
1269 MYMAIL_DB_FORMAT_VERSION);
1271 while(optind < argc) {
1272 recursive_index_mbox(db_file,
1273 argv[optind], mbox_filename_regexp,
1274 nb_fields_to_parse, fields_to_parse);
1281 if(mbox_filename_regexp) {
1282 regfree(mbox_filename_regexp);
1285 for(f = 0; f < nb_fields_to_parse; f++) {
1286 regfree(&fields_to_parse[f].regexp);
1295 int nb_extracted_mails = 0;
1297 if(output_filename[0]) {
1298 output_file = safe_fopen(output_filename, "w", "result mbox");
1300 output_file = stdout;
1304 if(nb_search_conditions > 0) {
1306 /* Recursive search if db_root_path is set */
1309 regex_t db_filename_regexp;
1310 if(regcomp(&db_filename_regexp,
1311 db_filename_regexp_string,
1314 "mymail: Syntax error in regexp \"%s\".\n",
1315 db_filename_regexp_string);
1319 nb_extracted_mails = recursive_search_in_db(db_root_path, &db_filename_regexp,
1321 nb_search_conditions, search_conditions,
1324 regfree(&db_filename_regexp);
1327 /* Search in all db files listed in db_filename_list */
1329 if(db_filename_list) {
1330 char db_filename[PATH_MAX + 1];
1333 s = db_filename_list;
1336 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1338 if(db_filename[0]) {
1339 nb_extracted_mails =
1340 search_in_db(db_filename,
1342 nb_search_conditions, search_conditions, output_file);
1347 /* Search in all db files listed in the command arguments */
1349 while(optind < argc) {
1350 nb_extracted_mails =
1351 search_in_db(argv[optind],
1353 nb_search_conditions, search_conditions, output_file);
1359 if(nb_extracted_mails > 0) {
1360 printf("Found %d matching mails.\n", nb_extracted_mails);
1362 printf("No matching mail found.\n");
1366 fflush(output_file);
1368 if(output_file != stdout) {
1369 fclose(output_file);
1373 for(n = 0; n < nb_search_conditions; n++) {
1374 free_condition(&search_conditions[n]);
1377 a = global_alias_list;
1387 free(db_filename_regexp_string);
1389 free(db_filename_list);
1390 free(mbox_filename_regexp_string);
1392 regfree(&global_leading_from_line_regexp);