3 * Copyright (c) 2013 Francois Fleuret
4 * Written by Francois Fleuret <francois@fleuret.org>
6 * This file is part of mymail.
8 * mymail is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
12 * mymail is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with mymail. If not, see <http://www.gnu.org/licenses/>.
24 This command is a dumb mail indexer. It can either (1) scan
25 directories containing mbox files, and create a db file containing
26 for each mail a list of fields computed from the header, or (2)
27 read such a db file and get all the mails matching regexp-defined
28 conditions on the fields.
30 It is low-tech, simple, light and fast.
49 #define BUFFER_SIZE 65536
51 struct parsable_field {
63 char *segment_next_field(char *current) {
64 while(*current && *current != ' ') current++;
65 *current = '\0'; current++;
66 while(*current && *current == ' ') current++;
70 void remove_eof(char *c) {
71 while(*c && *c != '\n' && *c != '\r') c++;
75 /********************************************************************/
77 /* malloc with error checking. */
79 void *safe_malloc(size_t n) {
83 "mymail: can not allocate memory: %s\n", strerror(errno));
89 /*********************************************************************/
91 void print_version(FILE *out) {
92 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
95 void print_usage(FILE *out) {
97 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
99 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
101 fprintf(out, " -h, --help\n");
102 fprintf(out, " show this help\n");
103 fprintf(out, " -v, --version\n");
104 fprintf(out, " print the version number\n");
105 fprintf(out, " -i, --index\n");
106 fprintf(out, " index mails\n");
107 fprintf(out, " -d <db filename>, --db-file <db filename>\n");
108 fprintf(out, " set the data-base file\n");
109 fprintf(out, " -s <search pattern>, --search <search pattern>\n");
110 fprintf(out, " search for matching mails in the data-base file\n");
113 /*********************************************************************/
115 void search_in_db(const char *search_name, const char *search_regexp_string,
117 char raw_line[BUFFER_SIZE];
118 char current_mail_filename[BUFFER_SIZE];
119 unsigned long int current_position_in_mail;
125 search_regexp_string,
128 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
129 search_regexp_string,
134 current_position_in_mail = 0;
137 while(fgets(raw_line, BUFFER_SIZE, db_file)) {
139 value = segment_next_field(raw_line);
141 if(strcmp("mail", name) == 0) {
142 char *position_in_file_string = value;
143 char *mail_filename = segment_next_field(value);
144 current_position_in_mail = atol(position_in_file_string);
145 strcpy(current_mail_filename, mail_filename);
146 remove_eof(current_mail_filename);
150 else if(!already_written) {
151 if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) {
153 mail_file = fopen(current_mail_filename, "r");
155 fprintf(stderr, "mymail: Can not open '%s'.\n", current_mail_filename);
158 fseek(mail_file, current_position_in_mail, SEEK_SET);
159 if(fgets(raw_line, BUFFER_SIZE, mail_file)) {
160 printf("%s", raw_line);
161 while(fgets(raw_line, BUFFER_SIZE, mail_file) &&
162 strncmp(raw_line, "From ", 5)) {
163 printf("%s", raw_line);
175 /*********************************************************************/
177 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
178 char *raw_line, FILE *db_file) {
181 for(f = 0; f < nb_fields_to_parse; f++) {
182 if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
183 fprintf(db_file, "%s %s\n",
184 fields_to_parse[f].name,
185 raw_line + matches.rm_eo);
190 void index_mbox(const char *input_filename,
191 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
193 char raw_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
194 char *end_of_full_line;
196 int in_header, new_header;
197 unsigned long int position_in_file;
199 file = fopen(input_filename, "r");
202 fprintf(stderr, "mymail: Can not open '%s'.\n", input_filename);
203 if(paranoid) { exit(EXIT_FAILURE); }
210 position_in_file = 0;
211 end_of_full_line = 0;
214 while(fgets(raw_line, BUFFER_SIZE, file)) {
215 if(strncmp(raw_line, "From ", 5) == 0) {
218 "Got a ^\"From \" in the header in %s:%lu.\n",
219 input_filename, position_in_file);
220 fprintf(stderr, "%s", raw_line);
221 if(paranoid) { exit(EXIT_FAILURE); }
225 } else if(strncmp(raw_line, "\n", 1) == 0) {
226 if(in_header) { in_header = 0; }
229 /* printf("PARSE %d %s", in_header, raw_line); */
233 fprintf(db_file, "mail %lu %s\n", position_in_file, input_filename);
237 if(raw_line[0] == ' ' || raw_line[0] == '\t') {
238 char *start = raw_line;
239 while(*start == ' ' || *start == '\t') start++;
240 *(end_of_full_line++) = ' ';
241 strcpy(end_of_full_line, start);
242 while(*end_of_full_line && *end_of_full_line != '\n') {
245 *end_of_full_line = '\0';
249 /* if(!((raw_line[0] >= 'a' && raw_line[0] <= 'z') || */
250 /* (raw_line[0] >= 'A' && raw_line[0] <= 'Z'))) { */
251 /* fprintf(stderr, */
252 /* "Header line syntax error %s:%lu.\n", */
253 /* input_filename, position_in_file); */
254 /* fprintf(stderr, "%s", raw_line); */
258 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
261 end_of_full_line = full_line;
262 strcpy(end_of_full_line, raw_line);
263 while(*end_of_full_line && *end_of_full_line != '\n') {
266 *end_of_full_line = '\0';
271 position_in_file += strlen(raw_line);
277 int ignore_entry(const char *name) {
279 /* strcmp(name, ".") == 0 || */
280 /* strcmp(name, "..") == 0 || */
281 (name[0] == '.' && name[1] != '/');
284 void process_entry(const char *dir_name,
285 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
288 struct dirent *dir_e;
290 char subname[PATH_MAX + 1];
292 if(lstat(dir_name, &sb) != 0) {
294 "mymail: Can not stat \"%s\": %s\n",
300 dir = opendir(dir_name);
303 printf("Processing directory '%s'.\n", dir_name);
304 while((dir_e = readdir(dir))) {
305 if(!ignore_entry(dir_e->d_name)) {
306 snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
307 process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
312 index_mbox(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
316 /*********************************************************************/
318 /* For long options that have no equivalent short option, use a
319 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
321 OPT_BASH_MODE = CHAR_MAX + 1
324 static struct option long_options[] = {
325 { "help", no_argument, 0, 'h' },
326 { "version", no_argument, 0, 'v' },
327 { "db-file", 1, 0, 'd' },
328 { "search-pattern", 1, 0, 's' },
329 { "index", 0, 0, 'i' },
333 static struct parsable_field fields_to_parse[] = {
336 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
337 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
342 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
343 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
347 int main(int argc, char **argv) {
348 int error = 0, show_help = 0;
349 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
357 setlocale(LC_ALL, "");
359 while ((c = getopt_long(argc, argv, "hvip:s:",
360 long_options, NULL)) != -1) {
369 print_version(stdout);
377 db_filename = strdup(optarg);
378 /* printf("db_filename=\"%s\"\n", db_filename); */
383 fprintf(stderr, "mymail: Search pattern already defined.\n");
386 search_pattern = strdup(optarg);
396 char *default_db_filename = getenv("MYMAIL_DB_FILE");
397 if(!default_db_filename) { default_db_filename = "/tmp/mymail.db"; }
398 db_filename = strdup(default_db_filename);
412 FILE *db_file = fopen(db_filename, "w");
415 "mymail: Can not open \"%s\" for writing: %s\n",
421 for(f = 0; f < nb_fields_to_parse; f++) {
422 if(regcomp(&fields_to_parse[f].regexp,
423 fields_to_parse[f].regexp_string,
426 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
427 fields_to_parse[f].regexp_string,
428 fields_to_parse[f].name);
433 while(optind < argc) {
434 process_entry(argv[optind],
435 nb_fields_to_parse, fields_to_parse, db_file);
441 for(f = 0; f < nb_fields_to_parse; f++) {
442 regfree(&fields_to_parse[f].regexp);
450 char *search_regexp_string;
451 search_name = search_pattern;
452 search_regexp_string = segment_next_field(search_pattern);
453 if(!*search_regexp_string) {
455 "Syntax error in the search pattern.\n");
459 db_file = fopen(db_filename, "r");
463 "mymail: Can not open \"%s\" for reading: %s\n",
469 search_in_db(search_name, search_regexp_string, db_file);
472 free(search_pattern);