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) {
146 return search_id == mbox_id && regexec(search_regexp, mbox_value, 0, 0, 0) == 0;
149 void search_in_db(int nb_search_patterns,
150 int *search_ids, char **search_regexp_strings,
152 int hits[MAX_NB_SEARCH_PATTERNS];
153 char raw_db_line[BUFFER_SIZE];
154 char raw_mbox_line[BUFFER_SIZE];
155 char current_mail_filename[PATH_MAX + 1];
156 unsigned long int current_position_in_mail;
157 char *mbox_name, *mbox_value;
159 regex_t search_regexps[MAX_NB_SEARCH_PATTERNS];
160 int already_written, m, n;
162 for(n = 0; n < nb_search_patterns; n++) {
163 if(regcomp(&search_regexps[n],
164 search_regexp_strings[n],
167 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
168 search_regexp_strings[n],
169 field_names[search_ids[n]]);
174 current_position_in_mail = 0;
177 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
179 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
180 mbox_name = raw_db_line;
181 mbox_value = segment_next_field(raw_db_line);
183 if(strcmp("mail", mbox_name) == 0) {
184 char *position_in_file_string;
187 for(n = 0; n < nb_search_patterns && hits[n]; n++);
189 if(n == nb_search_patterns) {
192 mail_file = fopen(current_mail_filename, "r");
195 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
199 fseek(mail_file, current_position_in_mail, SEEK_SET);
201 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
202 printf("%s", raw_mbox_line);
203 while(fgets(raw_mbox_line, BUFFER_SIZE, mail_file) &&
204 strncmp(raw_mbox_line, "From ", 5)) {
205 printf("%s", raw_mbox_line);
212 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
214 position_in_file_string = mbox_value;
215 mail_filename = segment_next_field(mbox_value);
216 current_position_in_mail = atol(position_in_file_string);
217 strcpy(current_mail_filename, mail_filename);
219 remove_eof(current_mail_filename);
225 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
226 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
230 for(n = 0; n < nb_search_patterns; n++) {
231 hits[n] |= mbox_line_match_search(search_ids[n], &search_regexps[n],
232 mbox_id, mbox_value);
237 for(n = 0; n < nb_search_patterns; n++) {
238 regfree(&search_regexps[n]);
242 void recursive_search_in_db(const char *entry_name,
243 int nb_search_patterns,
244 int *search_ids, char **search_regexp_strings) {
246 struct dirent *dir_e;
248 char raw_db_line[BUFFER_SIZE];
249 char subname[PATH_MAX + 1];
251 if(lstat(entry_name, &sb) != 0) {
253 "mymail: Cannot stat \"%s\": %s\n",
259 dir = opendir(entry_name);
262 while((dir_e = readdir(dir))) {
263 if(!ignore_entry(dir_e->d_name)) {
264 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
265 recursive_search_in_db(subname,
267 search_ids, search_regexp_strings);
272 const char *s = entry_name, *filename = entry_name;
273 while(*s) { if(*s == '/') { filename = s+1; } s++; }
275 if(strcmp(filename, db_filename) == 0) {
276 FILE *db_file = fopen(entry_name, "r");
280 "mymail: Cannot open \"%s\" for reading: %s\n",
286 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
287 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
289 "mymail: Header line in '%s' does not match the mymail db format.\n",
295 "mymail: Cannot read the header line in '%s'.\n",
300 search_in_db(nb_search_patterns, search_ids, search_regexp_strings,
308 /*********************************************************************/
310 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
311 char *raw_mbox_line, FILE *db_file) {
314 for(f = 0; f < nb_fields_to_parse; f++) {
315 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
316 fprintf(db_file, "%s %s\n",
317 field_names[fields_to_parse[f].id],
318 raw_mbox_line + matches.rm_eo);
323 void index_mbox(const char *mbox_filename,
324 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
326 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
327 char *end_of_full_line;
329 int in_header, new_header;
330 unsigned long int position_in_file;
332 file = fopen(mbox_filename, "r");
335 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
336 if(paranoid) { exit(EXIT_FAILURE); }
343 position_in_file = 0;
344 end_of_full_line = 0;
347 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
348 if(strncmp(raw_mbox_line, "From ", 5) == 0) {
351 "Got a ^\"From \" in the header in %s:%lu.\n",
352 mbox_filename, position_in_file);
353 fprintf(stderr, "%s", raw_mbox_line);
354 if(paranoid) { exit(EXIT_FAILURE); }
358 } else if(strncmp(raw_mbox_line, "\n", 1) == 0) {
359 if(in_header) { in_header = 0; }
364 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
368 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
369 char *start = raw_mbox_line;
370 while(*start == ' ' || *start == '\t') start++;
371 *(end_of_full_line++) = ' ';
372 strcpy(end_of_full_line, start);
373 while(*end_of_full_line && *end_of_full_line != '\n') {
376 *end_of_full_line = '\0';
381 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
382 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
384 "Header line syntax error %s:%lu.\n",
385 mbox_filename, position_in_file);
386 fprintf(stderr, "%s", raw_mbox_line);
391 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
394 end_of_full_line = full_line;
395 strcpy(end_of_full_line, raw_mbox_line);
396 while(*end_of_full_line && *end_of_full_line != '\n') {
399 *end_of_full_line = '\0';
404 position_in_file += strlen(raw_mbox_line);
410 void recursive_index_mbox(FILE *db_file,
411 const char *entry_name,
412 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
414 struct dirent *dir_e;
416 char subname[PATH_MAX + 1];
418 if(lstat(entry_name, &sb) != 0) {
420 "mymail: Cannot stat \"%s\": %s\n",
426 dir = opendir(entry_name);
429 while((dir_e = readdir(dir))) {
430 if(!ignore_entry(dir_e->d_name)) {
431 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
432 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
437 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
441 /*********************************************************************/
443 /* For long options that have no equivalent short option, use a
444 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
446 OPT_BASH_MODE = CHAR_MAX + 1
449 static struct option long_options[] = {
450 { "help", no_argument, 0, 'h' },
451 { "version", no_argument, 0, 'v' },
452 { "db-file", 1, 0, 'd' },
453 { "db-root", 1, 0, 'r' },
454 { "search", 1, 0, 's' },
455 { "index", 0, 0, 'i' },
459 static struct parsable_field fields_to_parse[] = {
462 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
463 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
468 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
469 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
474 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: *",
475 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
480 /*********************************************************************/
482 int main(int argc, char **argv) {
483 int error = 0, show_help = 0;
484 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
487 int nb_search_patterns;
488 char *search_pattern[MAX_NB_SEARCH_PATTERNS];
490 /* for(f = 0; f < argc; f++) { */
491 /* printf("arg %d \"%s\"\n", f, argv[f]); */
499 setlocale(LC_ALL, "");
501 nb_search_patterns = 0;
503 while ((c = getopt_long(argc, argv, "hvip:s:d:r:",
504 long_options, NULL)) != -1) {
513 print_version(stdout);
521 db_filename = strdup(optarg);
525 db_root_path = strdup(optarg);
529 if(nb_search_patterns == MAX_NB_SEARCH_PATTERNS) {
530 fprintf(stderr, "mymail: Too many search patterns.\n");
533 search_pattern[nb_search_patterns++] = strdup(optarg);
543 char *default_db_filename = getenv("MYMAIL_DB_FILE");
545 if(!default_db_filename) {
546 default_db_filename = "mymail.db";
549 db_filename = strdup(default_db_filename);
553 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
555 if(default_db_root_path) {
556 db_root_path = strdup(default_db_root_path);
562 "mymail: db root path is not set\n");
580 db_file = fopen(db_filename, "w");
584 "mymail: Cannot open \"%s\" for writing: %s\n",
590 for(f = 0; f < nb_fields_to_parse; f++) {
591 if(regcomp(&fields_to_parse[f].regexp,
592 fields_to_parse[f].regexp_string,
595 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
596 fields_to_parse[f].regexp_string,
597 field_names[fields_to_parse[f].id]);
602 fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
604 while(optind < argc) {
605 recursive_index_mbox(db_file,
607 nb_fields_to_parse, fields_to_parse);
613 for(f = 0; f < nb_fields_to_parse; f++) {
614 regfree(&fields_to_parse[f].regexp);
620 if(nb_search_patterns > 0) {
621 int search_ids[MAX_NB_SEARCH_PATTERNS];
622 char *search_regexp_strings[MAX_NB_SEARCH_PATTERNS];
625 for(n = 0; n < nb_search_patterns; n++) {
626 search_regexp_strings[n] = segment_next_field(search_pattern[n]);
628 for(m = 0; (m < MAX_ID) && search_ids[n] == -1; m++) {
629 if(strncmp(field_names[m], search_pattern[n], strlen(search_pattern[n])) == 0) {
635 if(!*search_regexp_strings) {
637 "Syntax error in the search pattern.\n");
641 recursive_search_in_db(db_root_path,
642 nb_search_patterns, search_ids, search_regexp_strings);
644 for(n = 0; n < nb_search_patterns; n++) {
645 free(search_pattern[n]);