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

Annotation of /imapfilter/data.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (hide annotations)
Fri Jan 25 17:10:17 2002 UTC (22 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.20: +16 -5 lines
File MIME type: text/plain
Merged changes from version 0.7.2.

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.13 #include <time.h>
11 lefcha 1.1
12     #include "config.h"
13     #include "imapfilter.h"
14     #include "data.h"
15    
16    
17     account_t *accounts = NULL; /* First node of accounts linked list. */
18     filter_t *filters = NULL; /* First node of filters tree. */
19    
20 lefcha 1.5 static mboxgrp_t *mboxgrps = NULL; /* First node of mailbox-groups
21     tree. */
22 lefcha 1.1
23     static account_t *cur_acct = NULL; /* Current account. */
24     static filter_t *cur_fltr = NULL; /* Current filter. */
25    
26    
27     /*
28     * Set new account's variables to safe values.
29     */
30     void init_account(account_t * node)
31     {
32     node->next = NULL;
33     node->server[0] = node->username[0] = node->password[0] = 0;
34 lefcha 1.20 node->passwdattr = PASSWORD_NONE;
35 lefcha 1.1 node->port = 143;
36 lefcha 1.12 #ifdef SSL_TLS
37     node->ssl = SSL_DISABLED;
38     #endif
39 lefcha 1.1 node->mboxes = NULL;
40     }
41    
42    
43     /*
44     * Append account node to linked list.
45     */
46     void append_account(account_t * node)
47     {
48     account_t *pos;
49     account_t **app;
50    
51     APPEND_LINKED_LIST(accounts, node, pos, app);
52     }
53    
54    
55     /*
56     * A new account entry was declared. Create it and set it's variables
57     * accordingly.
58     */
59     int set_account(char *line, regmatch_t * match)
60     {
61 lefcha 1.16 int n;
62 lefcha 1.1 char p[6];
63     account_t *node;
64 lefcha 1.5
65 lefcha 1.1 node = (account_t *) create_node(sizeof(account_t));
66    
67     init_account(node);
68 lefcha 1.20
69 lefcha 1.19 if (match[2].rm_so != -1) {
70     strncat(node->username, line + match[2].rm_so,
71     min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1));
72     } else {
73     strncat(node->username, line + match[4].rm_so,
74     min(match[4].rm_eo - match[4].rm_so, USERNAME_LEN - 1));
75     }
76 lefcha 1.8 if (strchr(node->username, '%'))
77     if (string_decode(node->username))
78     return ERROR_CONFIG_PARSE;
79 lefcha 1.1 #ifdef DEBUG
80     printf("debug: USERNAME: '%s'\n", node->username);
81     #endif
82 lefcha 1.20
83 lefcha 1.19 if (match[3].rm_so != -1) {
84     strncat(node->password, line + match[3].rm_so,
85     min(match[3].rm_eo - match[3].rm_so, PASSWORD_LEN - 1));
86     if (strchr(node->password, '%'))
87     if (string_decode(node->password))
88     return ERROR_CONFIG_PARSE;
89 lefcha 1.20 node->passwdattr = PASSWORD_PLAIN;
90     }
91 lefcha 1.1 #ifdef DEBUG
92     printf("debug: PASSWORD: '%s'\n", node->password);
93     #endif
94    
95 lefcha 1.19 strncat(node->server, line + match[5].rm_so,
96     min(match[5].rm_eo - match[5].rm_so, SERVER_LEN - 1));
97 lefcha 1.1 #ifdef DEBUG
98     printf("debug: SERVER: '%s'\n", node->server);
99     #endif
100    
101 lefcha 1.19 if (match[6].rm_so != -1) {
102     n = min(match[6].rm_eo - match[6].rm_so - 1, 5);
103     xstrncpy(p, line + match[6].rm_so + 1, n);
104 lefcha 1.16 p[n] = 0;
105 lefcha 1.11 node->port = strtoul(p, NULL, 10);
106 lefcha 1.1 #ifdef DEBUG
107     printf("debug: PORT: %d\n", node->port);
108     #endif
109     }
110 lefcha 1.12 #ifdef SSL_TLS
111 lefcha 1.19 if (match[7].rm_so != -1) {
112     if (match[6].rm_so == -1)
113 lefcha 1.12 node->port = 993;
114    
115 lefcha 1.19 if (strcasestr(line + match[7].rm_so, "ssl3"))
116 lefcha 1.12 node->ssl = SSL_SSL_V3;
117 lefcha 1.19 else if (strcasestr(line + match[7].rm_so, "tls1"))
118 lefcha 1.12 node->ssl = SSL_TLS_V1;
119     else
120     node->ssl = SSL_SSL_V2;
121     }
122     #endif
123 lefcha 1.1 append_account(node);
124     cur_acct = node;
125    
126     return 0;
127     }
128    
129    
130 lefcha 1.20 #ifdef ENCRYPTED_PASSWORDS
131     /*
132     * Find accounts without a password (candicates for password encryption).
133     */
134     char *find_password(char *user, char *serv)
135     {
136     account_t *a;
137    
138     for (a = accounts; a; a = a->next)
139     if (a->passwdattr == PASSWORD_NONE && !strcmp(a->server, serv) &&
140     !strcmp(a->username, user)) {
141     a->passwdattr = PASSWORD_ENCRYPTED;
142     return a->password;
143     }
144     return NULL;
145     }
146     #endif
147    
148 lefcha 1.1 /*
149     * Set new mailbox-group's variables to safe values.
150     */
151     void init_mboxgrp(mboxgrp_t * node)
152     {
153     node->left = node->right = NULL;
154 lefcha 1.4 node->key[0] = 0;
155 lefcha 1.1 node->mboxes[0] = NULL;
156     }
157    
158    
159     /*
160     * Insert mailbox-group node in tree.
161     */
162     void insert_mboxgrp(mboxgrp_t * node)
163     {
164     int cmp;
165     mboxgrp_t *pos;
166     mboxgrp_t **ins;
167    
168     INSERT_TREE(mboxgrps, node, pos, ins, cmp);
169     }
170    
171    
172     /*
173     * A new mailbox-group entry was declared. Create it and set it's variables
174     * accordingly.
175     */
176     int set_mboxgrp(char *line, regmatch_t * match)
177     {
178     mboxgrp_t *node;
179     char mboxs[LINE_MAX];
180    
181 lefcha 1.4 mboxs[0] = 0;
182    
183 lefcha 1.1 if (!accounts)
184 lefcha 1.4 return ERROR_CONFIG_PARSE;
185 lefcha 1.1
186     node = (mboxgrp_t *) create_node(sizeof(mboxgrp_t));
187    
188     init_mboxgrp(node);
189    
190 lefcha 1.4 strncat(node->key, line + match[1].rm_so,
191     min(match[1].rm_eo - match[1].rm_so, KEY_LEN - 1));
192 lefcha 1.1
193     #ifdef DEBUG
194     printf("debug: FOLDER: '%s'\n", node->key);
195     #endif
196    
197 lefcha 1.4 strncat(mboxs, line + match[2].rm_so,
198     min(match[2].rm_eo - match[2].rm_so, LINE_MAX - 1));
199 lefcha 1.1
200     process_mboxgrp(node, mboxs);
201    
202     insert_mboxgrp(node);
203    
204     return 0;
205     }
206    
207    
208     /*
209     * Calls set_mbox() in order to create mailboxes that are part of
210     * the mailbox-group.
211     */
212     void process_mboxgrp(mboxgrp_t * node, char *mboxs)
213     {
214 lefcha 1.16 unsigned int i = 0;
215 lefcha 1.1 const char *delim = ",";
216     char *tok;
217    
218 lefcha 1.4 while (i < MBOXGRP_MBOXES_MAX - 1 && (tok = strsep(&mboxs, delim))) {
219 lefcha 1.1 node->mboxes[i] = (mbox_t *) set_mbox(tok);
220     node->mboxes[++i] = NULL;
221     }
222     }
223    
224    
225     /*
226     * Find in the mailbox-group tree, the node with the specified key,
227     * and return a pointer to it.
228     */
229     mboxgrp_t *find_mboxgrp(char *key)
230     {
231     int cmp;
232     mboxgrp_t *pos;
233    
234     FIND_TREE(mboxgrps, key, pos, cmp);
235     }
236    
237    
238     /*
239     * Set new mailbox's variables to safe values.
240     */
241     void init_mbox(mbox_t * node)
242     {
243     node->next = NULL;
244 lefcha 1.4 node->name[0] = 0;
245 lefcha 1.1 node->filters[0] = NULL;
246     }
247    
248    
249     /*
250     * Append mailbox node to linked list.
251     */
252     void append_mbox(mbox_t * node)
253     {
254     mbox_t *pos;
255     mbox_t **ins;
256    
257     APPEND_LINKED_LIST(cur_acct->mboxes, node, pos, ins);
258     }
259    
260    
261     /*
262     * A new mailbox was declared, create it and set it's variables accordingly.
263     */
264     mbox_t *set_mbox(char *name)
265     {
266     mbox_t *node;
267    
268     node = (mbox_t *) create_node(sizeof(mbox_t));
269    
270     init_mbox(node);
271    
272 lefcha 1.21 if (*name == '"' && *(name + strlen(name) - 1) == '"')
273     strncat(node->name, name + 1, min(strlen(name) - 2, MBOX_NAME_LEN - 1));
274     else
275     strncat(node->name, name, min(strlen(name), MBOX_NAME_LEN - 1));
276 lefcha 1.1
277     #ifdef DEBUG
278     printf("debug: MBOX: '%s'\n", node->name);
279     #endif
280    
281     append_mbox(node);
282    
283     return node;
284     }
285    
286    
287     /*
288     * Set new filter's variables to safe values.
289     */
290     void init_filter(filter_t * node)
291     {
292     node->left = node->right = NULL;
293 lefcha 1.4 node->key[0] = 0;
294 lefcha 1.1 node->mode = FILTER_MODE_AND;
295     node->action.type = 0;
296 lefcha 1.9 node->action.destmbox[0] = node->action.args[0] = 0;
297 lefcha 1.1 node->masks = NULL;
298 lefcha 1.4 node->masknum = node->masklen = 0;
299 lefcha 1.1 }
300    
301    
302     /*
303     * Insert filter node to tree.
304     */
305     void insert_filter(filter_t * node)
306     {
307     int cmp;
308     filter_t *pos;
309     filter_t **ins;
310    
311     INSERT_TREE(filters, node, pos, ins, cmp);
312     }
313    
314    
315     /*
316     * A filter entry was declared, create it and set it's variables accordingly.
317     */
318     int set_filter(char *line, regmatch_t * match)
319     {
320     filter_t *node;
321 lefcha 1.5
322 lefcha 1.4 if (cur_fltr && !cur_fltr->action.type)
323     return ERROR_CONFIG_PARSE;
324 lefcha 1.1
325     node = (filter_t *) create_node(sizeof(filter_t));
326    
327     init_filter(node);
328    
329 lefcha 1.4 strncat(node->key, line + match[1].rm_so,
330     min(match[1].rm_eo - match[1].rm_so, KEY_LEN - 1));
331 lefcha 1.1
332 lefcha 1.4 if (match[2].rm_so != -1) {
333 lefcha 1.9 if (!strncasecmp(line + match[2].rm_so + 1, "or", 2))
334 lefcha 1.1 node->mode = FILTER_MODE_OR;
335     else
336     node->mode = FILTER_MODE_AND;
337     }
338     #ifdef DEBUG
339     printf("debug: FILTER: '%s' %s\n", node->key,
340     (node->mode == FILTER_MODE_OR ? "OR" : "AND"));
341     #endif
342    
343     insert_filter(node);
344     cur_fltr = node;
345    
346     return 0;
347     }
348    
349    
350     /*
351     * Find in the filter tree, the node with the specified key and
352     * return a pointer to it.
353     */
354     filter_t *find_filter(char *key)
355     {
356     int cmp;
357     filter_t *pos;
358    
359     FIND_TREE(filters, key, pos, cmp);
360     }
361    
362    
363     /*
364 lefcha 1.5 * Assign an action to the last declared filter.
365 lefcha 1.1 */
366 lefcha 1.4 int set_action(char *line, regmatch_t * match)
367 lefcha 1.1 {
368     if (!cur_fltr)
369 lefcha 1.4 return ERROR_CONFIG_PARSE;
370 lefcha 1.7
371 lefcha 1.5 if (!strncasecmp(line + match[1].rm_so, "delete", 6))
372 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_DELETE;
373 lefcha 1.5 else if (!strncasecmp(line + match[1].rm_so, "copy", 4)) {
374 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_COPY;
375 lefcha 1.21 if (*(line + match[2].rm_so) == '"' && *(line + match[2].rm_eo - 1) == '"')
376     strncat(cur_fltr->action.destmbox, line + match[2].rm_so + 1,
377     min(match[2].rm_eo - match[2].rm_so - 2, MBOX_NAME_LEN - 1));
378     else
379     strncat(cur_fltr->action.destmbox, line + match[2].rm_so,
380     min(match[2].rm_eo - match[2].rm_so, MBOX_NAME_LEN - 1));
381 lefcha 1.5 } else if (!strncasecmp(line + match[1].rm_so, "move", 4)) {
382 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_MOVE;
383 lefcha 1.21 if (*(line + match[3].rm_so) == '"' && *(line + match[3].rm_eo - 1) == '"')
384     strncat(cur_fltr->action.destmbox, line + match[3].rm_so + 1,
385     min(match[3].rm_eo - match[3].rm_so - 2, MBOX_NAME_LEN - 1));
386     else
387     strncat(cur_fltr->action.destmbox, line + match[3].rm_so,
388     min(match[3].rm_eo - match[3].rm_so, MBOX_NAME_LEN - 1));
389 lefcha 1.5 } else if (!strncasecmp(line + match[1].rm_so, "list", 4))
390 lefcha 1.4 cur_fltr->action.type = FILTER_ACTION_LIST;
391 lefcha 1.7
392 lefcha 1.4 if (match[4].rm_so != -1)
393     strncat(cur_fltr->action.args, line + match[4].rm_so,
394 lefcha 1.5 min(match[4].rm_eo - match[4].rm_so, ARGS_LEN - 1));
395 lefcha 1.1
396 lefcha 1.4 return 0;
397 lefcha 1.5
398 lefcha 1.1 }
399    
400    
401     /*
402     * Set new mask's variables to safe values.
403     */
404     void init_mask(mask_t * node)
405     {
406     node->next = NULL;
407     node->body[0] = 0;
408     node->type = 0;
409     }
410    
411    
412     /*
413     * Append mask node to linked list.
414     */
415     void append_mask(mask_t * node)
416     {
417     mask_t *pos;
418     mask_t **app;
419    
420     APPEND_LINKED_LIST(cur_fltr->masks, node, pos, app);
421     }
422    
423    
424     /*
425     * A new mask entry was declared, create it and set it's
426     * variables accordingly.
427     */
428     int set_mask(char *line, regmatch_t * match)
429     {
430 lefcha 1.17 int n, i, f = 0;
431 lefcha 1.1 mask_t *node;
432 lefcha 1.4 char *bp;
433 lefcha 1.5
434 lefcha 1.4 if (!cur_fltr)
435     return ERROR_CONFIG_PARSE;
436 lefcha 1.1
437     node = (mask_t *) create_node(sizeof(mask_t));
438    
439     init_mask(node);
440 lefcha 1.5
441 lefcha 1.4 bp = node->body;
442 lefcha 1.5
443 lefcha 1.4 /* If specified set mask's type. */
444     if (match[2].rm_so != -1 && cur_fltr->masks) {
445 lefcha 1.9 if (!strncasecmp(line + match[2].rm_so, "or", 2))
446 lefcha 1.1 node->type = MASK_TYPE_OR;
447     else
448     node->type = MASK_TYPE_AND;
449     }
450 lefcha 1.4 /* Add NOT if specified. */
451     if (match[3].rm_so != -1) {
452 lefcha 1.16 n = min(match[3].rm_eo - match[3].rm_so,
453 lefcha 1.4 MASK_BODY_LEN - (bp - node->body) - 1);
454 lefcha 1.16 xstrncpy(bp, line + match[3].rm_so, n);
455     string_upper(bp, n);
456     *(bp + n - 1) = ' '; /* In case it's '\t'. */
457     *(bp + n) = 0;
458     bp += n;
459 lefcha 1.1 }
460 lefcha 1.4 /* Keyword of the search key. */
461 lefcha 1.16 n = min(match[4].rm_eo - match[4].rm_so,
462 lefcha 1.4 MASK_BODY_LEN - (bp - node->body) - 3);
463 lefcha 1.16 xstrncpy(bp, line + match[4].rm_so, n);
464     string_upper(bp, n);
465     *(bp + n) = 0;
466     bp += n;
467 lefcha 1.5
468 lefcha 1.4 /* Body of the search key (string/number). */
469     for (i = 5; i <= 6; i++)
470     if (match[i].rm_so != -1) {
471     *(bp++) = ' ';
472 lefcha 1.5
473 lefcha 1.4 /* Add '"' if not supplied and search key not a number. */
474     if (match[6].rm_so == -1 &&
475     (strstr(node->body, "LARGER") ||
476 lefcha 1.13 strstr(node->body, "SMALLER") ||
477     strstr(node->body, "OLDER") ||
478     strstr(node->body, "NEWER")))
479 lefcha 1.4 f = 1;
480     else if (*(line + match[i].rm_so) != '"')
481     *(bp++) = '"';
482 lefcha 1.5
483 lefcha 1.4 *bp = 0;
484 lefcha 1.5
485 lefcha 1.16 n = min(match[i].rm_eo - match[i].rm_so,
486 lefcha 1.4 MASK_BODY_LEN - (bp - node->body) - 2);
487 lefcha 1.16 xstrncpy(bp, line + match[i].rm_so, n);
488     *(bp + n) = 0;
489     bp += n;
490 lefcha 1.5
491     if (*(line + match[i].rm_so) != '"' && !f)
492 lefcha 1.4 *(bp++) = '"';
493 lefcha 1.5 *bp = 0;
494 lefcha 1.4 }
495 lefcha 1.18 if (f && (strstr(node->body, "OLDER") || strstr(node->body, "NEWER"))) {
496 lefcha 1.13 convert_date(node);
497 lefcha 1.18 bp = node->body + strlen(node->body);
498     }
499 lefcha 1.1 append_mask(node);
500    
501 lefcha 1.4 cur_fltr->masknum++;
502     cur_fltr->masklen += (bp - node->body);
503 lefcha 1.5
504    
505 lefcha 1.1 #ifdef DEBUG
506     printf("debug: MASK: '%s'\n", node->body);
507     #endif
508    
509     return 0;
510     }
511    
512    
513     /*
514 lefcha 1.13 * Converts masks related to date filtering, because IMAP servers do not
515     * understand for example "OLDER 3", but "BEFORE 18-Oct-2001" (if
516     * hypothetically current date was 21-Oct-2001).
517     */
518     void convert_date(mask_t * node)
519     {
520     char *cp, *c;
521     char s[16];
522     time_t t;
523     struct tm *bt;
524    
525     cp = xstrdup(node->body);
526     node->body[0] = 0;
527    
528 lefcha 1.18 if (strstr(cp, "NOT"))
529     strncat(node->body, "NOT ", 4);
530    
531     if ((c = strstr(cp, "OLDER")))
532 lefcha 1.13 strncat(node->body, "BEFORE ", 7);
533 lefcha 1.18 else if ((c = strstr(cp, "NEWER")))
534 lefcha 1.13 strncat(node->body, "SINCE ", 6);
535    
536 lefcha 1.18 c += 6;
537    
538 lefcha 1.13 t = time(NULL) - (time_t) (strtoul(c, NULL, 10) * 24 * 60 * 60);
539     bt = localtime(&t);
540    
541     if (strftime(s, 15, "%d-%b-%Y", bt))
542 lefcha 1.18 strncat(node->body, s, 15);
543 lefcha 1.13
544 lefcha 1.20 xfree(cp);
545 lefcha 1.13 }
546    
547    
548     /*
549 lefcha 1.1 * A new job was declared, link filters with mailbox-groups.
550     */
551     int set_job(char *line, regmatch_t * match)
552     {
553 lefcha 1.16 int n;
554 lefcha 1.1 const char *delim = ",";
555 lefcha 1.4 char *ftok, *gtok, *fltr, *mbgrp, *f, *g;
556 lefcha 1.1 filter_t *cf;
557 lefcha 1.4 mboxgrp_t *cg;
558 lefcha 1.5
559 lefcha 1.10 if (!accounts || !filters || !cur_fltr->action.type)
560 lefcha 1.4 return ERROR_CONFIG_PARSE;
561 lefcha 1.1
562 lefcha 1.16 n = match[1].rm_eo - match[1].rm_so;
563     fltr = (char *) xmalloc(n + 1);
564 lefcha 1.5
565 lefcha 1.16 f = xstrncpy(fltr, line + match[1].rm_so, n);
566     f[n] = 0;
567 lefcha 1.1
568 lefcha 1.16 n = match[2].rm_eo - match[2].rm_so;
569     mbgrp = (char *) xmalloc(n + 1);
570 lefcha 1.5
571 lefcha 1.4 /* Go through filters. */
572     while ((ftok = strsep(&f, delim))) {
573 lefcha 1.1 cf = (filter_t *) find_filter(ftok);
574     if (!cf)
575 lefcha 1.4 return ERROR_CONFIG_PARSE;
576 lefcha 1.5
577 lefcha 1.16 g = xstrncpy(mbgrp, line + match[2].rm_so, n);
578     g[n] = 0;
579 lefcha 1.5
580 lefcha 1.4 /* Go through mailbox groups. */
581     while ((gtok = strsep(&g, delim))) {
582     cg = (mboxgrp_t *) find_mboxgrp(gtok);
583     if (!cg)
584     return ERROR_CONFIG_PARSE;
585     link_mbox_filter(cf, cg);
586 lefcha 1.1 }
587     }
588 lefcha 1.5
589 lefcha 1.20 xfree(fltr);
590     xfree(mbgrp);
591 lefcha 1.1
592 lefcha 1.4 return 0;
593 lefcha 1.1 }
594    
595    
596     /*
597     * Link a filter with a mailbox.
598     */
599 lefcha 1.4 void link_mbox_filter(filter_t * cf, mboxgrp_t * cg)
600 lefcha 1.1 {
601 lefcha 1.17 int i, j, f;
602 lefcha 1.5
603     for (i = 0; cg->mboxes[i]; i++) {
604 lefcha 1.4 for (f = j = 0; cg->mboxes[i]->filters[j]; j++)
605     if (j == MBOX_FILTERS_MAX - 1 ||
606 lefcha 1.20 !strcmp(cf->key, cg->mboxes[i]->filters[j]->key))
607 lefcha 1.4 f = 1;
608 lefcha 1.5
609 lefcha 1.4 if (f)
610     continue;
611 lefcha 1.5
612 lefcha 1.4 cg->mboxes[i]->filters[j] = cf;
613     cg->mboxes[i]->filters[j + 1] = NULL;
614 lefcha 1.1
615     }
616    
617     #ifdef DEBUG
618 lefcha 1.4 printf("debug: JOB: '%s' '%s'\n", cf->key, cg->key);
619 lefcha 1.1 #endif
620     }
621    
622    
623     /*
624 lefcha 1.3 * Free allocated memory of data structures that are not needed anymore.
625     */
626     void destroy_data(void)
627     {
628     destroy_mboxgrp(mboxgrps);
629     }
630    
631    
632     /*
633 lefcha 1.12 * Go through the mailbox-group tree, and free the allocated memory of
634     * each node.
635 lefcha 1.1 */
636     void destroy_mboxgrp(mboxgrp_t * node)
637     {
638 lefcha 1.7 if (node->left) {
639 lefcha 1.1 destroy_mboxgrp(node->left);
640 lefcha 1.7 node->left = NULL;
641     }
642     if (node->right) {
643 lefcha 1.1 destroy_mboxgrp(node->right);
644 lefcha 1.7 node->right = NULL;
645     }
646 lefcha 1.1 #ifdef DEBUG
647     printf("debug: deleting FOLDER: '%s'\n", node->key);
648     #endif
649 lefcha 1.5
650 lefcha 1.20 xfree(node);
651 lefcha 1.2 }
652    
653    
654     /*
655 lefcha 1.17 * Overwrite the memory space that contains the user's passwords.
656 lefcha 1.14 */
657 lefcha 1.17 void overwrite_passwords(void)
658 lefcha 1.14 {
659 lefcha 1.17 account_t *a;
660    
661     for (a = accounts; a; a = a->next)
662     memset(a->password, 0, PASSWORD_LEN);
663 lefcha 1.14 }
664    
665    
666     /*
667 lefcha 1.2 * Convert a string of specified size to upper case.
668     */
669 lefcha 1.4 void string_upper(char *str, size_t size)
670 lefcha 1.2 {
671 lefcha 1.11 unsigned int i;
672 lefcha 1.2
673 lefcha 1.8 for (i = 0; i < size; i++, str++)
674 lefcha 1.13 *str = toupper(*str);
675 lefcha 1.8 }
676    
677    
678     /*
679     * Decode the character triplet, consisting of the character '%'
680     * followed by two hexadecimal digits to its corresponding ASCII
681     * character (described in Section 2.2 of RFC 1738).
682     */
683     int string_decode(char *str)
684     {
685     char *c;
686     char hex[3];
687    
688     c = str;
689    
690     while (*c) {
691     if (*c == '%') {
692     if (!isxdigit(*(c + 1)) || !isxdigit(*(c + 2)))
693     return ERROR_CONFIG_PARSE;
694    
695 lefcha 1.15 xstrncpy(hex, ++c, 2);
696 lefcha 1.8 hex[2] = 0;
697    
698     if (!isprint(*str = (char) strtoul(hex, NULL, 16)))
699     return ERROR_CONFIG_PARSE;
700    
701     str++;
702     c += 2;
703     } else
704     *(str++) = *(c++);
705     }
706     *str = 0;
707    
708     return 0;
709 lefcha 1.19 }
710    
711    
712     /*
713     * Convert the names of personal mailboxes using the namespace specified
714     * by the mail server.
715     */
716     char *apply_namespace(char *mbox, char *prefix, char delim)
717     {
718     static char m[MBOX_NAME_LEN];
719     char *c;
720    
721     if ((!prefix[0] && !delim) || (!prefix[0] && delim == '/')
722     || !strcasecmp(mbox, "INBOX"))
723     return mbox;
724    
725     m[0] = 0;
726     strncat(m, prefix, MBOX_NAME_LEN - 1);
727     strncat(m, mbox, MBOX_NAME_LEN - strlen(m) - 1);
728    
729     c = m;
730     while ((c = strchr(c, '/')))
731     *(c++) = delim;
732    
733     #ifdef DEBUG
734     printf("debug: MAILBOX: '%s'\n", m);
735     #endif
736    
737     return m;
738 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26