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

Annotation of /imapfilter/data.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations)
Thu Oct 4 14:15:38 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.8: +3 -6 lines
File MIME type: text/plain
Uninitialized variable.

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 lefcha 1.9 node->action.destmbox[0] = node->action.args[0] = 0;
251 lefcha 1.1 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.9 if (!strncasecmp(line + match[2].rm_so + 1, "or", 2))
288 lefcha 1.1 node->mode = FILTER_MODE_OR;
289     else
290     node->mode = FILTER_MODE_AND;
291     }
292     #ifdef DEBUG
293     printf("debug: FILTER: '%s' %s\n", node->key,
294     (node->mode == FILTER_MODE_OR ? "OR" : "AND"));
295     #endif
296    
297     insert_filter(node);
298     cur_fltr = node;
299    
300     return 0;
301     }
302    
303    
304     /*
305     * Find in the filter tree, the node with the specified key and
306     * return a pointer to it.
307     */
308     filter_t *find_filter(char *key)
309     {
310     int cmp;
311     filter_t *pos;
312    
313     FIND_TREE(filters, key, pos, cmp);
314     }
315    
316    
317     /*
318 lefcha 1.5 * Assign an action to the last declared filter.
319 lefcha 1.1 */
320 lefcha 1.4 int set_action(char *line, regmatch_t * match)
321 lefcha 1.1 {
322     if (!cur_fltr)
323 lefcha 1.4 return ERROR_CONFIG_PARSE;
324 lefcha 1.7
325 lefcha 1.5 if (!strncasecmp(line + match[1].rm_so, "delete", 6))
326 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_DELETE;
327 lefcha 1.5 else if (!strncasecmp(line + match[1].rm_so, "copy", 4)) {
328 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_COPY;
329     strncat(cur_fltr->action.destmbox, line + match[2].rm_so,
330     min(match[2].rm_eo - match[2].rm_so, MBOX_NAME_LEN - 1));
331 lefcha 1.5 } else if (!strncasecmp(line + match[1].rm_so, "move", 4)) {
332 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_MOVE;
333     strncat(cur_fltr->action.destmbox, line + match[3].rm_so,
334     min(match[3].rm_eo - match[3].rm_so, MBOX_NAME_LEN - 1));
335 lefcha 1.5 } else if (!strncasecmp(line + match[1].rm_so, "list", 4))
336 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_LIST;
337 lefcha 1.7
338 lefcha 1.4 if (match[4].rm_so != -1)
339     strncat(cur_fltr->action.args, line + match[4].rm_so,
340 lefcha 1.5 min(match[4].rm_eo - match[4].rm_so, ARGS_LEN - 1));
341 lefcha 1.1
342 lefcha 1.4 return 0;
343 lefcha 1.5
344 lefcha 1.1 }
345    
346    
347     /*
348     * Set new mask's variables to safe values.
349     */
350     void init_mask(mask_t * node)
351     {
352     node->next = NULL;
353     node->body[0] = 0;
354     node->type = 0;
355     }
356    
357    
358     /*
359     * Append mask node to linked list.
360     */
361     void append_mask(mask_t * node)
362     {
363     mask_t *pos;
364     mask_t **app;
365    
366     APPEND_LINKED_LIST(cur_fltr->masks, node, pos, app);
367     }
368    
369    
370     /*
371     * A new mask entry was declared, create it and set it's
372     * variables accordingly.
373     */
374     int set_mask(char *line, regmatch_t * match)
375     {
376 lefcha 1.4 int s, i, f = 0;
377 lefcha 1.1 mask_t *node;
378 lefcha 1.4 char *bp;
379 lefcha 1.5
380 lefcha 1.4 if (!cur_fltr)
381     return ERROR_CONFIG_PARSE;
382 lefcha 1.1
383     node = (mask_t *) create_node(sizeof(mask_t));
384    
385     init_mask(node);
386 lefcha 1.5
387 lefcha 1.4 bp = node->body;
388 lefcha 1.5
389 lefcha 1.4 /* If specified set mask's type. */
390     if (match[2].rm_so != -1 && cur_fltr->masks) {
391 lefcha 1.9 if (!strncasecmp(line + match[2].rm_so, "or", 2))
392 lefcha 1.1 node->type = MASK_TYPE_OR;
393     else
394     node->type = MASK_TYPE_AND;
395     }
396 lefcha 1.4 /* Add NOT if specified. */
397     if (match[3].rm_so != -1) {
398     s = min(match[3].rm_eo - match[3].rm_so,
399     MASK_BODY_LEN - (bp - node->body) - 1);
400     strncpy(bp, line + match[3].rm_so, s);
401     string_upper(bp, s);
402     *(bp + s - 1) = ' '; /* In case it's '\t'. */
403     *(bp + s) = 0;
404     bp += s;
405 lefcha 1.1 }
406 lefcha 1.4 /* Keyword of the search key. */
407     s = min(match[4].rm_eo - match[4].rm_so,
408     MASK_BODY_LEN - (bp - node->body) - 3);
409     strncpy(bp, line + match[4].rm_so, s);
410     string_upper(bp, s);
411     *(bp + s) = 0;
412     bp += s;
413 lefcha 1.5
414 lefcha 1.4 /* Body of the search key (string/number). */
415     for (i = 5; i <= 6; i++)
416     if (match[i].rm_so != -1) {
417     *(bp++) = ' ';
418 lefcha 1.5
419 lefcha 1.4 /* Add '"' if not supplied and search key not a number. */
420     if (match[6].rm_so == -1 &&
421     (strstr(node->body, "LARGER") ||
422 lefcha 1.5 strstr(node->body, "SMALLER")))
423 lefcha 1.4 f = 1;
424     else if (*(line + match[i].rm_so) != '"')
425     *(bp++) = '"';
426 lefcha 1.5
427 lefcha 1.4 *bp = 0;
428 lefcha 1.5
429 lefcha 1.4 s = min(match[i].rm_eo - match[i].rm_so,
430     MASK_BODY_LEN - (bp - node->body) - 2);
431     strncpy(bp, line + match[i].rm_so, s);
432     *(bp + s) = 0;
433     bp += s;
434 lefcha 1.5
435     if (*(line + match[i].rm_so) != '"' && !f)
436 lefcha 1.4 *(bp++) = '"';
437 lefcha 1.5 *bp = 0;
438 lefcha 1.4 }
439 lefcha 1.1 append_mask(node);
440    
441 lefcha 1.4 cur_fltr->masknum++;
442     cur_fltr->masklen += (bp - node->body);
443 lefcha 1.5
444    
445 lefcha 1.1 #ifdef DEBUG
446     printf("debug: MASK: '%s'\n", node->body);
447     #endif
448    
449     return 0;
450     }
451    
452    
453     /*
454     * A new job was declared, link filters with mailbox-groups.
455     */
456     int set_job(char *line, regmatch_t * match)
457     {
458 lefcha 1.4 int s;
459 lefcha 1.1 const char *delim = ",";
460 lefcha 1.4 char *ftok, *gtok, *fltr, *mbgrp, *f, *g;
461 lefcha 1.1 filter_t *cf;
462 lefcha 1.4 mboxgrp_t *cg;
463 lefcha 1.5
464 lefcha 1.4 if (!accounts || !filters)
465     return ERROR_CONFIG_PARSE;
466 lefcha 1.1
467     s = match[1].rm_eo - match[1].rm_so;
468 lefcha 1.6 fltr = (char *) xmalloc(s + 1);
469 lefcha 1.5
470 lefcha 1.4 f = strncpy(fltr, line + match[1].rm_so, s);
471     f[s] = 0;
472 lefcha 1.1
473     s = match[2].rm_eo - match[2].rm_so;
474 lefcha 1.6 mbgrp = (char *) xmalloc(s + 1);
475 lefcha 1.5
476 lefcha 1.4 /* Go through filters. */
477     while ((ftok = strsep(&f, delim))) {
478 lefcha 1.1 cf = (filter_t *) find_filter(ftok);
479     if (!cf)
480 lefcha 1.4 return ERROR_CONFIG_PARSE;
481 lefcha 1.5
482 lefcha 1.4 g = strncpy(mbgrp, line + match[2].rm_so, s);
483     g[s] = 0;
484 lefcha 1.5
485 lefcha 1.4 /* Go through mailbox groups. */
486     while ((gtok = strsep(&g, delim))) {
487     cg = (mboxgrp_t *) find_mboxgrp(gtok);
488     if (!cg)
489     return ERROR_CONFIG_PARSE;
490     link_mbox_filter(cf, cg);
491 lefcha 1.1 }
492     }
493 lefcha 1.5
494 lefcha 1.1 free(fltr);
495     free(mbgrp);
496    
497 lefcha 1.4 return 0;
498 lefcha 1.1 }
499    
500    
501     /*
502     * Link a filter with a mailbox.
503     */
504 lefcha 1.4 void link_mbox_filter(filter_t * cf, mboxgrp_t * cg)
505 lefcha 1.1 {
506     int i, j;
507 lefcha 1.4 int f;
508 lefcha 1.5
509     for (i = 0; cg->mboxes[i]; i++) {
510    
511 lefcha 1.4 for (f = j = 0; cg->mboxes[i]->filters[j]; j++)
512     if (j == MBOX_FILTERS_MAX - 1 ||
513 lefcha 1.5 !strncmp(cf->key, cg->mboxes[i]->filters[j]->key, KEY_LEN))
514 lefcha 1.4 f = 1;
515 lefcha 1.5
516 lefcha 1.4 if (f)
517     continue;
518 lefcha 1.5
519 lefcha 1.4 cg->mboxes[i]->filters[j] = cf;
520     cg->mboxes[i]->filters[j + 1] = NULL;
521 lefcha 1.1
522     }
523    
524     #ifdef DEBUG
525 lefcha 1.4 printf("debug: JOB: '%s' '%s'\n", cf->key, cg->key);
526 lefcha 1.1 #endif
527     }
528    
529    
530     /*
531 lefcha 1.3 * Free allocated memory of data structures that are not needed anymore.
532     */
533     void destroy_data(void)
534     {
535     destroy_mboxgrp(mboxgrps);
536     }
537    
538    
539     /*
540 lefcha 1.4 * Go through the mailbox-group tree, and free the memory of each node.
541 lefcha 1.1 */
542     void destroy_mboxgrp(mboxgrp_t * node)
543     {
544 lefcha 1.7 if (node->left) {
545 lefcha 1.1 destroy_mboxgrp(node->left);
546 lefcha 1.7 node->left = NULL;
547     }
548     if (node->right) {
549 lefcha 1.1 destroy_mboxgrp(node->right);
550 lefcha 1.7 node->right = NULL;
551     }
552 lefcha 1.1 #ifdef DEBUG
553     printf("debug: deleting FOLDER: '%s'\n", node->key);
554     #endif
555 lefcha 1.5
556 lefcha 1.4 free(node);
557 lefcha 1.2 }
558    
559    
560     /*
561     * Convert a string of specified size to upper case.
562     */
563 lefcha 1.4 void string_upper(char *str, size_t size)
564 lefcha 1.2 {
565     int i;
566    
567 lefcha 1.8 for (i = 0; i < size; i++, str++)
568     if (islower(*str))
569     *str = toupper(*str);
570     }
571    
572    
573     /*
574     * Decode the character triplet, consisting of the character '%'
575     * followed by two hexadecimal digits to its corresponding ASCII
576     * character (described in Section 2.2 of RFC 1738).
577     */
578     int string_decode(char *str)
579     {
580     char *c;
581     char hex[3];
582    
583     c = str;
584    
585     while (*c) {
586     if (*c == '%') {
587     if (!isxdigit(*(c + 1)) || !isxdigit(*(c + 2)))
588     return ERROR_CONFIG_PARSE;
589    
590     strncpy(hex, ++c, 2);
591     hex[2] = 0;
592    
593     if (!isprint(*str = (char) strtoul(hex, NULL, 16)))
594     return ERROR_CONFIG_PARSE;
595    
596     str++;
597     c += 2;
598     } else
599     *(str++) = *(c++);
600     }
601     *str = 0;
602    
603     return 0;
604 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26