]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/inboxrules/serv_inboxrules.c
Next step for inbox rules processing: retrieve each message in the room using a CtdlF...
[citadel.git] / citadel / modules / inboxrules / serv_inboxrules.c
index 44294ddb9dab1aa103bdbceeddfd3e8363db6927..ccc0cfe10aa57c37aad07d4ae2b9ea2adb23b076 100644 (file)
@@ -724,101 +724,23 @@ sieve2_callback_t ctdl_sieve_callbacks[] = {
 };
 
 
-/*
- * Perform sieve processing for a single room
- */
-void sieve_do_room(char *roomname) {
-       
-       struct sdm_userdata u;
-       sieve2_context_t *sieve2_context = NULL;        /* Context for sieve parser */
-       int res;                                        /* Return code from libsieve calls */
-       long orig_lastproc = 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), USERCONFIGROOM);
-       if (CtdlGetRoom(&CC->room, u.config_roomname) != 0) {
-               syslog(LOG_DEBUG, "<%s> does not exist.  No processing is required.", u.config_roomname);
-               return;
-       }
 
-       /*
-        * Find the sieve scripts and control record and do something
-        */
-       u.config_msgnum = (-1);
-       CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL, get_sieve_config_backend, (void *)&u );
 
-       if (u.config_msgnum < 0) {
-               syslog(LOG_DEBUG, "No Sieve rules exist.  No processing is required.");
-               return;
-       }
 
-       /*
-        * 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) {
-               syslog(LOG_ERR, "ERROR: cannot load <%s>", roomname);
-               return;
-       }
 
-       /* Initialize the Sieve parser */
-       
-       res = sieve2_alloc(&sieve2_context);
-       if (res != SIEVE2_OK) {
-               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) {
-               syslog(LOG_ERR, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
-               goto BAIL;
-       }
-
-       /* Validate the script */
-
-       struct ctdl_sieve my;           /* dummy ctdl_sieve struct just to pass "u" along */
-       memset(&my, 0, sizeof my);
-       my.u = &u;
-       res = sieve2_validate(sieve2_context, &my);
-       if (res != SIEVE2_OK) {
-               syslog(LOG_ERR, "sieve2_validate() returned %d: %s", res, sieve2_errstr(res));
-               goto BAIL;
-       }
-
-       /* Do something useful */
-       u.sieve2_context = sieve2_context;
-       orig_lastproc = u.lastproc;
-       CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL, sieve_do_msg, (void *) &u);
-
-BAIL:
-       res = sieve2_free(&sieve2_context);
-       if (res != SIEVE2_OK) {
-               syslog(LOG_ERR, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
-       }
-
-       /* Rewrite the config if we have to (we're not the user right now) */
-       rewrite_ctdl_sieve_config(&u, (u.lastproc > orig_lastproc) ) ;
-}
 
 #endif
 
 
+/*
+ * The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration.
+ */
 
+// Fields to be compared
 enum {
        field_from,             
        field_tocc,             
@@ -854,7 +776,7 @@ char *field_keys[] = {
        "all"
 };
 
-
+// Field comparison operators
 enum {
        fcomp_contains,
        fcomp_notcontains,
@@ -872,6 +794,7 @@ char *fcomp_keys[] = {
        "notmatches"
 };
 
+// Actions
 enum {
        action_keep,
        action_discard,
@@ -889,6 +812,7 @@ char *action_keys[] = {
        "vacation"
 };
 
+// Size comparison operators
 enum {
        scomp_larger,
        scomp_smaller
@@ -898,6 +822,7 @@ char *scomp_keys[] = {
        "smaller"
 };
 
+// Final actions
 enum {
        final_continue,
        final_stop
@@ -907,6 +832,7 @@ char *final_keys[] = {
        "stop"
 };
 
+// This data structure represents ONE inbox rule within the configuration.
 struct irule {
        int field_compare_op;
        int compared_field;
@@ -920,6 +846,7 @@ struct irule {
        int final_action;
 };
 
+// This data structure represents the entire inbox rules configuration AND current state for a single user.
 struct inboxrules {
        long lastproc;
        int num_rules;
@@ -927,15 +854,14 @@ struct inboxrules {
 };
 
 
+// Destructor for 'struct inboxrules'
 void free_inbox_rules(struct inboxrules *ibr) {
        free(ibr->rules);
        free(ibr);
 }
 
 
-/*
- * Convert the serialized inbox rules message to a data type.
- */
+// Constructor for 'struct inboxrules' that deserializes the configuration from text input.
 struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
        int i;
 
@@ -961,7 +887,7 @@ struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
 
                // For backwards compatibility, "# WEBCIT_RULE" is an alias for "rule".
                // Prior to version 930, WebCit converted its rules to Sieve scripts, but saved the rules as comments for later re-editing.
-               // Now, the rules hidden in the comments are the real rules.
+               // Now, the rules hidden in the comments become the real rules.
                if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
                        strcpy(token, "rule|"); 
                        strcpy(&token[5], &token[14]);
@@ -969,26 +895,20 @@ struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
 
                // Lines containing actual rules are double-serialized with Base64.  It seemed like a good idea at the time :(
                if (!strncasecmp(token, "rule|", 5)) {
-                       syslog(LOG_DEBUG, "rule: %s", &token[5]);
                        remove_token(&token[5], 0, '|');
                        char *decoded_rule = malloc(strlen(token));
                        CtdlDecodeBase64(decoded_rule, &token[5], strlen(&token[5]));
-                       TRACE;
-                       syslog(LOG_DEBUG, "%s", decoded_rule);  
-
                        ibr->num_rules++;
                        ibr->rules = realloc(ibr->rules, (sizeof(struct irule) * ibr->num_rules));
                        struct irule *new_rule = &ibr->rules[ibr->num_rules - 1];
                        memset(new_rule, 0, sizeof(struct irule));
 
                        // We have a rule , now parse it
-                       syslog(LOG_DEBUG, "Detokenizing: %s", decoded_rule);
                        char rtoken[SIZ];
                        int nt = num_tokens(decoded_rule, '|');
                        for (int t=0; t<nt; ++t) {
                                extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
                                striplt(rtoken);
-                               syslog(LOG_DEBUG, "Token %d : %s", t, rtoken);
                                switch(t) {
                                        case 1:                                                                 // field to compare
                                                for (i=0; i<=field_all; ++i) {
@@ -1047,18 +967,18 @@ struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
                        free(decoded_rule);
 
                        // if we re-serialized this now, what would it look like?
-                       syslog(LOG_DEBUG, "test reserialize: 0|%s|%s|%s|%s|%ld|%s|%s|%s|%s|%s",
-                               field_keys[new_rule->compared_field],
-                               fcomp_keys[new_rule->field_compare_op],
-                               new_rule->compared_value,
-                               scomp_keys[new_rule->size_compare_op],
-                               new_rule->compared_size,
-                               action_keys[new_rule->action],
-                               new_rule->file_into,
-                               new_rule->redirect_to,
-                               new_rule->autoreply_message,
-                               final_keys[new_rule->final_action]
-                       );
+                       //syslog(LOG_DEBUG, "test reserialize: 0|%s|%s|%s|%s|%ld|%s|%s|%s|%s|%s",
+                               //field_keys[new_rule->compared_field],
+                               //fcomp_keys[new_rule->field_compare_op],
+                               //new_rule->compared_value,
+                               //scomp_keys[new_rule->size_compare_op],
+                               //new_rule->compared_size,
+                               //action_keys[new_rule->action],
+                               //new_rule->file_into,
+                               //new_rule->redirect_to,
+                               //new_rule->autoreply_message,
+                               //final_keys[new_rule->final_action]
+                       //);
                        // delete the above after moving it to a reserialize function
 
                }
@@ -1071,44 +991,63 @@ struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
        }
 
        free(sr);               // free our copy of the source buffer that has now been trashed with null bytes...
-       abort();
        return(ibr);            // and return our complex data type to the caller.
 }
 
 
+/*
+ * rename this
+ */
+void sieve_do_msg(long msgnum, void *userdata) {
+       struct inboxrules *ii = (struct sdm_userdata *) userdata;
+       syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
+}
+
+
+
+
 /*
  * A user account is identified as requring inbox processing.
  * Do it.
  */
 void do_inbox_processing_for_user(long usernum) {
+       struct CtdlMessage *msg;
+       struct inboxrules *ii;
+       char roomname[ROOMNAMELEN];
+
        if (CtdlGetUserByNumber(&CC->user, usernum) == 0) {
-               TRACE;
                if (CC->user.msgnum_inboxrules <= 0) {
                        return;                                         // this user has no inbox rules
                }
 
-               struct CtdlMessage *msg;
-               char *conf;
-               long conflen;
-       
                msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1, 1);
                if (msg == NULL) {
                        return;                                         // config msgnum is set but that message does not exist
                }
        
-               CM_GetAsField(msg, eMesageText, &conf, &conflen);
+               ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
                CM_Free(msg);
        
-               if (conf == NULL) {
+               if (ii == NULL) {
                        return;                                         // config message exists but body is null
                }
 
-               syslog(LOG_DEBUG, "RULEZ for %s", CC->user.fullname);
-               syslog(LOG_DEBUG, "%s", conf);
+               TRACE;
+               syslog(LOG_DEBUG, "inboxrules: for %s", CC->user.fullname);
 
                // do something now FIXME actually write this
 
-               free(conf);
+               snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
+               if (CtdlGetRoom(&CC->room, roomname) == 0) {
+                       syslog(LOG_DEBUG, "GOT DA ROOM!  WE R000000000L!");
+
+                               /* Do something useful */
+                               CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, sieve_do_msg, (void *) ii);
+
+               }
+
+               // FIXME reserialize our inbox rules/state and write changes back to the config room
+               free_inbox_rules(ii);
        }
 }
 
@@ -1185,6 +1124,8 @@ int serv_inboxrules_roomhook(struct ctdlroom *room) {
  * Get InBox Rules
  *
  * This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
+ * 
+ * hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
  */
 void cmd_gibr(char *argbuf) {
 
@@ -1195,11 +1136,6 @@ void cmd_gibr(char *argbuf) {
        struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1, 1);
        if (msg != NULL) {
                if (!CM_IsEmpty(msg, eMesageText)) {
-
-
-                       struct inboxrules *ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
-                       free_inbox_rules(ii);
-
                        char *token; 
                        char *rest = msg->cm_fields[eMesageText];
                        while ((token = strtok_r(rest, "\n", &rest))) {