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 VERSION "0.9.2"
51 #define MAX_NB_SEARCH_CONDITIONS 32
53 #define BUFFER_SIZE 65536
54 #define TOKEN_BUFFER_SIZE 1024
56 #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\\) [ 123][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]\n$"
58 regex_t leading_from_line_regexp;
60 /* Global variables! */
66 /********************************************************************/
81 static char *field_names[] = {
93 /********************************************************************/
95 struct search_condition {
99 time_t interval_start, interval_stop;
102 /********************************************************************/
104 struct parsable_field {
111 static struct parsable_field fields_to_parse[] = {
116 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
122 "^\\(from\\|reply-to\\|sender\\|return-path\\): ",
123 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
129 "^\\(to\\|cc\\|bcc\\): ",
130 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
137 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
144 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
149 /********************************************************************/
151 int xor(int a, int b) {
152 return (a && !b) || (!a && b);
155 const char *parse_token(char *token_buffer, size_t token_buffer_size,
156 char separator, const char *string) {
157 char *u = token_buffer;
158 while(u < token_buffer + token_buffer_size - 1 && *string &&
159 *string != separator) {
160 *(u++) = *(string++);
162 while(*string == separator) string++;
167 char *default_value(char *current_value,
168 const char *env_variable,
169 const char *hard_default_value) {
171 return current_value;
173 char *env_value = getenv(env_variable);
175 return strdup(env_value);
176 } else if(hard_default_value) {
177 return strdup(hard_default_value);
184 FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
185 FILE *result = fopen(path, mode);
186 /* printf("Opening '%s' with mode \"%s\"\n", path, mode); */
191 "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
192 path, comment, mode);
197 /*********************************************************************/
199 void print_version(FILE *out) {
200 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
203 void print_usage(FILE *out) {
205 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
207 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
209 fprintf(out, " -h, --help\n");
210 fprintf(out, " show this help\n");
211 fprintf(out, " -v, --version\n");
212 fprintf(out, " print the version number\n");
213 fprintf(out, " -q, --quiet\n");
214 fprintf(out, " do not print information during search\n");
215 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
216 fprintf(out, " set the db filename pattern for recursive search\n");
217 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
218 fprintf(out, " set the db root path for recursive search\n");
219 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
220 fprintf(out, " set the semicolon-separated list of db files for search\n");
221 fprintf(out, " -m <mbox filename pattern>, --mbox-pattern <mbox filename pattern>\n");
222 fprintf(out, " set the mbox filename pattern for recursive search\n");
223 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
224 fprintf(out, " search for matching mails in the db file\n");
225 fprintf(out, " -d <db filename>, --db-file-generate <db filename>\n");
226 fprintf(out, " set the db filename for indexing\n");
227 fprintf(out, " -i, --index\n");
228 fprintf(out, " index mails\n");
229 fprintf(out, " -o <output filename>, --output <output filename>\n");
230 fprintf(out, " set the result file, use stdout if unset\n");
231 fprintf(out, " -a <search field>, --default-search <search field>\n");
232 fprintf(out, " set the default search field\n");
235 /*********************************************************************/
237 time_t time_for_past_day(int day) {
243 delta_day = (7 + tm->tm_wday - day) % 7 + 1;
244 return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
247 /*********************************************************************/
249 int ignore_entry(const char *name) {
251 strcmp(name, ".") == 0 ||
252 strcmp(name, "..") == 0 ||
253 (ignore_dot_files && name[0] == '.' && name[1] != '/');
256 int is_a_leading_from_line(char *mbox_line) {
258 strncmp(mbox_line, "From ", 5) == 0 &&
259 regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
262 int mbox_line_match_search(struct search_condition *condition,
263 int mbox_id, const char *mbox_value) {
265 if(condition->field_id == ID_INTERVAL) {
266 if(mbox_id == ID_LEADING_LINE) {
272 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
273 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
276 return (t >= condition->interval_start &&
277 (condition->interval_stop == 0 ||
278 t <= condition->interval_stop));
286 (condition->field_id == mbox_id)
290 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
291 mbox_id == ID_FROM ||
295 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
301 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
305 void update_body_hits(char *mail_filename, int position_in_mail,
306 int nb_search_conditions, struct search_condition *search_conditions,
307 int nb_body_conditions,
311 char raw_mbox_line[BUFFER_SIZE];
317 mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
319 fseek(mail_file, position_in_mail, SEEK_SET);
321 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
322 while(nb_body_hits < nb_body_conditions) {
323 /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
324 /* if(last_mbox_line_was_empty) { header = 0; } */
326 if(raw_mbox_line[0] == '\n') { header = 0; }
329 for(n = 0; n < nb_search_conditions; n++) {
330 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
332 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
340 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
341 (is_a_leading_from_line(raw_mbox_line)))
349 void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
351 char raw_mbox_line[BUFFER_SIZE];
354 mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
355 fseek(mail_file, position_in_mail, SEEK_SET);
357 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
358 fprintf(output_file, "%s", raw_mbox_line);
360 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
361 (is_a_leading_from_line(raw_mbox_line))
364 fprintf(output_file, "%s", raw_mbox_line);
371 int search_in_db(const char *db_filename,
372 int nb_search_conditions,
373 struct search_condition *search_conditions,
376 int hits[MAX_NB_SEARCH_CONDITIONS];
377 char raw_db_line[BUFFER_SIZE];
378 char current_mail_filename[PATH_MAX + 1];
379 unsigned long int current_position_in_mail;
380 char mbox_name[TOKEN_BUFFER_SIZE];
381 const char *mbox_value;
383 int already_written, m, n;
384 int nb_body_conditions, nb_fulfilled_body_conditions;
386 int nb_extracted_mails;
388 nb_extracted_mails = 0;
391 printf("Searching in '%s' ... ", db_filename);
395 db_file = safe_fopen(db_filename, "r", "index file for search");
397 /* First, check the db file leading line integrity */
399 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
400 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
402 "mymail: Header line in '%s' does not match the mymail db format.\n",
408 "mymail: Cannot read the header line in '%s'.\n",
413 /* Then parse the said db file */
415 current_position_in_mail = 0;
418 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
420 nb_body_conditions = 0;
421 for(n = 0; n < nb_search_conditions; n++) {
422 if(search_conditions[n].field_id == ID_BODY) {
423 nb_body_conditions++;
427 strcpy(current_mail_filename, "");
429 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
430 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
432 if(strcmp("mail", mbox_name) == 0) {
433 char position_in_file_string[TOKEN_BUFFER_SIZE];
435 if(current_mail_filename[0]) {
437 /* We first check all conditions but the body ones */
439 for(n = 0; n < nb_search_conditions &&
440 ((search_conditions[n].field_id == ID_BODY) ||
441 xor(hits[n], search_conditions[n].negation)); n++);
443 if(n == nb_search_conditions) {
445 /* Now check the body ones */
447 if(nb_body_conditions > 0) {
448 update_body_hits(current_mail_filename, current_position_in_mail,
449 nb_search_conditions, search_conditions,
454 nb_fulfilled_body_conditions = 0;
456 for(n = 0; n < nb_search_conditions; n++) {
457 if(search_conditions[n].field_id == ID_BODY &&
458 xor(hits[n], search_conditions[n].negation)) {
459 nb_fulfilled_body_conditions++;
463 if(nb_body_conditions == nb_fulfilled_body_conditions) {
464 nb_extracted_mails++;
465 extract_mail(current_mail_filename, current_position_in_mail, output_file);
470 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
472 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
473 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
474 current_position_in_mail = atol(position_in_file_string);
480 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
481 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
485 for(n = 0; n < nb_search_conditions; n++) {
486 hits[n] |= mbox_line_match_search(&search_conditions[n],
487 mbox_id, mbox_value);
499 return nb_extracted_mails;
502 int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
503 int nb_search_conditions,
504 struct search_condition *search_conditions,
507 struct dirent *dir_e;
509 char subname[PATH_MAX + 1];
510 int nb_extracted_mails = 0;
512 if(lstat(entry_name, &sb) != 0) {
514 "mymail: Cannot stat \"%s\": %s\n",
520 /* printf("recursive_search_in_db %s\n", entry_name); */
522 dir = opendir(entry_name);
525 while((dir_e = readdir(dir))) {
526 if(!ignore_entry(dir_e->d_name)) {
527 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
528 nb_extracted_mails += recursive_search_in_db(subname, db_filename_regexp,
529 nb_search_conditions, search_conditions,
537 const char *s = entry_name, *filename = entry_name;
538 while(*s) { if(*s == '/') { filename = s+1; } s++; }
540 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
541 nb_extracted_mails +=
542 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
546 return nb_extracted_mails;
549 /*********************************************************************/
551 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
552 char *raw_mbox_line, FILE *db_file) {
555 for(f = 0; f < nb_fields_to_parse; f++) {
556 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
557 fprintf(db_file, "%s %s\n",
558 field_names[fields_to_parse[f].id],
559 raw_mbox_line + matches.rm_eo);
564 void index_mbox(const char *mbox_filename,
565 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
567 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
568 char *end_of_full_line;
570 int in_header, new_header;
571 unsigned long int position_in_file;
573 file = safe_fopen(mbox_filename, "r", "mbox for indexing");
578 position_in_file = 0;
579 end_of_full_line = 0;
582 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
583 if(is_a_leading_from_line(raw_mbox_line)) {
586 "Got a ^\"From \" in the header in %s:%lu.\n",
587 mbox_filename, position_in_file);
588 fprintf(stderr, "%s", raw_mbox_line);
589 if(paranoid) { exit(EXIT_FAILURE); }
593 } else if(raw_mbox_line[0] == '\n') {
594 if(in_header) { in_header = 0; }
599 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
603 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
604 char *start = raw_mbox_line;
605 while(*start == ' ' || *start == '\t') start++;
606 *(end_of_full_line++) = ' ';
607 strcpy(end_of_full_line, start);
608 while(*end_of_full_line && *end_of_full_line != '\n') {
611 *end_of_full_line = '\0';
616 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
617 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
619 "Header line syntax error %s:%lu.\n",
620 mbox_filename, position_in_file);
621 fprintf(stderr, "%s", raw_mbox_line);
626 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
629 end_of_full_line = full_line;
630 strcpy(end_of_full_line, raw_mbox_line);
631 while(*end_of_full_line && *end_of_full_line != '\n') {
634 *end_of_full_line = '\0';
639 position_in_file += strlen(raw_mbox_line);
645 void recursive_index_mbox(FILE *db_file,
646 const char *entry_name, regex_t *mbox_filename_regexp,
647 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
649 struct dirent *dir_e;
651 char subname[PATH_MAX + 1];
653 if(lstat(entry_name, &sb) != 0) {
655 "mymail: Cannot stat \"%s\": %s\n",
661 dir = opendir(entry_name);
664 while((dir_e = readdir(dir))) {
665 if(!ignore_entry(dir_e->d_name)) {
666 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
667 recursive_index_mbox(db_file, subname, mbox_filename_regexp,
668 nb_fields_to_parse, fields_to_parse);
673 const char *s = entry_name, *filename = s;
674 while(*s) { if(*s == '/') { filename = s+1; }; s++; }
675 if(!mbox_filename_regexp || regexec(mbox_filename_regexp, filename, 0, 0, 0) == 0) {
676 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
681 /*********************************************************************/
683 /* For long options that have no equivalent short option, use a
684 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
686 OPT_BASH_MODE = CHAR_MAX + 1
689 static struct option long_options[] = {
690 { "help", no_argument, 0, 'h' },
691 { "version", no_argument, 0, 'v' },
692 { "quiet", no_argument, 0, 'q' },
693 { "db-file-generate", 1, 0, 'd' },
694 { "db-pattern", 1, 0, 'p' },
695 { "db-root", 1, 0, 'r' },
696 { "db-list", 1, 0, 'l' },
697 { "mbox-pattern", 1, 0, 'm' },
698 { "search", 1, 0, 's' },
699 { "index", 0, 0, 'i' },
700 { "output", 1, 0, 'o' },
701 { "default-search", 1, 0, 'a' },
705 /*********************************************************************/
707 void init_condition(struct search_condition *condition, const char *full_string,
708 const char *default_search_field) {
709 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
713 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
714 search_field = full_search_field;
716 if(search_field[0] == '!') {
718 condition->negation = 1;
720 condition->negation = 0;
725 if(strcmp(search_field, "8h") == 0) {
726 condition->field_id = ID_INTERVAL;
727 condition->interval_start = time(0) - 3600 * 8;
728 condition->interval_stop = 0;
731 else if(strcmp(search_field, "24h") == 0 ||
732 strcmp(search_field, "today") == 0) {
733 condition->field_id = ID_INTERVAL;
734 condition->interval_start = time(0) - 3600 * 24;
735 condition->interval_stop = 0;
738 else if(strcmp(search_field, "week") == 0) {
739 condition->field_id = ID_INTERVAL;
740 condition->interval_start = time(0) - 3600 * 24 * 7;
741 condition->interval_stop = 0;
744 else if(strcmp(search_field, "month") == 0) {
745 condition->field_id = ID_INTERVAL;
746 condition->interval_start = time(0) - 3600 * 24 * 31;
747 condition->interval_stop = 0;
750 else if(strcmp(search_field, "year") == 0) {
751 condition->field_id = ID_INTERVAL;
752 condition->interval_start = time(0) - 3600 * 24 * 365;
753 condition->interval_stop = 0;
758 else if(strcmp(search_field, "yesterday") == 0) {
759 condition->field_id = ID_INTERVAL;
760 condition->interval_start = time(0) - 2 * 3600 * 24;
761 condition->interval_stop = condition->interval_start + 3600 * 24;
766 else if(strcmp(search_field, "monday") == 0) {
767 condition->field_id = ID_INTERVAL;
768 condition->interval_start = time_for_past_day(1);
769 condition->interval_stop = condition->interval_start + 3600 * 24;
772 else if(strcmp(search_field, "tuesday") == 0) {
773 condition->field_id = ID_INTERVAL;
774 condition->interval_start = time_for_past_day(2);
775 condition->interval_stop = condition->interval_start + 3600 * 24;
778 else if(strcmp(search_field, "wednesday") == 0) {
779 condition->field_id = ID_INTERVAL;
780 condition->interval_start = time_for_past_day(3);
781 condition->interval_stop = condition->interval_start + 3600 * 24;
784 else if(strcmp(search_field, "thursday") == 0) {
785 condition->field_id = ID_INTERVAL;
786 condition->interval_start = time_for_past_day(4);
787 condition->interval_stop = condition->interval_start + 3600 * 24;
790 else if(strcmp(search_field, "friday") == 0) {
791 condition->field_id = ID_INTERVAL;
792 condition->interval_start = time_for_past_day(5);
793 condition->interval_stop = condition->interval_start + 3600 * 24;
796 else if(strcmp(search_field, "saturday") == 0) {
797 condition->field_id = ID_INTERVAL;
798 condition->interval_start = time_for_past_day(6);
799 condition->interval_stop = condition->interval_start + 3600 * 24;
802 else if(strcmp(search_field, "sunday") == 0) {
803 condition->field_id = ID_INTERVAL;
804 condition->interval_start = time_for_past_day(7);
805 condition->interval_stop = condition->interval_start + 3600 * 24;
810 /* header-related conditions */
812 condition->field_id = -1;
814 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
815 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
816 condition->field_id = m;
820 if(condition->field_id == -1) {
821 if(default_search_field) {
822 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
823 if(strncmp(field_names[m],
824 default_search_field, strlen(default_search_field)) == 0) {
825 condition->field_id = m;
828 string = full_string;
832 if(condition->field_id == -1) {
834 "mymail: Syntax error in field name \"%s\".\n",
839 if(regcomp(&condition->regexp,
843 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
845 field_names[condition->field_id]);
851 void free_condition(struct search_condition *condition) {
852 if(condition->field_id != ID_INTERVAL) {
853 regfree(&condition->regexp);
857 /*********************************************************************/
858 /*********************************************************************/
859 /*********************************************************************/
861 int main(int argc, char **argv) {
862 char *db_filename = 0;
863 char *db_filename_regexp_string = 0;
864 char *db_root_path = 0;
865 char *db_filename_list = 0;
866 char *mbox_filename_regexp_string = 0;
867 char *default_search_field;
868 char output_filename[PATH_MAX + 1];
869 int action_index = 0;
870 int error = 0, show_help = 0;
871 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
874 int nb_search_conditions;
875 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
877 if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP_STRING, 0)) {
879 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
885 default_search_field = 0;
886 ignore_dot_files = 1;
887 strncpy(output_filename, "", PATH_MAX);
889 setlocale(LC_ALL, "");
891 nb_search_conditions = 0;
893 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:m:",
894 long_options, NULL)) != -1) {
903 print_version(stdout);
916 fprintf(stderr, "mymail: Can not set the db filename twice.\n");
919 db_filename = strdup(optarg);
923 if(db_filename_regexp_string) {
924 fprintf(stderr, "mymail: Can not set the db filename pattern twice.\n");
927 db_filename_regexp_string = strdup(optarg);
931 if(mbox_filename_regexp_string) {
932 fprintf(stderr, "mymail: Can not set the mbox filename pattern twice.\n");
935 mbox_filename_regexp_string = strdup(optarg);
939 strncpy(output_filename, optarg, PATH_MAX);
944 fprintf(stderr, "mymail: Can not set the db root path twice.\n");
947 db_root_path = strdup(optarg);
951 if(db_filename_list) {
952 fprintf(stderr, "mymail: Can not set the db filename list twice.\n");
955 db_filename_list = strdup(optarg);
959 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
960 fprintf(stderr, "mymail: Too many search patterns.\n");
963 init_condition(&search_conditions[nb_search_conditions], optarg, default_search_field);
964 nb_search_conditions++;
968 default_search_field = optarg;
977 /* Set all the values that may defined in the arguments, through
978 environment variables, or hard-coded */
980 db_filename = default_value(db_filename,
984 db_filename_regexp_string = default_value(db_filename_regexp_string,
988 db_root_path = default_value(db_root_path,
992 db_filename_list = default_value(db_filename_list,
996 mbox_filename_regexp_string = default_value(mbox_filename_regexp_string,
997 "MYMAIL_MBOX_PATTERN",
1000 /* Start the processing */
1003 print_usage(stderr);
1008 print_usage(stdout);
1016 regex_t mbox_filename_regexp_static;
1017 regex_t *mbox_filename_regexp;
1019 if(mbox_filename_regexp_string) {
1020 if(regcomp(&mbox_filename_regexp_static,
1021 mbox_filename_regexp_string,
1024 "mymail: Syntax error in regexp \"%s\".\n",
1025 mbox_filename_regexp_string);
1028 mbox_filename_regexp = &mbox_filename_regexp_static;
1030 mbox_filename_regexp = 0;
1033 db_file = safe_fopen(db_filename, "w", "index file for indexing");
1035 for(f = 0; f < nb_fields_to_parse; f++) {
1036 if(regcomp(&fields_to_parse[f].regexp,
1037 fields_to_parse[f].regexp_string,
1038 fields_to_parse[f].cflags)) {
1040 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
1041 fields_to_parse[f].regexp_string,
1042 field_names[fields_to_parse[f].id]);
1047 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
1049 while(optind < argc) {
1050 recursive_index_mbox(db_file,
1051 argv[optind], mbox_filename_regexp,
1052 nb_fields_to_parse, fields_to_parse);
1059 if(mbox_filename_regexp) {
1060 regfree(mbox_filename_regexp);
1063 for(f = 0; f < nb_fields_to_parse; f++) {
1064 regfree(&fields_to_parse[f].regexp);
1073 int nb_extracted_mails = 0;
1075 if(output_filename[0]) {
1076 output_file = safe_fopen(output_filename, "w", "result mbox");
1078 output_file = stdout;
1082 if(nb_search_conditions > 0) {
1084 /* Recursive search if db_root_path is set */
1087 regex_t db_filename_regexp;
1088 if(regcomp(&db_filename_regexp,
1089 db_filename_regexp_string,
1092 "mymail: Syntax error in regexp \"%s\".\n",
1093 db_filename_regexp_string);
1097 nb_extracted_mails += recursive_search_in_db(db_root_path, &db_filename_regexp,
1098 nb_search_conditions, search_conditions,
1101 regfree(&db_filename_regexp);
1104 /* Search in all db files listed in db_filename_list */
1106 if(db_filename_list) {
1107 char db_filename[PATH_MAX + 1];
1110 s = db_filename_list;
1113 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
1115 if(db_filename[0]) {
1116 nb_extracted_mails +=
1117 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
1122 /* Search in all db files listed in the command arguments */
1124 while(optind < argc) {
1125 nb_extracted_mails +=
1126 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
1132 if(nb_extracted_mails > 0) {
1133 printf("Found %d matching mails.\n", nb_extracted_mails);
1135 printf("No matching mail found.\n");
1139 fflush(output_file);
1141 if(output_file != stdout) {
1142 fclose(output_file);
1146 for(n = 0; n < nb_search_conditions; n++) {
1147 free_condition(&search_conditions[n]);
1151 free(db_filename_regexp_string);
1153 free(db_filename_list);
1154 free(mbox_filename_regexp_string);
1156 regfree(&leading_from_line_regexp);