#include <limits.h>
#include <dirent.h>
#include <regex.h>
+#include <time.h>
#define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
#define VERSION "0.9.1"
int action_index;
int quiet;
+time_t being_today;
+
/********************************************************************/
enum {
ID_MAIL = 0,
- ID_FIRST_LINE,
+ ID_LEADING_LINE,
ID_FROM,
ID_TO,
ID_SUBJECT,
ID_DATE,
ID_PARTICIPANT,
ID_BODY,
+ ID_INTERVAL,
MAX_ID
};
static char *field_names[] = {
"mail",
- "first",
+ "lead",
"from",
"to",
"subject",
"date",
"part",
- "body"
+ "body",
+ "interval"
};
/********************************************************************/
int field_id;
int negation;
regex_t regexp;
+ time_t interval_start, interval_stop;
};
/********************************************************************/
static struct parsable_field fields_to_parse[] = {
{
- ID_FIRST_LINE,
+ ID_LEADING_LINE,
"^From ",
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
},
int mbox_line_match_search(struct search_condition *condition,
int mbox_id, char *mbox_value) {
- return
- (condition->field_id == mbox_id ||
- (condition->field_id == ID_PARTICIPANT && (mbox_id == ID_FROM || mbox_id == ID_TO)) ||
- (condition->field_id == ID_FROM && mbox_id == ID_FIRST_LINE))
- &&
- regexec(&condition->regexp, mbox_value, 0, 0, 0) == 0;
+
+ 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,
for(n = 0; n < nb_search_conditions; n++) {
search_field = search_condition_strings[n];
- search_regexp_string = segment_next_field(search_condition_strings[n]);
if(search_field[0] == '!') {
search_field++;
search_conditions[n].negation = 0;
}
- search_conditions[n].field_id = -1;
- for(m = 0; (m < MAX_ID) && search_conditions[n].field_id == -1; m++) {
- if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
- search_conditions[n].field_id = m;
- }
+ if(strcmp(search_field, "today") == 0) {
+ search_conditions[n].field_id = ID_INTERVAL;
+ search_conditions[n].interval_start = time(0) - 3600 * 24;
+ search_conditions[n].interval_stop = 0;
}
- if(search_conditions[n].field_id == -1) {
- fprintf(stderr,
- "mymail: Syntax error in field name \"%s\".\n",
- search_field);
- exit(EXIT_FAILURE);
+ else if(strcmp(search_field, "yesterday") == 0) {
+ search_conditions[n].field_id = ID_INTERVAL;
+ search_conditions[n].interval_start = time(0) - 2 * 3600 * 24;
+ search_conditions[n].interval_stop = time(0) - 3600 * 24;
}
- if(regcomp(&search_conditions[n].regexp,
- search_regexp_string,
- REG_ICASE)) {
- fprintf(stderr,
- "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
- search_regexp_string,
- field_names[search_conditions[n].field_id]);
- exit(EXIT_FAILURE);
+ else {
+ search_regexp_string = segment_next_field(search_condition_strings[n]);
+
+ search_conditions[n].field_id = -1;
+
+ for(m = 0; (m < MAX_ID) && search_conditions[n].field_id == -1; m++) {
+ if(strncmp(field_names[m], search_field, strlen(search_field)) == 0) {
+ search_conditions[n].field_id = m;
+ }
+ }
+
+ if(search_conditions[n].field_id == -1) {
+ fprintf(stderr,
+ "mymail: Syntax error in field name \"%s\".\n",
+ search_field);
+ exit(EXIT_FAILURE);
+ }
+
+ if(regcomp(&search_conditions[n].regexp,
+ search_regexp_string,
+ REG_ICASE)) {
+ fprintf(stderr,
+ "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
+ search_regexp_string,
+ field_names[search_conditions[n].field_id]);
+ exit(EXIT_FAILURE);
+ }
}
}