- if(n == nb_search_patterns) {
- FILE *mail_file;
- mail_file = fopen(current_mail_filename, "r");
- if(!mail_file) {
- fprintf(stderr, "mymail: Cannot open mbox '%s'.\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)) {
- printf("%s", raw_mbox_line);
- while(fgets(raw_mbox_line, BUFFER_SIZE, mail_file) &&
- strncmp(raw_mbox_line, "From ", 5)) {
- printf("%s", raw_mbox_line);
+ /* 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 */
+
+ nb_fulfilled_body_conditions = 0;
+
+ if(nb_body_conditions > 0) {
+ FILE *mail_file;
+ int header;
+
+ header = 1;
+ mail_file = fopen(current_mail_filename, "r");
+
+ if(!mail_file) {
+ fprintf(stderr,
+ "mymail: Cannot open mbox '%s' for body scan.\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)) {
+ while(nb_fulfilled_body_conditions < 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_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;
+ }
+ }
+
+ fclose(mail_file);
+ }
+
+ 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 = 1;
+ fprintf(output_file, "%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');
+ fprintf(output_file, "%s", raw_mbox_line);
+ }
+ }
+
+ fclose(mail_file);