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
63 static char *field_names[] = {
71 struct search_request {
77 struct parsable_field {
89 char *segment_next_field(char *current) {
90 while(*current && *current != ' ') current++;
91 *current = '\0'; current++;
92 while(*current && *current == ' ') current++;
96 void remove_eof(char *c) {
97 while(*c && *c != '\n' && *c != '\r') c++;
101 /********************************************************************/
103 /* malloc with error checking. */
105 void *safe_malloc(size_t n) {
109 "mymail: cannot allocate memory: %s\n", strerror(errno));
115 /*********************************************************************/
117 void print_version(FILE *out) {
118 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
121 void print_usage(FILE *out) {
123 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
125 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]]\n");
127 fprintf(out, " -h, --help\n");
128 fprintf(out, " show this help\n");
129 fprintf(out, " -v, --version\n");
130 fprintf(out, " print the version number\n");
131 fprintf(out, " -i, --index\n");
132 fprintf(out, " index mails\n");
133 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
134 fprintf(out, " search for matching mails in the data-base file\n");
135 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
136 fprintf(out, " set the data-base file\n");
137 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
138 fprintf(out, " set the data-base root path for recursive search\n");
141 /*********************************************************************/
143 int ignore_entry(const char *name) {
145 /* strcmp(name, ".") == 0 || */
146 /* strcmp(name, "..") == 0 || */
147 (name[0] == '.' && name[1] != '/');
150 int mbox_line_match_search(struct search_request *request,
151 int mbox_id, char *mbox_value) {
153 (request->field_id == mbox_id ||
154 (request->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_DEST)))
156 regexec(&request->regexp, mbox_value, 0, 0, 0) == 0;
159 void search_in_db(int nb_search_patterns,
160 struct search_request *search_requests,
162 int hits[MAX_NB_SEARCH_PATTERNS];
163 char raw_db_line[BUFFER_SIZE];
164 char raw_mbox_line[BUFFER_SIZE];
165 char current_mail_filename[PATH_MAX + 1];
166 unsigned long int current_position_in_mail;
167 char *mbox_name, *mbox_value;
169 int already_written, m, n;
171 current_position_in_mail = 0;
174 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
176 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
177 mbox_name = raw_db_line;
178 mbox_value = segment_next_field(raw_db_line);
180 if(strcmp("mail", mbox_name) == 0) {
181 char *position_in_file_string;
184 for(n = 0; n < nb_search_patterns &&
185 ((hits[n] && !search_requests[n].negation) ||
186 (!hits[n] && search_requests[n].negation)); n++);
188 if(n == nb_search_patterns) {
191 mail_file = fopen(current_mail_filename, "r");
194 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
198 fseek(mail_file, current_position_in_mail, SEEK_SET);
200 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
201 printf("%s", raw_mbox_line);
202 while(fgets(raw_mbox_line, BUFFER_SIZE, mail_file) &&
203 strncmp(raw_mbox_line, "From ", 5)) {
204 printf("%s", raw_mbox_line);
211 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
213 position_in_file_string = mbox_value;
214 mail_filename = segment_next_field(mbox_value);
215 current_position_in_mail = atol(position_in_file_string);
216 strcpy(current_mail_filename, mail_filename);
218 remove_eof(current_mail_filename);
224 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
225 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
229 for(n = 0; n < nb_search_patterns; n++) {
230 hits[n] |= mbox_line_match_search(&search_requests[n],
231 mbox_id, mbox_value);
237 void recursive_search_in_db(const char *entry_name,
238 int nb_search_patterns,
239 struct search_request *search_requests) {
241 struct dirent *dir_e;
243 char raw_db_line[BUFFER_SIZE];
244 char subname[PATH_MAX + 1];
246 if(lstat(entry_name, &sb) != 0) {
248 "mymail: Cannot stat \"%s\": %s\n",
254 dir = opendir(entry_name);
257 while((dir_e = readdir(dir))) {
258 if(!ignore_entry(dir_e->d_name)) {
259 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
260 recursive_search_in_db(subname,
267 const char *s = entry_name, *filename = entry_name;
268 while(*s) { if(*s == '/') { filename = s+1; } s++; }
270 if(strcmp(filename, db_filename) == 0) {
271 FILE *db_file = fopen(entry_name, "r");
275 "mymail: Cannot open \"%s\" for reading: %s\n",
281 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
282 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
284 "mymail: Header line in '%s' does not match the mymail db format.\n",
290 "mymail: Cannot read the header line in '%s'.\n",
295 search_in_db(nb_search_patterns, search_requests, db_file);
302 /*********************************************************************/
304 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
305 char *raw_mbox_line, FILE *db_file) {
308 for(f = 0; f < nb_fields_to_parse; f++) {
309 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
310 fprintf(db_file, "%s %s\n",
311 field_names[fields_to_parse[f].id],
312 raw_mbox_line + matches.rm_eo);
317 void index_mbox(const char *mbox_filename,
318 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
320 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
321 char *end_of_full_line;
323 int in_header, new_header;
324 unsigned long int position_in_file;
326 file = fopen(mbox_filename, "r");
329 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
330 if(paranoid) { exit(EXIT_FAILURE); }
337 position_in_file = 0;
338 end_of_full_line = 0;
341 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
342 if(strncmp(raw_mbox_line, "From ", 5) == 0) {
345 "Got a ^\"From \" in the header in %s:%lu.\n",
346 mbox_filename, position_in_file);
347 fprintf(stderr, "%s", raw_mbox_line);
348 if(paranoid) { exit(EXIT_FAILURE); }
352 } else if(strncmp(raw_mbox_line, "\n", 1) == 0) {
353 if(in_header) { in_header = 0; }
358 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
362 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
363 char *start = raw_mbox_line;
364 while(*start == ' ' || *start == '\t') start++;
365 *(end_of_full_line++) = ' ';
366 strcpy(end_of_full_line, start);
367 while(*end_of_full_line && *end_of_full_line != '\n') {
370 *end_of_full_line = '\0';
375 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
376 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
378 "Header line syntax error %s:%lu.\n",
379 mbox_filename, position_in_file);
380 fprintf(stderr, "%s", raw_mbox_line);
385 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
388 end_of_full_line = full_line;
389 strcpy(end_of_full_line, raw_mbox_line);
390 while(*end_of_full_line && *end_of_full_line != '\n') {
393 *end_of_full_line = '\0';
398 position_in_file += strlen(raw_mbox_line);
404 void recursive_index_mbox(FILE *db_file,
405 const char *entry_name,
406 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
408 struct dirent *dir_e;
410 char subname[PATH_MAX + 1];
412 if(lstat(entry_name, &sb) != 0) {
414 "mymail: Cannot stat \"%s\": %s\n",
420 dir = opendir(entry_name);
423 while((dir_e = readdir(dir))) {
424 if(!ignore_entry(dir_e->d_name)) {
425 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
426 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
431 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
435 /*********************************************************************/
437 /* For long options that have no equivalent short option, use a
438 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
440 OPT_BASH_MODE = CHAR_MAX + 1
443 static struct option long_options[] = {
444 { "help", no_argument, 0, 'h' },
445 { "version", no_argument, 0, 'v' },
446 { "db-file", 1, 0, 'd' },
447 { "db-root", 1, 0, 'r' },
448 { "search", 1, 0, 's' },
449 { "index", 0, 0, 'i' },
453 static struct parsable_field fields_to_parse[] = {
456 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
457 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
462 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
463 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
468 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: *",
469 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
474 /*********************************************************************/
476 int main(int argc, char **argv) {
477 int error = 0, show_help = 0;
478 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
481 int nb_search_patterns;
482 char *search_pattern[MAX_NB_SEARCH_PATTERNS];
484 /* for(f = 0; f < argc; f++) { */
485 /* printf("arg %d \"%s\"\n", f, argv[f]); */
493 setlocale(LC_ALL, "");
495 nb_search_patterns = 0;
497 while ((c = getopt_long(argc, argv, "hvip:s:d:r:",
498 long_options, NULL)) != -1) {
507 print_version(stdout);
515 db_filename = strdup(optarg);
519 db_root_path = strdup(optarg);
523 if(nb_search_patterns == MAX_NB_SEARCH_PATTERNS) {
524 fprintf(stderr, "mymail: Too many search patterns.\n");
527 search_pattern[nb_search_patterns++] = strdup(optarg);
537 char *default_db_filename = getenv("MYMAIL_DB_FILE");
539 if(!default_db_filename) {
540 default_db_filename = "mymail.db";
543 db_filename = strdup(default_db_filename);
547 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
549 if(default_db_root_path) {
550 db_root_path = strdup(default_db_root_path);
556 "mymail: db root path is not set\n");
574 db_file = fopen(db_filename, "w");
578 "mymail: Cannot open \"%s\" for writing: %s\n",
584 for(f = 0; f < nb_fields_to_parse; f++) {
585 if(regcomp(&fields_to_parse[f].regexp,
586 fields_to_parse[f].regexp_string,
589 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
590 fields_to_parse[f].regexp_string,
591 field_names[fields_to_parse[f].id]);
596 fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
598 while(optind < argc) {
599 recursive_index_mbox(db_file,
601 nb_fields_to_parse, fields_to_parse);
607 for(f = 0; f < nb_fields_to_parse; f++) {
608 regfree(&fields_to_parse[f].regexp);
614 if(nb_search_patterns > 0) {
615 struct search_request search_requests[MAX_NB_SEARCH_PATTERNS];
616 char *search_regexp_string;
619 for(n = 0; n < nb_search_patterns; n++) {
620 search_regexp_string = segment_next_field(search_pattern[n]);
622 if(search_pattern[n][0] == '!') {
624 search_requests[n].negation = 1;
626 search_requests[n].negation = 0;
629 search_requests[n].field_id = -1;
630 for(m = 0; (m < MAX_ID) && search_requests[n].field_id == -1; m++) {
631 if(strncmp(field_names[m], search_pattern[n], strlen(search_pattern[n])) == 0) {
632 search_requests[n].field_id = m;
636 if(regcomp(&search_requests[n].regexp,
637 search_regexp_string,
640 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
641 search_regexp_string,
642 field_names[search_requests[n].field_id]);
647 recursive_search_in_db(db_root_path,
648 nb_search_patterns, search_requests);
650 for(n = 0; n < nb_search_patterns; n++) {
651 free(search_pattern[n]);