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 BUFFER_SIZE 65536
52 struct parsable_field {
67 char *segment_next_field(char *current) {
68 while(*current && *current != ' ') current++;
69 *current = '\0'; current++;
70 while(*current && *current == ' ') current++;
74 void remove_eof(char *c) {
75 while(*c && *c != '\n' && *c != '\r') c++;
79 /********************************************************************/
81 /* malloc with error checking. */
83 void *safe_malloc(size_t n) {
87 "mymail: can not allocate memory: %s\n", strerror(errno));
93 /*********************************************************************/
95 void print_version(FILE *out) {
96 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
99 void print_usage(FILE *out) {
101 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
103 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
105 fprintf(out, " -h, --help\n");
106 fprintf(out, " show this help\n");
107 fprintf(out, " -v, --version\n");
108 fprintf(out, " print the version number\n");
109 fprintf(out, " -i, --index\n");
110 fprintf(out, " index mails\n");
111 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
112 fprintf(out, " set the data-base file\n");
113 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
114 fprintf(out, " search for matching mails in the data-base file\n");
117 /*********************************************************************/
119 int ignore_entry(const char *name) {
121 /* strcmp(name, ".") == 0 || */
122 /* strcmp(name, "..") == 0 || */
123 (name[0] == '.' && name[1] != '/');
126 void search_in_db(const char *search_name, const char *search_regexp_string,
129 char raw_line[BUFFER_SIZE];
130 char current_mail_filename[PATH_MAX + 1];
131 unsigned long int current_position_in_mail;
137 search_regexp_string,
140 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
141 search_regexp_string,
146 current_position_in_mail = 0;
149 while(fgets(raw_line, BUFFER_SIZE, db_file)) {
151 value = segment_next_field(raw_line);
153 if(strcmp("mail", name) == 0) {
154 char *position_in_file_string = value;
155 char *mail_filename = segment_next_field(value);
156 current_position_in_mail = atol(position_in_file_string);
157 strcpy(current_mail_filename, mail_filename);
158 remove_eof(current_mail_filename);
162 else if(!already_written) {
163 if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) {
165 mail_file = fopen(current_mail_filename, "r");
167 fprintf(stderr, "mymail: Can not open '%s'.\n", current_mail_filename);
170 fseek(mail_file, current_position_in_mail, SEEK_SET);
171 if(fgets(raw_line, BUFFER_SIZE, mail_file)) {
172 printf("%s", raw_line);
173 while(fgets(raw_line, BUFFER_SIZE, mail_file) &&
174 strncmp(raw_line, "From ", 5)) {
175 printf("%s", raw_line);
187 void recursive_search_in_db(const char *entry_name,
188 const char *search_name, const char *search_regexp_string) {
190 struct dirent *dir_e;
192 char raw_line[BUFFER_SIZE];
193 char subname[PATH_MAX + 1];
195 if(lstat(entry_name, &sb) != 0) {
197 "mymail: Can not stat \"%s\": %s\n",
203 dir = opendir(entry_name);
206 while((dir_e = readdir(dir))) {
207 if(!ignore_entry(dir_e->d_name)) {
208 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
209 recursive_search_in_db(subname, search_name, search_regexp_string);
214 const char *s = entry_name, *filename = entry_name;
215 while(*s) { if(*s == '/') { filename = s+1; } s++; }
217 if(strcmp(filename, db_filename) == 0) {
218 FILE *db_file = fopen(entry_name, "r");
222 "mymail: Can not open \"%s\" for reading: %s\n",
228 if(fgets(raw_line, BUFFER_SIZE, db_file)) {
229 if(strncmp(raw_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
231 "mymail: Header line in '%s' does not match the mymail db format.\n",
237 "mymail: Can not read the header line in '%s'.\n",
242 search_in_db(search_name, search_regexp_string, db_file);
249 /*********************************************************************/
251 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
252 char *raw_line, FILE *db_file) {
255 for(f = 0; f < nb_fields_to_parse; f++) {
256 if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
257 fprintf(db_file, "%s %s\n",
258 fields_to_parse[f].name,
259 raw_line + matches.rm_eo);
264 void index_mbox(const char *mbox_filename,
265 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
267 char raw_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
268 char *end_of_full_line;
270 int in_header, new_header;
271 unsigned long int position_in_file;
273 file = fopen(mbox_filename, "r");
276 fprintf(stderr, "mymail: Can not open '%s'.\n", mbox_filename);
277 if(paranoid) { exit(EXIT_FAILURE); }
284 position_in_file = 0;
285 end_of_full_line = 0;
288 while(fgets(raw_line, BUFFER_SIZE, file)) {
289 if(strncmp(raw_line, "From ", 5) == 0) {
292 "Got a ^\"From \" in the header in %s:%lu.\n",
293 mbox_filename, position_in_file);
294 fprintf(stderr, "%s", raw_line);
295 if(paranoid) { exit(EXIT_FAILURE); }
299 } else if(strncmp(raw_line, "\n", 1) == 0) {
300 if(in_header) { in_header = 0; }
305 fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
309 if(raw_line[0] == ' ' || raw_line[0] == '\t') {
310 char *start = raw_line;
311 while(*start == ' ' || *start == '\t') start++;
312 *(end_of_full_line++) = ' ';
313 strcpy(end_of_full_line, start);
314 while(*end_of_full_line && *end_of_full_line != '\n') {
317 *end_of_full_line = '\0';
321 /* if(!((raw_line[0] >= 'a' && raw_line[0] <= 'z') || */
322 /* (raw_line[0] >= 'A' && raw_line[0] <= 'Z'))) { */
323 /* fprintf(stderr, */
324 /* "Header line syntax error %s:%lu.\n", */
325 /* mbox_filename, position_in_file); */
326 /* fprintf(stderr, "%s", raw_line); */
330 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
333 end_of_full_line = full_line;
334 strcpy(end_of_full_line, raw_line);
335 while(*end_of_full_line && *end_of_full_line != '\n') {
338 *end_of_full_line = '\0';
343 position_in_file += strlen(raw_line);
349 void recursive_index_mbox(FILE *db_file,
350 const char *entry_name,
351 int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
353 struct dirent *dir_e;
355 char subname[PATH_MAX + 1];
357 if(lstat(entry_name, &sb) != 0) {
359 "mymail: Can not stat \"%s\": %s\n",
365 dir = opendir(entry_name);
368 while((dir_e = readdir(dir))) {
369 if(!ignore_entry(dir_e->d_name)) {
370 snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
371 recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
376 index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
380 /*********************************************************************/
382 /* For long options that have no equivalent short option, use a
383 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
385 OPT_BASH_MODE = CHAR_MAX + 1
388 static struct option long_options[] = {
389 { "help", no_argument, 0, 'h' },
390 { "version", no_argument, 0, 'v' },
391 { "db-file", 1, 0, 'd' },
392 { "db-root", 1, 0, 'p' },
393 { "search-pattern", 1, 0, 's' },
394 { "index", 0, 0, 'i' },
395 { "multi-db-files", 0, 0, 'm' },
399 static struct parsable_field fields_to_parse[] = {
402 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
403 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
408 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
409 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
414 "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: *",
415 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
420 int main(int argc, char **argv) {
421 int error = 0, show_help = 0;
422 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
433 setlocale(LC_ALL, "");
435 while ((c = getopt_long(argc, argv, "hvimp:s:d:p:",
436 long_options, NULL)) != -1) {
445 print_version(stdout);
457 db_filename = strdup(optarg);
461 db_root_path = strdup(optarg);
466 fprintf(stderr, "mymail: Search pattern already defined.\n");
469 search_pattern = strdup(optarg);
479 char *default_db_filename = getenv("MYMAIL_DB_FILE");
481 if(!default_db_filename) {
483 default_db_filename = "mymail.db";
485 default_db_filename = "/tmp/mymail.db";
489 db_filename = strdup(default_db_filename);
493 char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
495 if(!default_db_root_path) {
497 default_db_root_path = "mymail.db";
499 default_db_root_path = "/tmp/mymail.db";
503 db_root_path = strdup(default_db_root_path);
519 db_file = fopen(db_filename, "w");
523 "mymail: Can not open \"%s\" for writing: %s\n",
529 for(f = 0; f < nb_fields_to_parse; f++) {
530 if(regcomp(&fields_to_parse[f].regexp,
531 fields_to_parse[f].regexp_string,
534 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
535 fields_to_parse[f].regexp_string,
536 fields_to_parse[f].name);
541 fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
543 while(optind < argc) {
544 recursive_index_mbox(db_file,
546 nb_fields_to_parse, fields_to_parse);
552 for(f = 0; f < nb_fields_to_parse; f++) {
553 regfree(&fields_to_parse[f].regexp);
561 char *search_regexp_string;
562 search_name = search_pattern;
563 search_regexp_string = segment_next_field(search_pattern);
565 if(!*search_regexp_string) {
567 "Syntax error in the search pattern.\n");
571 recursive_search_in_db(db_root_path, search_name, search_regexp_string);
573 free(search_pattern);