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

Annotation of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Sun Aug 12 16:58:21 2001 UTC (22 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.2: +6 -0 lines
File MIME type: text/plain
Fix bug with no filters in config file, and introduce inverted DENY filters functionality

1 lefcha 1.1 #include <stdio.h>
2     #include <errno.h>
3     #include <regex.h>
4     #include <string.h>
5     #include <stdlib.h>
6     #include <limits.h>
7    
8     #include "config.h"
9     #include "imapfilter.h"
10     #include "file.h"
11     #include "log.h"
12    
13    
14     account_data account;
15 lefcha 1.2 filter_entry **dfilters; /* Filters of DENY type. */
16     filter_entry **afilters; /* Filters of ALLOW type. */
17     int dfcnt = 0;
18     int afcnt = 0; /* DENY and ALLOW type filters found
19     in the configuration file */
20 lefcha 1.1
21    
22     /*
23     * Finds path to configuration file, opens it and calls parse_config().
24     */
25     int read_config(char *cfg)
26     {
27     int r;
28     FILE *fp;
29     char *home = NULL;
30    
31     if (!cfg) {
32     cfg = (char *) malloc(PATH_MAX * sizeof(char));
33    
34     home = getenv("HOME");
35    
36     snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc");
37     }
38     #ifdef DEBUG
39     printf("debug: configuration file: %s\n", cfg);
40     #endif
41    
42     fp = fopen(cfg, "r");
43    
44     if (!fp) {
45     fprintf(stderr, "imapfilter: Could not open config file %s; %s\n",
46     cfg, strerror(errno));
47     return FAILURE;
48     }
49    
50     account.port = 143; /* Default IMAP protocol port to connect. */
51    
52     r = parse_config(fp);
53    
54     fclose(fp);
55    
56     return r;
57     }
58    
59     /*
60     * Parses configuration file.
61     */
62     int parse_config(FILE * fp)
63     {
64     int i;
65     unsigned int row = 0;
66     char line[LINE_MAX];
67     regex_t comreg[4];
68 lefcha 1.2 regmatch_t match[4];
69 lefcha 1.1 const char *reg[] = {
70     "^[[:blank:]]*(SERVER|PORT|USERNAME|PASSWORD|LOGFILE)[[:blank:]]*=[[:blank:]]*([[:graph:]]*)[[:blank:]]*",
71 lefcha 1.2 "^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*(FROM|CC|BCC|SUBJECT|TO):? ([[:print:]]*)",
72     "^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*([[:graph:]]+):? ([[:print:]]*)",
73 lefcha 1.1 "^[[:blank:]]*#{0,1}.*"
74     };
75    
76     for (i = 0; i < 4; i++)
77     regcomp(&comreg[i], reg[i], REG_EXTENDED);
78    
79     while (fgets(line, LINE_MAX - 1, fp)) {
80     row++;
81     if (!regexec(&comreg[0], line, 3, match, 0)) {
82     set_account(line, match);
83     continue;
84 lefcha 1.2 } else if (!regexec(&comreg[1], line, 4, match, 0)) {
85 lefcha 1.1 standard_filter(line, match);
86     continue;
87 lefcha 1.2 } else if (!regexec(&comreg[2], line, 4, match, 0)) {
88     custom_filter(line, match);
89 lefcha 1.1 continue;
90     } else if (!regexec(&comreg[3], line, 1, match, 0))
91     continue;
92     else {
93     fprintf(stderr,
94     "imapfilter: parse error in config file at row %d\n",
95     row);
96     return FAILURE;
97     }
98     }
99 lefcha 1.3
100     if (!dfcnt && !afcnt) {
101     fprintf(stderr, "imapfilter: no filters defined in config file\n");
102     return FAILURE;
103     }
104    
105 lefcha 1.1 #ifdef DEBUG
106     printf("debug: account setting SERVER: '%s'\n"
107     "debug: account setting PORT: %d\n"
108     "debug: account setting USERNAME: '%s'\n"
109     "debug: account setting PASSWORD: '%s'\n"
110     "debug: account setting LOGFILE: '%s'\n",
111     account.server, account.port, account.username,
112     account.password, account.logfile);
113     #endif
114     return SUCCESS;
115     }
116    
117    
118     /*
119     * An account setting was found from parse_config(), changes the
120     * apropriate variable.
121     */
122     void set_account(char *line, regmatch_t * match)
123     {
124     int s;
125     char port[6];
126    
127     if (!strncmp(line + match[1].rm_so, "SERVER", 6)) {
128     s = min((match[2].rm_eo - match[2].rm_so), SERVER_MAX - 1);
129     strncpy(account.server, line + match[2].rm_so, s);
130     account.server[s] = 0;
131     } else if (!strncmp(line + match[1].rm_so, "PORT", 4)) {
132     strncpy(port, line + match[2].rm_so,
133     min((match[2].rm_eo - match[2].rm_so), sizeof(port)));
134     account.port = strtoul(port, NULL, 0);
135     } else if (!strncmp(line + match[1].rm_so, "USERNAME", 8)) {
136     s = min((match[2].rm_eo - match[2].rm_so), USERNAME_MAX - 1);
137     strncpy(account.username, line + match[2].rm_so, s);
138     account.username[s] = 0;
139     } else if (!strncmp(line + match[1].rm_so, "PASSWORD", 8)) {
140     s = min((match[2].rm_eo - match[2].rm_so), PASSWORD_MAX - 1);
141     strncpy(account.password, line + match[2].rm_so, s);
142     account.password[s] = 0;
143     } else if (!strncmp(line + match[1].rm_so, "LOGFILE", 7)) {
144     s = min((match[2].rm_eo - match[2].rm_so), PATH_MAX - 1);
145     strncpy(account.logfile, line + match[2].rm_so, s);
146     account.logfile[s] = 0;
147     }
148     }
149    
150    
151     /*
152     * A filter entry was found from parse_config() proccesses it and saves it.
153     */
154     void set_filters(char *line, regmatch_t * match, int expl)
155     {
156     int s;
157 lefcha 1.2 int *fcnt;
158     filter_entry ***filters;
159    
160     if (!(strncmp(line + match[1].rm_so, "DENY", 4))) {
161     #ifdef DEBUG
162     printf("debug: filter entry: DENY");
163     #endif
164     fcnt = &dfcnt;
165     filters = &dfilters;
166     } else {
167     #ifdef DEBUG
168     printf("debug: filter entry: ALLOW");
169     #endif
170     fcnt = &afcnt;
171     filters = &afilters;
172     }
173 lefcha 1.1
174     /* Every FILTER_ENTRIES_MAX, space for more filters is realloc() -ed. */
175 lefcha 1.2 if (!*fcnt)
176     *filters =
177 lefcha 1.1 (filter_entry **) malloc(FILTER_ENTRIES_MAX *
178 lefcha 1.2 sizeof(filter_entry *));
179     else if (!(*fcnt % FILTER_ENTRIES_MAX))
180     *filters = (filter_entry **) realloc(*filters,
181     (((*fcnt /
182     FILTER_ENTRIES_MAX) +
183     1) * FILTER_ENTRIES_MAX) *
184     sizeof(filter_entry *));
185    
186     (*filters)[*fcnt] = (filter_entry *) malloc(sizeof(filter_entry));
187    
188     (*filters)[*fcnt]->custom = expl;
189    
190     s = min((match[2].rm_eo - match[2].rm_so), FIELD_NAME_MAX - 1);
191     strncpy((*filters)[*fcnt]->name, line + match[2].rm_so, s);
192     (*filters)[*fcnt]->name[s] = 0;
193    
194     s = min((match[3].rm_eo - match[3].rm_so), FIELD_BODY_MAX - 1);
195     strncpy((*filters)[*fcnt]->body, line + match[3].rm_so, s);
196     (*filters)[*fcnt]->body[s] = 0;
197 lefcha 1.1
198     #ifdef DEBUG
199 lefcha 1.2 printf(" '%s': '%s'\n", (*filters)[*fcnt]->name,
200     (*filters)[*fcnt]->body);
201 lefcha 1.1 #endif
202    
203 lefcha 1.2 (*fcnt)++;
204 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26