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

Annotation of /imapfilter/data.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations)
Wed Oct 3 17:42:43 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.7: +44 -5 lines
File MIME type: text/plain
Added URL like encoding of the username and password.

1 lefcha 1.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 lefcha 1.2 #include <ctype.h>
10 lefcha 1.1
11     #include "config.h"
12     #include "imapfilter.h"
13     #include "data.h"
14    
15    
16     account_t *accounts = NULL; /* First node of accounts linked list. */
17     filter_t *filters = NULL; /* First node of filters tree. */
18    
19 lefcha 1.5 static mboxgrp_t *mboxgrps = NULL; /* First node of mailbox-groups
20     tree. */
21 lefcha 1.1
22     static account_t *cur_acct = NULL; /* Current account. */
23     static filter_t *cur_fltr = NULL; /* Current filter. */
24    
25    
26     /*
27     * Set new account's variables to safe values.
28     */
29     void init_account(account_t * node)
30     {
31     node->next = NULL;
32     node->server[0] = node->username[0] = node->password[0] = 0;
33     node->port = 143;
34     node->mboxes = NULL;
35     }
36    
37    
38     /*
39     * Append account node to linked list.
40     */
41     void append_account(account_t * node)
42     {
43     account_t *pos;
44     account_t **app;
45    
46     APPEND_LINKED_LIST(accounts, node, pos, app);
47     }
48    
49    
50     /*
51     * A new account entry was declared. Create it and set it's variables
52     * accordingly.
53     */
54     int set_account(char *line, regmatch_t * match)
55     {
56     int s;
57     char p[6];
58     account_t *node;
59 lefcha 1.5
60 lefcha 1.1 node = (account_t *) create_node(sizeof(account_t));
61    
62     init_account(node);
63    
64 lefcha 1.5 strncat(node->username, line + match[1].rm_so,
65 lefcha 1.4 min(match[1].rm_eo - match[1].rm_so, USERNAME_LEN - 1));
66 lefcha 1.8 if (strchr(node->username, '%'))
67     if (string_decode(node->username))
68     return ERROR_CONFIG_PARSE;
69 lefcha 1.1 #ifdef DEBUG
70     printf("debug: USERNAME: '%s'\n", node->username);
71     #endif
72    
73 lefcha 1.5 strncat(node->password, line + match[2].rm_so,
74     min(match[2].rm_eo - match[2].rm_so, PASSWORD_LEN - 1));
75 lefcha 1.8 if (strchr(node->password, '%'))
76     if (string_decode(node->password))
77     return ERROR_CONFIG_PARSE;
78 lefcha 1.1 #ifdef DEBUG
79     printf("debug: PASSWORD: '%s'\n", node->password);
80     #endif
81    
82 lefcha 1.4 strncat(node->server, line + match[3].rm_so,
83     min(match[3].rm_eo - match[3].rm_so, SERVER_LEN - 1));
84 lefcha 1.1 #ifdef DEBUG
85     printf("debug: SERVER: '%s'\n", node->server);
86     #endif
87    
88 lefcha 1.4 if (match[4].rm_so != -1) {
89     s = min(match[4].rm_eo - match[4].rm_so - 1, 5);
90     strncpy(p, line + match[4].rm_so + 1, s);
91 lefcha 1.1 p[s] = 0;
92     node->port = strtoul(p, NULL, 0);
93     #ifdef DEBUG
94     printf("debug: PORT: %d\n", node->port);
95     #endif
96     }
97     append_account(node);
98     cur_acct = node;
99    
100     return 0;
101     }
102    
103    
104     /*
105     * Set new mailbox-group's variables to safe values.
106     */
107     void init_mboxgrp(mboxgrp_t * node)
108     {
109     node->left = node->right = NULL;
110 lefcha 1.4 node->key[0] = 0;
111 lefcha 1.1 node->mboxes[0] = NULL;
112     }
113    
114    
115     /*
116     * Insert mailbox-group node in tree.
117     */
118     void insert_mboxgrp(mboxgrp_t * node)
119     {
120     int cmp;
121     mboxgrp_t *pos;
122     mboxgrp_t **ins;
123    
124     INSERT_TREE(mboxgrps, node, pos, ins, cmp);
125     }
126    
127    
128     /*
129     * A new mailbox-group entry was declared. Create it and set it's variables
130     * accordingly.
131     */
132     int set_mboxgrp(char *line, regmatch_t * match)
133     {
134     mboxgrp_t *node;
135     char mboxs[LINE_MAX];
136    
137 lefcha 1.4 mboxs[0] = 0;
138    
139 lefcha 1.1 if (!accounts)
140 lefcha 1.4 return ERROR_CONFIG_PARSE;
141 lefcha 1.1
142     node = (mboxgrp_t *) create_node(sizeof(mboxgrp_t));
143    
144     init_mboxgrp(node);
145    
146 lefcha 1.4 strncat(node->key, line + match[1].rm_so,
147     min(match[1].rm_eo - match[1].rm_so, KEY_LEN - 1));
148 lefcha 1.1
149     #ifdef DEBUG
150     printf("debug: FOLDER: '%s'\n", node->key);
151     #endif
152    
153 lefcha 1.4 strncat(mboxs, line + match[2].rm_so,
154     min(match[2].rm_eo - match[2].rm_so, LINE_MAX - 1));
155 lefcha 1.1
156     process_mboxgrp(node, mboxs);
157    
158     insert_mboxgrp(node);
159    
160     return 0;
161     }
162    
163    
164     /*
165     * Calls set_mbox() in order to create mailboxes that are part of
166     * the mailbox-group.
167     */
168     void process_mboxgrp(mboxgrp_t * node, char *mboxs)
169     {
170     int i = 0;
171     const char *delim = ",";
172     char *tok;
173    
174 lefcha 1.4 while (i < MBOXGRP_MBOXES_MAX - 1 && (tok = strsep(&mboxs, delim))) {
175 lefcha 1.1 node->mboxes[i] = (mbox_t *) set_mbox(tok);
176     node->mboxes[++i] = NULL;
177     }
178     }
179    
180    
181     /*
182     * Find in the mailbox-group tree, the node with the specified key,
183     * and return a pointer to it.
184     */
185     mboxgrp_t *find_mboxgrp(char *key)
186     {
187     int cmp;
188     mboxgrp_t *pos;
189    
190     FIND_TREE(mboxgrps, key, pos, cmp);
191     }
192    
193    
194     /*
195     * Set new mailbox's variables to safe values.
196     */
197     void init_mbox(mbox_t * node)
198     {
199     node->next = NULL;
200 lefcha 1.4 node->name[0] = 0;
201 lefcha 1.1 node->filters[0] = NULL;
202     }
203    
204    
205     /*
206     * Append mailbox node to linked list.
207     */
208     void append_mbox(mbox_t * node)
209     {
210     mbox_t *pos;
211     mbox_t **ins;
212    
213     APPEND_LINKED_LIST(cur_acct->mboxes, node, pos, ins);
214     }
215    
216    
217     /*
218     * A new mailbox was declared, create it and set it's variables accordingly.
219     */
220     mbox_t *set_mbox(char *name)
221     {
222     mbox_t *node;
223    
224     node = (mbox_t *) create_node(sizeof(mbox_t));
225    
226     init_mbox(node);
227    
228 lefcha 1.5 strncat(node->name, name,
229 lefcha 1.4 min(strlen(name), MBOX_NAME_LEN - 1));
230 lefcha 1.1
231     #ifdef DEBUG
232     printf("debug: MBOX: '%s'\n", node->name);
233     #endif
234    
235     append_mbox(node);
236    
237     return node;
238     }
239    
240    
241     /*
242     * Set new filter's variables to safe values.
243     */
244     void init_filter(filter_t * node)
245     {
246     node->left = node->right = NULL;
247 lefcha 1.4 node->key[0] = 0;
248 lefcha 1.1 node->mode = FILTER_MODE_AND;
249     node->action.type = 0;
250     node->action.args[0] = 0;
251     node->masks = NULL;
252 lefcha 1.4 node->masknum = node->masklen = 0;
253 lefcha 1.1 }
254    
255    
256     /*
257     * Insert filter node to tree.
258     */
259     void insert_filter(filter_t * node)
260     {
261     int cmp;
262     filter_t *pos;
263     filter_t **ins;
264    
265     INSERT_TREE(filters, node, pos, ins, cmp);
266     }
267    
268    
269     /*
270     * A filter entry was declared, create it and set it's variables accordingly.
271     */
272     int set_filter(char *line, regmatch_t * match)
273     {
274     filter_t *node;
275 lefcha 1.5
276 lefcha 1.4 if (cur_fltr && !cur_fltr->action.type)
277     return ERROR_CONFIG_PARSE;
278 lefcha 1.1
279     node = (filter_t *) create_node(sizeof(filter_t));
280    
281     init_filter(node);
282    
283 lefcha 1.4 strncat(node->key, line + match[1].rm_so,
284     min(match[1].rm_eo - match[1].rm_so, KEY_LEN - 1));
285 lefcha 1.1
286 lefcha 1.4 if (match[2].rm_so != -1) {
287 lefcha 1.5 if (*(line + match[2].rm_so + 1) == 'o' ||
288     *(line + match[2].rm_so + 1) == 'O')
289 lefcha 1.1 node->mode = FILTER_MODE_OR;
290     else
291     node->mode = FILTER_MODE_AND;
292     }
293     #ifdef DEBUG
294     printf("debug: FILTER: '%s' %s\n", node->key,
295     (node->mode == FILTER_MODE_OR ? "OR" : "AND"));
296     #endif
297    
298     insert_filter(node);
299     cur_fltr = node;
300    
301     return 0;
302     }
303    
304    
305     /*
306     * Find in the filter tree, the node with the specified key and
307     * return a pointer to it.
308     */
309     filter_t *find_filter(char *key)
310     {
311     int cmp;
312     filter_t *pos;
313    
314     FIND_TREE(filters, key, pos, cmp);
315     }
316    
317    
318     /*
319 lefcha 1.5 * Assign an action to the last declared filter.
320 lefcha 1.1 */
321 lefcha 1.4 int set_action(char *line, regmatch_t * match)
322 lefcha 1.1 {
323     if (!cur_fltr)
324 lefcha 1.4 return ERROR_CONFIG_PARSE;
325 lefcha 1.7
326 lefcha 1.5 if (!strncasecmp(line + match[1].rm_so, "delete", 6))
327 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_DELETE;
328 lefcha 1.5 else if (!strncasecmp(line + match[1].rm_so, "copy", 4)) {
329 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_COPY;
330     strncat(cur_fltr->action.destmbox, line + match[2].rm_so,
331     min(match[2].rm_eo - match[2].rm_so, MBOX_NAME_LEN - 1));
332 lefcha 1.5 } else if (!strncasecmp(line + match[1].rm_so, "move", 4)) {
333 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_MOVE;
334     strncat(cur_fltr->action.destmbox, line + match[3].rm_so,
335     min(match[3].rm_eo - match[3].rm_so, MBOX_NAME_LEN - 1));
336 lefcha 1.5 } else if (!strncasecmp(line + match[1].rm_so, "list", 4))
337 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_LIST;
338 lefcha 1.7
339 lefcha 1.4 if (match[4].rm_so != -1)
340     strncat(cur_fltr->action.args, line + match[4].rm_so,
341 lefcha 1.5 min(match[4].rm_eo - match[4].rm_so, ARGS_LEN - 1));
342 lefcha 1.1
343 lefcha 1.4 return 0;
344 lefcha 1.5
345 lefcha 1.1 }
346    
347    
348     /*
349     * Set new mask's variables to safe values.
350     */
351     void init_mask(mask_t * node)
352     {
353     node->next = NULL;
354     node->body[0] = 0;
355     node->type = 0;
356     }
357    
358    
359     /*
360     * Append mask node to linked list.
361     */
362     void append_mask(mask_t * node)
363     {
364     mask_t *pos;
365     mask_t **app;
366    
367     APPEND_LINKED_LIST(cur_fltr->masks, node, pos, app);
368     }
369    
370    
371     /*
372     * A new mask entry was declared, create it and set it's
373     * variables accordingly.
374     */
375     int set_mask(char *line, regmatch_t * match)
376     {
377 lefcha 1.4 int s, i, f = 0;
378 lefcha 1.1 mask_t *node;
379 lefcha 1.4 char *bp;
380 lefcha 1.5
381 lefcha 1.4 if (!cur_fltr)
382     return ERROR_CONFIG_PARSE;
383 lefcha 1.1
384     node = (mask_t *) create_node(sizeof(mask_t));
385    
386     init_mask(node);
387 lefcha 1.5
388 lefcha 1.4 bp = node->body;
389 lefcha 1.5
390 lefcha 1.4 /* If specified set mask's type. */
391     if (match[2].rm_so != -1 && cur_fltr->masks) {
392 lefcha 1.5 if (*(line + match[2].rm_so) == 'o' ||
393     *(line + match[2].rm_so) == 'O')
394 lefcha 1.1 node->type = MASK_TYPE_OR;
395     else
396     node->type = MASK_TYPE_AND;
397     }
398 lefcha 1.4 /* Add NOT if specified. */
399     if (match[3].rm_so != -1) {
400     s = min(match[3].rm_eo - match[3].rm_so,
401     MASK_BODY_LEN - (bp - node->body) - 1);
402     strncpy(bp, line + match[3].rm_so, s);
403     string_upper(bp, s);
404     *(bp + s - 1) = ' '; /* In case it's '\t'. */
405     *(bp + s) = 0;
406     bp += s;
407 lefcha 1.1 }
408 lefcha 1.4 /* Keyword of the search key. */
409     s = min(match[4].rm_eo - match[4].rm_so,
410     MASK_BODY_LEN - (bp - node->body) - 3);
411     strncpy(bp, line + match[4].rm_so, s);
412     string_upper(bp, s);
413     *(bp + s) = 0;
414     bp += s;
415 lefcha 1.5
416 lefcha 1.4 /* Body of the search key (string/number). */
417     for (i = 5; i <= 6; i++)
418     if (match[i].rm_so != -1) {
419     *(bp++) = ' ';
420 lefcha 1.5
421 lefcha 1.4 /* Add '"' if not supplied and search key not a number. */
422     if (match[6].rm_so == -1 &&
423     (strstr(node->body, "LARGER") ||
424 lefcha 1.5 strstr(node->body, "SMALLER")))
425 lefcha 1.4 f = 1;
426     else if (*(line + match[i].rm_so) != '"')
427     *(bp++) = '"';
428 lefcha 1.5
429 lefcha 1.4 *bp = 0;
430 lefcha 1.5
431 lefcha 1.4 s = min(match[i].rm_eo - match[i].rm_so,
432     MASK_BODY_LEN - (bp - node->body) - 2);
433     strncpy(bp, line + match[i].rm_so, s);
434     *(bp + s) = 0;
435     bp += s;
436 lefcha 1.5
437     if (*(line + match[i].rm_so) != '"' && !f)
438 lefcha 1.4 *(bp++) = '"';
439 lefcha 1.5 *bp = 0;
440 lefcha 1.4 }
441 lefcha 1.1 append_mask(node);
442    
443 lefcha 1.4 cur_fltr->masknum++;
444     cur_fltr->masklen += (bp - node->body);
445 lefcha 1.5
446    
447 lefcha 1.1 #ifdef DEBUG
448     printf("debug: MASK: '%s'\n", node->body);
449     #endif
450    
451     return 0;
452     }
453    
454    
455     /*
456     * A new job was declared, link filters with mailbox-groups.
457     */
458     int set_job(char *line, regmatch_t * match)
459     {
460 lefcha 1.4 int s;
461 lefcha 1.1 const char *delim = ",";
462 lefcha 1.4 char *ftok, *gtok, *fltr, *mbgrp, *f, *g;
463 lefcha 1.1 filter_t *cf;
464 lefcha 1.4 mboxgrp_t *cg;
465 lefcha 1.5
466 lefcha 1.4 if (!accounts || !filters)
467     return ERROR_CONFIG_PARSE;
468 lefcha 1.1
469     s = match[1].rm_eo - match[1].rm_so;
470 lefcha 1.6 fltr = (char *) xmalloc(s + 1);
471 lefcha 1.5
472 lefcha 1.4 f = strncpy(fltr, line + match[1].rm_so, s);
473     f[s] = 0;
474 lefcha 1.1
475     s = match[2].rm_eo - match[2].rm_so;
476 lefcha 1.6 mbgrp = (char *) xmalloc(s + 1);
477 lefcha 1.5
478 lefcha 1.4 /* Go through filters. */
479     while ((ftok = strsep(&f, delim))) {
480 lefcha 1.1 cf = (filter_t *) find_filter(ftok);
481     if (!cf)
482 lefcha 1.4 return ERROR_CONFIG_PARSE;
483 lefcha 1.5
484 lefcha 1.4 g = strncpy(mbgrp, line + match[2].rm_so, s);
485     g[s] = 0;
486 lefcha 1.5
487 lefcha 1.4 /* Go through mailbox groups. */
488     while ((gtok = strsep(&g, delim))) {
489     cg = (mboxgrp_t *) find_mboxgrp(gtok);
490     if (!cg)
491     return ERROR_CONFIG_PARSE;
492     link_mbox_filter(cf, cg);
493 lefcha 1.1 }
494     }
495 lefcha 1.5
496 lefcha 1.1 free(fltr);
497     free(mbgrp);
498    
499 lefcha 1.4 return 0;
500 lefcha 1.1 }
501    
502    
503     /*
504     * Link a filter with a mailbox.
505     */
506 lefcha 1.4 void link_mbox_filter(filter_t * cf, mboxgrp_t * cg)
507 lefcha 1.1 {
508     int i, j;
509 lefcha 1.4 int f;
510 lefcha 1.5
511     for (i = 0; cg->mboxes[i]; i++) {
512    
513 lefcha 1.4 for (f = j = 0; cg->mboxes[i]->filters[j]; j++)
514     if (j == MBOX_FILTERS_MAX - 1 ||
515 lefcha 1.5 !strncmp(cf->key, cg->mboxes[i]->filters[j]->key, KEY_LEN))
516 lefcha 1.4 f = 1;
517 lefcha 1.5
518 lefcha 1.4 if (f)
519     continue;
520 lefcha 1.5
521 lefcha 1.4 cg->mboxes[i]->filters[j] = cf;
522     cg->mboxes[i]->filters[j + 1] = NULL;
523 lefcha 1.1
524     }
525    
526     #ifdef DEBUG
527 lefcha 1.4 printf("debug: JOB: '%s' '%s'\n", cf->key, cg->key);
528 lefcha 1.1 #endif
529     }
530    
531    
532     /*
533 lefcha 1.3 * Free allocated memory of data structures that are not needed anymore.
534     */
535     void destroy_data(void)
536     {
537     destroy_mboxgrp(mboxgrps);
538     }
539    
540    
541     /*
542 lefcha 1.4 * Go through the mailbox-group tree, and free the memory of each node.
543 lefcha 1.1 */
544     void destroy_mboxgrp(mboxgrp_t * node)
545     {
546 lefcha 1.7 if (node->left) {
547 lefcha 1.1 destroy_mboxgrp(node->left);
548 lefcha 1.7 node->left = NULL;
549     }
550     if (node->right) {
551 lefcha 1.1 destroy_mboxgrp(node->right);
552 lefcha 1.7 node->right = NULL;
553     }
554 lefcha 1.1 #ifdef DEBUG
555     printf("debug: deleting FOLDER: '%s'\n", node->key);
556     #endif
557 lefcha 1.5
558 lefcha 1.4 free(node);
559 lefcha 1.2 }
560    
561    
562     /*
563     * Convert a string of specified size to upper case.
564     */
565 lefcha 1.4 void string_upper(char *str, size_t size)
566 lefcha 1.2 {
567     int i;
568    
569 lefcha 1.8 for (i = 0; i < size; i++, str++)
570     if (islower(*str))
571     *str = toupper(*str);
572     }
573    
574    
575     /*
576     * Decode the character triplet, consisting of the character '%'
577     * followed by two hexadecimal digits to its corresponding ASCII
578     * character (described in Section 2.2 of RFC 1738).
579     *
580     */
581     int string_decode(char *str)
582     {
583     char *c;
584     char hex[3];
585    
586     c = str;
587    
588     while (*c) {
589     if (*c == '%') {
590     if (!isxdigit(*(c + 1)) || !isxdigit(*(c + 2)))
591     return ERROR_CONFIG_PARSE;
592    
593     strncpy(hex, ++c, 2);
594     hex[2] = 0;
595    
596     if (!isprint(*str = (char) strtoul(hex, NULL, 16)))
597     return ERROR_CONFIG_PARSE;
598    
599     str++;
600     c += 2;
601     } else
602     *(str++) = *(c++);
603     }
604     *str = 0;
605    
606     return 0;
607 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26