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_REQUESTS 10
52 #define BUFFER_SIZE 65536
55 char *db_filename_regexp_string;
57 char *db_filename_list;
62 /********************************************************************/
74 static char *field_names[] = {
83 /********************************************************************/
85 struct search_request {
91 /********************************************************************/
93 struct parsable_field {
99 static struct parsable_field fields_to_parse[] = {
102 "^\\(From \\|[Ff][Rr][Oo][Mm]:\\|[R][r][E][e][P][p][L][l][Y][y]-[T][t][O][o]:\\)",
103 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
108 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
109 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
114 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
115 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
120 "^[Dd][Aa][Tt][Ee]: ",
121 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
126 /********************************************************************/
128 int xor(int a, int b) {
129 return (a && !b) || (!a && b);
132 char *segment_next_field(char *current) {
133 while(*current && *current != ' ') current++;
134 *current = '\0'; current++;
135 while(*current && *current == ' ') current++;
139 void remove_eof(char *c) {
140 while(*c && *c != '\n' && *c != '\r') c++;
144 /********************************************************************/
146 /* malloc with error checking. */
148 void *safe_malloc(size_t n) {
152 "mymail: cannot allocate memory: %s\n", strerror(errno));
158 /*********************************************************************/
160 void print_version(FILE *out) {
161 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
164 void print_usage(FILE *out) {
166 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
168 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]]\n");
170 fprintf(out, " -h, --help\n");
171 fprintf(out, " show this help\n");
172 fprintf(out, " -v, --version\n");
173 fprintf(out, " print the version number\n");
174 fprintf(out, " -i, --index\n");
175 fprintf(out, " index mails\n");
176 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
177 fprintf(out, " search for matching mails in the data-base file\n");
178 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
179 fprintf(out, " set the data-base file\n");
180 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
181 fprintf(out, " set the data-base root path for recursive search\n");
184 /*********************************************************************/
186 int ignore_entry(const char *name) {
188 /* strcmp(name, ".") == 0 || */
189 /* strcmp(name, "..") == 0 || */
190 (name[0] == '.' && name[1] != '/');
193 int mbox_line_match_search(struct search_request *request,
194 int mbox_id, char *mbox_value) {
196 (request->field_id == mbox_id ||
197 (request->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_TO)))
199 regexec(&request->regexp, mbox_value, 0, 0, 0) == 0;
202 void search_in_db(int nb_search_requests,
203 struct search_request *search_requests,
205 int hits[MAX_NB_SEARCH_REQUESTS];
206 char raw_db_line[BUFFER_SIZE];
207 char raw_mbox_line[BUFFER_SIZE];
208 char current_mail_filename[PATH_MAX + 1];
209 unsigned long int current_position_in_mail;
210 char *mbox_name, *mbox_value;
212 int already_written, m, n;
213 int last_mbox_line_was_empty;
215 current_position_in_mail = 0;
218 for(n = 0; n < nb_search_requests; n++) { hits[n] = 0; }
220 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
221 mbox_name = raw_db_line;
222 mbox_value = segment_next_field(raw_db_line);
224 if(strcmp("mail", mbox_name) == 0) {
225 char *position_in_file_string;
228 for(n = 0; n < nb_search_requests && xor(hits[n], search_requests[n].negation); n++);
230 /* for(n = 0; n < nb_search_requests && */
231 /* ((hits[n] && !search_requests[n].negation) || */
232 /* (!hits[n] && search_requests[n].negation)); n++); */
234 if(n == nb_search_requests) {
237 mail_file = fopen(current_mail_filename, "r");
240 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
244 fseek(mail_file, current_position_in_mail, SEEK_SET);
246 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
247 last_mbox_line_was_empty = 1;
248 printf("%s", raw_mbox_line);
250 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
251 (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
252 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
253 printf("%s", raw_mbox_line);
260 for(n = 0; n < nb_search_requests; n++) { hits[n] = 0; }
262 position_in_file_string = mbox_value;
263 mail_filename = segment_next_field(mbox_value);
264 current_position_in_mail = atol(position_in_file_string);
265 strcpy(current_mail_filename, mail_filename);
267 remove_eof(current_mail_filename);
273 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
274 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
278 for(n = 0; n < nb_search_requests; n++) {
279 hits[n] |= mbox_line_match_search(&search_requests[n],
280 mbox_id, mbox_value);
286 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
287 int nb_search_requests, struct search_request *search_requests) {
289 struct dirent *dir_e;
291 char raw_db_line[BUFFER_SIZE];
292 char subname[PATH_MAX + 1];
294 if(lstat(entry_name, &sb) != 0) {
296 "mymail: Cannot stat \"%s\": %s\n",
302 dir = opendir(entry_name);
305 while((dir_e = readdir(dir))) {
306 if(!ignore_entry(dir_e->d_name)) {
307 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
308 recursive_search_in_db(subname, db_filename_regexp,
309 nb_search_requests, search_requests);
316 const char *s = entry_name, *filename = entry_name;
317 while(*s) { if(*s == '/') { filename = s+1; } s++; }
319 if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
320 FILE *db_file = fopen(entry_name, "r");
324 "mymail: Cannot open \"%s\" for reading: %s\n",
330 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
331 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
333 "mymail: Header line in '%s' does not match the mymail db format.\n",
339 "mymail: Cannot read the header line in '%s'.\n",
344 search_in_db(nb_search_requests, search_requests, db_file);
351 /*********************************************************************/
353 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
354 char *raw_mbox_line, FILE *db_file) {
357 for(f = 0; f < nb_fields_to_parse; f++) {
358 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
359 fprintf(db_file, "%s %s\n",
360 field_names[fields_to_parse[f].id],
361 raw_mbox_line + matches.rm_eo);
366 void index_mbox(const char *mbox_filename,
367 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
369 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
370 char *end_of_full_line;
372 int in_header, new_header, last_mbox_line_was_empty;
373 unsigned long int position_in_file;
375 file = fopen(mbox_filename, "r");
378 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
379 if(paranoid) { exit(EXIT_FAILURE); }
386 position_in_file = 0;
387 end_of_full_line = 0;
389 last_mbox_line_was_empty = 1;
391 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
392 if(last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0) {
395 "Got a ^\"From \" in the header in %s:%lu.\n",
396 mbox_filename, position_in_file);
397 fprintf(stderr, "%s", raw_mbox_line);
398 if(paranoid) { exit(EXIT_FAILURE); }
402 } else if(raw_mbox_line[0] == '\n') {
403 if(in_header) { in_header = 0; }
406 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
410 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
414 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
415 char *start = raw_mbox_line;
416 while(*start == ' ' || *start == '\t') start++;
417 *(end_of_full_line++) = ' ';
418 strcpy(end_of_full_line, start);
419 while(*end_of_full_line && *end_of_full_line != '\n') {
422 *end_of_full_line = '\0';
427 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
428 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
430 "Header line syntax error %s:%lu.\n",
431 mbox_filename, position_in_file);
432 fprintf(stderr, "%s", raw_mbox_line);
437 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
440 end_of_full_line = full_line;
441 strcpy(end_of_full_line, raw_mbox_line);
442 while(*end_of_full_line && *end_of_full_line != '\n') {
445 *end_of_full_line = '\0';
450 position_in_file += strlen(raw_mbox_line);
456 void recursive_index_mbox(FILE *db_file,
457 const char *entry_name,
458 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
460 struct dirent *dir_e;
462 char subname[PATH_MAX + 1];
464 if(lstat(entry_name, &sb) != 0) {
466 "mymail: Cannot stat \"%s\": %s\n",
472 dir = opendir(entry_name);
475 while((dir_e = readdir(dir))) {
476 if(!ignore_entry(dir_e->d_name)) {
477 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
478 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
483 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
487 /*********************************************************************/
489 /* For long options that have no equivalent short option, use a
490 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
492 OPT_BASH_MODE = CHAR_MAX + 1
495 static struct option long_options[] = {
496 { "help", no_argument, 0, 'h' },
497 { "version", no_argument, 0, 'v' },
498 { "db-file", 1, 0, 'd' },
499 { "db-pattern", 1, 0, 'p' },
500 { "db-root", 1, 0, 'r' },
501 { "db-list", 1, 0, 'l' },
502 { "search", 1, 0, 's' },
503 { "index", 0, 0, 'i' },
507 /*********************************************************************/
509 int main(int argc, char **argv) {
510 int error = 0, show_help = 0;
511 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
514 int nb_search_requests;
515 char *search_request_strings[MAX_NB_SEARCH_REQUESTS];
517 /* for(f = 0; f < argc; f++) { */
518 /* printf("arg %d \"%s\"\n", f, argv[f]); */
525 db_filename_list = 0;
527 setlocale(LC_ALL, "");
529 nb_search_requests = 0;
531 while ((c = getopt_long(argc, argv, "hvip:s:d:r:l:",
532 long_options, NULL)) != -1) {
541 print_version(stdout);
549 db_filename = strdup(optarg);
553 db_filename_regexp_string = strdup(optarg);
557 db_root_path = strdup(optarg);
561 db_filename_list = strdup(optarg);
565 if(nb_search_requests == MAX_NB_SEARCH_REQUESTS) {
566 fprintf(stderr, "mymail: Too many search patterns.\n");
569 search_request_strings[nb_search_requests++] = strdup(optarg);
579 char *default_db_filename = getenv("MYMAIL_DB_FILE");
581 if(!default_db_filename) {
582 default_db_filename = "mymail.db";
585 db_filename = strdup(default_db_filename);
588 if(!db_filename_regexp_string) {
589 char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
591 if(!default_db_filename_regexp_string) {
592 default_db_filename_regexp_string = "^mymail.db$";
595 db_filename_regexp_string = strdup(default_db_filename_regexp_string);
599 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
601 if(default_db_root_path) {
602 db_root_path = strdup(default_db_root_path);
606 if(!db_filename_list) {
607 char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
609 if(default_db_filename_list) {
610 db_filename_list = strdup(default_db_filename_list);
627 db_file = fopen(db_filename, "w");
631 "mymail: Cannot open \"%s\" for writing: %s\n",
637 for(f = 0; f < nb_fields_to_parse; f++) {
638 if(regcomp(&fields_to_parse[f].regexp,
639 fields_to_parse[f].regexp_string,
642 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
643 fields_to_parse[f].regexp_string,
644 field_names[fields_to_parse[f].id]);
649 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
651 while(optind < argc) {
652 recursive_index_mbox(db_file,
654 nb_fields_to_parse, fields_to_parse);
660 for(f = 0; f < nb_fields_to_parse; f++) {
661 regfree(&fields_to_parse[f].regexp);
667 if(nb_search_requests > 0) {
668 struct search_request search_requests[MAX_NB_SEARCH_REQUESTS];
669 char *search_field, *search_regexp_string;
672 for(n = 0; n < nb_search_requests; n++) {
673 search_field = search_request_strings[n];
674 search_regexp_string = segment_next_field(search_request_strings[n]);
676 if(search_field[0] == '!') {
678 search_requests[n].negation = 1;
680 search_requests[n].negation = 0;
683 search_requests[n].field_id = -1;
684 for(m = 0; (m < MAX_ID) && search_requests[n].field_id == -1; m++) {
685 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
686 search_requests[n].field_id = m;
690 if(search_requests[n].field_id == -1) {
692 "mymail: Syntax error in field name \"%s\".\n",
697 if(regcomp(&search_requests[n].regexp,
698 search_regexp_string,
701 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
702 search_regexp_string,
703 field_names[search_requests[n].field_id]);
708 /* Recursive search if db_root_path is set */
711 regex_t db_filename_regexp;
712 if(regcomp(&db_filename_regexp,
713 db_filename_regexp_string,
716 "mymail: Syntax error in regexp \"%s\".\n",
717 db_filename_regexp_string);
721 recursive_search_in_db(db_root_path, &db_filename_regexp,
722 nb_search_requests, search_requests);
724 regfree(&db_filename_regexp);
727 /* Search in all db files listed in db_filename_list */
729 if(db_filename_list) {
730 char db_filename[PATH_MAX + 1];
734 s = db_filename_list;
738 while(*s == ';') { s++; }
739 while(*s && *s != ';') { *t++ = *s++; }
743 db_file = fopen(db_filename, "r");
747 "mymail: Cannot open \"%s\" for reading: %s\n",
753 search_in_db(nb_search_requests, search_requests, db_file);
760 /* Search in all db files listed in the command arguments */
762 while(optind < argc) {
763 FILE *db_file = fopen(argv[optind], "r");
767 "mymail: Cannot open \"%s\" for reading: %s\n",
773 search_in_db(nb_search_requests, search_requests, db_file);
779 for(n = 0; n < nb_search_requests; n++) {
780 regfree(&search_requests[n].regexp);
781 free(search_request_strings[n]);
787 free(db_filename_regexp_string);
789 free(db_filename_list);