+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 search_in_db(FILE *db_file,
+ 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, *mbox_value;
+ int mbox_id;
+ int already_written, m, n;
+ int last_mbox_line_was_empty;
+ int nb_body_conditions, nb_fulfilled_body_conditions;
+
+ current_position_in_mail = 0;
+ already_written = 0;
+
+ 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_name = raw_db_line;
+ mbox_value = segment_next_field(raw_db_line);
+
+ if(strcmp("mail", mbox_name) == 0) {
+ char *position_in_file_string;
+ char *mail_filename;
+
+ 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 */
+
+ 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 &&
+ is_a_leading_from_line(raw_mbox_line)))
+ 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 &&
+ is_a_leading_from_line(raw_mbox_line))
+ )
+ break;
+ last_mbox_line_was_empty = (raw_mbox_line[0] == '\n');
+ fprintf(output_file, "%s", raw_mbox_line);
+ }
+ }
+
+ fclose(mail_file);
+ }
+ }
+ }
+
+ for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+
+ position_in_file_string = mbox_value;
+ mail_filename = segment_next_field(mbox_value);
+ current_position_in_mail = atol(position_in_file_string);
+ strcpy(current_mail_filename, mail_filename);
+
+ remove_eof(current_mail_filename);
+ already_written = 0;
+ }
+
+ 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;
+ }
+ }
+ for(n = 0; n < nb_search_conditions; n++) {
+ hits[n] |= mbox_line_match_search(&search_conditions[n],
+ mbox_id, mbox_value);
+ }
+ }
+ }
+}