+int ignore_entry(const char *name) {
+ return
+ strcmp(name, ".") == 0 ||
+ strcmp(name, "..") == 0 ||
+ (name[0] == '.' && name[1] != '/');
+}
+
+int is_a_leading_from_line(char *mbox_line) {
+ return
+ strncmp(mbox_line, "From ", 5) == 0 &&
+ regexec(&global_leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
+}
+
+int db_line_match_search(struct search_condition *condition,
+ int db_key, const char *db_value) {
+
+ return
+ (
+ (condition->db_key == db_key)
+
+ ||
+
+ (condition->db_key == ID_PARTICIPANT && (db_key == ID_LEADING_LINE ||
+ db_key == ID_FROM ||
+ db_key == ID_TO))
+ ||
+
+ (condition->db_key == ID_FROM && db_key == ID_LEADING_LINE)
+
+ )
+
+ &&
+
+ regexec(&condition->db_value_regexp, db_value, 0, 0, 0) == 0;
+}
+
+void update_body_hits(char *mail_filename, int position_in_mail,
+ int nb_search_conditions, struct search_condition *search_conditions,
+ int nb_body_conditions,
+ int *hits) {
+ FILE *mail_file;
+ int header, n;
+ char raw_mbox_line[BUFFER_SIZE];
+ int nb_body_hits;
+
+ nb_body_hits = 0;
+
+ header = 1;
+ mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
+
+ fseek(mail_file, position_in_mail, SEEK_SET);
+
+ if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+ while(nb_body_hits < nb_body_conditions) {
+ if(raw_mbox_line[0] == '\n') { header = 0; }
+
+ if(!header) {
+ for(n = 0; n < nb_search_conditions; n++) {
+ if(search_conditions[n].db_key == ID_BODY && !hits[n]) {
+ hits[n] =
+ (regexec(&search_conditions[n].db_value_regexp, raw_mbox_line, 0, 0, 0) == 0);
+ if(hits[n]) {
+ nb_body_hits++;
+ }
+ }
+ }
+ }
+
+ if(!fgets(raw_mbox_line, BUFFER_SIZE, mail_file) ||
+ (is_a_leading_from_line(raw_mbox_line)))
+ break;
+ }
+ }
+
+ fclose(mail_file);
+}
+
+void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
+ FILE *output_file) {
+ char raw_mbox_line[BUFFER_SIZE];
+ FILE *mail_file;
+
+ /* printf("Extract\n"); */
+
+ mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
+ 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);
+}
+
+int check_full_mail_match(char *current_mail_filename,
+ time_t mail_time,
+ int nb_search_conditions,
+ struct search_condition *search_conditions,
+ int nb_body_conditions,
+ int *hits,
+ int current_position_in_mail) {
+ int n, nb_fulfilled_body_conditions;
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
+ hits[n] = (mail_time >= search_conditions[n].time_start &&
+ (search_conditions[n].time_stop == 0 ||
+ mail_time <= search_conditions[n].time_stop));
+ }
+ }
+
+ /* We first check all conditions but the body ones */
+
+ for(n = 0; n < nb_search_conditions &&
+ ((search_conditions[n].db_key == ID_BODY) ||
+ xor(hits[n], search_conditions[n].negation)); n++);
+
+ if(n == nb_search_conditions) {
+
+ /* Now check the body ones */
+
+ nb_fulfilled_body_conditions = 0;
+
+ if(nb_body_conditions > 0) {
+ update_body_hits(current_mail_filename, current_position_in_mail,
+ nb_search_conditions, search_conditions,
+ nb_body_conditions,
+ hits);
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ if(search_conditions[n].db_key == ID_BODY &&
+ xor(hits[n], search_conditions[n].negation)) {
+ nb_fulfilled_body_conditions++;
+ }
+ }
+ }
+ return nb_body_conditions == nb_fulfilled_body_conditions;
+ } else {
+ return 0;
+ }
+}
+
+/* We use the mail leading line time by default, and if we should and
+ can, we update with the Date: field */
+
+void update_time(int db_key, const char *db_value, time_t *t) {
+ const char *c;
+ struct tm tm;
+
+ if(db_key == ID_LEADING_LINE) {
+ c = db_value;
+ while(*c && *c != ' ') c++; while(*c && *c == ' ') c++;
+ /* printf("From %s", db_value); */
+ strptime(c, "%a %b %e %k:%M:%S %Y", &tm);
+ *t = mktime(&tm);
+ } else {
+ if(!global_use_leading_time) {
+ if(db_key == ID_DATE) {
+ if(strptime(db_value, "%a, %d %b %Y %k:%M:%S", &tm) ||
+ strptime(db_value, "%d %b %Y %k:%M:%S", &tm)) {
+ /* printf("Date: %s", db_value); */
+ *t = mktime(&tm);
+ }
+ }
+ }
+ }
+}
+
+int search_in_db(const char *db_filename,
+ int nb_search_conditions,
+ struct search_condition *search_conditions,
+ FILE *output_file) {
+
+ FILE *db_file;
+ char raw_db_line[BUFFER_SIZE];
+ char current_mail_filename[PATH_MAX + 1];
+ char db_key_string[TOKEN_BUFFER_SIZE];
+ char position_in_file_string[TOKEN_BUFFER_SIZE];