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("{%ld}\r\n%s", (long)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)
100 * For mailboxes, just do it straight...
102 if (qrbuf->QRflags & QR_MAILBOX) {
103 safestrncpy(buf, qrbuf->QRname, bufsize);
104 strcpy(buf, &buf[11]);
105 if (!strcasecmp(buf, MAILROOM))
106 strcpy(buf, "INBOX");
109 * Otherwise, prefix the floor name as a "public folders" moniker
112 fl = cgetfloor(qrbuf->QRfloor);
113 snprintf(buf, bufsize, "%s|%s",
119 * Replace "/" characters with "|" for pseudo-folder-delimiting
121 for (i=0; i<strlen(buf); ++i) {
122 if (buf[i] == '/') buf[i] = '|';
128 * Convert an inputted folder name to our best guess as to what an equivalent
129 * room name should be.
131 * If an error occurs, it returns -1. Otherwise...
133 * The lower eight bits of the return value are the floor number on which the
134 * room most likely resides. The upper eight bits may contain flags,
135 * including IR_MAILBOX if we're dealing with a personal room.
138 int imap_roomname(char *rbuf, int bufsize, char *foldername)
148 if (foldername == NULL) return(-1);
149 levels = num_parms(foldername);
152 * Convert the crispy idiot's reserved names to our reserved names.
154 if (!strcasecmp(foldername, "INBOX")) {
155 safestrncpy(rbuf, MAILROOM, bufsize);
156 ret = (0 | IR_MAILBOX);
158 else if (levels > 1) {
159 extract(floorname, foldername, 0);
160 strcpy(roomname, &foldername[strlen(floorname)+1]);
161 for (i = 0; i < MAXFLOORS; ++i) {
163 if (fl->f_flags & F_INUSE) {
164 if (!strcasecmp(floorname, fl->f_name)) {
165 strcpy(rbuf, roomname);
172 /* No subfolderificationalisticism on this one... */
173 safestrncpy(rbuf, foldername, bufsize);
174 ret = (0 | IR_MAILBOX);
179 safestrncpy(rbuf, foldername, bufsize);
180 ret = (0 | IR_MAILBOX);
183 /* Undelimiterizationalize the room name (change '|' to '/') */
184 for (i=0; i<strlen(rbuf); ++i) {
185 if (rbuf[i] == '|') rbuf[i] = '/';
188 if (ret & IR_MAILBOX) {
189 if (atol(rbuf) == 0L) {
191 sprintf(rbuf, "%010ld.%s", CC->usersupp.usernum, buf);
195 lprintf(9, "(That translates to \"%s\")\n", rbuf);
204 * Output a struct internet_address_list in the form an IMAP client wants
206 void imap_ial_out(struct internet_address_list *ialist)
208 struct internet_address_list *iptr;
210 if (ialist == NULL) {
216 for (iptr = ialist; iptr != NULL; iptr = iptr->next) {
218 imap_strout(iptr->ial_name);
220 imap_strout(iptr->ial_user);
222 imap_strout(iptr->ial_node);
232 * Determine whether the supplied string is a valid message set.
233 * If the string contains only numbers, colons, commas, and asterisks,
234 * return 1 for a valid message set. If any other character is found,
237 int imap_is_message_set(char *buf)
242 return (0); /* stupidity checks */
243 if (strlen(buf) == 0)
246 if (!strcasecmp(buf, "ALL"))
247 return (1); /* macro? why? */
249 for (i = 0; i < strlen(buf); ++i) { /* now start the scan */
259 return (1); /* looks like we're good */
264 * imap_match.c, based on wildmat.c from INN
265 * hacked for Citadel/IMAP by Daniel Malament
268 /* note: not all return statements use these; don't change them */
269 #define WILDMAT_TRUE 1
270 #define WILDMAT_FALSE 0
271 #define WILDMAT_ABORT -1
272 #define WILDMAT_DELIM '|'
275 * Match text and p, return TRUE, FALSE, or ABORT.
277 static int do_imap_match(const char *supplied_text, const char *supplied_p)
280 char lcase_text[SIZ], lcase_p[SIZ];
281 char *text = lcase_text;
284 /* Copy both strings and lowercase them, in order to
285 * make this entire operation case-insensitive.
287 for (i=0; i<=strlen(supplied_text); ++i)
288 lcase_text[i] = tolower(supplied_text[i]);
289 for (i=0; i<=strlen(supplied_p); ++i)
290 p[i] = tolower(supplied_p[i]);
293 for (; *p; text++, p++) {
294 if ((*text == '\0') && (*p != '*') && (*p != '%')) {
295 return WILDMAT_ABORT;
300 return WILDMAT_FALSE;
305 while (++p, ((*p == '*') || (*p == '%'))) {
306 /* Consecutive stars or %'s act
307 * just like one star.
312 /* Trailing star matches everything. */
316 if ((matched = do_imap_match(text++, p))
321 return WILDMAT_ABORT;
323 while (++p, ((*p == '*') || (*p == '%'))) {
324 /* Consecutive %'s act just like one, but even
325 * a single star makes the sequence act like
335 * Trailing % matches everything
336 * without a delimiter.
339 if (*text == WILDMAT_DELIM) {
340 return WILDMAT_FALSE;
346 while (*text && (*(text - 1) != WILDMAT_DELIM)) {
347 if ((matched = do_imap_match(text++, p))
352 return WILDMAT_ABORT;
356 return (*text == '\0');
362 * Support function for mailbox pattern name matching in LIST and LSUB
363 * Returns nonzero if the supplied mailbox name matches the supplied pattern.
365 int imap_mailbox_matches_pattern(char *pattern, char *mailboxname)
367 /* handle just-star case quickly */
368 if ((pattern[0] == '*') && (pattern[1] == '\0')) {
371 return (do_imap_match(mailboxname, pattern) == WILDMAT_TRUE);