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 /********************************************************************/
94 static char *field_keys[] = {
110 /********************************************************************/
112 struct search_condition {
114 regex_t db_value_regexp;
116 time_t time_start, time_stop;
119 /********************************************************************/
121 struct parsable_field {
128 static struct parsable_field fields_to_parse[] = {
133 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
139 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
140 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
146 "^\\(to\\|cc\\|bcc\\): ",
147 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
154 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
161 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
169 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
175 "^\\(in-reply-to\\|references\\): ",
176 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
182 /********************************************************************/
184 int xor(int a, int b) {
185 return (a && !b) || (!a && b);
188 const char *parse_token(char *token_buffer, size_t token_buffer_size,
189 char separator, const char *string) {
190 char *u = token_buffer;
192 while(*string == separator) { string++; }
194 while(u < token_buffer + token_buffer_size - 1 && *string && *string != separator) {
195 *(u++) = *(string++);
198 while(*string == separator) { string++; }
204 char *default_value(char *current_value,
205 const char *env_variable,
206 const char *hard_default_value) {
208 return current_value;
210 char *env_value = getenv(env_variable);
212 return strdup(env_value);
213 } else if(hard_default_value) {
214 return strdup(hard_default_value);
221 /********************************************************************/
223 void *safe_malloc(size_t n) {
227 "mymail: cannot allocate memory: %s\n", strerror(errno));
233 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
234 FILE *result = fopen(path, mode);
239 "mymail: Cannot open file '%s' (%s) with mode \"%s\": %s\n",
246 /*********************************************************************/
248 void print_version(FILE *out) {
249 fprintf(out, "mymail version %s (%s)\n", MYMAIL_VERSION, UNAME);
252 void print_usage(FILE *out) {
254 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
256 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
258 fprintf(out, " -h, --help\n");
259 fprintf(out, " show this help\n");
260 fprintf(out, " -v, --version\n");
261 fprintf(out, " print the version number\n");
262 fprintf(out, " -q, --quiet\n");
263 fprintf(out, " do not print information during search\n");
264 fprintf(out, " -t, --use-leading-time\n");
265 fprintf(out, " use the time stamp from the leading line of each mail and not the Date:\n");
266 fprintf(out, " field\n");
267 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
268 fprintf(out, " set the db filename pattern for recursive search\n");
269 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
270 fprintf(out, " set the db root path for recursive search\n");
271 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
272 fprintf(out, " set the semicolon-separated list of db files for search\n");
273 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
274 fprintf(out, " set the mbox filename pattern for recursive search\n");
275 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
276 fprintf(out, " search for matching mails in the db file\n");
277 fprintf(out, " -d <db filename>, --db-file-output <db filename>\n");
278 fprintf(out, " set the db filename for indexing\n");
279 fprintf(out, " -i, --index\n");
280 fprintf(out, " index mails\n");
281 fprintf(out, " -o <output filename>, --output <output filename>\n");
282 fprintf(out, " set the result file, use stdout if unset\n");
283 fprintf(out, " -n <max number of mails>, --nb-mails-max <max number of mails>\n");
284 fprintf(out, " set the maximum number of mails to extract\n");
285 fprintf(out, " -a <search field>, --default-search <search field>\n");
286 fprintf(out, " set the default search field\n");
289 /*********************************************************************/
291 int ignore_entry(const char *name) {
293 strcmp(name, ".") == 0 ||
294 strcmp(name, "..") == 0 ||
295 (name[0] == '.' && name[1] != '/');
298 int is_a_leading_from_line(char *mbox_line) {
300 strncmp(mbox_line, "From ", 5) == 0 &&
301 regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
304 int db_line_match_search(struct search_condition *condition,
305 int db_key, const char *db_value) {
309 (condition->db_key == db_key)
313 (condition->db_key == ID_PARTICIPANT && (db_key == ID_LEADING_LINE ||
318 (condition->db_key == ID_FROM && db_key == ID_LEADING_LINE)
324 regexec(&condition->db_value_regexp, db_value, 0, 0, 0) == 0;
327 void update_body_hits(char *mail_filename, int position_in_mail,
328 int nb_search_conditions, struct search_condition *search_conditions,
329 int nb_body_conditions,
333 char raw_mbox_line[BUFFER_SIZE];
339 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
341 fseek(mail_file, position_in_mail, SEEK_SET);
343 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
344 while(nb_body_hits < nb_body_conditions) {
345 if(raw_mbox_line[0] == '\n') { header = 0; }
348 for(n = 0; n < nb_search_conditions; n++) {
349 if(search_conditions[n].db_key == ID_BODY && !hits[n]) {
351 (regexec(&search_conditions[n].db_value_regexp, raw_mbox_line, 0, 0, 0) == 0);
359 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
360 (is_a_leading_from_line(raw_mbox_line)))
368 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
370 char raw_mbox_line[BUFFER_SIZE];
373 /* printf("Extract\n"); */
375 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
376 fseek(mail_file, position_in_mail, SEEK_SET);
378 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
379 fprintf(output_file, "%s", raw_mbox_line);
381 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
382 is_a_leading_from_line(raw_mbox_line))
384 fprintf(output_file, "%s", raw_mbox_line);
391 int check_full_mail_match(char *current_mail_filename,
393 int nb_search_conditions,
394 struct search_condition *search_conditions,
395 int nb_body_conditions,
397 int current_position_in_mail) {
398 int n, nb_fulfilled_body_conditions;
400 for(n = 0; n < nb_search_conditions; n++) {
401 if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
402 hits[n] = (mail_time >= search_conditions[n].time_start &&
403 (search_conditions[n].time_stop == 0 ||
404 mail_time <= search_conditions[n].time_stop));
408 /* We first check all conditions but the body ones */
410 for(n = 0; n < nb_search_conditions &&
411 ((search_conditions[n].db_key == ID_BODY) ||
412 xor(hits[n], search_conditions[n].negation)); n++);
414 if(n == nb_search_conditions) {
416 /* Now check the body ones */
418 nb_fulfilled_body_conditions = 0;
420 if(nb_body_conditions > 0) {
421 update_body_hits(current_mail_filename, current_position_in_mail,
422 nb_search_conditions, search_conditions,
426 for(n = 0; n < nb_search_conditions; n++) {
427 if(search_conditions[n].db_key == ID_BODY &&
428 xor(hits[n], search_conditions[n].negation)) {
429 nb_fulfilled_body_conditions++;
433 return nb_body_conditions == nb_fulfilled_body_conditions;
439 /* We use the mail leading line time by default, and if we should and
440 can, we update with the Date: field */
442 void update_time(int db_key, const char *db_value, time_t *t) {
446 memset(&tm, 0, sizeof(struct tm));
448 if(db_key == ID_LEADING_LINE) {
450 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
451 /* printf("From %s", db_value); */
452 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
455 if(!global_use_leading_time) {
456 if(db_key == ID_DATE) {
457 if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
458 strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
459 /* printf("Date: %s", db_value); */
467 int search_in_db(const char *db_filename,
468 int nb_extracted_mails,
469 int nb_search_conditions,
470 struct search_condition *search_conditions,
474 char raw_db_line[BUFFER_SIZE];
475 char current_mail_filename[PATH_MAX + 1];
476 char db_key_string[TOKEN_BUFFER_SIZE];
477 char position_in_file_string[TOKEN_BUFFER_SIZE];
478 unsigned long int current_position_in_mail;
479 const char *db_value;
481 int hits[MAX_NB_SEARCH_CONDITIONS];
482 int nb_body_conditions, need_time;
488 printf("Searching in '%s' ... ", db_filename);
492 db_file = safe_fopen(db_filename, "r", "index file for search");
494 /* First, check the db file leading line integrity */
496 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
497 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
499 "mymail: Header line in '%s' does not match the mymail db format.\n",
505 "mymail: Cannot read the header line in '%s'.\n",
510 /* Then parse the said db file */
512 current_position_in_mail = 0;
514 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
516 nb_body_conditions = 0;
520 for(n = 0; n < nb_search_conditions; n++) {
521 if(search_conditions[n].db_key == ID_BODY) {
522 nb_body_conditions++;
524 else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
529 strcpy(current_mail_filename, "");
531 while(nb_extracted_mails < global_nb_mails_max &&
532 fgets(raw_db_line, BUFFER_SIZE, db_file)) {
535 char *s = raw_db_line;
536 while(*s && *s != '\n') { s++; }
539 db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
541 if(strcmp("mail", db_key_string) == 0) {
542 if(current_mail_filename[0]) {
543 if(check_full_mail_match(current_mail_filename,
545 nb_search_conditions, search_conditions,
546 nb_body_conditions, hits, current_position_in_mail)) {
547 extract_mail(current_mail_filename, current_position_in_mail, output_file);
548 nb_extracted_mails++;
552 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
553 db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
554 strncpy(current_mail_filename, db_value, PATH_MAX + 1);
555 current_position_in_mail = atol(position_in_file_string);
560 for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
561 if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
566 for(n = 0; n < nb_search_conditions; n++) {
567 hits[n] |= db_line_match_search(&search_conditions[n],
572 update_time(db_key, db_value, &mail_time);
577 if(nb_extracted_mails < global_nb_mails_max &&
578 current_mail_filename[0] &&
579 check_full_mail_match(current_mail_filename,
581 nb_search_conditions, search_conditions,
582 nb_body_conditions, hits, current_position_in_mail)) {
583 extract_mail(current_mail_filename, current_position_in_mail, output_file);
584 nb_extracted_mails++;
594 return nb_extracted_mails;
597 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
598 int nb_extracted_mails,
599 int nb_search_conditions,
600 struct search_condition *search_conditions,
603 struct dirent *dir_e;
605 char subname[PATH_MAX + 1];
607 if(lstat(entry_name, &sb) != 0) {
609 "mymail: Cannot stat \"%s\": %s\n",
615 /* printf("recursive_search_in_db %s\n", entry_name); */
617 dir = opendir(entry_name);
620 while((dir_e = readdir(dir)) &&
621 nb_extracted_mails < global_nb_mails_max) {
622 if(!ignore_entry(dir_e->d_name)) {
623 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
624 nb_extracted_mails = recursive_search_in_db(subname, db_filename_regexp,
626 nb_search_conditions, search_conditions,
634 const char *s = entry_name, *filename = entry_name;
635 while(*s) { if(*s == '/') { filename = s+1; } s++; }
637 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
639 search_in_db(entry_name,
641 nb_search_conditions, search_conditions, output_file);
645 return nb_extracted_mails;
648 /*********************************************************************/
650 void index_one_mbox_line(unsigned int nb_fields_to_parse,
651 struct parsable_field *fields_to_parse,
652 char *raw_mbox_line, FILE *db_file) {
655 for(f = 0; f < nb_fields_to_parse; f++) {
656 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
657 fprintf(db_file, "%s %s\n",
658 field_keys[fields_to_parse[f].id],
659 raw_mbox_line + matches.rm_eo);
664 void index_mbox(const char *mbox_filename,
665 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
667 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
668 char *end_of_full_line;
670 int in_header, new_header;
671 unsigned long int position_in_file;
673 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
678 position_in_file = 0;
679 end_of_full_line = 0;
682 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
683 if(is_a_leading_from_line(raw_mbox_line)) {
684 /* This starts a new mail */
687 "Got a ^\"From \" in the header in %s:%lu.\n",
688 mbox_filename, position_in_file);
689 fprintf(stderr, "%s", raw_mbox_line);
692 /* printf("LEADING_LINE %s", raw_mbox_line); */
696 } else if(raw_mbox_line[0] == '\n') {
699 /* We leave the header, index the current line */
701 /* printf("INDEX %s\n", full_line); */
702 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
704 end_of_full_line = full_line;
705 *end_of_full_line = '\0';
711 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
715 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
716 /* Continuation of a line */
717 char *start = raw_mbox_line;
718 while(*start == ' ' || *start == '\t') start++;
719 *(end_of_full_line++) = ' ';
720 strcpy(end_of_full_line, start);
721 while(*end_of_full_line && *end_of_full_line != '\n') {
724 *end_of_full_line = '\0';
728 /* Start a new header line, not a continuation */
731 /* printf("INDEX %s\n", full_line); */
732 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
735 end_of_full_line = full_line;
736 strcpy(end_of_full_line, raw_mbox_line);
737 while(*end_of_full_line && *end_of_full_line != '\n') {
740 *end_of_full_line = '\0';
745 position_in_file += strlen(raw_mbox_line);
751 void recursive_index_mbox(FILE *db_file,
752 const char *entry_name, regex_t *mbox_filename_regexp,
753 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
755 struct dirent *dir_e;
757 char subname[PATH_MAX + 1];
759 if(lstat(entry_name, &sb) != 0) {
761 "mymail: Cannot stat \"%s\": %s\n",
767 dir = opendir(entry_name);
770 while((dir_e = readdir(dir))) {
771 if(!ignore_entry(dir_e->d_name)) {
772 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
773 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
774 nb_fields_to_parse, fields_to_parse);
779 const char *s = entry_name, *filename = s;
780 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
781 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
782 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
787 /*********************************************************************/
789 /* For long options that have no equivalent short option, use a
790 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
792 OPT_BASH_MODE = CHAR_MAX + 1
795 static struct option long_options[] = {
796 { "help", no_argument, 0, 'h' },
797 { "version", no_argument, 0, 'v' },
798 { "quiet", no_argument, 0, 'q' },
799 { "use-leading-time", no_argument, 0, 't' },
800 { "db-file-output", 1, 0, 'd' },
801 { "db-pattern", 1, 0, 'p' },
802 { "db-root", 1, 0, 'r' },
803 { "db-list", 1, 0, 'l' },
804 { "mbox-pattern", 1, 0, 'm' },
805 { "search", 1, 0, 's' },
806 { "index", 0, 0, 'i' },
807 { "output", 1, 0, 'o' },
808 { "default-search", 1, 0, 'a' },
809 { "nb-mails-max", 1, 0, 'n' },
813 struct time_criterion {
816 int start_hour, end_hour;
820 /*********************************************************************/
822 static struct time_criterion time_criteria[] = {
824 { "8h", 0, 8, -1, -1 },
825 { "24h", 0, 24, -1, -1 },
826 { "48h", 0, 48, -1, -1 },
827 { "week", 0, 24 * 7, -1, -1 },
828 { "month", 0, 24 * 31, -1, -1 },
829 { "trimester", 0, 24 * 92, -1, -1 },
830 { "year", 0, 24 * 365, -1, -1 },
832 { "yesterday", 1, -1, -1, -1 },
833 { "today", 1, -1, -1, 0 },
835 { "monday", 1, -1, -1, 1 },
836 { "tuesday", 1, -1, -1, 2 },
837 { "wednesday", 1, -1, -1, 3 },
838 { "thursday", 1, -1, -1, 4 },
839 { "friday", 1, -1, -1, 5 },
840 { "saturday", 1, -1, -1, 6 },
841 { "sunday", 1, -1, -1, 7 },
845 /*********************************************************************/
847 time_t time_for_past_day(int day) {
854 delta_day = (7 + tm->tm_wday - day) % 7;
858 return t - (delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour);
861 void init_condition(struct search_condition *condition, const char *full_string,
862 const char *default_search_field) {
863 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
866 struct alias_node *a;
868 for(a = global_alias_list; a; a = a->next) {
869 if(strcmp(full_string, a->alias) == 0) {
870 full_string = a->value;
875 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
876 search_field = full_search_field;
878 if(search_field[0] == '!') {
880 condition->negation = 1;
882 condition->negation = 0;
885 condition->db_key = -1;
889 for(k = 0; k < sizeof(time_criteria) / sizeof(struct time_criterion); k++) {
890 if(strcmp(time_criteria[k].label, search_field) == 0) {
891 condition->db_key = ID_TIME_INTERVAL;
892 if(time_criteria[k].day_criterion) {
893 condition->time_start = time_for_past_day(time_criteria[k].past_week_day);
894 condition->time_stop = condition->time_start + 3600 * 24;
896 condition->time_start = time(0) - 3600 * time_criteria[k].start_hour;
897 if(time_criteria[k].end_hour >= 0) {
898 condition->time_stop = time(0) - 3600 * time_criteria[k].end_hour;
900 condition->time_stop = 0;
908 if(condition->db_key == -1) {
910 /* No time condition matched, look for the search fields */
912 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
913 if(strncmp(field_keys[m], search_field, strlen(search_field)) == 0) {
914 condition->db_key = m;
918 /* None match, if there is a default search field, re-run the search with it */
920 if(condition->db_key == -1) {
921 if(default_search_field) {
922 for(m = 0; (m < MAX_ID) && condition->db_key == -1; m++) {
923 if(strncmp(field_keys[m],
924 default_search_field, strlen(default_search_field)) == 0) {
925 condition->db_key = m;
928 string = full_string;
929 if(string[0] == '!') { string++; }
933 if(condition->db_key == -1) {
935 "mymail: Syntax error in field key \"%s\".\n",
940 if(regcomp(&condition->db_value_regexp,
944 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
946 field_keys[condition->db_key]);
952 void free_condition(struct search_condition *condition) {
953 if(condition->db_key != ID_TIME_INTERVAL) {
954 regfree(&condition->db_value_regexp);
958 const char *eat_space(const char *s) {
959 while(*s == ' ' || *s == '\t') { s++; }
963 void read_rc_file(const char *rc_filename) {
964 char raw_line[BUFFER_SIZE];
965 char command[TOKEN_BUFFER_SIZE], tmp_token[TOKEN_BUFFER_SIZE];
972 rc_file = fopen(rc_filename, "r");
976 while(fgets(raw_line, BUFFER_SIZE, rc_file)) {
978 while(*t) { if(*t == '\n') { *t = '\0'; }; t++; }
983 if(*s && *s != '#') {
984 s = parse_token(command, TOKEN_BUFFER_SIZE, ' ', s);
986 if(strcmp(command, "alias") == 0) {
987 struct alias_node *a = safe_malloc(sizeof(struct alias_node));
988 a->next = global_alias_list;
989 global_alias_list = a;
992 s = parse_token(tmp_token, TOKEN_BUFFER_SIZE, '=', s);
993 a->alias = strdup(tmp_token);
996 a->value = strdup(s);
998 fprintf(stderr, "%s:%d syntax error, missing alias value.\n",
1004 fprintf(stderr, "%s:%d syntax error, missing alias key.\n",
1010 fprintf(stderr, "%s:%d syntax error, unknown command '%s'.\n",
1024 /*********************************************************************/
1025 /*********************************************************************/
1026 /*********************************************************************/
1028 int main(int argc, char **argv) {
1029 char *db_filename = 0;
1030 char *db_filename_regexp_string = 0;
1031 char *db_root_path = 0;
1032 char *db_filename_list = 0;
1033 char *mbox_filename_regexp_string = 0;
1034 char *default_search_field;
1035 char output_filename[PATH_MAX + 1];
1036 char rc_filename[PATH_MAX + 1];
1037 int action_index = 0;
1038 int error = 0, show_help = 0;
1039 const unsigned int nb_fields_to_parse =
1040 sizeof(fields_to_parse) / sizeof(struct parsable_field);
1043 unsigned int nb_search_conditions;
1044 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
1045 struct alias_node *a, *b;
1047 if(regcomp(&global_leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
1049 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
1053 if(getenv("MYMAILRC")) {
1054 sprintf(rc_filename, "%s", getenv("MYMAILRC"));
1055 } else if(getenv("HOME")) {
1056 sprintf(rc_filename, "%s/.mymailrc", getenv("HOME"));
1058 rc_filename[0] = '\0';
1061 global_alias_list = 0;
1063 global_use_leading_time = 0;
1064 global_nb_mails_max = 250;
1066 default_search_field = 0;
1067 strncpy(output_filename, "", PATH_MAX);
1069 if(rc_filename[0]) {
1070 read_rc_file(rc_filename);
1075 #warning Test code added on 2013 May 02 11:17:01
1076 struct alias_node *a;
1077 for(a = global_alias_list; a; a = a->next) {
1078 printf ("ALIAS [%s] [%s]\n", a->alias, a->value);
1083 setlocale(LC_ALL, "");
1085 nb_search_conditions = 0;
1087 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
1088 long_options, NULL)) != -1) {
1097 print_version(stdout);
1105 global_use_leading_time = 1;
1114 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
1117 db_filename = strdup(optarg);
1121 if(db_filename_regexp_string) {
1122 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
1125 db_filename_regexp_string = strdup(optarg);
1129 if(mbox_filename_regexp_string) {
1130 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
1133 mbox_filename_regexp_string = strdup(optarg);
1137 strncpy(output_filename, optarg, PATH_MAX);
1142 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
1145 db_root_path = strdup(optarg);
1149 if(db_filename_list) {
1150 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
1153 db_filename_list = strdup(optarg);
1157 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
1158 fprintf(stderr, "mymail: Too many search patterns.\n");
1161 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
1162 nb_search_conditions++;
1166 default_search_field = optarg;
1170 global_nb_mails_max = atoi(optarg);
1180 print_usage(stderr);
1185 print_usage(stdout);
1189 /* Set all the values that may defined in the arguments, through
1190 environment variables, or hard-coded */
1192 db_filename = default_value(db_filename,
1196 db_filename_regexp_string = default_value(db_filename_regexp_string,
1200 db_root_path = default_value(db_root_path,
1204 db_filename_list = default_value(db_filename_list,
1208 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
1209 "MYMAIL_MBOX_PATTERN",
1216 regex_t mbox_filename_regexp_static;
1217 regex_t *mbox_filename_regexp;
1219 if(mbox_filename_regexp_string) {
1220 if(regcomp(&mbox_filename_regexp_static,
1221 mbox_filename_regexp_string,
1224 "mymail: Syntax error in regexp \"%s\".\n",
1225 mbox_filename_regexp_string);
1228 mbox_filename_regexp = &mbox_filename_regexp_static;
1230 mbox_filename_regexp = 0;
1233 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1235 for(f = 0; f < nb_fields_to_parse; f++) {
1236 if(regcomp(&fields_to_parse[f].regexp,
1237 fields_to_parse[f].regexp_string,
1238 fields_to_parse[f].cflags)) {
1240 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1241 fields_to_parse[f].regexp_string,
1242 field_keys[fields_to_parse[f].id]);
1248 "%s version_%s format_%d raw\n",
1249 MYMAIL_DB_MAGIC_TOKEN,
1251 MYMAIL_DB_FORMAT_VERSION);
1253 while(optind < argc) {
1254 recursive_index_mbox(db_file,
1255 argv[optind], mbox_filename_regexp,
1256 nb_fields_to_parse, fields_to_parse);
1263 if(mbox_filename_regexp) {
1264 regfree(mbox_filename_regexp);
1267 for(f = 0; f < nb_fields_to_parse; f++) {
1268 regfree(&fields_to_parse[f].regexp);
1277 int nb_extracted_mails = 0;
1279 if(output_filename[0]) {
1280 output_file = safe_fopen(output_filename, "w", "result mbox");
1282 output_file = stdout;
1286 if(nb_search_conditions > 0) {
1288 /* Recursive search if db_root_path is set */
1291 regex_t db_filename_regexp;
1292 if(regcomp(&db_filename_regexp,
1293 db_filename_regexp_string,
1296 "mymail: Syntax error in regexp \"%s\".\n",
1297 db_filename_regexp_string);
1301 nb_extracted_mails = recursive_search_in_db(db_root_path, &db_filename_regexp,
1303 nb_search_conditions, search_conditions,
1306 regfree(&db_filename_regexp);
1309 /* Search in all db files listed in db_filename_list */
1311 if(db_filename_list) {
1312 char db_filename[PATH_MAX + 1];
1315 s = db_filename_list;
1318 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1320 if(db_filename[0]) {
1321 nb_extracted_mails =
1322 search_in_db(db_filename,
1324 nb_search_conditions, search_conditions, output_file);
1329 /* Search in all db files listed in the command arguments */
1331 while(optind < argc) {
1332 nb_extracted_mails =
1333 search_in_db(argv[optind],
1335 nb_search_conditions, search_conditions, output_file);
1341 if(nb_extracted_mails > 0) {
1342 printf("Found %d matching mails.\n", nb_extracted_mails);
1344 printf("No matching mail found.\n");
1348 fflush(output_file);
1350 if(output_file != stdout) {
1351 fclose(output_file);
1355 for(n = 0; n < nb_search_conditions; n++) {
1356 free_condition(&search_conditions[n]);
1359 a = global_alias_list;
1369 free(db_filename_regexp_string);
1371 free(db_filename_list);
1372 free(mbox_filename_regexp_string);
1374 regfree(&global_leading_from_line_regexp);