+ /* 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++;
+ 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)) {
+ *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];
+ unsigned long int current_position_in_mail;
+ const char *db_value;
+ int db_key;
+ int hits[MAX_NB_SEARCH_CONDITIONS];
+ int nb_body_conditions, need_time;
+ int nb_extracted_mails;
+ time_t mail_time;
+
+ int m, n;
+
+ nb_extracted_mails = 0;
+
+ if(!global_quiet) {
+ printf("Searching in '%s' ... ", db_filename);
+ fflush(stdout);
+ }
+
+ db_file = safe_fopen(db_filename, "r", "index file for search");
+
+ /* First, check the db file leading line integrity */
+
+ if(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+ if(strncmp(raw_db_line, MYMAIL_DB_MAGIC_TOKEN, strlen(MYMAIL_DB_MAGIC_TOKEN))) {
+ fprintf(stderr,
+ "mymail: Header line in '%s' does not match the mymail db format.\n",
+ db_filename);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ fprintf(stderr,
+ "mymail: Cannot read the header line in '%s'.\n",
+ db_filename);
+ exit(EXIT_FAILURE);
+ }
+
+ /* Then parse the said db file */
+
+ current_position_in_mail = 0;
+
+ for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+
+ nb_body_conditions = 0;
+ need_time = 0;
+ mail_time = 0;
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ if(search_conditions[n].db_key == ID_BODY) {
+ nb_body_conditions++;
+ }
+ else if(search_conditions[n].db_key == ID_TIME_INTERVAL) {
+ need_time = 1;
+ }
+ }
+
+ strcpy(current_mail_filename, "");
+
+ while(fgets(raw_db_line, BUFFER_SIZE, db_file)) {
+ db_value = parse_token(db_key_string, TOKEN_BUFFER_SIZE, ' ', raw_db_line);
+
+ if(strcmp("mail", db_key_string) == 0) {
+ if(current_mail_filename[0]) {
+ if(check_full_mail_match(current_mail_filename,
+ mail_time,
+ nb_search_conditions, search_conditions,
+ nb_body_conditions, hits, current_position_in_mail)) {
+ extract_mail(current_mail_filename, current_position_in_mail, output_file);
+ nb_extracted_mails++;
+ }
+ }
+
+ for(n = 0; n < nb_search_conditions; n++) { hits[n] = 0; }
+ db_value = parse_token(position_in_file_string, TOKEN_BUFFER_SIZE, ' ', db_value);
+ db_value = parse_token(current_mail_filename, PATH_MAX+1, '\n', db_value);
+ current_position_in_mail = atol(position_in_file_string);
+ }
+
+ else {
+ db_key = -1;
+ for(m = 0; (m < MAX_ID) && db_key == -1; m++) {
+ if(strncmp(field_keys[m], db_key_string, strlen(db_key_string)) == 0) {
+ db_key = m;
+ }
+ }
+
+ for(n = 0; n < nb_search_conditions; n++) {
+ hits[n] |= db_line_match_search(&search_conditions[n],
+ db_key, db_value);
+ }
+
+ if(need_time) {
+ update_time(db_key, db_value, &mail_time);
+ }
+ }
+ }
+
+ if(current_mail_filename[0]) {
+ if(check_full_mail_match(current_mail_filename,
+ mail_time,
+ nb_search_conditions, search_conditions,
+ nb_body_conditions, hits, current_position_in_mail)) {
+ extract_mail(current_mail_filename, current_position_in_mail, output_file);
+ nb_extracted_mails++;
+ }
+ }
+
+ fclose(db_file);
+
+ if(!global_quiet) {
+ printf("done.\n");
+ fflush(stdout);
+ }
+
+ return nb_extracted_mails;
+}
+
+int recursive_search_in_db(const char *entry_name, regex_t *db_filename_regexp,
+ int nb_search_conditions,
+ struct search_condition *search_conditions,
+ FILE *output_file) {
+ DIR *dir;
+ struct dirent *dir_e;
+ struct stat sb;
+ char subname[PATH_MAX + 1];
+ int nb_extracted_mails = 0;
+
+ if(lstat(entry_name, &sb) != 0) {
+ fprintf(stderr,
+ "mymail: Cannot stat \"%s\": %s\n",
+ entry_name,
+ strerror(errno));