* IMAP stuff
[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 <stdio.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include "citadel.h"
14 #include "sysdep_decls.h"
15 #include "tools.h"
16 #include "internet_addressing.h"
17 #include "imap_tools.h"
18
19
20 /*
21  * Output a string to the IMAP client, either as a literal or quoted.
22  * (We do a literal if it has any double-quotes or backslashes.)
23  */
24 void imap_strout(char *buf) {
25         int i;
26         int is_literal = 0;
27
28         if (buf == NULL) {              /* yeah, we handle this */
29                 cprintf("NIL");
30                 return;
31         }
32
33         for (i=0; i<strlen(buf); ++i) {
34                 if ( (buf[i]=='\"') || (buf[i]=='\\') ) is_literal = 1;
35         }
36
37         if (is_literal) {
38                 cprintf("{%d}\r\n%s", strlen(buf), buf);
39         }
40
41         else {
42                 cprintf("\"%s\"", buf);
43         }
44 }
45
46         
47
48
49
50 /*
51  * Break a command down into tokens, taking into consideration the
52  * possibility of escaping spaces using quoted tokens
53  */
54 int imap_parameterize(char **args, char *buf) {
55         int num = 0;
56         int start = 0;
57         int i;
58         int in_quote = 0;
59         int original_len;
60
61         strcat(buf, " ");
62
63         original_len = strlen(buf);
64
65         for (i=0; i<original_len; ++i) {
66
67                 if ( (isspace(buf[i])) && (!in_quote) ) {
68                         buf[i] = 0;
69                         args[num] = &buf[start];
70                         start = i+1;
71                         if (args[num][0] == '\"') {
72                                 ++args[num];
73                                 args[num][strlen(args[num])-1] = 0;
74                         }
75                         ++num;
76                 }
77
78                 else if ( (buf[i] == '\"') && (!in_quote) ) {
79                         in_quote = 1;
80                 }
81
82                 else if ( (buf[i] == '\"') && (in_quote) ) {
83                         in_quote = 0;
84                 }
85
86         }
87
88         return(num);
89 }
90                         
91 /*
92  * Convert a struct quickroom to an IMAP-compatible mailbox name.
93  */
94 void imap_mailboxname(char *buf, int bufsize, struct quickroom *qrbuf) {
95
96         safestrncpy(buf, qrbuf->QRname, bufsize);
97         if (qrbuf->QRflags & QR_MAILBOX) {
98                 strcpy(buf, &buf[11]);
99                 if (!strcasecmp(buf, MAILROOM)) strcpy(buf, "INBOX");
100         }
101 }
102
103
104 /*
105  * Output a struct internet_address_list in the form an IMAP client wants
106  */
107 void imap_ial_out(struct internet_address_list *ialist) {
108         struct internet_address_list *iptr;
109
110         if (ialist == NULL) {
111                 cprintf("NIL");
112                 return;
113         }
114
115         cprintf("(");   
116
117         for (iptr = ialist; iptr != NULL; iptr = iptr->next) {
118                 cprintf("(");   
119                 imap_strout(iptr->ial_name);
120                 cprintf(" NIL ");
121                 imap_strout(iptr->ial_user);
122                 cprintf(" ");
123                 imap_strout(iptr->ial_node);
124                 cprintf(")");   
125         }
126
127         cprintf(")");
128 }