]> 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 2bba25abf2bf6e88140bd161474b4c24f2915794..5a80cba1918351ceff4848187446c76b811df008 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;
@@ -221,9 +223,7 @@ int ctdl_discard(sieve2_context_t *s, void *my)
 
        lprintf(CTDL_DEBUG, "Action is DISCARD\n");
 
-       /* 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.
-        */
+       /* Cancel the implicit keep.  That's all there is to it. */
        cs->cancel_implicit_keep = 1;
        return SIEVE2_OK;
 }
@@ -265,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,
@@ -282,12 +282,87 @@ 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;
+       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;
 }
 
 
@@ -315,11 +390,15 @@ int ctdl_getsubaddress(sieve2_context_t *s, void *my)
 
 /*
  * 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;
 }
 
 
@@ -327,10 +406,12 @@ 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
 
 
 /*
@@ -451,6 +532,33 @@ void sieve_do_msg(long msgnum, void *userdata) {
                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);
@@ -487,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) {
@@ -514,6 +623,23 @@ 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 */
        }
 }
@@ -552,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);
@@ -578,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,
@@ -597,22 +747,27 @@ 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_NOTIFY,         NULL    /* 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, NULL    /* ctdl_getsubaddress */        },
-       { SIEVE2_MESSAGE_GETENVELOPE,   ctdl_getenvelope                        },
-       { SIEVE2_MESSAGE_GETBODY,       ctdl_getbody                            },
-       { SIEVE2_MESSAGE_GETSIZE,       ctdl_getsize                            },
+       { 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 }
 };
 
@@ -632,7 +787,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;
@@ -743,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);
@@ -949,7 +1104,7 @@ 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("%d Script:\n", LISTING_FOLLOWS);
                        cprintf("%s000\n", script_content);
                }
                else {