]> code.citadel.org Git - citadel.git/blobdiff - webcit/sieve.c
more fields
[citadel.git] / webcit / sieve.c
index 210345ec7c794885a6f57f61e4271e6d493c0294..32bfd1bf618bb1d713b5349c3ddc51a8ab27e6b1 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,15 +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");
                }
        }
 
@@ -180,6 +191,101 @@ void display_sieve(void)
 }
 
 
+
+/**
+ * \brief Translate the fields from the rule editor into something we can save...
+ */
+void parse_fields_from_rule_editor(void) {
+
+       int active;
+       char hfield[256];
+       char compare[32];
+       char htext[256];
+       char sizecomp[32];
+       int sizeval;
+       char action[32];
+       char fileinto[128];
+       char redirect[256];
+       char automsg[1024];
+       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, "");
+
+               sprintf(fname, "active%d", i);
+               active = !strcasecmp(bstr(fname), "on") ;
+
+               sprintf(fname, "hfield%d", i);
+               safestrncpy(hfield, bstr(fname), sizeof hfield);
+
+               sprintf(fname, "compare%d", i);
+               safestrncpy(compare, bstr(fname), sizeof compare);
+
+               sprintf(fname, "htext%d", i);
+               safestrncpy(htext, bstr(fname), sizeof htext);
+
+               sprintf(fname, "sizecomp%d", i);
+               safestrncpy(sizecomp, bstr(fname), sizeof sizecomp);
+
+               sprintf(fname, "sizeval%d", i);
+               sizeval = atoi(bstr(fname));
+
+               sprintf(fname, "action%d", i);
+               safestrncpy(action, bstr(fname), sizeof action);
+
+               sprintf(fname, "fileinto%d", i);
+               safestrncpy(fileinto, bstr(fname), sizeof fileinto);
+
+               sprintf(fname, "redirect%d", i);
+               safestrncpy(redirect, bstr(fname), sizeof redirect);
+
+               sprintf(fname, "automsg%d", i);
+               safestrncpy(automsg, bstr(fname), sizeof automsg);
+
+               sprintf(fname, "final%d", i);
+               safestrncpy(final, bstr(fname), sizeof final);
+
+               snprintf(rule, sizeof rule, "%d|%s|%s|%s|%s|%d|%s|%s|%s|%s|%s",
+                       active, hfield, compare, htext, sizecomp, sizeval, action, fileinto,
+                       redirect, automsg, final
+               );
+
+               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");
+
+}
+
+
+
 /**
  * \brief save sieve config
  */
@@ -199,6 +305,8 @@ void save_sieve(void) {
                return;
        }
 
+       parse_fields_from_rule_editor();
+
        serv_puts("MSIV listscripts");
        serv_getln(buf, sizeof(buf));
        if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
@@ -218,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);
@@ -225,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");
+                               }
                        }
                }
        }
@@ -305,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");
@@ -373,7 +493,8 @@ void create_script(void) {
 
 void display_rules_editor_inner_div(void) {
        int i, j;
-       char buf[256];
+       char buf[4096];
+       char rules[MAX_RULES][2048];
 
        struct {
                char name[128];
@@ -381,6 +502,30 @@ void display_rules_editor_inner_div(void) {
        int num_roomnames = 0;
        int num_roomnames_alloc = 0;
 
+       int active;
+       char hfield[256];
+       char compare[32];
+       char htext[256];
+       char sizecomp[32];
+       int sizeval;
+       char action[32];
+       char fileinto[128];
+       char redirect[256];
+       char automsg[1024];
+       char final[32];
+
+       /* load the rules */
+       memset(rules, 0, sizeof rules);
+       serv_printf("MSIV getscript|%s", RULES_SCRIPT);
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
+               if (!strncasecmp(buf, "# WEBCIT_RULE|", 14)) {
+                       j = extract_int(buf, 1);
+                       remove_token(buf, 0, '|');
+                       remove_token(buf, 0, '|');
+                       CtdlDecodeBase64(rules[j], buf, strlen(buf));
+               }
+       }
 
        /* load the roomnames */
        serv_puts("LKRA");
@@ -418,14 +563,17 @@ void display_rules_editor_inner_div(void) {
                "    if (d == 'all') {                                                  \n"
                "      $('div_size'+i).style.display = 'none';                          \n"
                "      $('div_compare'+i).style.display = 'none';                       \n"
+               "      $('div_nocompare'+i).style.display = 'block';                    \n"
                "    }                                                                  \n"
                "    else if (d == 'size') {                                            \n"
                "      $('div_size'+i).style.display = 'block';                         \n"
                "      $('div_compare'+i).style.display = 'none';                       \n"
+               "      $('div_nocompare'+i).style.display = 'none';                     \n"
                "    }                                                                  \n"
                "    else {                                                             \n"
                "      $('div_size'+i).style.display = 'none';                          \n"
                "      $('div_compare'+i).style.display = 'block';                      \n"
+               "      $('div_nocompare'+i).style.display = 'none';                     \n"
                "    }                                                                  \n"
                "    d = ($('action'+i).options[$('action'+i).selectedIndex].value);    \n"
                "    if (d == 'fileinto') {                                             \n"
@@ -498,8 +646,7 @@ void display_rules_editor_inner_div(void) {
                "  }                                                                    \n"
                "}                                                                      \n"
 /*
- * Delete a rule (percolate the deleted rule out to the end,
- *                and then decrement highest_active_rule)
+ * Delete a rule (percolate the deleted rule out to the end, then deactivate it)
  */
                "function DeleteRule(rd) {                                              \n"
                "  for (i=rd; i<highest_active_rule; ++i) {                             \n"
@@ -513,80 +660,146 @@ void display_rules_editor_inner_div(void) {
 
        wprintf("<br />");
 
-       wprintf("<table border=1 cellpadding=2 width=100%%>");
+       wprintf("<table cellpadding=2 width=100%%>");
 
        for (i=0; i<MAX_RULES; ++i) {
+
+               /* Grab our existing values to populate */
+               active = extract_int(rules[i], 0);
+               extract_token(hfield, rules[i], 1, '|', sizeof hfield);
+               extract_token(compare, rules[i], 2, '|', sizeof compare);
+               extract_token(htext, rules[i], 3, '|', sizeof htext);
+               extract_token(sizecomp, rules[i], 4, '|', sizeof sizecomp);
+               sizeval = extract_int(rules[i], 5);
+               extract_token(action, rules[i], 6, '|', sizeof action);
+               extract_token(fileinto, rules[i], 7, '|', sizeof fileinto);
+               extract_token(redirect, rules[i], 8, '|', sizeof redirect);
+               extract_token(automsg, rules[i], 9, '|', sizeof automsg);
+               extract_token(final, rules[i], 10, '|', sizeof final);
                
-               wprintf("<tr id=\"rule%d\">", i);
+               /* now generate the table row */
+
+               wprintf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
+                       i,
+                       ((i%2) ? "DDDDDD" : "FFFFFF")
+               );
 
-               wprintf("<td>");
+               wprintf("<td width=5%% align=\"center\">");
 
                wprintf("<div style=\"display:none\">");
-               wprintf("<input type=\"checkbox\" id=\"active%d\">", i);
+               wprintf("<input type=\"checkbox\" name=\"active%d\" id=\"active%d\" %s>",
+                       i, i,
+                       (active ? "checked" : "")
+               );
                wprintf("</div>");
 
                if (i>0) wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
-                       "<img border=\"0\" src=\"static/up_pointer.gif\" /></a>", i-1, i);
+                       "<img border=\"0\" src=\"static/up_pointer.gif\" "
+                       "title=\"%s\"/></a>",
+                       i-1, i, _("Move rule up") );
 
                wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
-                       "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" /></a>",
-                       i, i+1, i);
+                       "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" "
+                       "title=\"%s\"/></a>",
+                       i, i+1, i, _("Move rule down") );
 
                wprintf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
-                       "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" /></a>",
-                       i, i);
+                       "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" "
+                       "title=\"%s\"/></a>",
+                       i, i, _("Delete rule") );
+
+               wprintf("</td>");
 
-               wprintf("&nbsp;%d.&nbsp;%s</td>", i+1, _("If") );
+               wprintf("<td width=5%% align=\"center\">");
+               wprintf("<font size=+2>%d</font>", i+1);
+               wprintf("</td>");
 
-               wprintf("<td>");
+               wprintf("<td width=20%%>%s ", _("If") );
+
+               char *hfield_values[14][2] = {
+                       {       "from",         _("From")               },
+                       {       "tocc",         _("To or Cc")           },
+                       {       "subject",      _("Subject")            },
+                       {       "replyto",      _("Reply-to")           },
+                       {       "sender",       _("Sender")             },
+                       {       "resentfrom",   _("Resent-From")        },
+                       {       "resentto",     _("Resent-To")          },
+                       {       "envfrom",      _("Envelope From")      },
+                       {       "envto",        _("Envelope To")        },
+                       {       "xmailer",      _("X-Mailer")           },
+                       {       "xspamflag",    _("X-Spam-Flag")        },
+                       {       "xspamstatus",  _("X-Spam-Status")      },
+                       {       "size",         _("Message size")       },
+                       {       "all",          _("All")                }
+               };
 
                wprintf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
                        i, i);
-               wprintf("<option value=\"from\">%s</option>", _("From"));
-               wprintf("<option value=\"tocc\">%s</option>", _("To or Cc"));
-               wprintf("<option value=\"subject\">%s</option>", _("Subject"));
-               wprintf("<option value=\"replyto\">%s</option>", _("Reply-to"));
-               wprintf("<option value=\"sender\">%s</option>", _("Sender"));
-               wprintf("<option value=\"resentfrom\">%s</option>", _("Resent-From"));
-               wprintf("<option value=\"resentto\">%s</option>", _("Resent-To"));
-               wprintf("<option value=\"envfrom\">%s</option>", _("Envelope From"));
-               wprintf("<option value=\"envto\">%s</option>", _("Envelope To"));
-               wprintf("<option value=\"xmailer\">%s</option>", _("X-Mailer"));
-               wprintf("<option value=\"xspamflag\">%s</option>", _("X-Spam-Flag"));
-               wprintf("<option value=\"xspamstatus\">%s</option>", _("X-Spam-Status"));
-               wprintf("<option value=\"size\">%s</option>", _("Message size"));
-               wprintf("<option value=\"all\">%s</option>", _("(All messages)"));
+               for (j=0; j<14; ++j) {
+                       wprintf("<option %s value=\"%s\">%s</option>",
+                               ( (!strcasecmp(hfield, hfield_values[j][0])) ? "selected" : ""),
+                               hfield_values[j][0],
+                               hfield_values[j][1]
+                       );
+               }
+
                wprintf("</select>");
                wprintf("</td>");
 
-               wprintf("<td>");
+               wprintf("<td width=20%%>");
+
+               char *compare_values[4][2] = {
+                       {       "contains",     _("contains")           },
+                       {       "notcontains",  _("does not contain")   },
+                       {       "is",           _("is")                 },
+                       {       "isnot",        _("is not")             }
+               };
 
                wprintf("<div id=\"div_compare%d\">", i);
                wprintf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
                        i, i);
-               wprintf("<option value=\"contains\">%s</option>", _("contains"));
-               wprintf("<option value=\"notcontains\">%s</option>", _("does not contain"));
-               wprintf("<option value=\"is\">%s</option>", _("is"));
-               wprintf("<option value=\"isnot\">%s</option>", _("is not"));
+               for (j=0; j<4; ++j) {
+                       wprintf("<option %s value=\"%s\">%s</option>",
+                               ( (!strcasecmp(compare, compare_values[j][0])) ? "selected" : ""),
+                               compare_values[j][0],
+                               compare_values[j][1]
+                       );
+               }
                wprintf("</select>");
 
-               wprintf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\">", i, i);
+               wprintf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\" value=\"", i, i);
+               escputs(htext);
+               wprintf("\"></div>");
+
+               wprintf("<div id=\"div_nocompare%d\">", i);
+               wprintf("%s", _("(All messages)"));
                wprintf("</div>");
 
+               char *sizecomp_values[2][2] = {
+                       {       "larger",       _("is larger than")     },
+                       {       "smaller",      _("is smaller than")    }
+               };
+
                wprintf("<div id=\"div_size%d\">", i);
                wprintf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
                        i, i);
-               wprintf("<option value=\"larger\">%s</option>", _("is larger than"));
-               wprintf("<option value=\"smaller\">%s</option>", _("is smaller than"));
+               for (j=0; j<2; ++j) {
+                       wprintf("<option %s value=\"%s\">%s</option>",
+                               ( (!strcasecmp(sizecomp, sizecomp_values[j][0])) ? "selected" : ""),
+                               sizecomp_values[j][0],
+                               sizecomp_values[j][1]
+                       );
+               }
                wprintf("</select>");
 
-               wprintf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\">", i, i);
+               wprintf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\" value=\"%d\">",
+                       i, i, sizeval);
                wprintf("bytes");
                wprintf("</div>");
 
                wprintf("</td>");
 
-               wprintf("<td>");
+               wprintf("<td width=20%%>");
                wprintf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
                        i, i);
                wprintf("<option value=\"keep\">%s</option>", _("Keep"));
@@ -619,6 +832,7 @@ void display_rules_editor_inner_div(void) {
 
                wprintf("<div id=\"div_automsg%d\">", i);
                wprintf(_("Message:"));
+               wprintf("<br />");
                wprintf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
                wprintf("</textarea>");
                wprintf("</div>");
@@ -627,13 +841,13 @@ void display_rules_editor_inner_div(void) {
 
 
 
-               wprintf("<td>%s</td>", _("and then") );
+               wprintf("<td width=10%% align=\"center\">%s</td>", _("and then") );
 
-               wprintf("<td>");
+               wprintf("<td width=20%%>");
                wprintf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
                        i, i);
-               wprintf("<option value=\"stop\">%s</option>", _("stop"));
                wprintf("<option value=\"continue\">%s</option>", _("continue processing"));
+               wprintf("<option value=\"stop\">%s</option>", _("stop"));
                wprintf("</select>");
                wprintf("</td>");
 
@@ -644,10 +858,9 @@ void display_rules_editor_inner_div(void) {
        wprintf("</table>");
        wprintf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">Add rule</a><br /></div>\n");
 
-       wprintf("<script type=\"text/javascript\">                                      \n"
-               "UpdateRules();                                                         \n"
-               "</script>                                                              \n"
-       );
+       wprintf("<script type=\"text/javascript\">                                      \n");
+       wprintf("UpdateRules();                                                         \n");
+       wprintf("</script>                                                              \n");
 
        free(rooms);
 }