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 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 /* printf("LINE [%s] %s", name, value); */
116 if(strcmp("mail", name) == 0) {
117 char *position_in_file_string = value;
118 char *mail_filename = segment_next_field(value);
119 current_position_in_mail = atoi(position_in_file_string);
120 strcpy(current_mail_filename, mail_filename);
121 remove_eof(current_mail_filename);
122 /* printf("READING [%s]\n", current_mail_filename); */
124 } else if(!already_written) {
125 if(strcmp(search_name, name) == 0 && regexec(®exp, value, 0, 0, 0) == 0) {
127 /* printf("%s:%u\n", current_mail_filename, current_position_in_mail); */
128 mail_file = fopen(current_mail_filename, "r");
130 fprintf(stderr, "mymail: Can not open `%s'.\n", current_mail_filename);
133 fseek(mail_file, current_position_in_mail, SEEK_SET);
134 if(fgets(raw_line, BUFFER_SIZE, mail_file)) {
135 printf("%s", raw_line);
136 while(fgets(raw_line, BUFFER_SIZE, mail_file) &&
137 strncmp(raw_line, "From ", 5)) {
138 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 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:%u.\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 %u %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",
238 if(S_ISLNK(sb.st_mode)) {
242 dir = opendir(dir_name);
245 printf("Processing directory '%s'.\n", dir_name);
246 while((dir_e = readdir(dir))) {
247 if(!ignore_entry(dir_e->d_name)) {
248 snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
249 process_entry(subname, nb_fields_to_parse, fields_to_parse, db_file);
254 if(S_ISREG(sb.st_mode)) {
255 /* printf("Processing regular file '%s'.\n", dir_name); */
256 read_file(dir_name, nb_fields_to_parse, fields_to_parse, db_file);
261 /*********************************************************************/
263 /* For long options that have no equivalent short option, use a
264 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
267 OPT_BASH_MODE = CHAR_MAX + 1
270 static struct option long_options[] = {
271 { "help", no_argument, 0, 'h' },
272 { "db-prefix", 1, 0, 'p' },
273 { "search-pattern", 1, 0, 's' },
274 { "index", 0, 0, 'i' },
278 static struct parsable_field fields_to_parse[] = {
281 "^\\([Ff][Rr][Oo][Mm]:\\|From\\) *",
282 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
287 "^\\([Tt][Oo]\\|[Cc][Cc]\\|[Bb][Cc][Cc]\\): *",
288 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
292 int main(int argc, char **argv) {
293 int error = 0, show_help = 0;
294 const int nb_fields_to_parse = sizeof(fields_to_parse) / sizeof(struct parsable_field);
301 setlocale(LC_ALL, "");
303 while ((c = getopt_long(argc, argv, "hip:s:",
304 long_options, NULL)) != -1) {
317 db_filename = strdup(optarg);
323 "mymail: Search pattern already defined.\n");
326 search_pattern = strdup(optarg);
336 db_filename = strdup("/tmp/mymail");
350 FILE *db_file = fopen(db_filename, "w");
353 "mymail: Can not open \"%s\" for writing: %s\n",
359 for(f = 0; f < nb_fields_to_parse; f++) {
360 if(regcomp(&fields_to_parse[f].regexp,
361 fields_to_parse[f].regexp_string,
364 "mymail: Syntax error in regexp \"%s\" for field \"%s\".\n",
365 fields_to_parse[f].regexp_string,
366 fields_to_parse[f].name);
371 while(optind < argc) {
372 process_entry(argv[optind],
373 nb_fields_to_parse, fields_to_parse, db_file);
379 for(f = 0; f < nb_fields_to_parse; f++) {
380 regfree(&fields_to_parse[f].regexp);
388 char *search_regexp_string;
389 search_name = search_pattern;
390 search_regexp_string = segment_next_field(search_pattern);
391 if(!*search_regexp_string) {
393 "Syntax error in the search pattern.\n");
397 /* printf("Starting search in %s for field \"%s\" matching \"%s\".\n", */
400 /* search_regexp_string); */
402 db_file = fopen(db_filename, "r");
406 "mymail: Can not open \"%s\" for reading: %s\n",
412 search_in_db(search_name, search_regexp_string, db_file);
415 free(search_pattern);