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

Annotation of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations)
Tue Sep 11 16:15:21 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.13: +1 -1 lines
File MIME type: text/plain
Case insensitive commands.

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    
28     if (!cfg) {
29     cfg = (char *) malloc(PATH_MAX * sizeof(char));
30    
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     if (!fp) {
45 lefcha 1.11 error("imapfilter: opening config file %s; %s\n",
46     cfg, strerror(errno));
47 lefcha 1.8 return 1;
48 lefcha 1.1 }
49    
50     r = parse_config(fp);
51    
52     fclose(fp);
53    
54 lefcha 1.11 #ifdef DEBUG
55     printf("debug: options: %0#10x\n", options);
56     #endif
57    
58 lefcha 1.1 return r;
59     }
60    
61 lefcha 1.6
62 lefcha 1.13 #ifdef CHECK_PERMISSIONS
63 lefcha 1.1 /*
64 lefcha 1.13 * Check the permissions of the configuration file.
65 lefcha 1.10 */
66 lefcha 1.13 int check_permissions(char *cfg)
67 lefcha 1.10 {
68     struct stat fs;
69    
70     if (stat(cfg, &fs)) {
71 lefcha 1.11 error("imapfilter: getting file %s status; %s\n", cfg,
72     strerror(errno));
73 lefcha 1.10 return 1;
74     }
75    
76     if (!S_ISREG(fs.st_mode)) {
77 lefcha 1.11 error("imapfilter: file %s not a regular file\n", cfg);
78 lefcha 1.10 return 1;
79     }
80    
81     if ((fs.st_mode & 00777) != (S_IRUSR | S_IWUSR)) {
82     fprintf(stderr,
83 lefcha 1.13 "imapfilter: warning: improper config file %s permissions\n"
84 lefcha 1.10 "imapfilter: warning: file's mode should be 600 not %o\n",
85     cfg, fs.st_mode & 00777);
86     return 1;
87     }
88    
89     return 0;
90     }
91 lefcha 1.12 #endif
92 lefcha 1.10
93     /*
94 lefcha 1.13 * Parse configuration file.
95 lefcha 1.1 */
96     int parse_config(FILE * fp)
97     {
98     int i;
99     unsigned int row = 0;
100     char line[LINE_MAX];
101 lefcha 1.13 regex_t creg[10];
102     regmatch_t match[7];
103     const char *reg[10] = {
104     "^[[:blank:]]*ACCOUNT[[:blank:]]+([[:graph:]]*):([[:graph:]]*)@([[:alnum:].-]+):?([[:digit:]]{0,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|MOVE|LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$",
108     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(ALL|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:]]+)\n$",
110     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(HEADER)[[:blank:]]+([[:graph:]]+)[[:blank:]]+([[:print:]]+)\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 lefcha 1.11 "^([[:blank:]]*\n|#.*\n)$"
114 lefcha 1.1 };
115    
116 lefcha 1.13 for (i = 0; i < 10; i++)
117 lefcha 1.14 regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE);
118 lefcha 1.1
119     while (fgets(line, LINE_MAX - 1, fp)) {
120     row++;
121 lefcha 1.13 if (!regexec(&creg[0], line, 6, match, 0)) {
122     set_account(line, match);
123 lefcha 1.1 continue;
124 lefcha 1.13 } else if (!regexec(&creg[1], line, 3, match, 0)) {
125     set_mboxgrp(line, match);
126 lefcha 1.1 continue;
127 lefcha 1.13 } else if (!regexec(&creg[2], line, 3, match, 0)) {
128     set_filter(line, match);
129 lefcha 1.1 continue;
130 lefcha 1.8 } else if (!regexec(&creg[3], line, 3, match, 0)) {
131 lefcha 1.13 set_action(line, match);
132 lefcha 1.4 continue;
133 lefcha 1.13 } else if (!regexec(&creg[4], line, 7, match, 0) ||
134     !regexec(&creg[5], line, 7, match, 0) ||
135     !regexec(&creg[6], line, 7, match, 0) ||
136     !regexec(&creg[7], line, 7, match, 0)) {
137     set_mask(line, match);
138 lefcha 1.1 continue;
139 lefcha 1.13 } else if (!regexec(&creg[8], line, 3, match, 0)) {
140     set_job(line, match);
141     continue;
142     } else if (!regexec(&creg[9], line, 0, match, 0)) {
143 lefcha 1.11 continue;
144     } else {
145     error("imapfilter: parse error in config file at row %d\n",
146     row);
147 lefcha 1.8 return 1;
148 lefcha 1.1 }
149     }
150 lefcha 1.3
151 lefcha 1.13 for (i = 0; i < 10; i++)
152 lefcha 1.12 regfree(&creg[i]);
153 lefcha 1.6
154 lefcha 1.8 return 0;
155 lefcha 1.1 }
156    
157    
158     /*
159 lefcha 1.13 * Set other options found in config file.
160 lefcha 1.11 */
161     void set_options(char *line, regmatch_t * match)
162     {
163     int s;
164    
165 lefcha 1.13 if (!*logfile && !strncmp(line + match[1].rm_so, "LOGFILE", 7)) {
166 lefcha 1.11 s = min((match[2].rm_eo - match[2].rm_so), PATH_MAX - 1);
167     strncpy(logfile, line + match[2].rm_so, s);
168     logfile[s] = 0;
169 lefcha 1.4 }
170 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26