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
54 struct parsable_field {
66 char *segment_next_field(char *current) {
67 while(*current && *current != ' ') current++;
68 *current = '\0'; current++;
69 while(*current && *current == ' ') current++;
73 void remove_eof(char *c) {
74 while(*c && *c != '\n' && *c != '\r') c++;
78 /********************************************************************/
80 /* malloc with error checking. */
82 void *safe_malloc(size_t n) {
86 "mymail: cannot allocate memory: %s\n", strerror(errno));
92 /*********************************************************************/
94 void print_version(FILE *out) {
95 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
98 void print_usage(FILE *out) {
100 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
102 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]]\n");
104 fprintf(out, " -h, --help\n");
105 fprintf(out, " show this help\n");
106 fprintf(out, " -v, --version\n");
107 fprintf(out, " print the version number\n");
108 fprintf(out, " -i, --index\n");
109 fprintf(out, " index mails\n");
110 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
111 fprintf(out, " search for matching mails in the data-base file\n");
112 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
113 fprintf(out, " set the data-base file\n");
114 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
115 fprintf(out, " set the data-base root path for recursive search\n");
118 /*********************************************************************/
120 int ignore_entry(const char *name) {
122 /* strcmp(name, ".") == 0 || */
123 /* strcmp(name, "..") == 0 || */
124 (name[0] == '.' && name[1] != '/');
127 void search_in_db(int nb_search_patterns,
128 char **search_name, char **search_regexp_string,
130 int hits[MAX_NB_SEARCH_PATTERNS];
131 char raw_db_line[BUFFER_SIZE];
132 char raw_mbox_line[BUFFER_SIZE];
133 char current_mail_filename[PATH_MAX + 1];
134 unsigned long int current_position_in_mail;
136 regex_t regexp[MAX_NB_SEARCH_PATTERNS];
137 int already_written, n;
139 for(n = 0; n < nb_search_patterns; n++) {
140 if(regcomp(®exp[n],
141 search_regexp_string[n],
144 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
145 search_regexp_string[n],
151 current_position_in_mail = 0;
154 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
156 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
158 value = segment_next_field(raw_db_line);
160 if(strcmp("mail", name) == 0) {
161 char *position_in_file_string;
164 for(n = 0; n < nb_search_patterns && hits[n]; n++);
166 if(n == nb_search_patterns) {
168 mail_file = fopen(current_mail_filename, "r");
170 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
173 fseek(mail_file, current_position_in_mail, SEEK_SET);
174 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
175 printf("%s", raw_mbox_line);
176 while(fgets(raw_mbox_line, BUFFER_SIZE, mail_file) &&
177 strncmp(raw_mbox_line, "From ", 5)) {
178 printf("%s", raw_mbox_line);
184 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
186 position_in_file_string = value;
187 mail_filename = segment_next_field(value);
188 current_position_in_mail = atol(position_in_file_string);
189 strcpy(current_mail_filename, mail_filename);
191 remove_eof(current_mail_filename);
196 for(n = 0; n < nb_search_patterns; n++) {
198 (strncmp(search_name[n], name, strlen(search_name[n])) == 0 &&
199 regexec(®exp[n], value, 0, 0, 0) == 0);
204 for(n = 0; n < nb_search_patterns; n++) {
209 void recursive_search_in_db(const char *entry_name,
210 int nb_search_patterns,
211 char **search_name, char **search_regexp_string) {
213 struct dirent *dir_e;
215 char raw_db_line[BUFFER_SIZE];
216 char subname[PATH_MAX + 1];
218 if(lstat(entry_name, &sb) != 0) {
220 "mymail: Cannot stat \"%s\": %s\n",
226 dir = opendir(entry_name);
229 while((dir_e = readdir(dir))) {
230 if(!ignore_entry(dir_e->d_name)) {
231 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
232 recursive_search_in_db(subname,
234 search_name, search_regexp_string);
239 const char *s = entry_name, *filename = entry_name;
240 while(*s) { if(*s == '/') { filename = s+1; } s++; }
242 if(strcmp(filename, db_filename) == 0) {
243 FILE *db_file = fopen(entry_name, "r");
247 "mymail: Cannot open \"%s\" for reading: %s\n",
253 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
254 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
256 "mymail: Header line in '%s' does not match the mymail db format.\n",
262 "mymail: Cannot read the header line in '%s'.\n",
267 search_in_db(nb_search_patterns, search_name, search_regexp_string,
275 /*********************************************************************/
277 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
278 char *raw_mbox_line, FILE *db_file) {
281 for(f = 0; f < nb_fields_to_parse; f++) {
282 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
283 fprintf(db_file, "%s %s\n",
284 fields_to_parse[f].name,
285 raw_mbox_line + matches.rm_eo);
290 void index_mbox(const char *mbox_filename,
291 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
293 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
294 char *end_of_full_line;
296 int in_header, new_header;
297 unsigned long int position_in_file;
299 file = fopen(mbox_filename, "r");
302 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
303 if(paranoid) { exit(EXIT_FAILURE); }
310 position_in_file = 0;
311 end_of_full_line = 0;
314 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
315 if(strncmp(raw_mbox_line, "From ", 5) == 0) {
318 "Got a ^\"From \" in the header in %s:%lu.\n",
319 mbox_filename, position_in_file);
320 fprintf(stderr, "%s", raw_mbox_line);
321 if(paranoid) { exit(EXIT_FAILURE); }
325 } else if(strncmp(raw_mbox_line, "\n", 1) == 0) {
326 if(in_header) { in_header = 0; }
331 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
335 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
336 char *start = raw_mbox_line;
337 while(*start == ' ' || *start == '\t') start++;
338 *(end_of_full_line++) = ' ';
339 strcpy(end_of_full_line, start);
340 while(*end_of_full_line && *end_of_full_line != '\n') {
343 *end_of_full_line = '\0';
347 /* if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') || */
348 /* (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) { */
349 /* fprintf(stderr, */
350 /* "Header line syntax error %s:%lu.\n", */
351 /* mbox_filename, position_in_file); */
352 /* fprintf(stderr, "%s", raw_mbox_line); */
356 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
359 end_of_full_line = full_line;
360 strcpy(end_of_full_line, raw_mbox_line);
361 while(*end_of_full_line && *end_of_full_line != '\n') {
364 *end_of_full_line = '\0';
369 position_in_file += strlen(raw_mbox_line);
375 void recursive_index_mbox(FILE *db_file,
376 const char *entry_name,
377 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
379 struct dirent *dir_e;
381 char subname[PATH_MAX + 1];
383 if(lstat(entry_name, &sb) != 0) {
385 "mymail: Cannot stat \"%s\": %s\n",
391 dir = opendir(entry_name);
394 while((dir_e = readdir(dir))) {
395 if(!ignore_entry(dir_e->d_name)) {
396 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
397 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
402 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
406 /*********************************************************************/
408 /* For long options that have no equivalent short option, use a
409 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
411 OPT_BASH_MODE = CHAR_MAX + 1
414 static struct option long_options[] = {
415 { "help", no_argument, 0, 'h' },
416 { "version", no_argument, 0, 'v' },
417 { "db-file", 1, 0, 'd' },
418 { "db-root", 1, 0, 'r' },
419 { "search", 1, 0, 's' },
420 { "index", 0, 0, 'i' },
424 static struct parsable_field fields_to_parse[] = {
427 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
428 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
433 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
434 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
439 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: *",
440 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
445 /*********************************************************************/
447 int main(int argc, char **argv) {
448 int error = 0, show_help = 0;
449 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
452 int nb_search_patterns;
453 char *search_pattern[MAX_NB_SEARCH_PATTERNS];
455 /* for(f = 0; f < argc; f++) { */
456 /* printf("arg %d \"%s\"\n", f, argv[f]); */
464 setlocale(LC_ALL, "");
466 nb_search_patterns = 0;
468 while ((c = getopt_long(argc, argv, "hvip:s:d:r:",
469 long_options, NULL)) != -1) {
478 print_version(stdout);
486 db_filename = strdup(optarg);
490 db_root_path = strdup(optarg);
494 if(nb_search_patterns == MAX_NB_SEARCH_PATTERNS) {
495 fprintf(stderr, "mymail: Too many search patterns.\n");
498 search_pattern[nb_search_patterns++] = strdup(optarg);
508 char *default_db_filename = getenv("MYMAIL_DB_FILE");
510 if(!default_db_filename) {
511 default_db_filename = "mymail.db";
514 db_filename = strdup(default_db_filename);
518 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
520 if(default_db_root_path) {
521 db_root_path = strdup(default_db_root_path);
527 "mymail: db root path is not set\n");
545 db_file = fopen(db_filename, "w");
549 "mymail: Cannot open \"%s\" for writing: %s\n",
555 for(f = 0; f < nb_fields_to_parse; f++) {
556 if(regcomp(&fields_to_parse[f].regexp,
557 fields_to_parse[f].regexp_string,
560 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
561 fields_to_parse[f].regexp_string,
562 fields_to_parse[f].name);
567 fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
569 while(optind < argc) {
570 recursive_index_mbox(db_file,
572 nb_fields_to_parse, fields_to_parse);
578 for(f = 0; f < nb_fields_to_parse; f++) {
579 regfree(&fields_to_parse[f].regexp);
585 if(nb_search_patterns > 0) {
586 char *search_name[MAX_NB_SEARCH_PATTERNS];
587 char *search_regexp_string[MAX_NB_SEARCH_PATTERNS];
590 for(n = 0; n < nb_search_patterns; n++) {
591 search_name[n] = search_pattern[n];
592 search_regexp_string[n] = segment_next_field(search_pattern[n]);
595 if(!*search_regexp_string) {
597 "Syntax error in the search pattern.\n");
601 recursive_search_in_db(db_root_path,
602 nb_search_patterns, search_name, search_regexp_string);
604 for(n = 0; n < nb_search_patterns; n++) {
605 free(search_pattern[n]);