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 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) {
136 if (foldername == NULL) return(-1);
137 levels = num_parms(foldername);
139 /* When we can support hierarchial mailboxes, take this out. */
140 if (levels > 2) return(-1);
143 * Convert the crispy idiot's reserved names to our reserved names.
145 if (!strcasecmp(foldername, "INBOX")) {
146 safestrncpy(rbuf, MAILROOM, bufsize);
147 return(0 | IR_MAILBOX);
151 extract(buf, foldername, 0);
152 for (i=0; i<MAXFLOORS; ++i) {
154 if (fl->f_flags & F_INUSE) {
155 if (!strcasecmp(buf, fl->f_name)) {
156 extract(rbuf, foldername, 1);
162 extract(rbuf, buf, 1);
166 safestrncpy(rbuf, foldername, bufsize);
167 return(0 | IR_MAILBOX);
175 * Output a struct internet_address_list in the form an IMAP client wants
177 void imap_ial_out(struct internet_address_list *ialist) {
178 struct internet_address_list *iptr;
180 if (ialist == NULL) {
187 for (iptr = ialist; iptr != NULL; iptr = iptr->next) {
189 imap_strout(iptr->ial_name);
191 imap_strout(iptr->ial_user);
193 imap_strout(iptr->ial_node);