#include <dirent.h>
#include <regex.h>
+#define MYMAIL_DB_MAGIC_TOKEN "mymail_index_file"
#define VERSION "0.1"
#define BUFFER_SIZE 65536
};
char *db_filename;
+char *db_root_path;
+
+int multi_db_files;
char *search_pattern;
int paranoid;
/*********************************************************************/
+int ignore_entry(const char *name) {
+ return
+ /* strcmp(name, ".") == 0 || */
+ /* strcmp(name, "..") == 0 || */
+ (name[0] == '.' && name[1] != '/');
+}
+
void search_in_db(const char *search_name, const char *search_regexp_string,
FILE *db_file) {
+
char raw_line[BUFFER_SIZE];
- char current_mail_filename[BUFFER_SIZE];
+ char current_mail_filename[PATH_MAX + 1];
unsigned long int current_position_in_mail;
char *name, *value;
regex_t regexp;
regfree(®exp);
}
+void recursive_search_in_db(const char *entry_name,
+ const char *search_name, const char *search_regexp_string) {
+ DIR *dir;
+ struct dirent *dir_e;
+ struct stat sb;
+ char raw_line[BUFFER_SIZE];
+ char subname[PATH_MAX + 1];
+
+ if(lstat(entry_name, &sb) != 0) {
+ fprintf(stderr,
+ "mymail: Can not stat \"%s\": %s\n",
+ entry_name,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ dir = opendir(entry_name);
+
+ if(dir) {
+ while((dir_e = readdir(dir))) {
+ if(!ignore_entry(dir_e->d_name)) {
+ snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
+ recursive_search_in_db(subname, search_name, search_regexp_string);
+ }
+ }
+ closedir(dir);
+ } else {
+ const char *s = entry_name, *filename = entry_name;
+ while(*s) { if(*s == '/') { filename = s+1; } s++; }
+
+ if(strcmp(filename, db_filename) == 0) {
+ FILE *db_file = fopen(entry_name, "r");
+
+ if(!db_file) {
+ fprintf(stderr,
+ "mymail: Can not open \"%s\" for reading: %s\n",
+ db_filename,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if(fgets(raw_line, BUFFER_SIZE, db_file)) {
+ if(strncmp(raw_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",
+ entry_name);
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ fprintf(stderr,
+ "mymail: Can not read the header line in '%s'.\n",
+ entry_name);
+ exit(EXIT_FAILURE);
+ }
+
+ search_in_db(search_name, search_regexp_string, db_file);
+
+ fclose(db_file);
+ }
+ }
+}
+
/*********************************************************************/
void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
}
}
-void index_mbox(const char *input_filename,
+void index_mbox(const char *mbox_filename,
int nb_fields_to_parse, struct parsable_field *fields_to_parse,
FILE *db_file) {
char raw_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
int in_header, new_header;
unsigned long int position_in_file;
- file = fopen(input_filename, "r");
+ file = fopen(mbox_filename, "r");
if(!file) {
- fprintf(stderr, "mymail: Can not open '%s'.\n", input_filename);
+ fprintf(stderr, "mymail: Can not open '%s'.\n", mbox_filename);
if(paranoid) { exit(EXIT_FAILURE); }
return;
}
if(in_header) {
fprintf(stderr,
"Got a ^\"From \" in the header in %s:%lu.\n",
- input_filename, position_in_file);
+ mbox_filename, position_in_file);
fprintf(stderr, "%s", raw_line);
if(paranoid) { exit(EXIT_FAILURE); }
}
if(in_header) { in_header = 0; }
}
- /* printf("PARSE %d %s", in_header, raw_line); */
-
if(in_header) {
if(new_header) {
- fprintf(db_file, "mail %lu %s\n", position_in_file, input_filename);
+ fprintf(db_file, "mail %lu %s\n", position_in_file, mbox_filename);
new_header = 0;
}
/* (raw_line[0] >= 'A' && raw_line[0] <= 'Z'))) { */
/* fprintf(stderr, */
/* "Header line syntax error %s:%lu.\n", */
- /* input_filename, position_in_file); */
+ /* mbox_filename, position_in_file); */
/* fprintf(stderr, "%s", raw_line); */
/* } */
fclose(file);
}
-int ignore_entry(const char *name) {
- return
- /* strcmp(name, ".") == 0 || */
- /* strcmp(name, "..") == 0 || */
- (name[0] == '.' && name[1] != '/');
-}
-
-void process_entry(const char *dir_name,
- int nb_fields_to_parse, struct parsable_field *fields_to_parse,
- FILE *db_file) {
+void recursive_index_mbox(FILE *db_file,
+ const char *entry_name,
+ int nb_fields_to_parse, struct parsable_field *fields_to_parse) {
DIR *dir;
struct dirent *dir_e;
struct stat sb;
char subname[PATH_MAX + 1];
- if(lstat(dir_name, &sb) != 0) {
+ if(lstat(entry_name, &sb) != 0) {
fprintf(stderr,
"mymail: Can not stat \"%s\": %s\n",
- dir_name,
+ entry_name,
strerror(errno));
exit(EXIT_FAILURE);
}
- dir = opendir(dir_name);
+ dir = opendir(entry_name);
if(dir) {
- printf("Processing directory '%s'.\n", dir_name);
while((dir_e = readdir(dir))) {
if(!ignore_entry(dir_e->d_name)) {
- snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
- process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
+ snprintf(subname, PATH_MAX, "%s/%s", entry_name, dir_e->d_name);
+ recursive_index_mbox(db_file, subname, nb_fields_to_parse, fields_to_parse);
}
}
closedir(dir);
} else {
- index_mbox(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
+ index_mbox(entry_name, nb_fields_to_parse, fields_to_parse, db_file);
}
}
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "db-file", 1, 0, 'd' },
+ { "db-root", 1, 0, 'p' },
{ "search-pattern", 1, 0, 's' },
{ "index", 0, 0, 'i' },
+ { "multi-db-files", 0, 0, 'm' },
{ 0, 0, 0, 0 }
};
"^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
},
+
+ {
+ "subj",
+ "^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]: *",
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+ },
+
};
int main(int argc, char **argv) {
paranoid = 0;
action_index = 0;
search_pattern = 0;
+ db_filename = 0;
+ db_root_path = 0;
+ multi_db_files = 0;
setlocale(LC_ALL, "");
- while ((c = getopt_long(argc, argv, "hvip:s:",
+ while ((c = getopt_long(argc, argv, "hvimp:s:d:p:",
long_options, NULL)) != -1) {
switch(c) {
action_index = 1;
break;
- case 'p':
+ case 'm':
+ multi_db_files = 1;
+ break;
+
+ case 'd':
db_filename = strdup(optarg);
- /* printf("db_filename=\"%s\"\n", db_filename); */
+ break;
+
+ case 'p':
+ db_root_path = strdup(optarg);
break;
case 's':
if(!db_filename) {
char *default_db_filename = getenv("MYMAIL_DB_FILE");
- if(!default_db_filename) { default_db_filename = "/tmp/mymail.db"; }
+
+ if(!default_db_filename) {
+ if(multi_db_files) {
+ default_db_filename = "mymail.db";
+ } else {
+ default_db_filename = "/tmp/mymail.db";
+ }
+ }
+
db_filename = strdup(default_db_filename);
}
+ if(!db_root_path) {
+ char *default_db_root_path = getenv("MYMAIL_DB_ROOT");
+
+ if(!default_db_root_path) {
+ if(multi_db_files) {
+ default_db_root_path = "mymail.db";
+ } else {
+ default_db_root_path = "/tmp/mymail.db";
+ }
+ }
+
+ db_root_path = strdup(default_db_root_path);
+ }
+
if(error) {
print_usage(stderr);
exit(EXIT_FAILURE);
}
if(action_index) {
- FILE *db_file = fopen(db_filename, "w");
+ FILE *db_file;
+
+ db_file = fopen(db_filename, "w");
+
if(!db_file) {
fprintf(stderr,
"mymail: Can not open \"%s\" for writing: %s\n",
}
}
+ fprintf(db_file, "%s version_%s raw version\n", MYMAIL_DB_MAGIC_TOKEN, VERSION);
+
while(optind < argc) {
- process_entry(argv[optind],
- nb_fields_to_parse, fields_to_parse, db_file);
+ recursive_index_mbox(db_file,
+ argv[optind],
+ nb_fields_to_parse, fields_to_parse);
optind++;
}
}
else {
+
if(search_pattern) {
- FILE *db_file;
char *search_name;
char *search_regexp_string;
search_name = search_pattern;
search_regexp_string = segment_next_field(search_pattern);
+
if(!*search_regexp_string) {
fprintf(stderr,
"Syntax error in the search pattern.\n");
exit(EXIT_FAILURE);
}
- db_file = fopen(db_filename, "r");
-
- if(!db_file) {
- fprintf(stderr,
- "mymail: Can not open \"%s\" for reading: %s\n",
- db_filename,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
-
- search_in_db(search_name, search_regexp_string, db_file);
+ recursive_search_in_db(db_root_path, search_name, search_regexp_string);
- fclose(db_file);
free(search_pattern);
}
}
free(db_filename);
+ free(db_root_path);
exit(EXIT_SUCCESS);
}