/[imapfilter]/imapfilter/file.c
ViewVC logotype

Contents of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (show annotations)
Sun Sep 30 20:16:25 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.16: +32 -37 lines
File MIME type: text/plain
Error codes, better string manipulation and parse errors handling.

1 #include <stdio.h>
2 #include <errno.h>
3 #include <sys/types.h>
4 #include <regex.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <limits.h>
8 #include <sys/stat.h>
9
10 #include "config.h"
11 #include "imapfilter.h"
12 #include "data.h"
13
14
15 extern char logfile[PATH_MAX];
16 extern unsigned int options;
17
18
19 /*
20 * Find the path to configuration file, open it and call parse_config().
21 */
22 int read_config(char *cfg)
23 {
24 int r;
25 FILE *fp;
26 char *home = NULL;
27
28 if (!cfg) {
29 cfg = (char *) malloc(PATH_MAX * sizeof(char));
30
31
32 home = getenv("HOME");
33
34 snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc");
35 }
36 #ifdef DEBUG
37 printf("debug: configuration file: '%s'\n", cfg);
38 #endif
39 #ifdef CHECK_PERMISSIONS
40 check_permissions(cfg);
41 #endif
42 fp = fopen(cfg, "r");
43
44 if (!fp)
45 fatal(ERROR_FILE_OPEN, "imapfilter: opening config file %s; %s\n",
46 cfg, strerror(errno));
47
48 if ((r = parse_config(fp)))
49 fatal(ERROR_CONFIG_PARSE,
50 "imapfilter: parse error in config file at row %d\n", r);
51
52 fclose(fp);
53
54 #ifdef DEBUG
55 printf("debug: options: %0#10x\n", options);
56 #endif
57
58 return 0;
59 }
60
61
62 #ifdef CHECK_PERMISSIONS
63 /*
64 * Check the permissions of the configuration file.
65 */
66 int check_permissions(char *cfg)
67 {
68 struct stat fs;
69
70 if (stat(cfg, &fs)) {
71 error("imapfilter: getting file %s status; %s\n", cfg,
72 strerror(errno));
73 return ERROR_TRIVIAL;
74 }
75
76 if (!S_ISREG(fs.st_mode)) {
77 error("imapfilter: file %s not a regular file\n", cfg);
78 return ERROR_TRIVIAL;
79 }
80
81 if ((fs.st_mode & 00777) != (S_IRUSR | S_IWUSR)) {
82 error("imapfilter: warning: improper config file %s permissions\n"
83 "imapfilter: warning: file's mode should be 600 not %o\n",
84 cfg, fs.st_mode & 00777);
85 return ERROR_TRIVIAL;
86 }
87
88 return 0;
89 }
90 #endif
91
92 /*
93 * Parse configuration file.
94 */
95 int parse_config(FILE * fp)
96 {
97 int i, r = 0;
98 unsigned int row = 0;
99 char line[LINE_MAX];
100 regex_t creg[11];
101 regmatch_t match[7];
102 const char *reg[11] = {
103 "^([[:blank:]]*\n|#.*\n)$",
104 "^[[:blank:]]*ACCOUNT[[:blank:]]+([[:graph:]]*):([[:graph:]]*)@([[:alnum:].-]+)(:[[:digit:]]{1,5})?[[:blank:]]*\n$",
105 "^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+([[:graph:]]+)[[:blank:]]*\n$",
106 "^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$",
107 "^[[:blank:]]*ACTION[[:blank:]]+(DELETE|COPY[[:blank:]]+([[:graph:]]+)|MOVE[[:blank:]]+([[:graph:]]+)|LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$",
108 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(ANSWERED|DELETED|DRAFT|FLAGGED|NEW|OLD|RECENT|SEEN|UNANSWERED|UNDELETED|UNDRAFT|UNFLAGGED|UNSEEN)[[:blank:]]*\n$",
109 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(BCC|BODY|CC|FROM|SUBJECT|TEXT|TO)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$",
110 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(HEADER)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$",
111 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(LARGER|SMALLER)[[:blank:]]+([[:digit:]]+)[[:blank:]]*\n$",
112 "^[[:blank:]]*JOB[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]*\n$",
113 "^[[:blank:]]*LOGFILE[[:blank:]]*=[[:blank:]]*([[:print:]]+)\n$"
114 };
115
116 for (i = 0; i < 11; i++)
117 regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE);
118
119 while (fgets(line, LINE_MAX - 1, fp)) {
120 row++;
121 if (!regexec(&creg[0], line, 0, match, 0))
122 continue;
123 else if (!regexec(&creg[1], line, 6, match, 0))
124 set_account(line, match);
125 else if (!regexec(&creg[2], line, 3, match, 0))
126 r = set_mboxgrp(line, match);
127 else if (!regexec(&creg[3], line, 3, match, 0))
128 r = set_filter(line, match);
129 else if (!regexec(&creg[4], line, 5, match, 0))
130 r = set_action(line, match);
131 else if (!regexec(&creg[5], line, 7, match, 0) ||
132 !regexec(&creg[6], line, 7, match, 0) ||
133 !regexec(&creg[7], line, 7, match, 0) ||
134 !regexec(&creg[8], line, 7, match, 0))
135 r = set_mask(line, match);
136 else if (!regexec(&creg[9], line, 3, match, 0))
137 r = set_job(line, match);
138 else if (!regexec(&creg[10], line, 2, match, 0))
139 set_options(line, match);
140 else
141 return row;
142
143 if (r == ERROR_CONFIG_PARSE)
144 return row;
145 }
146
147 for (i = 0; i < 11; i++)
148 regfree(&creg[i]);
149
150 destroy_data();
151
152 return 0;
153 }
154
155
156 /*
157 * Set other options found in config file.
158 */
159 void set_options(char *line, regmatch_t * match)
160 {
161 if (!*logfile)
162 strncat(logfile, line + match[1].rm_so,
163 min((match[1].rm_eo - match[1].rm_so), PATH_MAX - 1));
164 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26