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 {
68 char *segment_next_field(char *current) {
69 while(*current && *current != ' ') current++;
70 *current = '\0'; current++;
71 while(*current && *current == ' ') current++;
75 void remove_eof(char *c) {
76 while(*c && *c != '\n' && *c != '\r') c++;
80 /********************************************************************/
82 /* malloc with error checking. */
84 void *safe_malloc(size_t n) {
88 "mymail: cannot allocate memory: %s\n", strerror(errno));
94 /*********************************************************************/
96 void print_version(FILE *out) {
97 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
100 void print_usage(FILE *out) {
102 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
104 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
106 fprintf(out, " -h, --help\n");
107 fprintf(out, " show this help\n");
108 fprintf(out, " -v, --version\n");
109 fprintf(out, " print the version number\n");
110 fprintf(out, " -i, --index\n");
111 fprintf(out, " index mails\n");
112 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
113 fprintf(out, " set the data-base file\n");
114 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
115 fprintf(out, " search for matching mails in the data-base file\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 (strcmp(search_name[n], name) == 0 && regexec(®exp[n], value, 0, 0, 0) == 0);
203 for(n = 0; n < nb_search_patterns; n++) {
208 void recursive_search_in_db(const char *entry_name,
209 int nb_search_patterns,
210 char **search_name, char **search_regexp_string) {
212 struct dirent *dir_e;
214 char raw_db_line[BUFFER_SIZE];
215 char subname[PATH_MAX + 1];
217 if(lstat(entry_name, &sb) != 0) {
219 "mymail: Cannot stat \"%s\": %s\n",
225 dir = opendir(entry_name);
228 while((dir_e = readdir(dir))) {
229 if(!ignore_entry(dir_e->d_name)) {
230 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
231 recursive_search_in_db(subname,
233 search_name, search_regexp_string);
238 const char *s = entry_name, *filename = entry_name;
239 while(*s) { if(*s == '/') { filename = s+1; } s++; }
241 if(strcmp(filename, db_filename) == 0) {
242 FILE *db_file = fopen(entry_name, "r");
246 "mymail: Cannot open \"%s\" for reading: %s\n",
252 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
253 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
255 "mymail: Header line in '%s' does not match the mymail db format.\n",
261 "mymail: Cannot read the header line in '%s'.\n",
266 search_in_db(nb_search_patterns, search_name, search_regexp_string,
274 /*********************************************************************/
276 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
277 char *raw_mbox_line, FILE *db_file) {
280 for(f = 0; f < nb_fields_to_parse; f++) {
281 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
282 fprintf(db_file, "%s %s\n",
283 fields_to_parse[f].name,
284 raw_mbox_line + matches.rm_eo);
289 void index_mbox(const char *mbox_filename,
290 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
292 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
293 char *end_of_full_line;
295 int in_header, new_header;
296 unsigned long int position_in_file;
298 file = fopen(mbox_filename, "r");
301 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
302 if(paranoid) { exit(EXIT_FAILURE); }
309 position_in_file = 0;
310 end_of_full_line = 0;
313 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
314 if(strncmp(raw_mbox_line, "From ", 5) == 0) {
317 "Got a ^\"From \" in the header in %s:%lu.\n",
318 mbox_filename, position_in_file);
319 fprintf(stderr, "%s", raw_mbox_line);
320 if(paranoid) { exit(EXIT_FAILURE); }
324 } else if(strncmp(raw_mbox_line, "\n", 1) == 0) {
325 if(in_header) { in_header = 0; }
330 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
334 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
335 char *start = raw_mbox_line;
336 while(*start == ' ' || *start == '\t') start++;
337 *(end_of_full_line++) = ' ';
338 strcpy(end_of_full_line, start);
339 while(*end_of_full_line && *end_of_full_line != '\n') {
342 *end_of_full_line = '\0';
346 /* if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') || */
347 /* (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) { */
348 /* fprintf(stderr, */
349 /* "Header line syntax error %s:%lu.\n", */
350 /* mbox_filename, position_in_file); */
351 /* fprintf(stderr, "%s", raw_mbox_line); */
355 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
358 end_of_full_line = full_line;
359 strcpy(end_of_full_line, raw_mbox_line);
360 while(*end_of_full_line && *end_of_full_line != '\n') {
363 *end_of_full_line = '\0';
368 position_in_file += strlen(raw_mbox_line);
374 void recursive_index_mbox(FILE *db_file,
375 const char *entry_name,
376 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
378 struct dirent *dir_e;
380 char subname[PATH_MAX + 1];
382 if(lstat(entry_name, &sb) != 0) {
384 "mymail: Cannot stat \"%s\": %s\n",
390 dir = opendir(entry_name);
393 while((dir_e = readdir(dir))) {
394 if(!ignore_entry(dir_e->d_name)) {
395 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
396 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
401 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
405 /*********************************************************************/
407 /* For long options that have no equivalent short option, use a
408 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
410 OPT_BASH_MODE = CHAR_MAX + 1
413 static struct option long_options[] = {
414 { "help", no_argument, 0, 'h' },
415 { "version", no_argument, 0, 'v' },
416 { "db-file", 1, 0, 'd' },
417 { "db-root", 1, 0, 'p' },
418 { "search-pattern", 1, 0, 's' },
419 { "index", 0, 0, 'i' },
420 { "multi-db-files", 0, 0, 'm' },
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];
461 setlocale(LC_ALL, "");
463 nb_search_patterns = 0;
465 while ((c = getopt_long(argc, argv, "hvimp:s:d:p:",
466 long_options, NULL)) != -1) {
475 print_version(stdout);
487 db_filename = strdup(optarg);
491 db_root_path = strdup(optarg);
495 if(nb_search_patterns == MAX_NB_SEARCH_PATTERNS) {
496 fprintf(stderr, "mymail: Too many search patterns.\n");
499 search_pattern[nb_search_patterns++] = strdup(optarg);
509 char *default_db_filename = getenv("MYMAIL_DB_FILE");
511 if(!default_db_filename) {
513 default_db_filename = "mymail.db";
515 default_db_filename = "/tmp/mymail.db";
519 db_filename = strdup(default_db_filename);
523 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
525 if(!default_db_root_path) {
527 default_db_root_path = "mymail.db";
529 default_db_root_path = "/tmp/mymail.db";
533 db_root_path = strdup(default_db_root_path);
549 db_file = fopen(db_filename, "w");
553 "mymail: Cannot open \"%s\" for writing: %s\n",
559 for(f = 0; f < nb_fields_to_parse; f++) {
560 if(regcomp(&fields_to_parse[f].regexp,
561 fields_to_parse[f].regexp_string,
564 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
565 fields_to_parse[f].regexp_string,
566 fields_to_parse[f].name);
571 fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
573 while(optind < argc) {
574 recursive_index_mbox(db_file,
576 nb_fields_to_parse, fields_to_parse);
582 for(f = 0; f < nb_fields_to_parse; f++) {
583 regfree(&fields_to_parse[f].regexp);
589 if(nb_search_patterns > 0) {
590 char *search_name[MAX_NB_SEARCH_PATTERNS];
591 char *search_regexp_string[MAX_NB_SEARCH_PATTERNS];
594 for(n = 0; n < nb_search_patterns; n++) {
595 search_name[n] = search_pattern[n];
596 search_regexp_string[n] = segment_next_field(search_pattern[n]);
599 if(!*search_regexp_string) {
601 "Syntax error in the search pattern.\n");
605 recursive_search_in_db(db_root_path,
606 nb_search_patterns, search_name, search_regexp_string);
608 for(n = 0; n < nb_search_patterns; n++) {
609 free(search_pattern[n]);