Deals properly with case in the indexing fields.
[mymail.git] / mymail.c
index 6388365..dd84dbe 100644 (file)
--- a/mymail.c
+++ b/mymail.c
  *
  */
 
+/*
+
+  This command is a dumb mail indexer. It can either (1) scan
+  directories containing mbox files, and create a db file containing
+  for each mail a list of fields computed from the header, or (2)
+  read such a db file and get all the mails matching regexp-defined
+  conditions on the fields.
+
+  It is low-tech, simple, light and fast.
+
+*/
+
 #define _GNU_SOURCE
 
 #include <stdio.h>
 #include <limits.h>
 #include <dirent.h>
 #include <regex.h>
+#include <time.h>
+
+#define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
+#define VERSION "0.9.1"
 
-#define VERSION "0.1"
+#define MAX_NB_SEARCH_CONDITIONS 10
 
-#define BUFFER_SIZE 16384
+#define BUFFER_SIZE 65536
+#define TOKEN_BUFFER_SIZE 1024
+
+regex_t leading_from_line_regexp;
+
+/* Global variables! */
+
+int paranoid;
+int quiet;
+char *default_search_field;
+
+/********************************************************************/
+
+enum {
+  ID_MAIL = 0,
+  ID_LEADING_LINE,
+  ID_FROM,
+  ID_TO,
+  ID_SUBJECT,
+  ID_DATE,
+  ID_PARTICIPANT,
+  ID_BODY,
+  ID_INTERVAL,
+  MAX_ID
+};
+
+static char *field_names[] = {
+  "mail",
+  "lead",
+  "from",
+  "to",
+  "subject",
+  "date",
+  "part",
+  "body",
+  "interval"
+};
+
+/********************************************************************/
+
+struct search_condition {
+  int field_id;
+  int negation;
+  regex_t regexp;
+  time_t interval_start, interval_stop;
+};
+
+/********************************************************************/
 
 struct parsable_field {
-  char *name;
+  int id;
+  int cflags;
   char *regexp_string;
   regex_t regexp;
 };
 
-char *db_filename;
-char *search_pattern;
+static struct parsable_field fields_to_parse[] = {
+  {
+    ID_LEADING_LINE,
+    0,
+    "^From ",
+    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+  },
+
+  {
+    ID_FROM,
+    REG_ICASE,
+    "^\\(from:\\|reply-to:\\|sender:\\|return-path:\\)",
+    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+  },
+
+  {
+    ID_TO,
+    REG_ICASE,
+    "^\\(to\\|cc\\|bcc\\): ",
+    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+  },
+
+  {
+    ID_SUBJECT,
+    REG_ICASE,
+    "^subject: ",
+    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+  },
+
+  {
+    ID_DATE,
+    REG_ICASE,
+    "^date: ",
+    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+  },
+
+};
+
+/********************************************************************/
 
-int action_index;
+int xor(int a, int b) {
+  return (a && !b) || (!a && b);
+}
 
-char *segment_next_field(char *current) {
-  while(*current && *current != ' ') current++;
-  *current = '\0'; current++;
-  while(*current && *current == ' ') current++;
-  return current;
+char *parse_token(char *token_buffer, size_t token_buffer_size,
+                  char separator, char *string) {
+  char *u = token_buffer;
+  while(u < token_buffer + token_buffer_size - 1 && *string &&
+        *string != separator) {
+    *(u++) = *(string++);
+  }
+  while(*string == separator) string++;
+  *u = '\0';
+  return string;
 }
 
 void remove_eof(char *c) {
@@ -67,7 +175,7 @@ void *safe_malloc(size_t n) {
   void *p = malloc(n);
   if(!p && n != 0) {
     fprintf(stderr,
-            "mymail: can not allocate memory: %s\n", strerror(errno));
+            "mymail: cannot allocate memory: %s\n", strerror(errno));
     exit(EXIT_FAILURE);
   }
   return p;
@@ -75,186 +183,503 @@ void *safe_malloc(size_t n) {
 
 /*********************************************************************/
 
-void usage(FILE *out) {
+void print_version(FILE *out) {
   fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
+}
+
+void print_usage(FILE *out) {
+  print_version(out);
   fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
   fprintf(out, "\n");
-  fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
+  fprintf(out, "Usage: mymail [options] [<mbox dir1> [<mbox dir2> ...]|<db file1> [<db file2> ...]]\n");
   fprintf(out, "\n");
+  fprintf(out, " -h, --help\n");
+  fprintf(out, "         show this help\n");
+  fprintf(out, " -v, --version\n");
+  fprintf(out, "         print the version number\n");
+  fprintf(out, " -q, --quiet\n");
+  fprintf(out, "         do not print information during search\n");
+  fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
+  fprintf(out, "         set the db filename pattern for recursive search\n");
+  fprintf(out, " -r <db root path>, --db-root <db root path>\n");
+  fprintf(out, "         set the db root path for recursive search\n");
+  fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
+  fprintf(out, "         set the semicolon-separated list of db files for search\n");
+  fprintf(out, " -s <search pattern>, --search <search pattern>\n");
+  fprintf(out, "         search for matching mails in the db file\n");
+  fprintf(out, " -d <db filename>, --db-file <db filename>\n");
+  fprintf(out, "         set the db filename for indexing\n");
+  fprintf(out, " -i, --index\n");
+  fprintf(out, "         index mails\n");
+  fprintf(out, " -o <output filename>, --output <output filename>\n");
+  fprintf(out, "         set the result file, use stdout if unset\n");
+  fprintf(out, " -a <search field>, --default-search <search field>\n");
+  fprintf(out, "         set the default search field\n");
 }
 
 /*********************************************************************/
 
-void search_in_db(const char *search_name, const char *search_regexp_string,
-                  FILE *db_file) {
-  char raw_line[BUFFER_SIZE];
-  char current_mail_filename[BUFFER_SIZE];
-  unsigned int current_position_in_mail;
-  char *name, *value;
-  regex_t regexp;
-  int already_written;
+time_t time_for_past_day(int day) {
+  time_t t;
+  struct tm *tm;
+  int delta_day;
+  t = time(0);
+  tm = localtime(&t);
+  delta_day = (7 + tm->tm_wday - day) % 7 + 1;
+  return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
+}
+
+/*********************************************************************/
+
+int ignore_entry(const char *name) {
+  return
+    /* strcmp(name, ".") == 0 || */
+    /* strcmp(name, "..") == 0 || */
+    (name[0] == '.' && name[1] != '/');
+}
+
+int is_a_leading_from_line(int last_mbox_line_was_empty, char *mbox_line) {
+  return
+
+    /*
+
+      The mbox man page in qmail documentation states:
+
+       > The reader should not attempt to take advantage of the fact
+       > that every From_ line (past the beginning of the file) is
+       > preceded by a blank line.
+
+    */
+
+    /* last_mbox_line_was_empty && */
+    strncmp(mbox_line, "From ", 5) == 0 &&
+    regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
+}
+
+int mbox_line_match_search(struct search_condition *condition,
+                           int mbox_id, char *mbox_value) {
+
+  if(condition->field_id == ID_INTERVAL) {
+    if(mbox_id == ID_LEADING_LINE) {
+      char *c;
+      time_t t;
+      struct tm tm;
+
+      c = mbox_value;
+      while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
+      strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
+      t = mktime(&tm);
+
+      return (t >= condition->interval_start &&
+              (condition->interval_stop == 0 ||
+               t <= condition->interval_stop));
+    } else {
+      return 0;
+    }
+  } else {
+    return
+      (
+
+       (condition->field_id == mbox_id)
+
+       ||
+
+       (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_LEADING_LINE ||
+                                                  mbox_id == ID_FROM ||
+                                                  mbox_id == ID_TO))
+       ||
+
+       (condition->field_id == ID_FROM && mbox_id == ID_LEADING_LINE)
+
+       )
+      &&
+      regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
+  }
+}
+
+void update_body_hits(char *mail_filename, int position_in_mail,
+                      int nb_search_conditions, struct search_condition *search_conditions,
+                      int nb_body_conditions,
+                      int *hits) {
+  FILE *mail_file;
+  int header, n;
+  int last_mbox_line_was_empty;
+  char raw_mbox_line[BUFFER_SIZE];
+  int nb_body_hits;
+
+  nb_body_hits = 0;
+
+  header = 1;
+  mail_file = fopen(mail_filename, "r");
+
+  if(!mail_file) {
+    fprintf(stderr,
+            "mymail: Cannot open mbox '%s' for body scan.\n",
+            mail_filename);
+    exit(EXIT_FAILURE);
+  }
+
+  fseek(mail_file, position_in_mail, SEEK_SET);
 
-  if(regcomp(&regexp,
-             search_regexp_string,
-             REG_ICASE)) {
+  if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+    while(nb_body_hits < nb_body_conditions) {
+      last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+
+      if(last_mbox_line_was_empty) { header = 0; }
+
+      if(!header) {
+        for(n = 0; n < nb_search_conditions; n++) {
+          if(search_conditions[n].field_id == ID_BODY && !hits[n]) {
+            hits[n] =
+              (regexec(&search_conditions[n].regexp, raw_mbox_line, 0, 0, 0) == 0);
+            if(hits[n]) {
+              nb_body_hits++;
+            }
+          }
+        }
+      }
+
+      if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+         (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line)))
+        break;
+    }
+  }
+
+  fclose(mail_file);
+}
+
+void search_in_db(const char *db_filename,
+                  int nb_search_conditions,
+                  struct search_condition *search_conditions,
+                  FILE *output_file) {
+
+  int hits[MAX_NB_SEARCH_CONDITIONS];
+  char raw_db_line[BUFFER_SIZE];
+  char raw_mbox_line[BUFFER_SIZE];
+  char current_mail_filename[PATH_MAX + 1];
+  unsigned long int current_position_in_mail;
+  char mbox_name[TOKEN_BUFFER_SIZE], *mbox_value;
+  int mbox_id;
+  int already_written, m, n;
+  int last_mbox_line_was_empty;
+  int nb_body_conditions, nb_fulfilled_body_conditions;
+  FILE *db_file;
+
+  if(!quiet) {
+    printf("Searching in '%s' ... ", db_filename);
+    fflush(stdout);
+  }
+
+  db_file = fopen(db_filename, "r");
+
+  if(!db_file) {
     fprintf(stderr,
-            "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
-            search_regexp_string,
-            search_name);
+            "mymail: Cannot open \"%s\" for reading: %s\n",
+            db_filename,
+            strerror(errno));
+    exit(EXIT_FAILURE);
+  }
+
+  if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+    if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
+      fprintf(stderr,
+              "mymail: Header line in '%s' does not match the mymail db format.\n",
+              db_filename);
+      exit(EXIT_FAILURE);
+    }
+  } else {
+    fprintf(stderr,
+            "mymail: Cannot read the header line in '%s'.\n",
+            db_filename);
     exit(EXIT_FAILURE);
   }
 
   current_position_in_mail = 0;
   already_written = 0;
 
-  while(fgets(raw_line, BUFFER_SIZE, db_file)) {
-    name = raw_line;
-    value = segment_next_field(raw_line);
+  for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+
+  nb_body_conditions = 0;
+  for(n = 0; n < nb_search_conditions; n++) {
+    if(search_conditions[n].field_id == ID_BODY) {
+      nb_body_conditions++;
+    }
+  }
+
+  strcpy(current_mail_filename, "");
+
+  while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+    mbox_value = parse_token(mbox_name, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
+
+    if(strcmp("mail", mbox_name) == 0) {
+      char position_in_file_string[TOKEN_BUFFER_SIZE];
+
+      if(current_mail_filename[0]) {
+
+        /* We first check all conditions but the body ones */
+
+        for(n = 0; n < nb_search_conditions &&
+              ((search_conditions[n].field_id == ID_BODY) ||
+               xor(hits[n], search_conditions[n].negation)); n++);
+
+        if(n == nb_search_conditions) {
+
+          /* all conditions but the body ones are fine, check the body
+             ones */
+
+          if(nb_body_conditions > 0) {
+            update_body_hits(current_mail_filename, current_position_in_mail,
+                             nb_search_conditions, search_conditions,
+                             nb_body_conditions,
+                             hits);
+          }
+
+          nb_fulfilled_body_conditions = 0;
+
+          for(n = 0; n < nb_search_conditions; n++) {
+            if(search_conditions[n].field_id == ID_BODY &&
+               xor(hits[n], search_conditions[n].negation)) {
+              nb_fulfilled_body_conditions++;
+            }
+          }
+
+          if(nb_body_conditions == nb_fulfilled_body_conditions) {
+            FILE *mail_file;
+
+            mail_file = fopen(current_mail_filename, "r");
+
+            if(!mail_file) {
+              fprintf(stderr,
+                      "mymail: Cannot open mbox '%s' for mail extraction.\n",
+                      current_mail_filename);
+              exit(EXIT_FAILURE);
+            }
+
+            fseek(mail_file, current_position_in_mail, SEEK_SET);
+
+            if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+              last_mbox_line_was_empty = 0;
+              fprintf(output_file, "%s", raw_mbox_line);
+              while(1) {
+                if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+                   (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line))
+                   )
+                  break;
+                last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+                fprintf(output_file, "%s", raw_mbox_line);
+              }
+            }
+
+            fclose(mail_file);
+          }
+        }
+      }
 
-    /* printf("LINE [%s] %s", name, value); */
+      for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
 
-    if(strcmp("mail", name) == 0) {
-      char *position_in_file_string = value;
-      char *mail_filename = segment_next_field(value);
-      current_position_in_mail = atoi(position_in_file_string);
-      strcpy(current_mail_filename, mail_filename);
+      mbox_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', mbox_value);
+      mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, ' ', mbox_value);
+      current_position_in_mail = atol(position_in_file_string);
       remove_eof(current_mail_filename);
-      /* printf("READING [%s]\n", current_mail_filename); */
       already_written = 0;
-    } else if(!already_written) {
-      if(strcmp(search_name, name) == 0 && regexec(&regexp, value, 0, 0, 0) == 0) {
-        FILE *mail_file;
-        /* printf("%s:%u\n", current_mail_filename, current_position_in_mail); */
-        mail_file = fopen(current_mail_filename, "r");
-        if(!mail_file) {
-          fprintf(stderr, "mymail: Can not open `%s'.\n", current_mail_filename);
-          exit(EXIT_FAILURE);
-        }
-        fseek(mail_file, current_position_in_mail, SEEK_SET);
-        if(fgets(raw_line, BUFFER_SIZE, mail_file)) {
-          printf("%s", raw_line);
-          while(fgets(raw_line, BUFFER_SIZE, mail_file) &&
-                strncmp(raw_line, "From ", 5)) {
-            printf("%s", raw_line);
-          }
+    }
+
+    else {
+      mbox_id = -1;
+      for(m = 0; (m < MAX_ID) && mbox_id == -1; m++) {
+        if(strncmp(field_names[m], mbox_name, strlen(mbox_name)) == 0) {
+          mbox_id = m;
         }
-        fclose(mail_file);
-        already_written = 1;
+      }
+      for(n = 0; n < nb_search_conditions; n++) {
+        hits[n] |= mbox_line_match_search(&search_conditions[n],
+                                          mbox_id, mbox_value);
       }
     }
   }
+
+  fclose(db_file);
+
+  if(!quiet) {
+    printf("done.\n");
+    fflush(stdout);
+  }
 }
 
+void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
+                            int nb_search_conditions,
+                            struct search_condition *search_conditions,
+                            FILE *output_file) {
+  DIR *dir;
+  struct dirent *dir_e;
+  struct stat sb;
+  char subname[PATH_MAX + 1];
+
+  if(lstat(entry_name, &sb) != 0) {
+    fprintf(stderr,
+            "mymail: Cannot stat \"%s\": %s\n",
+            entry_name,
+            strerror(errno));
+    exit(EXIT_FAILURE);
+  }
+
+  dir = opendir(entry_name);
+
+  if(dir) {
+    while((dir_e = readdir(dir))) {
+      if(!ignore_entry(dir_e->d_name)) {
+        snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
+        recursive_search_in_db(subname, db_filename_regexp,
+                               nb_search_conditions, search_conditions,
+                               output_file);
+      }
+    }
+    closedir(dir);
+  }
+
+  else {
+    const char *s = entry_name, *filename = entry_name;
+    while(*s) { if(*s == '/') { filename = s+1; } s++; }
+
+    if(regexec(db_filename_regexp, filename, 0, 0, 0) == 0) {
+      search_in_db(entry_name, nb_search_conditions, search_conditions, output_file);
+    }
+  }
+}
 
 /*********************************************************************/
 
-void read_file(const char *input_filename,
-               int nb_fields_to_parse, struct parsable_field *fields_to_parse,
-               FILE *db_file) {
-  char raw_line[BUFFER_SIZE];
+void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
+                         char *raw_mbox_line, FILE *db_file) {
+  regmatch_t matches;
+  int f;
+  for(f = 0; f < nb_fields_to_parse; f++) {
+    if(regexec(&fields_to_parse[f].regexp, raw_mbox_line, 1, &matches, 0) == 0) {
+      fprintf(db_file, "%s %s\n",
+              field_names[fields_to_parse[f].id],
+              raw_mbox_line + matches.rm_eo);
+    }
+  }
+}
+
+void index_mbox(const char *mbox_filename,
+                int nb_fields_to_parse, struct parsable_field *fields_to_parse,
+                FILE *db_file) {
+  char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
+  char *end_of_full_line;
   FILE *file;
-  int in_header, new_header;
-  unsigned int position_in_file;
+  int in_header, new_header, last_mbox_line_was_empty;
+  unsigned long int position_in_file;
 
-  file = fopen(input_filename, "r");
+  file = fopen(mbox_filename, "r");
 
   if(!file) {
-    fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
-    exit(EXIT_FAILURE);
+    fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
+    if(paranoid) { exit(EXIT_FAILURE); }
+    return;
   }
 
   in_header = 0;
   new_header = 0;
 
   position_in_file = 0;
+  end_of_full_line = 0;
+  full_line[0] = '\0';
+  last_mbox_line_was_empty = 1;
 
-  while(fgets(raw_line, BUFFER_SIZE, file)) {
-    if(strncmp(raw_line, "From ", 5) == 0) {
+  while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
+    if(is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line)) {
       if(in_header) {
         fprintf(stderr,
-                "Got a 'From ' in the header in %s:%u.\n",
-                input_filename, position_in_file);
-        fprintf(stderr, "%s", raw_line);
-        exit(EXIT_FAILURE);
+                "Got a ^\"From \" in the header in %s:%lu.\n",
+                mbox_filename, position_in_file);
+        fprintf(stderr, "%s", raw_mbox_line);
+        if(paranoid) { exit(EXIT_FAILURE); }
       }
       in_header = 1;
       new_header = 1;
-    } else if(strncmp(raw_line, "\n", 1) == 0) {
+    } else if(raw_mbox_line[0] == '\n') {
       if(in_header) { in_header = 0; }
     }
 
-    /* if(in_header) { */
-    /* printf("LINE.H %s", raw_line); */
-    /* } else { */
-    /* printf("LINE.B %s", raw_line); */
-    /* } */
+    last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
 
     if(in_header) {
-      int f;
-      regmatch_t matches;
       if(new_header) {
-        fprintf(db_file, "mail %u %s\n", position_in_file, input_filename);
+        fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
         new_header = 0;
       }
-      for(f = 0; f < nb_fields_to_parse; f++) {
-        if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
-          fprintf(db_file, "%s %s",
-                  fields_to_parse[f].name,
-                  raw_line + matches.rm_eo);
+
+      if(raw_mbox_line[0] == ' ' || raw_mbox_line[0] == '\t') {
+        char *start = raw_mbox_line;
+        while(*start == ' ' || *start == '\t') start++;
+        *(end_of_full_line++) = ' ';
+        strcpy(end_of_full_line, start);
+        while(*end_of_full_line && *end_of_full_line != '\n') {
+          end_of_full_line++;
         }
+        *end_of_full_line = '\0';
       }
+
+      else {
+        /*
+          if(!((raw_mbox_line[0] >= 'a' && raw_mbox_line[0] <= 'z') ||
+          (raw_mbox_line[0] >= 'A' && raw_mbox_line[0] <= 'Z'))) {
+          fprintf(stderr,
+          "Header line syntax error %s:%lu.\n",
+          mbox_filename, position_in_file);
+          fprintf(stderr, "%s", raw_mbox_line);
+          }
+        */
+
+        if(full_line[0]) {
+          index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
+        }
+
+        end_of_full_line = full_line;
+        strcpy(end_of_full_line, raw_mbox_line);
+        while(*end_of_full_line && *end_of_full_line != '\n') {
+          end_of_full_line++;
+        }
+        *end_of_full_line = '\0';
+      }
+
     }
 
-    position_in_file += strlen(raw_line);
+    position_in_file += strlen(raw_mbox_line);
   }
 
   fclose(file);
 }
 
-int ignore_entry(const char *name) {
-  return
-    strcmp(name, ".") == 0 ||
-    strcmp(name, "..") == 0 ||
-    (name[0] == '.' && name[1] != '/');
-}
-
-void process_entry(const char *dir_name,
-                   int nb_fields_to_parse, struct parsable_field *fields_to_parse,
-                   FILE *db_file) {
+void recursive_index_mbox(FILE *db_file,
+                          const char *entry_name,
+                          int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
   DIR *dir;
   struct dirent *dir_e;
   struct stat sb;
   char subname[PATH_MAX + 1];
 
-  if(lstat(dir_name, &sb) != 0) {
+  if(lstat(entry_name, &sb) != 0) {
     fprintf(stderr,
-            "mymail: Can not stat \"%s\": %s\n",
-            dir_name,
+            "mymail: Cannot stat \"%s\": %s\n",
+            entry_name,
             strerror(errno));
     exit(EXIT_FAILURE);
-  } else {
   }
 
-  if(S_ISLNK(sb.st_mode)) {
-    return;
-  }
-
-  dir = opendir(dir_name);
+  dir = opendir(entry_name);
 
   if(dir) {
-    printf("Processing directory '%s'.\n", dir_name);
     while((dir_e = readdir(dir))) {
       if(!ignore_entry(dir_e->d_name)) {
-        snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
-        process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
+        snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
+        recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
       }
     }
     closedir(dir);
   } else {
-    if(S_ISREG(sb.st_mode)) {
-      /* printf("Processing regular file '%s'.\n", dir_name); */
-      read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
-    }
+    index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
   }
 }
 
@@ -262,45 +687,211 @@ void process_entry(const char *dir_name,
 
 /* For long options that have no equivalent short option, use a
    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
-enum
-{
+enum {
   OPT_BASH_MODE = CHAR_MAX + 1
 };
 
 static struct option long_options[] = {
   { "help", no_argument, 0, 'h' },
-  { "db-prefix", 1, 0, 'p' },
-  { "search-pattern", 1, 0, 's' },
+  { "version", no_argument, 0, 'v' },
+  { "quiet", no_argument, 0, 'q' },
+  { "db-file", 1, 0, 'd' },
+  { "db-pattern", 1, 0, 'p' },
+  { "db-root", 1, 0, 'r' },
+  { "db-list", 1, 0, 'l' },
+  { "search", 1, 0, 's' },
   { "index", 0, 0, 'i' },
+  { "output", 1, 0, 'o' },
+  { "default-search", 1, 0, 'a' },
   { 0, 0, 0, 0 }
 };
 
-static struct parsable_field fields_to_parse[] = {
-  {
-    "from",
-    "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
-    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-  },
+/*********************************************************************/
 
-  {
-    "dest",
-    "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
-    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-  },
-};
+void init_condition(struct search_condition *condition, char *full_string) {
+  char full_search_field[TOKEN_BUFFER_SIZE], *search_field;
+  int m;
+  char *string;
+
+  string = parse_token(full_search_field, TOKEN_BUFFER_SIZE, ' ', full_string);
+  search_field = full_search_field;
+
+  if(search_field[0] == '!') {
+    search_field++;
+    condition->negation = 1;
+  } else {
+    condition->negation = 0;
+  }
+
+  /* Recently */
+
+  if(strcmp(search_field, "8h") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time(0) - 3600 * 8;
+    condition->interval_stop = 0;
+  }
+
+  else if(strcmp(search_field, "week") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time(0) - 3600 * 24 * 7;
+    condition->interval_stop = 0;
+  }
+
+  else if(strcmp(search_field, "month") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time(0) - 3600 * 24 * 31;
+    condition->interval_stop = 0;
+  }
+
+  else if(strcmp(search_field, "24h") == 0 ||
+          strcmp(search_field, "today") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time(0) - 3600 * 24;
+    condition->interval_stop = 0;
+  }
+
+  /* Yesterday */
+
+  else if(strcmp(search_field, "yesterday") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time(0) - 2 * 3600 * 24;
+    condition->interval_stop = condition->interval_start + 3600 * 24;
+  }
+
+  /* Week days */
+
+  else if(strcmp(search_field, "monday") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time_for_past_day(1);
+    condition->interval_stop = condition->interval_start + 3600 * 24;
+  }
+
+  else if(strcmp(search_field, "tuesday") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time_for_past_day(2);
+    condition->interval_stop = condition->interval_start + 3600 * 24;
+  }
+
+  else if(strcmp(search_field, "wednesday") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time_for_past_day(3);
+    condition->interval_stop = condition->interval_start + 3600 * 24;
+  }
+
+  else if(strcmp(search_field, "thursday") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time_for_past_day(4);
+    condition->interval_stop = condition->interval_start + 3600 * 24;
+  }
+
+  else if(strcmp(search_field, "friday") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time_for_past_day(5);
+    condition->interval_stop = condition->interval_start + 3600 * 24;
+  }
+
+  else if(strcmp(search_field, "saturday") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time_for_past_day(6);
+    condition->interval_stop = condition->interval_start + 3600 * 24;
+  }
+
+  else if(strcmp(search_field, "sunday") == 0) {
+    condition->field_id = ID_INTERVAL;
+    condition->interval_start = time_for_past_day(7);
+    condition->interval_stop = condition->interval_start + 3600 * 24;
+  }
+
+  else {
+
+    /* header-related conditions */
+
+    condition->field_id = -1;
+
+    for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
+      if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
+        condition->field_id = m;
+      }
+    }
+
+    if(condition->field_id == -1) {
+      if(default_search_field) {
+        for(m = 0; (m < MAX_ID) && condition->field_id == -1; m++) {
+          if(strncmp(field_names[m],
+                     default_search_field, strlen(default_search_field)) == 0) {
+            condition->field_id = m;
+          }
+        }
+        string = full_string;
+      }
+    }
+
+    if(condition->field_id == -1) {
+      fprintf(stderr,
+              "mymail: Syntax error in field name \"%s\".\n",
+              search_field);
+      exit(EXIT_FAILURE);
+    }
+
+    if(regcomp(&condition->regexp,
+               string,
+               REG_ICASE)) {
+      fprintf(stderr,
+              "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
+              string,
+              field_names[condition->field_id]);
+      exit(EXIT_FAILURE);
+    }
+  }
+}
+
+void free_condition(struct search_condition *condition) {
+  if(condition->field_id != ID_INTERVAL) {
+    regfree(&condition->regexp);
+  }
+}
+
+/*********************************************************************/
+/*********************************************************************/
+/*********************************************************************/
 
 int main(int argc, char **argv) {
+  char *db_filename;
+  char *db_filename_regexp_string;
+  char *db_root_path;
+  char *db_filename_list;
+  char output_filename[PATH_MAX + 1];
+  int action_index;
   int error = 0, show_help = 0;
   const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
   char c;
-  int f;
+  int f, n;
+  int nb_search_conditions;
+  FILE *output_file;
+  struct search_condition search_conditions[MAX_NB_SEARCH_CONDITIONS];
+
+  if(regcomp(&leading_from_line_regexp,
+             "^From [^ ]*  \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\) \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\) [ 123][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]\n$",
+             0)) {
+    fprintf(stderr,
+            "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
+    exit(EXIT_FAILURE);
+  }
 
+  paranoid = 0;
   action_index = 0;
-  search_pattern = 0;
+  db_filename = 0;
+  db_filename_regexp_string = 0;
+  db_root_path = 0;
+  db_filename_list = 0;
+  quiet = 0;
+  default_search_field = 0;
 
   setlocale(LC_ALL, "");
 
-  while ((c = getopt_long(argc, argv, "hip:s:",
+  nb_search_conditions = 0;
+
+  while ((c = getopt_long(argc, argv, "hvqip:s:d:r:l:o:a:",
                           long_options, NULL)) != -1) {
 
     switch(c) {
@@ -309,21 +900,49 @@ int main(int argc, char **argv) {
       show_help = 1;
       break;
 
+    case 'v':
+      print_version(stdout);
+      break;
+
+    case 'q':
+      quiet = 1;
+      break;
+
     case 'i':
       action_index = 1;
       break;
 
-    case 'p':
+    case 'd':
       db_filename = strdup(optarg);
       break;
 
+    case 'p':
+      db_filename_regexp_string = strdup(optarg);
+      break;
+
+    case 'o':
+      strncpy(output_filename, optarg, PATH_MAX);
+      break;
+
+    case 'r':
+      db_root_path = strdup(optarg);
+      break;
+
+    case 'l':
+      db_filename_list = strdup(optarg);
+      break;
+
     case 's':
-      if(search_pattern) {
-        fprintf(stderr,
-                "mymail: Search pattern already defined.\n");
+      if(nb_search_conditions == MAX_NB_SEARCH_CONDITIONS) {
+        fprintf(stderr, "mymail: Too many search patterns.\n");
         exit(EXIT_FAILURE);
       }
-      search_pattern = strdup(optarg);
+      init_condition(&search_conditions[nb_search_conditions], optarg);
+      nb_search_conditions++;
+      break;
+
+    case 'a':
+      default_search_field = optarg;
       break;
 
     default:
@@ -333,24 +952,74 @@ int main(int argc, char **argv) {
   }
 
   if(!db_filename) {
-    db_filename = strdup("/tmp/mymail");
+    char *default_db_filename = getenv("MYMAIL_DB_FILE");
+
+    if(!default_db_filename) {
+      default_db_filename = "mymail.db";
+    }
+
+    db_filename = strdup(default_db_filename);
+  }
+
+  if(!db_filename_regexp_string) {
+    char *default_db_filename_regexp_string = getenv("MYMAIL_DB_PATTERN");
+
+    if(!default_db_filename_regexp_string) {
+      default_db_filename_regexp_string = "^mymail.db$";
+    }
+
+    db_filename_regexp_string = strdup(default_db_filename_regexp_string);
+  }
+
+  if(!db_root_path) {
+    char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
+
+    if(default_db_root_path) {
+      db_root_path = strdup(default_db_root_path);
+    }
+  }
+
+  if(!db_filename_list) {
+    char *default_db_filename_list = getenv("MYMAIL_DB_LIST");
+
+    if(default_db_filename_list) {
+      db_filename_list = strdup(default_db_filename_list);
+    }
+  }
+
+  if(output_filename[0]) {
+    output_file = fopen(output_filename, "w");
+
+    if(!output_file) {
+      fprintf(stderr,
+              "mymail: Cannot open result file \"%s\" for writing: %s\n",
+              output_filename,
+              strerror(errno));
+      exit(EXIT_FAILURE);
+    }
+  } else {
+    output_file = stdout;
+    quiet = 1;
   }
 
   if(error) {
-    usage(stderr);
+    print_usage(stderr);
     exit(EXIT_FAILURE);
   }
 
   if(show_help) {
-    usage(stdout);
+    print_usage(stdout);
     exit(EXIT_SUCCESS);
   }
 
   if(action_index) {
-    FILE *db_file = fopen(db_filename, "w");
+    FILE *db_file;
+
+    db_file = fopen(db_filename, "w");
+
     if(!db_file) {
       fprintf(stderr,
-              "mymail: Can not open \"%s\" for writing: %s\n",
+              "mymail: Cannot open \"%s\" for writing: %s\n",
               db_filename,
               strerror(errno));
       exit(EXIT_FAILURE);
@@ -359,21 +1028,25 @@ int main(int argc, char **argv) {
     for(f = 0; f < nb_fields_to_parse; f++) {
       if(regcomp(&fields_to_parse[f].regexp,
                  fields_to_parse[f].regexp_string,
-                 REG_ICASE)) {
+                 fields_to_parse[f].cflags)) {
         fprintf(stderr,
                 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
                 fields_to_parse[f].regexp_string,
-                fields_to_parse[f].name);
+                field_names[fields_to_parse[f].id]);
         exit(EXIT_FAILURE);
       }
     }
 
+    fprintf(db_file, "%s version_%s raw\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
+
     while(optind < argc) {
-      process_entry(argv[optind],
-                    nb_fields_to_parse, fields_to_parse, db_file);
+      recursive_index_mbox(db_file,
+                           argv[optind],
+                           nb_fields_to_parse, fields_to_parse);
       optind++;
     }
 
+    fflush(db_file);
     fclose(db_file);
 
     for(f = 0; f < nb_fields_to_parse; f++) {
@@ -382,39 +1055,70 @@ int main(int argc, char **argv) {
   }
 
   else {
-    if(search_pattern) {
-      FILE *db_file;
-      char *search_name;
-      char *search_regexp_string;
-      search_name = search_pattern;
-      search_regexp_string = segment_next_field(search_pattern);
-      if(!*search_regexp_string) {
-        fprintf(stderr,
-                "Syntax error in the search pattern.\n");
-        exit(EXIT_FAILURE);
+
+    if(nb_search_conditions > 0) {
+
+      /* Recursive search if db_root_path is set */
+
+      if(db_root_path) {
+        regex_t db_filename_regexp;
+        if(regcomp(&db_filename_regexp,
+                   db_filename_regexp_string,
+                   0)) {
+          fprintf(stderr,
+                  "mymail: Syntax error in regexp \"%s\".\n",
+                  db_filename_regexp_string);
+          exit(EXIT_FAILURE);
+        }
+
+        recursive_search_in_db(db_root_path, &db_filename_regexp,
+                               nb_search_conditions, search_conditions,
+                               output_file);
+
+        regfree(&db_filename_regexp);
       }
 
-      /* printf("Starting search in %s for field \"%s\" matching \"%s\".\n", */
-             /* db_filename, */
-             /* search_name, */
-             /* search_regexp_string); */
+      /* Search in all db files listed in db_filename_list */
 
-      db_file = fopen(db_filename, "r");
+      if(db_filename_list) {
+        char db_filename[PATH_MAX + 1];
+        char *s;
 
-      if(!db_file) {
-        fprintf(stderr,
-                "mymail: Can not open \"%s\" for reading: %s\n",
-                db_filename,
-                strerror(errno));
-        exit(EXIT_FAILURE);
+        s = db_filename_list;
+
+        while(*s) {
+          s = parse_token(db_filename, PATH_MAX + 1, ';', s);
+
+          if(db_filename[0]) {
+            search_in_db(db_filename, nb_search_conditions, search_conditions, output_file);
+          }
+        }
       }
 
-      search_in_db(search_name, search_regexp_string, db_file);
+      /* Search in all db files listed in the command arguments */
 
-      fclose(db_file);
-      free(search_pattern);
+      while(optind < argc) {
+        search_in_db(argv[optind], nb_search_conditions, search_conditions, output_file);
+        optind++;
+      }
     }
   }
 
+  for(n = 0; n < nb_search_conditions; n++) {
+    free_condition(&search_conditions[n]);
+  }
+
+  if(output_file != stdout) {
+    fflush(output_file);
+    fclose(output_file);
+  }
+
+  free(db_filename);
+  free(db_filename_regexp_string);
+  free(db_root_path);
+  free(db_filename_list);
+
+  regfree(&leading_from_line_regexp);
+
   exit(EXIT_SUCCESS);
 }