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 char *segment_next_field(char *current) {
51 while(*current && *current != ' ') current++;
52 *current = '\0'; current++;
53 while(*current && *current == ' ') current++;
57 void remove_eof(char *c) {
58 while(*c && *c != '\n' && *c != '\r') c++;
62 /********************************************************************/
64 /* malloc with error checking. */
66 void *safe_malloc(size_t n) {
70 "mymail: can not allocate memory: %s\n", strerror(errno));
76 /*********************************************************************/
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");
82 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
86 /*********************************************************************/
88 void search_in_db(const char *search_name, const char *search_regexp_string,
90 char raw_line[BUFFER_SIZE];
91 char current_mail_filename[BUFFER_SIZE];
92 unsigned long int current_position_in_mail;
101 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
102 search_regexp_string,
107 current_position_in_mail = 0;
110 while(fgets(raw_line, BUFFER_SIZE, db_file)) {
112 value = segment_next_field(raw_line);
114 if(strcmp("mail", name) == 0) {
115 char *position_in_file_string = value;
116 char *mail_filename = segment_next_field(value);
117 current_position_in_mail = atol(position_in_file_string);
118 strcpy(current_mail_filename, mail_filename);
119 remove_eof(current_mail_filename);
123 else if(!already_written) {
124 if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) {
126 mail_file = fopen(current_mail_filename, "r");
128 fprintf(stderr, "mymail: Can not open `%s'.\n", current_mail_filename);
131 fseek(mail_file, current_position_in_mail, SEEK_SET);
132 if(fgets(raw_line, BUFFER_SIZE, mail_file)) {
133 printf("%s", raw_line);
134 while(fgets(raw_line, BUFFER_SIZE, mail_file) &&
135 strncmp(raw_line, "From ", 5)) {
136 printf("%s", raw_line);
149 /*********************************************************************/
151 void read_file(const char *input_filename,
152 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
154 char raw_line[BUFFER_SIZE];
156 int in_header, new_header;
157 unsigned long int position_in_file;
159 file = fopen(input_filename, "r");
162 fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
169 position_in_file = 0;
171 while(fgets(raw_line, BUFFER_SIZE, file)) {
172 if(strncmp(raw_line, "From ", 5) == 0) {
175 "Got a 'From ' in the header in %s:%lu.\n",
176 input_filename, position_in_file);
177 fprintf(stderr, "%s", raw_line);
182 } else if(strncmp(raw_line, "\n", 1) == 0) {
183 if(in_header) { in_header = 0; }
186 /* if(in_header) { */
187 /* printf("LINE.H %s", raw_line); */
189 /* printf("LINE.B %s", raw_line); */
196 fprintf(db_file, "mail %lu %s\n", position_in_file, input_filename);
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);
208 position_in_file += strlen(raw_line);
214 int ignore_entry(const char *name) {
216 /* strcmp(name, ".") == 0 || */
217 /* strcmp(name, "..") == 0 || */
218 (name[0] == '.' && name[1] != '/');
221 void process_entry(const char *dir_name,
222 int nb_fields_to_parse, struct parsable_field *fields_to_parse,
225 struct dirent *dir_e;
227 char subname[PATH_MAX + 1];
229 if(lstat(dir_name, &sb) != 0) {
231 "mymail: Can not stat \"%s\": %s\n",
237 /* if(S_ISLNK(sb.st_mode)) { */
241 dir = opendir(dir_name);
244 printf("Processing directory '%s'.\n", dir_name);
245 while((dir_e = readdir(dir))) {
246 if(!ignore_entry(dir_e->d_name)) {
247 snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
248 process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
253 /* if(S_ISREG(sb.st_mode)) { */
254 read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
259 /*********************************************************************/
261 /* For long options that have no equivalent short option, use a
262 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
265 OPT_BASH_MODE = CHAR_MAX + 1
268 static struct option long_options[] = {
269 { "help", no_argument, 0, 'h' },
270 { "db-prefix", 1, 0, 'p' },
271 { "search-pattern", 1, 0, 's' },
272 { "index", 0, 0, 'i' },
276 static struct parsable_field fields_to_parse[] = {
279 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
280 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
285 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
286 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
290 int main(int argc, char **argv) {
291 int error = 0, show_help = 0;
292 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
299 setlocale(LC_ALL, "");
301 while ((c = getopt_long(argc, argv, "hip:s:",
302 long_options, NULL)) != -1) {
315 db_filename = strdup(optarg);
321 "mymail: Search pattern already defined.\n");
324 search_pattern = strdup(optarg);
334 db_filename = strdup("/tmp/mymail");
348 FILE *db_file = fopen(db_filename, "w");
351 "mymail: Can not open \"%s\" for writing: %s\n",
357 for(f = 0; f < nb_fields_to_parse; f++) {
358 if(regcomp(&fields_to_parse[f].regexp,
359 fields_to_parse[f].regexp_string,
362 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
363 fields_to_parse[f].regexp_string,
364 fields_to_parse[f].name);
369 while(optind < argc) {
370 process_entry(argv[optind],
371 nb_fields_to_parse, fields_to_parse, db_file);
377 for(f = 0; f < nb_fields_to_parse; f++) {
378 regfree(&fields_to_parse[f].regexp);
386 char *search_regexp_string;
387 search_name = search_pattern;
388 search_regexp_string = segment_next_field(search_pattern);
389 if(!*search_regexp_string) {
391 "Syntax error in the search pattern.\n");
395 /* printf("Starting search in %s for field \"%s\" matching \"%s\".\n", */
398 /* search_regexp_string); */
400 db_file = fopen(db_filename, "r");
404 "mymail: Can not open \"%s\" for reading: %s\n",
410 search_in_db(search_name, search_regexp_string, db_file);
413 free(search_pattern);