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_PATTERNS 10
52 #define BUFFER_SIZE 65536
60 /********************************************************************/
71 static char *field_names[] = {
79 /********************************************************************/
81 struct search_request {
87 /********************************************************************/
89 struct parsable_field {
95 static struct parsable_field fields_to_parse[] = {
98 "^\\(From \\|[Ff][Rr][Oo][Mm]:\\|[R][r][E][e][P][p][L][l][Y][y]-[T][t][O][o]:\\)",
99 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
104 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
105 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
110 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
111 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
116 /********************************************************************/
118 int xor(int a, int b) {
119 return (a && !b) || (!a && b);
122 char *segment_next_field(char *current) {
123 while(*current && *current != ' ') current++;
124 *current = '\0'; current++;
125 while(*current && *current == ' ') current++;
129 void remove_eof(char *c) {
130 while(*c && *c != '\n' && *c != '\r') c++;
134 /********************************************************************/
136 /* malloc with error checking. */
138 void *safe_malloc(size_t n) {
142 "mymail: cannot allocate memory: %s\n", strerror(errno));
148 /*********************************************************************/
150 void print_version(FILE *out) {
151 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
154 void print_usage(FILE *out) {
156 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
158 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]]\n");
160 fprintf(out, " -h, --help\n");
161 fprintf(out, " show this help\n");
162 fprintf(out, " -v, --version\n");
163 fprintf(out, " print the version number\n");
164 fprintf(out, " -i, --index\n");
165 fprintf(out, " index mails\n");
166 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
167 fprintf(out, " search for matching mails in the data-base file\n");
168 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
169 fprintf(out, " set the data-base file\n");
170 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
171 fprintf(out, " set the data-base root path for recursive search\n");
174 /*********************************************************************/
176 int ignore_entry(const char *name) {
178 /* strcmp(name, ".") == 0 || */
179 /* strcmp(name, "..") == 0 || */
180 (name[0] == '.' && name[1] != '/');
183 int mbox_line_match_search(struct search_request *request,
184 int mbox_id, char *mbox_value) {
186 (request->field_id == mbox_id ||
187 (request->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_DEST)))
189 regexec(&request->regexp, mbox_value, 0, 0, 0) == 0;
192 void search_in_db(int nb_search_patterns,
193 struct search_request *search_requests,
195 int hits[MAX_NB_SEARCH_PATTERNS];
196 char raw_db_line[BUFFER_SIZE];
197 char raw_mbox_line[BUFFER_SIZE];
198 char current_mail_filename[PATH_MAX + 1];
199 unsigned long int current_position_in_mail;
200 char *mbox_name, *mbox_value;
202 int already_written, m, n;
203 int last_mbox_line_was_empty;
205 current_position_in_mail = 0;
208 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
210 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
211 mbox_name = raw_db_line;
212 mbox_value = segment_next_field(raw_db_line);
214 if(strcmp("mail", mbox_name) == 0) {
215 char *position_in_file_string;
218 for(n = 0; n < nb_search_patterns && xor(hits[n], search_requests[n].negation); n++);
220 /* for(n = 0; n < nb_search_patterns && */
221 /* ((hits[n] && !search_requests[n].negation) || */
222 /* (!hits[n] && search_requests[n].negation)); n++); */
224 if(n == nb_search_patterns) {
227 mail_file = fopen(current_mail_filename, "r");
230 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
234 fseek(mail_file, current_position_in_mail, SEEK_SET);
236 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
237 last_mbox_line_was_empty = 1;
238 printf("%s", raw_mbox_line);
240 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
241 (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
242 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
243 printf("%s", raw_mbox_line);
250 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
252 position_in_file_string = mbox_value;
253 mail_filename = segment_next_field(mbox_value);
254 current_position_in_mail = atol(position_in_file_string);
255 strcpy(current_mail_filename, mail_filename);
257 remove_eof(current_mail_filename);
263 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
264 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
268 for(n = 0; n < nb_search_patterns; n++) {
269 hits[n] |= mbox_line_match_search(&search_requests[n],
270 mbox_id, mbox_value);
276 void recursive_search_in_db(const char *entry_name,
277 int nb_search_patterns,
278 struct search_request *search_requests) {
280 struct dirent *dir_e;
282 char raw_db_line[BUFFER_SIZE];
283 char subname[PATH_MAX + 1];
285 if(lstat(entry_name, &sb) != 0) {
287 "mymail: Cannot stat \"%s\": %s\n",
293 dir = opendir(entry_name);
296 while((dir_e = readdir(dir))) {
297 if(!ignore_entry(dir_e->d_name)) {
298 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
299 recursive_search_in_db(subname,
306 const char *s = entry_name, *filename = entry_name;
307 while(*s) { if(*s == '/') { filename = s+1; } s++; }
309 if(strcmp(filename, db_filename) == 0) {
310 FILE *db_file = fopen(entry_name, "r");
314 "mymail: Cannot open \"%s\" for reading: %s\n",
320 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
321 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
323 "mymail: Header line in '%s' does not match the mymail db format.\n",
329 "mymail: Cannot read the header line in '%s'.\n",
334 search_in_db(nb_search_patterns, search_requests, db_file);
341 /*********************************************************************/
343 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
344 char *raw_mbox_line, FILE *db_file) {
347 for(f = 0; f < nb_fields_to_parse; f++) {
348 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
349 fprintf(db_file, "%s %s\n",
350 field_names[fields_to_parse[f].id],
351 raw_mbox_line + matches.rm_eo);
356 void index_mbox(const char *mbox_filename,
357 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
359 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
360 char *end_of_full_line;
362 int in_header, new_header, last_mbox_line_was_empty;
363 unsigned long int position_in_file;
365 file = fopen(mbox_filename, "r");
368 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
369 if(paranoid) { exit(EXIT_FAILURE); }
376 position_in_file = 0;
377 end_of_full_line = 0;
379 last_mbox_line_was_empty = 1;
381 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
382 if(last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0) {
385 "Got a ^\"From \" in the header in %s:%lu.\n",
386 mbox_filename, position_in_file);
387 fprintf(stderr, "%s", raw_mbox_line);
388 if(paranoid) { exit(EXIT_FAILURE); }
392 } else if(raw_mbox_line[0] == '\n') {
393 if(in_header) { in_header = 0; }
396 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
400 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
404 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
405 char *start = raw_mbox_line;
406 while(*start == ' ' || *start == '\t') start++;
407 *(end_of_full_line++) = ' ';
408 strcpy(end_of_full_line, start);
409 while(*end_of_full_line && *end_of_full_line != '\n') {
412 *end_of_full_line = '\0';
417 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
418 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
420 "Header line syntax error %s:%lu.\n",
421 mbox_filename, position_in_file);
422 fprintf(stderr, "%s", raw_mbox_line);
427 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
430 end_of_full_line = full_line;
431 strcpy(end_of_full_line, raw_mbox_line);
432 while(*end_of_full_line && *end_of_full_line != '\n') {
435 *end_of_full_line = '\0';
440 position_in_file += strlen(raw_mbox_line);
446 void recursive_index_mbox(FILE *db_file,
447 const char *entry_name,
448 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
450 struct dirent *dir_e;
452 char subname[PATH_MAX + 1];
454 if(lstat(entry_name, &sb) != 0) {
456 "mymail: Cannot stat \"%s\": %s\n",
462 dir = opendir(entry_name);
465 while((dir_e = readdir(dir))) {
466 if(!ignore_entry(dir_e->d_name)) {
467 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
468 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
473 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
477 /*********************************************************************/
479 /* For long options that have no equivalent short option, use a
480 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
482 OPT_BASH_MODE = CHAR_MAX + 1
485 static struct option long_options[] = {
486 { "help", no_argument, 0, 'h' },
487 { "version", no_argument, 0, 'v' },
488 { "db-file", 1, 0, 'd' },
489 { "db-root", 1, 0, 'r' },
490 { "search", 1, 0, 's' },
491 { "index", 0, 0, 'i' },
495 /*********************************************************************/
497 int main(int argc, char **argv) {
498 int error = 0, show_help = 0;
499 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
502 int nb_search_patterns;
503 char *search_pattern[MAX_NB_SEARCH_PATTERNS];
505 /* for(f = 0; f < argc; f++) { */
506 /* printf("arg %d \"%s\"\n", f, argv[f]); */
514 setlocale(LC_ALL, "");
516 nb_search_patterns = 0;
518 while ((c = getopt_long(argc, argv, "hvip:s:d:r:",
519 long_options, NULL)) != -1) {
528 print_version(stdout);
536 db_filename = strdup(optarg);
540 db_root_path = strdup(optarg);
544 if(nb_search_patterns == MAX_NB_SEARCH_PATTERNS) {
545 fprintf(stderr, "mymail: Too many search patterns.\n");
548 search_pattern[nb_search_patterns++] = strdup(optarg);
558 char *default_db_filename = getenv("MYMAIL_DB_FILE");
560 if(!default_db_filename) {
561 default_db_filename = "mymail.db";
564 db_filename = strdup(default_db_filename);
568 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
570 if(default_db_root_path) {
571 db_root_path = strdup(default_db_root_path);
577 "mymail: db root path is not set\n");
595 db_file = fopen(db_filename, "w");
599 "mymail: Cannot open \"%s\" for writing: %s\n",
605 for(f = 0; f < nb_fields_to_parse; f++) {
606 if(regcomp(&fields_to_parse[f].regexp,
607 fields_to_parse[f].regexp_string,
610 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
611 fields_to_parse[f].regexp_string,
612 field_names[fields_to_parse[f].id]);
617 fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
619 while(optind < argc) {
620 recursive_index_mbox(db_file,
622 nb_fields_to_parse, fields_to_parse);
628 for(f = 0; f < nb_fields_to_parse; f++) {
629 regfree(&fields_to_parse[f].regexp);
635 if(nb_search_patterns > 0) {
636 struct search_request search_requests[MAX_NB_SEARCH_PATTERNS];
637 char *search_regexp_string;
640 for(n = 0; n < nb_search_patterns; n++) {
641 search_regexp_string = segment_next_field(search_pattern[n]);
643 if(search_pattern[n][0] == '!') {
645 search_requests[n].negation = 1;
647 search_requests[n].negation = 0;
650 search_requests[n].field_id = -1;
651 for(m = 0; (m < MAX_ID) && search_requests[n].field_id == -1; m++) {
652 if(strncmp(field_names[m], search_pattern[n], strlen(search_pattern[n])) == 0) {
653 search_requests[n].field_id = m;
657 if(regcomp(&search_requests[n].regexp,
658 search_regexp_string,
661 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
662 search_regexp_string,
663 field_names[search_requests[n].field_id]);
668 recursive_search_in_db(db_root_path,
669 nb_search_patterns, search_requests);
671 for(n = 0; n < nb_search_patterns; n++) {
672 free(search_pattern[n]);