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
55 regex_t leading_from_line_regexp;
58 char *db_filename_regexp_string;
60 char *db_filename_list;
61 char output_filename[PATH_MAX + 1];
69 /********************************************************************/
84 static char *field_names[] = {
96 /********************************************************************/
98 struct search_condition {
102 time_t interval_start, interval_stop;
105 /********************************************************************/
107 struct parsable_field {
113 static struct parsable_field fields_to_parse[] = {
117 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
122 "^\\([Ff][Rr][Oo][Mm]:\\|[Rr][Ee][Pp][Ll][Yy]-[Tt][Oo]:\\|[Ss][Ee][Nn][Dd][Ee][Rr]:\\)",
123 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
128 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
129 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
134 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
135 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
140 "^[Dd][Aa][Tt][Ee]: ",
141 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
146 /********************************************************************/
148 int xor(int a, int b) {
149 return (a && !b) || (!a && b);
152 char *segment_next_field(char *current) {
153 while(*current && *current != ' ') current++;
154 *current = '\0'; current++;
155 while(*current && *current == ' ') current++;
159 void remove_eof(char *c) {
160 while(*c && *c != '\n' && *c != '\r') c++;
164 /********************************************************************/
166 /* malloc with error checking. */
168 void *safe_malloc(size_t n) {
172 "mymail: cannot allocate memory: %s\n", strerror(errno));
178 /*********************************************************************/
180 void print_version(FILE *out) {
181 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
184 void print_usage(FILE *out) {
186 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
188 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
190 fprintf(out, " -h, --help\n");
191 fprintf(out, " show this help\n");
192 fprintf(out, " -v, --version\n");
193 fprintf(out, " print the version number\n");
194 fprintf(out, " -q, --quiet\n");
195 fprintf(out, " do not print information during search\n");
196 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
197 fprintf(out, " set the db filename pattern for recursive search\n");
198 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
199 fprintf(out, " set the db root path for recursive search\n");
200 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
201 fprintf(out, " set the semicolon-separated list of db files for search\n");
202 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
203 fprintf(out, " search for matching mails in the db file\n");
204 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
205 fprintf(out, " set the db filename for indexing\n");
206 fprintf(out, " -i, --index\n");
207 fprintf(out, " index mails\n");
208 fprintf(out, " -o <output filename>, --output <output filename>\n");
209 fprintf(out, " set the result file, use stdout if unset\n");
212 /*********************************************************************/
214 int ignore_entry(const char *name) {
216 /* strcmp(name, ".") == 0 || */
217 /* strcmp(name, "..") == 0 || */
218 (name[0] == '.' && name[1] != '/');
221 int is_a_leading_from_line(char *s) {
222 return strncmp(s, "From ", 5) == 0 &&
223 regexec(&leading_from_line_regexp, s, 0, 0, 0) == 0;
226 int mbox_line_match_search(struct search_condition *condition,
227 int mbox_id, char *mbox_value) {
229 if(condition->field_id == ID_INTERVAL) {
230 if(mbox_id == ID_LEADING_LINE) {
236 while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
237 strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
240 return (t >= condition->interval_start &&
241 (condition->interval_stop == 0 ||
242 t <= condition->interval_stop));
250 (condition->field_id == mbox_id)
254 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
255 mbox_id == ID_FROM ||
259 (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
263 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
267 void search_in_db(FILE *db_file,
268 int nb_search_conditions,
269 struct search_condition *search_conditions,
272 int hits[MAX_NB_SEARCH_CONDITIONS];
273 char raw_db_line[BUFFER_SIZE];
274 char raw_mbox_line[BUFFER_SIZE];
275 char current_mail_filename[PATH_MAX + 1];
276 unsigned long int current_position_in_mail;
277 char *mbox_name, *mbox_value;
279 int already_written, m, n;
280 int last_mbox_line_was_empty;
281 int nb_body_conditions, nb_fulfilled_body_conditions;
283 current_position_in_mail = 0;
286 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
288 nb_body_conditions = 0;
289 for(n = 0; n < nb_search_conditions; n++) {
290 if(search_conditions[n].field_id == ID_BODY) {
291 nb_body_conditions++;
295 strcpy(current_mail_filename, "");
297 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
298 mbox_name = raw_db_line;
299 mbox_value = segment_next_field(raw_db_line);
301 if(strcmp("mail", mbox_name) == 0) {
302 char *position_in_file_string;
305 if(current_mail_filename[0]) {
307 /* We first check all conditions but the body ones */
309 for(n = 0; n < nb_search_conditions &&
310 ((search_conditions[n].field_id == ID_BODY) ||
311 xor(hits[n], search_conditions[n].negation)); n++);
313 if(n == nb_search_conditions) {
315 /* all conditions but the body ones are fine, check the body
318 nb_fulfilled_body_conditions = 0;
320 if(nb_body_conditions > 0) {
325 mail_file = fopen(current_mail_filename, "r");
329 "mymail: Cannot open mbox '%s' for body scan.\n",
330 current_mail_filename);
334 fseek(mail_file, current_position_in_mail, SEEK_SET);
336 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
337 while(nb_fulfilled_body_conditions < nb_body_conditions) {
338 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
340 if(last_mbox_line_was_empty) { header = 0; }
343 for(n = 0; n < nb_search_conditions; n++) {
344 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
346 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
348 nb_fulfilled_body_conditions++;
354 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
355 (last_mbox_line_was_empty &&
356 is_a_leading_from_line(raw_mbox_line)))
364 if(nb_body_conditions == nb_fulfilled_body_conditions) {
367 mail_file = fopen(current_mail_filename, "r");
371 "mymail: Cannot open mbox '%s' for mail extraction.\n",
372 current_mail_filename);
376 fseek(mail_file, current_position_in_mail, SEEK_SET);
378 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
379 last_mbox_line_was_empty = 1;
380 fprintf(output_file, "%s", raw_mbox_line);
382 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
383 (last_mbox_line_was_empty &&
384 is_a_leading_from_line(raw_mbox_line))
387 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
388 fprintf(output_file, "%s", raw_mbox_line);
397 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
399 position_in_file_string = mbox_value;
400 mail_filename = segment_next_field(mbox_value);
401 current_position_in_mail = atol(position_in_file_string);
402 strcpy(current_mail_filename, mail_filename);
404 remove_eof(current_mail_filename);
410 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
411 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
415 for(n = 0; n < nb_search_conditions; n++) {
416 hits[n] |= mbox_line_match_search(&search_conditions[n],
417 mbox_id, mbox_value);
423 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
424 int nb_search_conditions,
425 struct search_condition *search_conditions,
428 struct dirent *dir_e;
430 char raw_db_line[BUFFER_SIZE];
431 char subname[PATH_MAX + 1];
433 if(lstat(entry_name, &sb) != 0) {
435 "mymail: Cannot stat \"%s\": %s\n",
441 dir = opendir(entry_name);
444 while((dir_e = readdir(dir))) {
445 if(!ignore_entry(dir_e->d_name)) {
446 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
447 recursive_search_in_db(subname, db_filename_regexp,
448 nb_search_conditions, search_conditions,
456 const char *s = entry_name, *filename = entry_name;
457 while(*s) { if(*s == '/') { filename = s+1; } s++; }
459 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
460 FILE *db_file = fopen(entry_name, "r");
463 printf("Searching in '%s' ... ", entry_name);
469 "mymail: Cannot open \"%s\" for reading: %s\n",
475 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
476 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
478 "mymail: Header line in '%s' does not match the mymail db format.\n",
484 "mymail: Cannot read the header line in '%s'.\n",
489 search_in_db(db_file, nb_search_conditions, search_conditions, output_file);
501 /*********************************************************************/
503 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
504 char *raw_mbox_line, FILE *db_file) {
507 for(f = 0; f < nb_fields_to_parse; f++) {
508 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
509 fprintf(db_file, "%s %s\n",
510 field_names[fields_to_parse[f].id],
511 raw_mbox_line + matches.rm_eo);
516 void index_mbox(const char *mbox_filename,
517 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
519 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
520 char *end_of_full_line;
522 int in_header, new_header, last_mbox_line_was_empty;
523 unsigned long int position_in_file;
525 file = fopen(mbox_filename, "r");
528 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
529 if(paranoid) { exit(EXIT_FAILURE); }
536 position_in_file = 0;
537 end_of_full_line = 0;
539 last_mbox_line_was_empty = 1;
541 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
542 if(last_mbox_line_was_empty &&
543 is_a_leading_from_line(raw_mbox_line)) {
546 "Got a ^\"From \" in the header in %s:%lu.\n",
547 mbox_filename, position_in_file);
548 fprintf(stderr, "%s", raw_mbox_line);
549 if(paranoid) { exit(EXIT_FAILURE); }
553 } else if(raw_mbox_line[0] == '\n') {
554 if(in_header) { in_header = 0; }
557 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
561 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
565 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
566 char *start = raw_mbox_line;
567 while(*start == ' ' || *start == '\t') start++;
568 *(end_of_full_line++) = ' ';
569 strcpy(end_of_full_line, start);
570 while(*end_of_full_line && *end_of_full_line != '\n') {
573 *end_of_full_line = '\0';
578 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
579 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
581 "Header line syntax error %s:%lu.\n",
582 mbox_filename, position_in_file);
583 fprintf(stderr, "%s", raw_mbox_line);
588 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
591 end_of_full_line = full_line;
592 strcpy(end_of_full_line, raw_mbox_line);
593 while(*end_of_full_line && *end_of_full_line != '\n') {
596 *end_of_full_line = '\0';
601 position_in_file += strlen(raw_mbox_line);
607 void recursive_index_mbox(FILE *db_file,
608 const char *entry_name,
609 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
611 struct dirent *dir_e;
613 char subname[PATH_MAX + 1];
615 if(lstat(entry_name, &sb) != 0) {
617 "mymail: Cannot stat \"%s\": %s\n",
623 dir = opendir(entry_name);
626 while((dir_e = readdir(dir))) {
627 if(!ignore_entry(dir_e->d_name)) {
628 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
629 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
634 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
638 /*********************************************************************/
640 /* For long options that have no equivalent short option, use a
641 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
643 OPT_BASH_MODE = CHAR_MAX + 1
646 static struct option long_options[] = {
647 { "help", no_argument, 0, 'h' },
648 { "version", no_argument, 0, 'v' },
649 { "quiet", no_argument, 0, 'q' },
650 { "db-file", 1, 0, 'd' },
651 { "db-pattern", 1, 0, 'p' },
652 { "db-root", 1, 0, 'r' },
653 { "db-list", 1, 0, 'l' },
654 { "search", 1, 0, 's' },
655 { "index", 0, 0, 'i' },
656 { "output", 1, 0, 'o' },
660 /*********************************************************************/
662 int main(int argc, char **argv) {
663 int error = 0, show_help = 0;
664 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
667 int nb_search_conditions;
668 char *search_condition_strings[MAX_NB_SEARCH_CONDITIONS];
671 if(regcomp(&leading_from_line_regexp,
672 "^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$",
675 "mymail: Cannot compile leading from line regexp. That is strange.\n");
679 /* printf("%d\n", regexec(&leading_from_line_regexp, "From root@idiap.ch Mon Apr 18 08:25:06 2011\n", 0, 0, 0)); */
680 /* exit(EXIT_SUCCESS); */
686 db_filename_list = 0;
689 setlocale(LC_ALL, "");
691 nb_search_conditions = 0;
693 while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:",
694 long_options, NULL)) != -1) {
703 print_version(stdout);
715 db_filename = strdup(optarg);
719 db_filename_regexp_string = strdup(optarg);
723 strncpy(output_filename, optarg, PATH_MAX);
727 db_root_path = strdup(optarg);
731 db_filename_list = strdup(optarg);
735 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
736 fprintf(stderr, "mymail: Too many search patterns.\n");
739 search_condition_strings[nb_search_conditions++] = strdup(optarg);
749 char *default_db_filename = getenv("MYMAIL_DB_FILE");
751 if(!default_db_filename) {
752 default_db_filename = "mymail.db";
755 db_filename = strdup(default_db_filename);
758 if(!db_filename_regexp_string) {
759 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
761 if(!default_db_filename_regexp_string) {
762 default_db_filename_regexp_string = "^mymail.db$";
765 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
769 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
771 if(default_db_root_path) {
772 db_root_path = strdup(default_db_root_path);
776 if(!db_filename_list) {
777 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
779 if(default_db_filename_list) {
780 db_filename_list = strdup(default_db_filename_list);
784 if(output_filename[0]) {
785 output_file = fopen(output_filename, "w");
789 "mymail: Cannot open result file \"%s\" for writing: %s\n",
795 output_file = stdout;
812 db_file = fopen(db_filename, "w");
816 "mymail: Cannot open \"%s\" for writing: %s\n",
822 for(f = 0; f < nb_fields_to_parse; f++) {
823 if(regcomp(&fields_to_parse[f].regexp,
824 fields_to_parse[f].regexp_string,
827 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
828 fields_to_parse[f].regexp_string,
829 field_names[fields_to_parse[f].id]);
834 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
836 while(optind < argc) {
837 recursive_index_mbox(db_file,
839 nb_fields_to_parse, fields_to_parse);
846 for(f = 0; f < nb_fields_to_parse; f++) {
847 regfree(&fields_to_parse[f].regexp);
853 if(nb_search_conditions > 0) {
854 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
855 char *search_field, *search_regexp_string;
858 for(n = 0; n < nb_search_conditions; n++) {
859 search_field = search_condition_strings[n];
861 if(search_field[0] == '!') {
863 search_conditions[n].negation = 1;
865 search_conditions[n].negation = 0;
868 if(strcmp(search_field, "today") == 0) {
869 search_conditions[n].field_id = ID_INTERVAL;
870 search_conditions[n].interval_start = time(0) - 3600 * 24;
871 search_conditions[n].interval_stop = 0;
874 else if(strcmp(search_field, "yesterday") == 0) {
875 search_conditions[n].field_id = ID_INTERVAL;
876 search_conditions[n].interval_start = time(0) - 2 * 3600 * 24;
877 search_conditions[n].interval_stop = time(0) - 3600 * 24;
881 search_regexp_string = segment_next_field(search_condition_strings[n]);
883 search_conditions[n].field_id = -1;
885 for(m = 0; (m < MAX_ID) && search_conditions[n].field_id == -1; m++) {
886 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
887 search_conditions[n].field_id = m;
891 if(search_conditions[n].field_id == -1) {
893 "mymail: Syntax error in field name \"%s\".\n",
898 if(regcomp(&search_conditions[n].regexp,
899 search_regexp_string,
902 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
903 search_regexp_string,
904 field_names[search_conditions[n].field_id]);
910 /* Recursive search if db_root_path is set */
913 regex_t db_filename_regexp;
914 if(regcomp(&db_filename_regexp,
915 db_filename_regexp_string,
918 "mymail: Syntax error in regexp \"%s\".\n",
919 db_filename_regexp_string);
923 recursive_search_in_db(db_root_path, &db_filename_regexp,
924 nb_search_conditions, search_conditions,
927 regfree(&db_filename_regexp);
930 /* Search in all db files listed in db_filename_list */
932 if(db_filename_list) {
933 char db_filename[PATH_MAX + 1];
937 s = db_filename_list;
941 while(*s == ';') { s++; }
942 while(*s && *s != ';') { *t++ = *s++; }
946 db_file = fopen(db_filename, "r");
950 "mymail: Cannot open \"%s\" for reading: %s\n",
956 search_in_db(db_file, nb_search_conditions, search_conditions, output_file);
963 /* Search in all db files listed in the command arguments */
965 while(optind < argc) {
966 FILE *db_file = fopen(argv[optind], "r");
970 "mymail: Cannot open \"%s\" for reading: %s\n",
976 search_in_db(db_file, nb_search_conditions, search_conditions, output_file);
982 for(n = 0; n < nb_search_conditions; n++) {
983 regfree(&search_conditions[n].regexp);
984 free(search_condition_strings[n]);
989 if(output_file != stdout) {
995 free(db_filename_regexp_string);
997 free(db_filename_list);
999 regfree(&leading_from_line_regexp);