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 parsable_field {
83 char *segment_next_field(char *current) {
84 while(*current && *current != ' ') current++;
85 *current = '\0'; current++;
86 while(*current && *current == ' ') current++;
90 void remove_eof(char *c) {
91 while(*c && *c != '\n' && *c != '\r') c++;
95 /********************************************************************/
97 /* malloc with error checking. */
99 void *safe_malloc(size_t n) {
103 "mymail: cannot allocate memory: %s\n", strerror(errno));
109 /*********************************************************************/
111 void print_version(FILE *out) {
112 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
115 void print_usage(FILE *out) {
117 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
119 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]]\n");
121 fprintf(out, " -h, --help\n");
122 fprintf(out, " show this help\n");
123 fprintf(out, " -v, --version\n");
124 fprintf(out, " print the version number\n");
125 fprintf(out, " -i, --index\n");
126 fprintf(out, " index mails\n");
127 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
128 fprintf(out, " search for matching mails in the data-base file\n");
129 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
130 fprintf(out, " set the data-base file\n");
131 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
132 fprintf(out, " set the data-base root path for recursive search\n");
135 /*********************************************************************/
137 int ignore_entry(const char *name) {
139 /* strcmp(name, ".") == 0 || */
140 /* strcmp(name, "..") == 0 || */
141 (name[0] == '.' && name[1] != '/');
144 int mbox_line_match_search(int search_id, regex_t *search_regexp,
145 int mbox_id, char *mbox_value) {
147 (search_id == mbox_id ||
148 (search_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_DEST)))
150 regexec(search_regexp, mbox_value, 0, 0, 0) == 0;
153 void search_in_db(int nb_search_patterns,
154 int *search_ids, char **search_regexp_strings,
156 int hits[MAX_NB_SEARCH_PATTERNS];
157 char raw_db_line[BUFFER_SIZE];
158 char raw_mbox_line[BUFFER_SIZE];
159 char current_mail_filename[PATH_MAX + 1];
160 unsigned long int current_position_in_mail;
161 char *mbox_name, *mbox_value;
163 regex_t search_regexps[MAX_NB_SEARCH_PATTERNS];
164 int already_written, m, n;
166 for(n = 0; n < nb_search_patterns; n++) {
167 if(regcomp(&search_regexps[n],
168 search_regexp_strings[n],
171 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
172 search_regexp_strings[n],
173 field_names[search_ids[n]]);
178 current_position_in_mail = 0;
181 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
183 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
184 mbox_name = raw_db_line;
185 mbox_value = segment_next_field(raw_db_line);
187 if(strcmp("mail", mbox_name) == 0) {
188 char *position_in_file_string;
191 for(n = 0; n < nb_search_patterns && hits[n]; n++);
193 if(n == nb_search_patterns) {
196 mail_file = fopen(current_mail_filename, "r");
199 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
203 fseek(mail_file, current_position_in_mail, SEEK_SET);
205 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
206 printf("%s", raw_mbox_line);
207 while(fgets(raw_mbox_line, BUFFER_SIZE, mail_file) &&
208 strncmp(raw_mbox_line, "From ", 5)) {
209 printf("%s", raw_mbox_line);
216 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
218 position_in_file_string = mbox_value;
219 mail_filename = segment_next_field(mbox_value);
220 current_position_in_mail = atol(position_in_file_string);
221 strcpy(current_mail_filename, mail_filename);
223 remove_eof(current_mail_filename);
229 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
230 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
234 for(n = 0; n < nb_search_patterns; n++) {
235 hits[n] |= mbox_line_match_search(search_ids[n], &search_regexps[n],
236 mbox_id, mbox_value);
241 for(n = 0; n < nb_search_patterns; n++) {
242 regfree(&search_regexps[n]);
246 void recursive_search_in_db(const char *entry_name,
247 int nb_search_patterns,
248 int *search_ids, char **search_regexp_strings) {
250 struct dirent *dir_e;
252 char raw_db_line[BUFFER_SIZE];
253 char subname[PATH_MAX + 1];
255 if(lstat(entry_name, &sb) != 0) {
257 "mymail: Cannot stat \"%s\": %s\n",
263 dir = opendir(entry_name);
266 while((dir_e = readdir(dir))) {
267 if(!ignore_entry(dir_e->d_name)) {
268 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
269 recursive_search_in_db(subname,
271 search_ids, search_regexp_strings);
276 const char *s = entry_name, *filename = entry_name;
277 while(*s) { if(*s == '/') { filename = s+1; } s++; }
279 if(strcmp(filename, db_filename) == 0) {
280 FILE *db_file = fopen(entry_name, "r");
284 "mymail: Cannot open \"%s\" for reading: %s\n",
290 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
291 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
293 "mymail: Header line in '%s' does not match the mymail db format.\n",
299 "mymail: Cannot read the header line in '%s'.\n",
304 search_in_db(nb_search_patterns, search_ids, search_regexp_strings,
312 /*********************************************************************/
314 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
315 char *raw_mbox_line, FILE *db_file) {
318 for(f = 0; f < nb_fields_to_parse; f++) {
319 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
320 fprintf(db_file, "%s %s\n",
321 field_names[fields_to_parse[f].id],
322 raw_mbox_line + matches.rm_eo);
327 void index_mbox(const char *mbox_filename,
328 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
330 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
331 char *end_of_full_line;
333 int in_header, new_header;
334 unsigned long int position_in_file;
336 file = fopen(mbox_filename, "r");
339 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
340 if(paranoid) { exit(EXIT_FAILURE); }
347 position_in_file = 0;
348 end_of_full_line = 0;
351 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
352 if(strncmp(raw_mbox_line, "From ", 5) == 0) {
355 "Got a ^\"From \" in the header in %s:%lu.\n",
356 mbox_filename, position_in_file);
357 fprintf(stderr, "%s", raw_mbox_line);
358 if(paranoid) { exit(EXIT_FAILURE); }
362 } else if(strncmp(raw_mbox_line, "\n", 1) == 0) {
363 if(in_header) { in_header = 0; }
368 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
372 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
373 char *start = raw_mbox_line;
374 while(*start == ' ' || *start == '\t') start++;
375 *(end_of_full_line++) = ' ';
376 strcpy(end_of_full_line, start);
377 while(*end_of_full_line && *end_of_full_line != '\n') {
380 *end_of_full_line = '\0';
385 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
386 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
388 "Header line syntax error %s:%lu.\n",
389 mbox_filename, position_in_file);
390 fprintf(stderr, "%s", raw_mbox_line);
395 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
398 end_of_full_line = full_line;
399 strcpy(end_of_full_line, raw_mbox_line);
400 while(*end_of_full_line && *end_of_full_line != '\n') {
403 *end_of_full_line = '\0';
408 position_in_file += strlen(raw_mbox_line);
414 void recursive_index_mbox(FILE *db_file,
415 const char *entry_name,
416 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
418 struct dirent *dir_e;
420 char subname[PATH_MAX + 1];
422 if(lstat(entry_name, &sb) != 0) {
424 "mymail: Cannot stat \"%s\": %s\n",
430 dir = opendir(entry_name);
433 while((dir_e = readdir(dir))) {
434 if(!ignore_entry(dir_e->d_name)) {
435 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
436 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
441 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
445 /*********************************************************************/
447 /* For long options that have no equivalent short option, use a
448 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
450 OPT_BASH_MODE = CHAR_MAX + 1
453 static struct option long_options[] = {
454 { "help", no_argument, 0, 'h' },
455 { "version", no_argument, 0, 'v' },
456 { "db-file", 1, 0, 'd' },
457 { "db-root", 1, 0, 'r' },
458 { "search", 1, 0, 's' },
459 { "index", 0, 0, 'i' },
463 static struct parsable_field fields_to_parse[] = {
466 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
467 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
472 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
473 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
478 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: *",
479 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
484 /*********************************************************************/
486 int main(int argc, char **argv) {
487 int error = 0, show_help = 0;
488 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
491 int nb_search_patterns;
492 char *search_pattern[MAX_NB_SEARCH_PATTERNS];
494 /* for(f = 0; f < argc; f++) { */
495 /* printf("arg %d \"%s\"\n", f, argv[f]); */
503 setlocale(LC_ALL, "");
505 nb_search_patterns = 0;
507 while ((c = getopt_long(argc, argv, "hvip:s:d:r:",
508 long_options, NULL)) != -1) {
517 print_version(stdout);
525 db_filename = strdup(optarg);
529 db_root_path = strdup(optarg);
533 if(nb_search_patterns == MAX_NB_SEARCH_PATTERNS) {
534 fprintf(stderr, "mymail: Too many search patterns.\n");
537 search_pattern[nb_search_patterns++] = strdup(optarg);
547 char *default_db_filename = getenv("MYMAIL_DB_FILE");
549 if(!default_db_filename) {
550 default_db_filename = "mymail.db";
553 db_filename = strdup(default_db_filename);
557 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
559 if(default_db_root_path) {
560 db_root_path = strdup(default_db_root_path);
566 "mymail: db root path is not set\n");
584 db_file = fopen(db_filename, "w");
588 "mymail: Cannot open \"%s\" for writing: %s\n",
594 for(f = 0; f < nb_fields_to_parse; f++) {
595 if(regcomp(&fields_to_parse[f].regexp,
596 fields_to_parse[f].regexp_string,
599 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
600 fields_to_parse[f].regexp_string,
601 field_names[fields_to_parse[f].id]);
606 fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
608 while(optind < argc) {
609 recursive_index_mbox(db_file,
611 nb_fields_to_parse, fields_to_parse);
617 for(f = 0; f < nb_fields_to_parse; f++) {
618 regfree(&fields_to_parse[f].regexp);
624 if(nb_search_patterns > 0) {
625 int search_ids[MAX_NB_SEARCH_PATTERNS];
626 char *search_regexp_strings[MAX_NB_SEARCH_PATTERNS];
629 for(n = 0; n < nb_search_patterns; n++) {
630 search_regexp_strings[n] = segment_next_field(search_pattern[n]);
632 for(m = 0; (m < MAX_ID) && search_ids[n] == -1; m++) {
633 if(strncmp(field_names[m], search_pattern[n], strlen(search_pattern[n])) == 0) {
639 if(!*search_regexp_strings) {
641 "Syntax error in the search pattern.\n");
645 recursive_search_in_db(db_root_path,
646 nb_search_patterns, search_ids, search_regexp_strings);
648 for(n = 0; n < nb_search_patterns; n++) {
649 free(search_pattern[n]);