remove the duplicate for 'allheaders'
[citadel.git] / citadel / modules / sieve / serv_sieve.c
index 0006604f32b040d017ccb8a68a07e5ded3aa8351..015ccdc8ba79c4400612ecb821620332134affb4 100644 (file)
@@ -1,10 +1,22 @@
 /*
- * $Id$
- *
  * This module glues libSieve to the Citadel server in order to implement
  * the Sieve mailbox filtering language (RFC 3028).
  *
- * This code is released under the terms of the GNU General Public License. 
+ * Copyright (c) 1987-2010 by the citadel.org team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * 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"
@@ -37,8 +49,6 @@
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "room_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
@@ -54,7 +64,7 @@ char *msiv_extensions = NULL;
  */
 int ctdl_debug(sieve2_context_t *s, void *my)
 {
-       CtdlLogPrintf(CTDL_DEBUG, "Sieve: %s\n", sieve2_getvalue_string(s, "message"));
+       syslog(LOG_DEBUG, "Sieve: %s\n", sieve2_getvalue_string(s, "message"));
        return SIEVE2_OK;
 }
 
@@ -64,7 +74,7 @@ int ctdl_debug(sieve2_context_t *s, void *my)
  */
 int ctdl_errparse(sieve2_context_t *s, void *my)
 {
-       CtdlLogPrintf(CTDL_WARNING, "Error in script, line %d: %s\n",
+       syslog(LOG_WARNING, "Error in script, line %d: %s\n",
                sieve2_getvalue_int(s, "lineno"),
                sieve2_getvalue_string(s, "message")
        );
@@ -77,7 +87,7 @@ int ctdl_errparse(sieve2_context_t *s, void *my)
  */
 int ctdl_errexec(sieve2_context_t *s, void *my)
 {
-       CtdlLogPrintf(CTDL_WARNING, "Error executing script: %s\n",
+       syslog(LOG_WARNING, "Error executing script: %s\n",
                sieve2_getvalue_string(s, "message")
        );
        return SIEVE2_OK;
@@ -96,27 +106,27 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
 
        safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
 
-       CtdlLogPrintf(CTDL_DEBUG, "Action is REDIRECT, recipient <%s>\n", recp);
+       syslog(LOG_DEBUG, "Action is REDIRECT, recipient <%s>\n", recp);
 
        valid = validate_recipients(recp, NULL, 0);
        if (valid == NULL) {
-               CtdlLogPrintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
+               syslog(LOG_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
                return SIEVE2_ERROR_BADARGS;
        }
        if (valid->num_error > 0) {
-               CtdlLogPrintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
+               syslog(LOG_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
                free_recipients(valid);
                return SIEVE2_ERROR_BADARGS;
        }
 
        msg = CtdlFetchMessage(cs->msgnum, 1);
        if (msg == NULL) {
-               CtdlLogPrintf(CTDL_WARNING, "REDIRECT failed: unable to fetch msg %ld\n", cs->msgnum);
+               syslog(LOG_WARNING, "REDIRECT failed: unable to fetch msg %ld\n", cs->msgnum);
                free_recipients(valid);
                return SIEVE2_ERROR_BADARGS;
        }
 
-       CtdlSubmitMsg(msg, valid, NULL);
+       CtdlSubmitMsg(msg, valid, NULL, 0);
        cs->cancel_implicit_keep = 1;
        free_recipients(valid);
        CtdlFreeMessage(msg);
@@ -131,7 +141,7 @@ int ctdl_keep(sieve2_context_t *s, void *my)
 {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
        
-       CtdlLogPrintf(CTDL_DEBUG, "Action is KEEP\n");
+       syslog(LOG_DEBUG, "Action is KEEP\n");
 
        cs->keep = 1;
        cs->cancel_implicit_keep = 1;
@@ -150,7 +160,7 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
        char foldername[256];
        char original_room_name[ROOMNAMELEN];
 
-       CtdlLogPrintf(CTDL_DEBUG, "Action is FILEINTO, destination is <%s>\n", dest_folder);
+       syslog(LOG_DEBUG, "Action is FILEINTO, destination is <%s>\n", dest_folder);
 
        /* FILEINTO 'INBOX' is the same thing as KEEP */
        if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
@@ -164,27 +174,27 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
 
        /* First try a mailbox name match (check personal mail folders first) */
        snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder);
-       c = getroom(&CC->room, foldername);
+       c = CtdlGetRoom(&CC->room, foldername);
 
        /* Then a regular room name match (public and private rooms) */
        if (c != 0) {
                safestrncpy(foldername, dest_folder, sizeof foldername);
-               c = getroom(&CC->room, foldername);
+               c = CtdlGetRoom(&CC->room, foldername);
        }
 
        if (c != 0) {
-               CtdlLogPrintf(CTDL_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
+               syslog(LOG_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
                return SIEVE2_ERROR_BADARGS;
        }
 
        /* Yes, we actually have to go there */
-       usergoto(NULL, 0, 0, NULL, NULL);
+       CtdlUserGoto(NULL, 0, 0, NULL, NULL);
 
-       c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL);
+       c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL, 0);
 
        /* Go back to the room we came from */
        if (strcasecmp(original_room_name, CC->room.QRname)) {
-               usergoto(original_room_name, 0, 0, NULL, NULL);
+               CtdlUserGoto(original_room_name, 0, 0, NULL, NULL);
        }
 
        if (c == 0) {
@@ -204,7 +214,7 @@ int ctdl_discard(sieve2_context_t *s, void *my)
 {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
 
-       CtdlLogPrintf(CTDL_DEBUG, "Action is DISCARD\n");
+       syslog(LOG_DEBUG, "Action is DISCARD\n");
 
        /* Cancel the implicit keep.  That's all there is to it. */
        cs->cancel_implicit_keep = 1;
@@ -221,11 +231,11 @@ int ctdl_reject(sieve2_context_t *s, void *my)
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
        char *reject_text = NULL;
 
-       CtdlLogPrintf(CTDL_DEBUG, "Action is REJECT\n");
+       syslog(LOG_DEBUG, "Action is REJECT\n");
 
        /* If we don't know who sent the message, do a DISCARD instead. */
        if (IsEmptyStr(cs->sender)) {
-               CtdlLogPrintf(CTDL_INFO, "Unknown sender.  Doing DISCARD instead of REJECT.\n");
+               syslog(LOG_INFO, "Unknown sender.  Doing DISCARD instead of REJECT.\n");
                return ctdl_discard(s, my);
        }
 
@@ -276,7 +286,7 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
        char *vacamsg_text = NULL;
        char vacamsg_subject[1024];
 
-       CtdlLogPrintf(CTDL_DEBUG, "Action is VACATION\n");
+       syslog(LOG_DEBUG, "Action is VACATION\n");
 
        message = sieve2_getvalue_string(s, "message");
        if (message == NULL) return SIEVE2_ERROR_BADARGS;
@@ -296,7 +306,7 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
        for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
                if (!strcasecmp(vptr->fromaddr, cs->sender)) {
                        if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
-                               CtdlLogPrintf(CTDL_DEBUG, "Already alerted <%s> recently.\n", cs->sender);
+                               syslog(LOG_DEBUG, "Already alerted <%s> recently.\n", cs->sender);
                                return SIEVE2_OK;
                        }
                }
@@ -309,7 +319,7 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
        }
 
        sprintf(vacamsg_text, 
-               "Content-type: text/plain\n"
+               "Content-type: text/plain charset=utf-8\n"
                "\n"
                "%s\n"
                "\n"
@@ -342,6 +352,7 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
        /* If we get to this point, create a new record.
         */
        vptr = malloc(sizeof(struct sdm_vacation));
+       memset(vptr, 0, sizeof(struct sdm_vacation));
        vptr->timestamp = time(NULL);
        safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
        vptr->next = cs->u->first_vacation;
@@ -380,7 +391,7 @@ int ctdl_getenvelope(sieve2_context_t *s, void *my)
 {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
 
-       CtdlLogPrintf(CTDL_DEBUG, "Action is GETENVELOPE\nEnvFrom: %s\n  EnvTo: %s\n",
+       syslog(LOG_DEBUG, "Action is GETENVELOPE\nEnvFrom: %s\n  EnvTo: %s\n",
                cs->envelope_from, cs->envelope_to);
 
        if (cs->envelope_from != NULL) {
@@ -452,13 +463,13 @@ int ctdl_getscript(sieve2_context_t *s, void *my) {
 
        for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
                if (sptr->script_active > 0) {
-                       CtdlLogPrintf(CTDL_DEBUG, "ctdl_getscript() is using script '%s'\n", sptr->script_name);
+                       syslog(LOG_DEBUG, "ctdl_getscript() is using script '%s'\n", sptr->script_name);
                        sieve2_setvalue_string(s, "script", sptr->script_content);
                        return SIEVE2_OK;
                }
        }
                
-       CtdlLogPrintf(CTDL_DEBUG, "ctdl_getscript() found no active script\n");
+       syslog(LOG_DEBUG, "ctdl_getscript() found no active script\n");
        return SIEVE2_ERROR_GETSCRIPT;
 }
 
@@ -469,7 +480,7 @@ int ctdl_getheaders(sieve2_context_t *s, void *my) {
 
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
 
-       CtdlLogPrintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
+       syslog(LOG_DEBUG, "ctdl_getheaders() was called\n");
        sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
        return SIEVE2_OK;
 }
@@ -490,7 +501,7 @@ void sieve_queue_room(struct ctdlroom *which_room) {
        ptr->next = sieve_list;
        sieve_list = ptr;
        end_critical_section(S_SIEVELIST);
-       CtdlLogPrintf(CTDL_DEBUG, "<%s> queued for Sieve processing\n", which_room->QRname);
+       syslog(LOG_DEBUG, "<%s> queued for Sieve processing\n", which_room->QRname);
 }
 
 
@@ -508,31 +519,30 @@ void sieve_do_msg(long msgnum, void *userdata) {
        size_t headers_len = 0;
        int len = 0;
 
-       if (userdata == NULL)
+       if (u == NULL)
        {
-               CtdlLogPrintf(CTDL_EMERG, "Cant process Message <%ld>without Userdata!\n", msgnum);
+               syslog(LOG_EMERG, "Can't process message <%ld> without userdata!\n", msgnum);
                return;
        }
 
        sieve2_context = u->sieve2_context;
 
-       CtdlLogPrintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
+       syslog(LOG_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
 
-       msg = CtdlFetchMessage(msgnum, 0);
+       /*
+        * Make sure you include message body so you can get those second-level headers ;)
+        */
+       msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) return;
 
        /*
         * Grab the message headers so we can feed them to libSieve.
+        * Use HEADERS_ONLY rather than HEADERS_FAST in order to include second-level headers.
         */
-       CC->redirect_buffer = malloc(SIZ);
-       CC->redirect_len = 0;
-       CC->redirect_alloc = SIZ;
-       CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
-       my.rfc822headers = CC->redirect_buffer;
-       headers_len = CC->redirect_len;
-       CC->redirect_buffer = NULL;
-       CC->redirect_len = 0;
-       CC->redirect_alloc = 0;
+       CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
+       CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1, 0);
+       headers_len = StrLength(CC->redirect_buffer);
+       my.rfc822headers = SmashStrBuf(&CC->redirect_buffer);
 
        /*
         * libSieve clobbers the stack if it encounters badly formed
@@ -625,13 +635,11 @@ void sieve_do_msg(long msgnum, void *userdata) {
        }
 
        CtdlFreeMessage(msg);
-
-       sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
        
-       CtdlLogPrintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
+       syslog(LOG_DEBUG, "Calling sieve2_execute()\n");
        res = sieve2_execute(sieve2_context, &my);
        if (res != SIEVE2_OK) {
-               CtdlLogPrintf(CTDL_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
+               syslog(LOG_CRIT, "sieve2_execute() returned %d: %s\n", res, sieve2_errstr(res));
        }
 
        free(my.rfc822headers);
@@ -642,11 +650,11 @@ void sieve_do_msg(long msgnum, void *userdata) {
         * if no other action was successfully taken.
         */
        if ( (!my.keep) && (my.cancel_implicit_keep) ) {
-               CtdlLogPrintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
+               syslog(LOG_DEBUG, "keep is 0 -- deleting message from inbox\n");
                CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
+       syslog(LOG_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
        u->lastproc = msgnum;
 
        return;
@@ -744,33 +752,28 @@ void get_sieve_config_backend(long msgnum, void *userdata) {
  * otherwise it just frees the data structures.)
  */
 void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
-       char *text;
+       StrBuf *text;
        struct sdm_script *sptr;
        struct sdm_vacation *vptr;
-       size_t tsize;
-
-       text = malloc(1024);
-       tsize = 1024;
-       snprintf(text, 1024,
-               "Content-type: application/x-citadel-sieve-config\n"
-               "\n"
-               CTDLSIEVECONFIGSEPARATOR
-               "lastproc|%ld"
-               CTDLSIEVECONFIGSEPARATOR
-       ,
-               u->lastproc
-       );
+       
+       text = NewStrBufPlain(NULL, SIZ);
+       StrBufPrintf(text,
+                    "Content-type: application/x-citadel-sieve-config\n"
+                    "\n"
+                    CTDLSIEVECONFIGSEPARATOR
+                    "lastproc|%ld"
+                    CTDLSIEVECONFIGSEPARATOR
+                    ,
+                    u->lastproc
+               );
 
        while (u->first_script != NULL) {
-               size_t tlen;
-               tlen = strlen(text);
-               tsize = tlen + strlen(u->first_script->script_content) +256;
-               text = realloc(text, tsize);
-               sprintf(&text[strlen(text)], "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
-                       u->first_script->script_name,
-                       u->first_script->script_active,
-                       u->first_script->script_content
-               );
+               StrBufAppendPrintf(text,
+                                  "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
+                                  u->first_script->script_name,
+                                  u->first_script->script_active,
+                                  u->first_script->script_content
+                       );
                sptr = u->first_script;
                u->first_script = u->first_script->next;
                free(sptr->script_content);
@@ -779,32 +782,26 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
 
        if (u->first_vacation != NULL) {
 
-               tsize = strlen(text) + 256;
-               for (vptr = u->first_vacation; vptr != NULL; vptr = vptr->next) {
-                       tsize += strlen(vptr->fromaddr + 32);
-               }
-               text = realloc(text, tsize);
-
-               sprintf(&text[strlen(text)], "vacation|\n");
+               StrBufAppendPrintf(text, "vacation|\n");
                while (u->first_vacation != NULL) {
                        if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
-                               sprintf(&text[strlen(text)], "%s|%ld\n",
-                                       u->first_vacation->fromaddr,
-                                       u->first_vacation->timestamp
-                               );
+                               StrBufAppendPrintf(text, "%s|%ld\n",
+                                                  u->first_vacation->fromaddr,
+                                                  u->first_vacation->timestamp
+                                       );
                        }
                        vptr = u->first_vacation;
                        u->first_vacation = u->first_vacation->next;
                        free(vptr);
                }
-               sprintf(&text[strlen(text)], CTDLSIEVECONFIGSEPARATOR);
+               StrBufAppendPrintf(text, CTDLSIEVECONFIGSEPARATOR);
        }
 
        if (yes_write_to_disk)
        {
                /* Save the config */
                quickie_message("Citadel", NULL, NULL, u->config_roomname,
-                               text,
+                               ChrPtr(text),
                                4,
                                "Sieve configuration"
                );
@@ -815,7 +812,7 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
                }
        }
 
-       free (text);
+       FreeStrBuf (&text);
 
 }
 
@@ -865,8 +862,8 @@ void sieve_do_room(char *roomname) {
         * require execution.
         */
        snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), USERCONFIGROOM);
-       if (getroom(&CC->room, u.config_roomname) != 0) {
-               CtdlLogPrintf(CTDL_DEBUG, "<%s> does not exist.  No processing is required.\n", u.config_roomname);
+       if (CtdlGetRoom(&CC->room, u.config_roomname) != 0) {
+               syslog(LOG_DEBUG, "<%s> does not exist.  No processing is required.\n", u.config_roomname);
                return;
        }
 
@@ -878,14 +875,14 @@ void sieve_do_room(char *roomname) {
                get_sieve_config_backend, (void *)&u );
 
        if (u.config_msgnum < 0) {
-               CtdlLogPrintf(CTDL_DEBUG, "No Sieve rules exist.  No processing is required.\n");
+               syslog(LOG_DEBUG, "No Sieve rules exist.  No processing is required.\n");
                return;
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Rules found.  Performing Sieve processing for <%s>\n", roomname);
+       syslog(LOG_DEBUG, "Rules found.  Performing Sieve processing for <%s>\n", roomname);
 
-       if (getroom(&CC->room, roomname) != 0) {
-               CtdlLogPrintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
+       if (CtdlGetRoom(&CC->room, roomname) != 0) {
+               syslog(LOG_CRIT, "ERROR: cannot load <%s>\n", roomname);
                return;
        }
 
@@ -893,13 +890,13 @@ void sieve_do_room(char *roomname) {
        
        res = sieve2_alloc(&sieve2_context);
        if (res != SIEVE2_OK) {
-               CtdlLogPrintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
+               syslog(LOG_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
                return;
        }
 
        res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
        if (res != SIEVE2_OK) {
-               CtdlLogPrintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
+               syslog(LOG_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
                goto BAIL;
        }
 
@@ -910,7 +907,7 @@ void sieve_do_room(char *roomname) {
        my.u = &u;
        res = sieve2_validate(sieve2_context, &my);
        if (res != SIEVE2_OK) {
-               CtdlLogPrintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
+               syslog(LOG_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
                goto BAIL;
        }
 
@@ -925,7 +922,7 @@ void sieve_do_room(char *roomname) {
 BAIL:
        res = sieve2_free(&sieve2_context);
        if (res != SIEVE2_OK) {
-               CtdlLogPrintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
+               syslog(LOG_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
        }
 
        /* Rewrite the config if we have to */
@@ -940,7 +937,7 @@ void perform_sieve_processing(void) {
        struct RoomProcList *ptr = NULL;
 
        if (sieve_list != NULL) {
-               CtdlLogPrintf(CTDL_DEBUG, "Begin Sieve processing\n");
+               syslog(LOG_DEBUG, "Begin Sieve processing\n");
                while (sieve_list != NULL) {
                        char spoolroomname[ROOMNAMELEN];
                        safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
@@ -973,7 +970,7 @@ void msiv_load(struct sdm_userdata *u) {
        strcpy(hold_rm, CC->room.QRname);       /* save current room */
 
        /* Take a spin through the user's personal address book */
-       if (getroom(&CC->room, USERCONFIGROOM) == 0) {
+       if (CtdlGetRoom(&CC->room, USERCONFIGROOM) == 0) {
        
                u->config_msgnum = (-1);
                strcpy(u->config_roomname, CC->room.QRname);
@@ -983,7 +980,7 @@ void msiv_load(struct sdm_userdata *u) {
        }
 
        if (strcmp(CC->room.QRname, hold_rm)) {
-               getroom(&CC->room, hold_rm);    /* return to saved room */
+               CtdlGetRoom(&CC->room, hold_rm);    /* return to saved room */
        }
 }
 
@@ -1154,7 +1151,7 @@ void cmd_msiv(char *argbuf) {
                extract_token(script_name, argbuf, 1, '|', sizeof script_name);
                if (!IsEmptyStr(script_name)) {
                        cprintf("%d Transmit script now\n", SEND_LISTING);
-                       script_content = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
+                       script_content = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
                        msiv_putscript(&u, script_name, script_content);
                        changes_made = 1;
                }
@@ -1257,7 +1254,7 @@ void ctdl_sieve_init(void) {
                strcpy(&cred[55], "...");
        }
 
-       CtdlLogPrintf(CTDL_INFO, "%s\n",cred);
+       syslog(LOG_INFO, "%s\n",cred);
        free(cred);
 
        /* Briefly initialize a Sieve parser instance just so we can list the
@@ -1265,26 +1262,45 @@ void ctdl_sieve_init(void) {
         */
        res = sieve2_alloc(&sieve2_context);
        if (res != SIEVE2_OK) {
-               CtdlLogPrintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
+               syslog(LOG_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
                return;
        }
 
        res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
        if (res != SIEVE2_OK) {
-               CtdlLogPrintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
+               syslog(LOG_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
                goto BAIL;
        }
 
        msiv_extensions = strdup(sieve2_listextensions(sieve2_context));
-       CtdlLogPrintf(CTDL_INFO, "Extensions: %s\n", msiv_extensions);
+       syslog(LOG_INFO, "Extensions: %s\n", msiv_extensions);
 
 BAIL:  res = sieve2_free(&sieve2_context);
        if (res != SIEVE2_OK) {
-               CtdlLogPrintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
+               syslog(LOG_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
        }
 
 }
 
+void cleanup_sieve(void)
+{
+        struct RoomProcList *ptr, *ptr2;
+
+       if (msiv_extensions != NULL)
+               free(msiv_extensions);
+       msiv_extensions = NULL;
+
+        begin_critical_section(S_SIEVELIST);
+       ptr=sieve_list;
+       while (ptr != NULL) {
+               ptr2 = ptr->next;
+               free(ptr);
+               ptr = ptr2;
+       }
+        sieve_list = NULL;
+        end_critical_section(S_SIEVELIST);
+}
+
 int serv_sieve_room(struct ctdlroom *room)
 {
        if (!strcasecmp(&room->QRname[11], MAILROOM)) {
@@ -1302,9 +1318,10 @@ CTDL_MODULE_INIT(sieve)
                CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
                CtdlRegisterRoomHook(serv_sieve_room);
                CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE);
+               CtdlRegisterCleanupHook(cleanup_sieve);
        }
        
-        /* return our Subversion id for the Log */
-       return "$Id$";
+        /* return our module name for the log */
+       return "sieve";
 }