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