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;
290 db_file = fopen(db_filename, "r");
294 "mymail: Cannot open \"%s\" for reading: %s\n",
300 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
301 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
303 "mymail: Header line in '%s' does not match the mymail db format.\n",
309 "mymail: Cannot read the header line in '%s'.\n",
314 current_position_in_mail = 0;
317 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
319 nb_body_conditions = 0;
320 for(n = 0; n < nb_search_conditions; n++) {
321 if(search_conditions[n].field_id == ID_BODY) {
322 nb_body_conditions++;
326 strcpy(current_mail_filename, "");
328 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
329 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
331 if(strcmp("mail", mbox_name) == 0) {
332 char position_in_file_string[TOKEN_BUFFER_SIZE];
334 if(current_mail_filename[0]) {
336 /* We first check all conditions but the body ones */
338 for(n = 0; n < nb_search_conditions &&
339 ((search_conditions[n].field_id == ID_BODY) ||
340 xor(hits[n], search_conditions[n].negation)); n++);
342 if(n == nb_search_conditions) {
344 /* all conditions but the body ones are fine, check the body
347 nb_fulfilled_body_conditions = 0;
349 if(nb_body_conditions > 0) {
354 mail_file = fopen(current_mail_filename, "r");
358 "mymail: Cannot open mbox '%s' for body scan.\n",
359 current_mail_filename);
363 fseek(mail_file, current_position_in_mail, SEEK_SET);
365 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
366 while(nb_fulfilled_body_conditions < nb_body_conditions) {
367 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
369 if(last_mbox_line_was_empty) { header = 0; }
372 for(n = 0; n < nb_search_conditions; n++) {
373 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
375 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
377 nb_fulfilled_body_conditions++;
383 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
384 (last_mbox_line_was_empty &&
385 is_a_leading_from_line(raw_mbox_line)))
393 if(nb_body_conditions == nb_fulfilled_body_conditions) {
396 mail_file = fopen(current_mail_filename, "r");
400 "mymail: Cannot open mbox '%s' for mail extraction.\n",
401 current_mail_filename);
405 fseek(mail_file, current_position_in_mail, SEEK_SET);
407 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
408 last_mbox_line_was_empty = 0;
409 fprintf(output_file, "%s", raw_mbox_line);
411 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
412 (last_mbox_line_was_empty &&
413 is_a_leading_from_line(raw_mbox_line))
416 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
417 fprintf(output_file, "%s", raw_mbox_line);
426 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
428 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
429 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
430 current_position_in_mail = atol(position_in_file_string);
431 remove_eof(current_mail_filename);
437 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
438 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
442 for(n = 0; n < nb_search_conditions; n++) {
443 hits[n] |= mbox_line_match_search(&search_conditions[n],
444 mbox_id, mbox_value);
452 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
453 int nb_search_conditions,
454 struct search_condition *search_conditions,
457 struct dirent *dir_e;
459 char subname[PATH_MAX + 1];
461 if(lstat(entry_name, &sb) != 0) {
463 "mymail: Cannot stat \"%s\": %s\n",
469 dir = opendir(entry_name);
472 while((dir_e = readdir(dir))) {
473 if(!ignore_entry(dir_e->d_name)) {
474 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
475 recursive_search_in_db(subname, db_filename_regexp,
476 nb_search_conditions, search_conditions,
484 const char *s = entry_name, *filename = entry_name;
485 while(*s) { if(*s == '/') { filename = s+1; } s++; }
487 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
490 printf("Searching in '%s' ... ", entry_name);
494 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
504 /*********************************************************************/
506 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
507 char *raw_mbox_line, FILE *db_file) {
510 for(f = 0; f < nb_fields_to_parse; f++) {
511 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
512 fprintf(db_file, "%s %s\n",
513 field_names[fields_to_parse[f].id],
514 raw_mbox_line + matches.rm_eo);
519 void index_mbox(const char *mbox_filename,
520 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
522 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
523 char *end_of_full_line;
525 int in_header, new_header, last_mbox_line_was_empty;
526 unsigned long int position_in_file;
528 file = fopen(mbox_filename, "r");
531 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
532 if(paranoid) { exit(EXIT_FAILURE); }
539 position_in_file = 0;
540 end_of_full_line = 0;
542 last_mbox_line_was_empty = 1;
544 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
545 if(last_mbox_line_was_empty &&
546 is_a_leading_from_line(raw_mbox_line)) {
549 "Got a ^\"From \" in the header in %s:%lu.\n",
550 mbox_filename, position_in_file);
551 fprintf(stderr, "%s", raw_mbox_line);
552 if(paranoid) { exit(EXIT_FAILURE); }
556 } else if(raw_mbox_line[0] == '\n') {
557 if(in_header) { in_header = 0; }
560 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
564 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
568 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
569 char *start = raw_mbox_line;
570 while(*start == ' ' || *start == '\t') start++;
571 *(end_of_full_line++) = ' ';
572 strcpy(end_of_full_line, start);
573 while(*end_of_full_line && *end_of_full_line != '\n') {
576 *end_of_full_line = '\0';
581 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
582 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
584 "Header line syntax error %s:%lu.\n",
585 mbox_filename, position_in_file);
586 fprintf(stderr, "%s", raw_mbox_line);
591 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
594 end_of_full_line = full_line;
595 strcpy(end_of_full_line, raw_mbox_line);
596 while(*end_of_full_line && *end_of_full_line != '\n') {
599 *end_of_full_line = '\0';
604 position_in_file += strlen(raw_mbox_line);
610 void recursive_index_mbox(FILE *db_file,
611 const char *entry_name,
612 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
614 struct dirent *dir_e;
616 char subname[PATH_MAX + 1];
618 if(lstat(entry_name, &sb) != 0) {
620 "mymail: Cannot stat \"%s\": %s\n",
626 dir = opendir(entry_name);
629 while((dir_e = readdir(dir))) {
630 if(!ignore_entry(dir_e->d_name)) {
631 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
632 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
637 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
641 /*********************************************************************/
643 /* For long options that have no equivalent short option, use a
644 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
646 OPT_BASH_MODE = CHAR_MAX + 1
649 static struct option long_options[] = {
650 { "help", no_argument, 0, 'h' },
651 { "version", no_argument, 0, 'v' },
652 { "quiet", no_argument, 0, 'q' },
653 { "db-file", 1, 0, 'd' },
654 { "db-pattern", 1, 0, 'p' },
655 { "db-root", 1, 0, 'r' },
656 { "db-list", 1, 0, 'l' },
657 { "search", 1, 0, 's' },
658 { "index", 0, 0, 'i' },
659 { "output", 1, 0, 'o' },
663 /*********************************************************************/
665 void init_condition(struct search_condition *condition, char *string) {
666 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
669 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', string);
670 search_field = full_search_field;
672 if(search_field[0] == '!') {
674 condition->negation = 1;
676 condition->negation = 0;
679 if(strcmp(search_field, "6h") == 0) {
680 condition->field_id = ID_INTERVAL;
681 condition->interval_start = time(0) - 3600 * 6;
682 condition->interval_stop = 0;
685 else if(strcmp(search_field, "24h") == 0 ||
686 strcmp(search_field, "today") == 0) {
687 condition->field_id = ID_INTERVAL;
688 condition->interval_start = time(0) - 3600 * 24;
689 condition->interval_stop = 0;
692 else if(strcmp(search_field, "yesterday") == 0) {
693 condition->field_id = ID_INTERVAL;
694 condition->interval_start = time(0) - 2 * 3600 * 24;
695 condition->interval_stop = time(0) - 3600 * 24;
699 condition->field_id = -1;
701 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
702 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
703 condition->field_id = m;
707 if(condition->field_id == -1) {
709 "mymail: Syntax error in field name \"%s\".\n",
714 if(regcomp(&condition->regexp,
718 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
720 field_names[condition->field_id]);
726 void free_condition(struct search_condition *condition) {
727 if(condition->field_id != ID_INTERVAL) {
728 regfree(&condition->regexp);
732 /*********************************************************************/
733 /*********************************************************************/
734 /*********************************************************************/
736 int main(int argc, char **argv) {
737 int error = 0, show_help = 0;
738 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
741 int nb_search_conditions;
743 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
745 if(regcomp(&leading_from_line_regexp,
746 "^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$",
749 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
757 db_filename_list = 0;
760 setlocale(LC_ALL, "");
762 nb_search_conditions = 0;
764 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:",
765 long_options, NULL)) != -1) {
774 print_version(stdout);
786 db_filename = strdup(optarg);
790 db_filename_regexp_string = strdup(optarg);
794 strncpy(output_filename, optarg, PATH_MAX);
798 db_root_path = strdup(optarg);
802 db_filename_list = strdup(optarg);
806 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
807 fprintf(stderr, "mymail: Too many search patterns.\n");
810 init_condition(&search_conditions[nb_search_conditions], optarg);
811 nb_search_conditions++;
821 char *default_db_filename = getenv("MYMAIL_DB_FILE");
823 if(!default_db_filename) {
824 default_db_filename = "mymail.db";
827 db_filename = strdup(default_db_filename);
830 if(!db_filename_regexp_string) {
831 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
833 if(!default_db_filename_regexp_string) {
834 default_db_filename_regexp_string = "^mymail.db$";
837 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
841 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
843 if(default_db_root_path) {
844 db_root_path = strdup(default_db_root_path);
848 if(!db_filename_list) {
849 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
851 if(default_db_filename_list) {
852 db_filename_list = strdup(default_db_filename_list);
856 if(output_filename[0]) {
857 output_file = fopen(output_filename, "w");
861 "mymail: Cannot open result file \"%s\" for writing: %s\n",
867 output_file = stdout;
884 db_file = fopen(db_filename, "w");
888 "mymail: Cannot open \"%s\" for writing: %s\n",
894 for(f = 0; f < nb_fields_to_parse; f++) {
895 if(regcomp(&fields_to_parse[f].regexp,
896 fields_to_parse[f].regexp_string,
899 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
900 fields_to_parse[f].regexp_string,
901 field_names[fields_to_parse[f].id]);
906 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
908 while(optind < argc) {
909 recursive_index_mbox(db_file,
911 nb_fields_to_parse, fields_to_parse);
918 for(f = 0; f < nb_fields_to_parse; f++) {
919 regfree(&fields_to_parse[f].regexp);
925 if(nb_search_conditions > 0) {
927 /* Recursive search if db_root_path is set */
930 regex_t db_filename_regexp;
931 if(regcomp(&db_filename_regexp,
932 db_filename_regexp_string,
935 "mymail: Syntax error in regexp \"%s\".\n",
936 db_filename_regexp_string);
940 recursive_search_in_db(db_root_path, &db_filename_regexp,
941 nb_search_conditions, search_conditions,
944 regfree(&db_filename_regexp);
947 /* Search in all db files listed in db_filename_list */
949 if(db_filename_list) {
950 char db_filename[PATH_MAX + 1];
953 s = db_filename_list;
956 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
959 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
964 /* Search in all db files listed in the command arguments */
966 while(optind < argc) {
967 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
973 for(n = 0; n < nb_search_conditions; n++) {
974 free_condition(&search_conditions[n]);
977 if(output_file != stdout) {
983 free(db_filename_regexp_string);
985 free(db_filename_list);
987 regfree(&leading_from_line_regexp);