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 "mymail: 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\": %s\n",
222 /*********************************************************************/
224 void print_version(FILE *out) {
225 fprintf(out, "mymail version %s (%s)\n", MYMAIL_VERSION, UNAME);
228 void print_usage(FILE *out) {
230 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
232 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
234 fprintf(out, " -h, --help\n");
235 fprintf(out, " show this help\n");
236 fprintf(out, " -v, --version\n");
237 fprintf(out, " print the version number\n");
238 fprintf(out, " -q, --quiet\n");
239 fprintf(out, " do not print information during search\n");
240 fprintf(out, " -t, --use-leading-time\n");
241 fprintf(out, " use the time stamp from the leading line of each mail and not the Date:\n");
242 fprintf(out, " field\n");
243 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
244 fprintf(out, " set the db filename pattern for recursive search\n");
245 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
246 fprintf(out, " set the db root path for recursive search\n");
247 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
248 fprintf(out, " set the semicolon-separated list of db files for search\n");
249 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
250 fprintf(out, " set the mbox filename pattern for recursive search\n");
251 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
252 fprintf(out, " search for matching mails in the db file\n");
253 fprintf(out, " -d <db filename>, --db-file-output <db filename>\n");
254 fprintf(out, " set the db filename for indexing\n");
255 fprintf(out, " -i, --index\n");
256 fprintf(out, " index mails\n");
257 fprintf(out, " -o <output filename>, --output <output filename>\n");
258 fprintf(out, " set the result file, use stdout if unset\n");
259 fprintf(out, " -n <max number of mails>, --nb-mails-max <max number of mails>\n");
260 fprintf(out, " set the maximum number of mails to extract\n");
261 fprintf(out, " -a <search field>, --default-search <search field>\n");
262 fprintf(out, " set the default search field\n");
265 /*********************************************************************/
267 int ignore_entry(const char *name) {
269 strcmp(name, ".") == 0 ||
270 strcmp(name, "..") == 0 ||
271 (name[0] == '.' && name[1] != '/');
274 int is_a_leading_from_line(char *mbox_line) {
276 strncmp(mbox_line, "From ", 5) == 0 &&
277 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
280 int db_line_match_search(struct search_condition *condition,
281 int db_key, const char *db_value) {
285 (condition->db_key == db_key)
289 (condition->db_key == ID_PARTICIPANT && (db_key == ID_LEADING_LINE ||
294 (condition->db_key == ID_FROM && db_key == ID_LEADING_LINE)
300 regexec(&condition->db_value_regexp, db_value, 0, 0, 0) == 0;
303 void update_body_hits(char *mail_filename, int position_in_mail,
304 int nb_search_conditions, struct search_condition *search_conditions,
305 int nb_body_conditions,
309 char raw_mbox_line[BUFFER_SIZE];
315 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
317 fseek(mail_file, position_in_mail, SEEK_SET);
319 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
320 while(nb_body_hits < nb_body_conditions) {
321 if(raw_mbox_line[0] == '\n') { header = 0; }
324 for(n = 0; n < nb_search_conditions; n++) {
325 if(search_conditions[n].db_key == ID_BODY && !hits[n]) {
327 (regexec(&search_conditions[n].db_value_regexp, raw_mbox_line, 0, 0, 0) == 0);
335 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
336 (is_a_leading_from_line(raw_mbox_line)))
344 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
346 char raw_mbox_line[BUFFER_SIZE];
349 /* printf("Extract\n"); */
351 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
352 fseek(mail_file, position_in_mail, SEEK_SET);
354 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
355 fprintf(output_file, "%s", raw_mbox_line);
357 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
358 is_a_leading_from_line(raw_mbox_line))
360 fprintf(output_file, "%s", raw_mbox_line);
367 int check_full_mail_match(char *current_mail_filename,
369 int nb_search_conditions,
370 struct search_condition *search_conditions,
371 int nb_body_conditions,
373 int current_position_in_mail) {
374 int n, nb_fulfilled_body_conditions;
376 for(n = 0; n < nb_search_conditions; n++) {
377 if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
378 hits[n] = (mail_time >= search_conditions[n].time_start &&
379 (search_conditions[n].time_stop == 0 ||
380 mail_time <= search_conditions[n].time_stop));
384 /* We first check all conditions but the body ones */
386 for(n = 0; n < nb_search_conditions &&
387 ((search_conditions[n].db_key == ID_BODY) ||
388 xor(hits[n], search_conditions[n].negation)); n++);
390 if(n == nb_search_conditions) {
392 /* Now check the body ones */
394 nb_fulfilled_body_conditions = 0;
396 if(nb_body_conditions > 0) {
397 update_body_hits(current_mail_filename, current_position_in_mail,
398 nb_search_conditions, search_conditions,
402 for(n = 0; n < nb_search_conditions; n++) {
403 if(search_conditions[n].db_key == ID_BODY &&
404 xor(hits[n], search_conditions[n].negation)) {
405 nb_fulfilled_body_conditions++;
409 return nb_body_conditions == nb_fulfilled_body_conditions;
415 /* We use the mail leading line time by default, and if we should and
416 can, we update with the Date: field */
418 void update_time(int db_key, const char *db_value, time_t *t) {
422 memset(&tm, 0, sizeof(struct tm));
424 if(db_key == ID_LEADING_LINE) {
426 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
427 /* printf("From %s", db_value); */
428 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
431 if(!global_use_leading_time) {
432 if(db_key == ID_DATE) {
433 if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
434 strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
435 /* printf("Date: %s", db_value); */
443 int search_in_db(const char *db_filename,
444 int nb_extracted_mails,
445 int nb_search_conditions,
446 struct search_condition *search_conditions,
450 char raw_db_line[BUFFER_SIZE];
451 char current_mail_filename[PATH_MAX + 1];
452 char db_key_string[TOKEN_BUFFER_SIZE];
453 char position_in_file_string[TOKEN_BUFFER_SIZE];
454 unsigned long int current_position_in_mail;
455 const char *db_value;
457 int hits[MAX_NB_SEARCH_CONDITIONS];
458 int nb_body_conditions, need_time;
464 printf("Searching in '%s' ... ", db_filename);
468 db_file = safe_fopen(db_filename, "r", "index file for search");
470 /* First, check the db file leading line integrity */
472 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
473 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
475 "mymail: Header line in '%s' does not match the mymail db format.\n",
481 "mymail: Cannot read the header line in '%s'.\n",
486 /* Then parse the said db file */
488 current_position_in_mail = 0;
490 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
492 nb_body_conditions = 0;
496 for(n = 0; n < nb_search_conditions; n++) {
497 if(search_conditions[n].db_key == ID_BODY) {
498 nb_body_conditions++;
500 else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
505 strcpy(current_mail_filename, "");
507 while(nb_extracted_mails < global_nb_mails_max &&
508 fgets(raw_db_line, BUFFER_SIZE, db_file)) {
509 db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
511 if(strcmp("mail", db_key_string) == 0) {
512 if(current_mail_filename[0]) {
513 if(check_full_mail_match(current_mail_filename,
515 nb_search_conditions, search_conditions,
516 nb_body_conditions, hits, current_position_in_mail)) {
517 extract_mail(current_mail_filename, current_position_in_mail, output_file);
518 nb_extracted_mails++;
522 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
523 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
524 db_value = parse_token(current_mail_filename, PATH_MAX+1, '\n', db_value);
525 current_position_in_mail = atol(position_in_file_string);
530 for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
531 if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
536 for(n = 0; n < nb_search_conditions; n++) {
537 hits[n] |= db_line_match_search(&search_conditions[n],
542 update_time(db_key, db_value, &mail_time);
547 if(nb_extracted_mails < global_nb_mails_max &&
548 current_mail_filename[0] &&
549 check_full_mail_match(current_mail_filename,
551 nb_search_conditions, search_conditions,
552 nb_body_conditions, hits, current_position_in_mail)) {
553 extract_mail(current_mail_filename, current_position_in_mail, output_file);
554 nb_extracted_mails++;
564 return nb_extracted_mails;
567 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
568 int nb_extracted_mails,
569 int nb_search_conditions,
570 struct search_condition *search_conditions,
573 struct dirent *dir_e;
575 char subname[PATH_MAX + 1];
577 if(lstat(entry_name, &sb) != 0) {
579 "mymail: Cannot stat \"%s\": %s\n",
585 /* printf("recursive_search_in_db %s\n", entry_name); */
587 dir = opendir(entry_name);
590 while((dir_e = readdir(dir)) &&
591 nb_extracted_mails < global_nb_mails_max) {
592 if(!ignore_entry(dir_e->d_name)) {
593 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
594 nb_extracted_mails = recursive_search_in_db(subname, db_filename_regexp,
596 nb_search_conditions, search_conditions,
604 const char *s = entry_name, *filename = entry_name;
605 while(*s) { if(*s == '/') { filename = s+1; } s++; }
607 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
609 search_in_db(entry_name,
611 nb_search_conditions, search_conditions, output_file);
615 return nb_extracted_mails;
618 /*********************************************************************/
620 void index_one_mbox_line(unsigned int nb_fields_to_parse,
621 struct parsable_field *fields_to_parse,
622 char *raw_mbox_line, FILE *db_file) {
625 for(f = 0; f < nb_fields_to_parse; f++) {
626 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
627 fprintf(db_file, "%s %s\n",
628 field_keys[fields_to_parse[f].id],
629 raw_mbox_line + matches.rm_eo);
634 void index_mbox(const char *mbox_filename,
635 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
637 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
638 char *end_of_full_line;
640 int in_header, new_header;
641 unsigned long int position_in_file;
643 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
648 position_in_file = 0;
649 end_of_full_line = 0;
652 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
653 if(is_a_leading_from_line(raw_mbox_line)) {
654 /* This starts a new mail */
657 "Got a ^\"From \" in the header in %s:%lu.\n",
658 mbox_filename, position_in_file);
659 fprintf(stderr, "%s", raw_mbox_line);
662 /* printf("LEADING_LINE %s", raw_mbox_line); */
666 } else if(raw_mbox_line[0] == '\n') {
669 /* We leave the header, index the current line */
671 /* printf("INDEX %s\n", full_line); */
672 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
674 end_of_full_line = full_line;
675 *end_of_full_line = '\0';
681 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
685 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
686 /* Continuation of a line */
687 char *start = raw_mbox_line;
688 while(*start == ' ' || *start == '\t') start++;
689 *(end_of_full_line++) = ' ';
690 strcpy(end_of_full_line, start);
691 while(*end_of_full_line && *end_of_full_line != '\n') {
694 *end_of_full_line = '\0';
698 /* Start a new header line, not a continuation */
701 /* printf("INDEX %s\n", full_line); */
702 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
705 end_of_full_line = full_line;
706 strcpy(end_of_full_line, raw_mbox_line);
707 while(*end_of_full_line && *end_of_full_line != '\n') {
710 *end_of_full_line = '\0';
715 position_in_file += strlen(raw_mbox_line);
721 void recursive_index_mbox(FILE *db_file,
722 const char *entry_name, regex_t *mbox_filename_regexp,
723 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
725 struct dirent *dir_e;
727 char subname[PATH_MAX + 1];
729 if(lstat(entry_name, &sb) != 0) {
731 "mymail: Cannot stat \"%s\": %s\n",
737 dir = opendir(entry_name);
740 while((dir_e = readdir(dir))) {
741 if(!ignore_entry(dir_e->d_name)) {
742 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
743 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
744 nb_fields_to_parse, fields_to_parse);
749 const char *s = entry_name, *filename = s;
750 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
751 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
752 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
757 /*********************************************************************/
759 /* For long options that have no equivalent short option, use a
760 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
762 OPT_BASH_MODE = CHAR_MAX + 1
765 static struct option long_options[] = {
766 { "help", no_argument, 0, 'h' },
767 { "version", no_argument, 0, 'v' },
768 { "quiet", no_argument, 0, 'q' },
769 { "use-leading-time", no_argument, 0, 't' },
770 { "db-file-output", 1, 0, 'd' },
771 { "db-pattern", 1, 0, 'p' },
772 { "db-root", 1, 0, 'r' },
773 { "db-list", 1, 0, 'l' },
774 { "mbox-pattern", 1, 0, 'm' },
775 { "search", 1, 0, 's' },
776 { "index", 0, 0, 'i' },
777 { "output", 1, 0, 'o' },
778 { "default-search", 1, 0, 'a' },
779 { "nb-mails-max", 1, 0, 'n' },
783 struct time_criterion {
786 int start_hour, end_hour;
790 /*********************************************************************/
792 static struct time_criterion time_criteria[] = {
794 { "8h", 0, 8, -1, -1 },
795 { "24h", 0, 24, -1, -1 },
796 { "48h", 0, 48, -1, -1 },
797 { "week", 0, 24 * 7, -1, -1 },
798 { "month", 0, 24 * 31, -1, -1 },
799 { "trimester", 0, 24 * 92, -1, -1 },
800 { "year", 0, 24 * 365, -1, -1 },
802 { "yesterday", 1, -1, -1, -1 },
803 { "today", 1, -1, -1, 0 },
805 { "monday", 1, -1, -1, 1 },
806 { "tuesday", 1, -1, -1, 2 },
807 { "wednesday", 1, -1, -1, 3 },
808 { "thursday", 1, -1, -1, 4 },
809 { "friday", 1, -1, -1, 5 },
810 { "saturday", 1, -1, -1, 6 },
811 { "sunday", 1, -1, -1, 7 },
815 /*********************************************************************/
817 time_t time_for_past_day(int day) {
824 delta_day = (7 + tm->tm_wday - day) % 7;
828 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
831 void init_condition(struct search_condition *condition, const char *full_string,
832 const char *default_search_field) {
833 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
836 struct alias_node *a;
838 for(a = global_alias_list; a; a = a->next) {
839 if(strcmp(full_string, a->alias) == 0) {
840 full_string = a->value;
845 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
846 search_field = full_search_field;
848 if(search_field[0] == '!') {
850 condition->negation = 1;
852 condition->negation = 0;
855 condition->db_key = -1;
859 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
860 if(strcmp(time_criteria[k].label, search_field) == 0) {
861 condition->db_key = ID_TIME_INTERVAL;
862 if(time_criteria[k].day_criterion) {
863 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
864 condition->time_stop = condition->time_start + 3600 * 24;
866 condition->time_start = time(0) - 3600 * time_criteria[k].start_hour;
867 if(time_criteria[k].end_hour >= 0) {
868 condition->time_stop = time(0) - 3600 * time_criteria[k].end_hour;
870 condition->time_stop = 0;
878 if(condition->db_key == -1) {
880 /* No time condition matched, look for the search fields */
882 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
883 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
884 condition->db_key = m;
888 /* None match, if there is a default search field, re-run the search with it */
890 if(condition->db_key == -1) {
891 if(default_search_field) {
892 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
893 if(strncmp(field_keys[m],
894 default_search_field, strlen(default_search_field)) == 0) {
895 condition->db_key = m;
898 string = full_string;
899 if(string[0] == '!') { string++; }
903 if(condition->db_key == -1) {
905 "mymail: Syntax error in field key \"%s\".\n",
910 if(regcomp(&condition->db_value_regexp,
914 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
916 field_keys[condition->db_key]);
922 void free_condition(struct search_condition *condition) {
923 if(condition->db_key != ID_TIME_INTERVAL) {
924 regfree(&condition->db_value_regexp);
928 const char *eat_space(const char *s) {
929 while(*s == ' ' || *s == '\t') { s++; }
933 void read_rc_file(const char *rc_filename) {
934 char raw_line[BUFFER_SIZE];
935 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
942 rc_file = fopen(rc_filename, "r");
946 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
948 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
953 if(*s && *s != '#') {
954 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
956 if(strcmp(command, "alias") == 0) {
957 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
958 a->next = global_alias_list;
959 global_alias_list = a;
962 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
963 a->alias = strdup(tmp_token);
966 a->value = strdup(s);
968 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
974 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
980 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
994 /*********************************************************************/
995 /*********************************************************************/
996 /*********************************************************************/
998 int main(int argc, char **argv) {
999 char *db_filename = 0;
1000 char *db_filename_regexp_string = 0;
1001 char *db_root_path = 0;
1002 char *db_filename_list = 0;
1003 char *mbox_filename_regexp_string = 0;
1004 char *default_search_field;
1005 char output_filename[PATH_MAX + 1];
1006 char rc_filename[PATH_MAX + 1];
1007 int action_index = 0;
1008 int error = 0, show_help = 0;
1009 const unsigned int nb_fields_to_parse =
1010 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1013 unsigned int nb_search_conditions;
1014 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1015 struct alias_node *a, *b;
1017 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1019 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1023 if(getenv("MYMAILRC")) {
1024 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1025 } else if(getenv("HOME")) {
1026 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1028 rc_filename[0] = '\0';
1031 global_alias_list = 0;
1033 global_use_leading_time = 0;
1034 global_nb_mails_max = 250;
1036 default_search_field = 0;
1037 strncpy(output_filename, "", PATH_MAX);
1039 if(rc_filename[0]) {
1040 read_rc_file(rc_filename);
1045 #warning Test code added on 2013 May 02 11:17:01
1046 struct alias_node *a;
1047 for(a = global_alias_list; a; a = a->next) {
1048 printf ("ALIAS [%s] [%s]\n", a->alias, a->value);
1053 setlocale(LC_ALL, "");
1055 nb_search_conditions = 0;
1057 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
1058 long_options, NULL)) != -1) {
1067 print_version(stdout);
1075 global_use_leading_time = 1;
1084 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1087 db_filename = strdup(optarg);
1091 if(db_filename_regexp_string) {
1092 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1095 db_filename_regexp_string = strdup(optarg);
1099 if(mbox_filename_regexp_string) {
1100 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1103 mbox_filename_regexp_string = strdup(optarg);
1107 strncpy(output_filename, optarg, PATH_MAX);
1112 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1115 db_root_path = strdup(optarg);
1119 if(db_filename_list) {
1120 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1123 db_filename_list = strdup(optarg);
1127 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1128 fprintf(stderr, "mymail: Too many search patterns.\n");
1131 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1132 nb_search_conditions++;
1136 default_search_field = optarg;
1140 global_nb_mails_max = atoi(optarg);
1149 /* Set all the values that may defined in the arguments, through
1150 environment variables, or hard-coded */
1152 db_filename = default_value(db_filename,
1156 db_filename_regexp_string = default_value(db_filename_regexp_string,
1160 db_root_path = default_value(db_root_path,
1164 db_filename_list = default_value(db_filename_list,
1168 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1169 "MYMAIL_MBOX_PATTERN",
1172 /* Start the processing */
1175 print_usage(stderr);
1180 print_usage(stdout);
1188 regex_t mbox_filename_regexp_static;
1189 regex_t *mbox_filename_regexp;
1191 if(mbox_filename_regexp_string) {
1192 if(regcomp(&mbox_filename_regexp_static,
1193 mbox_filename_regexp_string,
1196 "mymail: Syntax error in regexp \"%s\".\n",
1197 mbox_filename_regexp_string);
1200 mbox_filename_regexp = &mbox_filename_regexp_static;
1202 mbox_filename_regexp = 0;
1205 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1207 for(f = 0; f < nb_fields_to_parse; f++) {
1208 if(regcomp(&fields_to_parse[f].regexp,
1209 fields_to_parse[f].regexp_string,
1210 fields_to_parse[f].cflags)) {
1212 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1213 fields_to_parse[f].regexp_string,
1214 field_keys[fields_to_parse[f].id]);
1220 "%s version_%s format_%d raw\n",
1221 MYMAIL_DB_MAGIC_TOKEN,
1223 MYMAIL_DB_FORMAT_VERSION);
1225 while(optind < argc) {
1226 recursive_index_mbox(db_file,
1227 argv[optind], mbox_filename_regexp,
1228 nb_fields_to_parse, fields_to_parse);
1235 if(mbox_filename_regexp) {
1236 regfree(mbox_filename_regexp);
1239 for(f = 0; f < nb_fields_to_parse; f++) {
1240 regfree(&fields_to_parse[f].regexp);
1249 int nb_extracted_mails = 0;
1251 if(output_filename[0]) {
1252 output_file = safe_fopen(output_filename, "w", "result mbox");
1254 output_file = stdout;
1258 if(nb_search_conditions > 0) {
1260 /* Recursive search if db_root_path is set */
1263 regex_t db_filename_regexp;
1264 if(regcomp(&db_filename_regexp,
1265 db_filename_regexp_string,
1268 "mymail: Syntax error in regexp \"%s\".\n",
1269 db_filename_regexp_string);
1273 nb_extracted_mails = recursive_search_in_db(db_root_path, &db_filename_regexp,
1275 nb_search_conditions, search_conditions,
1278 regfree(&db_filename_regexp);
1281 /* Search in all db files listed in db_filename_list */
1283 if(db_filename_list) {
1284 char db_filename[PATH_MAX + 1];
1287 s = db_filename_list;
1290 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1292 if(db_filename[0]) {
1293 nb_extracted_mails =
1294 search_in_db(db_filename,
1296 nb_search_conditions, search_conditions, output_file);
1301 /* Search in all db files listed in the command arguments */
1303 while(optind < argc) {
1304 nb_extracted_mails =
1305 search_in_db(argv[optind],
1307 nb_search_conditions, search_conditions, output_file);
1313 if(nb_extracted_mails > 0) {
1314 printf("Found %d matching mails.\n", nb_extracted_mails);
1316 printf("No matching mail found.\n");
1320 fflush(output_file);
1322 if(output_file != stdout) {
1323 fclose(output_file);
1327 for(n = 0; n < nb_search_conditions; n++) {
1328 free_condition(&search_conditions[n]);
1331 a = global_alias_list;
1341 free(db_filename_regexp_string);
1343 free(db_filename_list);
1344 free(mbox_filename_regexp_string);
1346 regfree(&global_leading_from_line_regexp);