]> code.citadel.org Git - citadel.git/commitdiff
Completed Sieve VACATION handler. Still needs testing.
authorArt Cancro <ajc@citadel.org>
Tue, 24 Oct 2006 03:55:38 +0000 (03:55 +0000)
committerArt Cancro <ajc@citadel.org>
Tue, 24 Oct 2006 03:55:38 +0000 (03:55 +0000)
citadel/serv_sieve.c
citadel/serv_sieve.h

index cf5305b95717425b669dd177444e57221f25ad8b..d478c4af7ffecaaafdc1f1a43e83a2709649c2e2 100644 (file)
@@ -62,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;
@@ -263,7 +265,7 @@ int ctdl_reject(sieve2_context_t *s, void *my)
        );
 
        quickie_message(        /* This delivers the message */
-               "Citadel",
+               "Citadel",      /* FIXME make it myself */
                cs->sender,
                NULL,
                reject_text,
@@ -280,28 +282,79 @@ int ctdl_reject(sieve2_context_t *s, void *my)
 
 /*
  * 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;
 
        lprintf(CTDL_DEBUG, "Action is VACATION\n");
 
-               // sieve2_getvalue_string(s, "hash"),
-               // sieve2_getvalue_int(s, "days") );
+       message = sieve2_getvalue_string(s, "message");
+       if (message == NULL) return SIEVE2_ERROR_BADARGS;
+
+       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,
+               "Delivery status notification"
+       );
+
+       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;
+               }
+       }
 
-       /* add hash to list - FIXME only do this on a miss */
+       /* If we get to this point, create a new record.
+        */
        vptr = malloc(sizeof(struct sdm_vacation));
        vptr->timestamp = time(NULL);
-       safestrncpy(vptr->hash, sieve2_getvalue_string(s, "hash"), sizeof vptr->hash);
+       safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
        vptr->next = cs->u->first_vacation;
        cs->u->first_vacation = vptr;
 
        return SIEVE2_OK;
-       /* return SIEVE2_ERROR_UNSUPPORTED; */
 }
 
 
@@ -565,7 +618,7 @@ void parse_sieve_config(char *conf, struct sdm_userdata *u) {
                else if (!strcasecmp(keyword, "vacation")) {
                        vptr = malloc(sizeof(struct sdm_vacation));
                        vptr->timestamp = extract_long(c, 1);
-                       extract_token(vptr->hash, c, 2, '|', sizeof vptr->hash);
+                       extract_token(vptr->fromaddr, c, 2, '|', sizeof vptr->fromaddr);
                        vptr->next = u->first_vacation;
                        u->first_vacation = vptr;
                }
@@ -637,10 +690,10 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u) {
 
        while (u->first_vacation != NULL) {
                if ( (time(NULL) - u->first_vacation->timestamp) < MAX_VACATION) {
-                       text = realloc(text, strlen(text) + strlen(u->first_vacation->hash) + 256);
+                       text = realloc(text, strlen(text) + strlen(u->first_vacation->fromaddr) + 256);
                        sprintf(&text[strlen(text)], "vacation|%ld|%s" CTDLSIEVECONFIGSEPARATOR,
                                u->first_vacation->timestamp,
-                               u->first_vacation->hash
+                               u->first_vacation->fromaddr
                        );
                }
                vptr = u->first_vacation;
index e4ded2897cf297db13baf24db448d55d416911f6..3213504f6159e121d3e679867806076f1b527dd1 100644 (file)
@@ -17,7 +17,7 @@ struct sdm_script {
 
 struct sdm_vacation {
        struct sdm_vacation *next;
-       char hash[256];
+       char fromaddr[256];
        time_t timestamp;
 };
 
@@ -50,11 +50,11 @@ struct ctdl_sieve {
 /* If you change this string you will break all of your Sieve configs. */
 #define CTDLSIEVECONFIGSEPARATOR       "\n-=<CtdlSieveConfigSeparator>=-\n"
 
-/* Maximum time we keep vacation hash records online.  This implies that a vacation
- * rule cannot exceed this amount of time.  5184000 seconds == 60 days, which is
- * way too long for anyone to be on vacation.
+/* Maximum time we keep vacation fromaddr records online.  This implies that a vacation
+ * rule cannot exceed this amount of time.   (Any more than 30 days is a ridiculously
+ * long vacation which the person probably doesn't deserve.)
  */
-#define MAX_VACATION                   5184000
+#define MAX_VACATION   30
 
 extern struct RoomProcList *sieve_list;