cleaned up some compiler warnings
[citadel.git] / citadel / modules / sieve / serv_sieve.c
index e166964e132e3de3da529e66d678e056c78c8780..30e03100759b8272827a7792facf871325be1b96 100644 (file)
@@ -1,24 +1,16 @@
 /*
- * $Id$
- *
  * This module glues libSieve to the Citadel server in order to implement
  * the Sieve mailbox filtering language (RFC 3028).
  *
- * Copyright (c) 1987-2010 by the citadel.org team
+ * Copyright (c) 1987-2018 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 open source software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3.
  *
  * 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"
@@ -51,7 +43,6 @@
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
@@ -67,7 +58,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, "%s", sieve2_getvalue_string(s, "message"));
        return SIEVE2_OK;
 }
 
@@ -77,9 +68,9 @@ 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",
-               sieve2_getvalue_int(s, "lineno"),
-               sieve2_getvalue_string(s, "message")
+       syslog(LOG_WARNING, "Error in script, line %d: %s",
+                 sieve2_getvalue_int(s, "lineno"),
+                 sieve2_getvalue_string(s, "message")
        );
        return SIEVE2_OK;
 }
@@ -90,9 +81,9 @@ 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",
-               sieve2_getvalue_string(s, "message")
-       );
+       syslog(LOG_WARNING, "Error executing script: %s",
+                 sieve2_getvalue_string(s, "message")
+               );
        return SIEVE2_OK;
 }
 
@@ -104,27 +95,27 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
 {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
        struct CtdlMessage *msg = NULL;
-       struct recptypes *valid = NULL;
+       recptypes *valid = NULL;
        char recp[256];
 
        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>", 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>", 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>", recp);
                free_recipients(valid);
                return SIEVE2_ERROR_BADARGS;
        }
 
-       msg = CtdlFetchMessage(cs->msgnum, 1);
+       msg = CtdlFetchMessage(cs->msgnum, 1, 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", cs->msgnum);
                free_recipients(valid);
                return SIEVE2_ERROR_BADARGS;
        }
@@ -132,7 +123,7 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
        CtdlSubmitMsg(msg, valid, NULL, 0);
        cs->cancel_implicit_keep = 1;
        free_recipients(valid);
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
        return SIEVE2_OK;
 }
 
@@ -144,7 +135,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");
 
        cs->keep = 1;
        cs->cancel_implicit_keep = 1;
@@ -163,7 +154,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>", dest_folder);
 
        /* FILEINTO 'INBOX' is the same thing as KEEP */
        if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
@@ -186,18 +177,18 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
        }
 
        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", dest_folder);
                return SIEVE2_ERROR_BADARGS;
        }
 
        /* Yes, we actually have to go there */
-       CtdlUserGoto(NULL, 0, 0, NULL, NULL);
+       CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, 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)) {
-               CtdlUserGoto(original_room_name, 0, 0, NULL, NULL);
+               CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
        }
 
        if (c == 0) {
@@ -217,7 +208,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");
 
        /* Cancel the implicit keep.  That's all there is to it. */
        cs->cancel_implicit_keep = 1;
@@ -225,7 +216,6 @@ int ctdl_discard(sieve2_context_t *s, void *my)
 }
 
 
-
 /*
  * Callback function to indicate that a message should be rejected
  */
@@ -234,11 +224,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");
 
        /* 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.");
                return ctdl_discard(s, my);
        }
 
@@ -276,7 +266,6 @@ int ctdl_reject(sieve2_context_t *s, void *my)
 }
 
 
-
 /*
  * Callback function to indicate that a vacation message should be generated
  */
@@ -289,7 +278,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");
 
        message = sieve2_getvalue_string(s, "message");
        if (message == NULL) return SIEVE2_ERROR_BADARGS;
@@ -309,7 +298,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.", cs->sender);
                                return SIEVE2_OK;
                        }
                }
@@ -322,7 +311,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"
@@ -365,11 +354,11 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
 }
 
 
+#if 0
 /*
  * Callback function to parse addresses per local system convention
  * It is disabled because we don't support subaddresses.
  */
-#if 0
 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
 {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
@@ -394,8 +383,9 @@ 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",
-               cs->envelope_from, cs->envelope_to);
+       syslog(LOG_DEBUG, "Action is GETENVELOPE");
+       syslog(LOG_DEBUG, "EnvFrom: %s", cs->envelope_from);
+       syslog(LOG_DEBUG, "EnvTo: %s", cs->envelope_to);
 
        if (cs->envelope_from != NULL) {
                if ((cs->envelope_from[0] != '@')&&(cs->envelope_from[strlen(cs->envelope_from)-1] != '@')) {
@@ -426,16 +416,17 @@ int ctdl_getenvelope(sieve2_context_t *s, void *my)
 }
 
 
+#if 0
 /*
  * Callback function to fetch message body
  * (Uncomment the code if we implement this extension)
  *
+ */
 int ctdl_getbody(sieve2_context_t *s, void *my)
 {
        return SIEVE2_ERROR_UNSUPPORTED;
 }
- *
- */
+#endif
 
 
 /*
@@ -458,24 +449,40 @@ int ctdl_getsize(sieve2_context_t *s, void *my)
 
 
 /*
- * Callback function to retrieve the sieve script
+ * Return a pointer to the active Sieve script.
+ * (Caller does NOT own the memory and should not free the returned pointer.)
  */
-int ctdl_getscript(sieve2_context_t *s, void *my) {
+char *get_active_script(struct sdm_userdata *u) {
        struct sdm_script *sptr;
-       struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
 
-       for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
+       for (sptr=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);
-                       sieve2_setvalue_string(s, "script", sptr->script_content);
-                       return SIEVE2_OK;
+                       syslog(LOG_DEBUG, "get_active_script() is using script '%s'", sptr->script_name);
+                       return(sptr->script_content);
                }
        }
-               
-       CtdlLogPrintf(CTDL_DEBUG, "ctdl_getscript() found no active script\n");
+
+       syslog(LOG_DEBUG, "get_active_script() found no active script");
+       return(NULL);
+}
+
+
+/*
+ * Callback function to retrieve the sieve script
+ */
+int ctdl_getscript(sieve2_context_t *s, void *my) {
+       struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
+
+       char *active_script = get_active_script(cs->u);
+       if (active_script != NULL) {
+               sieve2_setvalue_string(s, "script", active_script);
+               return SIEVE2_OK;
+       }
+
        return SIEVE2_ERROR_GETSCRIPT;
 }
 
+
 /*
  * Callback function to retrieve message headers
  */
@@ -483,13 +490,12 @@ 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");
        sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
        return SIEVE2_OK;
 }
 
 
-
 /*
  * Add a room to the list of those rooms which potentially require sieve processing
  */
@@ -504,11 +510,10 @@ 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", which_room->QRname);
 }
 
 
-
 /*
  * Perform sieve processing for one message (called by sieve_do_room() for each message)
  */
@@ -522,20 +527,19 @@ void sieve_do_msg(long msgnum, void *userdata) {
        size_t headers_len = 0;
        int len = 0;
 
-       if (u == NULL)
-       {
-               CtdlLogPrintf(CTDL_EMERG, "Can't process message <%ld> without userdata!\n", msgnum);
+       if (u == NULL) {
+               syslog(LOG_ERR, "Can't process message <%ld> without userdata!", 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>", msgnum);
 
        /*
         * Make sure you include message body so you can get those second-level headers ;)
         */
-       msg = CtdlFetchMessage(msgnum, 1);
+       msg = CtdlFetchMessage(msgnum, 1, 1);
        if (msg == NULL) return;
 
        /*
@@ -565,37 +569,34 @@ void sieve_do_msg(long msgnum, void *userdata) {
        my.u = u;                               /* Hand off a pointer to the rest of this info */
 
        /* Keep track of the recipient so we can do handling based on it later */
-       process_rfc822_addr(msg->cm_fields['R'], my.recp_user, my.recp_node, my.recp_name);
+       process_rfc822_addr(msg->cm_fields[eRecipient], my.recp_user, my.recp_node, my.recp_name);
 
        /* Keep track of the sender so we can use it for REJECT and VACATION responses */
-       if (msg->cm_fields['F'] != NULL) {
-               safestrncpy(my.sender, msg->cm_fields['F'], sizeof my.sender);
+       if (!CM_IsEmpty(msg, erFc822Addr)) {
+               safestrncpy(my.sender, msg->cm_fields[erFc822Addr], sizeof my.sender);
        }
-       else if ( (msg->cm_fields['A'] != NULL) && (msg->cm_fields['N'] != NULL) ) {
-               snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields['A'], msg->cm_fields['N']);
-       }
-       else if (msg->cm_fields['A'] != NULL) {
-               safestrncpy(my.sender, msg->cm_fields['A'], sizeof my.sender);
+       else if (!CM_IsEmpty(msg, eAuthor)) {
+               safestrncpy(my.sender, msg->cm_fields[eAuthor], sizeof my.sender);
        }
        else {
                strcpy(my.sender, "");
        }
 
        /* Keep track of the subject so we can use it for VACATION responses */
-       if (msg->cm_fields['U'] != NULL) {
-               safestrncpy(my.subject, msg->cm_fields['U'], sizeof my.subject);
+       if (!CM_IsEmpty(msg, eMsgSubject)) {
+               safestrncpy(my.subject, msg->cm_fields[eMsgSubject], sizeof my.subject);
        }
        else {
                strcpy(my.subject, "");
        }
 
        /* Keep track of the envelope-from address (use body-from if not found) */
-       if (msg->cm_fields['P'] != NULL) {
-               safestrncpy(my.envelope_from, msg->cm_fields['P'], sizeof my.envelope_from);
+       if (!CM_IsEmpty(msg, eMessagePath)) {
+               safestrncpy(my.envelope_from, msg->cm_fields[eMessagePath], sizeof my.envelope_from);
                stripallbut(my.envelope_from, '<', '>');
        }
-       else if (msg->cm_fields['F'] != NULL) {
-               safestrncpy(my.envelope_from, msg->cm_fields['F'], sizeof my.envelope_from);
+       else if (!CM_IsEmpty(msg, erFc822Addr)) {
+               safestrncpy(my.envelope_from, msg->cm_fields[erFc822Addr], sizeof my.envelope_from);
                stripallbut(my.envelope_from, '<', '>');
        }
        else {
@@ -608,19 +609,19 @@ void sieve_do_msg(long msgnum, void *userdata) {
        }
        if (haschar(my.envelope_from, '@') == 0) {
                strcat(my.envelope_from, "@");
-               strcat(my.envelope_from, config.c_fqdn);
+               strcat(my.envelope_from, CtdlGetConfigStr("c_fqdn"));
        }
 
        /* Keep track of the envelope-to address (use body-to if not found) */
-       if (msg->cm_fields['V'] != NULL) {
-               safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
+       if (!CM_IsEmpty(msg, eenVelopeTo)) {
+               safestrncpy(my.envelope_to, msg->cm_fields[eenVelopeTo], sizeof my.envelope_to);
                stripallbut(my.envelope_to, '<', '>');
        }
-       else if (msg->cm_fields['R'] != NULL) {
-               safestrncpy(my.envelope_to, msg->cm_fields['R'], sizeof my.envelope_to);
-               if (msg->cm_fields['D'] != NULL) {
+       else if (!CM_IsEmpty(msg, eRecipient)) {
+               safestrncpy(my.envelope_to, msg->cm_fields[eRecipient], sizeof my.envelope_to);
+               if (!CM_IsEmpty(msg, eDestination)) {
                        strcat(my.envelope_to, "@");
-                       strcat(my.envelope_to, msg->cm_fields['D']);
+                       strcat(my.envelope_to, msg->cm_fields[eDestination]);
                }
                stripallbut(my.envelope_to, '<', '>');
        }
@@ -634,17 +635,15 @@ void sieve_do_msg(long msgnum, void *userdata) {
        }
        if (haschar(my.envelope_to, '@') == 0) {
                strcat(my.envelope_to, "@");
-               strcat(my.envelope_to, config.c_fqdn);
+               strcat(my.envelope_to, CtdlGetConfigStr("c_fqdn"));
        }
 
-       CtdlFreeMessage(msg);
-
-       sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
+       CM_Free(msg);
        
-       CtdlLogPrintf(CTDL_DEBUG, "Calling sieve2_execute()\n");
+       syslog(LOG_DEBUG, "Calling sieve2_execute()");
        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_ERR, "sieve2_execute() returned %d: %s", res, sieve2_errstr(res));
        }
 
        free(my.rfc822headers);
@@ -655,11 +654,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");
                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>", msgnum);
        u->lastproc = msgnum;
 
        return;
@@ -730,17 +729,18 @@ void get_sieve_config_backend(long msgnum, void *userdata) {
        struct sdm_userdata *u = (struct sdm_userdata *) userdata;
        struct CtdlMessage *msg;
        char *conf;
+       long conflen;
 
        u->config_msgnum = msgnum;
-       msg = CtdlFetchMessage(msgnum, 1);
+       msg = CtdlFetchMessage(msgnum, 1, 1);
        if (msg == NULL) {
                u->config_msgnum = (-1) ;
                return;
        }
 
-       conf = msg->cm_fields['M'];
-       msg->cm_fields['M'] = NULL;
-       CtdlFreeMessage(msg);
+       CM_GetAsField(msg, eMesageText, &conf, &conflen);
+
+       CM_Free(msg);
 
        if (conf != NULL) {
                parse_sieve_config(conf, u);
@@ -868,7 +868,7 @@ void sieve_do_room(char *roomname) {
         */
        snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), USERCONFIGROOM);
        if (CtdlGetRoom(&CC->room, u.config_roomname) != 0) {
-               CtdlLogPrintf(CTDL_DEBUG, "<%s> does not exist.  No processing is required.\n", u.config_roomname);
+               syslog(LOG_DEBUG, "<%s> does not exist.  No processing is required.", u.config_roomname);
                return;
        }
 
@@ -880,14 +880,26 @@ 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.");
                return;
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Rules found.  Performing Sieve processing for <%s>\n", roomname);
+       /*
+        * Check to see whether the script is empty and should not be processed.
+        * A script is considered non-empty if it contains at least one semicolon.
+        */
+       if (
+               (get_active_script(&u) == NULL)
+               || (strchr(get_active_script(&u), ';') == NULL)
+       ) {
+               syslog(LOG_DEBUG, "Sieve script is empty.  No processing is required.");
+               return;
+       }
+
+       syslog(LOG_DEBUG, "Rules found.  Performing Sieve processing for <%s>", roomname);
 
        if (CtdlGetRoom(&CC->room, roomname) != 0) {
-               CtdlLogPrintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", roomname);
+               syslog(LOG_ERR, "ERROR: cannot load <%s>", roomname);
                return;
        }
 
@@ -895,13 +907,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_ERR, "sieve2_alloc() returned %d: %s", 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_ERR, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
                goto BAIL;
        }
 
@@ -912,7 +924,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_ERR, "sieve2_validate() returned %d: %s", res, sieve2_errstr(res));
                goto BAIL;
        }
 
@@ -927,7 +939,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_ERR, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
        }
 
        /* Rewrite the config if we have to */
@@ -942,7 +954,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");
                while (sieve_list != NULL) {
                        char spoolroomname[ROOMNAMELEN];
                        safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
@@ -1156,7 +1168,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(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
+                       script_content = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
                        msiv_putscript(&u, script_name, script_content);
                        changes_made = 1;
                }
@@ -1259,7 +1271,7 @@ void ctdl_sieve_init(void) {
                strcpy(&cred[55], "...");
        }
 
-       CtdlLogPrintf(CTDL_INFO, "%s\n",cred);
+       syslog(LOG_INFO, "%s",cred);
        free(cred);
 
        /* Briefly initialize a Sieve parser instance just so we can list the
@@ -1267,26 +1279,47 @@ 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_ERR, "sieve2_alloc() returned %d: %s", 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_ERR, "sieve2_callbacks() returned %d: %s", 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", 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_ERR, "sieve2_free() returned %d: %s", 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)) {
@@ -1295,18 +1328,18 @@ int serv_sieve_room(struct ctdlroom *room)
        return 0;
 }
 
+
 CTDL_MODULE_INIT(sieve)
 {
        if (!threading)
        {
-
                ctdl_sieve_init();
                CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
                CtdlRegisterRoomHook(serv_sieve_room);
-               CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE);
+               CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE, PRIO_HOUSE + 10);
+               CtdlRegisterCleanupHook(cleanup_sieve);
        }
        
-        /* return our Subversion id for the Log */
-       return "$Id$";
+        /* return our module name for the log */
+       return "sieve";
 }
-