29 |
/* |
/* |
30 |
* Set new account's variables to safe values. |
* Set new account's variables to safe values. |
31 |
*/ |
*/ |
32 |
void init_account(account_t * node) |
void |
33 |
|
init_account(account_t * node) |
34 |
{ |
{ |
35 |
node->next = NULL; |
node->next = NULL; |
36 |
node->key[0] = node->server[0] = node->username[0] = node->password[0] = 0; |
node->key[0] = node->server[0] = '\0'; |
37 |
node->passwdattr = PASSWORD_NONE; |
node->username[0] = node->password[0] = '\0'; |
38 |
node->port = 143; |
node->passwdattr = PASSWORD_NONE; |
39 |
node->ssl = SSL_DISABLED; |
node->port = 143; |
40 |
node->mboxes = NULL; |
node->ssl = SSL_DISABLED; |
41 |
|
node->mboxes = NULL; |
42 |
} |
} |
43 |
|
|
44 |
|
|
45 |
/* |
/* |
46 |
* Append account node to linked list. |
* Append account node to linked list. |
47 |
*/ |
*/ |
48 |
void append_account(account_t * node) |
void |
49 |
|
append_account(account_t * node) |
50 |
{ |
{ |
51 |
account_t *pos; |
account_t *pos; |
52 |
account_t **app; |
account_t **app; |
53 |
|
|
54 |
APPEND_LINKED_LIST(accounts, node, pos, app); |
APPEND_LINKED_LIST(accounts, node, pos, app); |
55 |
} |
} |
56 |
|
|
57 |
|
|
59 |
* A new account entry was declared. Create it and set it's variables |
* A new account entry was declared. Create it and set it's variables |
60 |
* accordingly. |
* accordingly. |
61 |
*/ |
*/ |
62 |
int set_account(char *line, regmatch_t * match) |
int |
63 |
|
set_account(char *line, regmatch_t * m) |
64 |
{ |
{ |
65 |
int n; |
int n; |
66 |
char p[6]; |
char p[6]; |
67 |
account_t *node; |
account_t *node; |
68 |
|
|
69 |
node = (account_t *) create_node(sizeof(account_t)); |
node = (account_t *) create_node(sizeof(account_t)); |
70 |
node->password = (char *)smalloc(PASSWORD_LEN); |
node->password = (char *)smalloc(PASSWORD_LEN); |
71 |
|
|
72 |
init_account(node); |
init_account(node); |
73 |
|
|
74 |
strncat(node->key, line + match[1].rm_so, |
strncat(node->key, line + m[1].rm_so, |
75 |
min(match[1].rm_eo - match[1].rm_so, KEY_LEN - 1)); |
min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1)); |
76 |
#ifdef DEBUG |
#ifdef DEBUG |
77 |
printf("debug: ACCOUNT: '%s'\n", node->key); |
fprintf(stderr, "debug: ACCOUNT: '%s'\n", node->key); |
78 |
#endif |
#endif |
79 |
|
|
80 |
if (match[3].rm_so != -1) { |
if (m[3].rm_so != -1) { |
81 |
strncat(node->username, line + match[3].rm_so, |
strncat(node->username, line + m[3].rm_so, |
82 |
min(match[3].rm_eo - match[3].rm_so, USERNAME_LEN - 1)); |
min(m[3].rm_eo - m[3].rm_so, USERNAME_LEN - 1)); |
83 |
} else { |
} else { |
84 |
strncat(node->username, line + match[5].rm_so, |
strncat(node->username, line + m[5].rm_so, |
85 |
min(match[5].rm_eo - match[5].rm_so, USERNAME_LEN - 1)); |
min(m[5].rm_eo - m[5].rm_so, USERNAME_LEN - 1)); |
86 |
} |
} |
87 |
if (strchr(node->username, '%')) |
if (strchr(node->username, '%')) |
88 |
if (string_decode(node->username)) |
if (string_decode(node->username)) |
89 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
90 |
#ifdef DEBUG |
#ifdef DEBUG |
91 |
printf("debug: USERNAME: '%s'\n", node->username); |
fprintf(stderr, "debug: USERNAME: '%s'\n", node->username); |
92 |
#endif |
#endif |
93 |
|
|
94 |
if (match[4].rm_so != -1) { |
if (m[4].rm_so != -1) { |
95 |
strncat(node->password, line + match[4].rm_so, |
strncat(node->password, line + m[4].rm_so, |
96 |
min(match[4].rm_eo - match[4].rm_so, PASSWORD_LEN - 1)); |
min(m[4].rm_eo - m[4].rm_so, PASSWORD_LEN - 1)); |
97 |
if (strchr(node->password, '%')) |
if (strchr(node->password, '%')) |
98 |
if (string_decode(node->password)) |
if (string_decode(node->password)) |
99 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
100 |
node->passwdattr = PASSWORD_PLAIN; |
node->passwdattr = PASSWORD_PLAIN; |
101 |
} else |
} else |
102 |
flags |= FLAG_BLANK_PASSWORD; |
flags |= FLAG_BLANK_PASSWORD; |
103 |
#ifdef DEBUG |
#ifdef DEBUG |
104 |
printf("debug: PASSWORD: '%s'\n", node->password); |
fprintf(stderr, "debug: PASSWORD: '%s'\n", node->password); |
105 |
#endif |
#endif |
106 |
|
|
107 |
strncat(node->server, line + match[6].rm_so, |
strncat(node->server, line + m[6].rm_so, |
108 |
min(match[6].rm_eo - match[6].rm_so, SERVER_LEN - 1)); |
min(m[6].rm_eo - m[6].rm_so, SERVER_LEN - 1)); |
109 |
#ifdef DEBUG |
#ifdef DEBUG |
110 |
printf("debug: SERVER: '%s'\n", node->server); |
fprintf(stderr, "debug: SERVER: '%s'\n", node->server); |
111 |
#endif |
#endif |
112 |
|
|
113 |
if (match[7].rm_so != -1) { |
if (m[7].rm_so != -1) { |
114 |
n = min(match[7].rm_eo - match[7].rm_so - 1, 5); |
n = min(m[7].rm_eo - m[7].rm_so - 1, 5); |
115 |
xstrncpy(p, line + match[7].rm_so + 1, n); |
xstrncpy(p, line + m[7].rm_so + 1, n); |
116 |
p[n] = 0; |
p[n] = '\0'; |
117 |
node->port = strtoul(p, NULL, 10); |
node->port = strtoul(p, NULL, 10); |
118 |
#ifdef DEBUG |
#ifdef DEBUG |
119 |
printf("debug: PORT: %d\n", node->port); |
fprintf(stderr, "debug: PORT: %d\n", node->port); |
120 |
#endif |
#endif |
121 |
} |
} |
122 |
if (match[8].rm_so != -1) { |
if (m[8].rm_so != -1) { |
123 |
if (match[7].rm_so == -1) |
if (m[7].rm_so == -1) |
124 |
node->port = 993; |
node->port = 993; |
125 |
|
|
126 |
if (strcasestr(line + match[8].rm_so, "ssl3")) |
if (strcasestr(line + m[8].rm_so, "SSL2")) |
127 |
node->ssl = SSL_SSL_V3; |
node->ssl = SSL_SSL_V2; |
128 |
else if (strcasestr(line + match[8].rm_so, "tls1")) |
else if (strcasestr(line + m[8].rm_so, "SSL3")) |
129 |
node->ssl = SSL_TLS_V1; |
node->ssl = SSL_SSL_V3; |
130 |
else |
else |
131 |
node->ssl = SSL_SSL_V2; |
node->ssl = SSL_TLS_V1; |
132 |
} |
} |
133 |
append_account(node); |
append_account(node); |
134 |
cur_acct = node; |
cur_acct = node; |
135 |
|
|
136 |
return 0; |
return 0; |
137 |
} |
} |
138 |
|
|
139 |
|
|
141 |
/* |
/* |
142 |
* Find accounts without a password (candicates for password encryption). |
* Find accounts without a password (candicates for password encryption). |
143 |
*/ |
*/ |
144 |
char *find_password(char *user, char *serv) |
char * |
145 |
|
find_password(char *user, char *serv) |
146 |
{ |
{ |
147 |
account_t *a; |
account_t *a; |
148 |
|
|
149 |
for (a = accounts; a; a = a->next) |
for (a = accounts; a != NULL; a = a->next) |
150 |
if (a->passwdattr == PASSWORD_NONE && !strcmp(a->server, serv) && |
if (a->passwdattr == PASSWORD_NONE && |
151 |
!strcmp(a->username, user)) { |
!strcmp(a->server, serv) && !strcmp(a->username, user)) { |
152 |
a->passwdattr = PASSWORD_ENCRYPTED; |
a->passwdattr = PASSWORD_ENCRYPTED; |
153 |
return a->password; |
return a->password; |
154 |
} |
} |
155 |
return NULL; |
return NULL; |
156 |
} |
} |
157 |
|
|
158 |
#endif |
#endif |
160 |
/* |
/* |
161 |
* Set new mailbox-group's variables to safe values. |
* Set new mailbox-group's variables to safe values. |
162 |
*/ |
*/ |
163 |
void init_mboxgrp(mboxgrp_t * node) |
void |
164 |
|
init_mboxgrp(mboxgrp_t * node) |
165 |
{ |
{ |
166 |
node->left = node->right = NULL; |
node->left = node->right = NULL; |
167 |
node->key[0] = 0; |
node->key[0] = '\0'; |
168 |
node->mboxes[0] = NULL; |
node->mboxes[0] = NULL; |
169 |
} |
} |
170 |
|
|
171 |
|
|
172 |
/* |
/* |
173 |
* Insert mailbox-group node in tree. |
* Insert mailbox-group node in tree. |
174 |
*/ |
*/ |
175 |
void insert_mboxgrp(mboxgrp_t * node) |
void |
176 |
|
insert_mboxgrp(mboxgrp_t * node) |
177 |
{ |
{ |
178 |
int cmp; |
int cmp; |
179 |
mboxgrp_t *pos; |
mboxgrp_t *pos; |
180 |
mboxgrp_t **ins; |
mboxgrp_t **ins; |
181 |
|
|
182 |
INSERT_TREE(mboxgrps, node, pos, ins, cmp); |
INSERT_TREE(mboxgrps, node, pos, ins, cmp); |
183 |
} |
} |
184 |
|
|
185 |
|
|
187 |
* A new mailbox-group entry was declared. Create it and set it's variables |
* A new mailbox-group entry was declared. Create it and set it's variables |
188 |
* accordingly. |
* accordingly. |
189 |
*/ |
*/ |
190 |
int set_mboxgrp(char *line, regmatch_t * match) |
int |
191 |
|
set_mboxgrp(char *line, regmatch_t * m) |
192 |
{ |
{ |
193 |
mboxgrp_t *node; |
mboxgrp_t *node; |
194 |
char mboxs[LINE_MAX]; |
char mboxs[LINE_MAX]; |
195 |
|
|
196 |
mboxs[0] = 0; |
mboxs[0] = '\0'; |
197 |
|
|
198 |
if (!accounts) |
if (accounts == NULL) |
199 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
200 |
|
|
201 |
node = (mboxgrp_t *) create_node(sizeof(mboxgrp_t)); |
node = (mboxgrp_t *) create_node(sizeof(mboxgrp_t)); |
202 |
|
|
203 |
init_mboxgrp(node); |
init_mboxgrp(node); |
204 |
|
|
205 |
strncat(node->key, line + match[1].rm_so, |
strncat(node->key, line + m[1].rm_so, |
206 |
min(match[1].rm_eo - match[1].rm_so, KEY_LEN - 1)); |
min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1)); |
207 |
|
|
208 |
#ifdef DEBUG |
#ifdef DEBUG |
209 |
printf("debug: FOLDER: '%s'\n", node->key); |
fprintf(stderr, "debug: FOLDER: '%s'\n", node->key); |
210 |
#endif |
#endif |
211 |
|
|
212 |
strncat(mboxs, line + match[2].rm_so, |
strncat(mboxs, line + m[2].rm_so, |
213 |
min(match[2].rm_eo - match[2].rm_so, LINE_MAX - 1)); |
min(m[2].rm_eo - m[2].rm_so, LINE_MAX - 1)); |
214 |
|
|
215 |
process_mboxgrp(node, mboxs); |
process_mboxgrp(node, mboxs); |
216 |
|
|
217 |
insert_mboxgrp(node); |
insert_mboxgrp(node); |
218 |
|
|
219 |
return 0; |
return 0; |
220 |
} |
} |
221 |
|
|
222 |
|
|
224 |
* Calls set_mbox() in order to create mailboxes that are part of |
* Calls set_mbox() in order to create mailboxes that are part of |
225 |
* the mailbox-group. |
* the mailbox-group. |
226 |
*/ |
*/ |
227 |
void process_mboxgrp(mboxgrp_t * node, char *mboxs) |
void |
228 |
|
process_mboxgrp(mboxgrp_t * node, char *mboxs) |
229 |
{ |
{ |
230 |
unsigned int i = 0; |
unsigned int i; |
231 |
char *tok; |
char *tok; |
232 |
|
|
233 |
|
i = 0; |
234 |
|
|
235 |
tok = strtok_r(mboxs, ",", &mboxs); |
tok = strtok_r(mboxs, ",", &mboxs); |
236 |
while (i < MBOXGRP_MBOXES_MAX - 1 && tok) { |
while (i < MBOXGRP_MBOXES_MAX - 1 && tok != NULL) { |
237 |
node->mboxes[i] = (mbox_t *) set_mbox(tok); |
node->mboxes[i] = (mbox_t *) set_mbox(tok); |
238 |
node->mboxes[++i] = NULL; |
node->mboxes[++i] = NULL; |
239 |
|
|
240 |
tok = strtok_r(NULL, ",", &mboxs); |
tok = strtok_r(NULL, ",", &mboxs); |
241 |
} |
} |
242 |
} |
} |
243 |
|
|
244 |
|
|
246 |
* Find in the mailbox-group tree, the node with the specified key, |
* Find in the mailbox-group tree, the node with the specified key, |
247 |
* and return a pointer to it. |
* and return a pointer to it. |
248 |
*/ |
*/ |
249 |
mboxgrp_t *find_mboxgrp(char *key) |
mboxgrp_t * |
250 |
|
find_mboxgrp(char *key) |
251 |
{ |
{ |
252 |
int cmp; |
int cmp; |
253 |
mboxgrp_t *pos; |
mboxgrp_t *pos; |
254 |
|
|
255 |
FIND_TREE(mboxgrps, key, pos, cmp); |
FIND_TREE(mboxgrps, key, pos, cmp); |
256 |
} |
} |
257 |
|
|
258 |
|
|
259 |
/* |
/* |
260 |
* Set new mailbox's variables to safe values. |
* Set new mailbox's variables to safe values. |
261 |
*/ |
*/ |
262 |
void init_mbox(mbox_t * node) |
void |
263 |
|
init_mbox(mbox_t * node) |
264 |
{ |
{ |
265 |
node->next = NULL; |
node->next = NULL; |
266 |
node->name[0] = 0; |
node->name[0] = '\0'; |
267 |
node->filters[0] = NULL; |
node->filters[0] = NULL; |
268 |
} |
} |
269 |
|
|
270 |
|
|
271 |
/* |
/* |
272 |
* Append mailbox node to linked list. |
* Append mailbox node to linked list. |
273 |
*/ |
*/ |
274 |
void append_mbox(mbox_t * node) |
void |
275 |
|
append_mbox(mbox_t * node) |
276 |
{ |
{ |
277 |
mbox_t *pos; |
mbox_t *pos; |
278 |
mbox_t **ins; |
mbox_t **ins; |
279 |
|
|
280 |
APPEND_LINKED_LIST(cur_acct->mboxes, node, pos, ins); |
APPEND_LINKED_LIST(cur_acct->mboxes, node, pos, ins); |
281 |
} |
} |
282 |
|
|
283 |
|
|
284 |
/* |
/* |
285 |
* A new mailbox was declared, create it and set it's variables accordingly. |
* A new mailbox was declared, create it and set it's variables accordingly. |
286 |
*/ |
*/ |
287 |
mbox_t *set_mbox(char *name) |
mbox_t * |
288 |
|
set_mbox(char *name) |
289 |
{ |
{ |
290 |
mbox_t *node; |
char s[MBOX_NAME_LEN]; |
291 |
|
mbox_t *node, *m; |
292 |
|
|
293 |
|
*s = '\0'; |
294 |
|
|
295 |
node = (mbox_t *) create_node(sizeof(mbox_t)); |
if (*name == '"' && *(name + strlen(name) - 1) == '"') |
296 |
|
strncat(s, name + 1, min(strlen(name) - 2, MBOX_NAME_LEN - 1)); |
297 |
|
else |
298 |
|
strncat(s, name, min(strlen(name), MBOX_NAME_LEN - 1)); |
299 |
|
|
300 |
init_mbox(node); |
for (m = cur_acct->mboxes; m != NULL; m = m->next) |
301 |
|
if (!strcmp(m->name, s)) |
302 |
|
return m; |
303 |
|
|
304 |
if (*name == '"' && *(name + strlen(name) - 1) == '"') |
node = (mbox_t *) create_node(sizeof(mbox_t)); |
305 |
strncat(node->name, name + 1, min(strlen(name) - 2, MBOX_NAME_LEN - 1)); |
|
306 |
else |
init_mbox(node); |
307 |
strncat(node->name, name, min(strlen(name), MBOX_NAME_LEN - 1)); |
|
308 |
|
strncat(node->name, s, MBOX_NAME_LEN - 1); |
309 |
|
|
310 |
#ifdef DEBUG |
#ifdef DEBUG |
311 |
printf("debug: MBOX: '%s'\n", node->name); |
fprintf(stderr, "debug: MBOX: '%s'\n", node->name); |
312 |
#endif |
#endif |
313 |
|
|
314 |
append_mbox(node); |
append_mbox(node); |
315 |
|
|
316 |
return node; |
return node; |
317 |
} |
} |
318 |
|
|
319 |
|
|
320 |
/* |
/* |
321 |
* Set new filter's variables to safe values. |
* Set new filter's variables to safe values. |
322 |
*/ |
*/ |
323 |
void init_filter(filter_t * node) |
void |
324 |
|
init_filter(filter_t * node) |
325 |
{ |
{ |
326 |
node->left = node->right = NULL; |
node->left = node->right = NULL; |
327 |
node->key[0] = 0; |
node->key[0] = '\0'; |
328 |
node->mode = FILTER_MODE_AND; |
node->mode = FILTER_MODE_AND; |
329 |
node->action.type = node->action.msgflags = 0; |
node->action.type = node->action.msgflags = 0; |
330 |
node->action.raccount = NULL; |
node->action.raccount = NULL; |
331 |
node->action.destmbox[0] = node->action.args[0] = 0; |
node->action.destmbox[0] = node->action.args[0] = '\0'; |
332 |
node->masks = NULL; |
node->masks = NULL; |
333 |
node->masknum = node->masklen = 0; |
node->masknum = node->masklen = 0; |
334 |
} |
} |
335 |
|
|
336 |
|
|
337 |
/* |
/* |
338 |
* Insert filter node to tree. |
* Insert filter node to tree. |
339 |
*/ |
*/ |
340 |
void insert_filter(filter_t * node) |
void |
341 |
|
insert_filter(filter_t * node) |
342 |
{ |
{ |
343 |
int cmp; |
int cmp; |
344 |
filter_t *pos; |
filter_t *pos; |
345 |
filter_t **ins; |
filter_t **ins; |
346 |
|
|
347 |
INSERT_TREE(filters, node, pos, ins, cmp); |
INSERT_TREE(filters, node, pos, ins, cmp); |
348 |
} |
} |
349 |
|
|
350 |
|
|
351 |
/* |
/* |
352 |
* A filter entry was declared, create it and set it's variables accordingly. |
* A filter entry was declared, create it and set it's variables accordingly. |
353 |
*/ |
*/ |
354 |
int set_filter(char *line, regmatch_t * match) |
int |
355 |
|
set_filter(char *line, regmatch_t * m) |
356 |
{ |
{ |
357 |
filter_t *node; |
filter_t *node; |
358 |
|
|
359 |
if (cur_fltr && !cur_fltr->action.type) |
if (cur_fltr != NULL && cur_fltr->action.type == 0) |
360 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
361 |
|
|
362 |
node = (filter_t *) create_node(sizeof(filter_t)); |
node = (filter_t *) create_node(sizeof(filter_t)); |
363 |
|
|
364 |
init_filter(node); |
init_filter(node); |
365 |
|
|
366 |
strncat(node->key, line + match[1].rm_so, |
strncat(node->key, line + m[1].rm_so, |
367 |
min(match[1].rm_eo - match[1].rm_so, KEY_LEN - 1)); |
min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1)); |
368 |
|
|
369 |
if (match[2].rm_so != -1) { |
if (m[2].rm_so != -1) { |
370 |
if (!strncasecmp(line + match[2].rm_so + 1, "or", 2)) |
if (!strncasecmp(line + m[2].rm_so + 1, "or", 2)) |
371 |
node->mode = FILTER_MODE_OR; |
node->mode = FILTER_MODE_OR; |
372 |
else |
else |
373 |
node->mode = FILTER_MODE_AND; |
node->mode = FILTER_MODE_AND; |
374 |
} |
} |
375 |
#ifdef DEBUG |
#ifdef DEBUG |
376 |
printf("debug: FILTER: '%s' %s\n", node->key, |
fprintf(stderr, "debug: FILTER: '%s' %s\n", node->key, |
377 |
(node->mode == FILTER_MODE_OR ? "OR" : "AND")); |
(node->mode == FILTER_MODE_OR ? "OR" : "AND")); |
378 |
#endif |
#endif |
379 |
|
|
380 |
insert_filter(node); |
insert_filter(node); |
381 |
cur_fltr = node; |
cur_fltr = node; |
382 |
|
|
383 |
return 0; |
return 0; |
384 |
} |
} |
385 |
|
|
386 |
|
|
388 |
* Find in the filter tree, the node with the specified key and |
* Find in the filter tree, the node with the specified key and |
389 |
* return a pointer to it. |
* return a pointer to it. |
390 |
*/ |
*/ |
391 |
filter_t *find_filter(char *key) |
filter_t * |
392 |
|
find_filter(char *key) |
393 |
{ |
{ |
394 |
int cmp; |
int cmp; |
395 |
filter_t *pos; |
filter_t *pos; |
396 |
|
|
397 |
FIND_TREE(filters, key, pos, cmp); |
FIND_TREE(filters, key, pos, cmp); |
398 |
} |
} |
399 |
|
|
400 |
|
|
401 |
/* |
/* |
402 |
* Assign an action to the last declared filter. |
* Assign an action to the last declared filter. |
403 |
*/ |
*/ |
404 |
int set_action(char *line, regmatch_t * match) |
int |
405 |
|
set_action(char *line, regmatch_t * m) |
406 |
{ |
{ |
407 |
char *c, *cp, *t; |
char *c, *cp, *t; |
408 |
account_t *a; |
account_t *a; |
|
|
|
|
if (!cur_fltr) |
|
|
return ERROR_CONFIG_PARSE; |
|
|
|
|
|
if (!strncasecmp(line + match[1].rm_so, "delete", 6)) |
|
|
cur_fltr->action.type = FILTER_ACTION_DELETE; |
|
|
else if (!strncasecmp(line + match[1].rm_so, "copy", 4)) { |
|
|
cur_fltr->action.type = FILTER_ACTION_COPY; |
|
|
if (*(line + match[2].rm_so) == '"' && *(line + match[2].rm_eo - 1) == '"') |
|
|
strncat(cur_fltr->action.destmbox, line + match[2].rm_so + 1, |
|
|
min(match[2].rm_eo - match[2].rm_so - 2, MBOX_NAME_LEN - 1)); |
|
|
else |
|
|
strncat(cur_fltr->action.destmbox, line + match[2].rm_so, |
|
|
min(match[2].rm_eo - match[2].rm_so, MBOX_NAME_LEN - 1)); |
|
|
} else if (!strncasecmp(line + match[1].rm_so, "move", 4)) { |
|
|
cur_fltr->action.type = FILTER_ACTION_MOVE; |
|
|
if (*(line + match[3].rm_so) == '"' && *(line + match[3].rm_eo - 1) == '"') |
|
|
strncat(cur_fltr->action.destmbox, line + match[3].rm_so + 1, |
|
|
min(match[3].rm_eo - match[3].rm_so - 2, MBOX_NAME_LEN - 1)); |
|
|
else |
|
|
strncat(cur_fltr->action.destmbox, line + match[3].rm_so, |
|
|
min(match[3].rm_eo - match[3].rm_so, MBOX_NAME_LEN - 1)); |
|
|
} else if (!strncasecmp(line + match[1].rm_so, "rcopy", 5)) { |
|
|
cur_fltr->action.type = FILTER_ACTION_RCOPY; |
|
|
for (a = accounts; a; a = a->next) |
|
|
if (!strncasecmp(line + match[4].rm_so, a->key, strlen(a->key))) |
|
|
cur_fltr->action.raccount = a; |
|
|
if (!cur_fltr->action.raccount) |
|
|
return ERROR_CONFIG_PARSE; |
|
|
|
|
|
if (*(line + match[5].rm_so) == '"' && *(line + match[5].rm_eo - 1) == '"') |
|
|
strncat(cur_fltr->action.destmbox, line + match[5].rm_so + 1, |
|
|
min(match[5].rm_eo - match[5].rm_so - 2, MBOX_NAME_LEN - 1)); |
|
|
else |
|
|
strncat(cur_fltr->action.destmbox, line + match[5].rm_so, |
|
|
min(match[5].rm_eo - match[5].rm_so, MBOX_NAME_LEN - 1)); |
|
|
} else if (!strncasecmp(line + match[1].rm_so, "rmove", 5)) { |
|
|
cur_fltr->action.type = FILTER_ACTION_RMOVE; |
|
|
for (a = accounts; a; a = a->next) |
|
|
if (!strncasecmp(line + match[6].rm_so, a->key, strlen(a->key))) |
|
|
cur_fltr->action.raccount = a; |
|
|
if (!cur_fltr->action.raccount) |
|
|
return ERROR_CONFIG_PARSE; |
|
|
|
|
|
if (*(line + match[7].rm_so) == '"' && *(line + match[7].rm_eo - 1) == '"') |
|
|
strncat(cur_fltr->action.destmbox, line + match[7].rm_so + 1, |
|
|
min(match[7].rm_eo - match[7].rm_so - 2, MBOX_NAME_LEN - 1)); |
|
|
else |
|
|
strncat(cur_fltr->action.destmbox, line + match[7].rm_so, |
|
|
min(match[7].rm_eo - match[7].rm_so, MBOX_NAME_LEN - 1)); |
|
|
} else if (!strncasecmp(line + match[1].rm_so, "flag", 4)) { |
|
|
if (!strncasecmp(line + match[8].rm_so, "replace", 7)) |
|
|
cur_fltr->action.type = FILTER_ACTION_FLAG_REPLACE; |
|
|
else if (!strncasecmp(line + match[8].rm_so, "add", 3)) |
|
|
cur_fltr->action.type = FILTER_ACTION_FLAG_ADD; |
|
|
else |
|
|
cur_fltr->action.type = FILTER_ACTION_FLAG_REMOVE; |
|
|
|
|
|
c = cp = (char *)malloc(match[9].rm_eo - match[9].rm_so + 1); |
|
|
xstrncpy(c, line + match[9].rm_so, match[9].rm_eo - match[9].rm_so); |
|
409 |
|
|
410 |
t = strtok_r(c, ",", &c); |
if (cur_fltr == NULL) |
|
while (t) { |
|
|
if (!strcasecmp(t, "none")) { |
|
|
if (match[9].rm_eo - match[9].rm_so != strlen("none")) |
|
|
return ERROR_CONFIG_PARSE; |
|
|
} else if (!strcasecmp(t, "seen")) |
|
|
cur_fltr->action.msgflags |= MESSAGE_FLAG_SEEN; |
|
|
else if (!strcasecmp(t, "answered")) |
|
|
cur_fltr->action.msgflags |= MESSAGE_FLAG_ANSWERED; |
|
|
else if (!strcasecmp(t, "flagged")) |
|
|
cur_fltr->action.msgflags |= MESSAGE_FLAG_FLAGGED; |
|
|
else if (!strcasecmp(t, "deleted")) |
|
|
cur_fltr->action.msgflags |= MESSAGE_FLAG_DELETED; |
|
|
else if (!strcasecmp(t, "draft")) |
|
|
cur_fltr->action.msgflags |= MESSAGE_FLAG_DRAFT; |
|
|
else |
|
411 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
412 |
|
|
413 |
t = strtok_r(NULL, ",", &c); |
if (!strncasecmp(line + m[1].rm_so, "delete", 6)) |
414 |
|
cur_fltr->action.type = FILTER_ACTION_DELETE; |
415 |
|
else if (!strncasecmp(line + m[1].rm_so, "copy", 4)) { |
416 |
|
cur_fltr->action.type = FILTER_ACTION_COPY; |
417 |
|
if (*(line + m[2].rm_so) == '"' && |
418 |
|
*(line + m[2].rm_eo - 1) == '"') |
419 |
|
strncat(cur_fltr->action.destmbox, |
420 |
|
line + m[2].rm_so + 1, |
421 |
|
min(m[2].rm_eo - m[2].rm_so - 2, |
422 |
|
MBOX_NAME_LEN - 1)); |
423 |
|
else |
424 |
|
strncat(cur_fltr->action.destmbox, line + m[2].rm_so, |
425 |
|
min(m[2].rm_eo - m[2].rm_so, MBOX_NAME_LEN - 1)); |
426 |
|
} else if (!strncasecmp(line + m[1].rm_so, "move", 4)) { |
427 |
|
cur_fltr->action.type = FILTER_ACTION_MOVE; |
428 |
|
if (*(line + m[3].rm_so) == '"' && |
429 |
|
*(line + m[3].rm_eo - 1) == '"') |
430 |
|
strncat(cur_fltr->action.destmbox, |
431 |
|
line + m[3].rm_so + 1, |
432 |
|
min(m[3].rm_eo - m[3].rm_so - 2, |
433 |
|
MBOX_NAME_LEN - 1)); |
434 |
|
else |
435 |
|
strncat(cur_fltr->action.destmbox, line + m[3].rm_so, |
436 |
|
min(m[3].rm_eo - m[3].rm_so, MBOX_NAME_LEN - 1)); |
437 |
|
} else if (!strncasecmp(line + m[1].rm_so, "rcopy", 5)) { |
438 |
|
cur_fltr->action.type = FILTER_ACTION_RCOPY; |
439 |
|
for (a = accounts; a; a = a->next) |
440 |
|
if (!strncasecmp(line + m[4].rm_so, a->key, |
441 |
|
strlen(a->key))) |
442 |
|
cur_fltr->action.raccount = a; |
443 |
|
if (!cur_fltr->action.raccount) |
444 |
|
return ERROR_CONFIG_PARSE; |
445 |
|
|
446 |
|
if (*(line + m[5].rm_so) == '"' && |
447 |
|
*(line + m[5].rm_eo - 1) == '"') |
448 |
|
strncat(cur_fltr->action.destmbox, |
449 |
|
line + m[5].rm_so + 1, |
450 |
|
min(m[5].rm_eo - m[5].rm_so - 2, |
451 |
|
MBOX_NAME_LEN - 1)); |
452 |
|
else |
453 |
|
strncat(cur_fltr->action.destmbox, line + m[5].rm_so, |
454 |
|
min(m[5].rm_eo - m[5].rm_so, MBOX_NAME_LEN - 1)); |
455 |
|
} else if (!strncasecmp(line + m[1].rm_so, "rmove", 5)) { |
456 |
|
cur_fltr->action.type = FILTER_ACTION_RMOVE; |
457 |
|
for (a = accounts; a; a = a->next) |
458 |
|
if (!strncasecmp(line + m[6].rm_so, a->key, |
459 |
|
strlen(a->key))) |
460 |
|
cur_fltr->action.raccount = a; |
461 |
|
if (!cur_fltr->action.raccount) |
462 |
|
return ERROR_CONFIG_PARSE; |
463 |
|
|
464 |
|
if (*(line + m[7].rm_so) == '"' && |
465 |
|
*(line + m[7].rm_eo - 1) == '"') |
466 |
|
strncat(cur_fltr->action.destmbox, |
467 |
|
line + m[7].rm_so + 1, |
468 |
|
min(m[7].rm_eo - m[7].rm_so - 2, |
469 |
|
MBOX_NAME_LEN - 1)); |
470 |
|
else |
471 |
|
strncat(cur_fltr->action.destmbox, line + m[7].rm_so, |
472 |
|
min(m[7].rm_eo - m[7].rm_so, MBOX_NAME_LEN - 1)); |
473 |
|
} else if (!strncasecmp(line + m[1].rm_so, "flag", 4)) { |
474 |
|
if (!strncasecmp(line + m[8].rm_so, "replace", 7)) |
475 |
|
cur_fltr->action.type = FILTER_ACTION_FLAG_REPLACE; |
476 |
|
else if (!strncasecmp(line + m[8].rm_so, "add", 3)) |
477 |
|
cur_fltr->action.type = FILTER_ACTION_FLAG_ADD; |
478 |
|
else |
479 |
|
cur_fltr->action.type = FILTER_ACTION_FLAG_REMOVE; |
480 |
|
|
481 |
|
c = cp = (char *)malloc(m[9].rm_eo - m[9].rm_so + 1); |
482 |
|
xstrncpy(c, line + m[9].rm_so, m[9].rm_eo - m[9].rm_so); |
483 |
|
|
484 |
|
t = strtok_r(c, ",", &c); |
485 |
|
while (t) { |
486 |
|
if (!strcasecmp(t, "none")) { |
487 |
|
if (m[9].rm_eo - m[9].rm_so != strlen("none")) |
488 |
|
return ERROR_CONFIG_PARSE; |
489 |
|
} else if (!strcasecmp(t, "seen")) |
490 |
|
cur_fltr->action.msgflags |= MESSAGE_FLAG_SEEN; |
491 |
|
else if (!strcasecmp(t, "answered")) |
492 |
|
cur_fltr->action.msgflags |= |
493 |
|
MESSAGE_FLAG_ANSWERED; |
494 |
|
else if (!strcasecmp(t, "flagged")) |
495 |
|
cur_fltr->action.msgflags |= |
496 |
|
MESSAGE_FLAG_FLAGGED; |
497 |
|
else if (!strcasecmp(t, "deleted")) |
498 |
|
cur_fltr->action.msgflags |= |
499 |
|
MESSAGE_FLAG_DELETED; |
500 |
|
else if (!strcasecmp(t, "draft")) |
501 |
|
cur_fltr->action.msgflags |= MESSAGE_FLAG_DRAFT; |
502 |
|
else |
503 |
|
return ERROR_CONFIG_PARSE; |
504 |
|
|
505 |
|
t = strtok_r(NULL, ",", &c); |
506 |
|
} |
507 |
|
xfree(cp); |
508 |
|
} else if (!strncasecmp(line + m[1].rm_so, "list", 4)) { |
509 |
|
cur_fltr->action.type = FILTER_ACTION_LIST; |
510 |
} |
} |
511 |
xfree(cp); |
if (m[10].rm_so != -1) { |
512 |
} else if (!strncasecmp(line + match[1].rm_so, "list", 4)) { |
strncat(cur_fltr->action.args, line + m[10].rm_so, |
513 |
cur_fltr->action.type = FILTER_ACTION_LIST; |
min(m[10].rm_eo - m[10].rm_so, ARGS_LEN - 2)); |
514 |
} |
while ((c = strchr(cur_fltr->action.args, ','))) |
515 |
if (match[10].rm_so != -1) { |
*c = ' '; |
516 |
strncat(cur_fltr->action.args, line + match[10].rm_so, |
} |
517 |
min(match[10].rm_eo - match[10].rm_so, ARGS_LEN - 2)); |
#ifdef DEBUG |
518 |
while ((c = strchr(cur_fltr->action.args, ','))) |
fprintf(stderr, "debug: ACTION: %d '%s' '%s' %d '%s'\n", |
519 |
*c = ' '; |
cur_fltr->action.type, |
520 |
} |
(cur_fltr->action.raccount ? cur_fltr->action.raccount->key : ""), |
521 |
#ifdef DEBUG |
cur_fltr->action.destmbox, |
522 |
printf("debug: ACTION: %d '%s' '%s' %d '%s'\n", cur_fltr->action.type, |
cur_fltr->action.msgflags, |
523 |
(cur_fltr->action.raccount ? cur_fltr->action.raccount->key : ""), |
cur_fltr->action.args); |
|
cur_fltr->action.destmbox, cur_fltr->action.msgflags, |
|
|
cur_fltr->action.args); |
|
524 |
#endif |
#endif |
525 |
return 0; |
return 0; |
526 |
} |
} |
527 |
|
|
528 |
|
|
529 |
/* |
/* |
530 |
* Set new mask's variables to safe values. |
* Set new mask's variables to safe values. |
531 |
*/ |
*/ |
532 |
void init_mask(mask_t * node) |
void |
533 |
|
init_mask(mask_t * node) |
534 |
{ |
{ |
535 |
node->next = NULL; |
node->next = NULL; |
536 |
node->body[0] = 0; |
node->body[0] = '\0'; |
537 |
node->type = 0; |
node->type = 0; |
538 |
} |
} |
539 |
|
|
540 |
|
|
541 |
/* |
/* |
542 |
* Append mask node to linked list. |
* Append mask node to linked list. |
543 |
*/ |
*/ |
544 |
void append_mask(mask_t * node) |
void |
545 |
|
append_mask(mask_t * node) |
546 |
{ |
{ |
547 |
mask_t *pos; |
mask_t *pos; |
548 |
mask_t **app; |
mask_t **app; |
549 |
|
|
550 |
APPEND_LINKED_LIST(cur_fltr->masks, node, pos, app); |
APPEND_LINKED_LIST(cur_fltr->masks, node, pos, app); |
551 |
} |
} |
552 |
|
|
553 |
|
|
555 |
* A new mask entry was declared, create it and set it's |
* A new mask entry was declared, create it and set it's |
556 |
* variables accordingly. |
* variables accordingly. |
557 |
*/ |
*/ |
558 |
int set_mask(char *line, regmatch_t * match, int mmt) |
int |
559 |
|
set_mask(char *line, regmatch_t * m, int mmt) |
560 |
{ |
{ |
561 |
int n; |
int n; |
562 |
mask_t *node; |
mask_t *node; |
563 |
char *bp; |
char *bp; |
564 |
|
|
565 |
if (!cur_fltr) |
if (cur_fltr == NULL) |
566 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
567 |
|
|
568 |
node = (mask_t *) create_node(sizeof(mask_t)); |
node = (mask_t *) create_node(sizeof(mask_t)); |
569 |
|
|
570 |
init_mask(node); |
init_mask(node); |
571 |
|
|
572 |
bp = node->body; |
bp = node->body; |
573 |
|
|
574 |
/* If specified set mask's type. */ |
/* If specified set mask's type. */ |
575 |
if (match[2].rm_so != -1 && cur_fltr->masks) { |
if (m[2].rm_so != -1 && cur_fltr->masks) { |
576 |
if (!strncasecmp(line + match[2].rm_so, "or", 2)) { |
if (!strncasecmp(line + m[2].rm_so, "or", 2)) { |
577 |
node->type = MASK_TYPE_OR; |
node->type = MASK_TYPE_OR; |
578 |
} else |
} else |
579 |
node->type = MASK_TYPE_AND; |
node->type = MASK_TYPE_AND; |
580 |
} |
} |
581 |
/* Add NOT if specified. */ |
/* Add NOT if specified. */ |
582 |
if (match[3].rm_so != -1) { |
if (m[3].rm_so != -1) { |
583 |
n = min(match[3].rm_eo - match[3].rm_so, |
n = min(m[3].rm_eo - m[3].rm_so, |
584 |
MASK_BODY_LEN - (bp - node->body) - 1); |
MASK_BODY_LEN - (bp - node->body) - 1); |
585 |
xstrncpy(bp, line + match[3].rm_so, n); |
xstrncpy(bp, line + m[3].rm_so, n); |
586 |
string_upper(bp, n); |
string_upper(bp, n); |
587 |
*(bp + n - 1) = ' '; /* In case it's '\t'. */ |
*(bp + n - 1) = ' '; /* In case it's '\t'. */ |
588 |
*(bp + n) = 0; |
*(bp + n) = '\0'; |
589 |
bp += n; |
bp += n; |
590 |
} |
} |
591 |
/* Keyword of the search key. */ |
/* Keyword of the search key. */ |
592 |
n = min(match[4].rm_eo - match[4].rm_so, |
n = min(m[4].rm_eo - m[4].rm_so, |
593 |
MASK_BODY_LEN - (bp - node->body) - 3); |
MASK_BODY_LEN - (bp - node->body) - 3); |
594 |
xstrncpy(bp, line + match[4].rm_so, n); |
xstrncpy(bp, line + m[4].rm_so, n); |
595 |
string_upper(bp, n); |
string_upper(bp, n); |
596 |
*(bp + n) = 0; |
*(bp + n) = '\0'; |
|
bp += n; |
|
|
|
|
|
if (mmt != MASK_MATCH_1) { |
|
|
*(bp++) = ' '; |
|
|
|
|
|
if (mmt != MASK_MATCH_4 && *(line + match[5].rm_so) != '"') |
|
|
*(bp++) = '"'; |
|
|
*bp = 0; |
|
|
|
|
|
n = min(match[5].rm_eo - match[5].rm_so, |
|
|
MASK_BODY_LEN - (bp - node->body) - 2); |
|
|
xstrncpy(bp, line + match[5].rm_so, n); |
|
|
*(bp + n) = 0; |
|
597 |
bp += n; |
bp += n; |
598 |
|
|
599 |
if (mmt != MASK_MATCH_4 && *(line + match[5].rm_so) != '"') |
if (mmt != MASK_MATCH_1) { |
600 |
*(bp++) = '"'; |
*(bp++) = ' '; |
|
*bp = 0; |
|
|
|
|
|
if (mmt == MASK_MATCH_3) { |
|
|
*(bp++) = ' '; |
|
|
|
|
|
if (*(line + match[6].rm_so) != '"') |
|
|
*(bp++) = '"'; |
|
|
*bp = 0; |
|
601 |
|
|
602 |
n = min(match[6].rm_eo - match[6].rm_so, |
if (mmt != MASK_MATCH_4 && *(line + m[5].rm_so) != '"') |
603 |
MASK_BODY_LEN - (bp - node->body) - 2); |
*(bp++) = '"'; |
604 |
xstrncpy(bp, line + match[6].rm_so, n); |
*bp = '\0'; |
|
*(bp + n) = 0; |
|
|
bp += n; |
|
605 |
|
|
606 |
if (*(line + match[6].rm_so) != '"') |
n = min(m[5].rm_eo - m[5].rm_so, |
607 |
*(bp++) = '"'; |
MASK_BODY_LEN - (bp - node->body) - 2); |
608 |
*bp = 0; |
xstrncpy(bp, line + m[5].rm_so, n); |
609 |
} |
*(bp + n) = '\0'; |
610 |
if (mmt == MASK_MATCH_4 && (strstr(node->body, "OLDER") || |
bp += n; |
611 |
strstr(node->body, "NEWER"))) { |
|
612 |
convert_date(node); |
if (mmt != MASK_MATCH_4 && *(line + m[5].rm_so) != '"') |
613 |
bp = node->body + strlen(node->body); |
*(bp++) = '"'; |
614 |
|
*bp = '\0'; |
615 |
|
|
616 |
|
if (mmt == MASK_MATCH_3) { |
617 |
|
*(bp++) = ' '; |
618 |
|
|
619 |
|
if (*(line + m[6].rm_so) != '"') |
620 |
|
*(bp++) = '"'; |
621 |
|
*bp = '\0'; |
622 |
|
|
623 |
|
n = min(m[6].rm_eo - m[6].rm_so, |
624 |
|
MASK_BODY_LEN - (bp - node->body) - 2); |
625 |
|
xstrncpy(bp, line + m[6].rm_so, n); |
626 |
|
*(bp + n) = '\0'; |
627 |
|
bp += n; |
628 |
|
|
629 |
|
if (*(line + m[6].rm_so) != '"') |
630 |
|
*(bp++) = '"'; |
631 |
|
*bp = '\0'; |
632 |
|
} |
633 |
|
if (mmt == MASK_MATCH_4 && (strstr(node->body, "OLDER") || |
634 |
|
strstr(node->body, "NEWER"))) { |
635 |
|
convert_date(node); |
636 |
|
bp = node->body + strlen(node->body); |
637 |
|
} |
638 |
} |
} |
639 |
} |
append_mask(node); |
|
append_mask(node); |
|
640 |
|
|
641 |
cur_fltr->masknum++; |
cur_fltr->masknum++; |
642 |
cur_fltr->masklen += (bp - node->body); |
cur_fltr->masklen += (bp - node->body); |
643 |
|
|
644 |
|
|
645 |
#ifdef DEBUG |
#ifdef DEBUG |
646 |
printf("debug: MASK: '%s'\n", node->body); |
fprintf(stderr, "debug: MASK: '%s'\n", node->body); |
647 |
#endif |
#endif |
648 |
|
|
649 |
return 0; |
return 0; |
650 |
} |
} |
651 |
|
|
652 |
|
|
655 |
* understand for example "OLDER 3", but "BEFORE 18-Oct-2001" (if |
* understand for example "OLDER 3", but "BEFORE 18-Oct-2001" (if |
656 |
* hypothetically current date was 21-Oct-2001). |
* hypothetically current date was 21-Oct-2001). |
657 |
*/ |
*/ |
658 |
void convert_date(mask_t * node) |
void |
659 |
|
convert_date(mask_t * node) |
660 |
{ |
{ |
661 |
char *cp, *c; |
char *cp, *c; |
662 |
char s[16]; |
char s[16]; |
663 |
time_t t; |
time_t te; |
664 |
struct tm *bt; |
struct tm *tl; |
665 |
|
|
666 |
cp = xstrdup(node->body); |
cp = xstrdup(node->body); |
667 |
node->body[0] = 0; |
node->body[0] = '\0'; |
668 |
|
|
669 |
if (strstr(cp, "NOT")) |
if (strstr(cp, "NOT")) |
670 |
strncat(node->body, "NOT ", 4); |
strncat(node->body, "NOT ", 4); |
671 |
|
|
672 |
if ((c = strstr(cp, "OLDER"))) |
if ((c = strstr(cp, "OLDER"))) |
673 |
strncat(node->body, "BEFORE ", 7); |
strncat(node->body, "BEFORE ", 7); |
674 |
else if ((c = strstr(cp, "NEWER"))) |
else if ((c = strstr(cp, "NEWER"))) |
675 |
strncat(node->body, "SINCE ", 6); |
strncat(node->body, "SINCE ", 6); |
676 |
|
|
677 |
c += 6; |
c += 6; |
678 |
|
|
679 |
t = time(NULL) - (time_t) (strtoul(c, NULL, 10) * 24 * 60 * 60); |
te = time(NULL) - (time_t) (strtoul(c, NULL, 10) * 24 * 60 * 60); |
680 |
bt = localtime(&t); |
tl = localtime(&te); |
681 |
|
|
682 |
if (strftime(s, 15, "%d-%b-%Y", bt)) |
if (strftime(s, 15, "%d-%b-%Y", tl)) |
683 |
strncat(node->body, s, 15); |
strncat(node->body, s, 15); |
684 |
|
|
685 |
xfree(cp); |
xfree(cp); |
686 |
} |
} |
687 |
|
|
688 |
|
|
689 |
/* |
/* |
690 |
* A new job was declared, link filters with mailbox-groups. |
* A new job was declared, link filters with mailbox-groups. |
691 |
*/ |
*/ |
692 |
int set_job(char *line, regmatch_t * match) |
int |
693 |
|
set_job(char *line, regmatch_t * match) |
694 |
{ |
{ |
695 |
int n; |
int n; |
696 |
char *ftok, *gtok, *fltr, *mbgrp, *f, *g; |
char *ftok, *gtok, *fltr, *mbgrp, *f, *g; |
697 |
filter_t *cf; |
filter_t *cf; |
698 |
mboxgrp_t *cg; |
mboxgrp_t *cg; |
699 |
|
|
700 |
if (!accounts || !filters || !cur_fltr->action.type) |
if (accounts == NULL || filters == NULL || cur_fltr->action.type == 0) |
|
return ERROR_CONFIG_PARSE; |
|
|
|
|
|
n = match[1].rm_eo - match[1].rm_so; |
|
|
fltr = (char *)xmalloc(n + 1); |
|
|
|
|
|
f = xstrncpy(fltr, line + match[1].rm_so, n); |
|
|
f[n] = 0; |
|
|
|
|
|
n = match[2].rm_eo - match[2].rm_so; |
|
|
mbgrp = (char *)xmalloc(n + 1); |
|
|
|
|
|
/* Go through filters. */ |
|
|
ftok = strtok_r(f, ",", &f); |
|
|
while (ftok) { |
|
|
cf = (filter_t *) find_filter(ftok); |
|
|
if (!cf) |
|
|
return ERROR_CONFIG_PARSE; |
|
|
|
|
|
g = xstrncpy(mbgrp, line + match[2].rm_so, n); |
|
|
g[n] = 0; |
|
|
|
|
|
/* Go through mailbox groups. */ |
|
|
gtok = strtok_r(g, ",", &g); |
|
|
while (gtok) { |
|
|
cg = (mboxgrp_t *) find_mboxgrp(gtok); |
|
|
if (!cg) |
|
701 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
|
link_mbox_filter(cf, cg); |
|
702 |
|
|
703 |
gtok = strtok_r(NULL, ",", &g); |
n = match[1].rm_eo - match[1].rm_so; |
704 |
} |
fltr = (char *)xmalloc(n + 1); |
705 |
|
|
706 |
|
f = xstrncpy(fltr, line + match[1].rm_so, n); |
707 |
|
f[n] = '\0'; |
708 |
|
|
709 |
ftok = strtok_r(NULL, ",", &f); |
n = match[2].rm_eo - match[2].rm_so; |
710 |
} |
mbgrp = (char *)xmalloc(n + 1); |
711 |
|
|
712 |
xfree(fltr); |
/* Go through filters. */ |
713 |
xfree(mbgrp); |
ftok = strtok_r(f, ",", &f); |
714 |
|
while (ftok != NULL) { |
715 |
|
cf = (filter_t *) find_filter(ftok); |
716 |
|
if (cf == NULL) |
717 |
|
return ERROR_CONFIG_PARSE; |
718 |
|
|
719 |
|
g = xstrncpy(mbgrp, line + match[2].rm_so, n); |
720 |
|
g[n] = '\0'; |
721 |
|
|
722 |
|
/* Go through mailbox groups. */ |
723 |
|
gtok = strtok_r(g, ",", &g); |
724 |
|
while (gtok != NULL) { |
725 |
|
cg = (mboxgrp_t *) find_mboxgrp(gtok); |
726 |
|
if (cg == NULL) |
727 |
|
return ERROR_CONFIG_PARSE; |
728 |
|
link_mbox_filter(cf, cg); |
729 |
|
|
730 |
|
gtok = strtok_r(NULL, ",", &g); |
731 |
|
} |
732 |
|
|
733 |
|
ftok = strtok_r(NULL, ",", &f); |
734 |
|
} |
735 |
|
|
736 |
return 0; |
xfree(fltr); |
737 |
|
xfree(mbgrp); |
738 |
|
|
739 |
|
return 0; |
740 |
} |
} |
741 |
|
|
742 |
|
|
743 |
/* |
/* |
744 |
* Link a filter with a mailbox. |
* Link a filter with a mailbox. |
745 |
*/ |
*/ |
746 |
void link_mbox_filter(filter_t * cf, mboxgrp_t * cg) |
void |
747 |
|
link_mbox_filter(filter_t * cf, mboxgrp_t * cg) |
748 |
{ |
{ |
749 |
int i, j, f; |
int i, j, f; |
750 |
|
|
751 |
for (i = 0; cg->mboxes[i]; i++) { |
for (i = 0; cg->mboxes[i] != NULL; i++) { |
752 |
for (f = j = 0; cg->mboxes[i]->filters[j]; j++) |
for (f = j = 0; cg->mboxes[i]->filters[j] != NULL; j++) |
753 |
if (j == MBOX_FILTERS_MAX - 1 || |
if (j == MBOX_FILTERS_MAX - 1 || |
754 |
!strcmp(cf->key, cg->mboxes[i]->filters[j]->key)) |
!strcmp(cf->key, cg->mboxes[i]->filters[j]->key)) |
755 |
f = 1; |
f = 1; |
756 |
|
|
757 |
if (f) |
if (f) |
758 |
continue; |
continue; |
759 |
|
|
760 |
cg->mboxes[i]->filters[j] = cf; |
cg->mboxes[i]->filters[j] = cf; |
761 |
cg->mboxes[i]->filters[j + 1] = NULL; |
cg->mboxes[i]->filters[j + 1] = NULL; |
762 |
|
|
763 |
} |
} |
764 |
|
|
765 |
#ifdef DEBUG |
#ifdef DEBUG |
766 |
printf("debug: JOB: '%s' '%s'\n", cf->key, cg->key); |
fprintf(stderr, "debug: JOB: '%s' '%s'\n", cf->key, cg->key); |
767 |
#endif |
#endif |
768 |
} |
} |
769 |
|
|
770 |
|
|
771 |
/* |
/* |
772 |
|
* Free allocated memory of all data structures. |
773 |
|
*/ |
774 |
|
void |
775 |
|
destroy_all(void) |
776 |
|
{ |
777 |
|
destroy_accounts(accounts); |
778 |
|
accounts = NULL; |
779 |
|
destroy_filters(filters); |
780 |
|
filters = NULL; |
781 |
|
destroy_mboxgrps(mboxgrps); |
782 |
|
mboxgrps = NULL; |
783 |
|
} |
784 |
|
|
785 |
|
|
786 |
|
/* |
787 |
* Free allocated memory of data structures that are not needed anymore. |
* Free allocated memory of data structures that are not needed anymore. |
788 |
*/ |
*/ |
789 |
void destroy_data(void) |
void |
790 |
|
destroy_unneeded(void) |
791 |
{ |
{ |
792 |
destroy_mboxgrp(mboxgrps); |
destroy_mboxgrps(mboxgrps); |
793 |
|
mboxgrps = NULL; |
794 |
} |
} |
795 |
|
|
796 |
|
|
798 |
* Go through the mailbox-group tree, and free the allocated memory of |
* Go through the mailbox-group tree, and free the allocated memory of |
799 |
* each node. |
* each node. |
800 |
*/ |
*/ |
801 |
void destroy_mboxgrp(mboxgrp_t * node) |
void |
802 |
|
destroy_mboxgrps(mboxgrp_t * node) |
803 |
{ |
{ |
804 |
if (!node) |
if (node == NULL) |
805 |
return; |
return; |
806 |
|
|
807 |
if (node->left) { |
if (node->left != NULL) { |
808 |
destroy_mboxgrp(node->left); |
destroy_mboxgrps(node->left); |
809 |
node->left = NULL; |
node->left = NULL; |
810 |
} |
} |
811 |
if (node->right) { |
if (node->right != NULL) { |
812 |
destroy_mboxgrp(node->right); |
destroy_mboxgrps(node->right); |
813 |
node->right = NULL; |
node->right = NULL; |
814 |
} |
} |
815 |
#ifdef DEBUG |
#ifdef DEBUG |
816 |
printf("debug: deleting FOLDER: '%s'\n", node->key); |
fprintf(stderr, "debug: deleting FOLDER: '%s'\n", node->key); |
817 |
#endif |
#endif |
818 |
|
|
819 |
xfree(node); |
xfree(node); |
820 |
|
} |
821 |
|
|
822 |
|
|
823 |
|
/* |
824 |
|
* Go through the mailbox linked list of the account and free the allocated |
825 |
|
* memory of each node. |
826 |
|
*/ |
827 |
|
void |
828 |
|
destroy_mboxs(mbox_t * node) |
829 |
|
{ |
830 |
|
mbox_t *p, *t; |
831 |
|
|
832 |
|
for (p = node; p != NULL; p = t) { |
833 |
|
t = p->next; |
834 |
|
#ifdef DEBUG |
835 |
|
fprintf(stderr, "debug: deleting MBOX: '%s'\n", p->name); |
836 |
|
#endif |
837 |
|
xfree(p); |
838 |
|
} |
839 |
|
} |
840 |
|
|
841 |
|
|
842 |
|
/* |
843 |
|
* Go through the accounts' linked list and free the allocated memory of |
844 |
|
* each node. |
845 |
|
*/ |
846 |
|
void |
847 |
|
destroy_accounts(account_t * node) |
848 |
|
{ |
849 |
|
account_t *p, *t; |
850 |
|
|
851 |
|
for (p = node; p != NULL; p = t) { |
852 |
|
t = p->next; |
853 |
|
#ifdef DEBUG |
854 |
|
fprintf(stderr, "debug: deleting ACCOUNT: '%s'\n", p->key); |
855 |
|
#endif |
856 |
|
destroy_mboxs(p->mboxes); |
857 |
|
sfree(p->password); |
858 |
|
xfree(p); |
859 |
|
} |
860 |
|
} |
861 |
|
|
862 |
|
|
863 |
|
/* |
864 |
|
* Go through the filters' tree and free the allocated memory of each node. |
865 |
|
*/ |
866 |
|
void |
867 |
|
destroy_filters(filter_t * node) |
868 |
|
{ |
869 |
|
if (node == NULL) |
870 |
|
return; |
871 |
|
|
872 |
|
if (node->left != NULL) { |
873 |
|
destroy_filters(node->left); |
874 |
|
node->left = NULL; |
875 |
|
} |
876 |
|
if (node->right != NULL) { |
877 |
|
destroy_filters(node->right); |
878 |
|
node->right = NULL; |
879 |
|
} |
880 |
|
#ifdef DEBUG |
881 |
|
fprintf(stderr, "debug: deleting FILTER: '%s'\n", node->key); |
882 |
|
#endif |
883 |
|
destroy_masks(node->masks); |
884 |
|
xfree(node); |
885 |
|
} |
886 |
|
|
887 |
|
|
888 |
|
/* |
889 |
|
* Go through the masks' linked list and free the allocated memory of each |
890 |
|
* node. |
891 |
|
*/ |
892 |
|
void |
893 |
|
destroy_masks(mask_t * node) |
894 |
|
{ |
895 |
|
mask_t *p, *t; |
896 |
|
|
897 |
|
for (p = node; p != NULL; p = t) { |
898 |
|
t = p->next; |
899 |
|
#ifdef DEBUG |
900 |
|
fprintf(stderr, "debug: deleting MASK: '%s'\n", p->body); |
901 |
|
#endif |
902 |
|
xfree(p); |
903 |
|
} |
904 |
} |
} |
905 |
|
|
906 |
|
|
907 |
/* |
/* |
908 |
* Convert a string of specified size to upper case. |
* Convert a string of specified size to upper case. |
909 |
*/ |
*/ |
910 |
void string_upper(char *str, size_t size) |
void |
911 |
|
string_upper(char *str, size_t size) |
912 |
{ |
{ |
913 |
unsigned int i; |
unsigned int i; |
914 |
|
|
915 |
for (i = 0; i < size; i++, str++) |
for (i = 0; i < size; i++, str++) |
916 |
*str = toupper(*str); |
*str = toupper(*str); |
917 |
} |
} |
918 |
|
|
919 |
|
|
922 |
* followed by two hexadecimal digits to its corresponding ASCII |
* followed by two hexadecimal digits to its corresponding ASCII |
923 |
* character (described in Section 2.2 of RFC 1738). |
* character (described in Section 2.2 of RFC 1738). |
924 |
*/ |
*/ |
925 |
int string_decode(char *str) |
int |
926 |
|
string_decode(char *str) |
927 |
{ |
{ |
928 |
char *c; |
char *c; |
929 |
char hex[3]; |
char hex[3]; |
930 |
|
|
931 |
c = str; |
c = str; |
932 |
|
|
933 |
while (*c) { |
while (*c != '\0') { |
934 |
if (*c == '%') { |
if (*c == '%') { |
935 |
if (!isxdigit((unsigned char)(*(c + 1))) || |
if (!isxdigit((unsigned char)(*(c + 1))) || |
936 |
!isxdigit((unsigned char)(*(c + 2)))) |
!isxdigit((unsigned char)(*(c + 2)))) |
937 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
938 |
|
|
939 |
xstrncpy(hex, ++c, 2); |
xstrncpy(hex, ++c, 2); |
940 |
hex[2] = 0; |
hex[2] = '\0'; |
941 |
|
|
942 |
if (!isprint((unsigned char)(*str = (char)strtoul(hex, NULL, 16)))) |
if (!isprint((unsigned char)(*str = (char)strtoul(hex, |
943 |
return ERROR_CONFIG_PARSE; |
NULL, 16)))) |
944 |
|
return ERROR_CONFIG_PARSE; |
945 |
str++; |
|
946 |
c += 2; |
str++; |
947 |
} else |
c += 2; |
948 |
*(str++) = *(c++); |
} else |
949 |
} |
*(str++) = *(c++); |
950 |
*str = 0; |
} |
951 |
|
*str = '\0'; |
952 |
|
|
953 |
return 0; |
return 0; |
954 |
} |
} |
955 |
|
|
956 |
|
|
958 |
* Convert the names of personal mailboxes using the namespace specified |
* Convert the names of personal mailboxes using the namespace specified |
959 |
* by the mail server. |
* by the mail server. |
960 |
*/ |
*/ |
961 |
char *apply_namespace(char *mbox, char *prefix, char delim) |
char * |
962 |
|
apply_namespace(char *mbox, char *prefix, char delim) |
963 |
{ |
{ |
964 |
static char m[MBOX_NAME_LEN]; |
static char m[MBOX_NAME_LEN]; |
965 |
char *c; |
char *c; |
966 |
|
|
967 |
if ((!prefix[0] && !delim) || (!prefix[0] && delim == '/') |
if ((prefix[0] == '\0' && delim == '\0') || |
968 |
|| !strcasecmp(mbox, "INBOX")) |
(prefix[0] == '\0' && delim == '/') || |
969 |
return mbox; |
!strcasecmp(mbox, "INBOX")) |
970 |
|
return mbox; |
971 |
|
|
972 |
m[0] = 0; |
m[0] = '\0'; |
973 |
strncat(m, prefix, MBOX_NAME_LEN - 1); |
strncat(m, prefix, MBOX_NAME_LEN - 1); |
974 |
strncat(m, mbox, MBOX_NAME_LEN - strlen(m) - 1); |
strncat(m, mbox, MBOX_NAME_LEN - strlen(m) - 1); |
975 |
|
|
976 |
c = m; |
c = m; |
977 |
while ((c = strchr(c, '/'))) |
while ((c = strchr(c, '/'))) |
978 |
*(c++) = delim; |
*(c++) = delim; |
979 |
|
|
980 |
#ifdef DEBUG |
#ifdef DEBUG |
981 |
printf("debug: MAILBOX: '%s'\n", m); |
fprintf(stderr, "debug: MAILBOX: '%s'\n", m); |
982 |
#endif |
#endif |
983 |
|
|
984 |
return m; |
return m; |
985 |
} |
} |