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