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/>.
37 #define BUFFER_SIZE 16384
39 struct parsable_field {
50 /********************************************************************/
52 /* malloc with error checking. */
54 void *safe_malloc(size_t n) {
58 "mymail: can not allocate memory: %s\n", strerror(errno));
64 /*********************************************************************/
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");
70 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
74 /*********************************************************************/
76 void search_in_db(const char *search_name, const char *search_regexp_string,
78 char raw_line[BUFFER_SIZE];
79 char current_mail[BUFFER_SIZE];
87 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
93 while(fgets(raw_line, BUFFER_SIZE, db_file)) {
96 while(*value && *value != ' ') value++;
97 *value = '\0'; value++;
98 while(*value && *value == ' ') value++;
100 /* printf("LINE [%s] %s", name, value); */
102 if(strcmp("mail", name) == 0) {
103 strcpy(current_mail, value);
104 /* printf("READING [%s]\n", current_mail); */
106 if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) {
107 printf("%s", current_mail);
114 /*********************************************************************/
116 void read_file(const char *input_filename,
117 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
119 char raw_line[BUFFER_SIZE];
121 int in_header, new_header;
122 unsigned int position_in_file;
124 file = fopen(input_filename, "r");
127 fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
134 position_in_file = 0;
136 while(fgets(raw_line, BUFFER_SIZE, file)) {
137 if(strncmp(raw_line, "From ", 5) == 0) {
140 "Got a 'From ' in the header in %s:%u.\n",
141 input_filename, position_in_file);
142 fprintf(stderr, "%s", raw_line);
147 } else if(strncmp(raw_line, "\n", 1) == 0) {
148 if(in_header) { in_header = 0; }
151 /* if(in_header) { */
152 /* printf("LINE.H %s", raw_line); */
154 /* printf("LINE.B %s", raw_line); */
161 fprintf(db_file, "mail %s:%d\n", input_filename, position_in_file);
164 for(f = 0; f < nb_fields_to_parse; f++) {
165 if(regexec(&fields_to_parse[f].regexp, raw_line, 1, &matches, 0) == 0) {
166 fprintf(db_file, "%s %s",
167 fields_to_parse[f].name,
168 raw_line + matches.rm_eo);
173 position_in_file += strlen(raw_line);
179 int ignore_entry(const char *name) {
181 strcmp(name, ".") == 0 ||
182 strcmp(name, "..") == 0 ||
183 (name[0] == '.' && name[1] != '/');
186 void process_entry(const char *dir_name,
187 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
190 struct dirent *dir_e;
192 char subname[PATH_MAX + 1];
194 if(lstat(dir_name, &sb) != 0) {
196 "mymail: Can not stat \"%s\": %s\n",
203 if(S_ISLNK(sb.st_mode)) {
207 dir = opendir(dir_name);
210 printf("Processing directory '%s'.\n", dir_name);
211 while((dir_e = readdir(dir))) {
212 if(!ignore_entry(dir_e->d_name)) {
213 snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
214 process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
219 if(S_ISREG(sb.st_mode)) {
220 /* printf("Processing regular file '%s'.\n", dir_name); */
221 read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
226 /*********************************************************************/
228 /* For long options that have no equivalent short option, use a
229 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
232 OPT_BASH_MODE = CHAR_MAX + 1
235 static struct option long_options[] = {
236 { "help", no_argument, 0, 'h' },
237 { "db-prefix", 1, 0, 'p' },
238 { "search-pattern", 1, 0, 's' },
239 { "index", 0, 0, 'i' },
243 static struct parsable_field fields_to_parse[] = {
246 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
247 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
252 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
253 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
257 int main(int argc, char **argv) {
258 int error = 0, show_help = 0;
259 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
266 setlocale(LC_ALL, "");
268 while ((c = getopt_long(argc, argv, "hip:s:",
269 long_options, NULL)) != -1) {
282 db_filename = strdup(optarg);
288 "mymail: Search pattern already defined.\n");
291 search_pattern = strdup(optarg);
301 db_filename = strdup("/tmp/mymail");
315 FILE *db_file = fopen(db_filename, "w");
318 "mymail: Can not open \"%s\" for writing: %s\n",
324 for(f = 0; f < nb_fields_to_parse; f++) {
325 if(regcomp(&fields_to_parse[f].regexp,
326 fields_to_parse[f].regexp_string,
329 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
330 fields_to_parse[f].regexp_string,
331 fields_to_parse[f].name);
336 while(optind < argc) {
337 process_entry(argv[optind],
338 nb_fields_to_parse, fields_to_parse, db_file);
344 for(f = 0; f < nb_fields_to_parse; f++) {
345 regfree(&fields_to_parse[f].regexp);
353 char *search_regexp_string;
354 search_name = search_pattern;
355 search_regexp_string = search_pattern;
356 while(*search_regexp_string && *search_regexp_string != ' ') search_regexp_string++;
357 *search_regexp_string = '\0'; search_regexp_string++;
358 while(*search_regexp_string && *search_regexp_string == ' ') search_regexp_string++;
359 if(!*search_regexp_string) {
361 "Syntax error in the search pattern.\n");
365 printf("Starting search in %s for field \"%s\" matching \"%s\".\n",
368 search_regexp_string);
370 db_file = fopen(db_filename, "r");
374 "mymail: Can not open \"%s\" for reading: %s\n",
379 search_in_db(search_name, search_regexp_string, db_file);
382 free(search_pattern);