+ fprintf(out, " -h, --help\n");
+ fprintf(out, " show this help\n");
+ fprintf(out, " -v, --version\n");
+ fprintf(out, " print the version number\n");
+ fprintf(out, " -q, --quiet\n");
+ fprintf(out, " do not print information during search\n");
+ fprintf(out, " -p <db filename pattern>, --db-pattern <db filename pattern>\n");
+ fprintf(out, " set the db filename pattern for recursive search\n");
+ fprintf(out, " -r <db root path>, --db-root <db root path>\n");
+ fprintf(out, " set the db root path for recursive search\n");
+ fprintf(out, " -l <db filename list>, --db-list <db filename list>\n");
+ fprintf(out, " set the semicolon-separated list of db files for search\n");
+ fprintf(out, " -s <search pattern>, --search <search pattern>\n");
+ fprintf(out, " search for matching mails in the db file\n");
+ fprintf(out, " -d <db filename>, --db-file <db filename>\n");
+ fprintf(out, " set the db filename for indexing\n");
+ fprintf(out, " -i, --index\n");
+ fprintf(out, " index mails\n");
+ fprintf(out, " -o <output filename>, --output <output filename>\n");
+ fprintf(out, " set the result file, use stdout if unset\n");
+ fprintf(out, " -a <search field>, --default-search <search field>\n");
+ fprintf(out, " set the default search field\n");
+}
+
+/*********************************************************************/
+
+time_t time_for_past_day(int day) {
+ time_t t;
+ struct tm *tm;
+ int delta_day;
+ t = time(0);
+ tm = localtime(&t);
+ delta_day = (7 + tm->tm_wday - day) % 7 + 1;
+ return t - delta_day * 3600 * 24 + tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
+}
+
+/*********************************************************************/
+
+int ignore_entry(const char *name) {
+ return
+ strcmp(name, ".") == 0 ||
+ strcmp(name, "..") == 0 ||
+ (ignore_dot_files && name[0] == '.' && name[1] != '/');
+}
+
+int is_a_leading_from_line(char *mbox_line) {
+ return
+
+ /*
+
+ The mbox man page in qmail documentation states:
+
+ > The reader should not attempt to take advantage of the fact
+ > that every From_ line (past the beginning of the file) is
+ > preceded by a blank line.
+
+ */
+
+ strncmp(mbox_line, "From ", 5) == 0 &&
+ regexec(&leading_from_line_regexp, mbox_line, 0, 0, 0) == 0;
+}
+
+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 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 = fopen(mail_filename, "r");
+
+ if(!mail_file) {
+ fprintf(stderr,
+ "mymail: Cannot open mbox '%s' for body scan.\n",
+ mail_filename);
+ exit(EXIT_FAILURE);
+ }
+
+ fseek(mail_file, position_in_mail, SEEK_SET);
+
+ if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
+ while(nb_body_hits < nb_body_conditions) {
+ /* last_mbox_line_was_empty = (raw_mbox_line[0] == '\n'); */
+ /* if(last_mbox_line_was_empty) { header = 0; } */
+
+ if(raw_mbox_line[0] == '\n') { 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_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;
+
+ mail_file = fopen(mail_filename, "r");
+
+ if(!mail_file) {
+ fprintf(stderr,
+ "mymail: Cannot open mbox '%s' for mail extraction.\n",
+ mail_filename);
+ exit(EXIT_FAILURE);
+ }
+
+ 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);