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 void read_file(const char *input_filename,
75 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
77 char raw_line[BUFFER_SIZE];
79 int in_header, new_header;
80 unsigned int position_in_file;
82 file = fopen(input_filename, "r");
85 fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
94 while(fgets(raw_line, BUFFER_SIZE, file)) {
95 if(strncmp(raw_line, "From ", 5) == 0) {
98 "Got a 'From ' in the header in %s:%u.\n",
99 input_filename, position_in_file);
100 fprintf(stderr, "%s", raw_line);
105 } else if(strncmp(raw_line, "\n", 1) == 0) {
106 if(in_header) { in_header = 0; }
109 /* if(in_header) { */
110 /* printf("LINE.H %s", raw_line); */
112 /* printf("LINE.B %s", raw_line); */
119 fprintf(db_file, "mail %s:%d\n", input_filename, position_in_file);
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);
131 position_in_file += strlen(raw_line);
137 int ignore_entry(const char *name) {
139 strcmp(name, ".") == 0 ||
140 strcmp(name, "..") == 0 ||
141 (name[0] == '.' && name[1] != '/');
144 void process_entry(const char *dir_name,
145 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
148 struct dirent *dir_e;
150 char subname[PATH_MAX + 1];
152 if(lstat(dir_name, &sb) != 0) {
154 "mymail: Can not stat \"%s\": %s\n",
161 if(S_ISLNK(sb.st_mode)) {
165 dir = opendir(dir_name);
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);
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);
184 /*********************************************************************/
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. */
190 OPT_BASH_MODE = CHAR_MAX + 1
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' },
201 static struct parsable_field fields_to_parse[] = {
204 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
205 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
210 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
211 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
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);
224 setlocale(LC_ALL, "");
226 while ((c = getopt_long(argc, argv, "hip:s:",
227 long_options, NULL)) != -1) {
240 db_filename = strdup(optarg);
246 "mymail: Search pattern already defined.\n");
249 search_pattern = strdup(optarg);
259 db_filename = strdup("/tmp/mymail");
273 FILE *db_file = fopen(db_filename, "w");
276 "mymail: Can not open \"%s\" for writing: %s\n",
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,
287 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
288 fields_to_parse[f].regexp_string,
289 fields_to_parse[f].name);
294 while(optind < argc) {
295 process_entry(argv[optind],
296 nb_fields_to_parse, fields_to_parse, db_file);
302 for(f = 0; f < nb_fields_to_parse; f++) {
303 regfree(&fields_to_parse[f].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) {
318 "Syntax error in the search pattern.\n");
321 printf("Starting search in %s for field \"%s\" matching \"%s\".\n",
325 free(search_pattern);