Rules editor now encodes the form fields and saves them
authorArt Cancro <ajc@citadel.org>
Thu, 16 Nov 2006 04:32:22 +0000 (04:32 +0000)
committerArt Cancro <ajc@citadel.org>
Thu, 16 Nov 2006 04:32:22 +0000 (04:32 +0000)
into a hidden script.  Still need to write the decoding side, and the script generator.

webcit/messages.c
webcit/sieve.c
webcit/tools.c
webcit/webcit.h

index 15446c75f16d48f0b64867974648e039af644239..67d2b5054d3938e930aeb20fe827413473984da8 100644 (file)
@@ -2607,7 +2607,7 @@ void post_mime_to_server(void) {
                        encoded_length = ((att->length * 150) / 100);
                        encoded = malloc(encoded_length);
                        if (encoded == NULL) break;
-                       CtdlEncodeBase64(encoded, att->data, att->length);
+                       CtdlEncodeBase64(encoded, att->data, att->length, 1);
 
                        serv_printf("--%s", boundary);
                        serv_printf("Content-type: %s", att->content_type);
index da7c85d371a26656aa1ce0e2d4dfaf0f8f072c58..4cf8afdb3ec4a8370b31ba6a89e12d0d43c00f8d 100644 (file)
@@ -10,6 +10,7 @@
 
 #define MAX_SCRIPTS    100
 #define MAX_RULES      25
+#define RULES_SCRIPT   "__WebCit_Generated_Script__"
 
 /**
  * \brief view/edit sieve config
@@ -21,6 +22,7 @@ void display_sieve(void)
        int active_script = (-1);
        char buf[256];
        int i;
+       int rules_script_is_active = 0;
        
 
        memset(script_names, 0, sizeof script_names);
@@ -32,6 +34,9 @@ void display_sieve(void)
                        extract_token(script_names[num_scripts], buf, 0, '|', 64);
                        if (extract_int(buf, 1) > 0) {
                                active_script = num_scripts;
+                               if (!strcasecmp(script_names[num_scripts], RULES_SCRIPT)) {
+                                       rules_script_is_active = 1;
+                               }
                        }
                        ++num_scripts;
                }
@@ -90,11 +95,12 @@ void display_sieve(void)
        wprintf(_("Leave it in my inbox without filtering"));
        wprintf("</option>\n");
 
-       wprintf("<option value=\"1\">");
+       wprintf("<option %s value=\"1\">", ((rules_script_is_active) ? "selected" : ""));
        wprintf(_("Filter it according to rules selected below"));
        wprintf("</option>\n");
 
-       wprintf("<option %s value=\"2\">", ((active_script >= 0) ? "selected" : ""));
+       wprintf("<option %s value=\"2\">",
+                       (((active_script >= 0) && (!rules_script_is_active)) ? "selected" : ""));
        wprintf(_("Filter it through a manually edited script (advanced users only)"));
        wprintf("</option>\n");
 
@@ -124,11 +130,13 @@ void display_sieve(void)
                wprintf(_("The currently active script is: "));
                wprintf("<select name=\"active_script\" size=1 onChange=\"ToggleScriptPanels();\">\n");
                for (i=0; i<num_scripts; ++i) {
-                       wprintf("<option %s value=\"%s\">%s</option>\n",
-                               ((active_script == i) ? "selected" : ""),
-                               script_names[i],
-                               script_names[i]
-                       );
+                       if (strcasecmp(script_names[i], RULES_SCRIPT)) {
+                               wprintf("<option %s value=\"%s\">%s</option>\n",
+                                       ((active_script == i) ? "selected" : ""),
+                                       script_names[i],
+                                       script_names[i]
+                               );
+                       }
                }
                wprintf("</select>\n");
        }
@@ -140,16 +148,18 @@ void display_sieve(void)
 
        if (num_scripts > 0) {
                for (i=0; i<num_scripts; ++i) {
-                       wprintf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
-                       wprintf("<textarea name=\"text_%s\" wrap=soft rows=20 cols=80 width=80>\n",
-                               script_names[i]);
-                       serv_printf("MSIV getscript|%s", script_names[i]);
-                       serv_getln(buf, sizeof buf);
-                       if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
-                               wprintf("%s\n", buf);
+                       if (strcasecmp(script_names[i], RULES_SCRIPT)) {
+                               wprintf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
+                               wprintf("<textarea name=\"text_%s\" wrap=soft rows=20 cols=80 width=80>\n",
+                                       script_names[i]);
+                               serv_printf("MSIV getscript|%s", script_names[i]);
+                               serv_getln(buf, sizeof buf);
+                               if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
+                                       wprintf("%s\n", buf);
+                               }
+                               wprintf("</textarea>\n");
+                               wprintf("</div>\n");
                        }
-                       wprintf("</textarea>\n");
-                       wprintf("</div>\n");
                }
        }
 
@@ -200,10 +210,26 @@ void parse_fields_from_rule_editor(void) {
        char final[32];
 
        int i;
+       char buf[256];
        char fname[256];
        char rule[2048];
        char encoded_rule[4096];
 
+       serv_printf("MSIV putscript|%s|", RULES_SCRIPT);
+       serv_getln(buf, sizeof buf);
+       if (buf[0] != '4') {
+               return;
+       }
+
+       serv_puts("# THIS SCRIPT WAS AUTOMATICALLY GENERATED BY WEBCIT.");
+       serv_puts("# ");
+       serv_puts("# Do not attempt to manually edit it.  If you do so,");
+       serv_puts("# your changes will be overwritten the next time WebCit");
+       serv_puts("# saves its mail filtering rule set.  If you really want");
+       serv_puts("# to use these rules as the basis for another script,");
+       serv_puts("# copy them to another script and save that instead.");
+       serv_puts("");
+
        for (i=0; i<MAX_RULES; ++i) {
                
                strcpy(rule, "");
@@ -246,11 +272,16 @@ void parse_fields_from_rule_editor(void) {
                        redirect, automsg, final
                );
 
-               lprintf(9, "Rule:\n%s\n", rule);
-               CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1);
-               lprintf(9, "Encoded rule:\n%s\n", encoded_rule);
+               CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1, 0);
+               serv_printf("# WEBCIT_RULE|%d|%s|", i, encoded_rule);
+
+               /* FIXME: this is where we need to generate Sieve code based on the rule */
+
+
        }
 
+       serv_puts("000");
+
 }
 
 
@@ -295,6 +326,11 @@ void save_sieve(void) {
                serv_getln(buf, sizeof buf);
        }
 
+       else if (bigaction == 1) {
+               serv_printf("MSIV setactive|%s|", RULES_SCRIPT);
+               serv_getln(buf, sizeof buf);
+       }
+
        else if (bigaction == 2) {
                serv_printf("MSIV setactive|%s|", bstr("active_script"));
                serv_getln(buf, sizeof buf);
@@ -302,13 +338,20 @@ void save_sieve(void) {
 
        if (num_scripts > 0) {
                for (i=0; i<num_scripts; ++i) {
-                       serv_printf("MSIV putscript|%s|", script_names[i]);
-                       serv_getln(buf, sizeof buf);
-                       if (buf[0] == '4') {
-                               snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
-                               striplt(bstr(this_name));
-                               serv_printf("%s", bstr(this_name));
-                               serv_puts("000");
+                       /*
+                        * We only want to save the scripts from the "manually edited scripts"
+                        * screen.  The script that WebCit generates from its ruleset will be
+                        * auto-generated by parse_fields_from_rule_editor() and saved there.
+                        */
+                       if (strcasecmp(script_names[i], RULES_SCRIPT)) {
+                               serv_printf("MSIV putscript|%s|", script_names[i]);
+                               serv_getln(buf, sizeof buf);
+                               if (buf[0] == '4') {
+                                       snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
+                                       striplt(bstr(this_name));
+                                       serv_printf("%s", bstr(this_name));
+                                       serv_puts("000");
+                               }
                        }
                }
        }
@@ -382,7 +425,7 @@ void display_add_remove_scripts(char *message)
         if (buf[0] == '1') {
                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                         extract_token(script_name, buf, 0, '|', sizeof script_name);
-                       if (extract_int(buf, 1) == 0) {
+                       if ( (extract_int(buf, 1) == 0) && (strcasecmp(script_name, RULES_SCRIPT)) ) {
                                wprintf("<option>");
                                escputs(script_name);
                                wprintf("</option>\n");
index 0a0718381d7ae3d388ea1c6fdb7ac45b8d2d0669..f6ef5088d5a0cfe9ec04445eb0664e6a2f3a2789 100644 (file)
@@ -359,7 +359,7 @@ void sleeeeeeeeeep(int seconds)
  * \param sourcelen the length of the source data (may contain string terminators)
  */
 
-void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
+void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks)
 {
        int i, hiteof = FALSE;
        int spos = 0;
@@ -418,7 +418,7 @@ void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
                                dest[dpos] = 0;
                        }
                        thisline += 4;
-                       if (thisline > 70) {
+                       if ( (linebreaks) && (thisline > 70) ) {
                                dest[dpos++] = '\r';
                                dest[dpos++] = '\n';
                                dest[dpos] = 0;
@@ -426,7 +426,7 @@ void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
                        }
                }
        }
-       if (thisline > 70) {
+       if ( (linebreaks) && (thisline > 70) ) {
                dest[dpos++] = '\r';
                dest[dpos++] = '\n';
                dest[dpos] = 0;
index 856854d0415b035dd0cd91bf2a026aac30c84ed1..4aef6ac85daa42981c229ed348c2f04bc6ee9780 100644 (file)
@@ -616,7 +616,7 @@ void do_tasks_view(void);
 void free_calendar_buffer(void);
 void calendar_summary_view(void);
 int load_msg_ptrs(char *servcmd, int with_headers);
-void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen);
+void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks);
 int CtdlDecodeBase64(char *dest, const char *source, size_t length);
 void free_attachments(struct wcsession *sess);
 void free_march_list(struct wcsession *wcf);