4 * Utility functions for the IMAP module.
14 #include "sysdep_decls.h"
17 #include "internet_addressing.h"
18 #include "imap_tools.h"
22 * Output a string to the IMAP client, either as a literal or quoted.
23 * (We do a literal if it has any double-quotes or backslashes.)
25 void imap_strout(char *buf) {
29 if (buf == NULL) { /* yeah, we handle this */
34 for (i=0; i<strlen(buf); ++i) {
35 if ( (buf[i]=='\"') || (buf[i]=='\\') ) is_literal = 1;
39 cprintf("{%d}\r\n%s", strlen(buf), buf);
43 cprintf("\"%s\"", buf);
52 * Break a command down into tokens, taking into consideration the
53 * possibility of escaping spaces using quoted tokens
55 int imap_parameterize(char **args, char *buf) {
64 original_len = strlen(buf);
66 for (i=0; i<original_len; ++i) {
68 if ( (isspace(buf[i])) && (!in_quote) ) {
70 args[num] = &buf[start];
72 if (args[num][0] == '\"') {
74 args[num][strlen(args[num])-1] = 0;
79 else if ( (buf[i] == '\"') && (!in_quote) ) {
83 else if ( (buf[i] == '\"') && (in_quote) ) {
93 * Convert a struct quickroom to an IMAP-compatible mailbox name.
95 void imap_mailboxname(char *buf, int bufsize, struct quickroom *qrbuf) {
99 * For mailboxes, just do it straight...
101 if (qrbuf->QRflags & QR_MAILBOX) {
102 safestrncpy(buf, qrbuf->QRname, bufsize);
103 strcpy(buf, &buf[11]);
104 if (!strcasecmp(buf, MAILROOM)) strcpy(buf, "INBOX");
108 * Otherwise, prefix the floor name as a "public folders" moniker
111 fl = cgetfloor(qrbuf->QRfloor);
112 lprintf(9, "floor %d: %s\n", qrbuf->QRfloor, fl->f_name); /* FIXME take out */
113 snprintf(buf, bufsize, "%s|%s",
121 * Convert an inputted folder name to our best guess as to what an equivalent
122 * room name should be.
124 * If an error occurs, it returns -1. Otherwise...
126 * The lower eight bits of the return value are the floor number on which the
127 * room most likely resides. The upper eight bits may contain flags,
128 * including IR_MAILBOX if we're dealing with a personal room.
131 int imap_roomname(char *rbuf, int bufsize, char *foldername) {
137 if (foldername == NULL) return(-1);
138 levels = num_parms(foldername);
140 /* When we can support hierarchial mailboxes, take this out. */
141 if (levels > 2) return(-1);
144 * Convert the crispy idiot's reserved names to our reserved names.
146 if (!strcasecmp(foldername, "INBOX")) {
147 safestrncpy(rbuf, MAILROOM, bufsize);
148 return(0 | IR_MAILBOX);
152 extract(buf, foldername, 0);
153 for (i=0; i<MAXFLOORS; ++i) {
155 lprintf(9, "floor %d: %s\n", i, fl->f_name); /* FIXME take out */
156 if (fl->f_flags & F_INUSE) {
157 if (!strcasecmp(buf, fl->f_name)) {
158 extract(rbuf, foldername, 1);
164 extract(rbuf, buf, 1);
168 safestrncpy(rbuf, foldername, bufsize);
169 return(0 | IR_MAILBOX);
177 * Output a struct internet_address_list in the form an IMAP client wants
179 void imap_ial_out(struct internet_address_list *ialist) {
180 struct internet_address_list *iptr;
182 if (ialist == NULL) {
189 for (iptr = ialist; iptr != NULL; iptr = iptr->next) {
191 imap_strout(iptr->ial_name);
193 imap_strout(iptr->ial_user);
195 imap_strout(iptr->ial_node);