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)) {
511 char *s = raw_db_line;
512 while(*s && *s != '\n') { s++; }
515 db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
517 if(strcmp("mail", db_key_string) == 0) {
518 if(current_mail_filename[0]) {
519 if(check_full_mail_match(current_mail_filename,
521 nb_search_conditions, search_conditions,
522 nb_body_conditions, hits, current_position_in_mail)) {
523 extract_mail(current_mail_filename, current_position_in_mail, output_file);
524 nb_extracted_mails++;
528 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
529 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
530 strncpy(current_mail_filename, db_value, PATH_MAX + 1);
531 current_position_in_mail = atol(position_in_file_string);
536 for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
537 if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
542 for(n = 0; n < nb_search_conditions; n++) {
543 hits[n] |= db_line_match_search(&search_conditions[n],
548 update_time(db_key, db_value, &mail_time);
553 if(nb_extracted_mails < global_nb_mails_max &&
554 current_mail_filename[0] &&
555 check_full_mail_match(current_mail_filename,
557 nb_search_conditions, search_conditions,
558 nb_body_conditions, hits, current_position_in_mail)) {
559 extract_mail(current_mail_filename, current_position_in_mail, output_file);
560 nb_extracted_mails++;
570 return nb_extracted_mails;
573 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
574 int nb_extracted_mails,
575 int nb_search_conditions,
576 struct search_condition *search_conditions,
579 struct dirent *dir_e;
581 char subname[PATH_MAX + 1];
583 if(lstat(entry_name, &sb) != 0) {
585 "mymail: Cannot stat \"%s\": %s\n",
591 /* printf("recursive_search_in_db %s\n", entry_name); */
593 dir = opendir(entry_name);
596 while((dir_e = readdir(dir)) &&
597 nb_extracted_mails < global_nb_mails_max) {
598 if(!ignore_entry(dir_e->d_name)) {
599 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
600 nb_extracted_mails = recursive_search_in_db(subname, db_filename_regexp,
602 nb_search_conditions, search_conditions,
610 const char *s = entry_name, *filename = entry_name;
611 while(*s) { if(*s == '/') { filename = s+1; } s++; }
613 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
615 search_in_db(entry_name,
617 nb_search_conditions, search_conditions, output_file);
621 return nb_extracted_mails;
624 /*********************************************************************/
626 void index_one_mbox_line(unsigned int nb_fields_to_parse,
627 struct parsable_field *fields_to_parse,
628 char *raw_mbox_line, FILE *db_file) {
631 for(f = 0; f < nb_fields_to_parse; f++) {
632 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
633 fprintf(db_file, "%s %s\n",
634 field_keys[fields_to_parse[f].id],
635 raw_mbox_line + matches.rm_eo);
640 void index_mbox(const char *mbox_filename,
641 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
643 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
644 char *end_of_full_line;
646 int in_header, new_header;
647 unsigned long int position_in_file;
649 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
654 position_in_file = 0;
655 end_of_full_line = 0;
658 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
659 if(is_a_leading_from_line(raw_mbox_line)) {
660 /* This starts a new mail */
663 "Got a ^\"From \" in the header in %s:%lu.\n",
664 mbox_filename, position_in_file);
665 fprintf(stderr, "%s", raw_mbox_line);
668 /* printf("LEADING_LINE %s", raw_mbox_line); */
672 } else if(raw_mbox_line[0] == '\n') {
675 /* We leave the header, index the current line */
677 /* printf("INDEX %s\n", full_line); */
678 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
680 end_of_full_line = full_line;
681 *end_of_full_line = '\0';
687 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
691 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
692 /* Continuation of a line */
693 char *start = raw_mbox_line;
694 while(*start == ' ' || *start == '\t') start++;
695 *(end_of_full_line++) = ' ';
696 strcpy(end_of_full_line, start);
697 while(*end_of_full_line && *end_of_full_line != '\n') {
700 *end_of_full_line = '\0';
704 /* Start a new header line, not a continuation */
707 /* printf("INDEX %s\n", full_line); */
708 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
711 end_of_full_line = full_line;
712 strcpy(end_of_full_line, raw_mbox_line);
713 while(*end_of_full_line && *end_of_full_line != '\n') {
716 *end_of_full_line = '\0';
721 position_in_file += strlen(raw_mbox_line);
727 void recursive_index_mbox(FILE *db_file,
728 const char *entry_name, regex_t *mbox_filename_regexp,
729 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
731 struct dirent *dir_e;
733 char subname[PATH_MAX + 1];
735 if(lstat(entry_name, &sb) != 0) {
737 "mymail: Cannot stat \"%s\": %s\n",
743 dir = opendir(entry_name);
746 while((dir_e = readdir(dir))) {
747 if(!ignore_entry(dir_e->d_name)) {
748 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
749 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
750 nb_fields_to_parse, fields_to_parse);
755 const char *s = entry_name, *filename = s;
756 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
757 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
758 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
763 /*********************************************************************/
765 /* For long options that have no equivalent short option, use a
766 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
768 OPT_BASH_MODE = CHAR_MAX + 1
771 static struct option long_options[] = {
772 { "help", no_argument, 0, 'h' },
773 { "version", no_argument, 0, 'v' },
774 { "quiet", no_argument, 0, 'q' },
775 { "use-leading-time", no_argument, 0, 't' },
776 { "db-file-output", 1, 0, 'd' },
777 { "db-pattern", 1, 0, 'p' },
778 { "db-root", 1, 0, 'r' },
779 { "db-list", 1, 0, 'l' },
780 { "mbox-pattern", 1, 0, 'm' },
781 { "search", 1, 0, 's' },
782 { "index", 0, 0, 'i' },
783 { "output", 1, 0, 'o' },
784 { "default-search", 1, 0, 'a' },
785 { "nb-mails-max", 1, 0, 'n' },
789 struct time_criterion {
792 int start_hour, end_hour;
796 /*********************************************************************/
798 static struct time_criterion time_criteria[] = {
800 { "8h", 0, 8, -1, -1 },
801 { "24h", 0, 24, -1, -1 },
802 { "48h", 0, 48, -1, -1 },
803 { "week", 0, 24 * 7, -1, -1 },
804 { "month", 0, 24 * 31, -1, -1 },
805 { "trimester", 0, 24 * 92, -1, -1 },
806 { "year", 0, 24 * 365, -1, -1 },
808 { "yesterday", 1, -1, -1, -1 },
809 { "today", 1, -1, -1, 0 },
811 { "monday", 1, -1, -1, 1 },
812 { "tuesday", 1, -1, -1, 2 },
813 { "wednesday", 1, -1, -1, 3 },
814 { "thursday", 1, -1, -1, 4 },
815 { "friday", 1, -1, -1, 5 },
816 { "saturday", 1, -1, -1, 6 },
817 { "sunday", 1, -1, -1, 7 },
821 /*********************************************************************/
823 time_t time_for_past_day(int day) {
830 delta_day = (7 + tm->tm_wday - day) % 7;
834 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
837 void init_condition(struct search_condition *condition, const char *full_string,
838 const char *default_search_field) {
839 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
842 struct alias_node *a;
844 for(a = global_alias_list; a; a = a->next) {
845 if(strcmp(full_string, a->alias) == 0) {
846 full_string = a->value;
851 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
852 search_field = full_search_field;
854 if(search_field[0] == '!') {
856 condition->negation = 1;
858 condition->negation = 0;
861 condition->db_key = -1;
865 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
866 if(strcmp(time_criteria[k].label, search_field) == 0) {
867 condition->db_key = ID_TIME_INTERVAL;
868 if(time_criteria[k].day_criterion) {
869 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
870 condition->time_stop = condition->time_start + 3600 * 24;
872 condition->time_start = time(0) - 3600 * time_criteria[k].start_hour;
873 if(time_criteria[k].end_hour >= 0) {
874 condition->time_stop = time(0) - 3600 * time_criteria[k].end_hour;
876 condition->time_stop = 0;
884 if(condition->db_key == -1) {
886 /* No time condition matched, look for the search fields */
888 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
889 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
890 condition->db_key = m;
894 /* None match, if there is a default search field, re-run the search with it */
896 if(condition->db_key == -1) {
897 if(default_search_field) {
898 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
899 if(strncmp(field_keys[m],
900 default_search_field, strlen(default_search_field)) == 0) {
901 condition->db_key = m;
904 string = full_string;
905 if(string[0] == '!') { string++; }
909 if(condition->db_key == -1) {
911 "mymail: Syntax error in field key \"%s\".\n",
916 if(regcomp(&condition->db_value_regexp,
920 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
922 field_keys[condition->db_key]);
928 void free_condition(struct search_condition *condition) {
929 if(condition->db_key != ID_TIME_INTERVAL) {
930 regfree(&condition->db_value_regexp);
934 const char *eat_space(const char *s) {
935 while(*s == ' ' || *s == '\t') { s++; }
939 void read_rc_file(const char *rc_filename) {
940 char raw_line[BUFFER_SIZE];
941 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
948 rc_file = fopen(rc_filename, "r");
952 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
954 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
959 if(*s && *s != '#') {
960 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
962 if(strcmp(command, "alias") == 0) {
963 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
964 a->next = global_alias_list;
965 global_alias_list = a;
968 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
969 a->alias = strdup(tmp_token);
972 a->value = strdup(s);
974 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
980 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
986 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
1000 /*********************************************************************/
1001 /*********************************************************************/
1002 /*********************************************************************/
1004 int main(int argc, char **argv) {
1005 char *db_filename = 0;
1006 char *db_filename_regexp_string = 0;
1007 char *db_root_path = 0;
1008 char *db_filename_list = 0;
1009 char *mbox_filename_regexp_string = 0;
1010 char *default_search_field;
1011 char output_filename[PATH_MAX + 1];
1012 char rc_filename[PATH_MAX + 1];
1013 int action_index = 0;
1014 int error = 0, show_help = 0;
1015 const unsigned int nb_fields_to_parse =
1016 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1019 unsigned int nb_search_conditions;
1020 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1021 struct alias_node *a, *b;
1023 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1025 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1029 if(getenv("MYMAILRC")) {
1030 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1031 } else if(getenv("HOME")) {
1032 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1034 rc_filename[0] = '\0';
1037 global_alias_list = 0;
1039 global_use_leading_time = 0;
1040 global_nb_mails_max = 250;
1042 default_search_field = 0;
1043 strncpy(output_filename, "", PATH_MAX);
1045 if(rc_filename[0]) {
1046 read_rc_file(rc_filename);
1051 #warning Test code added on 2013 May 02 11:17:01
1052 struct alias_node *a;
1053 for(a = global_alias_list; a; a = a->next) {
1054 printf ("ALIAS [%s] [%s]\n", a->alias, a->value);
1059 setlocale(LC_ALL, "");
1061 nb_search_conditions = 0;
1063 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
1064 long_options, NULL)) != -1) {
1073 print_version(stdout);
1081 global_use_leading_time = 1;
1090 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1093 db_filename = strdup(optarg);
1097 if(db_filename_regexp_string) {
1098 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1101 db_filename_regexp_string = strdup(optarg);
1105 if(mbox_filename_regexp_string) {
1106 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1109 mbox_filename_regexp_string = strdup(optarg);
1113 strncpy(output_filename, optarg, PATH_MAX);
1118 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1121 db_root_path = strdup(optarg);
1125 if(db_filename_list) {
1126 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1129 db_filename_list = strdup(optarg);
1133 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1134 fprintf(stderr, "mymail: Too many search patterns.\n");
1137 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1138 nb_search_conditions++;
1142 default_search_field = optarg;
1146 global_nb_mails_max = atoi(optarg);
1155 /* Set all the values that may defined in the arguments, through
1156 environment variables, or hard-coded */
1158 db_filename = default_value(db_filename,
1162 db_filename_regexp_string = default_value(db_filename_regexp_string,
1166 db_root_path = default_value(db_root_path,
1170 db_filename_list = default_value(db_filename_list,
1174 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1175 "MYMAIL_MBOX_PATTERN",
1178 /* Start the processing */
1181 print_usage(stderr);
1186 print_usage(stdout);
1194 regex_t mbox_filename_regexp_static;
1195 regex_t *mbox_filename_regexp;
1197 if(mbox_filename_regexp_string) {
1198 if(regcomp(&mbox_filename_regexp_static,
1199 mbox_filename_regexp_string,
1202 "mymail: Syntax error in regexp \"%s\".\n",
1203 mbox_filename_regexp_string);
1206 mbox_filename_regexp = &mbox_filename_regexp_static;
1208 mbox_filename_regexp = 0;
1211 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1213 for(f = 0; f < nb_fields_to_parse; f++) {
1214 if(regcomp(&fields_to_parse[f].regexp,
1215 fields_to_parse[f].regexp_string,
1216 fields_to_parse[f].cflags)) {
1218 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1219 fields_to_parse[f].regexp_string,
1220 field_keys[fields_to_parse[f].id]);
1226 "%s version_%s format_%d raw\n",
1227 MYMAIL_DB_MAGIC_TOKEN,
1229 MYMAIL_DB_FORMAT_VERSION);
1231 while(optind < argc) {
1232 recursive_index_mbox(db_file,
1233 argv[optind], mbox_filename_regexp,
1234 nb_fields_to_parse, fields_to_parse);
1241 if(mbox_filename_regexp) {
1242 regfree(mbox_filename_regexp);
1245 for(f = 0; f < nb_fields_to_parse; f++) {
1246 regfree(&fields_to_parse[f].regexp);
1255 int nb_extracted_mails = 0;
1257 if(output_filename[0]) {
1258 output_file = safe_fopen(output_filename, "w", "result mbox");
1260 output_file = stdout;
1264 if(nb_search_conditions > 0) {
1266 /* Recursive search if db_root_path is set */
1269 regex_t db_filename_regexp;
1270 if(regcomp(&db_filename_regexp,
1271 db_filename_regexp_string,
1274 "mymail: Syntax error in regexp \"%s\".\n",
1275 db_filename_regexp_string);
1279 nb_extracted_mails = recursive_search_in_db(db_root_path, &db_filename_regexp,
1281 nb_search_conditions, search_conditions,
1284 regfree(&db_filename_regexp);
1287 /* Search in all db files listed in db_filename_list */
1289 if(db_filename_list) {
1290 char db_filename[PATH_MAX + 1];
1293 s = db_filename_list;
1296 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1298 if(db_filename[0]) {
1299 nb_extracted_mails =
1300 search_in_db(db_filename,
1302 nb_search_conditions, search_conditions, output_file);
1307 /* Search in all db files listed in the command arguments */
1309 while(optind < argc) {
1310 nb_extracted_mails =
1311 search_in_db(argv[optind],
1313 nb_search_conditions, search_conditions, output_file);
1319 if(nb_extracted_mails > 0) {
1320 printf("Found %d matching mails.\n", nb_extracted_mails);
1322 printf("No matching mail found.\n");
1326 fflush(output_file);
1328 if(output_file != stdout) {
1329 fclose(output_file);
1333 for(n = 0; n < nb_search_conditions; n++) {
1334 free_condition(&search_conditions[n]);
1337 a = global_alias_list;
1347 free(db_filename_regexp_string);
1349 free(db_filename_list);
1350 free(mbox_filename_regexp_string);
1352 regfree(&global_leading_from_line_regexp);