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 ctdlroom to an IMAP-compatible mailbox name.
94 void imap_mailboxname(char *buf, int bufsize, struct ctdlroom *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 delimiter characters with "|" for pseudo-folder-delimiting
121 for (i=0; i<strlen(buf); ++i) {
122 if (buf[i] == FDELIM) 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)
147 if (foldername == NULL) return(-1);
148 levels = num_parms(foldername);
151 * Convert the crispy idiot's reserved names to our reserved names.
153 if (!strcasecmp(foldername, "INBOX")) {
154 safestrncpy(rbuf, MAILROOM, bufsize);
155 ret = (0 | IR_MAILBOX);
157 else if (levels > 1) {
158 extract(floorname, foldername, 0);
159 strcpy(roomname, &foldername[strlen(floorname)+1]);
160 for (i = 0; i < MAXFLOORS; ++i) {
162 if (fl->f_flags & F_INUSE) {
163 if (!strcasecmp(floorname, fl->f_name)) {
164 strcpy(rbuf, roomname);
171 /* No subfolderificationalisticism on this one... */
172 safestrncpy(rbuf, foldername, bufsize);
173 ret = (0 | IR_MAILBOX);
178 safestrncpy(rbuf, foldername, bufsize);
179 ret = (0 | IR_MAILBOX);
182 /* Undelimiterizationalize the room name (change '|') */
183 for (i=0; i<strlen(rbuf); ++i) {
184 if (rbuf[i] == '|') rbuf[i] = FDELIM;
188 /*** This doesn't work.
190 if (ret & IR_MAILBOX) {
191 if (atol(rbuf) == 0L) {
193 sprintf(rbuf, "%010ld.%s", CC->user.usernum, buf);
198 lprintf(9, "(That translates to \"%s\")\n", rbuf);
207 * Output a struct internet_address_list in the form an IMAP client wants
209 void imap_ial_out(struct internet_address_list *ialist)
211 struct internet_address_list *iptr;
213 if (ialist == NULL) {
219 for (iptr = ialist; iptr != NULL; iptr = iptr->next) {
221 imap_strout(iptr->ial_name);
223 imap_strout(iptr->ial_user);
225 imap_strout(iptr->ial_node);
235 * Determine whether the supplied string is a valid message set.
236 * If the string contains only numbers, colons, commas, and asterisks,
237 * return 1 for a valid message set. If any other character is found,
240 int imap_is_message_set(char *buf)
245 return (0); /* stupidity checks */
246 if (strlen(buf) == 0)
249 if (!strcasecmp(buf, "ALL"))
250 return (1); /* macro? why? */
252 for (i = 0; i < strlen(buf); ++i) { /* now start the scan */
262 return (1); /* looks like we're good */
267 * imap_match.c, based on wildmat.c from INN
268 * hacked for Citadel/IMAP by Daniel Malament
271 /* note: not all return statements use these; don't change them */
272 #define WILDMAT_TRUE 1
273 #define WILDMAT_FALSE 0
274 #define WILDMAT_ABORT -1
275 #define WILDMAT_DELIM '|'
278 * Match text and p, return TRUE, FALSE, or ABORT.
280 static int do_imap_match(const char *supplied_text, const char *supplied_p)
283 char lcase_text[SIZ], lcase_p[SIZ];
284 char *text = lcase_text;
287 /* Copy both strings and lowercase them, in order to
288 * make this entire operation case-insensitive.
290 for (i=0; i<=strlen(supplied_text); ++i)
291 lcase_text[i] = tolower(supplied_text[i]);
292 for (i=0; i<=strlen(supplied_p); ++i)
293 p[i] = tolower(supplied_p[i]);
296 for (; *p; text++, p++) {
297 if ((*text == '\0') && (*p != '*') && (*p != '%')) {
298 return WILDMAT_ABORT;
303 return WILDMAT_FALSE;
308 while (++p, ((*p == '*') || (*p == '%'))) {
309 /* Consecutive stars or %'s act
310 * just like one star.
315 /* Trailing star matches everything. */
319 if ((matched = do_imap_match(text++, p))
324 return WILDMAT_ABORT;
326 while (++p, ((*p == '*') || (*p == '%'))) {
327 /* Consecutive %'s act just like one, but even
328 * a single star makes the sequence act like
338 * Trailing % matches everything
339 * without a delimiter.
342 if (*text == WILDMAT_DELIM) {
343 return WILDMAT_FALSE;
349 while (*text && (*(text - 1) != WILDMAT_DELIM)) {
350 if ((matched = do_imap_match(text++, p))
355 return WILDMAT_ABORT;
359 return (*text == '\0');
365 * Support function for mailbox pattern name matching in LIST and LSUB
366 * Returns nonzero if the supplied mailbox name matches the supplied pattern.
368 int imap_mailbox_matches_pattern(char *pattern, char *mailboxname)
370 /* handle just-star case quickly */
371 if ((pattern[0] == '*') && (pattern[1] == '\0')) {
374 return (do_imap_match(mailboxname, pattern) == WILDMAT_TRUE);
380 * Compare an IMAP date string (date only, no time) to the date found in
383 int imap_datecmp(char *datestr, time_t msgtime) {
388 int day, month, year;
389 int msgday, msgmonth, msgyear;
392 if (datestr == NULL) return(0);
394 /* Expecting a date in the form dd-Mmm-yyyy */
395 extract_token(daystr, datestr, 0, '-');
396 extract_token(monthstr, datestr, 1, '-');
397 extract_token(yearstr, datestr, 2, '-');
400 year = atoi(yearstr);
402 for (i=0; i<12; ++i) {
403 if (!strcasecmp(monthstr, ascmonths[i])) {
408 /* Extract day/month/year from message timestamp */
409 memcpy(&msgtm, localtime(&msgtime), sizeof(struct tm));
410 msgday = msgtm.tm_mday;
411 msgmonth = msgtm.tm_mon;
412 msgyear = msgtm.tm_year + 1900;
414 /* Now start comparing */
416 if (year < msgyear) return(+1);
417 if (year > msgyear) return(-1);
419 if (month < msgmonth) return(+1);
420 if (month > msgmonth) return(-1);
422 if (day < msgday) return(+1);
423 if (day > msgday) return(-1);