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_REQUESTS 10
52 #define BUFFER_SIZE 65536
60 /********************************************************************/
72 static char *field_names[] = {
81 /********************************************************************/
83 struct search_request {
89 /********************************************************************/
91 struct parsable_field {
97 static struct parsable_field fields_to_parse[] = {
100 "^\\(From \\|[Ff][Rr][Oo][Mm]:\\|[R][r][E][e][P][p][L][l][Y][y]-[T][t][O][o]:\\)",
101 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
106 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ",
107 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
112 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ",
113 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
118 "^[Dd][Aa][Tt][Ee]: ",
119 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
124 /********************************************************************/
126 int xor(int a, int b) {
127 return (a && !b) || (!a && b);
130 char *segment_next_field(char *current) {
131 while(*current && *current != ' ') current++;
132 *current = '\0'; current++;
133 while(*current && *current == ' ') current++;
137 void remove_eof(char *c) {
138 while(*c && *c != '\n' && *c != '\r') c++;
142 /********************************************************************/
144 /* malloc with error checking. */
146 void *safe_malloc(size_t n) {
150 "mymail: cannot allocate memory: %s\n", strerror(errno));
156 /*********************************************************************/
158 void print_version(FILE *out) {
159 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
162 void print_usage(FILE *out) {
164 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
166 fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]]\n");
168 fprintf(out, " -h, --help\n");
169 fprintf(out, " show this help\n");
170 fprintf(out, " -v, --version\n");
171 fprintf(out, " print the version number\n");
172 fprintf(out, " -i, --index\n");
173 fprintf(out, " index mails\n");
174 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
175 fprintf(out, " search for matching mails in the data-base file\n");
176 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
177 fprintf(out, " set the data-base file\n");
178 fprintf(out, " -r <db root path>, --db-root <db root path>\n");
179 fprintf(out, " set the data-base root path for recursive search\n");
182 /*********************************************************************/
184 int ignore_entry(const char *name) {
186 /* strcmp(name, ".") == 0 || */
187 /* strcmp(name, "..") == 0 || */
188 (name[0] == '.' && name[1] != '/');
191 int mbox_line_match_search(struct search_request *request,
192 int mbox_id, char *mbox_value) {
194 (request->field_id == mbox_id ||
195 (request->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_TO)))
197 regexec(&request->regexp, mbox_value, 0, 0, 0) == 0;
200 void search_in_db(int nb_search_requests,
201 struct search_request *search_requests,
203 int hits[MAX_NB_SEARCH_REQUESTS];
204 char raw_db_line[BUFFER_SIZE];
205 char raw_mbox_line[BUFFER_SIZE];
206 char current_mail_filename[PATH_MAX + 1];
207 unsigned long int current_position_in_mail;
208 char *mbox_name, *mbox_value;
210 int already_written, m, n;
211 int last_mbox_line_was_empty;
213 current_position_in_mail = 0;
216 for(n = 0; n < nb_search_requests; n++) { hits[n] = 0; }
218 while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
219 mbox_name = raw_db_line;
220 mbox_value = segment_next_field(raw_db_line);
222 if(strcmp("mail", mbox_name) == 0) {
223 char *position_in_file_string;
226 for(n = 0; n < nb_search_requests && xor(hits[n], search_requests[n].negation); n++);
228 /* for(n = 0; n < nb_search_requests && */
229 /* ((hits[n] && !search_requests[n].negation) || */
230 /* (!hits[n] && search_requests[n].negation)); n++); */
232 if(n == nb_search_requests) {
235 mail_file = fopen(current_mail_filename, "r");
238 fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
242 fseek(mail_file, current_position_in_mail, SEEK_SET);
244 if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
245 last_mbox_line_was_empty = 1;
246 printf("%s", raw_mbox_line);
248 if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
249 (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
250 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
251 printf("%s", raw_mbox_line);
258 for(n = 0; n < nb_search_requests; n++) { hits[n] = 0; }
260 position_in_file_string = mbox_value;
261 mail_filename = segment_next_field(mbox_value);
262 current_position_in_mail = atol(position_in_file_string);
263 strcpy(current_mail_filename, mail_filename);
265 remove_eof(current_mail_filename);
271 for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
272 if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
276 for(n = 0; n < nb_search_requests; n++) {
277 hits[n] |= mbox_line_match_search(&search_requests[n],
278 mbox_id, mbox_value);
284 void recursive_search_in_db(const char *entry_name,
285 int nb_search_requests,
286 struct search_request *search_requests) {
288 struct dirent *dir_e;
290 char raw_db_line[BUFFER_SIZE];
291 char subname[PATH_MAX + 1];
293 if(lstat(entry_name, &sb) != 0) {
295 "mymail: Cannot stat \"%s\": %s\n",
301 dir = opendir(entry_name);
304 while((dir_e = readdir(dir))) {
305 if(!ignore_entry(dir_e->d_name)) {
306 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
307 recursive_search_in_db(subname,
314 const char *s = entry_name, *filename = entry_name;
315 while(*s) { if(*s == '/') { filename = s+1; } s++; }
317 if(strcmp(filename, db_filename) == 0) {
318 FILE *db_file = fopen(entry_name, "r");
322 "mymail: Cannot open \"%s\" for reading: %s\n",
328 if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
329 if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
331 "mymail: Header line in '%s' does not match the mymail db format.\n",
337 "mymail: Cannot read the header line in '%s'.\n",
342 search_in_db(nb_search_requests, search_requests, db_file);
349 /*********************************************************************/
351 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
352 char *raw_mbox_line, FILE *db_file) {
355 for(f = 0; f < nb_fields_to_parse; f++) {
356 if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
357 fprintf(db_file, "%s %s\n",
358 field_names[fields_to_parse[f].id],
359 raw_mbox_line + matches.rm_eo);
364 void index_mbox(const char *mbox_filename,
365 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
367 char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
368 char *end_of_full_line;
370 int in_header, new_header, last_mbox_line_was_empty;
371 unsigned long int position_in_file;
373 file = fopen(mbox_filename, "r");
376 fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
377 if(paranoid) { exit(EXIT_FAILURE); }
384 position_in_file = 0;
385 end_of_full_line = 0;
387 last_mbox_line_was_empty = 1;
389 while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
390 if(last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0) {
393 "Got a ^\"From \" in the header in %s:%lu.\n",
394 mbox_filename, position_in_file);
395 fprintf(stderr, "%s", raw_mbox_line);
396 if(paranoid) { exit(EXIT_FAILURE); }
400 } else if(raw_mbox_line[0] == '\n') {
401 if(in_header) { in_header = 0; }
404 last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
408 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
412 if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
413 char *start = raw_mbox_line;
414 while(*start == ' ' || *start == '\t') start++;
415 *(end_of_full_line++) = ' ';
416 strcpy(end_of_full_line, start);
417 while(*end_of_full_line && *end_of_full_line != '\n') {
420 *end_of_full_line = '\0';
425 if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
426 (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
428 "Header line syntax error %s:%lu.\n",
429 mbox_filename, position_in_file);
430 fprintf(stderr, "%s", raw_mbox_line);
435 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
438 end_of_full_line = full_line;
439 strcpy(end_of_full_line, raw_mbox_line);
440 while(*end_of_full_line && *end_of_full_line != '\n') {
443 *end_of_full_line = '\0';
448 position_in_file += strlen(raw_mbox_line);
454 void recursive_index_mbox(FILE *db_file,
455 const char *entry_name,
456 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
458 struct dirent *dir_e;
460 char subname[PATH_MAX + 1];
462 if(lstat(entry_name, &sb) != 0) {
464 "mymail: Cannot stat \"%s\": %s\n",
470 dir = opendir(entry_name);
473 while((dir_e = readdir(dir))) {
474 if(!ignore_entry(dir_e->d_name)) {
475 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
476 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
481 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
485 /*********************************************************************/
487 /* For long options that have no equivalent short option, use a
488 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
490 OPT_BASH_MODE = CHAR_MAX + 1
493 static struct option long_options[] = {
494 { "help", no_argument, 0, 'h' },
495 { "version", no_argument, 0, 'v' },
496 { "db-file", 1, 0, 'd' },
497 { "db-root", 1, 0, 'r' },
498 { "search", 1, 0, 's' },
499 { "index", 0, 0, 'i' },
503 /*********************************************************************/
505 int main(int argc, char **argv) {
506 int error = 0, show_help = 0;
507 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
510 int nb_search_requests;
511 char *search_request_strings[MAX_NB_SEARCH_REQUESTS];
513 /* for(f = 0; f < argc; f++) { */
514 /* printf("arg %d \"%s\"\n", f, argv[f]); */
522 setlocale(LC_ALL, "");
524 nb_search_requests = 0;
526 while ((c = getopt_long(argc, argv, "hvip:s:d:r:",
527 long_options, NULL)) != -1) {
536 print_version(stdout);
544 db_filename = strdup(optarg);
548 db_root_path = strdup(optarg);
552 if(nb_search_requests == MAX_NB_SEARCH_REQUESTS) {
553 fprintf(stderr, "mymail: Too many search patterns.\n");
556 search_request_strings[nb_search_requests++] = strdup(optarg);
566 char *default_db_filename = getenv("MYMAIL_DB_FILE");
568 if(!default_db_filename) {
569 default_db_filename = "mymail.db";
572 db_filename = strdup(default_db_filename);
576 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
578 if(default_db_root_path) {
579 db_root_path = strdup(default_db_root_path);
596 db_file = fopen(db_filename, "w");
600 "mymail: Cannot open \"%s\" for writing: %s\n",
606 for(f = 0; f < nb_fields_to_parse; f++) {
607 if(regcomp(&fields_to_parse[f].regexp,
608 fields_to_parse[f].regexp_string,
611 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
612 fields_to_parse[f].regexp_string,
613 field_names[fields_to_parse[f].id]);
618 fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
620 while(optind < argc) {
621 recursive_index_mbox(db_file,
623 nb_fields_to_parse, fields_to_parse);
629 for(f = 0; f < nb_fields_to_parse; f++) {
630 regfree(&fields_to_parse[f].regexp);
638 "mymail: db root path is not set\n");
642 if(nb_search_requests > 0) {
643 struct search_request search_requests[MAX_NB_SEARCH_REQUESTS];
644 char *search_field, *search_regexp_string;
647 for(n = 0; n < nb_search_requests; n++) {
648 search_field = search_request_strings[n];
649 search_regexp_string = segment_next_field(search_request_strings[n]);
651 if(search_field[0] == '!') {
653 search_requests[n].negation = 1;
655 search_requests[n].negation = 0;
658 search_requests[n].field_id = -1;
659 for(m = 0; (m < MAX_ID) && search_requests[n].field_id == -1; m++) {
660 if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
661 search_requests[n].field_id = m;
665 if(search_requests[n].field_id == -1) {
667 "mymail: Syntax error in field name \"%s\".\n",
672 if(regcomp(&search_requests[n].regexp,
673 search_regexp_string,
676 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
677 search_regexp_string,
678 field_names[search_requests[n].field_id]);
683 recursive_search_in_db(db_root_path,
684 nb_search_requests, search_requests);
686 for(n = 0; n < nb_search_requests; n++) {
687 regfree(&search_requests[n].regexp);
688 free(search_request_strings[n]);