2 * Copyright (c) 1996-2011 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define MAX_SCRIPTS 100
23 #define RULES_SCRIPT "__WebCit_Generated_Script__"
27 * dummy panel indicating to the user that the server doesn't support Sieve
29 void display_no_sieve(void) {
31 output_headers(1, 1, 2, 0, 0, 0);
33 wc_printf("<div id=\"banner\">\n");
34 wc_printf("<img src=\"static/icons/essen/32x32/config.png\">");
36 wc_printf(_("View/edit server-side mail filters"));
38 wc_printf("</div>\n");
40 wc_printf("<div id=\"content\" class=\"service\">\n");
42 wc_printf("<table class=\"sieve_background\">"
43 "<tr><td valign=top>\n");
45 wc_printf(_("This installation of Citadel was built without support for server-side mail filtering."
46 "<br>Please contact your system administrator if you require this feature.<br>"));
48 wc_printf("</td></tr></table>\n");
54 * view/edit sieve config
56 void display_sieve(void)
58 char script_names[MAX_SCRIPTS][64];
60 int active_script = (-1);
61 char buf[SIZ]; /* Don't make this buffer smaller or it will restrict line length */
63 int rules_script_is_active = 0;
65 if (!WC->serv_info->serv_supports_sieve) {
70 memset(script_names, 0, sizeof script_names);
72 serv_puts("MSIV listscripts");
73 serv_getln(buf, sizeof(buf));
74 if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
75 if (num_scripts < MAX_SCRIPTS) {
76 extract_token(script_names[num_scripts], buf, 0, '|', 64);
77 if (extract_int(buf, 1) > 0) {
78 active_script = num_scripts;
79 if (!strcasecmp(script_names[num_scripts], RULES_SCRIPT)) {
80 rules_script_is_active = 1;
87 output_headers(1, 1, 2, 0, 0, 0);
89 wc_printf("<script type=\"text/javascript\"> \n"
91 "var previously_active_script; \n"
93 "function ToggleSievePanels() { \n"
94 " d = ($('sieveform').bigaction.options[$('sieveform').bigaction.selectedIndex].value); \n"
95 " for (i=0; i<3; ++i) { \n"
97 " $('sievediv' + i).style.display = 'block'; \n"
100 " $('sievediv' + i).style.display = 'none'; \n"
105 "function ToggleScriptPanels() { \n"
106 " d = ($('sieveform').active_script.options[$('sieveform').active_script.selectedIndex].value); \n"
107 " if ($('script_' + previously_active_script)) { \n"
108 " $('script_' + previously_active_script).style.display = 'none'; \n"
110 " $('script_' + d).style.display = 'block'; \n"
111 " previously_active_script = d; \n"
117 wc_printf("<div id=\"banner\">\n");
118 wc_printf("<img src=\"static/icons/essen/32x32/config.png\">");
120 wc_printf(_("View/edit server-side mail filters"));
121 wc_printf("</h1>\n");
122 wc_printf("</div>\n");
124 wc_printf("<div id=\"content\" class=\"service\">\n");
126 wc_printf("<table class=\"sieve_background\">"
127 "<tr><td valign=top>\n");
130 wc_printf("<form id=\"sieveform\" method=\"post\" action=\"save_sieve\">\n");
131 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
133 wc_printf(_("When new mail arrives: "));
134 wc_printf("<select name=\"bigaction\" size=1 onChange=\"ToggleSievePanels();\">\n");
136 wc_printf("<option %s value=\"0\">", ((active_script < 0) ? "selected" : ""));
137 wc_printf(_("Leave it in my inbox without filtering"));
138 wc_printf("</option>\n");
140 wc_printf("<option %s value=\"1\">", ((rules_script_is_active) ? "selected" : ""));
141 wc_printf(_("Filter it according to rules selected below"));
142 wc_printf("</option>\n");
144 wc_printf("<option %s value=\"2\">",
145 (((active_script >= 0) && (!rules_script_is_active)) ? "selected" : ""));
146 wc_printf(_("Filter it through a manually edited script (advanced users only)"));
147 wc_printf("</option>\n");
149 wc_printf("</select>");
153 /* The "no filtering" div */
155 wc_printf("<div id=\"sievediv0\" style=\"display:none\">\n");
156 wc_printf("<div align=\"center\"><br><br>");
157 wc_printf(_("Your incoming mail will not be filtered through any scripts."));
158 wc_printf("<br><br></div>\n");
159 wc_printf("</div>\n");
161 /* The "webcit managed scripts" div */
163 wc_printf("<div id=\"sievediv1\" style=\"display:none\">\n");
164 display_rules_editor_inner_div();
165 wc_printf("</div>\n");
167 /* The "I'm smart and can write my own Sieve scripts" div */
169 wc_printf("<div id=\"sievediv2\" style=\"display:none\">\n");
171 if (num_scripts > 0) {
172 wc_printf(_("The currently active script is: "));
173 wc_printf("<select name=\"active_script\" size=1 onChange=\"ToggleScriptPanels();\">\n");
174 for (i=0; i<num_scripts; ++i) {
175 if (strcasecmp(script_names[i], RULES_SCRIPT)) {
176 wc_printf("<option %s value=\"%s\">%s</option>\n",
177 ((active_script == i) ? "selected" : ""),
183 wc_printf("</select>\n");
186 wc_printf(" ");
187 wc_printf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add or delete scripts"));
191 if (num_scripts > 0) {
192 for (i=0; i<num_scripts; ++i) {
193 if (strcasecmp(script_names[i], RULES_SCRIPT)) {
194 wc_printf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
195 wc_printf("<textarea name=\"text_%s\" wrap=soft rows=20 cols=80 width=80>\n",
197 serv_printf("MSIV getscript|%s", script_names[i]);
198 serv_getln(buf, sizeof buf);
199 if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
200 wc_printf("%s\n", buf);
202 wc_printf("</textarea>\n");
203 wc_printf("</div>\n");
208 wc_printf("<script type=\"text/javascript\"> \n"
209 "ToggleScriptPanels(); \n"
213 wc_printf("</div>\n");
216 /* The rest of this is common for all panels... */
218 wc_printf("<div align=\"center\"><br>");
219 wc_printf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
221 wc_printf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
222 wc_printf("</div></form>\n");
224 wc_printf("</td></tr></table>\n");
226 wc_printf("<script type=\"text/javascript\"> \n"
227 "ToggleSievePanels(); \n"
238 * Helper function for output_sieve_rule() to output strings with quotes escaped
240 void osr_sanitize(char *str) {
243 if (str == NULL) return;
245 for (i=0; i<len; ++i) {
249 else if (isspace(str[i])) {
257 * Output parseable Sieve script code based on rules input
259 void output_sieve_rule(char *hfield, char *compare, char *htext, char *sizecomp, int sizeval,
260 char *action, char *fileinto, char *redirect, char *automsg, char *final,
267 osr_sanitize(fileinto);
268 osr_sanitize(redirect);
269 osr_sanitize(automsg);
271 /* Prepare negation and match operators that will be used iff we apply a conditional */
273 if (!strcasecmp(compare, "contains")) {
277 else if (!strcasecmp(compare, "notcontains")) {
281 else if (!strcasecmp(compare, "is")) {
285 else if (!strcasecmp(compare, "isnot")) {
289 else if (!strcasecmp(compare, "matches")) {
293 else if (!strcasecmp(compare, "notmatches")) {
298 /* Now do the conditional */
300 if (!strcasecmp(hfield, "from")) {
301 serv_printf("if%s header %s \"From\" \"%s\"",
307 else if (!strcasecmp(hfield, "tocc")) {
308 serv_printf("if%s header %s [\"To\", \"Cc\"] \"%s\"",
314 else if (!strcasecmp(hfield, "subject")) {
315 serv_printf("if%s header %s \"Subject\" \"%s\"",
321 else if (!strcasecmp(hfield, "replyto")) {
322 serv_printf("if%s header %s \"Reply-to\" \"%s\"",
328 else if (!strcasecmp(hfield, "sender")) {
329 serv_printf("if%s header %s \"Sender\" \"%s\"",
335 else if (!strcasecmp(hfield, "resentfrom")) {
336 serv_printf("if%s header %s \"Resent-from\" \"%s\"",
342 else if (!strcasecmp(hfield, "resentto")) {
343 serv_printf("if%s header %s \"Resent-to\" \"%s\"",
349 else if (!strcasecmp(hfield, "xmailer")) {
350 serv_printf("if%s header %s \"X-Mailer\" \"%s\"",
356 else if (!strcasecmp(hfield, "xspamflag")) {
357 serv_printf("if%s header %s \"X-Spam-Flag\" \"%s\"",
363 else if (!strcasecmp(hfield, "xspamstatus")) {
364 serv_printf("if%s header %s \"X-Spam-Status\" \"%s\"",
370 else if (!strcasecmp(hfield, "listid")) {
371 serv_printf("if%s header %s \"List-ID\" \"%s\"",
377 else if (!strcasecmp(hfield, "envfrom")) {
378 serv_printf("if%s envelope %s \"From\" \"%s\"",
384 else if (!strcasecmp(hfield, "envto")) {
385 serv_printf("if%s envelope %s \"To\" \"%s\"",
391 else if (!strcasecmp(hfield, "size")) {
392 if (!strcasecmp(sizecomp, "larger")) {
393 serv_printf("if size :over %d", sizeval);
395 else if (!strcasecmp(sizecomp, "smaller")) {
396 serv_printf("if size :under %d", sizeval);
398 else { /* failsafe - should never get here, but just in case... */
399 serv_printf("if size :over 1");
403 /* Open braces if we're in a conditional loop */
405 if (strcasecmp(hfield, "all")) {
412 if (!strcasecmp(action, "keep")) {
413 serv_printf("keep;");
416 else if (!strcasecmp(action, "discard")) {
417 serv_printf("discard;");
420 else if (!strcasecmp(action, "reject")) {
421 serv_printf("reject \"%s\";", automsg);
424 else if (!strcasecmp(action, "fileinto")) {
425 serv_printf("fileinto \"%s\";", fileinto);
428 else if (!strcasecmp(action, "redirect")) {
429 serv_printf("redirect \"%s\";", redirect);
432 else if (!strcasecmp(action, "vacation")) {
433 serv_printf("vacation :addresses [%s]\n\"%s\";", my_addresses, automsg);
437 /* Do 'final' action */
439 if (!strcasecmp(final, "stop")) {
440 serv_printf("stop;");
444 /* Close the braces if we're in a conditional loop */
446 if (strcasecmp(hfield, "all")) {
457 * Translate the fields from the rule editor into something we can save...
459 void parse_fields_from_rule_editor(void) {
476 char encoded_rule[4096];
477 char my_addresses[4096];
479 /* Enumerate my email addresses in case they are needed for a vacation rule */
482 serv_getln(buf, sizeof buf);
483 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
484 if (!IsEmptyStr(my_addresses)) {
485 strcat(my_addresses, ",\n");
487 strcat(my_addresses, "\"");
488 strcat(my_addresses, buf);
489 strcat(my_addresses, "\"");
492 /* Now generate the script and write it to the Citadel server */
493 serv_printf("MSIV putscript|%s|", RULES_SCRIPT);
494 serv_getln(buf, sizeof buf);
499 serv_puts("# THIS SCRIPT WAS AUTOMATICALLY GENERATED BY WEBCIT.");
501 serv_puts("# Do not attempt to manually edit it. If you do so,");
502 serv_puts("# your changes will be overwritten the next time WebCit");
503 serv_puts("# saves its mail filtering rule set. If you really want");
504 serv_puts("# to use these rules as the basis for another script,");
505 serv_puts("# copy them to another script and save that instead.");
507 serv_puts("require \"fileinto\";");
508 serv_puts("require \"reject\";");
509 serv_puts("require \"vacation\";");
510 serv_puts("require \"envelope\";");
513 for (i=0; i<MAX_RULES; ++i) {
517 sprintf(fname, "active%d", i);
518 active = !strcasecmp(BSTR(fname), "on") ;
522 sprintf(fname, "hfield%d", i);
523 safestrncpy(hfield, BSTR(fname), sizeof hfield);
525 sprintf(fname, "compare%d", i);
526 safestrncpy(compare, BSTR(fname), sizeof compare);
528 sprintf(fname, "htext%d", i);
529 safestrncpy(htext, BSTR(fname), sizeof htext);
531 sprintf(fname, "sizecomp%d", i);
532 safestrncpy(sizecomp, BSTR(fname), sizeof sizecomp);
534 sprintf(fname, "sizeval%d", i);
535 sizeval = IBSTR(fname);
537 sprintf(fname, "action%d", i);
538 safestrncpy(action, BSTR(fname), sizeof action);
540 sprintf(fname, "fileinto%d", i);
541 safestrncpy(fileinto, BSTR(fname), sizeof fileinto);
543 sprintf(fname, "redirect%d", i);
544 safestrncpy(redirect, BSTR(fname), sizeof redirect);
546 sprintf(fname, "automsg%d", i);
547 safestrncpy(automsg, BSTR(fname), sizeof automsg);
549 sprintf(fname, "final%d", i);
550 safestrncpy(final, BSTR(fname), sizeof final);
552 snprintf(rule, sizeof rule, "%d|%s|%s|%s|%s|%d|%s|%s|%s|%s|%s",
553 active, hfield, compare, htext, sizecomp, sizeval, action, fileinto,
554 redirect, automsg, final
557 CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1, 0);
558 serv_printf("# WEBCIT_RULE|%d|%s|", i, encoded_rule);
559 output_sieve_rule(hfield, compare, htext, sizecomp, sizeval,
560 action, fileinto, redirect, automsg, final, my_addresses);
576 void save_sieve(void) {
578 char script_names[MAX_SCRIPTS][64];
580 int active_script = (-1);
585 if (!havebstr("save_button")) {
586 strcpy(WC->ImportantMessage,
587 _("Cancelled. Changes were not saved."));
592 parse_fields_from_rule_editor();
594 serv_puts("MSIV listscripts");
595 serv_getln(buf, sizeof(buf));
596 if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
597 if (num_scripts < MAX_SCRIPTS) {
598 extract_token(script_names[num_scripts], buf, 0, '|', 64);
599 if (extract_int(buf, 1) > 0) {
600 active_script = num_scripts;
606 bigaction = ibstr("bigaction");
608 if (bigaction == 0) {
609 serv_puts("MSIV setactive||");
610 serv_getln(buf, sizeof buf);
613 else if (bigaction == 1) {
614 serv_printf("MSIV setactive|%s|", RULES_SCRIPT);
615 serv_getln(buf, sizeof buf);
618 else if (bigaction == 2) {
619 serv_printf("MSIV setactive|%s|", bstr("active_script"));
620 serv_getln(buf, sizeof buf);
623 if (num_scripts > 0) {
624 for (i=0; i<num_scripts; ++i) {
626 * We only want to save the scripts from the "manually edited scripts"
627 * screen. The script that WebCit generates from its ruleset will be
628 * auto-generated by parse_fields_from_rule_editor() and saved there.
630 if (strcasecmp(script_names[i], RULES_SCRIPT)) {
631 serv_printf("MSIV putscript|%s|", script_names[i]);
632 serv_getln(buf, sizeof buf);
634 snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
635 striplt((char *)BSTR(this_name)); /* TODO: get rid of typecast*/
636 serv_write(BSTR(this_name), strlen(BSTR(this_name)));
643 strcpy(WC->ImportantMessage, _("Your changes have been saved."));
650 * show a list of available scripts to add/remove them
652 void display_add_remove_scripts(char *message)
655 char script_name[256];
657 output_headers(1, 1, 2, 0, 0, 0);
658 wc_printf("<div id=\"banner\">\n");
659 wc_printf("<img src=\"static/icons/essen/32x32/config.png\">");
660 wc_printf(_("Add or delete scripts"));
661 wc_printf("</h1>\n");
662 wc_printf("</div>\n");
664 wc_printf("<div id=\"content\" class=\"service\">\n");
666 if (message != NULL) {
667 wc_printf("%s", message);
670 wc_printf("<table border=0 cellspacing=10><tr valign=top><td>\n");
672 do_template("beginbox_1");
673 StrBufAppendBufPlain(WC->WBuf, _("Add a new script"), -1, 0);
674 do_template("beginbox_2");
676 wc_printf(_("To create a new script, enter the desired "
677 "script name in the box below and click 'Create'."));
678 wc_printf("<br><br>");
680 wc_printf("<center><form method=\"POST\" action=\"create_script\">\n");
681 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
682 wc_printf(_("Script name: "));
683 wc_printf("<input type=\"text\" name=\"script_name\"><br>\n"
684 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
685 "</form></center>\n", _("Create"));
687 do_template("endbox");
689 do_template("beginbox_1");
690 StrBufAppendBufPlain(WC->WBuf, _("Edit scripts"), -1, 0);
691 do_template("beginbox_2");
692 wc_printf("<br><div align=center><a href=\"display_sieve\">%s</a><br><br>\n",
693 _("Return to the script editing screen")
695 do_template("endbox");
697 wc_printf("</td><td>");
699 do_template("beginbox_1");
700 StrBufAppendBufPlain(WC->WBuf, _("Delete scripts"), -1, 0);
701 do_template("beginbox_2");
703 wc_printf(_("To delete an existing script, select the script "
704 "name from the list and click 'Delete'."));
705 wc_printf("<br><br>");
708 "<form method=\"POST\" action=\"delete_script\">\n");
709 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
710 wc_printf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
712 serv_puts("MSIV listscripts");
713 serv_getln(buf, sizeof buf);
715 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
716 extract_token(script_name, buf, 0, '|', sizeof script_name);
717 if ( (extract_int(buf, 1) == 0) && (strcasecmp(script_name, RULES_SCRIPT)) ) {
718 wc_printf("<option>");
719 escputs(script_name);
720 wc_printf("</option>\n");
724 wc_printf("</select><br>\n");
726 wc_printf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
727 "onClick=\"return confirm('%s');\">", _("Delete script"), _("Delete this script?"));
728 wc_printf("</form></center>\n");
729 do_template("endbox");
731 wc_printf("</td></tr></table>\n");
741 void delete_script(void) {
744 serv_printf("MSIV deletescript|%s", bstr("script_name"));
745 serv_getln(buf, sizeof buf);
746 display_add_remove_scripts(&buf[4]);
752 * create a new script
753 * take the web environment script name and create it on the citadel server
755 void create_script(void) {
758 serv_printf("MSIV getscript|%s", bstr("script_name"));
759 serv_getln(buf, sizeof buf);
761 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
764 display_add_remove_scripts(_("A script by that name already exists."));
768 serv_printf("MSIV putscript|%s", bstr("script_name"));
769 serv_getln(buf, sizeof buf);
773 display_add_remove_scripts(_("A new script has been created. Return to the script editing screen to edit and activate it."));
777 display_add_remove_scripts(&buf[4]);
783 void display_rules_editor_inner_div(void) {
786 char rules[MAX_RULES][2048];
791 int num_roomnames = 0;
792 int num_roomnames_alloc = 0;
807 memset(rules, 0, sizeof rules);
808 serv_printf("MSIV getscript|%s", RULES_SCRIPT);
809 serv_getln(buf, sizeof buf);
810 if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
811 if (!strncasecmp(buf, "# WEBCIT_RULE|", 14)) {
812 j = extract_int(buf, 1);
813 remove_token(buf, 0, '|');
814 remove_token(buf, 0, '|');
815 CtdlDecodeBase64(rules[j], buf, strlen(buf));
819 /* load the roomnames */
821 serv_getln(buf, sizeof buf);
823 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
825 if (num_roomnames > num_roomnames_alloc) {
826 num_roomnames_alloc += 250;
827 rooms = realloc(rooms, (num_roomnames_alloc * 128));
829 extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
835 * This script should get called by every onChange event...
838 wc_printf("<script type=\"text/javascript\"> \n"
840 "var highest_active_rule = (-1); \n"
842 "function UpdateRules() { \n");
844 * Show only the active rows...
846 wc_printf(" highest_active_rule = (-1); \n");
847 wc_printf(" for (i=0; i<%d; ++i) { \n", MAX_RULES);
848 wc_printf(" if ($('active'+i).checked) { \n"
849 " $('rule' + i).style.display = 'block'; \n"
850 " highest_active_rule = i; \n"
853 " $('rule' + i).style.display = 'none'; \n"
857 * Show only the fields relevant to the rules...
859 wc_printf(" for (i=0; i<=highest_active_rule; ++i) { \n"
860 " d = ($('movedown'+i)); \n"
861 " if (i < highest_active_rule) { \n"
862 " d.style.display = 'block'; \n"
865 " d.style.display = 'none'; \n"
867 " d = ($('hfield'+i).options[$('hfield'+i).selectedIndex].value); \n"
868 " if (d == 'all') { \n"
869 " $('div_size'+i).style.display = 'none'; \n"
870 " $('div_compare'+i).style.display = 'none'; \n"
871 " $('div_nocompare'+i).style.display = 'block'; \n"
873 " else if (d == 'size') { \n"
874 " $('div_size'+i).style.display = 'block'; \n"
875 " $('div_compare'+i).style.display = 'none'; \n"
876 " $('div_nocompare'+i).style.display = 'none'; \n"
879 " $('div_size'+i).style.display = 'none'; \n"
880 " $('div_compare'+i).style.display = 'block'; \n"
881 " $('div_nocompare'+i).style.display = 'none'; \n"
883 " d = ($('action'+i).options[$('action'+i).selectedIndex].value); \n"
884 " if (d == 'fileinto') { \n"
885 " $('div_fileinto'+i).style.display = 'block'; \n"
886 " $('div_redirect'+i).style.display = 'none'; \n"
887 " $('div_automsg'+i).style.display = 'none'; \n"
888 " } else if (d == 'redirect') { \n"
889 " $('div_fileinto'+i).style.display = 'none'; \n"
890 " $('div_redirect'+i).style.display = 'block'; \n"
891 " $('div_automsg'+i).style.display = 'none'; \n"
892 " } else if ((d == 'reject') || (d == 'vacation')) { \n"
893 " $('div_fileinto'+i).style.display = 'none'; \n"
894 " $('div_redirect'+i).style.display = 'none'; \n"
895 " $('div_automsg'+i).style.display = 'block'; \n"
897 " $('div_fileinto'+i).style.display = 'none'; \n"
898 " $('div_redirect'+i).style.display = 'none'; \n"
899 " $('div_automsg'+i).style.display = 'none'; \n"
901 " if (highest_active_rule < %d) { \n", MAX_RULES-1 );
902 wc_printf(" $('div_addrule').style.display = 'block'; \n"
904 " $('div_addrule').style.display = 'none'; \n"
909 * Add a rule (really, just un-hide it)
911 "function AddRule() { \n"
912 " highest_active_rule = highest_active_rule + 1; \n"
913 " $('active'+highest_active_rule).checked = true; \n"
919 "function SwapRules(ra, rb) { \n"
921 " var things = new Array(); \n"
922 " things[0] = 'hfield'; \n"
923 " things[1] = 'compare'; \n"
924 " things[2] = 'htext'; \n"
925 " things[3] = 'action'; \n"
926 " things[4] = 'fileinto'; \n"
927 " things[5] = 'redirect'; \n"
928 " things[6] = 'final'; \n"
929 " things[7] = 'sizecomp'; \n"
930 " things[8] = 'sizeval'; \n"
931 " things[9] = 'automsg'; \n"
933 " for (i=0; i<=9; ++i) { \n"
934 " tempval=$(things[i]+ra).value; \n"
935 " $(things[i]+ra).value = $(things[i]+rb).value; \n"
936 " $(things[i]+rb).value = tempval; \n"
940 * Delete a rule (percolate the deleted rule out to the end, then deactivate it)
942 "function DeleteRule(rd) { \n"
943 " for (j=rd; j<=highest_active_rule; ++j) { \n"
944 " SwapRules(j, (j+1)); \n"
946 " $('active'+highest_active_rule).checked = false; \n"
954 wc_printf("<table cellpadding=2 width=100%%>");
956 for (i=0; i<MAX_RULES; ++i) {
958 /* Grab our existing values to populate */
959 active = extract_int(rules[i], 0);
960 extract_token(hfield, rules[i], 1, '|', sizeof hfield);
961 extract_token(compare, rules[i], 2, '|', sizeof compare);
962 extract_token(htext, rules[i], 3, '|', sizeof htext);
963 extract_token(sizecomp, rules[i], 4, '|', sizeof sizecomp);
964 sizeval = extract_int(rules[i], 5);
965 extract_token(action, rules[i], 6, '|', sizeof action);
966 extract_token(fileinto, rules[i], 7, '|', sizeof fileinto);
967 extract_token(redirect, rules[i], 8, '|', sizeof redirect);
968 extract_token(automsg, rules[i], 9, '|', sizeof automsg);
969 extract_token(final, rules[i], 10, '|', sizeof final);
971 /* now generate the table row */
973 wc_printf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
975 ((i%2) ? "DDDDDD" : "FFFFFF")
978 wc_printf("<td width=5%% align=\"center\">");
980 wc_printf("<div style=\"display:none\">");
981 wc_printf("<input type=\"checkbox\" name=\"active%d\" id=\"active%d\" %s>",
983 (active ? "checked" : "")
987 if (i>0) wc_printf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
988 "<img border=\"0\" src=\"static/icons/up_pointer.gif\" "
989 "title=\"%s\"/></a>",
990 i-1, i, _("Move rule up") );
992 wc_printf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
993 "<img id=\"movedown%d\" border=\"0\" src=\"static/icons/down_pointer.gif\" "
994 "title=\"%s\"/></a>",
995 i, i+1, i, _("Move rule down") );
997 wc_printf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
998 "<img id=\"delete%d\" border=\"0\" src=\"static/icons/delete.gif\" "
999 "title=\"%s\"/></a>",
1000 i, i, _("Delete rule") );
1004 wc_printf("<td width=5%% align=\"center\">");
1005 wc_printf("<font size=+2>%d</font>", i+1);
1008 wc_printf("<td width=20%%>%s ", _("If") );
1010 char *hfield_values[15][2] = {
1011 { "from", _("From") },
1012 { "tocc", _("To or Cc") },
1013 { "subject", _("Subject") },
1014 { "replyto", _("Reply-to") },
1015 { "sender", _("Sender") },
1016 { "resentfrom", _("Resent-From") },
1017 { "resentto", _("Resent-To") },
1018 { "envfrom", _("Envelope From") },
1019 { "envto", _("Envelope To") },
1020 { "xmailer", _("X-Mailer") },
1021 { "xspamflag", _("X-Spam-Flag") },
1022 { "xspamstatus", _("X-Spam-Status") },
1023 { "listid", _("List-ID") },
1024 { "size", _("Message size") },
1028 wc_printf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
1030 for (j=0; j<15; ++j) {
1031 wc_printf("<option %s value=\"%s\">%s</option>",
1032 ( (!strcasecmp(hfield, hfield_values[j][0])) ? "selected" : ""),
1033 hfield_values[j][0],
1038 wc_printf("</select>");
1041 wc_printf("<td width=20%%>");
1043 char *compare_values[6][2] = {
1044 { "contains", _("contains") },
1045 { "notcontains", _("does not contain") },
1047 { "isnot", _("is not") },
1048 { "matches", _("matches") },
1049 { "notmatches", _("does not match") }
1052 wc_printf("<div id=\"div_compare%d\">", i);
1053 wc_printf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
1055 for (j=0; j<6; ++j) {
1056 wc_printf("<option %s value=\"%s\">%s</option>",
1057 ( (!strcasecmp(compare, compare_values[j][0])) ? "selected" : ""),
1058 compare_values[j][0],
1059 compare_values[j][1]
1062 wc_printf("</select>");
1064 wc_printf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\" value=\"", i, i);
1066 wc_printf("\"></div>");
1068 wc_printf("<div id=\"div_nocompare%d\">", i);
1069 wc_printf("%s", _("(All messages)"));
1070 wc_printf("</div>");
1072 char *sizecomp_values[2][2] = {
1073 { "larger", _("is larger than") },
1074 { "smaller", _("is smaller than") }
1077 wc_printf("<div id=\"div_size%d\">", i);
1078 wc_printf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
1080 for (j=0; j<2; ++j) {
1081 wc_printf("<option %s value=\"%s\">%s</option>",
1082 ( (!strcasecmp(sizecomp, sizecomp_values[j][0])) ? "selected" : ""),
1083 sizecomp_values[j][0],
1084 sizecomp_values[j][1]
1087 wc_printf("</select>");
1089 wc_printf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\" value=\"%d\">",
1092 wc_printf("</div>");
1096 char *action_values[6][2] = {
1097 { "keep", _("Keep") },
1098 { "discard", _("Discard silently") },
1099 { "reject", _("Reject") },
1100 { "fileinto", _("Move message to") },
1101 { "redirect", _("Forward to") },
1102 { "vacation", _("Vacation") }
1105 wc_printf("<td width=20%%>");
1106 wc_printf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
1108 for (j=0; j<6; ++j) {
1109 wc_printf("<option %s value=\"%s\">%s</option>",
1110 ( (!strcasecmp(action, action_values[j][0])) ? "selected" : ""),
1111 action_values[j][0],
1115 wc_printf("</select>");
1117 wc_printf("<div id=\"div_fileinto%d\">", i);
1118 wc_printf("<select name=\"fileinto%d\" id=\"fileinto%d\">", i, i);
1119 for (j=0; j<num_roomnames; ++j) {
1120 wc_printf("<option ");
1121 if (!strcasecmp(rooms[j].name, fileinto)) {
1122 wc_printf("selected ");
1124 wc_printf("value=\"");
1125 escputs(rooms[j].name);
1127 escputs(rooms[j].name);
1128 wc_printf("</option>\n");
1130 wc_printf("</select>\n");
1131 wc_printf("</div>");
1133 wc_printf("<div id=\"div_redirect%d\">", i);
1134 wc_printf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\" value=\"", i, i);
1136 wc_printf("\"></div>");
1138 wc_printf("<div id=\"div_automsg%d\">", i);
1139 wc_printf(_("Message:"));
1141 wc_printf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
1143 wc_printf("</textarea>");
1144 wc_printf("</div>");
1148 char *final_values[2][2] = {
1149 { "continue", _("continue processing") },
1150 { "stop", _("stop") }
1153 wc_printf("<td width=10%% align=\"center\">%s</td>", _("and then") );
1155 wc_printf("<td width=20%%>");
1156 wc_printf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
1158 for (j=0; j<2; ++j) {
1159 wc_printf("<option %s value=\"%s\">%s</option>",
1160 ( (!strcasecmp(final, final_values[j][0])) ? "selected" : ""),
1165 wc_printf("</select>");
1168 wc_printf("</tr>\n");
1172 wc_printf("</table>");
1173 wc_printf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">%s</a><br></div>\n",
1177 wc_printf("<script type=\"text/javascript\"> \n");
1178 wc_printf("UpdateRules(); \n");
1179 wc_printf("</script> \n");
1184 void _display_add_remove_scripts(void) {display_add_remove_scripts(NULL);}
1190 WebcitAddUrlHandler(HKEY("display_sieve"), "", 0, display_sieve, 0);
1191 WebcitAddUrlHandler(HKEY("save_sieve"), "", 0, save_sieve, 0);
1192 WebcitAddUrlHandler(HKEY("display_add_remove_scripts"), "", 0, _display_add_remove_scripts, 0);
1193 WebcitAddUrlHandler(HKEY("create_script"), "", 0, create_script, 0);
1194 WebcitAddUrlHandler(HKEY("delete_script"), "", 0, delete_script, 0);