Works, with a remaining bug in the indexing of multi-line field values.
[mymail.git] / mymail.c
1
2 /*
3  *  Copyright (c) 2013 Francois Fleuret
4  *  Written by Francois Fleuret <francois@fleuret.org>
5  *
6  *  This file is part of mymail.
7  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 #define _GNU_SOURCE
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <locale.h>
30 #include <getopt.h>
31 #include <limits.h>
32 #include <dirent.h>
33 #include <regex.h>
34
35 #define VERSION "0.1"
36
37 #define BUFFER_SIZE 16384
38
39 struct parsable_field {
40   char *name;
41   char *regexp_string;
42   regex_t regexp;
43 };
44
45 char *db_filename;
46 char *search_pattern;
47
48 int action_index;
49
50 char *segment_next_field(char *current) {
51   while(*current && *current != ' ') current++;
52   *current = '\0'; current++;
53   while(*current && *current == ' ') current++;
54   return current;
55 }
56
57 void remove_eof(char *c) {
58   while(*c && *c != '\n' && *c != '\r') c++;
59   *c = '\0';
60 }
61
62 /********************************************************************/
63
64 /* malloc with error checking.  */
65
66 void *safe_malloc(size_t n) {
67   void *p = malloc(n);
68   if(!p && n != 0) {
69     fprintf(stderr,
70             "mymail: can not allocate memory: %s\n", strerror(errno));
71     exit(EXIT_FAILURE);
72   }
73   return p;
74 }
75
76 /*********************************************************************/
77
78 void usage(FILE *out) {
79   fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
80   fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
81   fprintf(out, "\n");
82   fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
83   fprintf(out, "\n");
84 }
85
86 /*********************************************************************/
87
88 void search_in_db(const char *search_name, const char *search_regexp_string,
89                   FILE *db_file) {
90   char raw_line[BUFFER_SIZE];
91   char current_mail_filename[BUFFER_SIZE];
92   unsigned int current_position_in_mail;
93   char *name, *value;
94   regex_t regexp;
95   int already_written;
96
97   if(regcomp(&regexp,
98              search_regexp_string,
99              REG_ICASE)) {
100     fprintf(stderr,
101             "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
102             search_regexp_string,
103             search_name);
104     exit(EXIT_FAILURE);
105   }
106
107   current_position_in_mail = 0;
108   already_written = 0;
109
110   while(fgets(raw_line, BUFFER_SIZE, db_file)) {
111     name = raw_line;
112     value = segment_next_field(raw_line);
113
114     /* printf("LINE [%s] %s", name, value); */
115
116     if(strcmp("mail", name) == 0) {
117       char *position_in_file_string = value;
118       char *mail_filename = segment_next_field(value);
119       current_position_in_mail = atoi(position_in_file_string);
120       strcpy(current_mail_filename, mail_filename);
121       remove_eof(current_mail_filename);
122       /* printf("READING [%s]\n", current_mail_filename); */
123       already_written = 0;
124     } else if(!already_written) {
125       if(strcmp(search_name, name) == 0 && regexec(&regexp, value, 0, 0, 0) == 0) {
126         FILE *mail_file;
127         /* printf("%s:%u\n", current_mail_filename, current_position_in_mail); */
128         mail_file = fopen(current_mail_filename, "r");
129         if(!mail_file) {
130           fprintf(stderr, "mymail: Can not open `%s'.\n", current_mail_filename);
131           exit(EXIT_FAILURE);
132         }
133         fseek(mail_file, current_position_in_mail, SEEK_SET);
134         if(fgets(raw_line, BUFFER_SIZE, mail_file)) {
135           printf("%s", raw_line);
136           while(fgets(raw_line, BUFFER_SIZE, mail_file) &&
137                 strncmp(raw_line, "From ", 5)) {
138             printf("%s", raw_line);
139           }
140         }
141         fclose(mail_file);
142         already_written = 1;
143       }
144     }
145   }
146 }
147
148
149 /*********************************************************************/
150
151 void read_file(const char *input_filename,
152                int nb_fields_to_parse, struct parsable_field *fields_to_parse,
153                FILE *db_file) {
154   char raw_line[BUFFER_SIZE];
155   FILE *file;
156   int in_header, new_header;
157   unsigned int position_in_file;
158
159   file = fopen(input_filename, "r");
160
161   if(!file) {
162     fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
163     exit(EXIT_FAILURE);
164   }
165
166   in_header = 0;
167   new_header = 0;
168
169   position_in_file = 0;
170
171   while(fgets(raw_line, BUFFER_SIZE, file)) {
172     if(strncmp(raw_line, "From ", 5) == 0) {
173       if(in_header) {
174         fprintf(stderr,
175                 "Got a 'From ' in the header in %s:%u.\n",
176                 input_filename, position_in_file);
177         fprintf(stderr, "%s", raw_line);
178         exit(EXIT_FAILURE);
179       }
180       in_header = 1;
181       new_header = 1;
182     } else if(strncmp(raw_line, "\n", 1) == 0) {
183       if(in_header) { in_header = 0; }
184     }
185
186     /* if(in_header) { */
187     /* printf("LINE.H %s", raw_line); */
188     /* } else { */
189     /* printf("LINE.B %s", raw_line); */
190     /* } */
191
192     if(in_header) {
193       int f;
194       regmatch_t matches;
195       if(new_header) {
196         fprintf(db_file, "mail %u %s\n", position_in_file, input_filename);
197         new_header = 0;
198       }
199       for(f = 0; f < nb_fields_to_parse; f++) {
200         if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
201           fprintf(db_file, "%s %s",
202                   fields_to_parse[f].name,
203                   raw_line + matches.rm_eo);
204         }
205       }
206     }
207
208     position_in_file += strlen(raw_line);
209   }
210
211   fclose(file);
212 }
213
214 int ignore_entry(const char *name) {
215   return
216     strcmp(name, ".") == 0 ||
217     strcmp(name, "..") == 0 ||
218     (name[0] == '.' && name[1] != '/');
219 }
220
221 void process_entry(const char *dir_name,
222                    int nb_fields_to_parse, struct parsable_field *fields_to_parse,
223                    FILE *db_file) {
224   DIR *dir;
225   struct dirent *dir_e;
226   struct stat sb;
227   char subname[PATH_MAX + 1];
228
229   if(lstat(dir_name, &sb) != 0) {
230     fprintf(stderr,
231             "mymail: Can not stat \"%s\": %s\n",
232             dir_name,
233             strerror(errno));
234     exit(EXIT_FAILURE);
235   } else {
236   }
237
238   if(S_ISLNK(sb.st_mode)) {
239     return;
240   }
241
242   dir = opendir(dir_name);
243
244   if(dir) {
245     printf("Processing directory '%s'.\n", dir_name);
246     while((dir_e = readdir(dir))) {
247       if(!ignore_entry(dir_e->d_name)) {
248         snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
249         process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
250       }
251     }
252     closedir(dir);
253   } else {
254     if(S_ISREG(sb.st_mode)) {
255       /* printf("Processing regular file '%s'.\n", dir_name); */
256       read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
257     }
258   }
259 }
260
261 /*********************************************************************/
262
263 /* For long options that have no equivalent short option, use a
264    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
265 enum
266 {
267   OPT_BASH_MODE = CHAR_MAX + 1
268 };
269
270 static struct option long_options[] = {
271   { "help", no_argument, 0, 'h' },
272   { "db-prefix", 1, 0, 'p' },
273   { "search-pattern", 1, 0, 's' },
274   { "index", 0, 0, 'i' },
275   { 0, 0, 0, 0 }
276 };
277
278 static struct parsable_field fields_to_parse[] = {
279   {
280     "from",
281     "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
282     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
283   },
284
285   {
286     "dest",
287     "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
288     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
289   },
290 };
291
292 int main(int argc, char **argv) {
293   int error = 0, show_help = 0;
294   const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
295   char c;
296   int f;
297
298   action_index = 0;
299   search_pattern = 0;
300
301   setlocale(LC_ALL, "");
302
303   while ((c = getopt_long(argc, argv, "hip:s:",
304                           long_options, NULL)) != -1) {
305
306     switch(c) {
307
308     case 'h':
309       show_help = 1;
310       break;
311
312     case 'i':
313       action_index = 1;
314       break;
315
316     case 'p':
317       db_filename = strdup(optarg);
318       break;
319
320     case 's':
321       if(search_pattern) {
322         fprintf(stderr,
323                 "mymail: Search pattern already defined.\n");
324         exit(EXIT_FAILURE);
325       }
326       search_pattern = strdup(optarg);
327       break;
328
329     default:
330       error = 1;
331       break;
332     }
333   }
334
335   if(!db_filename) {
336     db_filename = strdup("/tmp/mymail");
337   }
338
339   if(error) {
340     usage(stderr);
341     exit(EXIT_FAILURE);
342   }
343
344   if(show_help) {
345     usage(stdout);
346     exit(EXIT_SUCCESS);
347   }
348
349   if(action_index) {
350     FILE *db_file = fopen(db_filename, "w");
351     if(!db_file) {
352       fprintf(stderr,
353               "mymail: Can not open \"%s\" for writing: %s\n",
354               db_filename,
355               strerror(errno));
356       exit(EXIT_FAILURE);
357     }
358
359     for(f = 0; f < nb_fields_to_parse; f++) {
360       if(regcomp(&fields_to_parse[f].regexp,
361                  fields_to_parse[f].regexp_string,
362                  REG_ICASE)) {
363         fprintf(stderr,
364                 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
365                 fields_to_parse[f].regexp_string,
366                 fields_to_parse[f].name);
367         exit(EXIT_FAILURE);
368       }
369     }
370
371     while(optind < argc) {
372       process_entry(argv[optind],
373                     nb_fields_to_parse, fields_to_parse, db_file);
374       optind++;
375     }
376
377     fclose(db_file);
378
379     for(f = 0; f < nb_fields_to_parse; f++) {
380       regfree(&fields_to_parse[f].regexp);
381     }
382   }
383
384   else {
385     if(search_pattern) {
386       FILE *db_file;
387       char *search_name;
388       char *search_regexp_string;
389       search_name = search_pattern;
390       search_regexp_string = segment_next_field(search_pattern);
391       if(!*search_regexp_string) {
392         fprintf(stderr,
393                 "Syntax error in the search pattern.\n");
394         exit(EXIT_FAILURE);
395       }
396
397       /* printf("Starting search in %s for field \"%s\" matching \"%s\".\n", */
398              /* db_filename, */
399              /* search_name, */
400              /* search_regexp_string); */
401
402       db_file = fopen(db_filename, "r");
403
404       if(!db_file) {
405         fprintf(stderr,
406                 "mymail: Can not open \"%s\" for reading: %s\n",
407                 db_filename,
408                 strerror(errno));
409         exit(EXIT_FAILURE);
410       }
411
412       search_in_db(search_name, search_regexp_string, db_file);
413
414       fclose(db_file);
415       free(search_pattern);
416     }
417   }
418
419   exit(EXIT_SUCCESS);
420 }