Starting to write the search.
[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 /********************************************************************/
51
52 /* malloc with error checking.  */
53
54 void *safe_malloc(size_t n) {
55   void *p = malloc(n);
56   if(!p && n != 0) {
57     fprintf(stderr,
58             "mymail: can not allocate memory: %s\n", strerror(errno));
59     exit(EXIT_FAILURE);
60   }
61   return p;
62 }
63
64 /*********************************************************************/
65
66 void usage(FILE *out) {
67   fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
68   fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
69   fprintf(out, "\n");
70   fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
71   fprintf(out, "\n");
72 }
73
74 void read_file(const char *input_filename,
75                int nb_fields_to_parse, struct parsable_field *fields_to_parse,
76                FILE *db_file) {
77   char raw_line[BUFFER_SIZE];
78   FILE *file;
79   int in_header, new_header;
80   unsigned int position_in_file;
81
82   file = fopen(input_filename, "r");
83
84   if(!file) {
85     fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
86     exit(EXIT_FAILURE);
87   }
88
89   in_header = 0;
90   new_header = 0;
91
92   position_in_file = 0;
93
94   while(fgets(raw_line, BUFFER_SIZE, file)) {
95     if(strncmp(raw_line, "From ", 5) == 0) {
96       if(in_header) {
97         fprintf(stderr,
98                 "Got a 'From ' in the header in %s:%u.\n",
99                 input_filename, position_in_file);
100         fprintf(stderr, "%s", raw_line);
101         exit(EXIT_FAILURE);
102       }
103       in_header = 1;
104       new_header = 1;
105     } else if(strncmp(raw_line, "\n", 1) == 0) {
106       if(in_header) { in_header = 0; }
107     }
108
109     /* if(in_header) { */
110     /* printf("LINE.H %s", raw_line); */
111     /* } else { */
112     /* printf("LINE.B %s", raw_line); */
113     /* } */
114
115     if(in_header) {
116       int f;
117       regmatch_t matches;
118       if(new_header) {
119         fprintf(db_file, "mail %s:%d\n", input_filename, position_in_file);
120         new_header = 0;
121       }
122       for(f = 0; f < nb_fields_to_parse; f++) {
123         if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
124           fprintf(db_file, "%s %s",
125                   fields_to_parse[f].name,
126                   raw_line + matches.rm_eo);
127         }
128       }
129     }
130
131     position_in_file += strlen(raw_line);
132   }
133
134   fclose(file);
135 }
136
137 int ignore_entry(const char *name) {
138   return
139     strcmp(name, ".") == 0 ||
140     strcmp(name, "..") == 0 ||
141     (name[0] == '.' && name[1] != '/');
142 }
143
144 void process_entry(const char *dir_name,
145                    int nb_fields_to_parse, struct parsable_field *fields_to_parse,
146                    FILE *db_file) {
147   DIR *dir;
148   struct dirent *dir_e;
149   struct stat sb;
150   char subname[PATH_MAX + 1];
151
152   if(lstat(dir_name, &sb) != 0) {
153     fprintf(stderr,
154             "mymail: Can not stat \"%s\": %s\n",
155             dir_name,
156             strerror(errno));
157     exit(EXIT_FAILURE);
158   } else {
159   }
160
161   if(S_ISLNK(sb.st_mode)) {
162     return;
163   }
164
165   dir = opendir(dir_name);
166
167   if(dir) {
168     printf("Processing directory '%s'.\n", dir_name);
169     while((dir_e = readdir(dir))) {
170       if(!ignore_entry(dir_e->d_name)) {
171         snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
172         process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
173       }
174     }
175     closedir(dir);
176   } else {
177     if(S_ISREG(sb.st_mode)) {
178       /* printf("Processing regular file '%s'.\n", dir_name); */
179       read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
180     }
181   }
182 }
183
184 /*********************************************************************/
185
186 /* For long options that have no equivalent short option, use a
187    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
188 enum
189 {
190   OPT_BASH_MODE = CHAR_MAX + 1
191 };
192
193 static struct option long_options[] = {
194   { "help", no_argument, 0, 'h' },
195   { "db-prefix", 1, 0, 'p' },
196   { "search-pattern", 1, 0, 's' },
197   { "index", 0, 0, 'i' },
198   { 0, 0, 0, 0 }
199 };
200
201 static struct parsable_field fields_to_parse[] = {
202   {
203     "from",
204     "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
205     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
206   },
207
208   {
209     "dest",
210     "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
211     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
212   },
213 };
214
215 int main(int argc, char **argv) {
216   int error = 0, show_help = 0;
217   const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
218   char c;
219   int f;
220
221   action_index = 0;
222   search_pattern = 0;
223
224   setlocale(LC_ALL, "");
225
226   while ((c = getopt_long(argc, argv, "hip:s:",
227                           long_options, NULL)) != -1) {
228
229     switch(c) {
230
231     case 'h':
232       show_help = 1;
233       break;
234
235     case 'i':
236       action_index = 1;
237       break;
238
239     case 'p':
240       db_filename = strdup(optarg);
241       break;
242
243     case 's':
244       if(search_pattern) {
245         fprintf(stderr,
246                 "mymail: Search pattern already defined.\n");
247         exit(EXIT_FAILURE);
248       }
249       search_pattern = strdup(optarg);
250       break;
251
252     default:
253       error = 1;
254       break;
255     }
256   }
257
258   if(!db_filename) {
259     db_filename = strdup("/tmp/mymail");
260   }
261
262   if(error) {
263     usage(stderr);
264     exit(EXIT_FAILURE);
265   }
266
267   if(show_help) {
268     usage(stdout);
269     exit(EXIT_SUCCESS);
270   }
271
272   if(action_index) {
273     FILE *db_file = fopen(db_filename, "w");
274     if(!db_file) {
275       fprintf(stderr,
276               "mymail: Can not open \"%s\" for writing: %s\n",
277               db_filename,
278               strerror(errno));
279       exit(EXIT_FAILURE);
280     }
281
282     for(f = 0; f < nb_fields_to_parse; f++) {
283       if(regcomp(&fields_to_parse[f].regexp,
284                  fields_to_parse[f].regexp_string,
285                  REG_ICASE)) {
286         fprintf(stderr,
287                 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
288                 fields_to_parse[f].regexp_string,
289                 fields_to_parse[f].name);
290         exit(EXIT_FAILURE);
291       }
292     }
293
294     while(optind < argc) {
295       process_entry(argv[optind],
296                     nb_fields_to_parse, fields_to_parse, db_file);
297       optind++;
298     }
299
300     fclose(db_file);
301
302     for(f = 0; f < nb_fields_to_parse; f++) {
303       regfree(&fields_to_parse[f].regexp);
304     }
305   }
306
307   else {
308     if(search_pattern) {
309       char *search_name;
310       char *search_regexp;
311       search_name = search_pattern;
312       search_regexp = search_pattern;
313       while(*search_regexp && *search_regexp != ' ') search_regexp++;
314       *search_regexp = '\0'; search_regexp++;
315       while(*search_regexp && *search_regexp == ' ') search_regexp++;
316       if(!*search_regexp) {
317         fprintf(stderr,
318                 "Syntax error in the search pattern.\n");
319         exit(EXIT_FAILURE);
320       }
321       printf("Starting search in %s for field \"%s\" matching \"%s\".\n",
322              db_filename,
323              search_name,
324              search_regexp);
325       free(search_pattern);
326     }
327   }
328
329   exit(EXIT_SUCCESS);
330 }