Fixed the body content condition.
authorFrancois Fleuret <francois@fleuret.org>
Sun, 3 Feb 2013 19:55:25 +0000 (20:55 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Sun, 3 Feb 2013 19:55:25 +0000 (20:55 +0100)
mymail.c

index 3d5efc0..03ecfb8 100644 (file)
--- a/mymail.c
+++ b/mymail.c
@@ -231,6 +231,8 @@ void search_in_db(int nb_search_conditions,
     }
   }
 
+  strcpy(current_mail_filename, "");
+
   while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
     mbox_name = raw_db_line;
     mbox_value = segment_next_field(raw_db_line);
@@ -239,84 +241,92 @@ void search_in_db(int nb_search_conditions,
       char *position_in_file_string;
       char *mail_filename;
 
-      /* We first check all conditions but the body ones */
+      if(current_mail_filename[0]) {
 
-      for(n = 0; n < nb_search_conditions &&
-            ((search_conditions[n].field_id == ID_BODY) ||
-             xor(hits[n], search_conditions[n].negation)); n++);
+        /* We first check all conditions but the body ones */
 
-      if(n == nb_search_conditions) {
+        for(n = 0; n < nb_search_conditions &&
+              ((search_conditions[n].field_id == ID_BODY) ||
+               xor(hits[n], search_conditions[n].negation)); n++);
 
-        /* all conditions but the body ones are fine, check the body
-           ones */
+        if(n == nb_search_conditions) {
 
-        nb_fulfilled_body_conditions = 0;
+          /* all conditions but the body ones are fine, check the body
+             ones */
 
-        if(nb_body_conditions > 0) {
-          FILE *mail_file;
-          int header;
+          nb_fulfilled_body_conditions = 0;
 
-          header = 1;
-          mail_file = fopen(current_mail_filename, "r");
+          if(nb_body_conditions > 0) {
+            FILE *mail_file;
+            int header;
 
-          if(!mail_file) {
-            fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
-            exit(EXIT_FAILURE);
-          }
+            header = 1;
+            mail_file = fopen(current_mail_filename, "r");
 
-          fseek(mail_file, current_position_in_mail, SEEK_SET);
+            if(!mail_file) {
+              fprintf(stderr,
+                      "mymail: Cannot open mbox '%s' for body scan.\n",
+                      current_mail_filename);
+              exit(EXIT_FAILURE);
+            }
 
-          if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
-            while(nb_fulfilled_body_conditions < nb_body_conditions) {
-              last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+            fseek(mail_file, current_position_in_mail, SEEK_SET);
 
-              if(last_mbox_line_was_empty) { header = 0; }
+            if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+              while(nb_fulfilled_body_conditions < nb_body_conditions) {
+                last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
 
-              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]) {
-                      fprintf(stderr, "** %d %s", header, raw_mbox_line);
-                      nb_fulfilled_body_conditions++;
+                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_fulfilled_body_conditions++;
+                      }
                     }
                   }
                 }
-              }
 
-              if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
-                 (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
+                if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+                   (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0))
+                  break;
+              }
             }
-          }
 
-          fclose(mail_file);
-        }
+            fclose(mail_file);
+          }
 
-        if(nb_body_conditions == nb_fulfilled_body_conditions) {
-          FILE *mail_file;
+          if(nb_body_conditions == nb_fulfilled_body_conditions) {
+            FILE *mail_file;
 
-          mail_file = fopen(current_mail_filename, "r");
+            mail_file = fopen(current_mail_filename, "r");
 
-          if(!mail_file) {
-            fprintf(stderr, "mymail: Cannot open mbox '%s'.\n", current_mail_filename);
-            exit(EXIT_FAILURE);
-          }
+            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);
+            fseek(mail_file, current_position_in_mail, SEEK_SET);
 
-          if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
-            last_mbox_line_was_empty = 1;
-            printf("%s", raw_mbox_line);
-            while(1) {
-              if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
-                 (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0)) break;
-              last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+            if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+              last_mbox_line_was_empty = 1;
               printf("%s", raw_mbox_line);
+              while(1) {
+                if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+                   (last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0))
+                  break;
+                last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+                printf("%s", raw_mbox_line);
+              }
             }
-          }
 
-          fclose(mail_file);
+            fclose(mail_file);
+          }
         }
       }
 
@@ -347,7 +357,8 @@ void search_in_db(int nb_search_conditions,
 }
 
 void recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
-                            int nb_search_conditions, struct search_condition *search_conditions) {
+                            int nb_search_conditions,
+                            struct search_condition *search_conditions) {
   DIR *dir;
   struct dirent *dir_e;
   struct stat sb;