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

Annotation of /imapfilter/data.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Tue Sep 11 16:12:42 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.1: +19 -2 lines
File MIME type: text/plain
Case insensitive commands.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26