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 && hits[n]; n++);
186 if(n == nb_search_patterns) {
189 mail_file = fopen(current_mail_filename, "r");
192 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
196 fseek(mail_file, current_position_in_mail, SEEK_SET);
198 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
199 printf("%s", raw_mbox_line);
200 while(fgets(raw_mbox_line, BUFFER_SIZE, mail_file) &&
201 strncmp(raw_mbox_line, "From ", 5)) {
202 printf("%s", raw_mbox_line);
209 for(n = 0; n < nb_search_patterns; n++) { hits[n] = 0; }
211 position_in_file_string = mbox_value;
212 mail_filename = segment_next_field(mbox_value);
213 current_position_in_mail = atol(position_in_file_string);
214 strcpy(current_mail_filename, mail_filename);
216 remove_eof(current_mail_filename);
222 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
223 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
227 for(n = 0; n < nb_search_patterns; n++) {
228 hits[n] |= mbox_line_match_search(&search_requests[n],
229 mbox_id, mbox_value);
235 void recursive_search_in_db(const char *entry_name,
236 int nb_search_patterns,
237 struct search_request *search_requests) {
239 struct dirent *dir_e;
241 char raw_db_line[BUFFER_SIZE];
242 char subname[PATH_MAX + 1];
244 if(lstat(entry_name, &sb) != 0) {
246 "mymail: Cannot stat \"%s\": %s\n",
252 dir = opendir(entry_name);
255 while((dir_e = readdir(dir))) {
256 if(!ignore_entry(dir_e->d_name)) {
257 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
258 recursive_search_in_db(subname,
265 const char *s = entry_name, *filename = entry_name;
266 while(*s) { if(*s == '/') { filename = s+1; } s++; }
268 if(strcmp(filename, db_filename) == 0) {
269 FILE *db_file = fopen(entry_name, "r");
273 "mymail: Cannot open \"%s\" for reading: %s\n",
279 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
280 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
282 "mymail: Header line in '%s' does not match the mymail db format.\n",
288 "mymail: Cannot read the header line in '%s'.\n",
293 search_in_db(nb_search_patterns, search_requests, db_file);
300 /*********************************************************************/
302 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
303 char *raw_mbox_line, FILE *db_file) {
306 for(f = 0; f < nb_fields_to_parse; f++) {
307 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
308 fprintf(db_file, "%s %s\n",
309 field_names[fields_to_parse[f].id],
310 raw_mbox_line + matches.rm_eo);
315 void index_mbox(const char *mbox_filename,
316 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
318 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
319 char *end_of_full_line;
321 int in_header, new_header;
322 unsigned long int position_in_file;
324 file = fopen(mbox_filename, "r");
327 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
328 if(paranoid) { exit(EXIT_FAILURE); }
335 position_in_file = 0;
336 end_of_full_line = 0;
339 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
340 if(strncmp(raw_mbox_line, "From ", 5) == 0) {
343 "Got a ^\"From \" in the header in %s:%lu.\n",
344 mbox_filename, position_in_file);
345 fprintf(stderr, "%s", raw_mbox_line);
346 if(paranoid) { exit(EXIT_FAILURE); }
350 } else if(strncmp(raw_mbox_line, "\n", 1) == 0) {
351 if(in_header) { in_header = 0; }
356 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
360 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
361 char *start = raw_mbox_line;
362 while(*start == ' ' || *start == '\t') start++;
363 *(end_of_full_line++) = ' ';
364 strcpy(end_of_full_line, start);
365 while(*end_of_full_line && *end_of_full_line != '\n') {
368 *end_of_full_line = '\0';
373 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
374 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
376 "Header line syntax error %s:%lu.\n",
377 mbox_filename, position_in_file);
378 fprintf(stderr, "%s", raw_mbox_line);
383 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
386 end_of_full_line = full_line;
387 strcpy(end_of_full_line, raw_mbox_line);
388 while(*end_of_full_line && *end_of_full_line != '\n') {
391 *end_of_full_line = '\0';
396 position_in_file += strlen(raw_mbox_line);
402 void recursive_index_mbox(FILE *db_file,
403 const char *entry_name,
404 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
406 struct dirent *dir_e;
408 char subname[PATH_MAX + 1];
410 if(lstat(entry_name, &sb) != 0) {
412 "mymail: Cannot stat \"%s\": %s\n",
418 dir = opendir(entry_name);
421 while((dir_e = readdir(dir))) {
422 if(!ignore_entry(dir_e->d_name)) {
423 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
424 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
429 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
433 /*********************************************************************/
435 /* For long options that have no equivalent short option, use a
436 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
438 OPT_BASH_MODE = CHAR_MAX + 1
441 static struct option long_options[] = {
442 { "help", no_argument, 0, 'h' },
443 { "version", no_argument, 0, 'v' },
444 { "db-file", 1, 0, 'd' },
445 { "db-root", 1, 0, 'r' },
446 { "search", 1, 0, 's' },
447 { "index", 0, 0, 'i' },
451 static struct parsable_field fields_to_parse[] = {
454 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
455 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
460 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
461 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
466 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: *",
467 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
472 /*********************************************************************/
474 int main(int argc, char **argv) {
475 int error = 0, show_help = 0;
476 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
479 int nb_search_patterns;
480 char *search_pattern[MAX_NB_SEARCH_PATTERNS];
482 /* for(f = 0; f < argc; f++) { */
483 /* printf("arg %d \"%s\"\n", f, argv[f]); */
491 setlocale(LC_ALL, "");
493 nb_search_patterns = 0;
495 while ((c = getopt_long(argc, argv, "hvip:s:d:r:",
496 long_options, NULL)) != -1) {
505 print_version(stdout);
513 db_filename = strdup(optarg);
517 db_root_path = strdup(optarg);
521 if(nb_search_patterns == MAX_NB_SEARCH_PATTERNS) {
522 fprintf(stderr, "mymail: Too many search patterns.\n");
525 search_pattern[nb_search_patterns++] = strdup(optarg);
535 char *default_db_filename = getenv("MYMAIL_DB_FILE");
537 if(!default_db_filename) {
538 default_db_filename = "mymail.db";
541 db_filename = strdup(default_db_filename);
545 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
547 if(default_db_root_path) {
548 db_root_path = strdup(default_db_root_path);
554 "mymail: db root path is not set\n");
572 db_file = fopen(db_filename, "w");
576 "mymail: Cannot open \"%s\" for writing: %s\n",
582 for(f = 0; f < nb_fields_to_parse; f++) {
583 if(regcomp(&fields_to_parse[f].regexp,
584 fields_to_parse[f].regexp_string,
587 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
588 fields_to_parse[f].regexp_string,
589 field_names[fields_to_parse[f].id]);
594 fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
596 while(optind < argc) {
597 recursive_index_mbox(db_file,
599 nb_fields_to_parse, fields_to_parse);
605 for(f = 0; f < nb_fields_to_parse; f++) {
606 regfree(&fields_to_parse[f].regexp);
612 if(nb_search_patterns > 0) {
613 struct search_request search_requests[MAX_NB_SEARCH_PATTERNS];
614 char *search_regexp_string;
617 for(n = 0; n < nb_search_patterns; n++) {
618 search_regexp_string = segment_next_field(search_pattern[n]);
620 search_requests[n].field_id = -1;
621 for(m = 0; (m < MAX_ID) && search_requests[n].field_id == -1; m++) {
622 if(strncmp(field_names[m], search_pattern[n], strlen(search_pattern[n])) == 0) {
623 search_requests[n].field_id = m;
627 if(regcomp(&search_requests[n].regexp,
628 search_regexp_string,
631 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
632 search_regexp_string,
633 field_names[search_requests[n].field_id]);
638 recursive_search_in_db(db_root_path,
639 nb_search_patterns, search_requests);
641 for(n = 0; n < nb_search_patterns; n++) {
642 free(search_pattern[n]);