4 * Utility functions for the IMAP module.
14 #include "sysdep_decls.h"
17 #include "internet_addressing.h"
18 #include "imap_tools.h"
26 * Output a string to the IMAP client, either as a literal or quoted.
27 * (We do a literal if it has any double-quotes or backslashes.)
29 void imap_strout(char *buf)
34 if (buf == NULL) { /* yeah, we handle this */
38 for (i = 0; i < strlen(buf); ++i) {
39 if ((buf[i] == '\"') || (buf[i] == '\\'))
44 cprintf("{%d}\r\n%s", strlen(buf), buf);
46 cprintf("\"%s\"", buf);
55 * Break a command down into tokens, taking into consideration the
56 * possibility of escaping spaces using quoted tokens
58 int imap_parameterize(char **args, char *buf)
68 original_len = strlen(buf);
70 for (i = 0; i < original_len; ++i) {
72 if ((isspace(buf[i])) && (!in_quote)) {
74 args[num] = &buf[start];
76 if (args[num][0] == '\"') {
78 args[num][strlen(args[num]) - 1] = 0;
81 } else if ((buf[i] == '\"') && (!in_quote)) {
83 } else if ((buf[i] == '\"') && (in_quote)) {
92 * Convert a struct quickroom to an IMAP-compatible mailbox name.
94 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))
105 strcpy(buf, "INBOX");
108 * Otherwise, prefix the floor name as a "public folders" moniker
111 fl = cgetfloor(qrbuf->QRfloor);
112 snprintf(buf, bufsize, "%s|%s",
120 * Convert an inputted folder name to our best guess as to what an equivalent
121 * room name should be.
123 * If an error occurs, it returns -1. Otherwise...
125 * The lower eight bits of the return value are the floor number on which the
126 * room most likely resides. The upper eight bits may contain flags,
127 * including IR_MAILBOX if we're dealing with a personal room.
130 int imap_roomname(char *rbuf, int bufsize, char *foldername)
137 if (foldername == NULL)
139 levels = num_parms(foldername);
141 /* When we can support hierarchial mailboxes, take this out. */
146 * Convert the crispy idiot's reserved names to our reserved names.
148 if (!strcasecmp(foldername, "INBOX")) {
149 safestrncpy(rbuf, MAILROOM, bufsize);
150 return (0 | IR_MAILBOX);
153 extract(buf, foldername, 0);
154 for (i = 0; i < MAXFLOORS; ++i) {
156 if (fl->f_flags & F_INUSE) {
157 if (!strcasecmp(buf, fl->f_name)) {
158 extract(rbuf, foldername, 1);
164 /* since we don't allow multi-level yet, fail.
165 extract(rbuf, buf, 1);
170 safestrncpy(rbuf, foldername, bufsize);
171 return (0 | IR_MAILBOX);
179 * Output a struct internet_address_list in the form an IMAP client wants
181 void imap_ial_out(struct internet_address_list *ialist)
183 struct internet_address_list *iptr;
185 if (ialist == NULL) {
191 for (iptr = ialist; iptr != NULL; iptr = iptr->next) {
193 imap_strout(iptr->ial_name);
195 imap_strout(iptr->ial_user);
197 imap_strout(iptr->ial_node);
207 * Determine whether the supplied string is a valid message set.
208 * If the string contains only numbers, colons, commas, and asterisks,
209 * return 1 for a valid message set. If any other character is found,
212 int imap_is_message_set(char *buf)
217 return (0); /* stupidity checks */
218 if (strlen(buf) == 0)
221 if (!strcasecmp(buf, "ALL"))
222 return (1); /* macro? why? */
224 for (i = 0; i < strlen(buf); ++i) { /* now start the scan */
234 return (1); /* looks like we're good */
239 * imap_match.c, based on wildmat.c from INN
240 * hacked for Citadel/IMAP by Daniel Malament
243 /* note: not all return statements use these; don't change them */
244 #define WILDMAT_TRUE 1
245 #define WILDMAT_FALSE 0
246 #define WILDMAT_ABORT -1
247 #define WILDMAT_DELIM '|'
250 * Match text and p, return TRUE, FALSE, or ABORT.
252 static int do_imap_match(const char *supplied_text, const char *supplied_p)
255 char lcase_text[SIZ], lcase_p[SIZ];
256 char *text = lcase_text;
259 /* Copy both strings and lowercase them, in order to
260 * make this entire operation case-insensitive.
262 for (i=0; i<=strlen(supplied_text); ++i)
263 lcase_text[i] = tolower(supplied_text[i]);
264 for (i=0; i<=strlen(supplied_p); ++i)
265 p[i] = tolower(supplied_p[i]);
268 for (; *p; text++, p++) {
269 if ((*text == '\0') && (*p != '*') && (*p != '%')) {
270 return WILDMAT_ABORT;
275 return WILDMAT_FALSE;
280 while (++p, ((*p == '*') || (*p == '%'))) {
281 /* Consecutive stars or %'s act
282 * just like one star.
287 /* Trailing star matches everything. */
291 if ((matched = do_imap_match(text++, p))
296 return WILDMAT_ABORT;
298 while (++p, ((*p == '*') || (*p == '%'))) {
299 /* Consecutive %'s act just like one, but even
300 * a single star makes the sequence act like
310 * Trailing % matches everything
311 * without a delimiter.
314 if (*text == WILDMAT_DELIM) {
315 return WILDMAT_FALSE;
321 while (*text && (*(text - 1) != WILDMAT_DELIM)) {
322 if ((matched = do_imap_match(text++, p))
327 return WILDMAT_ABORT;
331 return (*text == '\0');
337 * Support function for mailbox pattern name matching in LIST and LSUB
338 * Returns nonzero if the supplied mailbox name matches the supplied pattern.
340 int imap_mailbox_matches_pattern(char *pattern, char *mailboxname)
342 /* handle just-star case quickly */
343 if ((pattern[0] == '*') && (pattern[1] == '\0')) {
346 return (do_imap_match(mailboxname, pattern) == WILDMAT_TRUE);