Cosmetics + increased version number.
[mymail.git] / mymail.c
index fe2d9a0..cc55261 100644 (file)
--- a/mymail.c
+++ b/mymail.c
 #include <time.h>
 
 #define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
-#define VERSION "0.9.1"
+#define VERSION "0.9.2"
 
 #define MAX_NB_SEARCH_CONDITIONS 10
 
 #define BUFFER_SIZE 65536
 #define TOKEN_BUFFER_SIZE 1024
 
+#define 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$"
+
 regex_t leading_from_line_regexp;
 
 /* Global variables! */
@@ -60,6 +62,7 @@ regex_t leading_from_line_regexp;
 int paranoid;
 int quiet;
 char *default_search_field;
+int ignore_dot_files;
 
 /********************************************************************/
 
@@ -162,11 +165,6 @@ char *parse_token(char *token_buffer, size_t token_buffer_size,
   return string;
 }
 
-void remove_eof(char *c) {
-  while(*c && *c != '\n' && *c != '\r') c++;
-  *c = '\0';
-}
-
 /********************************************************************/
 
 /* malloc with error checking.  */
@@ -233,12 +231,12 @@ time_t time_for_past_day(int day) {
 
 int ignore_entry(const char *name) {
   return
-    /* strcmp(name, ".") == 0 || */
-    /* strcmp(name, "..") == 0 || */
-    (name[0] == '.' && name[1] != '/');
+    strcmp(name, ".") == 0 ||
+    strcmp(name, "..") == 0 ||
+    (ignore_dot_files && name[0] == '.' && name[1] != '/');
 }
 
-int is_a_leading_from_line(int last_mbox_line_was_empty, char *mbox_line) {
+int is_a_leading_from_line(char *mbox_line) {
   return
 
     /*
@@ -251,7 +249,6 @@ int is_a_leading_from_line(int last_mbox_line_was_empty, char *mbox_line) {
 
     */
 
-    /* last_mbox_line_was_empty && */
     strncmp(mbox_line, "From ", 5) == 0 &&
     regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
 }
@@ -305,7 +302,6 @@ void update_body_hits(char *mail_filename, int position_in_mail,
                       int *hits) {
   FILE *mail_file;
   int header, n;
-  int last_mbox_line_was_empty;
   char raw_mbox_line[BUFFER_SIZE];
   int nb_body_hits;
 
@@ -325,9 +321,10 @@ void update_body_hits(char *mail_filename, int position_in_mail,
 
   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');
+      /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
+      /* if(last_mbox_line_was_empty) { header = 0; } */
 
-      if(last_mbox_line_was_empty) { header = 0; }
+      if(raw_mbox_line[0] == '\n') { header = 0; }
 
       if(!header) {
         for(n = 0; n < nb_search_conditions; n++) {
@@ -342,7 +339,7 @@ void update_body_hits(char *mail_filename, int position_in_mail,
       }
 
       if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
-         (is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line)))
+         (is_a_leading_from_line(raw_mbox_line)))
         break;
     }
   }
@@ -350,6 +347,36 @@ void update_body_hits(char *mail_filename, int position_in_mail,
   fclose(mail_file);
 }
 
+void write_mail(const char *mail_filename, unsigned long int position_in_mail,
+                FILE *output_file) {
+  char raw_mbox_line[BUFFER_SIZE];
+  FILE *mail_file;
+
+  mail_file = fopen(mail_filename, "r");
+
+  if(!mail_file) {
+    fprintf(stderr,
+            "mymail: Cannot open mbox '%s' for mail extraction.\n",
+            mail_filename);
+    exit(EXIT_FAILURE);
+  }
+
+  fseek(mail_file, position_in_mail, SEEK_SET);
+
+  if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+    fprintf(output_file, "%s", raw_mbox_line);
+    while(1) {
+      if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+         (is_a_leading_from_line(raw_mbox_line))
+         )
+        break;
+      fprintf(output_file, "%s", raw_mbox_line);
+    }
+  }
+
+  fclose(mail_file);
+}
+
 void search_in_db(const char *db_filename,
                   int nb_search_conditions,
                   struct search_condition *search_conditions,
@@ -357,13 +384,11 @@ void search_in_db(const char *db_filename,
 
   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;
 
@@ -382,6 +407,8 @@ void search_in_db(const char *db_filename,
     exit(EXIT_FAILURE);
   }
 
+  /* First, check the db file leading line integrity */
+
   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,
@@ -396,6 +423,8 @@ void search_in_db(const char *db_filename,
     exit(EXIT_FAILURE);
   }
 
+  /* Then parse the said db file */
+
   current_position_in_mail = 0;
   already_written = 0;
 
@@ -426,8 +455,7 @@ void search_in_db(const char *db_filename,
 
         if(n == nb_search_conditions) {
 
-          /* all conditions but the body ones are fine, check the body
-             ones */
+          /* Now check the body ones */
 
           if(nb_body_conditions > 0) {
             update_body_hits(current_mail_filename, current_position_in_mail,
@@ -446,33 +474,7 @@ void search_in_db(const char *db_filename,
           }
 
           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);
+            write_mail(current_mail_filename, current_position_in_mail, output_file);
           }
         }
       }
@@ -480,9 +482,8 @@ void search_in_db(const char *db_filename,
       for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
 
       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);
+      mbox_value = parse_token(current_mail_filename, TOKEN_BUFFER_SIZE, '\n', mbox_value);
       current_position_in_mail = atol(position_in_file_string);
-      remove_eof(current_mail_filename);
       already_written = 0;
     }
 
@@ -570,7 +571,7 @@ void index_mbox(const char *mbox_filename,
   char raw_mbox_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
   char *end_of_full_line;
   FILE *file;
-  int in_header, new_header, last_mbox_line_was_empty;
+  int in_header, new_header;
   unsigned long int position_in_file;
 
   file = fopen(mbox_filename, "r");
@@ -587,10 +588,9 @@ void index_mbox(const char *mbox_filename,
   position_in_file = 0;
   end_of_full_line = 0;
   full_line[0] = '\0';
-  last_mbox_line_was_empty = 1;
 
   while(fgets(raw_mbox_line, BUFFER_SIZE, file)) {
-    if(is_a_leading_from_line(last_mbox_line_was_empty, raw_mbox_line)) {
+    if(is_a_leading_from_line(raw_mbox_line)) {
       if(in_header) {
         fprintf(stderr,
                 "Got a ^\"From \" in the header in %s:%lu.\n",
@@ -604,8 +604,6 @@ void index_mbox(const char *mbox_filename,
       if(in_header) { in_header = 0; }
     }
 
-    last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
-
     if(in_header) {
       if(new_header) {
         fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
@@ -878,9 +876,7 @@ int main(int argc, char **argv) {
   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)) {
+  if(regcomp(&leading_from_line_regexp, LEADING_FROM_LINE_REGEXP, 0)) {
     fprintf(stderr,
             "mymail: Cannot compile leading \"from\" line regexp. That is strange.\n");
     exit(EXIT_FAILURE);
@@ -894,6 +890,7 @@ int main(int argc, char **argv) {
   db_filename_list = 0;
   quiet = 0;
   default_search_field = 0;
+  ignore_dot_files = 1;
 
   setlocale(LC_ALL, "");