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.
51 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
52 #define MYMAIL_VERSION "0.9.10"
54 #define MYMAIL_DB_FORMAT_VERSION 1
56 #define MAX_NB_SEARCH_CONDITIONS 32
58 #define BUFFER_SIZE 65536
59 #define TOKEN_BUFFER_SIZE 1024
61 #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$"
63 /********************************************************************/
67 struct alias_node *next;
70 /* Global variables! */
73 int global_use_leading_time;
74 int global_nb_mails_max;
75 int global_discard_mail_from_the_future;
76 regex_t global_leading_from_line_regexp;
77 struct alias_node *global_alias_list;
78 time_t global_current_time;
80 /********************************************************************/
98 static char *field_keys[] = {
113 /********************************************************************/
115 struct search_condition {
117 regex_t db_value_regexp;
119 time_t time_start, time_stop;
122 /********************************************************************/
124 struct parsable_field {
131 static struct parsable_field fields_to_parse[] = {
136 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
142 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
143 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
149 "^\\(to\\|cc\\|bcc\\): ",
150 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
157 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
164 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
171 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
177 "^\\(in-reply-to\\|references\\): ",
178 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
183 /********************************************************************/
185 int xor(int a, int b) {
186 return (a && !b) || (!a && b);
189 const char *parse_token(char *token_buffer, size_t token_buffer_size,
190 char separator, const char *string) {
191 char *u = token_buffer;
193 while(*string == separator) { string++; }
195 while(u < token_buffer + token_buffer_size - 1 && *string && *string != separator) {
196 *(u++) = *(string++);
199 while(*string == separator) { string++; }
205 char *default_value(char *current_value,
206 const char *env_variable,
207 const char *hard_default_value) {
209 return current_value;
211 char *env_value = getenv(env_variable);
213 return strdup(env_value);
214 } else if(hard_default_value) {
215 return strdup(hard_default_value);
222 /********************************************************************/
224 void *safe_malloc(size_t n) {
228 "mymail: cannot allocate memory: %s\n", strerror(errno));
234 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
235 FILE *result = fopen(path, mode);
240 "mymail: Cannot open file '%s' (%s) with mode \"%s\": %s\n",
247 /*********************************************************************/
249 void print_version(FILE *out) {
250 fprintf(out, "mymail version %s (%s)\n", MYMAIL_VERSION, UNAME);
253 void print_usage(FILE *out) {
255 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
257 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
259 fprintf(out, " -h, --help\n");
260 fprintf(out, " show this help\n");
261 fprintf(out, " -v, --version\n");
262 fprintf(out, " print the version number\n");
263 fprintf(out, " -q, --quiet\n");
264 fprintf(out, " do not print information during search\n");
265 fprintf(out, " -t, --use-leading-time\n");
266 fprintf(out, " use the time stamp from the leading line of each mail and not the Date:\n");
267 fprintf(out, " field\n");
268 fprintf(out, " -f, --do-not-discard-mails-from-the-future\n");
269 fprintf(out, " do not ignore mails with a date more than 24h in the future\n");
270 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
271 fprintf(out, " set the db filename pattern for recursive search\n");
272 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
273 fprintf(out, " set the db root path for recursive search\n");
274 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
275 fprintf(out, " set the semicolon-separated list of db files for search\n");
276 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
277 fprintf(out, " set the mbox filename pattern for recursive search\n");
278 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
279 fprintf(out, " search for matching mails in the db file\n");
280 fprintf(out, " -d <db filename>, --db-file-output <db filename>\n");
281 fprintf(out, " set the db filename for indexing\n");
282 fprintf(out, " -i, --index\n");
283 fprintf(out, " index mails\n");
284 fprintf(out, " -o <output filename>, --output <output filename>\n");
285 fprintf(out, " set the result file, use stdout if unset\n");
286 fprintf(out, " -n <max number of mails>, --nb-mails-max <max number of mails>\n");
287 fprintf(out, " set the maximum number of mails to extract\n");
288 fprintf(out, " -a <search field>, --default-search <search field>\n");
289 fprintf(out, " set the default search field\n");
293 /*********************************************************************/
295 int ignore_entry(const char *name) {
297 strcmp(name, ".") == 0 ||
298 strcmp(name, "..") == 0 ||
299 (name[0] == '.' && name[1] != '/');
302 int is_a_leading_from_line(char *mbox_line) {
304 strncmp(mbox_line, "From ", 5) == 0 &&
305 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
308 int db_line_match_search(struct search_condition *condition,
309 int db_key, const char *db_value) {
313 (condition->db_key == db_key)
317 (condition->db_key == ID_PARTICIPANT && (db_key == ID_LEADING_LINE ||
322 (condition->db_key == ID_FROM && db_key == ID_LEADING_LINE)
326 (condition->db_key == ID_THREAD_ID && (db_key == ID_MAIL_ID ||
327 db_key == ID_REFERENCE_ID))
332 regexec(&condition->db_value_regexp, db_value, 0, 0, 0) == 0;
335 void update_body_hits(char *mail_filename, int position_in_mail,
336 int nb_search_conditions, struct search_condition *search_conditions,
337 int nb_body_conditions,
341 char raw_mbox_line[BUFFER_SIZE];
347 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
349 fseek(mail_file, position_in_mail, SEEK_SET);
351 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
352 while(nb_body_hits < nb_body_conditions) {
353 if(raw_mbox_line[0] == '\n') { header = 0; }
356 for(n = 0; n < nb_search_conditions; n++) {
357 if(search_conditions[n].db_key == ID_BODY && !hits[n]) {
359 (regexec(&search_conditions[n].db_value_regexp, raw_mbox_line, 0, 0, 0) == 0);
367 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
368 (is_a_leading_from_line(raw_mbox_line)))
376 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
378 char raw_mbox_line[BUFFER_SIZE];
381 /* printf("Extract\n"); */
383 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
384 /* fchmod(fileno(mail_file), 0x660); */
385 fseek(mail_file, position_in_mail, SEEK_SET);
387 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
388 fprintf(output_file, "%s", raw_mbox_line);
390 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
391 is_a_leading_from_line(raw_mbox_line))
393 fprintf(output_file, "%s", raw_mbox_line);
400 int check_full_mail_match(char *current_mail_filename,
402 int nb_search_conditions,
403 struct search_condition *search_conditions,
404 int nb_body_conditions,
406 int current_position_in_mail) {
407 int n, nb_fulfilled_body_conditions;
409 for(n = 0; n < nb_search_conditions; n++) {
410 if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
411 hits[n] = (mail_time >= search_conditions[n].time_start &&
412 (search_conditions[n].time_stop == 0 ||
413 mail_time <= search_conditions[n].time_stop));
417 /* We first check all conditions but the body ones */
419 for(n = 0; n < nb_search_conditions &&
420 ((search_conditions[n].db_key == ID_BODY) ||
421 xor(hits[n], search_conditions[n].negation)); n++);
423 if(n == nb_search_conditions) {
425 /* Now check the body ones */
427 nb_fulfilled_body_conditions = 0;
429 if(nb_body_conditions > 0) {
430 update_body_hits(current_mail_filename, current_position_in_mail,
431 nb_search_conditions, search_conditions,
435 for(n = 0; n < nb_search_conditions; n++) {
436 if(search_conditions[n].db_key == ID_BODY &&
437 xor(hits[n], search_conditions[n].negation)) {
438 nb_fulfilled_body_conditions++;
442 return nb_body_conditions == nb_fulfilled_body_conditions;
448 /* We use the mail leading line time by default, and if we should and
449 can, we update with the Date: field */
451 void update_time(int db_key, const char *db_value, time_t *t) {
455 memset(&tm, 0, sizeof(struct tm));
457 if(db_key == ID_LEADING_LINE) {
459 while(*c && *c != ' ') c++;
460 while(*c && *c == ' ') c++;
461 /* printf("From %s", db_value); */
462 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
465 if(!global_use_leading_time) {
466 if(db_key == ID_DATE) {
467 if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
468 strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
469 /* printf("Date: %s", db_value); */
477 int search_in_db(const char *db_filename,
478 int nb_extracted_mails,
479 int nb_search_conditions,
480 struct search_condition *search_conditions,
484 char raw_db_line[BUFFER_SIZE];
485 char current_mail_filename[PATH_MAX + 1];
486 char db_key_string[TOKEN_BUFFER_SIZE];
487 char position_in_file_string[TOKEN_BUFFER_SIZE];
488 unsigned long int current_position_in_mail;
489 const char *db_value;
491 int hits[MAX_NB_SEARCH_CONDITIONS];
492 int nb_body_conditions, need_time;
498 printf("Searching in '%s' ... ", db_filename);
502 db_file = safe_fopen(db_filename, "r", "index file for search");
504 /* First, check the db file leading line integrity */
506 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
507 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
509 "mymail: Header line in '%s' does not match the mymail db format.\n",
515 "mymail: Cannot read the header line in '%s'.\n",
520 /* Then parse the said db file */
522 current_position_in_mail = 0;
524 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
526 nb_body_conditions = 0;
527 need_time = global_discard_mail_from_the_future;
530 for(n = 0; n < nb_search_conditions; n++) {
531 if(search_conditions[n].db_key == ID_BODY) {
532 nb_body_conditions++;
534 else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
539 strcpy(current_mail_filename, "");
541 while(nb_extracted_mails < global_nb_mails_max &&
542 fgets(raw_db_line, BUFFER_SIZE, db_file)) {
545 char *s = raw_db_line;
546 while(*s && *s != '\n') { s++; }
549 db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
551 if(strcmp("mail", db_key_string) == 0) {
552 if(current_mail_filename[0] &&
553 (!global_discard_mail_from_the_future || mail_time < global_current_time + 3600 * 24) &&
554 check_full_mail_match(current_mail_filename,
556 nb_search_conditions, search_conditions,
557 nb_body_conditions, hits, current_position_in_mail)) {
558 extract_mail(current_mail_filename, current_position_in_mail, output_file);
559 nb_extracted_mails++;
562 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
563 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
564 strncpy(current_mail_filename, db_value, PATH_MAX + 1);
565 current_position_in_mail = atol(position_in_file_string);
570 for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
571 if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
576 for(n = 0; n < nb_search_conditions; n++) {
577 hits[n] |= db_line_match_search(&search_conditions[n],
582 update_time(db_key, db_value, &mail_time);
587 /* printf("global_discard_mail_from_the_future = %d\n",
588 global_discard_mail_from_the_future); */
590 if(nb_extracted_mails < global_nb_mails_max &&
591 current_mail_filename[0] &&
592 (!global_discard_mail_from_the_future || mail_time < global_current_time + 3600 * 24) &&
593 check_full_mail_match(current_mail_filename,
595 nb_search_conditions, search_conditions,
596 nb_body_conditions, hits, current_position_in_mail)) {
597 extract_mail(current_mail_filename, current_position_in_mail, output_file);
598 nb_extracted_mails++;
608 return nb_extracted_mails;
611 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
612 int nb_extracted_mails,
613 int nb_search_conditions,
614 struct search_condition *search_conditions,
617 struct dirent *dir_e;
619 char subname[PATH_MAX + 1];
621 if(lstat(entry_name, &sb) != 0) {
623 "mymail: Cannot stat \"%s\": %s\n",
629 /* printf("recursive_search_in_db %s\n", entry_name); */
631 dir = opendir(entry_name);
634 while((dir_e = readdir(dir)) &&
635 nb_extracted_mails < global_nb_mails_max) {
636 if(!ignore_entry(dir_e->d_name)) {
637 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
638 nb_extracted_mails = recursive_search_in_db(subname, db_filename_regexp,
640 nb_search_conditions, search_conditions,
648 const char *s = entry_name, *filename = entry_name;
649 while(*s) { if(*s == '/') { filename = s+1; } s++; }
651 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
653 search_in_db(entry_name,
655 nb_search_conditions, search_conditions, output_file);
659 return nb_extracted_mails;
662 /*********************************************************************/
664 void index_one_mbox_line(unsigned int nb_fields_to_parse,
665 struct parsable_field *fields_to_parse,
666 char *raw_mbox_line, FILE *db_file) {
669 for(f = 0; f < nb_fields_to_parse; f++) {
670 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
671 fprintf(db_file, "%s %s\n",
672 field_keys[fields_to_parse[f].id],
673 raw_mbox_line + matches.rm_eo);
678 void index_mbox(const char *mbox_filename,
679 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
681 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
682 char *end_of_full_line;
684 int in_header, new_header;
685 unsigned long int position_in_file;
687 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
692 position_in_file = 0;
693 end_of_full_line = 0;
696 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
697 if(is_a_leading_from_line(raw_mbox_line)) {
698 /* This starts a new mail */
701 "Got a ^\"From \" in the header in %s:%lu.\n",
702 mbox_filename, position_in_file);
703 fprintf(stderr, "%s", raw_mbox_line);
706 /* printf("LEADING_LINE %s", raw_mbox_line); */
710 } else if(raw_mbox_line[0] == '\n') {
713 /* We leave the header, index the current line */
715 /* printf("INDEX %s\n", full_line); */
716 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
718 end_of_full_line = full_line;
719 *end_of_full_line = '\0';
725 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
729 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
730 /* Continuation of a line */
731 char *start = raw_mbox_line;
732 while(*start == ' ' || *start == '\t') start++;
733 *(end_of_full_line++) = ' ';
734 strcpy(end_of_full_line, start);
735 while(*end_of_full_line && *end_of_full_line != '\n') {
738 *end_of_full_line = '\0';
742 /* Start a new header line, not a continuation */
745 /* printf("INDEX %s\n", full_line); */
746 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
749 end_of_full_line = full_line;
750 strcpy(end_of_full_line, raw_mbox_line);
751 while(*end_of_full_line && *end_of_full_line != '\n') {
754 *end_of_full_line = '\0';
759 position_in_file += strlen(raw_mbox_line);
765 void recursive_index_mbox(FILE *db_file,
766 const char *entry_name, regex_t *mbox_filename_regexp,
767 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
769 struct dirent *dir_e;
771 char subname[PATH_MAX + 1];
773 if(lstat(entry_name, &sb) != 0) {
775 "mymail: Cannot stat \"%s\": %s\n",
781 dir = opendir(entry_name);
784 while((dir_e = readdir(dir))) {
785 if(!ignore_entry(dir_e->d_name)) {
786 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
787 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
788 nb_fields_to_parse, fields_to_parse);
793 const char *s = entry_name, *filename = s;
794 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
795 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
796 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
801 /*********************************************************************/
803 /* For long options that have no equivalent short option, use a
804 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
806 OPT_BASH_MODE = CHAR_MAX + 1
809 static struct option long_options[] = {
810 { "help", no_argument, 0, 'h' },
811 { "version", no_argument, 0, 'v' },
812 { "quiet", no_argument, 0, 'q' },
813 { "use-leading-time", no_argument, 0, 't' },
814 { "do-not-discard-mails-from-the-future", no_argument, 0, 'f' },
815 { "db-file-output", 1, 0, 'd' },
816 { "db-pattern", 1, 0, 'p' },
817 { "db-root", 1, 0, 'r' },
818 { "db-list", 1, 0, 'l' },
819 { "mbox-pattern", 1, 0, 'm' },
820 { "search", 1, 0, 's' },
821 { "index", 0, 0, 'i' },
822 { "output", 1, 0, 'o' },
823 { "default-search", 1, 0, 'a' },
824 { "nb-mails-max", 1, 0, 'n' },
828 struct time_criterion {
831 int start_hour, end_hour;
835 /*********************************************************************/
837 static struct time_criterion time_criteria[] = {
839 { "1h", 0, 1, -1, -1 },
840 { "2h", 0, 2, -1, -1 },
841 { "4h", 0, 4, -1, -1 },
842 { "8h", 0, 8, -1, -1 },
843 { "24h", 0, 24, -1, -1 },
844 { "48h", 0, 48, -1, -1 },
845 { "week", 0, 24 * 7, -1, -1 },
846 { "2weeks", 0, 24 * 14, -1, -1 },
847 { "month", 0, 24 * 31, -1, -1 },
848 { "semester", 0, 24 * 185, -1, -1 },
849 { "trimester", 0, 24 * 92, -1, -1 },
850 { "year", 0, 24 * 365, -1, -1 },
852 { "yesterday", 1, -1, -1, -1 },
853 { "today", 1, -1, -1, 0 },
855 { "monday", 1, -1, -1, 1 },
856 { "tuesday", 1, -1, -1, 2 },
857 { "wednesday", 1, -1, -1, 3 },
858 { "thursday", 1, -1, -1, 4 },
859 { "friday", 1, -1, -1, 5 },
860 { "saturday", 1, -1, -1, 6 },
861 { "sunday", 1, -1, -1, 7 },
865 /*********************************************************************/
867 time_t time_for_past_day(int day) {
870 tm = localtime(&global_current_time);
872 delta_day = (7 + tm->tm_wday - day) % 7;
876 return global_current_time - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
879 void init_condition(struct search_condition *condition, const char *full_string,
880 const char *default_search_field) {
881 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
884 struct alias_node *a;
886 for(a = global_alias_list; a; a = a->next) {
887 if(strcmp(full_string, a->alias) == 0) {
888 full_string = a->value;
893 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
894 search_field = full_search_field;
896 if(search_field[0] == '!') {
898 condition->negation = 1;
900 condition->negation = 0;
903 condition->db_key = -1;
907 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
908 if(strcmp(time_criteria[k].label, search_field) == 0) {
909 condition->db_key = ID_TIME_INTERVAL;
910 if(time_criteria[k].day_criterion) {
911 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
912 condition->time_stop = condition->time_start + 3600 * 24;
914 condition->time_start = global_current_time - 3600 * time_criteria[k].start_hour;
915 if(time_criteria[k].end_hour >= 0) {
916 condition->time_stop = global_current_time - 3600 * time_criteria[k].end_hour;
918 condition->time_stop = 0;
926 if(condition->db_key == -1) {
928 /* No time condition matched, look for the search fields */
930 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
931 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
932 condition->db_key = m;
936 /* None match, if there is a default search field, re-run the search with it */
938 if(condition->db_key == -1) {
939 if(default_search_field) {
940 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
941 if(strncmp(field_keys[m],
942 default_search_field, strlen(default_search_field)) == 0) {
943 condition->db_key = m;
946 string = full_string;
947 if(string[0] == '!') { string++; }
951 if(condition->db_key == -1) {
953 "mymail: Syntax error in field key \"%s\".\n",
958 if(regcomp(&condition->db_value_regexp,
962 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
964 field_keys[condition->db_key]);
970 void free_condition(struct search_condition *condition) {
971 if(condition->db_key != ID_TIME_INTERVAL) {
972 regfree(&condition->db_value_regexp);
976 const char *eat_space(const char *s) {
977 while(*s == ' ' || *s == '\t') { s++; }
981 void read_rc_file(const char *rc_filename) {
982 char raw_line[BUFFER_SIZE];
983 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
990 rc_file = fopen(rc_filename, "r");
994 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
996 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
1001 if(*s && *s != '#') {
1002 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
1004 if(strcmp(command, "alias") == 0) {
1005 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
1006 a->next = global_alias_list;
1007 global_alias_list = a;
1010 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
1011 a->alias = strdup(tmp_token);
1014 a->value = strdup(s);
1016 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
1022 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
1028 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
1042 /*********************************************************************/
1043 /*********************************************************************/
1044 /*********************************************************************/
1046 int main(int argc, char **argv) {
1047 char *db_filename = 0;
1048 char *db_filename_regexp_string = 0;
1049 char *db_root_path = 0;
1050 char *db_filename_list = 0;
1051 char *mbox_filename_regexp_string = 0;
1052 char *default_search_field;
1053 char output_filename[PATH_MAX + 1];
1054 char rc_filename[PATH_MAX + 1];
1055 int action_index = 0;
1056 int error = 0, show_help = 0;
1057 const unsigned int nb_fields_to_parse =
1058 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1061 unsigned int nb_search_conditions;
1062 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1063 struct alias_node *a, *b;
1065 /* Group and others have no access */
1066 umask(S_IRWXG | S_IRWXO);
1068 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1070 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1074 if(getenv("MYMAILRC")) {
1075 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1076 } else if(getenv("HOME")) {
1077 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1079 rc_filename[0] = '\0';
1082 global_alias_list = 0;
1084 global_use_leading_time = 0;
1085 global_nb_mails_max = 250;
1086 global_discard_mail_from_the_future = 1;
1087 global_current_time = time(0);
1089 default_search_field = 0;
1090 strncpy(output_filename, "", PATH_MAX);
1092 if(rc_filename[0]) {
1093 read_rc_file(rc_filename);
1098 #warning Test code added on 2013 May 02 11:17:01
1099 struct alias_node *a;
1100 for(a = global_alias_list; a; a = a->next) {
1101 printf ("ALIAS [%s] [%s]\n", a->alias, a->value);
1106 setlocale(LC_ALL, "");
1108 nb_search_conditions = 0;
1110 while ((c = getopt_long(argc, argv, "hvqtfip:s:d:r:l:o:a:m:",
1111 long_options, NULL)) != -1) {
1120 print_version(stdout);
1128 global_use_leading_time = 1;
1132 global_discard_mail_from_the_future = 0;
1141 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1144 db_filename = strdup(optarg);
1148 if(db_filename_regexp_string) {
1149 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1152 db_filename_regexp_string = strdup(optarg);
1156 if(mbox_filename_regexp_string) {
1157 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1160 mbox_filename_regexp_string = strdup(optarg);
1164 strncpy(output_filename, optarg, PATH_MAX);
1169 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1172 db_root_path = strdup(optarg);
1176 if(db_filename_list) {
1177 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1180 db_filename_list = strdup(optarg);
1184 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1185 fprintf(stderr, "mymail: Too many search patterns.\n");
1188 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1189 nb_search_conditions++;
1193 default_search_field = optarg;
1197 global_nb_mails_max = atoi(optarg);
1207 print_usage(stderr);
1212 print_usage(stdout);
1216 /* Set all the values that may defined in the arguments, through
1217 environment variables, or hard-coded */
1219 db_filename = default_value(db_filename,
1223 db_filename_regexp_string = default_value(db_filename_regexp_string,
1227 db_root_path = default_value(db_root_path,
1231 db_filename_list = default_value(db_filename_list,
1235 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1236 "MYMAIL_MBOX_PATTERN",
1243 regex_t mbox_filename_regexp_static;
1244 regex_t *mbox_filename_regexp;
1246 if(mbox_filename_regexp_string) {
1247 if(regcomp(&mbox_filename_regexp_static,
1248 mbox_filename_regexp_string,
1251 "mymail: Syntax error in regexp \"%s\".\n",
1252 mbox_filename_regexp_string);
1255 mbox_filename_regexp = &mbox_filename_regexp_static;
1257 mbox_filename_regexp = 0;
1260 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1262 for(f = 0; f < nb_fields_to_parse; f++) {
1263 if(regcomp(&fields_to_parse[f].regexp,
1264 fields_to_parse[f].regexp_string,
1265 fields_to_parse[f].cflags)) {
1267 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1268 fields_to_parse[f].regexp_string,
1269 field_keys[fields_to_parse[f].id]);
1275 "%s version_%s format_%d raw\n",
1276 MYMAIL_DB_MAGIC_TOKEN,
1278 MYMAIL_DB_FORMAT_VERSION);
1280 while(optind < argc) {
1281 recursive_index_mbox(db_file,
1282 argv[optind], mbox_filename_regexp,
1283 nb_fields_to_parse, fields_to_parse);
1290 if(mbox_filename_regexp) {
1291 regfree(mbox_filename_regexp);
1294 for(f = 0; f < nb_fields_to_parse; f++) {
1295 regfree(&fields_to_parse[f].regexp);
1304 int nb_extracted_mails = 0;
1306 if(output_filename[0]) {
1307 output_file = safe_fopen(output_filename, "w", "result mbox");
1309 output_file = stdout;
1313 if(nb_search_conditions > 0) {
1315 /* Recursive search if db_root_path is set */
1318 regex_t db_filename_regexp;
1319 if(regcomp(&db_filename_regexp,
1320 db_filename_regexp_string,
1323 "mymail: Syntax error in regexp \"%s\".\n",
1324 db_filename_regexp_string);
1328 nb_extracted_mails = recursive_search_in_db(db_root_path, &db_filename_regexp,
1330 nb_search_conditions, search_conditions,
1333 regfree(&db_filename_regexp);
1336 /* Search in all db files listed in db_filename_list */
1338 if(db_filename_list) {
1339 char db_filename[PATH_MAX + 1];
1342 s = db_filename_list;
1345 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1347 if(db_filename[0]) {
1348 nb_extracted_mails =
1349 search_in_db(db_filename,
1351 nb_search_conditions, search_conditions, output_file);
1356 /* Search in all db files listed in the command arguments */
1358 while(optind < argc) {
1359 nb_extracted_mails =
1360 search_in_db(argv[optind],
1362 nb_search_conditions, search_conditions, output_file);
1368 if(nb_extracted_mails > 0) {
1369 printf("Found %d matching mails.\n", nb_extracted_mails);
1371 printf("No matching mail found.\n");
1375 fflush(output_file);
1377 if(output_file != stdout) {
1378 fclose(output_file);
1382 for(n = 0; n < nb_search_conditions; n++) {
1383 free_condition(&search_conditions[n]);
1386 a = global_alias_list;
1396 free(db_filename_regexp_string);
1398 free(db_filename_list);
1399 free(mbox_filename_regexp_string);
1401 regfree(&global_leading_from_line_regexp);