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.
30 It is low-tech, simple, light and fast.
48 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
49 #define VERSION "0.9.1"
51 #define MAX_NB_SEARCH_CONDITIONS 10
53 #define BUFFER_SIZE 65536
54 #define TOKEN_BUFFER_SIZE 1024
56 regex_t leading_from_line_regexp;
59 char *db_filename_regexp_string;
61 char *db_filename_list;
62 char output_filename[PATH_MAX + 1];
70 /********************************************************************/
85 static char *field_names[] = {
97 /********************************************************************/
99 struct search_condition {
103 time_t interval_start, interval_stop;
106 /********************************************************************/
108 struct parsable_field {
114 static struct parsable_field fields_to_parse[] = {
118 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
123 "^\\([Ff][Rr][Oo][Mm]:\\|[Rr][Ee][Pp][Ll][Yy]-[Tt][Oo]:\\|[Ss][Ee][Nn][Dd][Ee][Rr]:\\)",
124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
129 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
130 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
135 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
136 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
141 "^[Dd][Aa][Tt][Ee]: ",
142 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
147 /********************************************************************/
149 int xor(int a, int b) {
150 return (a && !b) || (!a && b);
153 char *parse_token(char *token_buffer, size_t token_buffer_size,
154 char separator, char *string) {
155 char *u = token_buffer;
156 while(u < token_buffer + token_buffer_size - 1 && *string &&
157 *string != separator) {
158 *(u++) = *(string++);
160 while(*string == separator) string++;
165 void remove_eof(char *c) {
166 while(*c && *c != '\n' && *c != '\r') c++;
170 /********************************************************************/
172 /* malloc with error checking. */
174 void *safe_malloc(size_t n) {
178 "mymail: cannot allocate memory: %s\n", strerror(errno));
184 /*********************************************************************/
186 void print_version(FILE *out) {
187 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
190 void print_usage(FILE *out) {
192 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
194 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
196 fprintf(out, " -h, --help\n");
197 fprintf(out, " show this help\n");
198 fprintf(out, " -v, --version\n");
199 fprintf(out, " print the version number\n");
200 fprintf(out, " -q, --quiet\n");
201 fprintf(out, " do not print information during search\n");
202 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
203 fprintf(out, " set the db filename pattern for recursive search\n");
204 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
205 fprintf(out, " set the db root path for recursive search\n");
206 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
207 fprintf(out, " set the semicolon-separated list of db files for search\n");
208 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
209 fprintf(out, " search for matching mails in the db file\n");
210 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
211 fprintf(out, " set the db filename for indexing\n");
212 fprintf(out, " -i, --index\n");
213 fprintf(out, " index mails\n");
214 fprintf(out, " -o <output filename>, --output <output filename>\n");
215 fprintf(out, " set the result file, use stdout if unset\n");
218 /*********************************************************************/
220 int ignore_entry(const char *name) {
222 /* strcmp(name, ".") == 0 || */
223 /* strcmp(name, "..") == 0 || */
224 (name[0] == '.' && name[1] != '/');
227 int is_a_leading_from_line(char *s) {
228 return strncmp(s, "From ", 5) == 0 &&
229 regexec(&leading_from_line_regexp, s, 0, 0, 0) == 0;
232 int mbox_line_match_search(struct search_condition *condition,
233 int mbox_id, char *mbox_value) {
235 if(condition->field_id == ID_INTERVAL) {
236 if(mbox_id == ID_LEADING_LINE) {
242 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
243 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
246 return (t >= condition->interval_start &&
247 (condition->interval_stop == 0 ||
248 t <= condition->interval_stop));
256 (condition->field_id == mbox_id)
260 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
261 mbox_id == ID_FROM ||
265 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
269 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
273 void search_in_db(const char *db_filename,
274 int nb_search_conditions,
275 struct search_condition *search_conditions,
278 int hits[MAX_NB_SEARCH_CONDITIONS];
279 char raw_db_line[BUFFER_SIZE];
280 char raw_mbox_line[BUFFER_SIZE];
281 char current_mail_filename[PATH_MAX + 1];
282 unsigned long int current_position_in_mail;
283 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
285 int already_written, m, n;
286 int last_mbox_line_was_empty;
287 int nb_body_conditions, nb_fulfilled_body_conditions;
291 printf("Searching in '%s' ... ", db_filename);
295 db_file = fopen(db_filename, "r");
299 "mymail: Cannot open \"%s\" for reading: %s\n",
305 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
306 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
308 "mymail: Header line in '%s' does not match the mymail db format.\n",
314 "mymail: Cannot read the header line in '%s'.\n",
319 current_position_in_mail = 0;
322 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
324 nb_body_conditions = 0;
325 for(n = 0; n < nb_search_conditions; n++) {
326 if(search_conditions[n].field_id == ID_BODY) {
327 nb_body_conditions++;
331 strcpy(current_mail_filename, "");
333 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
334 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
336 if(strcmp("mail", mbox_name) == 0) {
337 char position_in_file_string[TOKEN_BUFFER_SIZE];
339 if(current_mail_filename[0]) {
341 /* We first check all conditions but the body ones */
343 for(n = 0; n < nb_search_conditions &&
344 ((search_conditions[n].field_id == ID_BODY) ||
345 xor(hits[n], search_conditions[n].negation)); n++);
347 if(n == nb_search_conditions) {
349 /* all conditions but the body ones are fine, check the body
352 nb_fulfilled_body_conditions = 0;
354 if(nb_body_conditions > 0) {
359 mail_file = fopen(current_mail_filename, "r");
363 "mymail: Cannot open mbox '%s' for body scan.\n",
364 current_mail_filename);
368 fseek(mail_file, current_position_in_mail, SEEK_SET);
370 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
371 while(nb_fulfilled_body_conditions < nb_body_conditions) {
372 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
374 if(last_mbox_line_was_empty) { header = 0; }
377 for(n = 0; n < nb_search_conditions; n++) {
378 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
380 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
382 nb_fulfilled_body_conditions++;
388 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
389 (last_mbox_line_was_empty &&
390 is_a_leading_from_line(raw_mbox_line)))
398 if(nb_body_conditions == nb_fulfilled_body_conditions) {
401 mail_file = fopen(current_mail_filename, "r");
405 "mymail: Cannot open mbox '%s' for mail extraction.\n",
406 current_mail_filename);
410 fseek(mail_file, current_position_in_mail, SEEK_SET);
412 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
413 last_mbox_line_was_empty = 0;
414 fprintf(output_file, "%s", raw_mbox_line);
416 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
417 (last_mbox_line_was_empty &&
418 is_a_leading_from_line(raw_mbox_line))
421 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
422 fprintf(output_file, "%s", raw_mbox_line);
431 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
433 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
434 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
435 current_position_in_mail = atol(position_in_file_string);
436 remove_eof(current_mail_filename);
442 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
443 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
447 for(n = 0; n < nb_search_conditions; n++) {
448 hits[n] |= mbox_line_match_search(&search_conditions[n],
449 mbox_id, mbox_value);
462 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
463 int nb_search_conditions,
464 struct search_condition *search_conditions,
467 struct dirent *dir_e;
469 char subname[PATH_MAX + 1];
471 if(lstat(entry_name, &sb) != 0) {
473 "mymail: Cannot stat \"%s\": %s\n",
479 dir = opendir(entry_name);
482 while((dir_e = readdir(dir))) {
483 if(!ignore_entry(dir_e->d_name)) {
484 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
485 recursive_search_in_db(subname, db_filename_regexp,
486 nb_search_conditions, search_conditions,
494 const char *s = entry_name, *filename = entry_name;
495 while(*s) { if(*s == '/') { filename = s+1; } s++; }
497 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
498 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
503 /*********************************************************************/
505 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
506 char *raw_mbox_line, FILE *db_file) {
509 for(f = 0; f < nb_fields_to_parse; f++) {
510 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
511 fprintf(db_file, "%s %s\n",
512 field_names[fields_to_parse[f].id],
513 raw_mbox_line + matches.rm_eo);
518 void index_mbox(const char *mbox_filename,
519 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
521 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
522 char *end_of_full_line;
524 int in_header, new_header, last_mbox_line_was_empty;
525 unsigned long int position_in_file;
527 file = fopen(mbox_filename, "r");
530 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
531 if(paranoid) { exit(EXIT_FAILURE); }
538 position_in_file = 0;
539 end_of_full_line = 0;
541 last_mbox_line_was_empty = 1;
543 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
544 if(last_mbox_line_was_empty &&
545 is_a_leading_from_line(raw_mbox_line)) {
548 "Got a ^\"From \" in the header in %s:%lu.\n",
549 mbox_filename, position_in_file);
550 fprintf(stderr, "%s", raw_mbox_line);
551 if(paranoid) { exit(EXIT_FAILURE); }
555 } else if(raw_mbox_line[0] == '\n') {
556 if(in_header) { in_header = 0; }
559 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
563 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
567 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
568 char *start = raw_mbox_line;
569 while(*start == ' ' || *start == '\t') start++;
570 *(end_of_full_line++) = ' ';
571 strcpy(end_of_full_line, start);
572 while(*end_of_full_line && *end_of_full_line != '\n') {
575 *end_of_full_line = '\0';
580 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
581 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
583 "Header line syntax error %s:%lu.\n",
584 mbox_filename, position_in_file);
585 fprintf(stderr, "%s", raw_mbox_line);
590 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
593 end_of_full_line = full_line;
594 strcpy(end_of_full_line, raw_mbox_line);
595 while(*end_of_full_line && *end_of_full_line != '\n') {
598 *end_of_full_line = '\0';
603 position_in_file += strlen(raw_mbox_line);
609 void recursive_index_mbox(FILE *db_file,
610 const char *entry_name,
611 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
613 struct dirent *dir_e;
615 char subname[PATH_MAX + 1];
617 if(lstat(entry_name, &sb) != 0) {
619 "mymail: Cannot stat \"%s\": %s\n",
625 dir = opendir(entry_name);
628 while((dir_e = readdir(dir))) {
629 if(!ignore_entry(dir_e->d_name)) {
630 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
631 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
636 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
640 /*********************************************************************/
642 /* For long options that have no equivalent short option, use a
643 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
645 OPT_BASH_MODE = CHAR_MAX + 1
648 static struct option long_options[] = {
649 { "help", no_argument, 0, 'h' },
650 { "version", no_argument, 0, 'v' },
651 { "quiet", no_argument, 0, 'q' },
652 { "db-file", 1, 0, 'd' },
653 { "db-pattern", 1, 0, 'p' },
654 { "db-root", 1, 0, 'r' },
655 { "db-list", 1, 0, 'l' },
656 { "search", 1, 0, 's' },
657 { "index", 0, 0, 'i' },
658 { "output", 1, 0, 'o' },
662 /*********************************************************************/
664 void init_condition(struct search_condition *condition, char *string) {
665 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
668 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', string);
669 search_field = full_search_field;
671 if(search_field[0] == '!') {
673 condition->negation = 1;
675 condition->negation = 0;
678 if(strcmp(search_field, "6h") == 0) {
679 condition->field_id = ID_INTERVAL;
680 condition->interval_start = time(0) - 3600 * 6;
681 condition->interval_stop = 0;
684 else if(strcmp(search_field, "24h") == 0 ||
685 strcmp(search_field, "today") == 0) {
686 condition->field_id = ID_INTERVAL;
687 condition->interval_start = time(0) - 3600 * 24;
688 condition->interval_stop = 0;
691 else if(strcmp(search_field, "yesterday") == 0) {
692 condition->field_id = ID_INTERVAL;
693 condition->interval_start = time(0) - 2 * 3600 * 24;
694 condition->interval_stop = time(0) - 3600 * 24;
698 condition->field_id = -1;
700 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
701 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
702 condition->field_id = m;
706 if(condition->field_id == -1) {
708 "mymail: Syntax error in field name \"%s\".\n",
713 if(regcomp(&condition->regexp,
717 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
719 field_names[condition->field_id]);
725 void free_condition(struct search_condition *condition) {
726 if(condition->field_id != ID_INTERVAL) {
727 regfree(&condition->regexp);
731 /*********************************************************************/
732 /*********************************************************************/
733 /*********************************************************************/
735 int main(int argc, char **argv) {
736 int error = 0, show_help = 0;
737 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
740 int nb_search_conditions;
742 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
744 if(regcomp(&leading_from_line_regexp,
745 "^From [^ ]*@[^ ]* \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\) \\(Jan\\|Feb\\|Mar\\|Apr\\|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$",
748 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
756 db_filename_list = 0;
759 setlocale(LC_ALL, "");
761 nb_search_conditions = 0;
763 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:",
764 long_options, NULL)) != -1) {
773 print_version(stdout);
785 db_filename = strdup(optarg);
789 db_filename_regexp_string = strdup(optarg);
793 strncpy(output_filename, optarg, PATH_MAX);
797 db_root_path = strdup(optarg);
801 db_filename_list = strdup(optarg);
805 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
806 fprintf(stderr, "mymail: Too many search patterns.\n");
809 init_condition(&search_conditions[nb_search_conditions], optarg);
810 nb_search_conditions++;
820 char *default_db_filename = getenv("MYMAIL_DB_FILE");
822 if(!default_db_filename) {
823 default_db_filename = "mymail.db";
826 db_filename = strdup(default_db_filename);
829 if(!db_filename_regexp_string) {
830 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
832 if(!default_db_filename_regexp_string) {
833 default_db_filename_regexp_string = "^mymail.db$";
836 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
840 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
842 if(default_db_root_path) {
843 db_root_path = strdup(default_db_root_path);
847 if(!db_filename_list) {
848 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
850 if(default_db_filename_list) {
851 db_filename_list = strdup(default_db_filename_list);
855 if(output_filename[0]) {
856 output_file = fopen(output_filename, "w");
860 "mymail: Cannot open result file \"%s\" for writing: %s\n",
866 output_file = stdout;
883 db_file = fopen(db_filename, "w");
887 "mymail: Cannot open \"%s\" for writing: %s\n",
893 for(f = 0; f < nb_fields_to_parse; f++) {
894 if(regcomp(&fields_to_parse[f].regexp,
895 fields_to_parse[f].regexp_string,
898 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
899 fields_to_parse[f].regexp_string,
900 field_names[fields_to_parse[f].id]);
905 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
907 while(optind < argc) {
908 recursive_index_mbox(db_file,
910 nb_fields_to_parse, fields_to_parse);
917 for(f = 0; f < nb_fields_to_parse; f++) {
918 regfree(&fields_to_parse[f].regexp);
924 if(nb_search_conditions > 0) {
926 /* Recursive search if db_root_path is set */
929 regex_t db_filename_regexp;
930 if(regcomp(&db_filename_regexp,
931 db_filename_regexp_string,
934 "mymail: Syntax error in regexp \"%s\".\n",
935 db_filename_regexp_string);
939 recursive_search_in_db(db_root_path, &db_filename_regexp,
940 nb_search_conditions, search_conditions,
943 regfree(&db_filename_regexp);
946 /* Search in all db files listed in db_filename_list */
948 if(db_filename_list) {
949 char db_filename[PATH_MAX + 1];
952 s = db_filename_list;
955 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
958 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
963 /* Search in all db files listed in the command arguments */
965 while(optind < argc) {
966 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
972 for(n = 0; n < nb_search_conditions; n++) {
973 free_condition(&search_conditions[n]);
976 if(output_file != stdout) {
982 free(db_filename_regexp_string);
984 free(db_filename_list);
986 regfree(&leading_from_line_regexp);