struct recptypes now uses dynamically allocated
[citadel.git] / citadel / serv_sieve.c
index e1be346b37a9dff8bfb112417078b6fc77fc46e5..e8277d697324ff5dc7061fc13bab3a066dd82b5e 100644 (file)
@@ -12,7 +12,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
-#include <signal.h>
+#include <ctype.h>
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
@@ -122,20 +122,20 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
        }
        if (valid->num_error > 0) {
                lprintf(CTDL_WARNING, "REDIRECT failed: bad recipient <%s>\n", recp);
-               free(valid);
+               free_recipients(valid);
                return SIEVE2_ERROR_BADARGS;
        }
 
        msg = CtdlFetchMessage(cs->msgnum, 1);
        if (msg == NULL) {
                lprintf(CTDL_WARNING, "REDIRECT failed: unable to fetch msg %ld\n", cs->msgnum);
-               free(valid);
+               free_recipients(valid);
                return SIEVE2_ERROR_BADARGS;
        }
 
        CtdlSubmitMsg(msg, valid, NULL);
        cs->cancel_implicit_keep = 1;
-       free(valid);
+       free_recipients(valid);
        CtdlFreeMessage(msg);
        return SIEVE2_OK;
 }
@@ -184,12 +184,12 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
        c = getroom(&CC->room, foldername);
 
        /* Then a regular room name match (public and private rooms) */
-       if (c < 0) {
+       if (c != 0) {
                safestrncpy(foldername, dest_folder, sizeof foldername);
                c = getroom(&CC->room, foldername);
        }
 
-       if (c < 0) {
+       if (c != 0) {
                lprintf(CTDL_WARNING, "FILEINTO failed: target <%s> does not exist\n", dest_folder);
                return SIEVE2_ERROR_BADARGS;
        }
@@ -265,7 +265,8 @@ int ctdl_reject(sieve2_context_t *s, void *my)
        );
 
        quickie_message(        /* This delivers the message */
-               "Citadel",      /* FIXME make it myself */
+               NULL,
+               cs->envelope_to,
                cs->sender,
                NULL,
                reject_text,
@@ -290,12 +291,20 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
        int days = 1;
        const char *message;
        char *vacamsg_text = NULL;
+       char vacamsg_subject[1024];
 
        lprintf(CTDL_DEBUG, "Action is VACATION\n");
 
        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;
@@ -326,12 +335,13 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
        );
 
        quickie_message(        /* This delivers the message */
-               "Citadel",      /* FIXME make it myself */
+               NULL,
+               cs->envelope_to,
                cs->sender,
                NULL,
                vacamsg_text,
                FMT_RFC822,
-               "Delivery status notification"
+               vacamsg_subject
        );
 
        free(vacamsg_text);
@@ -396,14 +406,14 @@ int ctdl_getenvelope(sieve2_context_t *s, void *my)
 
 /*
  * Callback function to fetch message body
- * FIXME implement this
- */
-#if 0
+ * (Uncomment the code if we implement this extension)
+ *
 int ctdl_getbody(sieve2_context_t *s, void *my)
 {
        return SIEVE2_ERROR_UNSUPPORTED;
 }
-#endif
+ *
+ */
 
 
 /*
@@ -452,7 +462,6 @@ int ctdl_getheaders(sieve2_context_t *s, void *my) {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
 
        lprintf(CTDL_DEBUG, "ctdl_getheaders() was called\n");
-
        sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
        return SIEVE2_OK;
 }
@@ -486,21 +495,38 @@ void sieve_do_msg(long msgnum, void *userdata) {
        struct ctdl_sieve my;
        int res;
        struct CtdlMessage *msg;
+       int i;
+       size_t headers_len = 0;
 
        lprintf(CTDL_DEBUG, "Performing sieve processing on msg <%ld>\n", msgnum);
 
        msg = CtdlFetchMessage(msgnum, 0);
        if (msg == NULL) return;
 
+       /*
+        * Grab the message headers so we can feed them to libSieve.
+        */
        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;
 
+       /*
+        * libSieve clobbers the stack if it encounters badly formed
+        * headers.  Sanitize our headers by stripping nonprintable
+        * characters.
+        */
+       for (i=0; i<headers_len; ++i) {
+               if (!isascii(my.rfc822headers[i])) {
+                       my.rfc822headers[i] = '_';
+               }
+       }
+
        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 */
@@ -551,7 +577,7 @@ void sieve_do_msg(long msgnum, void *userdata) {
                strcpy(my.envelope_to, "");
        }
 
-       free(msg);
+       CtdlFreeMessage(msg);
 
        sieve2_setvalue_string(sieve2_context, "allheaders", my.rfc822headers);
        
@@ -570,7 +596,7 @@ void sieve_do_msg(long msgnum, void *userdata) {
         */
        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);
+               CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
        }
 
        lprintf(CTDL_DEBUG, "Completed sieve processing on msg <%ld>\n", msgnum);
@@ -666,14 +692,18 @@ void get_sieve_config_backend(long msgnum, void *userdata) {
 
 /* 
  * Write our citadel sieve config back to disk
+ * 
+ * (Set yes_write_to_disk to nonzero to make it actually write the config;
+ * otherwise it just frees the data structures.)
  */
-void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
+void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
        char *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"
@@ -685,7 +715,10 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
        );
 
        while (u->first_script != NULL) {
-               text = realloc(text, strlen(text) + strlen(u->first_script->script_content) + 256);
+               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,
@@ -699,11 +732,11 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
 
        if (u->first_vacation != NULL) {
 
-               size_t realloc_len = strlen(text) + 256;
+               tsize = strlen(text) + 256;
                for (vptr = u->first_vacation; vptr != NULL; vptr = vptr->next) {
-                       realloc_len += strlen(vptr->fromaddr + 32);
+                       tsize += strlen(vptr->fromaddr + 32);
                }
-               text = realloc(text, realloc_len);
+               text = realloc(text, tsize);
 
                sprintf(&text[strlen(text)], "vacation|\n");
                while (u->first_vacation != NULL) {
@@ -721,15 +754,16 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
        }
 
        /* Save the config */
-       quickie_message("Citadel", NULL, u->config_roomname,
+       quickie_message("Citadel", NULL, NULL, u->config_roomname,
                        text,
                        4,
                        "Sieve configuration"
        );
-
+       
+       free (text);
        /* And delete the old one */
        if (u->config_msgnum > 0) {
-               CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "", 0);
+               CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "");
        }
 
 }
@@ -779,7 +813,7 @@ void sieve_do_room(char *roomname) {
        /* 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;
@@ -844,9 +878,7 @@ BAIL:
        }
 
        /* Rewrite the config if we have to */
-       if (u.lastproc > orig_lastproc) {
-               rewrite_ctdl_sieve_config(&u);
-       }
+       rewrite_ctdl_sieve_config(&u, (u.lastproc > orig_lastproc) ) ;
 }
 
 
@@ -890,7 +922,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);
@@ -904,8 +936,8 @@ void msiv_load(struct sdm_userdata *u) {
        }
 }
 
-void msiv_store(struct sdm_userdata *u) {
-       rewrite_ctdl_sieve_config(u);
+void msiv_store(struct sdm_userdata *u, int yes_write_to_disk) {
+       rewrite_ctdl_sieve_config(u, yes_write_to_disk);
 }
 
 
@@ -1050,6 +1082,7 @@ void cmd_msiv(char *argbuf) {
        char *script_content = NULL;
        struct sdm_script *s;
        int i;
+       int changes_made = 0;
 
        memset(&u, 0, sizeof(struct sdm_userdata));
 
@@ -1063,6 +1096,7 @@ void cmd_msiv(char *argbuf) {
                        cprintf("%d Transmit script now\n", SEND_LISTING);
                        script_content = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0);
                        msiv_putscript(&u, script_name, script_content);
+                       changes_made = 1;
                }
                else {
                        cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
@@ -1083,6 +1117,7 @@ void cmd_msiv(char *argbuf) {
                extract_token(script_name, argbuf, 1, '|', sizeof script_name);
                if (msiv_setactive(&u, script_name) == 0) {
                        cprintf("%d ok\n", CIT_OK);
+                       changes_made = 1;
                }
                else {
                        cprintf("%d Script '%s' does not exist.\n",
@@ -1096,8 +1131,15 @@ void cmd_msiv(char *argbuf) {
                extract_token(script_name, argbuf, 1, '|', sizeof script_name);
                script_content = msiv_getscript(&u, script_name);
                if (script_content != NULL) {
-                       cprintf("%d Script:\n", SEND_LISTING);
-                       cprintf("%s000\n", script_content);
+                       int script_len;
+
+                       cprintf("%d Script:\n", LISTING_FOLLOWS);
+                       script_len = strlen(script_content);
+                       client_write(script_content, script_len);
+                       if (script_content[script_len-1] != '\n') {
+                               cprintf("\n");
+                       }
+                       cprintf("000\n");
                }
                else {
                        cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
@@ -1109,6 +1151,7 @@ void cmd_msiv(char *argbuf) {
                i = msiv_deletescript(&u, script_name);
                if (i == 0) {
                        cprintf("%d ok\n", CIT_OK);
+                       changes_made = 1;
                }
                else if (i == 1) {
                        cprintf("%d Script '%s' does not exist.\n",
@@ -1131,7 +1174,7 @@ void cmd_msiv(char *argbuf) {
                cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
        }
 
-       msiv_store(&u);
+       msiv_store(&u, changes_made);
 }