From aad323611737c25595b8729bfc2cf06ae83cad8f Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Sat, 2 Feb 2013 14:24:53 +0100 Subject: [PATCH] Cosmetics + now check that each ^"From " line follows an empty line. --- mymail.c | 89 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/mymail.c b/mymail.c index b114e8b..eb329b1 100644 --- a/mymail.c +++ b/mymail.c @@ -51,8 +51,16 @@ #define BUFFER_SIZE 65536 +char *db_filename; +char *db_root_path; + +int paranoid; +int action_index; + +/********************************************************************/ + enum { - ID_MAIL, + ID_MAIL = 0, ID_FROM, ID_DEST, ID_SUBJECT, @@ -68,23 +76,48 @@ static char *field_names[] = { "part" }; +/********************************************************************/ + struct search_request { int field_id; int negation; regex_t regexp; }; +/********************************************************************/ + struct parsable_field { int id; char *regexp_string; regex_t regexp; }; -char *db_filename; -char *db_root_path; +static struct parsable_field fields_to_parse[] = { + { + ID_FROM, + "^\\(From \\|[Ff][Rr][Oo][Mm]:\\|[R][r][E][e][P][p][L][l][Y][y]-[T][t][O][o]:\\)", + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } + }, -int paranoid; -int action_index; + { + ID_DEST, + "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): ", + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } + }, + + { + ID_SUBJECT, + "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: ", + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } + }, + +}; + +/********************************************************************/ + +int xor(int a, int b) { + return (a && !b) || (!a && b); +} char *segment_next_field(char *current) { while(*current && *current != ' ') current++; @@ -167,6 +200,7 @@ void search_in_db(int nb_search_patterns, char *mbox_name, *mbox_value; int mbox_id; int already_written, m, n; + int last_mbox_line_was_empty; current_position_in_mail = 0; already_written = 0; @@ -181,9 +215,11 @@ void search_in_db(int nb_search_patterns, char *position_in_file_string; char *mail_filename; - for(n = 0; n < nb_search_patterns && - ((hits[n] && !search_requests[n].negation) || - (!hits[n] && search_requests[n].negation)); n++); + for(n = 0; n < nb_search_patterns && xor(hits[n], search_requests[n].negation); n++); + + /* for(n = 0; n < nb_search_patterns && */ + /* ((hits[n] && !search_requests[n].negation) || */ + /* (!hits[n] && search_requests[n].negation)); n++); */ if(n == nb_search_patterns) { FILE *mail_file; @@ -198,9 +234,12 @@ void search_in_db(int nb_search_patterns, 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(fgets(raw_mbox_line, BUFFER_SIZE, mail_file) && - strncmp(raw_mbox_line, "From ", 5)) { + 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); } } @@ -320,7 +359,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; + int in_header, new_header, last_mbox_line_was_empty; unsigned long int position_in_file; file = fopen(mbox_filename, "r"); @@ -337,9 +376,10 @@ 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(strncmp(raw_mbox_line, "From ", 5) == 0) { + if(last_mbox_line_was_empty && strncmp(raw_mbox_line, "From ", 5) == 0) { if(in_header) { fprintf(stderr, "Got a ^\"From \" in the header in %s:%lu.\n", @@ -349,10 +389,12 @@ void index_mbox(const char *mbox_filename, } in_header = 1; new_header = 1; - } else if(strncmp(raw_mbox_line, "\n", 1) == 0) { + } else if(raw_mbox_line[0] == '\n') { 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); @@ -450,27 +492,6 @@ static struct option long_options[] = { { 0, 0, 0, 0 } }; -static struct parsable_field fields_to_parse[] = { - { - ID_FROM, - "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *", - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } - }, - - { - ID_DEST, - "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *", - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } - }, - - { - ID_SUBJECT, - "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: *", - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } - }, - -}; - /*********************************************************************/ int main(int argc, char **argv) { -- 2.20.1