#define MAX_SCRIPTS 100
#define MAX_RULES 25
+#define RULES_SCRIPT "__WebCit_Generated_Script__"
/**
* \brief view/edit sieve config
int active_script = (-1);
char buf[256];
int i;
+ int rules_script_is_active = 0;
memset(script_names, 0, sizeof script_names);
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;
}
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");
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");
}
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");
}
}
}
+
+/**
+ * \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
*/
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")) {
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);
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");
+ }
}
}
}
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");
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];
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");
" } \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"
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);
+ /* now generate the table row */
+
wprintf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
i,
((i%2) ? "DDDDDD" : "FFFFFF")
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();\">"
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"));
+ 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 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("</div>");
+ 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 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>");
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);
}