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 update_body_hits(char *mail_filename, int position_in_mail,
274 int nb_search_conditions, struct search_condition *search_conditions,
275 int nb_body_conditions,
279 int last_mbox_line_was_empty;
280 char raw_mbox_line[BUFFER_SIZE];
286 mail_file = fopen(mail_filename, "r");
290 "mymail: Cannot open mbox '%s' for body scan.\n",
295 fseek(mail_file, position_in_mail, SEEK_SET);
297 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
298 while(nb_body_hits < nb_body_conditions) {
299 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
301 if(last_mbox_line_was_empty) { header = 0; }
304 for(n = 0; n < nb_search_conditions; n++) {
305 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
307 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
315 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
316 (last_mbox_line_was_empty &&
317 is_a_leading_from_line(raw_mbox_line)))
325 void search_in_db(const char *db_filename,
326 int nb_search_conditions,
327 struct search_condition *search_conditions,
330 int hits[MAX_NB_SEARCH_CONDITIONS];
331 char raw_db_line[BUFFER_SIZE];
332 char raw_mbox_line[BUFFER_SIZE];
333 char current_mail_filename[PATH_MAX + 1];
334 unsigned long int current_position_in_mail;
335 char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
337 int already_written, m, n;
338 int last_mbox_line_was_empty;
339 int nb_body_conditions, nb_fulfilled_body_conditions;
343 printf("Searching in '%s' ... ", db_filename);
347 db_file = fopen(db_filename, "r");
351 "mymail: Cannot open \"%s\" for reading: %s\n",
357 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
358 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
360 "mymail: Header line in '%s' does not match the mymail db format.\n",
366 "mymail: Cannot read the header line in '%s'.\n",
371 current_position_in_mail = 0;
374 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
376 nb_body_conditions = 0;
377 for(n = 0; n < nb_search_conditions; n++) {
378 if(search_conditions[n].field_id == ID_BODY) {
379 nb_body_conditions++;
383 strcpy(current_mail_filename, "");
385 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
386 mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
388 if(strcmp("mail", mbox_name) == 0) {
389 char position_in_file_string[TOKEN_BUFFER_SIZE];
391 if(current_mail_filename[0]) {
393 /* We first check all conditions but the body ones */
395 for(n = 0; n < nb_search_conditions &&
396 ((search_conditions[n].field_id == ID_BODY) ||
397 xor(hits[n], search_conditions[n].negation)); n++);
399 if(n == nb_search_conditions) {
401 /* all conditions but the body ones are fine, check the body
404 if(nb_body_conditions > 0) {
405 update_body_hits(current_mail_filename, current_position_in_mail,
406 nb_search_conditions, search_conditions,
411 nb_fulfilled_body_conditions = 0;
413 for(n = 0; n < nb_search_conditions; n++) {
414 if(search_conditions[n].field_id == ID_BODY &&
415 xor(hits[n], search_conditions[n].negation)) {
416 nb_fulfilled_body_conditions++;
420 if(nb_body_conditions == nb_fulfilled_body_conditions) {
423 mail_file = fopen(current_mail_filename, "r");
427 "mymail: Cannot open mbox '%s' for mail extraction.\n",
428 current_mail_filename);
432 fseek(mail_file, current_position_in_mail, SEEK_SET);
434 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
435 last_mbox_line_was_empty = 0;
436 fprintf(output_file, "%s", raw_mbox_line);
438 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
439 (last_mbox_line_was_empty &&
440 is_a_leading_from_line(raw_mbox_line))
443 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
444 fprintf(output_file, "%s", raw_mbox_line);
453 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
455 mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
456 mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
457 current_position_in_mail = atol(position_in_file_string);
458 remove_eof(current_mail_filename);
464 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
465 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
469 for(n = 0; n < nb_search_conditions; n++) {
470 hits[n] |= mbox_line_match_search(&search_conditions[n],
471 mbox_id, mbox_value);
484 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
485 int nb_search_conditions,
486 struct search_condition *search_conditions,
489 struct dirent *dir_e;
491 char subname[PATH_MAX + 1];
493 if(lstat(entry_name, &sb) != 0) {
495 "mymail: Cannot stat \"%s\": %s\n",
501 dir = opendir(entry_name);
504 while((dir_e = readdir(dir))) {
505 if(!ignore_entry(dir_e->d_name)) {
506 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
507 recursive_search_in_db(subname, db_filename_regexp,
508 nb_search_conditions, search_conditions,
516 const char *s = entry_name, *filename = entry_name;
517 while(*s) { if(*s == '/') { filename = s+1; } s++; }
519 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
520 search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
525 /*********************************************************************/
527 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
528 char *raw_mbox_line, FILE *db_file) {
531 for(f = 0; f < nb_fields_to_parse; f++) {
532 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
533 fprintf(db_file, "%s %s\n",
534 field_names[fields_to_parse[f].id],
535 raw_mbox_line + matches.rm_eo);
540 void index_mbox(const char *mbox_filename,
541 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
543 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
544 char *end_of_full_line;
546 int in_header, new_header, last_mbox_line_was_empty;
547 unsigned long int position_in_file;
549 file = fopen(mbox_filename, "r");
552 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
553 if(paranoid) { exit(EXIT_FAILURE); }
560 position_in_file = 0;
561 end_of_full_line = 0;
563 last_mbox_line_was_empty = 1;
565 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
566 if(last_mbox_line_was_empty &&
567 is_a_leading_from_line(raw_mbox_line)) {
570 "Got a ^\"From \" in the header in %s:%lu.\n",
571 mbox_filename, position_in_file);
572 fprintf(stderr, "%s", raw_mbox_line);
573 if(paranoid) { exit(EXIT_FAILURE); }
577 } else if(raw_mbox_line[0] == '\n') {
578 if(in_header) { in_header = 0; }
581 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
585 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
589 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
590 char *start = raw_mbox_line;
591 while(*start == ' ' || *start == '\t') start++;
592 *(end_of_full_line++) = ' ';
593 strcpy(end_of_full_line, start);
594 while(*end_of_full_line && *end_of_full_line != '\n') {
597 *end_of_full_line = '\0';
602 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
603 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
605 "Header line syntax error %s:%lu.\n",
606 mbox_filename, position_in_file);
607 fprintf(stderr, "%s", raw_mbox_line);
612 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
615 end_of_full_line = full_line;
616 strcpy(end_of_full_line, raw_mbox_line);
617 while(*end_of_full_line && *end_of_full_line != '\n') {
620 *end_of_full_line = '\0';
625 position_in_file += strlen(raw_mbox_line);
631 void recursive_index_mbox(FILE *db_file,
632 const char *entry_name,
633 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
635 struct dirent *dir_e;
637 char subname[PATH_MAX + 1];
639 if(lstat(entry_name, &sb) != 0) {
641 "mymail: Cannot stat \"%s\": %s\n",
647 dir = opendir(entry_name);
650 while((dir_e = readdir(dir))) {
651 if(!ignore_entry(dir_e->d_name)) {
652 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
653 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
658 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
662 /*********************************************************************/
664 /* For long options that have no equivalent short option, use a
665 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
667 OPT_BASH_MODE = CHAR_MAX + 1
670 static struct option long_options[] = {
671 { "help", no_argument, 0, 'h' },
672 { "version", no_argument, 0, 'v' },
673 { "quiet", no_argument, 0, 'q' },
674 { "db-file", 1, 0, 'd' },
675 { "db-pattern", 1, 0, 'p' },
676 { "db-root", 1, 0, 'r' },
677 { "db-list", 1, 0, 'l' },
678 { "search", 1, 0, 's' },
679 { "index", 0, 0, 'i' },
680 { "output", 1, 0, 'o' },
684 /*********************************************************************/
686 void init_condition(struct search_condition *condition, char *string) {
687 char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
690 string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', string);
691 search_field = full_search_field;
693 if(search_field[0] == '!') {
695 condition->negation = 1;
697 condition->negation = 0;
700 if(strcmp(search_field, "6h") == 0) {
701 condition->field_id = ID_INTERVAL;
702 condition->interval_start = time(0) - 3600 * 6;
703 condition->interval_stop = 0;
706 else if(strcmp(search_field, "24h") == 0 ||
707 strcmp(search_field, "today") == 0) {
708 condition->field_id = ID_INTERVAL;
709 condition->interval_start = time(0) - 3600 * 24;
710 condition->interval_stop = 0;
713 else if(strcmp(search_field, "yesterday") == 0) {
714 condition->field_id = ID_INTERVAL;
715 condition->interval_start = time(0) - 2 * 3600 * 24;
716 condition->interval_stop = time(0) - 3600 * 24;
720 condition->field_id = -1;
722 for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
723 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
724 condition->field_id = m;
728 if(condition->field_id == -1) {
730 "mymail: Syntax error in field name \"%s\".\n",
735 if(regcomp(&condition->regexp,
739 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
741 field_names[condition->field_id]);
747 void free_condition(struct search_condition *condition) {
748 if(condition->field_id != ID_INTERVAL) {
749 regfree(&condition->regexp);
753 /*********************************************************************/
754 /*********************************************************************/
755 /*********************************************************************/
757 int main(int argc, char **argv) {
758 int error = 0, show_help = 0;
759 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
762 int nb_search_conditions;
764 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
766 if(regcomp(&leading_from_line_regexp,
767 "^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$",
770 "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
778 db_filename_list = 0;
781 setlocale(LC_ALL, "");
783 nb_search_conditions = 0;
785 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:",
786 long_options, NULL)) != -1) {
795 print_version(stdout);
807 db_filename = strdup(optarg);
811 db_filename_regexp_string = strdup(optarg);
815 strncpy(output_filename, optarg, PATH_MAX);
819 db_root_path = strdup(optarg);
823 db_filename_list = strdup(optarg);
827 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
828 fprintf(stderr, "mymail: Too many search patterns.\n");
831 init_condition(&search_conditions[nb_search_conditions], optarg);
832 nb_search_conditions++;
842 char *default_db_filename = getenv("MYMAIL_DB_FILE");
844 if(!default_db_filename) {
845 default_db_filename = "mymail.db";
848 db_filename = strdup(default_db_filename);
851 if(!db_filename_regexp_string) {
852 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
854 if(!default_db_filename_regexp_string) {
855 default_db_filename_regexp_string = "^mymail.db$";
858 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
862 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
864 if(default_db_root_path) {
865 db_root_path = strdup(default_db_root_path);
869 if(!db_filename_list) {
870 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
872 if(default_db_filename_list) {
873 db_filename_list = strdup(default_db_filename_list);
877 if(output_filename[0]) {
878 output_file = fopen(output_filename, "w");
882 "mymail: Cannot open result file \"%s\" for writing: %s\n",
888 output_file = stdout;
905 db_file = fopen(db_filename, "w");
909 "mymail: Cannot open \"%s\" for writing: %s\n",
915 for(f = 0; f < nb_fields_to_parse; f++) {
916 if(regcomp(&fields_to_parse[f].regexp,
917 fields_to_parse[f].regexp_string,
920 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
921 fields_to_parse[f].regexp_string,
922 field_names[fields_to_parse[f].id]);
927 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
929 while(optind < argc) {
930 recursive_index_mbox(db_file,
932 nb_fields_to_parse, fields_to_parse);
939 for(f = 0; f < nb_fields_to_parse; f++) {
940 regfree(&fields_to_parse[f].regexp);
946 if(nb_search_conditions > 0) {
948 /* Recursive search if db_root_path is set */
951 regex_t db_filename_regexp;
952 if(regcomp(&db_filename_regexp,
953 db_filename_regexp_string,
956 "mymail: Syntax error in regexp \"%s\".\n",
957 db_filename_regexp_string);
961 recursive_search_in_db(db_root_path, &db_filename_regexp,
962 nb_search_conditions, search_conditions,
965 regfree(&db_filename_regexp);
968 /* Search in all db files listed in db_filename_list */
970 if(db_filename_list) {
971 char db_filename[PATH_MAX + 1];
974 s = db_filename_list;
977 s = parse_token(db_filename, PATH_MAX + 1, ';', s);
980 search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
985 /* Search in all db files listed in the command arguments */
987 while(optind < argc) {
988 search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
994 for(n = 0; n < nb_search_conditions; n++) {
995 free_condition(&search_conditions[n]);
998 if(output_file != stdout) {
1000 fclose(output_file);
1004 free(db_filename_regexp_string);
1006 free(db_filename_list);
1008 regfree(&leading_from_line_regexp);