3 * Copyright (c) 2013 Francois Fleuret
4 * Written by Francois Fleuret <francois@fleuret.org>
6 * This file is part of mymail.
8 * mymail is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
12 * mymail is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with mymail. If not, see <http://www.gnu.org/licenses/>.
24 This command is a dumb mail indexer. It can either (1) scan
25 directories containing mbox files, and create a db file containing
26 for each mail a list of fields computed from the header, or (2)
27 read such a db file and get all the mails matching regexp-defined
28 conditions on the fields, to create a resulting mbox file.
30 It is low-tech, simple, light and fast.
48 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
49 #define MYMAIL_VERSION "0.9.9"
51 #define MYMAIL_DB_FORMAT_VERSION 1
53 #define MAX_NB_SEARCH_CONDITIONS 32
55 #define BUFFER_SIZE 65536
56 #define TOKEN_BUFFER_SIZE 1024
58 #define LEADING_FROM_LINE_REGEXP_STRING "^From .*\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\) \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\) [ 0123][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]\n$"
60 /********************************************************************/
64 struct alias_node *next;
67 /* Global variables! */
70 int global_use_leading_time;
71 int global_nb_mails_max;
72 regex_t global_leading_from_line_regexp;
73 struct alias_node *global_alias_list;
75 /********************************************************************/
90 static char *field_keys[] = {
102 /********************************************************************/
104 struct search_condition {
106 regex_t db_value_regexp;
108 time_t time_start, time_stop;
111 /********************************************************************/
113 struct parsable_field {
120 static struct parsable_field fields_to_parse[] = {
125 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
131 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
132 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
138 "^\\(to\\|cc\\|bcc\\): ",
139 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
146 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
153 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
158 /********************************************************************/
160 int xor(int a, int b) {
161 return (a && !b) || (!a && b);
164 const char *parse_token(char *token_buffer, size_t token_buffer_size,
165 char separator, const char *string) {
166 char *u = token_buffer;
168 while(*string == separator) { string++; }
170 while(u < token_buffer + token_buffer_size - 1 && *string && *string != separator) {
171 *(u++) = *(string++);
174 while(*string == separator) { string++; }
180 char *default_value(char *current_value,
181 const char *env_variable,
182 const char *hard_default_value) {
184 return current_value;
186 char *env_value = getenv(env_variable);
188 return strdup(env_value);
189 } else if(hard_default_value) {
190 return strdup(hard_default_value);
197 /********************************************************************/
199 void *safe_malloc(size_t n) {
203 "selector: cannot allocate memory: %s\n", strerror(errno));
209 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
210 FILE *result = fopen(path, mode);
215 "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
216 path, comment, mode);
221 /*********************************************************************/
223 void print_version(FILE *out) {
224 fprintf(out, "mymail version %s (%s)\n", MYMAIL_VERSION, UNAME);
227 void print_usage(FILE *out) {
229 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
231 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
233 fprintf(out, " -h, --help\n");
234 fprintf(out, " show this help\n");
235 fprintf(out, " -v, --version\n");
236 fprintf(out, " print the version number\n");
237 fprintf(out, " -q, --quiet\n");
238 fprintf(out, " do not print information during search\n");
239 fprintf(out, " -t, --use-leading-time\n");
240 fprintf(out, " use the time stamp from the leading line of each mail and not the Date:\n");
241 fprintf(out, " field\n");
242 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
243 fprintf(out, " set the db filename pattern for recursive search\n");
244 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
245 fprintf(out, " set the db root path for recursive search\n");
246 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
247 fprintf(out, " set the semicolon-separated list of db files for search\n");
248 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
249 fprintf(out, " set the mbox filename pattern for recursive search\n");
250 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
251 fprintf(out, " search for matching mails in the db file\n");
252 fprintf(out, " -d <db filename>, --db-file-output <db filename>\n");
253 fprintf(out, " set the db filename for indexing\n");
254 fprintf(out, " -i, --index\n");
255 fprintf(out, " index mails\n");
256 fprintf(out, " -o <output filename>, --output <output filename>\n");
257 fprintf(out, " set the result file, use stdout if unset\n");
258 fprintf(out, " -n <max number of mails>, --nb-mails-max <max number of mails>\n");
259 fprintf(out, " set the maximum number of mails to extract\n");
260 fprintf(out, " -a <search field>, --default-search <search field>\n");
261 fprintf(out, " set the default search field\n");
264 /*********************************************************************/
266 int ignore_entry(const char *name) {
268 strcmp(name, ".") == 0 ||
269 strcmp(name, "..") == 0 ||
270 (name[0] == '.' && name[1] != '/');
273 int is_a_leading_from_line(char *mbox_line) {
275 strncmp(mbox_line, "From ", 5) == 0 &&
276 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
279 int db_line_match_search(struct search_condition *condition,
280 int db_key, const char *db_value) {
284 (condition->db_key == db_key)
288 (condition->db_key == ID_PARTICIPANT && (db_key == ID_LEADING_LINE ||
293 (condition->db_key == ID_FROM && db_key == ID_LEADING_LINE)
299 regexec(&condition->db_value_regexp, db_value, 0, 0, 0) == 0;
302 void update_body_hits(char *mail_filename, int position_in_mail,
303 int nb_search_conditions, struct search_condition *search_conditions,
304 int nb_body_conditions,
308 char raw_mbox_line[BUFFER_SIZE];
314 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
316 fseek(mail_file, position_in_mail, SEEK_SET);
318 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
319 while(nb_body_hits < nb_body_conditions) {
320 if(raw_mbox_line[0] == '\n') { header = 0; }
323 for(n = 0; n < nb_search_conditions; n++) {
324 if(search_conditions[n].db_key == ID_BODY && !hits[n]) {
326 (regexec(&search_conditions[n].db_value_regexp, raw_mbox_line, 0, 0, 0) == 0);
334 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
335 (is_a_leading_from_line(raw_mbox_line)))
343 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
345 char raw_mbox_line[BUFFER_SIZE];
348 /* printf("Extract\n"); */
350 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
351 fseek(mail_file, position_in_mail, SEEK_SET);
353 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
354 fprintf(output_file, "%s", raw_mbox_line);
356 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
357 is_a_leading_from_line(raw_mbox_line))
359 fprintf(output_file, "%s", raw_mbox_line);
366 int check_full_mail_match(char *current_mail_filename,
368 int nb_search_conditions,
369 struct search_condition *search_conditions,
370 int nb_body_conditions,
372 int current_position_in_mail) {
373 int n, nb_fulfilled_body_conditions;
375 for(n = 0; n < nb_search_conditions; n++) {
376 if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
377 hits[n] = (mail_time >= search_conditions[n].time_start &&
378 (search_conditions[n].time_stop == 0 ||
379 mail_time <= search_conditions[n].time_stop));
383 /* We first check all conditions but the body ones */
385 for(n = 0; n < nb_search_conditions &&
386 ((search_conditions[n].db_key == ID_BODY) ||
387 xor(hits[n], search_conditions[n].negation)); n++);
389 if(n == nb_search_conditions) {
391 /* Now check the body ones */
393 nb_fulfilled_body_conditions = 0;
395 if(nb_body_conditions > 0) {
396 update_body_hits(current_mail_filename, current_position_in_mail,
397 nb_search_conditions, search_conditions,
401 for(n = 0; n < nb_search_conditions; n++) {
402 if(search_conditions[n].db_key == ID_BODY &&
403 xor(hits[n], search_conditions[n].negation)) {
404 nb_fulfilled_body_conditions++;
408 return nb_body_conditions == nb_fulfilled_body_conditions;
414 /* We use the mail leading line time by default, and if we should and
415 can, we update with the Date: field */
417 void update_time(int db_key, const char *db_value, time_t *t) {
421 memset(&tm, 0, sizeof(struct tm));
423 if(db_key == ID_LEADING_LINE) {
425 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
426 /* printf("From %s", db_value); */
427 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
430 if(!global_use_leading_time) {
431 if(db_key == ID_DATE) {
432 if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
433 strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
434 /* printf("Date: %s", db_value); */
442 int search_in_db(const char *db_filename,
443 int nb_extracted_mails,
444 int nb_search_conditions,
445 struct search_condition *search_conditions,
449 char raw_db_line[BUFFER_SIZE];
450 char current_mail_filename[PATH_MAX + 1];
451 char db_key_string[TOKEN_BUFFER_SIZE];
452 char position_in_file_string[TOKEN_BUFFER_SIZE];
453 unsigned long int current_position_in_mail;
454 const char *db_value;
456 int hits[MAX_NB_SEARCH_CONDITIONS];
457 int nb_body_conditions, need_time;
463 printf("Searching in '%s' ... ", db_filename);
467 db_file = safe_fopen(db_filename, "r", "index file for search");
469 /* First, check the db file leading line integrity */
471 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
472 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
474 "mymail: Header line in '%s' does not match the mymail db format.\n",
480 "mymail: Cannot read the header line in '%s'.\n",
485 /* Then parse the said db file */
487 current_position_in_mail = 0;
489 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
491 nb_body_conditions = 0;
495 for(n = 0; n < nb_search_conditions; n++) {
496 if(search_conditions[n].db_key == ID_BODY) {
497 nb_body_conditions++;
499 else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
504 strcpy(current_mail_filename, "");
506 while(nb_extracted_mails < global_nb_mails_max &&
507 fgets(raw_db_line, BUFFER_SIZE, db_file)) {
508 db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
510 if(strcmp("mail", db_key_string) == 0) {
511 if(current_mail_filename[0]) {
512 if(check_full_mail_match(current_mail_filename,
514 nb_search_conditions, search_conditions,
515 nb_body_conditions, hits, current_position_in_mail)) {
516 extract_mail(current_mail_filename, current_position_in_mail, output_file);
517 nb_extracted_mails++;
521 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
522 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
523 db_value = parse_token(current_mail_filename, PATH_MAX+1, '\n', db_value);
524 current_position_in_mail = atol(position_in_file_string);
529 for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
530 if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
535 for(n = 0; n < nb_search_conditions; n++) {
536 hits[n] |= db_line_match_search(&search_conditions[n],
541 update_time(db_key, db_value, &mail_time);
546 if(nb_extracted_mails < global_nb_mails_max &&
547 current_mail_filename[0] &&
548 check_full_mail_match(current_mail_filename,
550 nb_search_conditions, search_conditions,
551 nb_body_conditions, hits, current_position_in_mail)) {
552 extract_mail(current_mail_filename, current_position_in_mail, output_file);
553 nb_extracted_mails++;
563 return nb_extracted_mails;
566 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
567 int nb_extracted_mails,
568 int nb_search_conditions,
569 struct search_condition *search_conditions,
572 struct dirent *dir_e;
574 char subname[PATH_MAX + 1];
576 if(lstat(entry_name, &sb) != 0) {
578 "mymail: Cannot stat \"%s\": %s\n",
584 /* printf("recursive_search_in_db %s\n", entry_name); */
586 dir = opendir(entry_name);
589 while((dir_e = readdir(dir)) &&
590 nb_extracted_mails < global_nb_mails_max) {
591 if(!ignore_entry(dir_e->d_name)) {
592 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
593 nb_extracted_mails = recursive_search_in_db(subname, db_filename_regexp,
595 nb_search_conditions, search_conditions,
603 const char *s = entry_name, *filename = entry_name;
604 while(*s) { if(*s == '/') { filename = s+1; } s++; }
606 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
608 search_in_db(entry_name,
610 nb_search_conditions, search_conditions, output_file);
614 return nb_extracted_mails;
617 /*********************************************************************/
619 void index_one_mbox_line(unsigned int nb_fields_to_parse,
620 struct parsable_field *fields_to_parse,
621 char *raw_mbox_line, FILE *db_file) {
624 for(f = 0; f < nb_fields_to_parse; f++) {
625 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
626 fprintf(db_file, "%s %s\n",
627 field_keys[fields_to_parse[f].id],
628 raw_mbox_line + matches.rm_eo);
633 void index_mbox(const char *mbox_filename,
634 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
636 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
637 char *end_of_full_line;
639 int in_header, new_header;
640 unsigned long int position_in_file;
642 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
647 position_in_file = 0;
648 end_of_full_line = 0;
651 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
652 if(is_a_leading_from_line(raw_mbox_line)) {
653 /* This starts a new mail */
656 "Got a ^\"From \" in the header in %s:%lu.\n",
657 mbox_filename, position_in_file);
658 fprintf(stderr, "%s", raw_mbox_line);
661 /* printf("LEADING_LINE %s", raw_mbox_line); */
665 } else if(raw_mbox_line[0] == '\n') {
668 /* We leave the header, index the current line */
670 /* printf("INDEX %s\n", full_line); */
671 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
673 end_of_full_line = full_line;
674 *end_of_full_line = '\0';
680 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
684 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
685 /* Continuation of a line */
686 char *start = raw_mbox_line;
687 while(*start == ' ' || *start == '\t') start++;
688 *(end_of_full_line++) = ' ';
689 strcpy(end_of_full_line, start);
690 while(*end_of_full_line && *end_of_full_line != '\n') {
693 *end_of_full_line = '\0';
697 /* Start a new header line, not a continuation */
700 /* printf("INDEX %s\n", full_line); */
701 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
704 end_of_full_line = full_line;
705 strcpy(end_of_full_line, raw_mbox_line);
706 while(*end_of_full_line && *end_of_full_line != '\n') {
709 *end_of_full_line = '\0';
714 position_in_file += strlen(raw_mbox_line);
720 void recursive_index_mbox(FILE *db_file,
721 const char *entry_name, regex_t *mbox_filename_regexp,
722 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
724 struct dirent *dir_e;
726 char subname[PATH_MAX + 1];
728 if(lstat(entry_name, &sb) != 0) {
730 "mymail: Cannot stat \"%s\": %s\n",
736 dir = opendir(entry_name);
739 while((dir_e = readdir(dir))) {
740 if(!ignore_entry(dir_e->d_name)) {
741 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
742 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
743 nb_fields_to_parse, fields_to_parse);
748 const char *s = entry_name, *filename = s;
749 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
750 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
751 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
756 /*********************************************************************/
758 /* For long options that have no equivalent short option, use a
759 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
761 OPT_BASH_MODE = CHAR_MAX + 1
764 static struct option long_options[] = {
765 { "help", no_argument, 0, 'h' },
766 { "version", no_argument, 0, 'v' },
767 { "quiet", no_argument, 0, 'q' },
768 { "use-leading-time", no_argument, 0, 't' },
769 { "db-file-output", 1, 0, 'd' },
770 { "db-pattern", 1, 0, 'p' },
771 { "db-root", 1, 0, 'r' },
772 { "db-list", 1, 0, 'l' },
773 { "mbox-pattern", 1, 0, 'm' },
774 { "search", 1, 0, 's' },
775 { "index", 0, 0, 'i' },
776 { "output", 1, 0, 'o' },
777 { "default-search", 1, 0, 'a' },
778 { "nb-mails-max", 1, 0, 'n' },
782 struct time_criterion {
785 int start_hour, end_hour;
789 /*********************************************************************/
791 static struct time_criterion time_criteria[] = {
793 { "8h", 0, 8, -1, -1 },
794 { "24h", 0, 24, -1, -1 },
795 { "48h", 0, 48, -1, -1 },
796 { "week", 0, 24 * 7, -1, -1 },
797 { "month", 0, 24 * 31, -1, -1 },
798 { "trimester", 0, 24 * 92, -1, -1 },
799 { "year", 0, 24 * 365, -1, -1 },
801 { "yesterday", 1, -1, -1, -1 },
802 { "today", 1, -1, -1, 0 },
804 { "monday", 1, -1, -1, 1 },
805 { "tuesday", 1, -1, -1, 2 },
806 { "wednesday", 1, -1, -1, 3 },
807 { "thursday", 1, -1, -1, 4 },
808 { "friday", 1, -1, -1, 5 },
809 { "saturday", 1, -1, -1, 6 },
810 { "sunday", 1, -1, -1, 7 },
814 /*********************************************************************/
816 time_t time_for_past_day(int day) {
823 delta_day = (7 + tm->tm_wday - day) % 7;
827 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
830 void init_condition(struct search_condition *condition, const char *full_string,
831 const char *default_search_field) {
832 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
835 struct alias_node *a;
837 for(a = global_alias_list; a; a = a->next) {
838 if(strcmp(full_string, a->alias) == 0) {
839 full_string = a->value;
844 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
845 search_field = full_search_field;
847 if(search_field[0] == '!') {
849 condition->negation = 1;
851 condition->negation = 0;
854 condition->db_key = -1;
858 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
859 if(strcmp(time_criteria[k].label, search_field) == 0) {
860 condition->db_key = ID_TIME_INTERVAL;
861 if(time_criteria[k].day_criterion) {
862 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
863 condition->time_stop = condition->time_start + 3600 * 24;
865 condition->time_start = time(0) - 3600 * time_criteria[k].start_hour;
866 if(time_criteria[k].end_hour >= 0) {
867 condition->time_stop = time(0) - 3600 * time_criteria[k].end_hour;
869 condition->time_stop = 0;
877 if(condition->db_key == -1) {
879 /* No time condition matched, look for the search fields */
881 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
882 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
883 condition->db_key = m;
887 /* None match, if there is a default search field, re-run the search with it */
889 if(condition->db_key == -1) {
890 if(default_search_field) {
891 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
892 if(strncmp(field_keys[m],
893 default_search_field, strlen(default_search_field)) == 0) {
894 condition->db_key = m;
897 string = full_string;
898 if(string[0] == '!') { string++; }
902 if(condition->db_key == -1) {
904 "mymail: Syntax error in field key \"%s\".\n",
909 if(regcomp(&condition->db_value_regexp,
913 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
915 field_keys[condition->db_key]);
921 void free_condition(struct search_condition *condition) {
922 if(condition->db_key != ID_TIME_INTERVAL) {
923 regfree(&condition->db_value_regexp);
927 const char *eat_space(const char *s) {
928 while(*s == ' ' || *s == '\t') { s++; }
932 void read_rc_file(const char *rc_filename) {
933 char raw_line[BUFFER_SIZE];
934 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
941 rc_file = fopen(rc_filename, "r");
945 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
947 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
952 if(*s && *s != '#') {
953 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
955 if(strcmp(command, "alias") == 0) {
956 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
957 a->next = global_alias_list;
958 global_alias_list = a;
961 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
962 a->alias = strdup(tmp_token);
965 a->value = strdup(s);
967 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
973 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
979 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
993 /*********************************************************************/
994 /*********************************************************************/
995 /*********************************************************************/
997 int main(int argc, char **argv) {
998 char *db_filename = 0;
999 char *db_filename_regexp_string = 0;
1000 char *db_root_path = 0;
1001 char *db_filename_list = 0;
1002 char *mbox_filename_regexp_string = 0;
1003 char *default_search_field;
1004 char output_filename[PATH_MAX + 1];
1005 char rc_filename[PATH_MAX + 1];
1006 int action_index = 0;
1007 int error = 0, show_help = 0;
1008 const unsigned int nb_fields_to_parse =
1009 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1012 unsigned int nb_search_conditions;
1013 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1014 struct alias_node *a, *b;
1016 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1018 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1022 if(getenv("MYMAILRC")) {
1023 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1024 } else if(getenv("HOME")) {
1025 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1027 rc_filename[0] = '\0';
1030 global_alias_list = 0;
1032 global_use_leading_time = 0;
1033 global_nb_mails_max = 250;
1035 default_search_field = 0;
1036 strncpy(output_filename, "", PATH_MAX);
1038 if(rc_filename[0]) {
1039 read_rc_file(rc_filename);
1044 #warning Test code added on 2013 May 02 11:17:01
1045 struct alias_node *a;
1046 for(a = global_alias_list; a; a = a->next) {
1047 printf ("ALIAS [%s] [%s]\n", a->alias, a->value);
1052 setlocale(LC_ALL, "");
1054 nb_search_conditions = 0;
1056 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
1057 long_options, NULL)) != -1) {
1066 print_version(stdout);
1074 global_use_leading_time = 1;
1083 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1086 db_filename = strdup(optarg);
1090 if(db_filename_regexp_string) {
1091 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1094 db_filename_regexp_string = strdup(optarg);
1098 if(mbox_filename_regexp_string) {
1099 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1102 mbox_filename_regexp_string = strdup(optarg);
1106 strncpy(output_filename, optarg, PATH_MAX);
1111 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1114 db_root_path = strdup(optarg);
1118 if(db_filename_list) {
1119 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1122 db_filename_list = strdup(optarg);
1126 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1127 fprintf(stderr, "mymail: Too many search patterns.\n");
1130 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1131 nb_search_conditions++;
1135 default_search_field = optarg;
1139 global_nb_mails_max = atoi(optarg);
1148 /* Set all the values that may defined in the arguments, through
1149 environment variables, or hard-coded */
1151 db_filename = default_value(db_filename,
1155 db_filename_regexp_string = default_value(db_filename_regexp_string,
1159 db_root_path = default_value(db_root_path,
1163 db_filename_list = default_value(db_filename_list,
1167 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1168 "MYMAIL_MBOX_PATTERN",
1171 /* Start the processing */
1174 print_usage(stderr);
1179 print_usage(stdout);
1187 regex_t mbox_filename_regexp_static;
1188 regex_t *mbox_filename_regexp;
1190 if(mbox_filename_regexp_string) {
1191 if(regcomp(&mbox_filename_regexp_static,
1192 mbox_filename_regexp_string,
1195 "mymail: Syntax error in regexp \"%s\".\n",
1196 mbox_filename_regexp_string);
1199 mbox_filename_regexp = &mbox_filename_regexp_static;
1201 mbox_filename_regexp = 0;
1204 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1206 for(f = 0; f < nb_fields_to_parse; f++) {
1207 if(regcomp(&fields_to_parse[f].regexp,
1208 fields_to_parse[f].regexp_string,
1209 fields_to_parse[f].cflags)) {
1211 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1212 fields_to_parse[f].regexp_string,
1213 field_keys[fields_to_parse[f].id]);
1219 "%s version_%s format_%d raw\n",
1220 MYMAIL_DB_MAGIC_TOKEN,
1222 MYMAIL_DB_FORMAT_VERSION);
1224 while(optind < argc) {
1225 recursive_index_mbox(db_file,
1226 argv[optind], mbox_filename_regexp,
1227 nb_fields_to_parse, fields_to_parse);
1234 if(mbox_filename_regexp) {
1235 regfree(mbox_filename_regexp);
1238 for(f = 0; f < nb_fields_to_parse; f++) {
1239 regfree(&fields_to_parse[f].regexp);
1248 int nb_extracted_mails = 0;
1250 if(output_filename[0]) {
1251 output_file = safe_fopen(output_filename, "w", "result mbox");
1253 output_file = stdout;
1257 if(nb_search_conditions > 0) {
1259 /* Recursive search if db_root_path is set */
1262 regex_t db_filename_regexp;
1263 if(regcomp(&db_filename_regexp,
1264 db_filename_regexp_string,
1267 "mymail: Syntax error in regexp \"%s\".\n",
1268 db_filename_regexp_string);
1272 nb_extracted_mails = recursive_search_in_db(db_root_path, &db_filename_regexp,
1274 nb_search_conditions, search_conditions,
1277 regfree(&db_filename_regexp);
1280 /* Search in all db files listed in db_filename_list */
1282 if(db_filename_list) {
1283 char db_filename[PATH_MAX + 1];
1286 s = db_filename_list;
1289 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1291 if(db_filename[0]) {
1292 nb_extracted_mails =
1293 search_in_db(db_filename,
1295 nb_search_conditions, search_conditions, output_file);
1300 /* Search in all db files listed in the command arguments */
1302 while(optind < argc) {
1303 nb_extracted_mails =
1304 search_in_db(argv[optind],
1306 nb_search_conditions, search_conditions, output_file);
1312 if(nb_extracted_mails > 0) {
1313 printf("Found %d matching mails.\n", nb_extracted_mails);
1315 printf("No matching mail found.\n");
1319 fflush(output_file);
1321 if(output_file != stdout) {
1322 fclose(output_file);
1326 for(n = 0; n < nb_search_conditions; n++) {
1327 free_condition(&search_conditions[n]);
1330 a = global_alias_list;
1340 free(db_filename_regexp_string);
1342 free(db_filename_list);
1343 free(mbox_filename_regexp_string);
1345 regfree(&global_leading_from_line_regexp);