Moved to new module init structure.
[citadel.git] / citadel / serv_imap.c
index 0f21c01504f8d2f82c2a4d909151a4ed0d3bf1cf..be4132ea3cf4698ee7df433de28484e165872535 100644 (file)
@@ -1,12 +1,13 @@
 /*
  * $Id$ 
  *
- * IMAP server for the Citadel/UX system
- * Copyright (C) 2000-2002 by Art Cancro and others.
+ * IMAP server for the Citadel system
+ * Copyright (C) 2000-2007 by Art Cancro and others.
  * This code is released under the terms of the GNU General Public License.
  *
- * WARNING: Mark Crispin is an idiot.  IMAP is the most brain-damaged protocol
- * you will ever have the profound lack of pleasure to encounter.
+ * WARNING: the IMAP protocol is badly designed.  No implementation of it
+ * is perfect.  Indeed, with so much gratuitous complexity, *all* IMAP
+ * implementations have bugs.
  */
 
 #include "sysdep.h"
 #include <ctype.h>
 #include <string.h>
 #include <limits.h>
-
-#ifdef HAVE_OPENSSL
-#include <openssl/ssl.h>
-#include <openssl/err.h>
-#include <openssl/rand.h>
-#endif
-
 #include "citadel.h"
 #include "server.h"
-#include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "serv_extensions.h"
 #include "room_ops.h"
 #include "user_ops.h"
 #include "policy.h"
 #include "internet_addressing.h"
 #include "serv_imap.h"
 #include "imap_tools.h"
+#include "imap_list.h"
 #include "imap_fetch.h"
 #include "imap_search.h"
 #include "imap_store.h"
+#include "imap_acl.h"
+#include "imap_metadata.h"
 #include "imap_misc.h"
 
 #ifdef HAVE_OPENSSL
 #include "serv_crypto.h"
 #endif
 
+
+#include "ctdl_module.h"
+
+
 /* imap_rename() uses this struct containing list of rooms to rename */
 struct irl {
        struct irl *next;
@@ -88,14 +87,16 @@ struct irlparms {
 void imap_free_msgids(void)
 {
        if (IMAP->msgids != NULL) {
-               phree(IMAP->msgids);
+               free(IMAP->msgids);
                IMAP->msgids = NULL;
                IMAP->num_msgs = 0;
+               IMAP->num_alloc = 0;
        }
        if (IMAP->flags != NULL) {
-               phree(IMAP->flags);
+               free(IMAP->flags);
                IMAP->flags = NULL;
        }
+       IMAP->last_mtime = (-1);
 }
 
 
@@ -105,7 +106,7 @@ void imap_free_msgids(void)
 void imap_free_transmitted_message(void)
 {
        if (IMAP->transmitted_message != NULL) {
-               phree(IMAP->transmitted_message);
+               free(IMAP->transmitted_message);
                IMAP->transmitted_message = NULL;
                IMAP->transmitted_length = 0;
        }
@@ -113,27 +114,88 @@ void imap_free_transmitted_message(void)
 
 
 /*
- * Set the \\Seen flag for messages which aren't new
+ * Set the \Seen, \Recent. and \Answered flags, based on the sequence
+ * sets stored in the visit record for this user/room.  Note that we have
+ * to parse each sequence set manually here, because calling the utility
+ * function is_msg_in_sequence_set() over and over again is too expensive.
+ *
+ * first_msg should be set to 0 to rescan the flags for every message in the
+ * room, or some other value if we're only interested in an incremental
+ * update.
  */
-void imap_set_seen_flags(void)
+void imap_set_seen_flags(int first_msg)
 {
        struct visit vbuf;
        int i;
+       int num_sets;
+       int s;
+       char setstr[64], lostr[64], histr[64];
+       long lo, hi;
 
+       if (IMAP->num_msgs < 1) return;
        CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
-       if (IMAP->num_msgs > 0) {
-               for (i = 0; i < IMAP->num_msgs; ++i) {
-                       if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
+
+       for (i = first_msg; i < IMAP->num_msgs; ++i) {
+               IMAP->flags[i] = IMAP->flags[i] & ~IMAP_SEEN;
+               IMAP->flags[i] |= IMAP_RECENT;
+               IMAP->flags[i] = IMAP->flags[i] & ~IMAP_ANSWERED;
+       }
+
+       /*
+        * Do the "\Seen" flag.
+        * (Any message not "\Seen" is considered "\Recent".)
+        */
+       num_sets = num_tokens(vbuf.v_seen, ',');
+       for (s=0; s<num_sets; ++s) {
+               extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
+
+               extract_token(lostr, setstr, 0, ':', sizeof lostr);
+               if (num_tokens(setstr, ':') >= 2) {
+                       extract_token(histr, setstr, 1, ':', sizeof histr);
+                       if (!strcmp(histr, "*")) {
+                               snprintf(histr, sizeof histr, "%ld", LONG_MAX);
+                       }
+               } 
+               else {
+                       strcpy(histr, lostr);
+               }
+               lo = atol(lostr);
+               hi = atol(histr);
+
+               for (i = first_msg; i < IMAP->num_msgs; ++i) {
+                       if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
                                IMAP->flags[i] |= IMAP_SEEN;
+                               IMAP->flags[i] = IMAP->flags[i] & ~IMAP_RECENT;
                        }
-                       if (is_msg_in_mset
-                           (vbuf.v_answered, IMAP->msgids[i])) {
+               }
+       }
+
+       /* Do the ANSWERED flag */
+       num_sets = num_tokens(vbuf.v_answered, ',');
+       for (s=0; s<num_sets; ++s) {
+               extract_token(setstr, vbuf.v_answered, s, ',', sizeof setstr);
+
+               extract_token(lostr, setstr, 0, ':', sizeof lostr);
+               if (num_tokens(setstr, ':') >= 2) {
+                       extract_token(histr, setstr, 1, ':', sizeof histr);
+                       if (!strcmp(histr, "*")) {
+                               snprintf(histr, sizeof histr, "%ld", LONG_MAX);
+                       }
+               } 
+               else {
+                       strcpy(histr, lostr);
+               }
+               lo = atol(lostr);
+               hi = atol(histr);
+
+               for (i = first_msg; i < IMAP->num_msgs; ++i) {
+                       if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
                                IMAP->flags[i] |= IMAP_ANSWERED;
                        }
                }
        }
-}
 
+}
 
 
 
@@ -147,22 +209,13 @@ void imap_set_seen_flags(void)
 void imap_add_single_msgid(long msgnum, void *userdata)
 {
 
-       IMAP->num_msgs = IMAP->num_msgs + 1;
-       if (IMAP->msgids == NULL) {
-               IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
-                                     * REALLOC_INCREMENT);
-       } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
-               IMAP->msgids = reallok(IMAP->msgids,
-                                      (IMAP->num_msgs +
-                                       REALLOC_INCREMENT) * sizeof(long));
-       }
-       if (IMAP->flags == NULL) {
-               IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
-                                    * REALLOC_INCREMENT);
-       } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
-               IMAP->flags = reallok(IMAP->flags,
-                                     (IMAP->num_msgs +
-                                      REALLOC_INCREMENT) * sizeof(long));
+       ++IMAP->num_msgs;
+       if (IMAP->num_msgs > IMAP->num_alloc) {
+               IMAP->num_alloc += REALLOC_INCREMENT;
+               IMAP->msgids = realloc(IMAP->msgids,
+                                       (IMAP->num_alloc * sizeof(long)) );
+               IMAP->flags = realloc(IMAP->flags,
+                                       (IMAP->num_alloc * sizeof(long)) );
        }
        IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
        IMAP->flags[IMAP->num_msgs - 1] = 0;
@@ -175,21 +228,32 @@ void imap_add_single_msgid(long msgnum, void *userdata)
  */
 void imap_load_msgids(void)
 {
+       struct cdbdata *cdbfr;
 
        if (IMAP->selected == 0) {
-               lprintf(5,
+               lprintf(CTDL_ERR,
                        "imap_load_msgids() can't run; no room selected\n");
                return;
        }
 
        imap_free_msgids();     /* If there was already a map, free it */
 
-       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
-                          imap_add_single_msgid, NULL);
+       /* Load the message list */
+       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
+       if (cdbfr != NULL) {
+               IMAP->msgids = malloc(cdbfr->len);
+               memcpy(IMAP->msgids, cdbfr->ptr, cdbfr->len);
+               IMAP->num_msgs = cdbfr->len / sizeof(long);
+               IMAP->num_alloc = cdbfr->len / sizeof(long);
+               cdb_free(cdbfr);
+       }
+
+       if (IMAP->num_msgs) {
+               IMAP->flags = malloc(IMAP->num_alloc * sizeof(long));
+               memset(IMAP->flags, 0, (IMAP->num_alloc * sizeof(long)) );
+       }
 
-       imap_set_seen_flags();
-       lprintf(9, "imap_load_msgids() mapped %d messages\n",
-               IMAP->num_msgs);
+       imap_set_seen_flags(0);
 }
 
 
@@ -201,26 +265,39 @@ void imap_rescan_msgids(void)
 
        int original_num_msgs = 0;
        long original_highest = 0L;
-       int i, j;
+       int i, j, jstart;
        int message_still_exists;
        struct cdbdata *cdbfr;
        long *msglist = NULL;
        int num_msgs = 0;
-
+       int num_recent = 0;
 
        if (IMAP->selected == 0) {
-               lprintf(5,
+               lprintf(CTDL_ERR,
                        "imap_load_msgids() can't run; no room selected\n");
                return;
        }
 
+       /*
+        * Check to see if the room's contents have changed.
+        * If not, we can avoid this rescan.
+        */
+       getroom(&CC->room, CC->room.QRname);
+       if (IMAP->last_mtime == CC->room.QRmtime) {     /* No changes! */
+               return;
+       }
+
        /* Load the *current* message list from disk, so we can compare it
         * to what we have in memory.
         */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr != NULL) {
-               msglist = mallok(cdbfr->len);
-               memcpy(msglist, cdbfr->ptr, cdbfr->len);
+               msglist = malloc(cdbfr->len);
+               if (msglist == NULL) {
+                       lprintf(CTDL_CRIT, "malloc() failed\n");
+                       abort();
+               }
+               memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
                num_msgs = cdbfr->len / sizeof(long);
                cdb_free(cdbfr);
        } else {
@@ -230,23 +307,28 @@ void imap_rescan_msgids(void)
        /*
         * Check to see if any of the messages we know about have been expunged
         */
-       if (IMAP->num_msgs > 0)
+       if (IMAP->num_msgs > 0) {
+               jstart = 0;
                for (i = 0; i < IMAP->num_msgs; ++i) {
 
                        message_still_exists = 0;
-                       if (num_msgs > 0)
-                               for (j = 0; j < num_msgs; ++j) {
+                       if (num_msgs > 0) {
+                               for (j = jstart; j < num_msgs; ++j) {
                                        if (msglist[j] == IMAP->msgids[i]) {
                                                message_still_exists = 1;
+                                               jstart = j;
+                                               break;
                                        }
                                }
+                       }
 
                        if (message_still_exists == 0) {
                                cprintf("* %d EXPUNGE\r\n", i + 1);
 
-                               /* Here's some nice stupid nonsense.  When a message
-                                * is expunged, we have to slide all the existing
-                                * messages up in the message array.
+                               /* Here's some nice stupid nonsense.  When a
+                                * message is expunged, we have to slide all
+                                * the existing messages up in the message
+                                * array.
                                 */
                                --IMAP->num_msgs;
                                memcpy(&IMAP->msgids[i],
@@ -262,6 +344,7 @@ void imap_rescan_msgids(void)
                        }
 
                }
+       }
 
        /*
         * Remember how many messages were here before we re-scanned.
@@ -276,23 +359,34 @@ void imap_rescan_msgids(void)
        /*
         * Now peruse the room for *new* messages only.
         */
-       if (num_msgs > 0)
+       if (num_msgs > 0) {
                for (j = 0; j < num_msgs; ++j) {
                        if (msglist[j] > original_highest) {
                                imap_add_single_msgid(msglist[j], NULL);
                        }
                }
-       imap_set_seen_flags();
+       }
+       imap_set_seen_flags(original_num_msgs);
 
        /*
         * If new messages have arrived, tell the client about them.
         */
        if (IMAP->num_msgs > original_num_msgs) {
+
+               for (j = 0; j < num_msgs; ++j) {
+                       if (IMAP->flags[j] & IMAP_RECENT) {
+                               ++num_recent;
+                       }
+               }
+
                cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
+               cprintf("* %d RECENT\r\n", num_recent);
        }
 
-       if (num_msgs != 0)
-               phree(msglist);
+       if (num_msgs != 0) {
+               free(msglist);
+       }
+       IMAP->last_mtime = CC->room.QRmtime;
 }
 
 
@@ -312,10 +406,80 @@ void imap_cleanup_function(void)
        if (CC->h_command_function != imap_command_loop)
                return;
 
-       lprintf(9, "Performing IMAP cleanup hook\n");
+       /* If there is a mailbox selected, auto-expunge it. */
+       if (IMAP->selected) {
+               imap_do_expunge();
+       }
+
+       lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
        imap_free_msgids();
        imap_free_transmitted_message();
-       lprintf(9, "Finished IMAP cleanup hook\n");
+
+       if (IMAP->cached_rfc822_data != NULL) {
+               free(IMAP->cached_rfc822_data);
+               IMAP->cached_rfc822_data = NULL;
+               IMAP->cached_rfc822_msgnum = (-1);
+               IMAP->cached_rfc822_withbody = 0;
+       }
+
+       if (IMAP->cached_body != NULL) {
+               free(IMAP->cached_body);
+               IMAP->cached_body = NULL;
+               IMAP->cached_body_len = 0;
+               IMAP->cached_bodymsgnum = (-1);
+       }
+
+       free(IMAP);
+       lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
+}
+
+
+/*
+ * Does the actual work of the CAPABILITY command (because we need to
+ * output this stuff in other places as well)
+ */
+void imap_output_capability_string(void) {
+       cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID ACL AUTH=PLAIN AUTH=LOGIN");
+
+#ifdef HAVE_OPENSSL
+       if (!CC->redirect_ssl) cprintf(" STARTTLS");
+#endif
+
+       /* We are building a partial implementation of METADATA for the sole purpose
+        * of interoperating with the ical/vcard version of the Bynari Insight Connector.
+        * If you were expecting something else, comment out one or both of these
+        * extension advertisements.
+        */
+       cprintf(" METADATA");
+       /* cprintf(" LIST-EXTENDED"); */
+}
+
+/*
+ * implements the CAPABILITY command
+ */
+void imap_capability(int num_parms, char *parms[])
+{
+       cprintf("* ");
+       imap_output_capability_string();
+       cprintf("\r\n");
+       cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
+}
+
+
+
+/*
+ * Implements the ID command (specified by RFC2971)
+ *
+ * We ignore the client-supplied information, and output a NIL response.
+ * Although this is technically a valid implementation of the extension, it
+ * is quite useless.  It exists only so that we may see which clients are
+ * making use of this extension.
+ * 
+ */
+void imap_id(int num_parms, char *parms[])
+{
+       cprintf("* ID NIL\r\n");
+       cprintf("%s OK ID completed\r\n", parms[0]);
 }
 
 
@@ -327,22 +491,44 @@ void imap_greeting(void)
 {
 
        strcpy(CC->cs_clientname, "IMAP session");
-       CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
+       IMAP = malloc(sizeof (struct citimap));
+       memset(IMAP, 0, sizeof(struct citimap));
        IMAP->authstate = imap_as_normal;
+       IMAP->cached_rfc822_data = NULL;
+       IMAP->cached_rfc822_msgnum = (-1);
+       IMAP->cached_rfc822_withbody = 0;
 
-       cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
-               config.c_fqdn);
+       cprintf("* OK [");
+       imap_output_capability_string();
+       cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
 }
 
+/*
+ * IMAPS is just like IMAP, except it goes crypto right away.
+ */
+#ifdef HAVE_OPENSSL
+void imaps_greeting(void) {
+       CtdlStartTLS(NULL, NULL, NULL);
+       imap_greeting();
+}
+#endif
+
 
 /*
  * implements the LOGIN command (ordinary username/password login)
  */
 void imap_login(int num_parms, char *parms[])
 {
-       if (CtdlLoginExistingUser(parms[2]) == login_ok) {
+       if (num_parms != 4) {
+               cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
+               return;
+       }
+
+       if (CtdlLoginExistingUser(NULL, parms[2]) == login_ok) {
                if (CtdlTryPassword(parms[3]) == pass_ok) {
-                       cprintf("%s OK login successful\r\n", parms[0]);
+                       cprintf("%s OK [", parms[0]);
+                       imap_output_capability_string();
+                       cprintf("] Hello, %s\r\n", CC->user.fullname);
                        return;
                }
        }
@@ -377,18 +563,58 @@ void imap_authenticate(int num_parms, char *parms[])
                return;
        }
 
+       if (!strcasecmp(parms[2], "PLAIN")) {
+               // CtdlEncodeBase64(buf, "Username:", 9);
+               // cprintf("+ %s\r\n", buf);
+               cprintf("+ \r\n");
+               IMAP->authstate = imap_as_expecting_plainauth;
+               strcpy(IMAP->authseq, parms[0]);
+               return;
+       }
+
        else {
                cprintf("%s NO AUTHENTICATE %s failed\r\n",
                        parms[0], parms[1]);
        }
 }
 
+void imap_auth_plain(char *cmd)
+{
+       char decoded_authstring[1024];
+       char ident[256];
+       char user[256];
+       char pass[256];
+       int result;
+
+       CtdlDecodeBase64(decoded_authstring, cmd, strlen(cmd));
+       safestrncpy(ident, decoded_authstring, sizeof ident);
+       safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
+       safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
+
+       IMAP->authstate = imap_as_normal;
+
+       if (strlen(ident) > 0) {
+               result = CtdlLoginExistingUser(user, ident);
+       }
+       else {
+               result = CtdlLoginExistingUser(NULL, user);
+       }
+
+       if (result == login_ok) {
+               if (CtdlTryPassword(pass) == pass_ok) {
+                       cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
+                       return;
+               }
+       }
+       cprintf("%s NO authentication failed\r\n", IMAP->authseq);
+}
+
 void imap_auth_login_user(char *cmd)
 {
        char buf[SIZ];
 
        CtdlDecodeBase64(buf, cmd, SIZ);
-       CtdlLoginExistingUser(buf);
+       CtdlLoginExistingUser(NULL, buf);
        CtdlEncodeBase64(buf, "Password:", 9);
        cprintf("+ %s\r\n", buf);
        IMAP->authstate = imap_as_expecting_password;
@@ -401,8 +627,7 @@ void imap_auth_login_pass(char *cmd)
 
        CtdlDecodeBase64(buf, cmd, SIZ);
        if (CtdlTryPassword(buf) == pass_ok) {
-               cprintf("%s OK authentication succeeded\r\n",
-                       IMAP->authseq);
+               cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
        } else {
                cprintf("%s NO authentication failed\r\n", IMAP->authseq);
        }
@@ -412,161 +637,29 @@ void imap_auth_login_pass(char *cmd)
 
 
 /*
- * implements the CAPABILITY command
+ * implements the STARTTLS command (Citadel API version)
  */
-void imap_capability(int num_parms, char *parms[])
-{
-       cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
-
 #ifdef HAVE_OPENSSL
-       cprintf(" STARTTLS");
-#endif
-
-       cprintf("\r\n");
-       cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
-}
-
-
-/*
- * implements the STARTTLS command (lifted-from-Cyrus version)
- */
-#ifdef HAVE_OPENSSX
 void imap_starttls(int num_parms, char *parms[])
 {
-       int sts;
-       SSL_CIPHER *cipher;
-       const char *tls_protocol = NULL;
-       const char *tls_cipher_name = NULL;
-       int tls_cipher_usebits = 0;
-       int tls_cipher_algbits = 0;
-       SSL *tls_conn;
-       int r = 0;
-
-       lprintf(9, "imap_starttls() called\n");
-       tls_conn = (SSL *) SSL_new(ssl_ctx);
-       if (tls_conn == NULL) {
-               CC->ssl = NULL;
-               r = -1;
-               goto done;
-       }
-       SSL_clear(tls_conn);
-
-       /* set the file descriptors for SSL to use */
-       if (SSL_set_fd(tls_conn, CC->client_socket) == 0) {
-               r = -1;
-               goto done;
-       }
-
-       /*
-        * This is the actual handshake routine. It will do all the negotiations
-        * and will check the client cert etc.
-        */
-       SSL_set_accept_state(tls_conn);
-
-       cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
-       if ((sts = SSL_accept(tls_conn)) <= 0) {
-               SSL_SESSION *session = SSL_get_session(tls_conn);
-               if (session) {
-                       SSL_CTX_remove_session(ssl_ctx, session);
-               }
-               r = -1;
-               goto done;
-       }
-
-       tls_protocol = SSL_get_version(tls_conn);
-       cipher = SSL_get_current_cipher(tls_conn);
-       tls_cipher_name = SSL_CIPHER_get_name(cipher);
-       tls_cipher_usebits =
-               SSL_CIPHER_get_bits(cipher, &tls_cipher_algbits);
-
-       lprintf(9, "starttls: %s with cipher %s (%d/%d bits %s)\n",
-               tls_protocol, tls_cipher_name,
-               tls_cipher_usebits, tls_cipher_algbits,
-               SSL_session_reused(tls_conn) ? "reused" : "new");
-
-done:
-       if (r && tls_conn) {
-               /* error; clean up */
-               SSL_free(tls_conn);
-               tls_conn = NULL;
-               cprintf("%s NO negotiation failed\r\n", parms[0]);
-       } else {
-               CC->ssl = tls_conn;
-               CC->redirect_ssl = 1;
-       }
+       char ok_response[SIZ];
+       char nosup_response[SIZ];
+       char error_response[SIZ];
+
+       sprintf(ok_response,
+               "%s OK begin TLS negotiation now\r\n",
+               parms[0]);
+       sprintf(nosup_response,
+               "%s NO TLS not supported here\r\n",
+               parms[0]);
+       sprintf(error_response,
+               "%s BAD Internal error\r\n",
+               parms[0]);
+       CtdlStartTLS(ok_response, nosup_response, error_response);
 }
-
 #endif
 
 
-/*
- * implements the STARTTLS command (original version)
- */
-#ifdef HAVE_OPENSSL
-void imap_starttls(int num_parms, char *parms[])
-{
-       int retval, bits, alg_bits;
-       long ssloptions;
-
-       if (!ssl_ctx) {
-               cprintf("%s NO No SSL_CTX available\r\n", parms[0]);
-               return;
-       }
-       if (!(CC->ssl = SSL_new(ssl_ctx))) {
-               lprintf(2, "SSL_new failed: %s\n",
-                       ERR_reason_error_string(ERR_peek_error()));
-               cprintf("%s NO SSL_new: %s\r\n", parms[0],
-                       ERR_reason_error_string(ERR_get_error()));
-               return;
-       }
-
-       /* Set the options */
-       ssloptions = SSL_get_options(CC->ssl);
-       ssloptions |= SSL_OP_ALL;       /* Work around all known bugs */
-       ssloptions |= SSL_OP_NO_SSLv2;
-       ssloptions |= SSL_OP_NO_SSLv3;
-       SSL_set_options(CC->ssl, ssloptions);
-
-       if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
-               lprintf(2, "SSL_set_fd failed: %s\n",
-                       ERR_reason_error_string(ERR_peek_error()));
-               SSL_free(CC->ssl);
-               CC->ssl = NULL;
-               cprintf("%s NO SSL_set_fd: %s\r\n", parms[0],
-                       ERR_reason_error_string(ERR_get_error()));
-               return;
-       }
-       cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
-       retval = SSL_accept(CC->ssl);
-       if (retval < 1) {
-               /*
-                * Can't notify the client of an error here; they will
-                * discover the problem at the SSL layer and should
-                * revert to unencrypted communications.
-                */
-               long errval;
-
-               errval = SSL_get_error(CC->ssl, retval);
-               lprintf(2, "SSL_accept failed: %s\n",
-                       ERR_reason_error_string(ERR_get_error()));
-               SSL_free(CC->ssl);
-               CC->ssl = NULL;
-               return;
-       }
-       BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
-       bits =
-           SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
-                               &alg_bits);
-       lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
-               SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
-               SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
-               bits, alg_bits);
-       CC->redirect_ssl = 1;
-}
-#endif
-
-
-
 /*
  * implements the SELECT command
  */
@@ -608,7 +701,7 @@ void imap_select(int num_parms, char *parms[])
        /* If the room exists, check security/access */
        if (c == 0) {
                /* See if there is an existing user/room relationship */
-               ra = CtdlRoomAccess(&QRscratch, &CC->user);
+               CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
 
                /* normal clients have to pass through security */
                if (ra & UA_KNOWN) {
@@ -620,7 +713,6 @@ void imap_select(int num_parms, char *parms[])
        if (!ok) {
                cprintf("%s NO ... no such room, or access denied\r\n",
                        parms[0]);
-               /* IMAP->selected = 0; */
                return;
        }
 
@@ -642,19 +734,22 @@ void imap_select(int num_parms, char *parms[])
        }
 
        imap_load_msgids();
+       IMAP->last_mtime = CC->room.QRmtime;
 
        cprintf("* %d EXISTS\r\n", msgs);
        cprintf("* %d RECENT\r\n", new);
 
+       cprintf("* OK [UIDVALIDITY 1] UID validity status\r\n");
+       cprintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
+
        /* Note that \Deleted is a valid flag, but not a permanent flag,
         * because we don't maintain its state across sessions.  Citadel
         * automatically expunges mailboxes when they are de-selected.
         */
        cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
-       cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
+       cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] "
                "permanent flags\r\n");
 
-       cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
        cprintf("%s OK [%s] %s completed\r\n",
                parms[0],
                (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
@@ -663,35 +758,40 @@ void imap_select(int num_parms, char *parms[])
 
 
 /*
- * does the real work for expunge
+ * Does the real work for expunge.
  */
 int imap_do_expunge(void)
 {
        int i;
        int num_expunged = 0;
+       long *delmsgs = NULL;
+       int num_delmsgs = 0;
 
-       lprintf(9, "imap_do_expunge() called\n");
-       if (IMAP->selected == 0)
+       lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
+       if (IMAP->selected == 0) {
                return (0);
+       }
 
-       if (IMAP->num_msgs > 0)
+       if (IMAP->num_msgs > 0) {
+               delmsgs = malloc(IMAP->num_msgs * sizeof(long));
                for (i = 0; i < IMAP->num_msgs; ++i) {
                        if (IMAP->flags[i] & IMAP_DELETED) {
-                               CtdlDeleteMessages(CC->room.QRname,
-                                                  IMAP->msgids[i], "");
-                               ++num_expunged;
-                               lprintf(9, "%ld ... deleted\n",
-                                       IMAP->msgids[i]);
-                       } else {
-                               lprintf(9, "%ld ... not deleted\n",
-                                       IMAP->msgids[i]);
+                               delmsgs[num_delmsgs++] = IMAP->msgids[i];
                        }
                }
+               if (num_delmsgs > 0) {
+                       CtdlDeleteMessages(CC->room.QRname, delmsgs, num_delmsgs, "");
+               }
+               num_expunged += num_delmsgs;
+               free(delmsgs);
+       }
 
        if (num_expunged > 0) {
                imap_rescan_msgids();
        }
 
+       lprintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n",
+               num_expunged, CC->room.QRname);
        return (num_expunged);
 }
 
@@ -715,7 +815,9 @@ void imap_close(int num_parms, char *parms[])
 {
 
        /* Yes, we always expunge on close. */
-       imap_do_expunge();
+       if (IMAP->selected) {
+               imap_do_expunge();
+       }
 
        IMAP->selected = 0;
        IMAP->readonly = 0;
@@ -724,131 +826,42 @@ void imap_close(int num_parms, char *parms[])
 }
 
 
-
-
 /*
- * Used by LIST and LSUB to show the floors in the listing
+ * Implements the NAMESPACE command.
  */
-void imap_list_floors(char *cmd, char *pattern)
+void imap_namespace(int num_parms, char *parms[])
 {
        int i;
        struct floor *fl;
-
-       for (i = 0; i < MAXFLOORS; ++i) {
-               fl = cgetfloor(i);
-               if (fl->f_flags & F_INUSE) {
-                       if (imap_mailbox_matches_pattern
-                           (pattern, fl->f_name)) {
-                               cprintf("* %s (\\NoSelect) \"|\" ", cmd);
-                               imap_strout(fl->f_name);
-                               cprintf("\r\n");
-                       }
-               }
-       }
-}
-
-
-
-/*
- * Back end for imap_lsub()
- *
- * IMAP "subscribed folder" is equivocated to Citadel "known rooms."  This
- * may or may not be the desired behavior in the future.
- */
-void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
-{
+       int floors = 0;
        char buf[SIZ];
-       int ra;
-       char *pattern;
-
-       pattern = (char *) data;
-
-       /* Only list rooms to which the user has access!! */
-       ra = CtdlRoomAccess(qrbuf, &CC->user);
-       if (ra & UA_KNOWN) {
-               imap_mailboxname(buf, sizeof buf, qrbuf);
-               if (imap_mailbox_matches_pattern(pattern, buf)) {
-                       cprintf("* LSUB () \"|\" ");
-                       imap_strout(buf);
-                       cprintf("\r\n");
-               }
-       }
-}
-
-
-/*
- * Implements the LSUB command
- */
-void imap_lsub(int num_parms, char *parms[])
-{
-       char pattern[SIZ];
-       if (num_parms < 4) {
-               cprintf("%s BAD arguments invalid\r\n", parms[0]);
-               return;
-       }
-       snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
 
-       if (strlen(parms[3]) == 0) {
-               cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
-       }
+       cprintf("* NAMESPACE ");
 
-       else {
-               imap_list_floors("LSUB", pattern);
-               ForEachRoom(imap_lsub_listroom, pattern);
-       }
+       /* All personal folders are subordinate to INBOX. */
+       cprintf("((\"INBOX/\" \"/\")) ");
 
-       cprintf("%s OK LSUB completed\r\n", parms[0]);
-}
-
-
-
-/*
- * Back end for imap_list()
- */
-void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
-{
-       char buf[SIZ];
-       int ra;
-       char *pattern;
+       /* Other users' folders ... coming soon! FIXME */
+       cprintf("NIL ");
 
-       pattern = (char *) data;
-
-       /* Only list rooms to which the user has access!! */
-       ra = CtdlRoomAccess(qrbuf, &CC->user);
-       if ((ra & UA_KNOWN)
-           || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
-               imap_mailboxname(buf, sizeof buf, qrbuf);
-               if (imap_mailbox_matches_pattern(pattern, buf)) {
-                       cprintf("* LIST () \"|\" ");
+       /* Show all floors as shared namespaces.  Neato! */
+       cprintf("(");
+       for (i = 0; i < MAXFLOORS; ++i) {
+               fl = cgetfloor(i);
+               if (fl->f_flags & F_INUSE) {
+                       if (floors > 0) cprintf(" ");
+                       cprintf("(");
+                       sprintf(buf, "%s/", fl->f_name);
                        imap_strout(buf);
-                       cprintf("\r\n");
+                       cprintf(" \"/\")");
+                       ++floors;
                }
        }
-}
-
-
-/*
- * Implements the LIST command
- */
-void imap_list(int num_parms, char *parms[])
-{
-       char pattern[SIZ];
-       if (num_parms < 4) {
-               cprintf("%s BAD arguments invalid\r\n", parms[0]);
-               return;
-       }
-       snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
-
-       if (strlen(parms[3]) == 0) {
-               cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
-       }
-
-       else {
-               imap_list_floors("LIST", pattern);
-               ForEachRoom(imap_list_listroom, pattern);
-       }
+       cprintf(")");
 
-       cprintf("%s OK LIST completed\r\n", parms[0]);
+       /* Wind it up with a newline and a completion message. */
+       cprintf("\r\n");
+       cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
 }
 
 
@@ -863,11 +876,13 @@ void imap_create(int num_parms, char *parms[])
        char roomname[ROOMNAMELEN];
        int floornum;
        int flags;
-       int newroomtype;
+       int newroomtype = 0;
+       int newroomview = 0;
 
        if (strchr(parms[2], '\\') != NULL) {
                cprintf("%s NO Invalid character in folder name\r\n",
                        parms[0]);
+               lprintf(CTDL_DEBUG, "invalid character in folder name\n");
                return;
        }
 
@@ -875,35 +890,47 @@ void imap_create(int num_parms, char *parms[])
        if (ret < 0) {
                cprintf("%s NO Invalid mailbox name or location\r\n",
                        parms[0]);
+               lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
                return;
        }
        floornum = (ret & 0x00ff);      /* lower 8 bits = floor number */
        flags = (ret & 0xff00); /* upper 8 bits = flags        */
 
        if (flags & IR_MAILBOX) {
-               newroomtype = 4;        /* private mailbox */
+               if (strncasecmp(parms[2], "INBOX/", 6)) {
+                       cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
+                       lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
+                       return;
+               }
+       }
+
+       if (flags & IR_MAILBOX) {
+               newroomtype = 4;                /* private mailbox */
+               newroomview = VIEW_MAILBOX;
        } else {
-               newroomtype = 0;        /* public folder */
+               newroomtype = 0;                /* public folder */
+               newroomview = VIEW_BBS;
        }
 
-       lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
+       lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
                roomname, floornum, newroomtype);
 
-       ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
+       ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview);
        if (ret == 0) {
-               cprintf
-                   ("%s NO Mailbox already exists, or create failed\r\n",
-                    parms[0]);
+               /*** DO NOT CHANGE THIS ERROR MESSAGE IN ANY WAY!  BYNARI CONNECTOR DEPENDS ON IT! ***/
+               cprintf("%s NO Mailbox already exists, or create failed\r\n", parms[0]);
        } else {
                cprintf("%s OK CREATE completed\r\n", parms[0]);
        }
+       lprintf(CTDL_DEBUG, "imap_create() completed\n");
 }
 
 
 /*
- * Locate a room by its IMAP folder name, and check access to it
+ * Locate a room by its IMAP folder name, and check access to it.
+ * If zapped_ok is nonzero, we can also look for the room in the zapped list.
  */
-int imap_grabroom(char *returned_roomname, char *foldername)
+int imap_grabroom(char *returned_roomname, char *foldername, int zapped_ok)
 {
        int ret;
        char augmented_roomname[ROOMNAMELEN];
@@ -933,12 +960,15 @@ int imap_grabroom(char *returned_roomname, char *foldername)
        /* If the room exists, check security/access */
        if (c == 0) {
                /* See if there is an existing user/room relationship */
-               ra = CtdlRoomAccess(&QRscratch, &CC->user);
+               CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
 
                /* normal clients have to pass through security */
                if (ra & UA_KNOWN) {
                        ok = 1;
                }
+               if ((zapped_ok) && (ra & UA_ZAPPED)) {
+                       ok = 1;
+               }
        }
 
        /* Fail here if no such room */
@@ -964,7 +994,7 @@ void imap_status(int num_parms, char *parms[])
        char savedroom[ROOMNAMELEN];
        int msgs, new;
 
-       ret = imap_grabroom(roomname, parms[2]);
+       ret = imap_grabroom(roomname, parms[2], 0);
        if (ret != 0) {
                cprintf
                    ("%s NO Invalid mailbox name or location, or access denied\r\n",
@@ -992,7 +1022,7 @@ void imap_status(int num_parms, char *parms[])
        cprintf("* STATUS ");
        imap_strout(buf);
        cprintf(" (MESSAGES %d ", msgs);
-       cprintf("RECENT 0 ");   /* FIXME we need to implement this */
+       cprintf("RECENT %d ", new);     /* Initially, new==recent */
        cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
        cprintf("UNSEEN %d)\r\n", new);
 
@@ -1023,11 +1053,13 @@ void imap_subscribe(int num_parms, char *parms[])
        char savedroom[ROOMNAMELEN];
        int msgs, new;
 
-       ret = imap_grabroom(roomname, parms[2]);
+       ret = imap_grabroom(roomname, parms[2], 1);
        if (ret != 0) {
-               cprintf
-                   ("%s NO Invalid mailbox name or location, or access denied\r\n",
-                    parms[0]);
+               cprintf(
+                       "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
+                       parms[0],
+                       ret
+               );
                return;
        }
 
@@ -1064,7 +1096,7 @@ void imap_unsubscribe(int num_parms, char *parms[])
        char savedroom[ROOMNAMELEN];
        int msgs, new;
 
-       ret = imap_grabroom(roomname, parms[2]);
+       ret = imap_grabroom(roomname, parms[2], 0);
        if (ret != 0) {
                cprintf
                    ("%s NO Invalid mailbox name or location, or access denied\r\n",
@@ -1113,7 +1145,7 @@ void imap_delete(int num_parms, char *parms[])
        char savedroom[ROOMNAMELEN];
        int msgs, new;
 
-       ret = imap_grabroom(roomname, parms[2]);
+       ret = imap_grabroom(roomname, parms[2], 1);
        if (ret != 0) {
                cprintf("%s NO Invalid mailbox name, or access denied\r\n",
                        parms[0]);
@@ -1134,8 +1166,8 @@ void imap_delete(int num_parms, char *parms[])
         * Now delete the room.
         */
        if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
+               schedule_room_for_deletion(&CC->room);
                cprintf("%s OK DELETE completed\r\n", parms[0]);
-               delete_room(&CC->room);
        } else {
                cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
        }
@@ -1168,9 +1200,9 @@ void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
        /* Rename subfolders */
        if ((!strncasecmp(foldername, irlparms->oldname,
                          strlen(irlparms->oldname))
-            && (foldername[strlen(irlparms->oldname)] == '|'))) {
+            && (foldername[strlen(irlparms->oldname)] == '/'))) {
 
-               sprintf(newfoldername, "%s|%s",
+               sprintf(newfoldername, "%s/%s",
                        irlparms->newname,
                        &foldername[strlen(irlparms->oldname) + 1]
                    );
@@ -1179,7 +1211,7 @@ void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
                                         sizeof newroomname,
                                         newfoldername) & 0xFF;
 
-               irlp = (struct irl *) mallok(sizeof(struct irl));
+               irlp = (struct irl *) malloc(sizeof(struct irl));
                strcpy(irlp->irl_newroom, newroomname);
                strcpy(irlp->irl_oldroom, qrbuf->QRname);
                irlp->irl_newfloor = newfloor;
@@ -1250,7 +1282,7 @@ void imap_rename(int num_parms, char *parms[])
         * (already did that) and create a new inbox.
         */
        if (!strcasecmp(parms[2], "INBOX")) {
-               create_room(MAILROOM, 4, "", 0, 1, 0);
+               create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
        }
 
        /* Otherwise, do the subfolders.  Build a list of rooms to rename... */
@@ -1267,12 +1299,11 @@ void imap_rename(int num_parms, char *parms[])
                                           irl->irl_newfloor);
                        if (r != crr_ok) {
                                /* FIXME handle error returns better */
-                               lprintf(5, "CtdlRenameRoom() error %d\n",
-                                       r);
+                               lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n", r);
                        }
                        irlp = irl;
                        irl = irl->next;
-                       phree(irlp);
+                       free(irlp);
                }
        }
 
@@ -1290,20 +1321,35 @@ void imap_command_loop(void)
        char cmdbuf[SIZ];
        char *parms[SIZ];
        int num_parms;
+       struct timeval tv1, tv2;
+       suseconds_t total_time = 0;
 
-       time(&CC->lastcmd);
+       gettimeofday(&tv1, NULL);
+       CC->lastcmd = time(NULL);
        memset(cmdbuf, 0, sizeof cmdbuf);       /* Clear it, just in case */
-       if (client_gets(cmdbuf) < 1) {
-               lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
+       flush_output();
+       if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
+               lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
                CC->kill_me = 1;
                return;
        }
 
-       lprintf(5, "IMAP: %s\r\n", cmdbuf);
+       if (IMAP->authstate == imap_as_expecting_password) {
+               lprintf(CTDL_INFO, "IMAP: <password>\n");
+       }
+       else if (IMAP->authstate == imap_as_expecting_plainauth) {
+               lprintf(CTDL_INFO, "IMAP: <plain_auth>\n");
+       }
+       else if (bmstrcasestr(cmdbuf, " LOGIN ")) {
+               lprintf(CTDL_INFO, "IMAP: LOGIN...\n");
+       }
+       else {
+               lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
+       }
+
        while (strlen(cmdbuf) < 5)
                strcat(cmdbuf, " ");
 
-
        /* strip off l/t whitespace and CRLF */
        if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
                cmdbuf[strlen(cmdbuf) - 1] = 0;
@@ -1316,16 +1362,19 @@ void imap_command_loop(void)
                imap_auth_login_user(cmdbuf);
                return;
        }
+       if (IMAP->authstate == imap_as_expecting_plainauth) {
+               imap_auth_plain(cmdbuf);
+               return;
+       }
        if (IMAP->authstate == imap_as_expecting_password) {
                imap_auth_login_pass(cmdbuf);
                return;
        }
 
-
        /* Ok, at this point we're in normal command mode.  The first thing
         * we do is print any incoming pages (yeah! we really do!)
         */
-       imap_print_express_messages();
+       imap_print_instant_messages();
 
        /*
         * Before processing the command that was just entered... if we happen
@@ -1349,14 +1398,21 @@ void imap_command_loop(void)
 
        else if ((!strcasecmp(parms[1], "NOOP"))
                 || (!strcasecmp(parms[1], "CHECK"))) {
-               cprintf("%s OK This command successfully did nothing.\r\n",
+               cprintf("%s OK No operation\r\n",
                        parms[0]);
        }
 
+       else if (!strcasecmp(parms[1], "ID")) {
+               imap_id(num_parms, parms);
+       }
+
+
        else if (!strcasecmp(parms[1], "LOGOUT")) {
-               imap_do_expunge();      /* yes, we auto-expunge */
+               if (IMAP->selected) {
+                       imap_do_expunge();      /* yes, we auto-expunge */
+               }
                cprintf("* BYE %s logging out\r\n", config.c_fqdn);
-               cprintf("%s OK thank you for using Citadel IMAP\r\n",
+               cprintf("%s OK Citadel IMAP session ended.\r\n",
                        parms[0]);
                CC->kill_me = 1;
                return;
@@ -1378,7 +1434,6 @@ void imap_command_loop(void)
                imap_starttls(num_parms, parms);
        }
 #endif
-
        else if (!CC->logged_in) {
                cprintf("%s BAD Not logged in.\r\n", parms[0]);
        }
@@ -1394,7 +1449,7 @@ void imap_command_loop(void)
        }
 
        else if (!strcasecmp(parms[1], "LSUB")) {
-               imap_lsub(num_parms, parms);
+               imap_list(num_parms, parms);
        }
 
        else if (!strcasecmp(parms[1], "LIST")) {
@@ -1429,6 +1484,38 @@ void imap_command_loop(void)
                imap_append(num_parms, parms);
        }
 
+       else if (!strcasecmp(parms[1], "NAMESPACE")) {
+               imap_namespace(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "SETACL")) {
+               imap_setacl(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "DELETEACL")) {
+               imap_deleteacl(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "GETACL")) {
+               imap_getacl(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "LISTRIGHTS")) {
+               imap_listrights(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "MYRIGHTS")) {
+               imap_myrights(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "GETMETADATA")) {
+               imap_getmetadata(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "SETMETADATA")) {
+               imap_setmetadata(num_parms, parms);
+       }
+
        else if (IMAP->selected == 0) {
                cprintf("%s BAD no folder selected\r\n", parms[0]);
        }
@@ -1489,17 +1576,29 @@ void imap_command_loop(void)
 
        /* If the client transmitted a message we can free it now */
        imap_free_transmitted_message();
-}
 
+       gettimeofday(&tv2, NULL);
+       total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
+       lprintf(CTDL_DEBUG, "IMAP command completed in %ld.%ld seconds\n",
+               (total_time / 1000000),
+               (total_time % 1000000)
+       );
+}
 
 
 /*
  * This function is called to register the IMAP extension with Citadel.
  */
-char *serv_imap_init(void)
+CTDL_MODULE_INIT(imap)
 {
        CtdlRegisterServiceHook(config.c_imap_port,
-                               NULL, imap_greeting, imap_command_loop);
+                               NULL, imap_greeting, imap_command_loop, NULL);
+#ifdef HAVE_OPENSSL
+       CtdlRegisterServiceHook(config.c_imaps_port,
+                               NULL, imaps_greeting, imap_command_loop, NULL);
+#endif
        CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
+
+       /* return our Subversion id for the Log */
        return "$Id$";
 }