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.
47 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
50 #define MAX_NB_SEARCH_CONDITIONS 10
52 #define BUFFER_SIZE 65536
55 char *db_filename_regexp_string;
57 char *db_filename_list;
62 /********************************************************************/
75 static char *field_names[] = {
85 /********************************************************************/
87 struct search_condition {
93 /********************************************************************/
95 struct parsable_field {
101 static struct parsable_field fields_to_parse[] = {
104 "^\\(From \\|[Ff][Rr][Oo][Mm]:\\|[R][r][E][e][P][p][L][l][Y][y]-[T][t][O][o]:\\)",
105 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
110 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
111 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
116 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
117 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
122 "^[Dd][Aa][Tt][Ee]: ",
123 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
128 /********************************************************************/
130 int xor(int a, int b) {
131 return (a && !b) || (!a && b);
134 char *segment_next_field(char *current) {
135 while(*current && *current != ' ') current++;
136 *current = '\0'; current++;
137 while(*current && *current == ' ') current++;
141 void remove_eof(char *c) {
142 while(*c && *c != '\n' && *c != '\r') c++;
146 /********************************************************************/
148 /* malloc with error checking. */
150 void *safe_malloc(size_t n) {
154 "mymail: cannot allocate memory: %s\n", strerror(errno));
160 /*********************************************************************/
162 void print_version(FILE *out) {
163 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
166 void print_usage(FILE *out) {
168 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
170 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
172 fprintf(out, " -h, --help\n");
173 fprintf(out, " show this help\n");
174 fprintf(out, " -v, --version\n");
175 fprintf(out, " print the version number\n");
176 fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
177 fprintf(out, " set the db filename pattern for recursive search\n");
178 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
179 fprintf(out, " set the db root path for recursive search\n");
180 fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
181 fprintf(out, " set the semicolon-separated list of db files for search\n");
182 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
183 fprintf(out, " search for matching mails in the db file\n");
184 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
185 fprintf(out, " set the db filename for indexing\n");
186 fprintf(out, " -i, --index\n");
187 fprintf(out, " index mails\n");
190 /*********************************************************************/
192 int ignore_entry(const char *name) {
194 /* strcmp(name, ".") == 0 || */
195 /* strcmp(name, "..") == 0 || */
196 (name[0] == '.' && name[1] != '/');
199 int mbox_line_match_search(struct search_condition *condition,
200 int mbox_id, char *mbox_value) {
202 (condition->field_id == mbox_id ||
203 (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_TO)))
205 regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
208 void search_in_db(int nb_search_conditions,
209 struct search_condition *search_conditions,
211 int hits[MAX_NB_SEARCH_CONDITIONS];
212 char raw_db_line[BUFFER_SIZE];
213 char raw_mbox_line[BUFFER_SIZE];
214 char current_mail_filename[PATH_MAX + 1];
215 unsigned long int current_position_in_mail;
216 char *mbox_name, *mbox_value;
218 int already_written, m, n;
219 int last_mbox_line_was_empty;
220 int nb_body_conditions, nb_fulfilled_body_conditions;
222 current_position_in_mail = 0;
225 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
227 nb_body_conditions = 0;
228 for(n = 0; n < nb_search_conditions; n++) {
229 if(search_conditions[n].field_id == ID_BODY) {
230 nb_body_conditions++;
234 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
235 mbox_name = raw_db_line;
236 mbox_value = segment_next_field(raw_db_line);
238 if(strcmp("mail", mbox_name) == 0) {
239 char *position_in_file_string;
242 /* We first check all conditions but the body ones */
244 for(n = 0; n < nb_search_conditions &&
245 ((search_conditions[n].field_id == ID_BODY) ||
246 xor(hits[n], search_conditions[n].negation)); n++);
248 if(n == nb_search_conditions) {
250 /* all conditions but the body ones are fine, check the body
253 nb_fulfilled_body_conditions = 0;
255 if(nb_body_conditions > 0) {
260 mail_file = fopen(current_mail_filename, "r");
263 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
267 fseek(mail_file, current_position_in_mail, SEEK_SET);
269 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
270 while(nb_fulfilled_body_conditions < nb_body_conditions) {
271 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
273 if(last_mbox_line_was_empty) { header = 0; }
276 for(n = 0; n < nb_search_conditions; n++) {
277 if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
279 (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
281 fprintf(stderr, "** %d %s", header, raw_mbox_line);
282 nb_fulfilled_body_conditions++;
288 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
289 (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
296 if(nb_body_conditions == nb_fulfilled_body_conditions) {
299 mail_file = fopen(current_mail_filename, "r");
302 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
306 fseek(mail_file, current_position_in_mail, SEEK_SET);
308 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
309 last_mbox_line_was_empty = 1;
310 printf("%s", raw_mbox_line);
312 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
313 (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
314 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
315 printf("%s", raw_mbox_line);
323 for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
325 position_in_file_string = mbox_value;
326 mail_filename = segment_next_field(mbox_value);
327 current_position_in_mail = atol(position_in_file_string);
328 strcpy(current_mail_filename, mail_filename);
330 remove_eof(current_mail_filename);
336 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
337 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
341 for(n = 0; n < nb_search_conditions; n++) {
342 hits[n] |= mbox_line_match_search(&search_conditions[n],
343 mbox_id, mbox_value);
349 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
350 int nb_search_conditions, struct search_condition *search_conditions) {
352 struct dirent *dir_e;
354 char raw_db_line[BUFFER_SIZE];
355 char subname[PATH_MAX + 1];
357 if(lstat(entry_name, &sb) != 0) {
359 "mymail: Cannot stat \"%s\": %s\n",
365 dir = opendir(entry_name);
368 while((dir_e = readdir(dir))) {
369 if(!ignore_entry(dir_e->d_name)) {
370 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
371 recursive_search_in_db(subname, db_filename_regexp,
372 nb_search_conditions, search_conditions);
379 const char *s = entry_name, *filename = entry_name;
380 while(*s) { if(*s == '/') { filename = s+1; } s++; }
382 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
383 FILE *db_file = fopen(entry_name, "r");
387 "mymail: Cannot open \"%s\" for reading: %s\n",
393 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
394 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
396 "mymail: Header line in '%s' does not match the mymail db format.\n",
402 "mymail: Cannot read the header line in '%s'.\n",
407 search_in_db(nb_search_conditions, search_conditions, db_file);
414 /*********************************************************************/
416 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
417 char *raw_mbox_line, FILE *db_file) {
420 for(f = 0; f < nb_fields_to_parse; f++) {
421 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
422 fprintf(db_file, "%s %s\n",
423 field_names[fields_to_parse[f].id],
424 raw_mbox_line + matches.rm_eo);
429 void index_mbox(const char *mbox_filename,
430 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
432 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
433 char *end_of_full_line;
435 int in_header, new_header, last_mbox_line_was_empty;
436 unsigned long int position_in_file;
438 file = fopen(mbox_filename, "r");
441 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
442 if(paranoid) { exit(EXIT_FAILURE); }
449 position_in_file = 0;
450 end_of_full_line = 0;
452 last_mbox_line_was_empty = 1;
454 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
455 if(last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0) {
458 "Got a ^\"From \" in the header in %s:%lu.\n",
459 mbox_filename, position_in_file);
460 fprintf(stderr, "%s", raw_mbox_line);
461 if(paranoid) { exit(EXIT_FAILURE); }
465 } else if(raw_mbox_line[0] == '\n') {
466 if(in_header) { in_header = 0; }
469 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
473 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
477 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
478 char *start = raw_mbox_line;
479 while(*start == ' ' || *start == '\t') start++;
480 *(end_of_full_line++) = ' ';
481 strcpy(end_of_full_line, start);
482 while(*end_of_full_line && *end_of_full_line != '\n') {
485 *end_of_full_line = '\0';
490 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
491 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
493 "Header line syntax error %s:%lu.\n",
494 mbox_filename, position_in_file);
495 fprintf(stderr, "%s", raw_mbox_line);
500 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
503 end_of_full_line = full_line;
504 strcpy(end_of_full_line, raw_mbox_line);
505 while(*end_of_full_line && *end_of_full_line != '\n') {
508 *end_of_full_line = '\0';
513 position_in_file += strlen(raw_mbox_line);
519 void recursive_index_mbox(FILE *db_file,
520 const char *entry_name,
521 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
523 struct dirent *dir_e;
525 char subname[PATH_MAX + 1];
527 if(lstat(entry_name, &sb) != 0) {
529 "mymail: Cannot stat \"%s\": %s\n",
535 dir = opendir(entry_name);
538 while((dir_e = readdir(dir))) {
539 if(!ignore_entry(dir_e->d_name)) {
540 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
541 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
546 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
550 /*********************************************************************/
552 /* For long options that have no equivalent short option, use a
553 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
555 OPT_BASH_MODE = CHAR_MAX + 1
558 static struct option long_options[] = {
559 { "help", no_argument, 0, 'h' },
560 { "version", no_argument, 0, 'v' },
561 { "db-file", 1, 0, 'd' },
562 { "db-pattern", 1, 0, 'p' },
563 { "db-root", 1, 0, 'r' },
564 { "db-list", 1, 0, 'l' },
565 { "search", 1, 0, 's' },
566 { "index", 0, 0, 'i' },
570 /*********************************************************************/
572 int main(int argc, char **argv) {
573 int error = 0, show_help = 0;
574 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
577 int nb_search_conditions;
578 char *search_condition_strings[MAX_NB_SEARCH_CONDITIONS];
580 /* for(f = 0; f < argc; f++) { */
581 /* printf("arg %d \"%s\"\n", f, argv[f]); */
588 db_filename_list = 0;
590 setlocale(LC_ALL, "");
592 nb_search_conditions = 0;
594 while ((c = getopt_long(argc, argv, "hvip:s:d:r:l:",
595 long_options, NULL)) != -1) {
604 print_version(stdout);
612 db_filename = strdup(optarg);
616 db_filename_regexp_string = strdup(optarg);
620 db_root_path = strdup(optarg);
624 db_filename_list = strdup(optarg);
628 if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
629 fprintf(stderr, "mymail: Too many search patterns.\n");
632 search_condition_strings[nb_search_conditions++] = strdup(optarg);
642 char *default_db_filename = getenv("MYMAIL_DB_FILE");
644 if(!default_db_filename) {
645 default_db_filename = "mymail.db";
648 db_filename = strdup(default_db_filename);
651 if(!db_filename_regexp_string) {
652 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
654 if(!default_db_filename_regexp_string) {
655 default_db_filename_regexp_string = "^mymail.db$";
658 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
662 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
664 if(default_db_root_path) {
665 db_root_path = strdup(default_db_root_path);
669 if(!db_filename_list) {
670 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
672 if(default_db_filename_list) {
673 db_filename_list = strdup(default_db_filename_list);
690 db_file = fopen(db_filename, "w");
694 "mymail: Cannot open \"%s\" for writing: %s\n",
700 for(f = 0; f < nb_fields_to_parse; f++) {
701 if(regcomp(&fields_to_parse[f].regexp,
702 fields_to_parse[f].regexp_string,
705 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
706 fields_to_parse[f].regexp_string,
707 field_names[fields_to_parse[f].id]);
712 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
714 while(optind < argc) {
715 recursive_index_mbox(db_file,
717 nb_fields_to_parse, fields_to_parse);
723 for(f = 0; f < nb_fields_to_parse; f++) {
724 regfree(&fields_to_parse[f].regexp);
730 if(nb_search_conditions > 0) {
731 struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
732 char *search_field, *search_regexp_string;
735 for(n = 0; n < nb_search_conditions; n++) {
736 search_field = search_condition_strings[n];
737 search_regexp_string = segment_next_field(search_condition_strings[n]);
739 if(search_field[0] == '!') {
741 search_conditions[n].negation = 1;
743 search_conditions[n].negation = 0;
746 search_conditions[n].field_id = -1;
747 for(m = 0; (m < MAX_ID) && search_conditions[n].field_id == -1; m++) {
748 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
749 search_conditions[n].field_id = m;
753 if(search_conditions[n].field_id == -1) {
755 "mymail: Syntax error in field name \"%s\".\n",
760 if(regcomp(&search_conditions[n].regexp,
761 search_regexp_string,
764 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
765 search_regexp_string,
766 field_names[search_conditions[n].field_id]);
771 /* Recursive search if db_root_path is set */
774 regex_t db_filename_regexp;
775 if(regcomp(&db_filename_regexp,
776 db_filename_regexp_string,
779 "mymail: Syntax error in regexp \"%s\".\n",
780 db_filename_regexp_string);
784 recursive_search_in_db(db_root_path, &db_filename_regexp,
785 nb_search_conditions, search_conditions);
787 regfree(&db_filename_regexp);
790 /* Search in all db files listed in db_filename_list */
792 if(db_filename_list) {
793 char db_filename[PATH_MAX + 1];
797 s = db_filename_list;
801 while(*s == ';') { s++; }
802 while(*s && *s != ';') { *t++ = *s++; }
806 db_file = fopen(db_filename, "r");
810 "mymail: Cannot open \"%s\" for reading: %s\n",
816 search_in_db(nb_search_conditions, search_conditions, db_file);
823 /* Search in all db files listed in the command arguments */
825 while(optind < argc) {
826 FILE *db_file = fopen(argv[optind], "r");
830 "mymail: Cannot open \"%s\" for reading: %s\n",
836 search_in_db(nb_search_conditions, search_conditions, db_file);
842 for(n = 0; n < nb_search_conditions; n++) {
843 regfree(&search_conditions[n].regexp);
844 free(search_condition_strings[n]);
850 free(db_filename_regexp_string);
852 free(db_filename_list);