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 usage(FILE *out) {
92 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
93 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
95 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
99 /*********************************************************************/
101 void search_in_db(const char *search_name, const char *search_regexp_string,
103 char raw_line[BUFFER_SIZE];
104 char current_mail_filename[BUFFER_SIZE];
105 unsigned long int current_position_in_mail;
111 search_regexp_string,
114 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
115 search_regexp_string,
120 current_position_in_mail = 0;
123 while(fgets(raw_line, BUFFER_SIZE, db_file)) {
125 value = segment_next_field(raw_line);
127 if(strcmp("mail", name) == 0) {
128 char *position_in_file_string = value;
129 char *mail_filename = segment_next_field(value);
130 current_position_in_mail = atol(position_in_file_string);
131 strcpy(current_mail_filename, mail_filename);
132 remove_eof(current_mail_filename);
136 else if(!already_written) {
137 if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) {
139 mail_file = fopen(current_mail_filename, "r");
141 fprintf(stderr, "mymail: Can not open '%s'.\n", current_mail_filename);
144 fseek(mail_file, current_position_in_mail, SEEK_SET);
145 if(fgets(raw_line, BUFFER_SIZE, mail_file)) {
146 printf("%s", raw_line);
147 while(fgets(raw_line, BUFFER_SIZE, mail_file) &&
148 strncmp(raw_line, "From ", 5)) {
149 printf("%s", raw_line);
161 /*********************************************************************/
163 void index_one_mbox_line(int nb_fields_to_parse, struct parsable_field *fields_to_parse,
164 char *raw_line, FILE *db_file) {
167 for(f = 0; f < nb_fields_to_parse; f++) {
168 if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
169 fprintf(db_file, "%s %s\n",
170 fields_to_parse[f].name,
171 raw_line + matches.rm_eo);
176 void index_mbox(const char *input_filename,
177 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
179 char raw_line[BUFFER_SIZE], full_line[BUFFER_SIZE];
180 char *end_of_full_line;
182 int in_header, new_header;
183 unsigned long int position_in_file;
185 file = fopen(input_filename, "r");
188 fprintf(stderr, "mymail: Can not open '%s'.\n", input_filename);
189 if(paranoid) { exit(EXIT_FAILURE); }
196 position_in_file = 0;
197 end_of_full_line = 0;
199 while(fgets(raw_line, BUFFER_SIZE, file)) {
200 if(strncmp(raw_line, "From ", 5) == 0) {
203 "Got a ^\"From \" in the header in %s:%lu.\n",
204 input_filename, position_in_file);
205 fprintf(stderr, "%s", raw_line);
206 if(paranoid) { exit(EXIT_FAILURE); }
210 } else if(strncmp(raw_line, "\n", 1) == 0) {
211 if(in_header) { in_header = 0; }
214 /* printf("PARSE %d %s", in_header, raw_line); */
218 fprintf(db_file, "mail %lu %s\n", position_in_file, input_filename);
222 if(raw_line[0] == ' ' || raw_line[0] == '\t') {
223 char *start = raw_line;
224 while(*start == ' ' || *start == '\t') start++;
225 *(end_of_full_line++) = ' ';
226 strcpy(end_of_full_line, start);
227 while(*end_of_full_line && *end_of_full_line != '\n') {
230 *end_of_full_line = '\0';
234 /* if(!((raw_line[0] >= 'a' && raw_line[0] <= 'z') || */
235 /* (raw_line[0] >= 'A' && raw_line[0] <= 'Z'))) { */
236 /* fprintf(stderr, */
237 /* "Header line syntax error %s:%lu.\n", */
238 /* input_filename, position_in_file); */
239 /* fprintf(stderr, "%s", raw_line); */
243 index_one_mbox_line(nb_fields_to_parse, fields_to_parse, full_line, db_file);
246 end_of_full_line = full_line;
247 strcpy(end_of_full_line, raw_line);
248 while(*end_of_full_line && *end_of_full_line != '\n') {
251 *end_of_full_line = '\0';
256 position_in_file += strlen(raw_line);
262 int ignore_entry(const char *name) {
264 /* strcmp(name, ".") == 0 || */
265 /* strcmp(name, "..") == 0 || */
266 (name[0] == '.' && name[1] != '/');
269 void process_entry(const char *dir_name,
270 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
273 struct dirent *dir_e;
275 char subname[PATH_MAX + 1];
277 if(lstat(dir_name, &sb) != 0) {
279 "mymail: Can not stat \"%s\": %s\n",
285 dir = opendir(dir_name);
288 printf("Processing directory '%s'.\n", dir_name);
289 while((dir_e = readdir(dir))) {
290 if(!ignore_entry(dir_e->d_name)) {
291 snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
292 process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
297 index_mbox(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
301 /*********************************************************************/
303 /* For long options that have no equivalent short option, use a
304 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
306 OPT_BASH_MODE = CHAR_MAX + 1
309 static struct option long_options[] = {
310 { "help", no_argument, 0, 'h' },
311 { "db-file", 1, 0, 'p' },
312 { "search-pattern", 1, 0, 's' },
313 { "index", 0, 0, 'i' },
317 static struct parsable_field fields_to_parse[] = {
320 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
321 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
326 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
327 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
331 int main(int argc, char **argv) {
332 int error = 0, show_help = 0;
333 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
341 setlocale(LC_ALL, "");
343 while ((c = getopt_long(argc, argv, "hip:s:",
344 long_options, NULL)) != -1) {
357 db_filename = strdup(optarg);
358 printf("db_filename=\"%s\"\n", db_filename);
363 fprintf(stderr, "mymail: Search pattern already defined.\n");
366 search_pattern = strdup(optarg);
376 char *default_db_filename = getenv("MYMAIL_DB_FILE");
377 if(!default_db_filename) { default_db_filename = "/tmp/mymail.db"; }
378 db_filename = strdup(default_db_filename);
392 FILE *db_file = fopen(db_filename, "w");
395 "mymail: Can not open \"%s\" for writing: %s\n",
401 for(f = 0; f < nb_fields_to_parse; f++) {
402 if(regcomp(&fields_to_parse[f].regexp,
403 fields_to_parse[f].regexp_string,
406 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
407 fields_to_parse[f].regexp_string,
408 fields_to_parse[f].name);
413 while(optind < argc) {
414 process_entry(argv[optind],
415 nb_fields_to_parse, fields_to_parse, db_file);
421 for(f = 0; f < nb_fields_to_parse; f++) {
422 regfree(&fields_to_parse[f].regexp);
430 char *search_regexp_string;
431 search_name = search_pattern;
432 search_regexp_string = segment_next_field(search_pattern);
433 if(!*search_regexp_string) {
435 "Syntax error in the search pattern.\n");
439 /* printf("Starting search in %s for field \"%s\" matching \"%s\".\n", */
442 /* search_regexp_string); */
444 db_file = fopen(db_filename, "r");
448 "mymail: Can not open \"%s\" for reading: %s\n",
454 search_in_db(search_name, search_regexp_string, db_file);
457 free(search_pattern);