635b2fc5314daf01c737b76b2eff0a67169d53d5
[citadel.git] / webcit / sieve.c
1 /*
2  * Copyright (c) 1996-2011 by the citadel.org team
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "webcit.h"
20
21 #define MAX_SCRIPTS     100
22 #define MAX_RULES       50
23 #define RULES_SCRIPT    "__WebCit_Generated_Script__"
24
25
26 /*
27  * dummy panel indicating to the user that the server doesn't support Sieve
28  */
29 void display_no_sieve(void) {
30
31         output_headers(1, 1, 2, 0, 0, 0);
32
33         wc_printf("<div id=\"banner\">\n");
34         wc_printf("<img src=\"static/icons/essen/32x32/config.png\">");
35         wc_printf("<h1>");
36         wc_printf(_("View/edit server-side mail filters"));
37         wc_printf("</h1>\n");
38         wc_printf("</div>\n");
39
40         wc_printf("<div id=\"content\" class=\"service\">\n");
41
42         wc_printf("<table class=\"sieve_background\">"
43                 "<tr><td valign=top>\n");
44
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>"));
47
48         wc_printf("</td></tr></table>\n");
49         wDumpContent(1);
50 }
51
52
53 /*
54  * view/edit sieve config
55  */
56 void display_sieve(void)
57 {
58         char script_names[MAX_SCRIPTS][64];
59         int num_scripts = 0;
60         int active_script = (-1);
61         char buf[SIZ];          /* Don't make this buffer smaller or it will restrict line length */
62         int i;
63         int rules_script_is_active = 0;
64
65         if (!WC->serv_info->serv_supports_sieve) {
66                 display_no_sieve();
67                 return;
68         }
69
70         memset(script_names, 0, sizeof script_names);
71
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;
81                                 }
82                         }
83                         ++num_scripts;
84                 }
85         }
86
87         output_headers(1, 1, 2, 0, 0, 0);
88
89         wc_printf("<script type=\"text/javascript\">                                    \n"
90                 "                                                                       \n"
91                 "var previously_active_script;                                          \n"
92                 "                                                                       \n"
93                 "function ToggleSievePanels() {                                         \n"
94                 " d = ($('sieveform').bigaction.options[$('sieveform').bigaction.selectedIndex].value); \n"
95                 " for (i=0; i<3; ++i) {                                                 \n"
96                 "  if (i == d) {                                                        \n"
97                 "   $('sievediv' + i).style.display = 'block';                          \n"
98                 "  }                                                                    \n"
99                 "  else {                                                               \n"
100                 "   $('sievediv' + i).style.display = 'none';                           \n"
101                 "  }                                                                    \n"
102                 " }                                                                     \n"
103                 "}                                                                      \n"
104                 "                                                                       \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"
109                 " }                                                                     \n"
110                 " $('script_' + d).style.display = 'block';                             \n"
111                 " previously_active_script = d;                                         \n"
112                 "}                                                                      \n"
113                 "                                                                       \n"
114                 "</script>                                                              \n"
115         );
116
117         wc_printf("<div id=\"banner\">\n");
118         wc_printf("<img src=\"static/icons/essen/32x32/config.png\">");
119         wc_printf("<h1>");
120         wc_printf(_("View/edit server-side mail filters"));
121         wc_printf("</h1>\n");
122         wc_printf("</div>\n");
123
124         wc_printf("<div id=\"content\" class=\"service\">\n");
125
126         wc_printf("<table class=\"sieve_background\">"
127                 "<tr><td valign=top>\n");
128
129
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);
132
133         wc_printf(_("When new mail arrives: "));
134         wc_printf("<select name=\"bigaction\" size=1 onChange=\"ToggleSievePanels();\">\n");
135
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");
139
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");
143
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");
148
149         wc_printf("</select>");
150
151
152
153         /* The "no filtering" div */
154
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");
160
161         /* The "webcit managed scripts" div */
162
163         wc_printf("<div id=\"sievediv1\" style=\"display:none\">\n");
164         display_rules_editor_inner_div();
165         wc_printf("</div>\n");
166
167         /* The "I'm smart and can write my own Sieve scripts" div */
168
169         wc_printf("<div id=\"sievediv2\" style=\"display:none\">\n");
170
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" : ""),
178                                         script_names[i],
179                                         script_names[i]
180                                 );
181                         }
182                 }
183                 wc_printf("</select>\n");
184         }
185
186         wc_printf("&nbsp;&nbsp;&nbsp;");
187         wc_printf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add or delete scripts"));
188
189         wc_printf("<br>\n");
190
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",
196                                         script_names[i]);
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);
201                                 }
202                                 wc_printf("</textarea>\n");
203                                 wc_printf("</div>\n");
204                         }
205                 }
206         }
207
208         wc_printf("<script type=\"text/javascript\">    \n"
209                 "ToggleScriptPanels();                  \n"
210                 "</script>                              \n"
211         );
212
213         wc_printf("</div>\n");
214
215
216         /* The rest of this is common for all panels... */
217
218         wc_printf("<div align=\"center\"><br>");
219         wc_printf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
220         wc_printf("&nbsp;");
221         wc_printf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
222         wc_printf("</div></form>\n");
223
224         wc_printf("</td></tr></table>\n");
225
226         wc_printf("<script type=\"text/javascript\">    \n"
227                 "ToggleSievePanels();                   \n"
228                 "</script>                              \n"
229         );
230
231         wDumpContent(1);
232
233 }
234
235
236
237 /*
238  * Helper function for output_sieve_rule() to output strings with quotes escaped
239  */
240 void osr_sanitize(char *str) {
241         int i, len;
242
243         if (str == NULL) return;
244         len = strlen(str);
245         for (i=0; i<len; ++i) {
246                 if (str[i]=='\"') {
247                         str[i] = '\'' ;
248                 }
249                 else if (isspace(str[i])) {
250                         str[i] = ' ';
251                 }
252         }
253 }
254
255
256 /*
257  * Output parseable Sieve script code based on rules input
258  */
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,
261                         char *my_addresses)
262 {
263         char *comp1 = "";
264         char *comp2 = "";
265
266         osr_sanitize(htext);
267         osr_sanitize(fileinto);
268         osr_sanitize(redirect);
269         osr_sanitize(automsg);
270
271         /* Prepare negation and match operators that will be used iff we apply a conditional */
272
273         if (!strcasecmp(compare, "contains")) {
274                 comp1 = "";
275                 comp2 = ":contains";
276         }
277         else if (!strcasecmp(compare, "notcontains")) {
278                 comp1 = "not";
279                 comp2 = ":contains";
280         }
281         else if (!strcasecmp(compare, "is")) {
282                 comp1 = "";
283                 comp2 = ":is";
284         }
285         else if (!strcasecmp(compare, "isnot")) {
286                 comp1 = "not";
287                 comp2 = ":is";
288         }
289         else if (!strcasecmp(compare, "matches")) {
290                 comp1 = "";
291                 comp2 = ":matches";
292         }
293         else if (!strcasecmp(compare, "notmatches")) {
294                 comp1 = "not";
295                 comp2 = ":matches";
296         }
297
298         /* Now do the conditional */
299
300         if (!strcasecmp(hfield, "from")) {
301                 serv_printf("if%s header %s \"From\" \"%s\"",
302                         comp1, comp2,
303                         htext
304                 );
305         }
306
307         else if (!strcasecmp(hfield, "tocc")) {
308                 serv_printf("if%s header %s [\"To\", \"Cc\"] \"%s\"",
309                         comp1, comp2,
310                         htext
311                 );
312         }
313
314         else if (!strcasecmp(hfield, "subject")) {
315                 serv_printf("if%s header %s \"Subject\" \"%s\"",
316                         comp1, comp2,
317                         htext
318                 );
319         }
320
321         else if (!strcasecmp(hfield, "replyto")) {
322                 serv_printf("if%s header %s \"Reply-to\" \"%s\"",
323                         comp1, comp2,
324                         htext
325                 );
326         }
327
328         else if (!strcasecmp(hfield, "sender")) {
329                 serv_printf("if%s header %s \"Sender\" \"%s\"",
330                         comp1, comp2,
331                         htext
332                 );
333         }
334
335         else if (!strcasecmp(hfield, "resentfrom")) {
336                 serv_printf("if%s header %s \"Resent-from\" \"%s\"",
337                         comp1, comp2,
338                         htext
339                 );
340         }
341
342         else if (!strcasecmp(hfield, "resentto")) {
343                 serv_printf("if%s header %s \"Resent-to\" \"%s\"",
344                         comp1, comp2,
345                         htext
346                 );
347         }
348
349         else if (!strcasecmp(hfield, "xmailer")) {
350                 serv_printf("if%s header %s \"X-Mailer\" \"%s\"",
351                         comp1, comp2,
352                         htext
353                 );
354         }
355
356         else if (!strcasecmp(hfield, "xspamflag")) {
357                 serv_printf("if%s header %s \"X-Spam-Flag\" \"%s\"",
358                         comp1, comp2,
359                         htext
360                 );
361         }
362
363         else if (!strcasecmp(hfield, "xspamstatus")) {
364                 serv_printf("if%s header %s \"X-Spam-Status\" \"%s\"",
365                         comp1, comp2,
366                         htext
367                 );
368         }
369
370         else if (!strcasecmp(hfield, "listid")) {
371                 serv_printf("if%s header %s \"List-ID\" \"%s\"",
372                         comp1, comp2,
373                         htext
374                 );
375         }
376
377         else if (!strcasecmp(hfield, "envfrom")) {
378                 serv_printf("if%s envelope %s \"From\" \"%s\"",
379                         comp1, comp2,
380                         htext
381                 );
382         }
383
384         else if (!strcasecmp(hfield, "envto")) {
385                 serv_printf("if%s envelope %s \"To\" \"%s\"",
386                         comp1, comp2,
387                         htext
388                 );
389         }
390
391         else if (!strcasecmp(hfield, "size")) {
392                 if (!strcasecmp(sizecomp, "larger")) {
393                         serv_printf("if size :over %d", sizeval);
394                 }
395                 else if (!strcasecmp(sizecomp, "smaller")) {
396                         serv_printf("if size :under %d", sizeval);
397                 }
398                 else {  /* failsafe - should never get here, but just in case... */
399                         serv_printf("if size :over 1");
400                 }
401         }
402
403         /* Open braces if we're in a conditional loop */
404
405         if (strcasecmp(hfield, "all")) {
406                 serv_printf("{");
407         }
408
409
410         /* Do action */
411
412         if (!strcasecmp(action, "keep")) {
413                 serv_printf("keep;");
414         }
415
416         else if (!strcasecmp(action, "discard")) {
417                 serv_printf("discard;");
418         }
419
420         else if (!strcasecmp(action, "reject")) {
421                 serv_printf("reject \"%s\";", automsg);
422         }
423
424         else if (!strcasecmp(action, "fileinto")) {
425                 serv_printf("fileinto \"%s\";", fileinto);
426         }
427
428         else if (!strcasecmp(action, "redirect")) {
429                 serv_printf("redirect \"%s\";", redirect);
430         }
431
432         else if (!strcasecmp(action, "vacation")) {
433                 serv_printf("vacation :addresses [%s]\n\"%s\";", my_addresses, automsg);
434         }
435
436
437         /* Do 'final' action */
438
439         if (!strcasecmp(final, "stop")) {
440                 serv_printf("stop;");
441         }
442
443
444         /* Close the braces if we're in a conditional loop */
445
446         if (strcasecmp(hfield, "all")) {
447                 serv_printf("}");
448         }
449
450
451         /* End of rule. */
452 }
453
454
455
456 /*
457  * Translate the fields from the rule editor into something we can save...
458  */
459 void parse_fields_from_rule_editor(void) {
460
461         int active;
462         char hfield[256];
463         char compare[32];
464         char htext[256];
465         char sizecomp[32];
466         int sizeval;
467         char action[32];
468         char fileinto[128];
469         char redirect[256];
470         char automsg[1024];
471         char final[32];
472         int i;
473         char buf[256];
474         char fname[256];
475         char rule[2048];
476         char encoded_rule[4096];
477         char my_addresses[4096];
478         
479         /* Enumerate my email addresses in case they are needed for a vacation rule */
480         my_addresses[0] = 0;
481         serv_puts("GVEA");
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");
486                 }
487                 strcat(my_addresses, "\"");
488                 strcat(my_addresses, buf);
489                 strcat(my_addresses, "\"");
490         }
491
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);
495         if (buf[0] != '4') {
496                 return;
497         }
498
499         serv_puts("# THIS SCRIPT WAS AUTOMATICALLY GENERATED BY WEBCIT.");
500         serv_puts("# ");
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.");
506         serv_puts("");
507         serv_puts("require \"fileinto\";");
508         serv_puts("require \"reject\";");
509         serv_puts("require \"vacation\";");
510         serv_puts("require \"envelope\";");
511         serv_puts("");
512
513         for (i=0; i<MAX_RULES; ++i) {
514                 
515                 strcpy(rule, "");
516
517                 sprintf(fname, "active%d", i);
518                 active = !strcasecmp(BSTR(fname), "on") ;
519
520                 if (active) {
521
522                         sprintf(fname, "hfield%d", i);
523                         safestrncpy(hfield, BSTR(fname), sizeof hfield);
524         
525                         sprintf(fname, "compare%d", i);
526                         safestrncpy(compare, BSTR(fname), sizeof compare);
527         
528                         sprintf(fname, "htext%d", i);
529                         safestrncpy(htext, BSTR(fname), sizeof htext);
530         
531                         sprintf(fname, "sizecomp%d", i);
532                         safestrncpy(sizecomp, BSTR(fname), sizeof sizecomp);
533         
534                         sprintf(fname, "sizeval%d", i);
535                         sizeval = IBSTR(fname);
536         
537                         sprintf(fname, "action%d", i);
538                         safestrncpy(action, BSTR(fname), sizeof action);
539         
540                         sprintf(fname, "fileinto%d", i);
541                         safestrncpy(fileinto, BSTR(fname), sizeof fileinto);
542         
543                         sprintf(fname, "redirect%d", i);
544                         safestrncpy(redirect, BSTR(fname), sizeof redirect);
545         
546                         sprintf(fname, "automsg%d", i);
547                         safestrncpy(automsg, BSTR(fname), sizeof automsg);
548         
549                         sprintf(fname, "final%d", i);
550                         safestrncpy(final, BSTR(fname), sizeof final);
551         
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
555                         );
556         
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);
561                         serv_puts("");
562                 }
563
564
565         }
566
567         serv_puts("stop;");
568         serv_puts("000");
569 }
570
571
572
573 /*
574  * save sieve config
575  */
576 void save_sieve(void) {
577         int bigaction;
578         char script_names[MAX_SCRIPTS][64];
579         int num_scripts = 0;
580         int active_script = (-1);
581         int i;
582         char this_name[64];
583         char buf[256];
584
585         if (!havebstr("save_button")) {
586                 strcpy(WC->ImportantMessage,
587                         _("Cancelled.  Changes were not saved."));
588                 display_main_menu();
589                 return;
590         }
591
592         parse_fields_from_rule_editor();
593
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;
601                         }
602                         ++num_scripts;
603                 }
604         }
605
606         bigaction = ibstr("bigaction");
607
608         if (bigaction == 0) {
609                 serv_puts("MSIV setactive||");
610                 serv_getln(buf, sizeof buf);
611         }
612
613         else if (bigaction == 1) {
614                 serv_printf("MSIV setactive|%s|", RULES_SCRIPT);
615                 serv_getln(buf, sizeof buf);
616         }
617
618         else if (bigaction == 2) {
619                 serv_printf("MSIV setactive|%s|", bstr("active_script"));
620                 serv_getln(buf, sizeof buf);
621         }
622
623         if (num_scripts > 0) {
624                 for (i=0; i<num_scripts; ++i) {
625                         /*
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.
629                          */
630                         if (strcasecmp(script_names[i], RULES_SCRIPT)) {
631                                 serv_printf("MSIV putscript|%s|", script_names[i]);
632                                 serv_getln(buf, sizeof buf);
633                                 if (buf[0] == '4') {
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)));
637                                         serv_puts("\n000");
638                                 }
639                         }
640                 }
641         }
642
643         strcpy(WC->ImportantMessage, _("Your changes have been saved."));
644         display_main_menu();
645         return;
646 }
647
648
649 /*
650  * show a list of available scripts to add/remove them
651  */
652 void display_add_remove_scripts(char *message)
653 {
654         char buf[256];
655         char script_name[256];
656
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");
663         
664         wc_printf("<div id=\"content\" class=\"service\">\n");
665
666         if (message != NULL) {
667                 wc_printf("%s", message);
668         }
669
670         wc_printf("<table border=0 cellspacing=10><tr valign=top><td>\n");
671
672         do_template("beginbox_1");
673         StrBufAppendBufPlain(WC->WBuf, _("Add a new script"), -1, 0);
674         do_template("beginbox_2");
675
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>");
679
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"));
686
687         do_template("endbox");
688
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")
694         );
695         do_template("endbox");
696
697         wc_printf("</td><td>");
698
699         do_template("beginbox_1");
700         StrBufAppendBufPlain(WC->WBuf, _("Delete scripts"), -1, 0);
701         do_template("beginbox_2");
702
703         wc_printf(_("To delete an existing script, select the script "
704                 "name from the list and click 'Delete'."));
705         wc_printf("<br><br>");
706         
707         wc_printf("<center>"
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");
711
712         serv_puts("MSIV listscripts");
713         serv_getln(buf, sizeof buf);
714         if (buf[0] == '1') {
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");
721                         }
722                 }
723         }
724         wc_printf("</select><br>\n");
725
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");
730
731         wc_printf("</td></tr></table>\n");
732
733         wDumpContent(1);
734 }
735
736
737
738 /*
739  * delete a script
740  */
741 void delete_script(void) {
742         char buf[256];
743
744         serv_printf("MSIV deletescript|%s", bstr("script_name"));
745         serv_getln(buf, sizeof buf);
746         display_add_remove_scripts(&buf[4]);
747 }
748                 
749
750
751 /*
752  * create a new script
753  * take the web environment script name and create it on the citadel server
754  */
755 void create_script(void) {
756         char buf[256];
757
758         serv_printf("MSIV getscript|%s", bstr("script_name"));
759         serv_getln(buf, sizeof buf);
760         if (buf[0] == '1') {
761                 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
762                         /* flush */
763                 }
764                 display_add_remove_scripts(_("A script by that name already exists."));
765                 return;
766         }
767         
768         serv_printf("MSIV putscript|%s", bstr("script_name"));
769         serv_getln(buf, sizeof buf);
770         if (buf[0] == '4') {
771                 serv_puts("keep;");
772                 serv_puts("000");
773                 display_add_remove_scripts(_("A new script has been created.  Return to the script editing screen to edit and activate it."));
774                 return;
775         }
776
777         display_add_remove_scripts(&buf[4]);
778 }
779
780
781
782
783 void display_rules_editor_inner_div(void) {
784         int i, j;
785         char buf[4096];
786         char rules[MAX_RULES][2048];
787
788         struct {
789                 char name[128];
790         } *rooms = NULL;
791         int num_roomnames = 0;
792         int num_roomnames_alloc = 0;
793
794         int active;
795         char hfield[256];
796         char compare[32];
797         char htext[256];
798         char sizecomp[32];
799         int sizeval;
800         char action[32];
801         char fileinto[128];
802         char redirect[256];
803         char automsg[1024];
804         char final[32];
805
806         /* load the rules */
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));
816                 }
817         }
818
819         /* load the roomnames */
820         serv_puts("LKRA");
821         serv_getln(buf, sizeof buf);
822         if (buf[0] == '1') {
823                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
824                         ++num_roomnames;
825                         if (num_roomnames > num_roomnames_alloc) {
826                                 num_roomnames_alloc += 250;
827                                 rooms = realloc(rooms, (num_roomnames_alloc * 128));
828                         }
829                         extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
830                 }
831         }
832
833
834 /*
835  * This script should get called by every onChange event...
836  *
837  */
838         wc_printf("<script type=\"text/javascript\">                                    \n"
839                 "                                                                       \n"
840                 "var highest_active_rule = (-1);                                        \n"
841                 "                                                                       \n"
842                 "function UpdateRules() {                                               \n");
843 /*
844  * Show only the active rows...
845  */
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"
851                 "   }                                                                   \n"
852                 "   else {                                                              \n"
853                 "     $('rule' + i).style.display = 'none';                             \n"
854                 "   }                                                                   \n"
855                 "  }                                                                    \n");
856 /*
857  * Show only the fields relevant to the rules...
858  */
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"
863                 "    }                                                                  \n"
864                 "    else {                                                             \n"
865                 "      d.style.display = 'none';                                        \n"
866                 "    }                                                                  \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"
872                 "    }                                                                  \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"
877                 "    }                                                                  \n"
878                 "    else {                                                             \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"
882                 "    }                                                                  \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"
896                 "    } else {                                                           \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"
900                 "    }                                                                  \n"
901                 "    if (highest_active_rule < %d) {                                    \n", MAX_RULES-1 );
902         wc_printf("      $('div_addrule').style.display = 'block';                      \n"
903                 "    } else {                                                           \n"
904                 "      $('div_addrule').style.display = 'none';                         \n"
905                 "    }                                                                  \n"
906                 "  }                                                                    \n"
907                 "}                                                                      \n"
908 /*
909  * Add a rule (really, just un-hide it)
910  */
911                 "function AddRule() {                                                   \n"
912                 "  highest_active_rule = highest_active_rule + 1;                       \n"
913                 "  $('active'+highest_active_rule).checked = true;                      \n"
914                 "  UpdateRules();                                                       \n"
915                 "}                                                                      \n"
916 /*
917  * Swap two rules
918  */
919                 "function SwapRules(ra, rb) {                                           \n"
920                 "                                                                       \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"
932                 "                                                                       \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"
937                 "  }                                                                    \n"
938                 "}                                                                      \n"
939 /*
940  * Delete a rule (percolate the deleted rule out to the end, then deactivate it)
941  */
942                 "function DeleteRule(rd) {                                              \n"
943                 "  for (j=rd; j<=highest_active_rule; ++j) {                            \n"
944                 "    SwapRules(j, (j+1));                                               \n"
945                 "  }                                                                    \n"
946                 "  $('active'+highest_active_rule).checked = false;                     \n"
947                 "}                                                                      \n"
948                 "</script>                                                              \n"
949         );
950
951
952         wc_printf("<br>");
953
954         wc_printf("<table cellpadding=2 width=100%%>");
955
956         for (i=0; i<MAX_RULES; ++i) {
957
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);
970                 
971                 /* now generate the table row */
972
973                 wc_printf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
974                         i,
975                         ((i%2) ? "DDDDDD" : "FFFFFF")
976                 );
977
978                 wc_printf("<td width=5%% align=\"center\">");
979
980                 wc_printf("<div style=\"display:none\">");
981                 wc_printf("<input type=\"checkbox\" name=\"active%d\" id=\"active%d\" %s>",
982                         i, i,
983                         (active ? "checked" : "")
984                 );
985                 wc_printf("</div>");
986
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") );
991
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") );
996
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") );
1001
1002                 wc_printf("</td>");
1003
1004                 wc_printf("<td width=5%% align=\"center\">");
1005                 wc_printf("<font size=+2>%d</font>", i+1);
1006                 wc_printf("</td>");
1007
1008                 wc_printf("<td width=20%%>%s ", _("If") );
1009
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")       },
1025                         {       "all",          _("All")                }
1026                 };
1027
1028                 wc_printf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
1029                         i, i);
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],
1034                                 hfield_values[j][1]
1035                         );
1036                 }
1037
1038                 wc_printf("</select>");
1039                 wc_printf("</td>");
1040
1041                 wc_printf("<td width=20%%>");
1042
1043                 char *compare_values[6][2] = {
1044                         {       "contains",     _("contains")           },
1045                         {       "notcontains",  _("does not contain")   },
1046                         {       "is",           _("is")                 },
1047                         {       "isnot",        _("is not")             },
1048                         {       "matches",      _("matches")            },
1049                         {       "notmatches",   _("does not match")     }
1050                 };
1051
1052                 wc_printf("<div id=\"div_compare%d\">", i);
1053                 wc_printf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
1054                         i, i);
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]
1060                         );
1061                 }
1062                 wc_printf("</select>");
1063
1064                 wc_printf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\" value=\"", i, i);
1065                 escputs(htext);
1066                 wc_printf("\"></div>");
1067
1068                 wc_printf("<div id=\"div_nocompare%d\">", i);
1069                 wc_printf("%s", _("(All messages)"));
1070                 wc_printf("</div>");
1071
1072                 char *sizecomp_values[2][2] = {
1073                         {       "larger",       _("is larger than")     },
1074                         {       "smaller",      _("is smaller than")    }
1075                 };
1076
1077                 wc_printf("<div id=\"div_size%d\">", i);
1078                 wc_printf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
1079                         i, i);
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]
1085                         );
1086                 }
1087                 wc_printf("</select>");
1088
1089                 wc_printf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\" value=\"%d\">",
1090                         i, i, sizeval);
1091                 wc_printf("bytes");
1092                 wc_printf("</div>");
1093
1094                 wc_printf("</td>");
1095
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")           }
1103                 };
1104
1105                 wc_printf("<td width=20%%>");
1106                 wc_printf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
1107                         i, i);
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],
1112                                 action_values[j][1]
1113                         );
1114                 }
1115                 wc_printf("</select>");
1116
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 ");
1123                         }
1124                         wc_printf("value=\"");
1125                         escputs(rooms[j].name);
1126                         wc_printf("\">");
1127                         escputs(rooms[j].name);
1128                         wc_printf("</option>\n");
1129                 }
1130                 wc_printf("</select>\n");
1131                 wc_printf("</div>");
1132
1133                 wc_printf("<div id=\"div_redirect%d\">", i);
1134                 wc_printf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\" value=\"", i, i);
1135                 escputs(redirect);
1136                 wc_printf("\"></div>");
1137
1138                 wc_printf("<div id=\"div_automsg%d\">", i);
1139                 wc_printf(_("Message:"));
1140                 wc_printf("<br>");
1141                 wc_printf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
1142                 escputs(automsg);
1143                 wc_printf("</textarea>");
1144                 wc_printf("</div>");
1145
1146                 wc_printf("</td>");
1147
1148                 char *final_values[2][2] = {
1149                         {       "continue",     _("continue processing")        },
1150                         {       "stop",         _("stop")                       }
1151                 };
1152
1153                 wc_printf("<td width=10%% align=\"center\">%s</td>", _("and then") );
1154
1155                 wc_printf("<td width=20%%>");
1156                 wc_printf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
1157                         i, i);
1158                 for (j=0; j<2; ++j) {
1159                         wc_printf("<option %s value=\"%s\">%s</option>",
1160                                 ( (!strcasecmp(final, final_values[j][0])) ? "selected" : ""),
1161                                 final_values[j][0],
1162                                 final_values[j][1]
1163                         );
1164                 }
1165                 wc_printf("</select>");
1166                 wc_printf("</td>");
1167
1168                 wc_printf("</tr>\n");
1169
1170         }
1171
1172         wc_printf("</table>");
1173         wc_printf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">%s</a><br></div>\n",
1174                 _("Add rule")
1175         );
1176
1177         wc_printf("<script type=\"text/javascript\">                                    \n");
1178         wc_printf("UpdateRules();                                                               \n");
1179         wc_printf("</script>                                                            \n");
1180
1181         free(rooms);
1182 }
1183
1184 void _display_add_remove_scripts(void) {display_add_remove_scripts(NULL);}
1185
1186 void 
1187 InitModule_SIEVE
1188 (void)
1189 {
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);
1195 }