]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_sieve.c
Remove the SieveRules room. Keep sieve config in My Citadel Config
[citadel.git] / citadel / serv_sieve.c
index 069f703c61ec648ce36e72fb491945eee8ed2d09..5a80cba1918351ceff4848187446c76b811df008 100644 (file)
@@ -42,6 +42,7 @@
 #include "policy.h"
 #include "database.h"
 #include "msgbase.h"
+#include "internet_addressing.h"
 #include "tools.h"
 
 #ifdef HAVE_LIBSIEVE
 #include "serv_sieve.h"
 
 struct RoomProcList *sieve_list = NULL;
-
-struct ctdl_sieve {
-       char *rfc822headers;
-       int actiontaken;                /* Set to 1 if the message was successfully acted upon */
-       int keep;                       /* Set to 1 to suppress message deletion from the inbox */
-       long usernum;                   /* Owner of the mailbox we're processing */
-       long msgnum;                    /* Message base ID of the message being processed */
-};
+char *msiv_extensions = NULL;
 
 
 /*
@@ -68,12 +62,14 @@ int ctdl_debug(sieve2_context_t *s, void *my)
        static int ctdl_libsieve_debug = 0;
 
        if (ctdl_libsieve_debug) {
+/*
                lprintf(CTDL_DEBUG, "Sieve: level [%d] module [%s] file [%s] function [%s]\n",
                        sieve2_getvalue_int(s, "level"),
                        sieve2_getvalue_string(s, "module"),
                        sieve2_getvalue_string(s, "file"),
                        sieve2_getvalue_string(s, "function"));
-               lprintf(CTDL_DEBUG, "       message [%s]\n",
+ */
+               lprintf(CTDL_DEBUG, "Sieve: %s\n",
                        sieve2_getvalue_string(s, "message"));
        }
        return SIEVE2_OK;
@@ -138,7 +134,7 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
        }
 
        CtdlSubmitMsg(msg, valid, NULL);
-       cs->actiontaken = 1;
+       cs->cancel_implicit_keep = 1;
        free(valid);
        CtdlFreeMessage(msg);
        return SIEVE2_OK;
@@ -155,7 +151,7 @@ int ctdl_keep(sieve2_context_t *s, void *my)
        lprintf(CTDL_DEBUG, "Action is KEEP\n");
 
        cs->keep = 1;
-       cs->actiontaken = 1;
+       cs->cancel_implicit_keep = 1;
        return SIEVE2_OK;
 }
 
@@ -176,7 +172,7 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
        /* FILEINTO 'INBOX' is the same thing as KEEP */
        if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
                cs->keep = 1;
-               cs->actiontaken = 1;
+               cs->cancel_implicit_keep = 1;
                return SIEVE2_OK;
        }
 
@@ -199,7 +195,7 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
        }
 
        /* Yes, we actually have to go there */
-        usergoto(NULL, 0, 0, NULL, NULL);
+       usergoto(NULL, 0, 0, NULL, NULL);
 
        c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL);
 
@@ -209,7 +205,7 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
        }
 
        if (c == 0) {
-               cs->actiontaken = 1;
+               cs->cancel_implicit_keep = 1;
                return SIEVE2_OK;
        }
        else {
@@ -219,55 +215,190 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
 
 
 /*
- * Callback function to indicate that a message should be rejected
- * FIXME implement this
+ * Callback function to indicate that a message should be discarded.
  */
-int ctdl_reject(sieve2_context_t *s, void *my)
+int ctdl_discard(sieve2_context_t *s, void *my)
 {
-       lprintf(CTDL_DEBUG, "Action is REJECT\n");
-       return SIEVE2_ERROR_UNSUPPORTED;
+       struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
+
+       lprintf(CTDL_DEBUG, "Action is DISCARD\n");
+
+       /* Cancel the implicit keep.  That's all there is to it. */
+       cs->cancel_implicit_keep = 1;
+       return SIEVE2_OK;
 }
 
 
+
 /*
- * Callback function to indicate that the user should be notified
- * FIXME implement this
+ * Callback function to indicate that a message should be rejected
  */
-int ctdl_notify(sieve2_context_t *s, void *my)
+int ctdl_reject(sieve2_context_t *s, void *my)
 {
-       lprintf(CTDL_DEBUG, "Action is NOTIFY\n");
-       return SIEVE2_ERROR_UNSUPPORTED;
+       struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
+       char *reject_text = NULL;
+
+       lprintf(CTDL_DEBUG, "Action is REJECT\n");
+
+       /* If we don't know who sent the message, do a DISCARD instead. */
+       if (strlen(cs->sender) == 0) {
+               lprintf(CTDL_INFO, "Unknown sender.  Doing DISCARD instead of REJECT.\n");
+               return ctdl_discard(s, my);
+       }
+
+       /* Assemble the reject message. */
+       reject_text = malloc(strlen(sieve2_getvalue_string(s, "message")) + 1024);
+       if (reject_text == NULL) {
+               return SIEVE2_ERROR_FAIL;
+       }
+
+       sprintf(reject_text, 
+               "Content-type: text/plain\n"
+               "\n"
+               "The message was refused by the recipient's mail filtering program.\n"
+               "The reason given was as follows:\n"
+               "\n"
+               "%s\n"
+               "\n"
+       ,
+               sieve2_getvalue_string(s, "message")
+       );
+
+       quickie_message(        /* This delivers the message */
+               "Citadel",      /* FIXME make it myself */
+               cs->sender,
+               NULL,
+               reject_text,
+               FMT_RFC822,
+               "Delivery status notification"
+       );
+
+       free(reject_text);
+       cs->cancel_implicit_keep = 1;
+       return SIEVE2_OK;
 }
 
 
+
 /*
  * Callback function to indicate that a vacation message should be generated
- * FIXME implement this
  */
 int ctdl_vacation(sieve2_context_t *s, void *my)
 {
+       struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
+       struct sdm_vacation *vptr;
+       int days = 1;
+       const char *message;
+       char *vacamsg_text = NULL;
+       char vacamsg_subject[1024];
+
        lprintf(CTDL_DEBUG, "Action is VACATION\n");
-       return SIEVE2_ERROR_UNSUPPORTED;
+
+       message = sieve2_getvalue_string(s, "message");
+       if (message == NULL) return SIEVE2_ERROR_BADARGS;
+
+       if (sieve2_getvalue_string(s, "subject") != NULL) {
+               safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject);
+       }
+       else {
+               snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject);
+       }
+
+       days = sieve2_getvalue_int(s, "days");
+       if (days < 1) days = 1;
+       if (days > MAX_VACATION) days = MAX_VACATION;
+
+       /* Check to see whether we've already alerted this sender that we're on vacation. */
+       for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
+               if (!strcasecmp(vptr->fromaddr, cs->sender)) {
+                       if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
+                               lprintf(CTDL_DEBUG, "Already alerted <%s> recently.\n", cs->sender);
+                               return SIEVE2_OK;
+                       }
+               }
+       }
+
+       /* Assemble the reject message. */
+       vacamsg_text = malloc(strlen(message) + 1024);
+       if (vacamsg_text == NULL) {
+               return SIEVE2_ERROR_FAIL;
+       }
+
+       sprintf(vacamsg_text, 
+               "Content-type: text/plain\n"
+               "\n"
+               "%s\n"
+               "\n"
+       ,
+               message
+       );
+
+       quickie_message(        /* This delivers the message */
+               "Citadel",      /* FIXME make it myself */
+               cs->sender,
+               NULL,
+               vacamsg_text,
+               FMT_RFC822,
+               vacamsg_subject
+       );
+
+       free(vacamsg_text);
+
+       /* Now update the list to reflect the fact that we've alerted this sender.
+        * If they're already in the list, just update the timestamp.
+        */
+       for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
+               if (!strcasecmp(vptr->fromaddr, cs->sender)) {
+                       vptr->timestamp = time(NULL);
+                       return SIEVE2_OK;
+               }
+       }
+
+       /* If we get to this point, create a new record.
+        */
+       vptr = malloc(sizeof(struct sdm_vacation));
+       vptr->timestamp = time(NULL);
+       safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
+       vptr->next = cs->u->first_vacation;
+       cs->u->first_vacation = vptr;
+
+       return SIEVE2_OK;
 }
 
 
 /*
  * Callback function to parse addresses per local system convention
- * FIXME implement this
+ * It is disabled because we don't support subaddresses.
  */
+#if 0
 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
 {
-       return SIEVE2_ERROR_UNSUPPORTED;
+       struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
+
+       /* libSieve does not take ownership of the memory used here.  But, since we
+        * are just pointing to locations inside a struct which we are going to free
+        * later, we're ok.
+        */
+       sieve2_setvalue_string(s, "user", cs->recp_user);
+       sieve2_setvalue_string(s, "detail", "");
+       sieve2_setvalue_string(s, "localpart", cs->recp_user);
+       sieve2_setvalue_string(s, "domain", cs->recp_node);
+       return SIEVE2_OK;
 }
+#endif
 
 
 /*
  * Callback function to parse message envelope
- * FIXME implement this
  */
 int ctdl_getenvelope(sieve2_context_t *s, void *my)
 {
-       return SIEVE2_ERROR_UNSUPPORTED;
+       struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
+
+       lprintf(CTDL_DEBUG, "Action is GETENVELOPE\n");
+       sieve2_setvalue_string(s, "to", cs->envelope_to);
+       sieve2_setvalue_string(s, "from", cs->envelope_from);
+       return SIEVE2_OK;
 }
 
 
@@ -275,58 +406,50 @@ int ctdl_getenvelope(sieve2_context_t *s, void *my)
  * Callback function to fetch message body
  * FIXME implement this
  */
+#if 0
 int ctdl_getbody(sieve2_context_t *s, void *my)
 {
        return SIEVE2_ERROR_UNSUPPORTED;
 }
+#endif
 
 
 /*
  * Callback function to fetch message size
- * FIXME implement this
  */
 int ctdl_getsize(sieve2_context_t *s, void *my)
-{
-       return SIEVE2_ERROR_UNSUPPORTED;
-}
-
-
-/*
- * Callback function to indicate that a message should be discarded.
- */
-int ctdl_discard(sieve2_context_t *s, void *my)
 {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
+       struct MetaData smi;
 
-       lprintf(CTDL_DEBUG, "Action is DISCARD\n");
+       GetMetaData(&smi, cs->msgnum);
+       
+       if (smi.meta_rfc822_length > 0L) {
+               sieve2_setvalue_int(s, "size", (int)smi.meta_rfc822_length);
+               return SIEVE2_OK;
+       }
 
-       /* Yes, this is really all there is to it.  Since we are not setting "keep" to 1,
-        * the message will be discarded because "some other action" was successfully taken.
-        */
-       cs->actiontaken = 1;
-       return SIEVE2_OK;
+       return SIEVE2_ERROR_UNSUPPORTED;
 }
 
 
-
 /*
  * Callback function to retrieve the sieve script
  */
 int ctdl_getscript(sieve2_context_t *s, void *my) {
        struct sdm_script *sptr;
+       struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
 
-       lprintf(CTDL_DEBUG, "ctdl_getscript() was called\n");
-
-       for (sptr=u->first_script; sptr!=NULL; sptr=sptr->next) {
+       for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
                if (sptr->script_active > 0) {
                        lprintf(CTDL_DEBUG, "ctdl_getscript() is using script '%s'\n", sptr->script_name);
                        sieve2_setvalue_string(s, "script", sptr->script_content);
-                       return SIEVE2_OK;
+                       return SIEVE2_OK;
                }
        }
                
        lprintf(CTDL_DEBUG, "ctdl_getscript() found no active script\n");
-        return SIEVE2_ERROR_GETSCRIPT;
+       return SIEVE2_ERROR_GETSCRIPT;
 }
 
 /*
@@ -370,22 +493,73 @@ void sieve_do_msg(long msgnum, void *userdata) {
        sieve2_context_t *sieve2_context = u->sieve2_context;
        struct ctdl_sieve my;
        int res;
+       struct CtdlMessage *msg;
 
        lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
 
+       msg = CtdlFetchMessage(msgnum, 0);
+       if (msg == NULL) return;
+
        CC->redirect_buffer = malloc(SIZ);
        CC->redirect_len = 0;
        CC->redirect_alloc = SIZ;
-       CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ONLY, 0, 1, NULL);
+       CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
        my.rfc822headers = CC->redirect_buffer;
        CC->redirect_buffer = NULL;
        CC->redirect_len = 0;
        CC->redirect_alloc = 0;
 
-       my.keep = 0;            /* Don't keep a copy in the inbox unless a callback tells us to do so */
-       my.actiontaken = 0;     /* Keep track of whether any actions were successfully taken */
+       my.keep = 0;                            /* Set to 1 to declare an *explicit* keep */
+       my.cancel_implicit_keep = 0;            /* Some actions will cancel the implicit keep */
        my.usernum = atol(CC->room.QRname);     /* Keep track of the owner of the room's namespace */
-       my.msgnum = msgnum;     /* Keep track of the message number in our local store */
+       my.msgnum = msgnum;                     /* Keep track of the message number in our local store */
+       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);
+
+       /* 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);
+       }
+       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 {
+               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);
+       }
+       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);
+       }
+       else if (msg->cm_fields['F'] != NULL) {
+               safestrncpy(my.envelope_from, msg->cm_fields['F'], sizeof my.envelope_from);
+       }
+       else {
+               strcpy(my.envelope_from, "");
+       }
+
+       /* Keep track of the envelope-to address */
+       if (msg->cm_fields['V'] != NULL) {
+               safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
+       }
+       else {
+               strcpy(my.envelope_to, "");
+       }
+
+       free(msg);
 
        sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
        
@@ -402,7 +576,7 @@ void sieve_do_msg(long msgnum, void *userdata) {
         * Delete the message from the inbox unless either we were told not to, or
         * if no other action was successfully taken.
         */
-       if ( (!my.keep) && (my.actiontaken) ) {
+       if ( (!my.keep) && (my.cancel_implicit_keep) ) {
                lprintf(CTDL_DEBUG, "keep is 0 -- deleting message from inbox\n");
                CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "", 0);
        }
@@ -421,9 +595,10 @@ void sieve_do_msg(long msgnum, void *userdata) {
  */
 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
        char *ptr;
-       char *c;
+       char *c, *vacrec;
        char keyword[256];
        struct sdm_script *sptr;
+       struct sdm_vacation *vptr;
 
        ptr = conf;
        while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
@@ -431,7 +606,6 @@ void parse_sieve_config(char *conf, struct sdm_userdata *u) {
                ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
 
                extract_token(keyword, c, 0, '|', sizeof keyword);
-               lprintf(CTDL_DEBUG, "CONFIG: <%s>\n", keyword);
 
                if (!strcasecmp(keyword, "lastproc")) {
                        u->lastproc = extract_long(c, 1);
@@ -439,8 +613,9 @@ void parse_sieve_config(char *conf, struct sdm_userdata *u) {
 
                else if (!strcasecmp(keyword, "script")) {
                        sptr = malloc(sizeof(struct sdm_script));
-                       sptr->script_active = extract_int(c, 1);
-                       extract_token(sptr->script_name, c, 2, '|', sizeof sptr->script_name);
+                       extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
+                       sptr->script_active = extract_int(c, 2);
+                       remove_token(c, 0, '|');
                        remove_token(c, 0, '|');
                        remove_token(c, 0, '|');
                        sptr->script_content = strdup(c);
@@ -448,9 +623,25 @@ void parse_sieve_config(char *conf, struct sdm_userdata *u) {
                        u->first_script = sptr;
                }
 
+               else if (!strcasecmp(keyword, "vacation")) {
+
+                       if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) {
+
+                               *c = 0;
+                               ++c;
+
+                               if (strncasecmp(vacrec, "vacation|", 9)) {
+                                       vptr = malloc(sizeof(struct sdm_vacation));
+                                       extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr);
+                                       vptr->timestamp = extract_long(vacrec, 1);
+                                       vptr->next = u->first_vacation;
+                                       u->first_vacation = vptr;
+                               }
+                       }
+               }
+
                /* ignore unknown keywords */
        }
-       lprintf(CTDL_DEBUG, "done parsing config\n");
 }
 
 /*
@@ -487,6 +678,7 @@ void get_sieve_config_backend(long msgnum, void *userdata) {
 void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
        char *text;
        struct sdm_script *sptr;
+       struct sdm_vacation *vptr;
 
 
        text = malloc(1024);
@@ -513,6 +705,29 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
                free(sptr);
        }
 
+       if (u->first_vacation != NULL) {
+
+               size_t realloc_len = strlen(text) + 256;
+               for (vptr = u->first_vacation; vptr != NULL; vptr = vptr->next) {
+                       realloc_len += strlen(vptr->fromaddr + 32);
+               }
+               text = realloc(text, realloc_len);
+
+               sprintf(&text[strlen(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
+                               );
+                       }
+                       vptr = u->first_vacation;
+                       u->first_vacation = u->first_vacation->next;
+                       free(vptr);
+               }
+               sprintf(&text[strlen(text)], CTDLSIEVECONFIGSEPARATOR);
+       }
+
        /* Save the config */
        quickie_message("Citadel", NULL, u->config_roomname,
                        text,
@@ -528,6 +743,34 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
 }
 
 
+/*
+ * This is our callback registration table for libSieve.
+ */
+sieve2_callback_t ctdl_sieve_callbacks[] = {
+       { SIEVE2_ACTION_REJECT,         ctdl_reject             },
+       { SIEVE2_ACTION_VACATION,       ctdl_vacation           },
+       { SIEVE2_ERRCALL_PARSE,         ctdl_errparse           },
+       { SIEVE2_ERRCALL_RUNTIME,       ctdl_errexec            },
+       { SIEVE2_ACTION_FILEINTO,       ctdl_fileinto           },
+       { SIEVE2_ACTION_REDIRECT,       ctdl_redirect           },
+       { SIEVE2_ACTION_DISCARD,        ctdl_discard            },
+       { SIEVE2_ACTION_KEEP,           ctdl_keep               },
+       { SIEVE2_SCRIPT_GETSCRIPT,      ctdl_getscript          },
+       { SIEVE2_DEBUG_TRACE,           ctdl_debug              },
+       { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders         },
+       { SIEVE2_MESSAGE_GETSIZE,       ctdl_getsize            },
+       { SIEVE2_MESSAGE_GETENVELOPE,   ctdl_getenvelope        },
+/*
+ * These actions are unsupported by Citadel so we don't declare them.
+ *
+       { SIEVE2_ACTION_NOTIFY,         ctdl_notify             },
+       { SIEVE2_MESSAGE_GETSUBADDRESS, ctdl_getsubaddress      },
+       { SIEVE2_MESSAGE_GETBODY,       ctdl_getbody            },
+ *
+ */
+       { 0 }
+};
+
 
 /*
  * Perform sieve processing for a single room
@@ -539,33 +782,12 @@ void sieve_do_room(char *roomname) {
        int res;                                        /* Return code from libsieve calls */
        long orig_lastproc = 0;
 
-       /*
-        * This is our callback registration table for libSieve.
-        */
-       sieve2_callback_t ctdl_sieve_callbacks[] = {
-               { SIEVE2_ACTION_REJECT,         ctdl_reject             },
-               { SIEVE2_ACTION_NOTIFY,         ctdl_notify             },
-               { SIEVE2_ACTION_VACATION,       ctdl_vacation           },
-               { SIEVE2_ERRCALL_PARSE,         ctdl_errparse           },
-               { SIEVE2_ERRCALL_RUNTIME,       ctdl_errexec            },
-               { SIEVE2_ACTION_FILEINTO,       ctdl_fileinto           },
-               { SIEVE2_ACTION_REDIRECT,       ctdl_redirect           },
-               { SIEVE2_ACTION_DISCARD,        ctdl_discard            },
-               { SIEVE2_ACTION_KEEP,           ctdl_keep               },
-               { SIEVE2_SCRIPT_GETSCRIPT,      ctdl_getscript          },
-               { SIEVE2_DEBUG_TRACE,           ctdl_debug              },
-               { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders         },
-               { SIEVE2_MESSAGE_GETSUBADDRESS, ctdl_getsubaddress      },
-               { SIEVE2_MESSAGE_GETENVELOPE,   ctdl_getenvelope        },
-               { SIEVE2_MESSAGE_GETBODY,       ctdl_getbody            },
-               { SIEVE2_MESSAGE_GETSIZE,       ctdl_getsize            },
-               { 0 }
-       };
+       memset(&u, 0, sizeof u);
 
        /* See if the user who owns this 'mailbox' has any Sieve scripts that
         * require execution.
         */
-       snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), SIEVERULES);
+       snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), USERCONFIGROOM);
        if (getroom(&CC->room, u.config_roomname) != 0) {
                lprintf(CTDL_DEBUG, "<%s> does not exist.  No processing is required.\n", u.config_roomname);
                return;
@@ -606,7 +828,10 @@ void sieve_do_room(char *roomname) {
 
        /* Validate the script */
 
-       res = sieve2_validate(sieve2_context, NULL);
+       struct ctdl_sieve my;           /* dummy ctdl_sieve struct just to pass "u" slong */
+       memset(&my, 0, sizeof my);
+       my.u = &u;
+       res = sieve2_validate(sieve2_context, &my);
        if (res != SIEVE2_OK) {
                lprintf(CTDL_CRIT, "sieve2_validate() returned %d: %s\n", res, sieve2_errstr(res));
                goto BAIL;
@@ -673,7 +898,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, SIEVERULES) == 0) {
+       if (getroom(&CC->room, USERCONFIGROOM) == 0) {
        
                u->config_msgnum = (-1);
                strcpy(u->config_roomname, CC->room.QRname);
@@ -692,6 +917,104 @@ void msiv_store(struct sdm_userdata *u) {
 }
 
 
+/*
+ * Select the active script.
+ * (Set script_name to an empty string to disable all scripts)
+ * 
+ * Returns 0 on success or nonzero for error.
+ */
+int msiv_setactive(struct sdm_userdata *u, char *script_name) {
+       int ok = 0;
+       struct sdm_script *s;
+
+       /* First see if the supplied value is ok */
+
+       if (strlen(script_name) == 0) {
+               ok = 1;
+       }
+       else {
+               for (s=u->first_script; s!=NULL; s=s->next) {
+                       if (!strcasecmp(s->script_name, script_name)) {
+                               ok = 1;
+                       }
+               }
+       }
+
+       if (!ok) return(-1);
+
+       /* Now set the active script */
+       for (s=u->first_script; s!=NULL; s=s->next) {
+               if (!strcasecmp(s->script_name, script_name)) {
+                       s->script_active = 1;
+               }
+               else {
+                       s->script_active = 0;
+               }
+       }
+       
+       return(0);
+}
+
+
+/*
+ * Fetch a script by name.
+ *
+ * Returns NULL if the named script was not found, or a pointer to the script
+ * if it was found.   NOTE: the caller does *not* own the memory returned by
+ * this function.  Copy it if you need to keep it.
+ */
+char *msiv_getscript(struct sdm_userdata *u, char *script_name) {
+       struct sdm_script *s;
+
+       for (s=u->first_script; s!=NULL; s=s->next) {
+               if (!strcasecmp(s->script_name, script_name)) {
+                       if (s->script_content != NULL) {
+                               return (s->script_content);
+                       }
+               }
+       }
+
+       return(NULL);
+}
+
+
+/*
+ * Delete a script by name.
+ *
+ * Returns 0 if the script was deleted.
+ *      1 if the script was not found.
+ *      2 if the script cannot be deleted because it is active.
+ */
+int msiv_deletescript(struct sdm_userdata *u, char *script_name) {
+       struct sdm_script *s = NULL;
+       struct sdm_script *script_to_delete = NULL;
+
+       for (s=u->first_script; s!=NULL; s=s->next) {
+               if (!strcasecmp(s->script_name, script_name)) {
+                       script_to_delete = s;
+                       if (s->script_active) {
+                               return(2);
+                       }
+               }
+       }
+
+       if (script_to_delete == NULL) return(1);
+
+       if (u->first_script == script_to_delete) {
+               u->first_script = u->first_script->next;
+       }
+       else for (s=u->first_script; s!=NULL; s=s->next) {
+               if (s->next == script_to_delete) {
+                       s->next = s->next->next;
+               }
+       }
+
+       free(script_to_delete->script_content);
+       free(script_to_delete);
+       return(0);
+}
+
+
 /*
  * Add or replace a new script.  
  * NOTE: after this function returns, "u" owns the memory that "script_content"
@@ -715,12 +1038,14 @@ void msiv_putscript(struct sdm_userdata *u, char *script_name, char *script_cont
                sptr = malloc(sizeof(struct sdm_script));
                safestrncpy(sptr->script_name, script_name, sizeof sptr->script_name);
                sptr->script_content = script_content;
+               sptr->script_active = 0;
                sptr->next = u->first_script;
                u->first_script = sptr;
        }
 }
 
 
+
 /*
  * Citadel protocol to manage sieve scripts.
  * This is basically a simplified (read: doesn't resemble IMAP) version
@@ -731,6 +1056,8 @@ void cmd_msiv(char *argbuf) {
        struct sdm_userdata u;
        char script_name[256];
        char *script_content = NULL;
+       struct sdm_script *s;
+       int i;
 
        memset(&u, 0, sizeof(struct sdm_userdata));
 
@@ -751,15 +1078,61 @@ void cmd_msiv(char *argbuf) {
        }       
        
        else if (!strcasecmp(subcmd, "listscripts")) {
+               cprintf("%d Scripts:\n", LISTING_FOLLOWS);
+               for (s=u.first_script; s!=NULL; s=s->next) {
+                       if (s->script_content != NULL) {
+                               cprintf("%s|%d|\n", s->script_name, s->script_active);
+                       }
+               }
+               cprintf("000\n");
        }
 
        else if (!strcasecmp(subcmd, "setactive")) {
+               extract_token(script_name, argbuf, 1, '|', sizeof script_name);
+               if (msiv_setactive(&u, script_name) == 0) {
+                       cprintf("%d ok\n", CIT_OK);
+               }
+               else {
+                       cprintf("%d Script '%s' does not exist.\n",
+                               ERROR + ILLEGAL_VALUE,
+                               script_name
+                       );
+               }
        }
 
        else if (!strcasecmp(subcmd, "getscript")) {
+               extract_token(script_name, argbuf, 1, '|', sizeof script_name);
+               script_content = msiv_getscript(&u, script_name);
+               if (script_content != NULL) {
+                       cprintf("%d Script:\n", LISTING_FOLLOWS);
+                       cprintf("%s000\n", script_content);
+               }
+               else {
+                       cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
+               }
        }
 
        else if (!strcasecmp(subcmd, "deletescript")) {
+               extract_token(script_name, argbuf, 1, '|', sizeof script_name);
+               i = msiv_deletescript(&u, script_name);
+               if (i == 0) {
+                       cprintf("%d ok\n", CIT_OK);
+               }
+               else if (i == 1) {
+                       cprintf("%d Script '%s' does not exist.\n",
+                               ERROR + ILLEGAL_VALUE,
+                               script_name
+                       );
+               }
+               else if (i == 2) {
+                       cprintf("%d Script '%s' is active and cannot be deleted.\n",
+                               ERROR + ILLEGAL_VALUE,
+                               script_name
+                       );
+               }
+               else {
+                       cprintf("%d unknown error\n", ERROR);
+               }
        }
 
        else {
@@ -771,15 +1144,17 @@ void cmd_msiv(char *argbuf) {
 
 
 
-/*
- *     We don't really care about dumping the entire credits to the log
- *     every time the server is initialized.  The documentation will suffice
- *     for that purpose.  We are making a call to sieve2_credits() in order
- *     to demonstrate that we have successfully linked in to libsieve.
- */
-void log_the_sieve2_credits(void) {
+void ctdl_sieve_init(void) {
        char *cred = NULL;
+       sieve2_context_t *sieve2_context = NULL;
+       int res;
 
+       /*
+        *      We don't really care about dumping the entire credits to the log
+        *      every time the server is initialized.  The documentation will suffice
+        *      for that purpose.  We are making a call to sieve2_credits() in order
+        *      to demonstrate that we have successfully linked in to libsieve.
+        */
        cred = strdup(sieve2_credits());
        if (cred == NULL) return;
 
@@ -789,14 +1164,38 @@ void log_the_sieve2_credits(void) {
 
        lprintf(CTDL_INFO, "%s\n",cred);
        free(cred);
+
+       /* Briefly initialize a Sieve parser instance just so we can list the
+        * extensions that are available.
+        */
+       res = sieve2_alloc(&sieve2_context);
+       if (res != SIEVE2_OK) {
+               lprintf(CTDL_CRIT, "sieve2_alloc() returned %d: %s\n", res, sieve2_errstr(res));
+               return;
+       }
+
+       res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
+       if (res != SIEVE2_OK) {
+               lprintf(CTDL_CRIT, "sieve2_callbacks() returned %d: %s\n", res, sieve2_errstr(res));
+               goto BAIL;
+       }
+
+       msiv_extensions = strdup(sieve2_listextensions(sieve2_context));
+       lprintf(CTDL_INFO, "Extensions: %s\n", msiv_extensions);
+
+BAIL:  res = sieve2_free(&sieve2_context);
+       if (res != SIEVE2_OK) {
+               lprintf(CTDL_CRIT, "sieve2_free() returned %d: %s\n", res, sieve2_errstr(res));
+       }
+
 }
 
 
 
 char *serv_sieve_init(void)
 {
-       log_the_sieve2_credits();
-        CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
+       ctdl_sieve_init();
+       CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
        return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
 }