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