Revert "Removed the "fix_scrollbarbug" div and all references to it."
[citadel.git] / webcit / sieve.c
1 #include "webcit.h"
2
3 #define MAX_SCRIPTS     100
4 #define MAX_RULES       50
5 #define RULES_SCRIPT    "__WebCit_Generated_Script__"
6
7
8 /*
9  * dummy panel indicating to the user that the server doesn't support Sieve
10  */
11 void display_no_sieve(void) {
12
13         output_headers(1, 1, 2, 0, 0, 0);
14
15         wc_printf("<div id=\"banner\">\n");
16         wc_printf("<img src=\"static/advanpage2_48x.gif\">");
17         wc_printf("<h1>");
18         wc_printf(_("View/edit server-side mail filters"));
19         wc_printf("</h1>\n");
20         wc_printf("</div>\n");
21
22         wc_printf("<div id=\"content\" class=\"service\">\n");
23
24         wc_printf("<div class=\"fix_scrollbar_bug\">"
25                 "<table class=\"sieve_background\">"
26                 "<tr><td valign=top>\n");
27
28         wc_printf(_("This installation of Citadel was built without support for server-side mail filtering."
29                 "<br>Please contact your system administrator if you require this feature.<br>"));
30
31         wc_printf("</td></tr></table></div>\n");
32         wDumpContent(1);
33 }
34
35
36 /*
37  * view/edit sieve config
38  */
39 void display_sieve(void)
40 {
41         char script_names[MAX_SCRIPTS][64];
42         int num_scripts = 0;
43         int active_script = (-1);
44         char buf[SIZ];          /* Don't make this buffer smaller or it will restrict line length */
45         int i;
46         int rules_script_is_active = 0;
47
48         if (!WC->serv_info->serv_supports_sieve) {
49                 display_no_sieve();
50                 return;
51         }
52
53         memset(script_names, 0, sizeof script_names);
54
55         serv_puts("MSIV listscripts");
56         serv_getln(buf, sizeof(buf));
57         if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
58                 if (num_scripts < MAX_SCRIPTS) {
59                         extract_token(script_names[num_scripts], buf, 0, '|', 64);
60                         if (extract_int(buf, 1) > 0) {
61                                 active_script = num_scripts;
62                                 if (!strcasecmp(script_names[num_scripts], RULES_SCRIPT)) {
63                                         rules_script_is_active = 1;
64                                 }
65                         }
66                         ++num_scripts;
67                 }
68         }
69
70         output_headers(1, 1, 2, 0, 0, 0);
71
72         wc_printf("<script type=\"text/javascript\">                                    \n"
73                 "                                                                       \n"
74                 "var previously_active_script;                                          \n"
75                 "                                                                       \n"
76                 "function ToggleSievePanels() {                                         \n"
77                 " d = ($('sieveform').bigaction.options[$('sieveform').bigaction.selectedIndex].value); \n"
78                 " for (i=0; i<3; ++i) {                                                 \n"
79                 "  if (i == d) {                                                        \n"
80                 "   $('sievediv' + i).style.display = 'block';                          \n"
81                 "  }                                                                    \n"
82                 "  else {                                                               \n"
83                 "   $('sievediv' + i).style.display = 'none';                           \n"
84                 "  }                                                                    \n"
85                 " }                                                                     \n"
86                 "}                                                                      \n"
87                 "                                                                       \n"
88                 "function ToggleScriptPanels() {                                        \n"
89                 " d = ($('sieveform').active_script.options[$('sieveform').active_script.selectedIndex].value); \n"
90                 " if ($('script_' + previously_active_script)) {                        \n"
91                 "  $('script_' + previously_active_script).style.display = 'none';      \n"
92                 " }                                                                     \n"
93                 " $('script_' + d).style.display = 'block';                             \n"
94                 " previously_active_script = d;                                         \n"
95                 "}                                                                      \n"
96                 "                                                                       \n"
97                 "</script>                                                              \n"
98         );
99
100         wc_printf("<div id=\"banner\">\n");
101         wc_printf("<img src=\"static/advanpage2_48x.gif\">");
102         wc_printf("<h1>");
103         wc_printf(_("View/edit server-side mail filters"));
104         wc_printf("</h1>\n");
105         wc_printf("</div>\n");
106
107         wc_printf("<div id=\"content\" class=\"service\">\n");
108
109         wc_printf("<div class=\"fix_scrollbar_bug\">"
110                 "<table class=\"sieve_background\">"
111                 "<tr><td valign=top>\n");
112
113
114         wc_printf("<form id=\"sieveform\" method=\"post\" action=\"save_sieve\">\n");
115         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
116
117         wc_printf(_("When new mail arrives: "));
118         wc_printf("<select name=\"bigaction\" size=1 onChange=\"ToggleSievePanels();\">\n");
119
120         wc_printf("<option %s value=\"0\">", ((active_script < 0) ? "selected" : ""));
121         wc_printf(_("Leave it in my inbox without filtering"));
122         wc_printf("</option>\n");
123
124         wc_printf("<option %s value=\"1\">", ((rules_script_is_active) ? "selected" : ""));
125         wc_printf(_("Filter it according to rules selected below"));
126         wc_printf("</option>\n");
127
128         wc_printf("<option %s value=\"2\">",
129                         (((active_script >= 0) && (!rules_script_is_active)) ? "selected" : ""));
130         wc_printf(_("Filter it through a manually edited script (advanced users only)"));
131         wc_printf("</option>\n");
132
133         wc_printf("</select>");
134
135
136
137         /* The "no filtering" div */
138
139         wc_printf("<div id=\"sievediv0\" style=\"display:none\">\n");
140         wc_printf("<div align=\"center\"><br /><br />");
141         wc_printf(_("Your incoming mail will not be filtered through any scripts."));
142         wc_printf("<br /><br /></div>\n");
143         wc_printf("</div>\n");
144
145         /* The "webcit managed scripts" div */
146
147         wc_printf("<div id=\"sievediv1\" style=\"display:none\">\n");
148         display_rules_editor_inner_div();
149         wc_printf("</div>\n");
150
151         /* The "I'm smart and can write my own Sieve scripts" div */
152
153         wc_printf("<div id=\"sievediv2\" style=\"display:none\">\n");
154
155         if (num_scripts > 0) {
156                 wc_printf(_("The currently active script is: "));
157                 wc_printf("<select name=\"active_script\" size=1 onChange=\"ToggleScriptPanels();\">\n");
158                 for (i=0; i<num_scripts; ++i) {
159                         if (strcasecmp(script_names[i], RULES_SCRIPT)) {
160                                 wc_printf("<option %s value=\"%s\">%s</option>\n",
161                                         ((active_script == i) ? "selected" : ""),
162                                         script_names[i],
163                                         script_names[i]
164                                 );
165                         }
166                 }
167                 wc_printf("</select>\n");
168         }
169
170         wc_printf("&nbsp;&nbsp;&nbsp;");
171         wc_printf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add or delete scripts"));
172
173         wc_printf("<br />\n");
174
175         if (num_scripts > 0) {
176                 for (i=0; i<num_scripts; ++i) {
177                         if (strcasecmp(script_names[i], RULES_SCRIPT)) {
178                                 wc_printf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
179                                 wc_printf("<textarea name=\"text_%s\" wrap=soft rows=20 cols=80 width=80>\n",
180                                         script_names[i]);
181                                 serv_printf("MSIV getscript|%s", script_names[i]);
182                                 serv_getln(buf, sizeof buf);
183                                 if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
184                                         wc_printf("%s\n", buf);
185                                 }
186                                 wc_printf("</textarea>\n");
187                                 wc_printf("</div>\n");
188                         }
189                 }
190         }
191
192         wc_printf("<script type=\"text/javascript\">    \n"
193                 "ToggleScriptPanels();                  \n"
194                 "</script>                              \n"
195         );
196
197         wc_printf("</div>\n");
198
199
200         /* The rest of this is common for all panels... */
201
202         wc_printf("<div align=\"center\"><br>");
203         wc_printf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
204         wc_printf("&nbsp;");
205         wc_printf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
206         wc_printf("</div></form>\n");
207
208         wc_printf("</td></tr></table></div>\n");
209
210         wc_printf("<script type=\"text/javascript\">    \n"
211                 "ToggleSievePanels();                   \n"
212                 "</script>                              \n"
213         );
214
215         wDumpContent(1);
216
217 }
218
219
220
221 /*
222  * Helper function for output_sieve_rule() to output strings with quotes escaped
223  */
224 void osr_sanitize(char *str) {
225         int i, len;
226
227         if (str == NULL) return;
228         len = strlen(str);
229         for (i=0; i<len; ++i) {
230                 if (str[i]=='\"') {
231                         str[i] = '\'' ;
232                 }
233                 else if (isspace(str[i])) {
234                         str[i] = ' ';
235                 }
236         }
237 }
238
239
240 /*
241  * Output parseable Sieve script code based on rules input
242  */
243 void output_sieve_rule(char *hfield, char *compare, char *htext, char *sizecomp, int sizeval,
244                         char *action, char *fileinto, char *redirect, char *automsg, char *final,
245                         char *my_addresses)
246 {
247         char *comp1 = "";
248         char *comp2 = "";
249
250         osr_sanitize(htext);
251         osr_sanitize(fileinto);
252         osr_sanitize(redirect);
253         osr_sanitize(automsg);
254
255         /* Prepare negation and match operators that will be used iff we apply a conditional */
256
257         if (!strcasecmp(compare, "contains")) {
258                 comp1 = "";
259                 comp2 = ":contains";
260         }
261         else if (!strcasecmp(compare, "notcontains")) {
262                 comp1 = "not";
263                 comp2 = ":contains";
264         }
265         else if (!strcasecmp(compare, "is")) {
266                 comp1 = "";
267                 comp2 = ":is";
268         }
269         else if (!strcasecmp(compare, "isnot")) {
270                 comp1 = "not";
271                 comp2 = ":is";
272         }
273         else if (!strcasecmp(compare, "matches")) {
274                 comp1 = "";
275                 comp2 = ":matches";
276         }
277         else if (!strcasecmp(compare, "notmatches")) {
278                 comp1 = "not";
279                 comp2 = ":matches";
280         }
281
282         /* Now do the conditional */
283
284         if (!strcasecmp(hfield, "from")) {
285                 serv_printf("if%s header %s \"From\" \"%s\"",
286                         comp1, comp2,
287                         htext
288                 );
289         }
290
291         else if (!strcasecmp(hfield, "tocc")) {
292                 serv_printf("if%s header %s [\"To\", \"Cc\"] \"%s\"",
293                         comp1, comp2,
294                         htext
295                 );
296         }
297
298         else if (!strcasecmp(hfield, "subject")) {
299                 serv_printf("if%s header %s \"Subject\" \"%s\"",
300                         comp1, comp2,
301                         htext
302                 );
303         }
304
305         else if (!strcasecmp(hfield, "replyto")) {
306                 serv_printf("if%s header %s \"Reply-to\" \"%s\"",
307                         comp1, comp2,
308                         htext
309                 );
310         }
311
312         else if (!strcasecmp(hfield, "sender")) {
313                 serv_printf("if%s header %s \"Sender\" \"%s\"",
314                         comp1, comp2,
315                         htext
316                 );
317         }
318
319         else if (!strcasecmp(hfield, "resentfrom")) {
320                 serv_printf("if%s header %s \"Resent-from\" \"%s\"",
321                         comp1, comp2,
322                         htext
323                 );
324         }
325
326         else if (!strcasecmp(hfield, "resentto")) {
327                 serv_printf("if%s header %s \"Resent-to\" \"%s\"",
328                         comp1, comp2,
329                         htext
330                 );
331         }
332
333         else if (!strcasecmp(hfield, "xmailer")) {
334                 serv_printf("if%s header %s \"X-Mailer\" \"%s\"",
335                         comp1, comp2,
336                         htext
337                 );
338         }
339
340         else if (!strcasecmp(hfield, "xspamflag")) {
341                 serv_printf("if%s header %s \"X-Spam-Flag\" \"%s\"",
342                         comp1, comp2,
343                         htext
344                 );
345         }
346
347         else if (!strcasecmp(hfield, "xspamstatus")) {
348                 serv_printf("if%s header %s \"X-Spam-Status\" \"%s\"",
349                         comp1, comp2,
350                         htext
351                 );
352         }
353
354         else if (!strcasecmp(hfield, "listid")) {
355                 serv_printf("if%s header %s \"List-ID\" \"%s\"",
356                         comp1, comp2,
357                         htext
358                 );
359         }
360
361         else if (!strcasecmp(hfield, "envfrom")) {
362                 serv_printf("if%s envelope %s \"From\" \"%s\"",
363                         comp1, comp2,
364                         htext
365                 );
366         }
367
368         else if (!strcasecmp(hfield, "envto")) {
369                 serv_printf("if%s envelope %s \"To\" \"%s\"",
370                         comp1, comp2,
371                         htext
372                 );
373         }
374
375         else if (!strcasecmp(hfield, "size")) {
376                 if (!strcasecmp(sizecomp, "larger")) {
377                         serv_printf("if size :over %d", sizeval);
378                 }
379                 else if (!strcasecmp(sizecomp, "smaller")) {
380                         serv_printf("if size :under %d", sizeval);
381                 }
382                 else {  /* failsafe - should never get here, but just in case... */
383                         serv_printf("if size :over 1");
384                 }
385         }
386
387         /* Open braces if we're in a conditional loop */
388
389         if (strcasecmp(hfield, "all")) {
390                 serv_printf("{");
391         }
392
393
394         /* Do action */
395
396         if (!strcasecmp(action, "keep")) {
397                 serv_printf("keep;");
398         }
399
400         else if (!strcasecmp(action, "discard")) {
401                 serv_printf("discard;");
402         }
403
404         else if (!strcasecmp(action, "reject")) {
405                 serv_printf("reject \"%s\";", automsg);
406         }
407
408         else if (!strcasecmp(action, "fileinto")) {
409                 serv_printf("fileinto \"%s\";", fileinto);
410         }
411
412         else if (!strcasecmp(action, "redirect")) {
413                 serv_printf("redirect \"%s\";", redirect);
414         }
415
416         else if (!strcasecmp(action, "vacation")) {
417                 serv_printf("vacation :addresses [%s]\n\"%s\";", my_addresses, automsg);
418         }
419
420
421         /* Do 'final' action */
422
423         if (!strcasecmp(final, "stop")) {
424                 serv_printf("stop;");
425         }
426
427
428         /* Close the braces if we're in a conditional loop */
429
430         if (strcasecmp(hfield, "all")) {
431                 serv_printf("}");
432         }
433
434
435         /* End of rule. */
436 }
437
438
439
440 /*
441  * Translate the fields from the rule editor into something we can save...
442  */
443 void parse_fields_from_rule_editor(void) {
444
445         int active;
446         char hfield[256];
447         char compare[32];
448         char htext[256];
449         char sizecomp[32];
450         int sizeval;
451         char action[32];
452         char fileinto[128];
453         char redirect[256];
454         char automsg[1024];
455         char final[32];
456         int i;
457         char buf[256];
458         char fname[256];
459         char rule[2048];
460         char encoded_rule[4096];
461         char my_addresses[4096];
462         
463         /* Enumerate my email addresses in case they are needed for a vacation rule */
464         my_addresses[0] = 0;
465         serv_puts("GVEA");
466         serv_getln(buf, sizeof buf);
467         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
468                 if (!IsEmptyStr(my_addresses)) {
469                         strcat(my_addresses, ",\n");
470                 }
471                 strcat(my_addresses, "\"");
472                 strcat(my_addresses, buf);
473                 strcat(my_addresses, "\"");
474         }
475
476         /* Now generate the script and write it to the Citadel server */
477         serv_printf("MSIV putscript|%s|", RULES_SCRIPT);
478         serv_getln(buf, sizeof buf);
479         if (buf[0] != '4') {
480                 return;
481         }
482
483         serv_puts("# THIS SCRIPT WAS AUTOMATICALLY GENERATED BY WEBCIT.");
484         serv_puts("# ");
485         serv_puts("# Do not attempt to manually edit it.  If you do so,");
486         serv_puts("# your changes will be overwritten the next time WebCit");
487         serv_puts("# saves its mail filtering rule set.  If you really want");
488         serv_puts("# to use these rules as the basis for another script,");
489         serv_puts("# copy them to another script and save that instead.");
490         serv_puts("");
491         serv_puts("require \"fileinto\";");
492         serv_puts("require \"reject\";");
493         serv_puts("require \"vacation\";");
494         serv_puts("require \"envelope\";");
495         serv_puts("");
496
497         for (i=0; i<MAX_RULES; ++i) {
498                 
499                 strcpy(rule, "");
500
501                 sprintf(fname, "active%d", i);
502                 active = !strcasecmp(BSTR(fname), "on") ;
503
504                 if (active) {
505
506                         sprintf(fname, "hfield%d", i);
507                         safestrncpy(hfield, BSTR(fname), sizeof hfield);
508         
509                         sprintf(fname, "compare%d", i);
510                         safestrncpy(compare, BSTR(fname), sizeof compare);
511         
512                         sprintf(fname, "htext%d", i);
513                         safestrncpy(htext, BSTR(fname), sizeof htext);
514         
515                         sprintf(fname, "sizecomp%d", i);
516                         safestrncpy(sizecomp, BSTR(fname), sizeof sizecomp);
517         
518                         sprintf(fname, "sizeval%d", i);
519                         sizeval = IBSTR(fname);
520         
521                         sprintf(fname, "action%d", i);
522                         safestrncpy(action, BSTR(fname), sizeof action);
523         
524                         sprintf(fname, "fileinto%d", i);
525                         safestrncpy(fileinto, BSTR(fname), sizeof fileinto);
526         
527                         sprintf(fname, "redirect%d", i);
528                         safestrncpy(redirect, BSTR(fname), sizeof redirect);
529         
530                         sprintf(fname, "automsg%d", i);
531                         safestrncpy(automsg, BSTR(fname), sizeof automsg);
532         
533                         sprintf(fname, "final%d", i);
534                         safestrncpy(final, BSTR(fname), sizeof final);
535         
536                         snprintf(rule, sizeof rule, "%d|%s|%s|%s|%s|%d|%s|%s|%s|%s|%s",
537                                 active, hfield, compare, htext, sizecomp, sizeval, action, fileinto,
538                                 redirect, automsg, final
539                         );
540         
541                         CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1, 0);
542                         serv_printf("# WEBCIT_RULE|%d|%s|", i, encoded_rule);
543                         output_sieve_rule(hfield, compare, htext, sizecomp, sizeval,
544                                         action, fileinto, redirect, automsg, final, my_addresses);
545                         serv_puts("");
546                 }
547
548
549         }
550
551         serv_puts("stop;");
552         serv_puts("000");
553 }
554
555
556
557 /*
558  * save sieve config
559  */
560 void save_sieve(void) {
561         int bigaction;
562         char script_names[MAX_SCRIPTS][64];
563         int num_scripts = 0;
564         int active_script = (-1);
565         int i;
566         char this_name[64];
567         char buf[256];
568
569         if (!havebstr("save_button")) {
570                 strcpy(WC->ImportantMessage,
571                         _("Cancelled.  Changes were not saved."));
572                 display_main_menu();
573                 return;
574         }
575
576         parse_fields_from_rule_editor();
577
578         serv_puts("MSIV listscripts");
579         serv_getln(buf, sizeof(buf));
580         if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
581                 if (num_scripts < MAX_SCRIPTS) {
582                         extract_token(script_names[num_scripts], buf, 0, '|', 64);
583                         if (extract_int(buf, 1) > 0) {
584                                 active_script = num_scripts;
585                         }
586                         ++num_scripts;
587                 }
588         }
589
590         bigaction = ibstr("bigaction");
591
592         if (bigaction == 0) {
593                 serv_puts("MSIV setactive||");
594                 serv_getln(buf, sizeof buf);
595         }
596
597         else if (bigaction == 1) {
598                 serv_printf("MSIV setactive|%s|", RULES_SCRIPT);
599                 serv_getln(buf, sizeof buf);
600         }
601
602         else if (bigaction == 2) {
603                 serv_printf("MSIV setactive|%s|", bstr("active_script"));
604                 serv_getln(buf, sizeof buf);
605         }
606
607         if (num_scripts > 0) {
608                 for (i=0; i<num_scripts; ++i) {
609                         /*
610                          * We only want to save the scripts from the "manually edited scripts"
611                          * screen.  The script that WebCit generates from its ruleset will be
612                          * auto-generated by parse_fields_from_rule_editor() and saved there.
613                          */
614                         if (strcasecmp(script_names[i], RULES_SCRIPT)) {
615                                 serv_printf("MSIV putscript|%s|", script_names[i]);
616                                 serv_getln(buf, sizeof buf);
617                                 if (buf[0] == '4') {
618                                         snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
619                                         striplt((char *)BSTR(this_name)); /* TODO: get rid of typecast*/
620                                         serv_write(BSTR(this_name), strlen(BSTR(this_name)));
621                                         serv_puts("\n000");
622                                 }
623                         }
624                 }
625         }
626
627         strcpy(WC->ImportantMessage, _("Your changes have been saved."));
628         display_main_menu();
629         return;
630 }
631
632
633 /*
634  * show a list of available scripts to add/remove them
635  */
636 void display_add_remove_scripts(char *message)
637 {
638         char buf[256];
639         char script_name[256];
640
641         output_headers(1, 1, 2, 0, 0, 0);
642         wc_printf("<div id=\"banner\">\n");
643         wc_printf("<img src=\"static/advanpage2_48x.gif\">");
644         wc_printf(_("Add or delete scripts"));
645         wc_printf("</h1>\n");
646         wc_printf("</div>\n");
647         
648         wc_printf("<div id=\"content\" class=\"service\">\n");
649
650         if (message != NULL) {
651                 wc_printf("%s", message);
652         }
653
654         wc_printf("<table border=0 cellspacing=10><tr valign=top><td>\n");
655
656         do_template("beginbox_1", NULL);
657         StrBufAppendBufPlain(WC->WBuf, _("Add a new script"), -1, 0);
658         do_template("beginbox_2", NULL);
659
660         wc_printf(_("To create a new script, enter the desired "
661                 "script name in the box below and click 'Create'."));
662         wc_printf("<br /><br />");
663
664         wc_printf("<center><form method=\"POST\" action=\"create_script\">\n");
665         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
666         wc_printf(_("Script name: "));
667         wc_printf("<input type=\"text\" name=\"script_name\"><br />\n"
668                 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
669                 "</form></center>\n", _("Create"));
670
671         do_template("endbox", NULL);
672
673         do_template("beginbox_1", NULL);
674         StrBufAppendBufPlain(WC->WBuf, _("Edit scripts"), -1, 0);
675         do_template("beginbox_2", NULL);
676         wc_printf("<br /><div align=center><a href=\"display_sieve\">%s</a><br /><br />\n",
677                 _("Return to the script editing screen")
678         );
679         do_template("endbox", NULL);
680
681         wc_printf("</td><td>");
682
683         do_template("beginbox_1", NULL);
684         StrBufAppendBufPlain(WC->WBuf, _("Delete scripts"), -1, 0);
685         do_template("beginbox_2", NULL);
686
687         wc_printf(_("To delete an existing script, select the script "
688                 "name from the list and click 'Delete'."));
689         wc_printf("<br /><br />");
690         
691         wc_printf("<center>"
692                 "<form method=\"POST\" action=\"delete_script\">\n");
693         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
694         wc_printf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
695
696         serv_puts("MSIV listscripts");
697         serv_getln(buf, sizeof buf);
698         if (buf[0] == '1') {
699                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
700                         extract_token(script_name, buf, 0, '|', sizeof script_name);
701                         if ( (extract_int(buf, 1) == 0) && (strcasecmp(script_name, RULES_SCRIPT)) ) {
702                                 wc_printf("<option>");
703                                 escputs(script_name);
704                                 wc_printf("</option>\n");
705                         }
706                 }
707         }
708         wc_printf("</select><br />\n");
709
710         wc_printf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
711                 "onClick=\"return confirm('%s');\">", _("Delete script"), _("Delete this script?"));
712         wc_printf("</form></center>\n");
713         do_template("endbox", NULL);
714
715         wc_printf("</td></tr></table>\n");
716
717         wDumpContent(1);
718 }
719
720
721
722 /*
723  * delete a script
724  */
725 void delete_script(void) {
726         char buf[256];
727
728         serv_printf("MSIV deletescript|%s", bstr("script_name"));
729         serv_getln(buf, sizeof buf);
730         display_add_remove_scripts(&buf[4]);
731 }
732                 
733
734
735 /*
736  * create a new script
737  * take the web environment script name and create it on the citadel server
738  */
739 void create_script(void) {
740         char buf[256];
741
742         serv_printf("MSIV getscript|%s", bstr("script_name"));
743         serv_getln(buf, sizeof buf);
744         if (buf[0] == '1') {
745                 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
746                         /* flush */
747                 }
748                 display_add_remove_scripts(_("A script by that name already exists."));
749                 return;
750         }
751         
752         serv_printf("MSIV putscript|%s", bstr("script_name"));
753         serv_getln(buf, sizeof buf);
754         if (buf[0] == '4') {
755                 serv_puts("keep;");
756                 serv_puts("000");
757                 display_add_remove_scripts(_("A new script has been created.  Return to the script editing screen to edit and activate it."));
758                 return;
759         }
760
761         display_add_remove_scripts(&buf[4]);
762 }
763
764
765
766
767 void display_rules_editor_inner_div(void) {
768         int i, j;
769         char buf[4096];
770         char rules[MAX_RULES][2048];
771
772         struct {
773                 char name[128];
774         } *rooms = NULL;
775         int num_roomnames = 0;
776         int num_roomnames_alloc = 0;
777
778         int active;
779         char hfield[256];
780         char compare[32];
781         char htext[256];
782         char sizecomp[32];
783         int sizeval;
784         char action[32];
785         char fileinto[128];
786         char redirect[256];
787         char automsg[1024];
788         char final[32];
789
790         /* load the rules */
791         memset(rules, 0, sizeof rules);
792         serv_printf("MSIV getscript|%s", RULES_SCRIPT);
793         serv_getln(buf, sizeof buf);
794         if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
795                 if (!strncasecmp(buf, "# WEBCIT_RULE|", 14)) {
796                         j = extract_int(buf, 1);
797                         remove_token(buf, 0, '|');
798                         remove_token(buf, 0, '|');
799                         CtdlDecodeBase64(rules[j], buf, strlen(buf));
800                 }
801         }
802
803         /* load the roomnames */
804         serv_puts("LKRA");
805         serv_getln(buf, sizeof buf);
806         if (buf[0] == '1') {
807                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
808                         ++num_roomnames;
809                         if (num_roomnames > num_roomnames_alloc) {
810                                 num_roomnames_alloc += 250;
811                                 rooms = realloc(rooms, (num_roomnames_alloc * 128));
812                         }
813                         extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
814                 }
815         }
816
817
818 /*
819  * This script should get called by every onChange event...
820  *
821  */
822         wc_printf("<script type=\"text/javascript\">                                    \n"
823                 "                                                                       \n"
824                 "var highest_active_rule = (-1);                                        \n"
825                 "                                                                       \n"
826                 "function UpdateRules() {                                               \n");
827 /*
828  * Show only the active rows...
829  */
830         wc_printf("  highest_active_rule = (-1);                                                \n");
831         wc_printf("  for (i=0; i<%d; ++i) {                                             \n", MAX_RULES);
832         wc_printf("   if ($('active'+i).checked) {                                      \n"
833                 "     $('rule' + i).style.display = 'block';                            \n"
834                 "     highest_active_rule = i;                                          \n"
835                 "   }                                                                   \n"
836                 "   else {                                                              \n"
837                 "     $('rule' + i).style.display = 'none';                             \n"
838                 "   }                                                                   \n"
839                 "  }                                                                    \n");
840 /*
841  * Show only the fields relevant to the rules...
842  */
843         wc_printf("  for (i=0; i<=highest_active_rule; ++i) {                           \n"
844                 "    d = ($('movedown'+i));                                             \n"
845                 "    if (i < highest_active_rule) {                                     \n"
846                 "      d.style.display = 'block';                                       \n"
847                 "    }                                                                  \n"
848                 "    else {                                                             \n"
849                 "      d.style.display = 'none';                                        \n"
850                 "    }                                                                  \n"
851                 "    d = ($('hfield'+i).options[$('hfield'+i).selectedIndex].value);    \n"
852                 "    if (d == 'all') {                                                  \n"
853                 "      $('div_size'+i).style.display = 'none';                          \n"
854                 "      $('div_compare'+i).style.display = 'none';                       \n"
855                 "      $('div_nocompare'+i).style.display = 'block';                    \n"
856                 "    }                                                                  \n"
857                 "    else if (d == 'size') {                                            \n"
858                 "      $('div_size'+i).style.display = 'block';                         \n"
859                 "      $('div_compare'+i).style.display = 'none';                       \n"
860                 "      $('div_nocompare'+i).style.display = 'none';                     \n"
861                 "    }                                                                  \n"
862                 "    else {                                                             \n"
863                 "      $('div_size'+i).style.display = 'none';                          \n"
864                 "      $('div_compare'+i).style.display = 'block';                      \n"
865                 "      $('div_nocompare'+i).style.display = 'none';                     \n"
866                 "    }                                                                  \n"
867                 "    d = ($('action'+i).options[$('action'+i).selectedIndex].value);    \n"
868                 "    if (d == 'fileinto') {                                             \n"
869                 "      $('div_fileinto'+i).style.display = 'block';                     \n"
870                 "      $('div_redirect'+i).style.display = 'none';                      \n"
871                 "      $('div_automsg'+i).style.display = 'none';                       \n"
872                 "    } else if (d == 'redirect') {                                      \n"
873                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
874                 "      $('div_redirect'+i).style.display = 'block';                     \n"
875                 "      $('div_automsg'+i).style.display = 'none';                       \n"
876                 "    } else if ((d == 'reject') || (d == 'vacation'))  {                \n"
877                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
878                 "      $('div_redirect'+i).style.display = 'none';                      \n"
879                 "      $('div_automsg'+i).style.display = 'block';                      \n"
880                 "    } else {                                                           \n"
881                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
882                 "      $('div_redirect'+i).style.display = 'none';                      \n"
883                 "      $('div_automsg'+i).style.display = 'none';                       \n"
884                 "    }                                                                  \n"
885                 "    if (highest_active_rule < %d) {                                    \n", MAX_RULES-1 );
886         wc_printf("      $('div_addrule').style.display = 'block';                      \n"
887                 "    } else {                                                           \n"
888                 "      $('div_addrule').style.display = 'none';                         \n"
889                 "    }                                                                  \n"
890                 "  }                                                                    \n"
891                 "}                                                                      \n"
892 /*
893  * Add a rule (really, just un-hide it)
894  */
895                 "function AddRule() {                                                   \n"
896                 "  highest_active_rule = highest_active_rule + 1;                       \n"
897                 "  $('active'+highest_active_rule).checked = true;                      \n"
898                 "  UpdateRules();                                                       \n"
899                 "}                                                                      \n"
900 /*
901  * Swap two rules
902  */
903                 "function SwapRules(ra, rb) {                                           \n"
904                 "                                                                       \n"
905                 "  var things = new Array();                                            \n"
906                 "  things[0] = 'hfield';                                                \n"
907                 "  things[1] = 'compare';                                               \n"
908                 "  things[2] = 'htext';                                                 \n"
909                 "  things[3] = 'action';                                                \n"
910                 "  things[4] = 'fileinto';                                              \n"
911                 "  things[5] = 'redirect';                                              \n"
912                 "  things[6] = 'final';                                                 \n"
913                 "  things[7] = 'sizecomp';                                              \n"
914                 "  things[8] = 'sizeval';                                               \n"
915                 "  things[9] = 'automsg';                                               \n"
916                 "                                                                       \n"
917                 "  for (i=0; i<=9; ++i) {                                               \n"
918                 "    tempval=$(things[i]+ra).value;                                     \n"
919                 "    $(things[i]+ra).value = $(things[i]+rb).value;                     \n"
920                 "    $(things[i]+rb).value = tempval;                                   \n"
921                 "  }                                                                    \n"
922                 "}                                                                      \n"
923 /*
924  * Delete a rule (percolate the deleted rule out to the end, then deactivate it)
925  */
926                 "function DeleteRule(rd) {                                              \n"
927                 "  for (j=rd; j<=highest_active_rule; ++j) {                            \n"
928                 "    SwapRules(j, (j+1));                                               \n"
929                 "  }                                                                    \n"
930                 "  $('active'+highest_active_rule).checked = false;                     \n"
931                 "}                                                                      \n"
932                 "</script>                                                              \n"
933         );
934
935
936         wc_printf("<br />");
937
938         wc_printf("<table cellpadding=2 width=100%%>");
939
940         for (i=0; i<MAX_RULES; ++i) {
941
942                 /* Grab our existing values to populate */
943                 active = extract_int(rules[i], 0);
944                 extract_token(hfield, rules[i], 1, '|', sizeof hfield);
945                 extract_token(compare, rules[i], 2, '|', sizeof compare);
946                 extract_token(htext, rules[i], 3, '|', sizeof htext);
947                 extract_token(sizecomp, rules[i], 4, '|', sizeof sizecomp);
948                 sizeval = extract_int(rules[i], 5);
949                 extract_token(action, rules[i], 6, '|', sizeof action);
950                 extract_token(fileinto, rules[i], 7, '|', sizeof fileinto);
951                 extract_token(redirect, rules[i], 8, '|', sizeof redirect);
952                 extract_token(automsg, rules[i], 9, '|', sizeof automsg);
953                 extract_token(final, rules[i], 10, '|', sizeof final);
954                 
955                 /* now generate the table row */
956
957                 wc_printf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
958                         i,
959                         ((i%2) ? "DDDDDD" : "FFFFFF")
960                 );
961
962                 wc_printf("<td width=5%% align=\"center\">");
963
964                 wc_printf("<div style=\"display:none\">");
965                 wc_printf("<input type=\"checkbox\" name=\"active%d\" id=\"active%d\" %s>",
966                         i, i,
967                         (active ? "checked" : "")
968                 );
969                 wc_printf("</div>");
970
971                 if (i>0) wc_printf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
972                         "<img border=\"0\" src=\"static/up_pointer.gif\" "
973                         "title=\"%s\"/></a>",
974                         i-1, i, _("Move rule up") );
975
976                 wc_printf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
977                         "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" "
978                         "title=\"%s\"/></a>",
979                         i, i+1, i, _("Move rule down") );
980
981                 wc_printf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
982                         "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" "
983                         "title=\"%s\"/></a>",
984                         i, i, _("Delete rule") );
985
986                 wc_printf("</td>");
987
988                 wc_printf("<td width=5%% align=\"center\">");
989                 wc_printf("<font size=+2>%d</font>", i+1);
990                 wc_printf("</td>");
991
992                 wc_printf("<td width=20%%>%s ", _("If") );
993
994                 char *hfield_values[15][2] = {
995                         {       "from",         _("From")               },
996                         {       "tocc",         _("To or Cc")           },
997                         {       "subject",      _("Subject")            },
998                         {       "replyto",      _("Reply-to")           },
999                         {       "sender",       _("Sender")             },
1000                         {       "resentfrom",   _("Resent-From")        },
1001                         {       "resentto",     _("Resent-To")          },
1002                         {       "envfrom",      _("Envelope From")      },
1003                         {       "envto",        _("Envelope To")        },
1004                         {       "xmailer",      _("X-Mailer")           },
1005                         {       "xspamflag",    _("X-Spam-Flag")        },
1006                         {       "xspamstatus",  _("X-Spam-Status")      },
1007                         {       "listid",       _("List-ID")            },
1008                         {       "size",         _("Message size")       },
1009                         {       "all",          _("All")                }
1010                 };
1011
1012                 wc_printf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
1013                         i, i);
1014                 for (j=0; j<15; ++j) {
1015                         wc_printf("<option %s value=\"%s\">%s</option>",
1016                                 ( (!strcasecmp(hfield, hfield_values[j][0])) ? "selected" : ""),
1017                                 hfield_values[j][0],
1018                                 hfield_values[j][1]
1019                         );
1020                 }
1021
1022                 wc_printf("</select>");
1023                 wc_printf("</td>");
1024
1025                 wc_printf("<td width=20%%>");
1026
1027                 char *compare_values[6][2] = {
1028                         {       "contains",     _("contains")           },
1029                         {       "notcontains",  _("does not contain")   },
1030                         {       "is",           _("is")                 },
1031                         {       "isnot",        _("is not")             },
1032                         {       "matches",      _("matches")            },
1033                         {       "notmatches",   _("does not match")     }
1034                 };
1035
1036                 wc_printf("<div id=\"div_compare%d\">", i);
1037                 wc_printf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
1038                         i, i);
1039                 for (j=0; j<6; ++j) {
1040                         wc_printf("<option %s value=\"%s\">%s</option>",
1041                                 ( (!strcasecmp(compare, compare_values[j][0])) ? "selected" : ""),
1042                                 compare_values[j][0],
1043                                 compare_values[j][1]
1044                         );
1045                 }
1046                 wc_printf("</select>");
1047
1048                 wc_printf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\" value=\"", i, i);
1049                 escputs(htext);
1050                 wc_printf("\"></div>");
1051
1052                 wc_printf("<div id=\"div_nocompare%d\">", i);
1053                 wc_printf("%s", _("(All messages)"));
1054                 wc_printf("</div>");
1055
1056                 char *sizecomp_values[2][2] = {
1057                         {       "larger",       _("is larger than")     },
1058                         {       "smaller",      _("is smaller than")    }
1059                 };
1060
1061                 wc_printf("<div id=\"div_size%d\">", i);
1062                 wc_printf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
1063                         i, i);
1064                 for (j=0; j<2; ++j) {
1065                         wc_printf("<option %s value=\"%s\">%s</option>",
1066                                 ( (!strcasecmp(sizecomp, sizecomp_values[j][0])) ? "selected" : ""),
1067                                 sizecomp_values[j][0],
1068                                 sizecomp_values[j][1]
1069                         );
1070                 }
1071                 wc_printf("</select>");
1072
1073                 wc_printf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\" value=\"%d\">",
1074                         i, i, sizeval);
1075                 wc_printf("bytes");
1076                 wc_printf("</div>");
1077
1078                 wc_printf("</td>");
1079
1080                 char *action_values[6][2] = {
1081                         {       "keep",         _("Keep")               },
1082                         {       "discard",      _("Discard silently")   },
1083                         {       "reject",       _("Reject")             },
1084                         {       "fileinto",     _("Move message to")    },
1085                         {       "redirect",     _("Forward to")         },
1086                         {       "vacation",     _("Vacation")           }
1087                 };
1088
1089                 wc_printf("<td width=20%%>");
1090                 wc_printf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
1091                         i, i);
1092                 for (j=0; j<6; ++j) {
1093                         wc_printf("<option %s value=\"%s\">%s</option>",
1094                                 ( (!strcasecmp(action, action_values[j][0])) ? "selected" : ""),
1095                                 action_values[j][0],
1096                                 action_values[j][1]
1097                         );
1098                 }
1099                 wc_printf("</select>");
1100
1101                 wc_printf("<div id=\"div_fileinto%d\">", i);
1102                 wc_printf("<select name=\"fileinto%d\" id=\"fileinto%d\">", i, i);
1103                 for (j=0; j<num_roomnames; ++j) {
1104                         wc_printf("<option ");
1105                         if (!strcasecmp(rooms[j].name, fileinto)) {
1106                                 wc_printf("selected ");
1107                         }
1108                         wc_printf("value=\"");
1109                         escputs(rooms[j].name);
1110                         wc_printf("\">");
1111                         escputs(rooms[j].name);
1112                         wc_printf("</option>\n");
1113                 }
1114                 wc_printf("</select>\n");
1115                 wc_printf("</div>");
1116
1117                 wc_printf("<div id=\"div_redirect%d\">", i);
1118                 wc_printf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\" value=\"", i, i);
1119                 escputs(redirect);
1120                 wc_printf("\"></div>");
1121
1122                 wc_printf("<div id=\"div_automsg%d\">", i);
1123                 wc_printf(_("Message:"));
1124                 wc_printf("<br />");
1125                 wc_printf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
1126                 escputs(automsg);
1127                 wc_printf("</textarea>");
1128                 wc_printf("</div>");
1129
1130                 wc_printf("</td>");
1131
1132                 char *final_values[2][2] = {
1133                         {       "continue",     _("continue processing")        },
1134                         {       "stop",         _("stop")                       }
1135                 };
1136
1137                 wc_printf("<td width=10%% align=\"center\">%s</td>", _("and then") );
1138
1139                 wc_printf("<td width=20%%>");
1140                 wc_printf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
1141                         i, i);
1142                 for (j=0; j<2; ++j) {
1143                         wc_printf("<option %s value=\"%s\">%s</option>",
1144                                 ( (!strcasecmp(final, final_values[j][0])) ? "selected" : ""),
1145                                 final_values[j][0],
1146                                 final_values[j][1]
1147                         );
1148                 }
1149                 wc_printf("</select>");
1150                 wc_printf("</td>");
1151
1152                 wc_printf("</tr>\n");
1153
1154         }
1155
1156         wc_printf("</table>");
1157         wc_printf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">%s</a><br /></div>\n",
1158                 _("Add rule")
1159         );
1160
1161         wc_printf("<script type=\"text/javascript\">                                    \n");
1162         wc_printf("UpdateRules();                                                               \n");
1163         wc_printf("</script>                                                            \n");
1164
1165         free(rooms);
1166 }
1167
1168 void _display_add_remove_scripts(void) {display_add_remove_scripts(NULL);}
1169
1170 void 
1171 InitModule_SIEVE
1172 (void)
1173 {
1174         WebcitAddUrlHandler(HKEY("display_sieve"), "", 0, display_sieve, 0);
1175         WebcitAddUrlHandler(HKEY("save_sieve"), "", 0, save_sieve, 0);
1176         WebcitAddUrlHandler(HKEY("display_add_remove_scripts"), "", 0, _display_add_remove_scripts, 0);
1177         WebcitAddUrlHandler(HKEY("create_script"), "", 0, create_script, 0);
1178         WebcitAddUrlHandler(HKEY("delete_script"), "", 0, delete_script, 0);
1179 }