]> code.citadel.org Git - citadel.git/blob - citadel/imap_tools.c
* mini fix to imap
[citadel.git] / citadel / imap_tools.c
1 /*
2  * $Id$
3  *
4  * Utility functions for the IMAP module.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <string.h>
12 #include "citadel.h"
13 #include "tools.h"
14 #include "imap_tools.h"
15
16 /*
17  * Break a command down into tokens, taking into consideration the
18  * possibility of escaping spaces using quoted tokens
19  */
20 int imap_parameterize(char **args, char *buf) {
21         int num = 0;
22         int start = 0;
23         int i;
24         int in_quote = 0;
25         int original_len;
26
27         strcat(buf, " ");
28
29         original_len = strlen(buf);
30
31         for (i=0; i<original_len; ++i) {
32
33                 if ( (isspace(buf[i])) && (!in_quote) ) {
34                         buf[i] = 0;
35                         args[num] = &buf[start];
36                         start = i+1;
37                         if (args[num][0] == '\"') {
38                                 ++args[num];
39                                 args[num][strlen(args[num])-1] = 0;
40                         }
41                         ++num;
42                 }
43
44                 else if ( (buf[i] == '\"') && (!in_quote) ) {
45                         in_quote = 1;
46                 }
47
48                 else if ( (buf[i] == '\"') && (in_quote) ) {
49                         in_quote = 0;
50                 }
51
52         }
53
54         return(num);
55 }
56                         
57 /*
58  * Convert a struct quickroom to an IMAP-compatible mailbox name.
59  */
60 void imap_mailboxname(char *buf, int bufsize, struct quickroom *qrbuf) {
61
62         safestrncpy(buf, qrbuf->QRname, bufsize);
63         if (qrbuf->QRflags & QR_MAILBOX) {
64                 strcpy(buf, &buf[11]);
65                 if (!strcasecmp(buf, MAILROOM)) strcpy(buf, "INBOX");
66         }
67 }
68
69
70 /*
71  * Parse a parenthesized list of items (as with the FETCH command)
72  */
73 int imap_extract_data_items(char *items) {
74         int num_items = 0;
75         int i;
76
77         /* First, convert all whitespace to ordinary space characters */
78         for (i=0; i<strlen(items); ++i) {
79                 if (isspace(items[i])) items[i] = ' ';
80         }
81
82         return num_items;
83 }
84
85