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

Contents of /imapfilter/data.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Sun Sep 30 20:13:46 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.3: +168 -160 lines
File MIME type: text/plain
Error codes, bug fixes, better string manipulation.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26