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

Annotation of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (hide annotations)
Tue Oct 23 09:21:45 2001 UTC (22 years, 5 months ago) by lefcha
Branch: MAIN
CVS Tags: release-0_7
Branch point for: release-0_7-patches
Changes since 1.25: +10 -10 lines
File MIME type: text/plain
Added filtering based on messages' date.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26