stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / imap / serv_imap.c
index e9f566375302109ffb6091f8032377e901a776bc..1609f8ee5b3a819da4ddaa91ddb20288b44c51c7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * IMAP server for the Citadel system
  *
- * Copyright (C) 2000-2011 by Art Cancro and others.
+ * Copyright (C) 2000-2021 by Art Cancro and others.
  * This code is released under the terms of the GNU General Public License.
  *
  * WARNING: the IMAP protocol is badly designed.  No implementation of it
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "sysdep.h"
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include <sys/wait.h>
 #include <ctype.h>
 #include <string.h>
@@ -69,7 +54,6 @@
 #include "imap_misc.h"
 
 #include "ctdl_module.h"
-
 HashList *ImapCmds = NULL;
 void registerImapCMD(const char *First, long FLen, 
                     const char *Second, long SLen,
@@ -95,15 +79,12 @@ void registerImapCMD(const char *First, long FLen,
        }
 }
 
-void imap_cleanup(void)
-{
-       DeleteHash(&ImapCmds);
-}
 
 const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params)
 {
+       struct CitContext *CCC = CC;
        void *v;
-       citimap *Imap = IMAP;
+       citimap *Imap = CCCIMAP;
 
        if (num_parms < 1)
                return NULL;
@@ -112,8 +93,7 @@ const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params)
        StrBufPlain(Imap->Reply, CKEY(Params[1]));
        StrBufUpCase(Imap->Reply);
 
-       syslog(LOG_DEBUG, "---- Looking up [%s] -----", 
-                     ChrPtr(Imap->Reply));
+       syslog(LOG_DEBUG, "---- Looking up [%s] -----", ChrPtr(Imap->Reply));
        if (GetHash(ImapCmds, SKEY(Imap->Reply), &v))
        {
                syslog(LOG_DEBUG, "Found."); 
@@ -128,8 +108,7 @@ const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params)
                return NULL;
        }
        
-       syslog(LOG_DEBUG, "---- Looking up [%s] -----", 
-                     ChrPtr(Imap->Reply));
+       syslog(LOG_DEBUG, "---- Looking up [%s] -----", ChrPtr(Imap->Reply));
        StrBufAppendBufPlain(Imap->Reply, CKEY(Params[2]), 0);
        StrBufUpCase(Imap->Reply);
        if (GetHash(ImapCmds, SKEY(Imap->Reply), &v))
@@ -152,11 +131,13 @@ struct irl {
 };
 
 /* Data which is passed between imap_rename() and imap_rename_backend() */
-struct irlparms {
-       char *oldname;
-       char *newname;
+typedef struct __irlparms {
+       const char *oldname;
+       long oldnamelen;
+       const char *newname;
+       long newnamelen;
        struct irl **irl;
-};
+}irlparms;
 
 
 /*
@@ -290,7 +271,7 @@ void imap_add_single_msgid(long msgnum, void *userdata)
        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->flags = realloc(Imap->flags, (Imap->num_alloc * sizeof(unsigned int)) );
        }
        Imap->msgids[Imap->num_msgs - 1] = msgnum;
        Imap->flags[Imap->num_msgs - 1] = 0;
@@ -303,8 +284,9 @@ void imap_add_single_msgid(long msgnum, void *userdata)
  */
 void imap_load_msgids(void)
 {
+       struct CitContext *CCC = CC;
        struct cdbdata *cdbfr;
-       citimap *Imap = IMAP;
+       citimap *Imap = CCCIMAP;
 
        if (Imap->selected == 0) {
                syslog(LOG_ERR, "imap_load_msgids() can't run; no room selected");
@@ -316,16 +298,17 @@ void imap_load_msgids(void)
        /* 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->msgids = (long*)cdbfr->ptr;
                Imap->num_msgs = cdbfr->len / sizeof(long);
                Imap->num_alloc = cdbfr->len / sizeof(long);
+               cdbfr->ptr = NULL;
+               cdbfr->len = 0;
                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->flags = malloc(Imap->num_alloc * sizeof(unsigned int));
+               memset(Imap->flags, 0, (Imap->num_alloc * sizeof(unsigned int)) );
        }
 
        imap_set_seen_flags(0);
@@ -337,7 +320,8 @@ void imap_load_msgids(void)
  */
 void imap_rescan_msgids(void)
 {
-       citimap *Imap = IMAP;
+       struct CitContext *CCC = CC;
+       citimap *Imap = CCCIMAP;
        int original_num_msgs = 0;
        long original_highest = 0L;
        int i, j, jstart;
@@ -366,14 +350,10 @@ void imap_rescan_msgids(void)
         */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr != NULL) {
-               msglist = malloc(cdbfr->len);
-               if (msglist == NULL) {
-                       syslog(LOG_CRIT, "IMAP: malloc() failed");
-                       CC->kill_me = KILLME_MALLOC_FAILED;
-                       return;
-               }
-               memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
+               msglist = (long*)cdbfr->ptr;
+               cdbfr->ptr = NULL;
                num_msgs = cdbfr->len / sizeof(long);
+               cdbfr->len = 0;
                cdb_free(cdbfr);
        } else {
                num_msgs = 0;
@@ -406,15 +386,14 @@ void imap_rescan_msgids(void)
                                 * array.
                                 */
                                --Imap->num_msgs;
-                               memcpy(&Imap->msgids[i],
-                                      &Imap->msgids[i + 1],
-                                      (sizeof(long) *
-                                       (Imap->num_msgs - i)));
-                               memcpy(&Imap->flags[i],
-                                      &Imap->flags[i + 1],
-                                      (sizeof(long) *
-                                       (Imap->num_msgs - i)));
-
+                               memmove(&Imap->msgids[i],
+                                       &Imap->msgids[i + 1],
+                                       (sizeof(long) *
+                                        (Imap->num_msgs - i)));
+                               memmove(&Imap->flags[i],
+                                       &Imap->flags[i + 1],
+                                       (sizeof(unsigned int) *
+                                        (Imap->num_msgs - i)));
                                --i;
                        }
 
@@ -460,7 +439,7 @@ void imap_rescan_msgids(void)
                IAPrintf("* %d RECENT\r\n", num_recent);
        }
 
-       if (num_msgs != 0) {
+       if (msglist != NULL) {
                free(msglist);
        }
        Imap->last_mtime = CC->room.QRmtime;
@@ -473,7 +452,8 @@ void imap_rescan_msgids(void)
  */
 void imap_cleanup_function(void)
 {
-       citimap *Imap = IMAP;
+       struct CitContext *CCC = CC;
+       citimap *Imap = CCCIMAP;
 
        /* Don't do this stuff if this is not a Imap session! */
        if (CC->h_command_function != imap_command_loop)
@@ -597,7 +577,7 @@ void imap_greeting(void)
 
        IAPuts("* OK [");
        imap_output_capability_string();
-       IAPrintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
+       IAPrintf("] %s IMAP4rev1 %s ready\r\n", CtdlGetConfigStr("c_fqdn"), CITADEL);
        IUnbuffer();
 }
 
@@ -633,7 +613,7 @@ void imap_login(int num_parms, ConstStr *Params)
                        return;
                }
        case 4:
-               if (CtdlLoginExistingUser(NULL, Params[2].Key) == login_ok) {
+               if (CtdlLoginExistingUser(Params[2].Key) == login_ok) {
                        if (CtdlTryPassword(Params[3].Key, Params[3].len) == pass_ok) {
                                /* hm, thats not doable by IReply :-( */
                                IAPrintf("%s OK [", Params[0].Key);
@@ -643,12 +623,13 @@ void imap_login(int num_parms, ConstStr *Params)
                        }
                        else
                        {
-                               IReplyPrintf("NO AUTHENTICATE %s failed",
-                                            Params[3].Key);
+                               IReplyPrintf("NO AUTHENTICATE %s failed", Params[3].Key);
+                               return;
                        }
                }
 
                IReply("BAD Login incorrect");
+               return;
        default:
                IReply("BAD incorrect number of parameters");
                return;
@@ -675,7 +656,11 @@ void imap_authenticate(int num_parms, ConstStr *Params)
        }
 
        if (!strcasecmp(Params[2].Key, "LOGIN")) {
-               CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
+               size_t len = CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
+               if (UsrBuf[len - 1] == '\n') {
+                       UsrBuf[len - 1] = '\0';
+               }
+
                IAPrintf("+ %s\r\n", UsrBuf);
                IMAP->authstate = imap_as_expecting_username;
                strcpy(IMAP->authseq, Params[0].Key);
@@ -683,7 +668,10 @@ void imap_authenticate(int num_parms, ConstStr *Params)
        }
 
        if (!strcasecmp(Params[2].Key, "PLAIN")) {
-               // CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
+               // size_t len = CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
+               // if (UsrBuf[len - 1] == '\n') {
+               //   UsrBuf[len - 1] = '\0';
+               // }
                // IAPuts("+ %s\r\n", UsrBuf);
                IAPuts("+ \r\n");
                IMAP->authstate = imap_as_expecting_plainauth;
@@ -702,33 +690,53 @@ void imap_auth_plain(void)
 {
        citimap *Imap = IMAP;
        const char *decoded_authstring;
-       char ident[256];
-       char user[256];
-       char pass[256];
+       char ident[256] = "";
+       char user[256] = "";
+       char pass[256] = "";
        int result;
-       long len;
+       long decoded_len;
+       long len = 0;
+       long plen = 0;
 
        memset(pass, 0, sizeof(pass));
-       StrBufDecodeBase64(Imap->Cmd.CmdBuf);
+       decoded_len = StrBufDecodeBase64(Imap->Cmd.CmdBuf);
 
-       decoded_authstring = ChrPtr(Imap->Cmd.CmdBuf);
-       safestrncpy(ident, decoded_authstring, sizeof ident);
-       safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
-       len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
-       if (len < 0)
-               len = sizeof(pass) - 1;
+       if (decoded_len > 0)
+       {
+               decoded_authstring = ChrPtr(Imap->Cmd.CmdBuf);
+
+               len = safestrncpy(ident, decoded_authstring, sizeof ident);
+
+               decoded_len -= len - 1;
+               decoded_authstring += len + 1;
+
+               if (decoded_len > 0)
+               {
+                       len = safestrncpy(user, decoded_authstring, sizeof user);
+
+                       decoded_authstring += len + 1;
+                       decoded_len -= len - 1;
+               }
+
+               if (decoded_len > 0)
+               {
+                       plen = safestrncpy(pass, decoded_authstring, sizeof pass);
 
+                       if (plen < 0)
+                               plen = sizeof(pass) - 1;
+               }
+       }
        Imap->authstate = imap_as_normal;
 
        if (!IsEmptyStr(ident)) {
-               result = CtdlLoginExistingUser(user, ident);
+               result = CtdlLoginExistingUser(ident);
        }
        else {
-               result = CtdlLoginExistingUser(NULL, user);
+               result = CtdlLoginExistingUser(user);
        }
 
        if (result == login_ok) {
-               if (CtdlTryPassword(pass, len) == pass_ok) {
+               if (CtdlTryPassword(pass, plen) == pass_ok) {
                        IAPrintf("%s OK authentication succeeded\r\n", Imap->authseq);
                        return;
                }
@@ -745,15 +753,19 @@ void imap_auth_login_user(long state)
        switch (state){
        case imap_as_expecting_username:
                StrBufDecodeBase64(Imap->Cmd.CmdBuf);
-               CtdlLoginExistingUser(NULL, ChrPtr(Imap->Cmd.CmdBuf));
-               CtdlEncodeBase64(PWBuf, "Password:", 9, 0);
+               CtdlLoginExistingUser(ChrPtr(Imap->Cmd.CmdBuf));
+               size_t len = CtdlEncodeBase64(PWBuf, "Password:", 9, 0);
+               if (PWBuf[len - 1] == '\n') {
+                       PWBuf[len - 1] = '\0';
+               }
+
                IAPrintf("+ %s\r\n", PWBuf);
                
                Imap->authstate = imap_as_expecting_password;
                return;
        case imap_as_expecting_multilineusername:
                extract_token(PWBuf, ChrPtr(Imap->Cmd.CmdBuf), 1, ' ', sizeof(PWBuf));
-               CtdlLoginExistingUser(NULL, ChrPtr(Imap->Cmd.CmdBuf));
+               CtdlLoginExistingUser(ChrPtr(Imap->Cmd.CmdBuf));
                IAPuts("+ go ahead\r\n");
                Imap->authstate = imap_as_expecting_multilinepassword;
                return;
@@ -821,8 +833,6 @@ void imap_select(int num_parms, ConstStr *Params)
        int ra = 0;
        struct ctdlroom QRscratch;
        int msgs, new;
-       int floornum;
-       int roomflags;
        int i;
 
        /* Convert the supplied folder name to a roomname */
@@ -832,8 +842,6 @@ void imap_select(int num_parms, ConstStr *Params)
                Imap->selected = 0;
                return;
        }
-       floornum = (i & 0x00ff);
-       roomflags = (i & 0xff00);
 
        /* First try a regular match */
        c = CtdlGetRoom(&QRscratch, towhere);
@@ -872,7 +880,7 @@ void imap_select(int num_parms, ConstStr *Params)
         * the number of messages and number of new messages.
         */
        memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
-       CtdlUserGoto(NULL, 0, 0, &msgs, &new);
+       CtdlUserGoto(NULL, 0, 0, &msgs, &new, NULL, NULL);
        Imap->selected = 1;
 
        if (!strcasecmp(Params[1].Key, "EXAMINE")) {
@@ -888,7 +896,7 @@ void imap_select(int num_parms, ConstStr *Params)
        IAPrintf("* %d RECENT\r\n", new);
 
        IAPrintf("* OK [UIDVALIDITY %ld] UID validity status\r\n", GLOBAL_UIDVALIDITY_VALUE);
-       IAPrintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
+       IAPrintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CtdlGetConfigLong("MMhighest") + 1);
 
        /* Technically, \Deleted is a valid flag, but not a permanent flag,
         * because we don't maintain its state across sessions.  Citadel
@@ -913,7 +921,8 @@ void imap_select(int num_parms, ConstStr *Params)
  */
 int imap_do_expunge(void)
 {
-       citimap *Imap = IMAP;
+       struct CitContext *CCC = CC;
+       citimap *Imap = CCCIMAP;
        int i;
        int num_expunged = 0;
        long *delmsgs = NULL;
@@ -982,6 +991,7 @@ void imap_close(int num_parms, ConstStr *Params)
  */
 void imap_namespace(int num_parms, ConstStr *Params)
 {
+       long len;
        int i;
        struct floor *fl;
        int floors = 0;
@@ -1002,8 +1012,8 @@ void imap_namespace(int num_parms, ConstStr *Params)
                if (fl->f_flags & F_INUSE) {
                        /* if (floors > 0) IAPuts(" "); samjam says this confuses javamail */
                        IAPuts("(");
-                       snprintf(Namespace, sizeof(Namespace), "%s/", fl->f_name);
-                       plain_imap_strout(Namespace);
+                       len = snprintf(Namespace, sizeof(Namespace), "%s/", fl->f_name);
+                       IPutStr(Namespace, len);
                        IAPuts(" \"/\")");
                        ++floors;
                }
@@ -1037,14 +1047,14 @@ void imap_create(int num_parms, ConstStr *Params)
 
        if (strchr(Params[2].Key, '\\') != NULL) {
                IReply("NO Invalid character in folder name");
-               syslog(LOG_DEBUG, "invalid character in folder name");
+               syslog(LOG_ERR, "invalid character in folder name");
                return;
        }
 
        ret = imap_roomname(roomname, sizeof roomname, Params[2].Key);
        if (ret < 0) {
                IReply("NO Invalid mailbox name or location");
-               syslog(LOG_DEBUG, "invalid mailbox name or location");
+               syslog(LOG_ERR, "invalid mailbox name or location");
                return;
        }
        floornum = (ret & 0x00ff);      /* lower 8 bits = floor number */
@@ -1053,7 +1063,7 @@ void imap_create(int num_parms, ConstStr *Params)
        if (flags & IR_MAILBOX) {
                if (strncasecmp(Params[2].Key, "INBOX/", 6)) {
                        IReply("NO Personal folders must be created under INBOX");
-                       syslog(LOG_DEBUG, "not subordinate to inbox");
+                       syslog(LOG_ERR, "not subordinate to inbox");
                        return;
                }
        }
@@ -1066,8 +1076,8 @@ void imap_create(int num_parms, ConstStr *Params)
                newroomview = VIEW_BBS;
        }
 
-       syslog(LOG_INFO, "IMAP: Create new room <%s> on floor <%d> with type <%d>",
-               roomname, floornum, newroomtype);
+       syslog(LOG_INFO, "Create new room <%s> on floor <%d> with type <%d>",
+                   roomname, floornum, newroomtype);
 
        ret = CtdlCreateRoom(roomname, newroomtype, "", floornum, 1, 0, newroomview);
        if (ret == 0) {
@@ -1154,6 +1164,7 @@ int imap_grabroom(char *returned_roomname, const char *foldername, int zapped_ok
  */
 void imap_status(int num_parms, ConstStr *Params)
 {
+       long len;
        int ret;
        char roomname[ROOMNAMELEN];
        char imaproomname[SIZ];
@@ -1174,7 +1185,7 @@ void imap_status(int num_parms, ConstStr *Params)
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
        /*
         * Tell the client what it wants to know.  In fact, tell it *more* than
@@ -1182,12 +1193,12 @@ void imap_status(int num_parms, ConstStr *Params)
         * names and simply spew all possible data items.  It's far easier to
         * code and probably saves us some processing time too.
         */
-       imap_mailboxname(imaproomname, sizeof imaproomname, &CC->room);
+       len = imap_mailboxname(imaproomname, sizeof imaproomname, &CC->room);
        IAPuts("* STATUS ");
-              plain_imap_strout(imaproomname);
+       IPutStr(imaproomname, len);
        IAPrintf(" (MESSAGES %d ", msgs);
        IAPrintf("RECENT %d ", new);    /* Initially, new==recent */
-       IAPrintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
+       IAPrintf("UIDNEXT %ld ", CtdlGetConfigLong("MMhighest") + 1);
        IAPrintf("UNSEEN %d)\r\n", new);
        
        /*
@@ -1195,7 +1206,7 @@ void imap_status(int num_parms, ConstStr *Params)
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
        }
 
        /*
@@ -1233,14 +1244,14 @@ void imap_subscribe(int num_parms, ConstStr *Params)
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
        /*
         * If another folder is selected, go back to that room so we can resume
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
        }
 
        IReply("OK SUBSCRIBE completed");
@@ -1270,7 +1281,7 @@ void imap_unsubscribe(int num_parms, ConstStr *Params)
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
        /* 
         * Now make the API call to zap the room
@@ -1286,7 +1297,7 @@ void imap_unsubscribe(int num_parms, ConstStr *Params)
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
        }
 }
 
@@ -1316,7 +1327,7 @@ void imap_delete(int num_parms, ConstStr *Params)
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
        /*
         * Now delete the room.
@@ -1333,7 +1344,7 @@ void imap_delete(int num_parms, ConstStr *Params)
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
        }
 }
 
@@ -1348,19 +1359,19 @@ void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
        char newroomname[ROOMNAMELEN];
        int newfloor = 0;
        struct irl *irlp = NULL;        /* scratch pointer */
-       struct irlparms *irlparms;
+       irlparms *myirlparms;
 
-       irlparms = (struct irlparms *) data;
+       myirlparms = (irlparms *) data;
        imap_mailboxname(foldername, sizeof foldername, qrbuf);
 
        /* Rename subfolders */
-       if ((!strncasecmp(foldername, irlparms->oldname,
-                         strlen(irlparms->oldname))
-            && (foldername[strlen(irlparms->oldname)] == '/'))) {
+       if ((!strncasecmp(foldername, myirlparms->oldname,
+                         myirlparms->oldnamelen)
+           && (foldername[myirlparms->oldnamelen] == '/'))) {
 
                sprintf(newfoldername, "%s/%s",
-                       irlparms->newname,
-                       &foldername[strlen(irlparms->oldname) + 1]
+                       myirlparms->newname,
+                       &foldername[myirlparms->oldnamelen + 1]
                    );
 
                newfloor = imap_roomname(newroomname,
@@ -1371,8 +1382,8 @@ void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
                strcpy(irlp->irl_newroom, newroomname);
                strcpy(irlp->irl_oldroom, qrbuf->QRname);
                irlp->irl_newfloor = newfloor;
-               irlp->next = *(irlparms->irl);
-               *(irlparms->irl) = irlp;
+               irlp->next = *(myirlparms->irl);
+               *(myirlparms->irl) = irlp;
        }
 }
 
@@ -1385,12 +1396,12 @@ void imap_rename(int num_parms, ConstStr *Params)
 {
        char old_room[ROOMNAMELEN];
        char new_room[ROOMNAMELEN];
-       int oldr, newr;
+       int newr;
        int new_floor;
        int r;
        struct irl *irl = NULL; /* the list */
        struct irl *irlp = NULL;        /* scratch pointer */
-       struct irlparms irlparms;
+       irlparms irlparms;
        char aidemsg[1024];
 
        if (strchr(Params[3].Key, '\\') != NULL) {
@@ -1398,7 +1409,7 @@ void imap_rename(int num_parms, ConstStr *Params)
                return;
        }
 
-       oldr = imap_roomname(old_room, sizeof old_room, Params[2].Key);
+       imap_roomname(old_room, sizeof old_room, Params[2].Key);
        newr = imap_roomname(new_room, sizeof new_room, Params[3].Key);
        new_floor = (newr & 0xFF);
 
@@ -1440,7 +1451,9 @@ void imap_rename(int num_parms, ConstStr *Params)
        /* Otherwise, do the subfolders.  Build a list of rooms to rename... */
        else {
                irlparms.oldname = Params[2].Key;
+               irlparms.oldnamelen = Params[2].len;
                irlparms.newname = Params[3].Key;
+               irlparms.newnamelen = Params[3].len;
                irlparms.irl = &irl;
                CtdlForEachRoom(imap_rename_backend, (void *) &irlparms);
 
@@ -1451,7 +1464,7 @@ void imap_rename(int num_parms, ConstStr *Params)
                                           irl->irl_newfloor);
                        if (r != crr_ok) {
                                /* FIXME handle error returns better */
-                               syslog(LOG_ERR, "IMAP: CtdlRenameRoom() error %d", r);
+                               syslog(LOG_ERR, "CtdlRenameRoom() error %d", r);
                        }
                        irlp = irl;
                        irl = irl->next;
@@ -1475,16 +1488,16 @@ void imap_rename(int num_parms, ConstStr *Params)
  */
 void imap_command_loop(void)
 {
+       struct CitContext *CCC = CC;
        struct timeval tv1, tv2;
        suseconds_t total_time = 0;
-       int untagged_ok = 1;
        citimap *Imap;
        const char *pchs, *pche;
        const imap_handler_hook *h;
 
        gettimeofday(&tv1, NULL);
-       CC->lastcmd = time(NULL);
-       Imap = IMAP;
+       CCC->lastcmd = time(NULL);
+       Imap = CCCIMAP;
 
        flush_output();
        if (Imap->Cmd.CmdBuf == NULL)
@@ -1493,23 +1506,23 @@ void imap_command_loop(void)
                FlushStrBuf(Imap->Cmd.CmdBuf);
 
        if (CtdlClientGetLine(Imap->Cmd.CmdBuf) < 1) {
-               syslog(LOG_ERR, "IMAP: client disconnected: ending session.");
+               syslog(LOG_ERR, "client disconnected: ending session.");
                CC->kill_me = KILLME_CLIENT_DISCONNECTED;
                return;
        }
 
        if (Imap->authstate == imap_as_expecting_password) {
-               syslog(LOG_INFO, "IMAP: <password>");
+               syslog(LOG_INFO, "<password>");
        }
        else if (Imap->authstate == imap_as_expecting_plainauth) {
-               syslog(LOG_INFO, "IMAP: <plain_auth>");
+               syslog(LOG_INFO, "<plain_auth>");
        }
        else if ((Imap->authstate == imap_as_expecting_multilineusername) || 
                 cbmstrcasestr(ChrPtr(Imap->Cmd.CmdBuf), " LOGIN ")) {
-               syslog(LOG_INFO, "IMAP: LOGIN...");
+               syslog(LOG_INFO, "LOGIN...");
        }
        else {
-               syslog(LOG_INFO, "IMAP: %s", ChrPtr(Imap->Cmd.CmdBuf));
+               syslog(LOG_DEBUG, "%s", ChrPtr(Imap->Cmd.CmdBuf));
        }
 
        pchs = ChrPtr(Imap->Cmd.CmdBuf);
@@ -1554,10 +1567,6 @@ void imap_command_loop(void)
         * If the command just submitted does not contain a literal, we
         * might think about delivering some untagged stuff...
         */
-       if (*(ChrPtr(Imap->Cmd.CmdBuf) + StrLength(Imap->Cmd.CmdBuf) - 1)
-           == '}') {
-               untagged_ok = 0;
-       }
 
        /* Grab the tag, command, and parameters. */
        imap_parameterize(&Imap->Cmd);
@@ -1570,13 +1579,13 @@ void imap_command_loop(void)
        for (i=0; i < Imap->Cmd.num_parms; i++) {
                if (Imap->Cmd.Params[i].len != strlen(Imap->Cmd.Params[i].Key))
                        syslog(LOG_DEBUG, "*********** %ld != %ld : %s",
-                                     Imap->Cmd.Params[i].len, 
-                                     strlen(Imap->Cmd.Params[i].Key),
+                                   Imap->Cmd.Params[i].len, 
+                                   strlen(Imap->Cmd.Params[i].Key),
                                      Imap->Cmd.Params[i].Key);
                else
                        syslog(LOG_DEBUG, "%ld : %s",
-                                     Imap->Cmd.Params[i].len, 
-                                     Imap->Cmd.Params[i].Key);
+                                   Imap->Cmd.Params[i].len, 
+                                   Imap->Cmd.Params[i].Key);
        }}
 #endif
 
@@ -1628,9 +1637,9 @@ BAIL:
        gettimeofday(&tv2, NULL);
        total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
        syslog(LOG_DEBUG, "IMAP command completed in %ld.%ld seconds",
-               (total_time / 1000000),
-               (total_time % 1000000)
-       );
+                   (total_time / 1000000),
+                   (total_time % 1000000)
+               );
 }
 
 void imap_noop (int num_parms, ConstStr *Params)
@@ -1643,7 +1652,7 @@ void imap_logout(int num_parms, ConstStr *Params)
        if (IMAP->selected) {
                imap_do_expunge();      /* yes, we auto-expunge at logout */
        }
-       IAPrintf("* BYE %s logging out\r\n", config.c_fqdn);
+       IAPrintf("* BYE %s logging out\r\n", CtdlGetConfigStr("c_fqdn"));
        IReply("OK Citadel IMAP session ended.");
        CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
        return;
@@ -1652,7 +1661,6 @@ void imap_logout(int num_parms, ConstStr *Params)
 const char *CitadelServiceIMAP="IMAP";
 const char *CitadelServiceIMAPS="IMAPS";
 
-
 /*
  * This function is called to register the IMAP extension with Citadel.
  */
@@ -1708,14 +1716,13 @@ CTDL_MODULE_INIT(imap)
 
        if (!threading)
        {
-               CtdlRegisterServiceHook(config.c_imap_port,
+               CtdlRegisterServiceHook(CtdlGetConfigInt("c_imap_port"),
                                        NULL, imap_greeting, imap_command_loop, NULL, CitadelServiceIMAP);
 #ifdef HAVE_OPENSSL
-               CtdlRegisterServiceHook(config.c_imaps_port,
+               CtdlRegisterServiceHook(CtdlGetConfigInt("c_imaps_port"),
                                        NULL, imaps_greeting, imap_command_loop, NULL, CitadelServiceIMAPS);
 #endif
-               CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
-               CtdlRegisterCleanupHook(imap_cleanup);
+               CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP, PRIO_STOP + 30);
        }
        
        /* return our module name for the log */