* move serv_info into the session, here we can control its de/allocation the right...
[citadel.git] / webcit / sieve.c
1 /* 
2  * $Id$
3  */
4
5 #include "webcit.h"
6
7 #define MAX_SCRIPTS     100
8 #define MAX_RULES       25
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         wprintf("<div id=\"banner\">\n");
20         wprintf("<img src=\"static/advanpage2_48x.gif\">");
21         wprintf("<h1>");
22         wprintf(_("View/edit server-side mail filters"));
23         wprintf("</h1>\n");
24         wprintf("</div>\n");
25
26         wprintf("<div id=\"content\" class=\"service\">\n");
27
28         wprintf("<div class=\"fix_scrollbar_bug\">"
29                 "<table class=\"sieve_background\">"
30                 "<tr><td valign=top>\n");
31
32         wprintf(_("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         wprintf("</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[256];
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         wprintf("<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         wprintf("<div id=\"banner\">\n");
105         wprintf("<img src=\"static/advanpage2_48x.gif\">");
106         wprintf("<h1>");
107         wprintf(_("View/edit server-side mail filters"));
108         wprintf("</h1>\n");
109         wprintf("</div>\n");
110
111         wprintf("<div id=\"content\" class=\"service\">\n");
112
113         wprintf("<div class=\"fix_scrollbar_bug\">"
114                 "<table class=\"sieve_background\">"
115                 "<tr><td valign=top>\n");
116
117
118         wprintf("<form id=\"sieveform\" method=\"post\" action=\"save_sieve\">\n");
119         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
120
121         wprintf(_("When new mail arrives: "));
122         wprintf("<select name=\"bigaction\" size=1 onChange=\"ToggleSievePanels();\">\n");
123
124         wprintf("<option %s value=\"0\">", ((active_script < 0) ? "selected" : ""));
125         wprintf(_("Leave it in my inbox without filtering"));
126         wprintf("</option>\n");
127
128         wprintf("<option %s value=\"1\">", ((rules_script_is_active) ? "selected" : ""));
129         wprintf(_("Filter it according to rules selected below"));
130         wprintf("</option>\n");
131
132         wprintf("<option %s value=\"2\">",
133                         (((active_script >= 0) && (!rules_script_is_active)) ? "selected" : ""));
134         wprintf(_("Filter it through a manually edited script (advanced users only)"));
135         wprintf("</option>\n");
136
137         wprintf("</select>");
138
139
140
141         /* The "no filtering" div */
142
143         wprintf("<div id=\"sievediv0\" style=\"display:none\">\n");
144         wprintf("<div align=\"center\"><br /><br />");
145         wprintf(_("Your incoming mail will not be filtered through any scripts."));
146         wprintf("<br /><br /></div>\n");
147         wprintf("</div>\n");
148
149         /* The "webcit managed scripts" div */
150
151         wprintf("<div id=\"sievediv1\" style=\"display:none\">\n");
152         display_rules_editor_inner_div();
153         wprintf("</div>\n");
154
155         /* The "I'm smart and can write my own Sieve scripts" div */
156
157         wprintf("<div id=\"sievediv2\" style=\"display:none\">\n");
158
159         if (num_scripts > 0) {
160                 wprintf(_("The currently active script is: "));
161                 wprintf("<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                                 wprintf("<option %s value=\"%s\">%s</option>\n",
165                                         ((active_script == i) ? "selected" : ""),
166                                         script_names[i],
167                                         script_names[i]
168                                 );
169                         }
170                 }
171                 wprintf("</select>\n");
172         }
173
174         wprintf("&nbsp;&nbsp;&nbsp;");
175         wprintf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add or delete scripts"));
176
177         wprintf("<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                                 wprintf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
183                                 wprintf("<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                                         wprintf("%s\n", buf);
189                                 }
190                                 wprintf("</textarea>\n");
191                                 wprintf("</div>\n");
192                         }
193                 }
194         }
195
196         wprintf("<script type=\"text/javascript\">      \n"
197                 "ToggleScriptPanels();                  \n"
198                 "</script>                              \n"
199         );
200
201         wprintf("</div>\n");
202
203
204         /* The rest of this is common for all panels... */
205
206         wprintf("<div align=\"center\"><br>");
207         wprintf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
208         wprintf("&nbsp;");
209         wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
210         wprintf("</div></form>\n");
211
212         wprintf("</td></tr></table></div>\n");
213
214         wprintf("<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_printf("%s", bstr(this_name));
625                                         serv_puts("000");
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         wprintf("<div id=\"banner\">\n");
647         wprintf("<img src=\"static/advanpage2_48x.gif\">");
648         wprintf(_("Add or delete scripts"));
649         wprintf("</h1>\n");
650         wprintf("</div>\n");
651         
652         wprintf("<div id=\"content\" class=\"service\">\n");
653
654         if (message != NULL) wprintf(message);
655
656         wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
657
658         svput("BOXTITLE", WCS_STRING, _("Add a new script"));
659         do_template("beginboxx", NULL);
660
661         wprintf(_("To create a new script, enter the desired "
662                 "script name in the box below and click 'Create'."));
663         wprintf("<br /><br />");
664
665         wprintf("<center><form method=\"POST\" action=\"create_script\">\n");
666         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
667         wprintf(_("Script name: "));
668         wprintf("<input type=\"text\" name=\"script_name\"><br />\n"
669                 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
670                 "</form></center>\n", _("Create"));
671
672         do_template("endbox", NULL);
673
674         svput("BOXTITLE", WCS_STRING, _("Edit scripts"));
675         do_template("beginboxx", NULL);
676         wprintf("<br /><div align=center><a href=\"display_sieve\">%s</a><br /><br />\n",
677                 _("Return to the script editing screen")
678         );
679         do_template("endbox", NULL);
680
681         wprintf("</td><td>");
682
683         svput("BOXTITLE", WCS_STRING, _("Delete scripts"));
684         do_template("beginboxx", NULL);
685
686         wprintf(_("To delete an existing script, select the script "
687                 "name from the list and click 'Delete'."));
688         wprintf("<br /><br />");
689         
690         wprintf("<center>"
691                 "<form method=\"POST\" action=\"delete_script\">\n");
692         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
693         wprintf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
694
695         serv_puts("MSIV listscripts");
696         serv_getln(buf, sizeof buf);
697         if (buf[0] == '1') {
698                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
699                         extract_token(script_name, buf, 0, '|', sizeof script_name);
700                         if ( (extract_int(buf, 1) == 0) && (strcasecmp(script_name, RULES_SCRIPT)) ) {
701                                 wprintf("<option>");
702                                 escputs(script_name);
703                                 wprintf("</option>\n");
704                         }
705                 }
706         }
707         wprintf("</select><br />\n");
708
709         wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
710                 "onClick=\"return confirm('%s');\">", _("Delete script"), _("Delete this script?"));
711         wprintf("</form></center>\n");
712         do_template("endbox", NULL);
713
714         wprintf("</td></tr></table>\n");
715
716         wDumpContent(1);
717 }
718
719
720
721 /*
722  * delete a script
723  */
724 void delete_script(void) {
725         char buf[256];
726
727         serv_printf("MSIV deletescript|%s", bstr("script_name"));
728         serv_getln(buf, sizeof buf);
729         display_add_remove_scripts(&buf[4]);
730 }
731                 
732
733
734 /*
735  * create a new script
736  * take the web environment script name and create it on the citadel server
737  */
738 void create_script(void) {
739         char buf[256];
740
741         serv_printf("MSIV getscript|%s", bstr("script_name"));
742         serv_getln(buf, sizeof buf);
743         if (buf[0] == '1') {
744                 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
745                         /* flush */
746                 }
747                 display_add_remove_scripts(_("A script by that name already exists."));
748                 return;
749         }
750         
751         serv_printf("MSIV putscript|%s", bstr("script_name"));
752         serv_getln(buf, sizeof buf);
753         if (buf[0] == '4') {
754                 serv_puts("keep;");
755                 serv_puts("000");
756                 display_add_remove_scripts(_("A new script has been created.  Return to the script editing screen to edit and activate it."));
757                 return;
758         }
759
760         display_add_remove_scripts(&buf[4]);
761 }
762
763
764
765
766 void display_rules_editor_inner_div(void) {
767         int i, j;
768         char buf[4096];
769         char rules[MAX_RULES][2048];
770
771         struct {
772                 char name[128];
773         } *rooms = NULL;
774         int num_roomnames = 0;
775         int num_roomnames_alloc = 0;
776
777         int active;
778         char hfield[256];
779         char compare[32];
780         char htext[256];
781         char sizecomp[32];
782         int sizeval;
783         char action[32];
784         char fileinto[128];
785         char redirect[256];
786         char automsg[1024];
787         char final[32];
788
789         /* load the rules */
790         memset(rules, 0, sizeof rules);
791         serv_printf("MSIV getscript|%s", RULES_SCRIPT);
792         serv_getln(buf, sizeof buf);
793         if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
794                 if (!strncasecmp(buf, "# WEBCIT_RULE|", 14)) {
795                         j = extract_int(buf, 1);
796                         remove_token(buf, 0, '|');
797                         remove_token(buf, 0, '|');
798                         CtdlDecodeBase64(rules[j], buf, strlen(buf));
799                 }
800         }
801
802         /* load the roomnames */
803         serv_puts("LKRA");
804         serv_getln(buf, sizeof buf);
805         if (buf[0] == '1') {
806                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
807                         ++num_roomnames;
808                         if (num_roomnames > num_roomnames_alloc) {
809                                 num_roomnames_alloc += 250;
810                                 rooms = realloc(rooms, (num_roomnames_alloc * 128));
811                         }
812                         extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
813                 }
814         }
815
816
817 /*
818  * This script should get called by every onChange event...
819  *
820  */
821         wprintf("<script type=\"text/javascript\">                                      \n"
822                 "                                                                       \n"
823                 "var highest_active_rule = (-1);                                        \n"
824                 "                                                                       \n"
825                 "function UpdateRules() {                                               \n");
826 /*
827  * Show only the active rows...
828  */
829         wprintf("  highest_active_rule = (-1);                                          \n");
830         wprintf("  for (i=0; i<%d; ++i) {                                               \n", MAX_RULES);
831         wprintf("   if ($('active'+i).checked) {                                        \n"
832                 "     $('rule' + i).style.display = 'block';                            \n"
833                 "     highest_active_rule = i;                                          \n"
834                 "   }                                                                   \n"
835                 "   else {                                                              \n"
836                 "     $('rule' + i).style.display = 'none';                             \n"
837                 "   }                                                                   \n"
838                 "  }                                                                    \n");
839 /*
840  * Show only the fields relevant to the rules...
841  */
842         wprintf("  for (i=0; i<=highest_active_rule; ++i) {                             \n"
843                 "    d = ($('movedown'+i));                                             \n"
844                 "    if (i < highest_active_rule) {                                     \n"
845                 "      d.style.display = 'block';                                       \n"
846                 "    }                                                                  \n"
847                 "    else {                                                             \n"
848                 "      d.style.display = 'none';                                        \n"
849                 "    }                                                                  \n"
850                 "    d = ($('hfield'+i).options[$('hfield'+i).selectedIndex].value);    \n"
851                 "    if (d == 'all') {                                                  \n"
852                 "      $('div_size'+i).style.display = 'none';                          \n"
853                 "      $('div_compare'+i).style.display = 'none';                       \n"
854                 "      $('div_nocompare'+i).style.display = 'block';                    \n"
855                 "    }                                                                  \n"
856                 "    else if (d == 'size') {                                            \n"
857                 "      $('div_size'+i).style.display = 'block';                         \n"
858                 "      $('div_compare'+i).style.display = 'none';                       \n"
859                 "      $('div_nocompare'+i).style.display = 'none';                     \n"
860                 "    }                                                                  \n"
861                 "    else {                                                             \n"
862                 "      $('div_size'+i).style.display = 'none';                          \n"
863                 "      $('div_compare'+i).style.display = 'block';                      \n"
864                 "      $('div_nocompare'+i).style.display = 'none';                     \n"
865                 "    }                                                                  \n"
866                 "    d = ($('action'+i).options[$('action'+i).selectedIndex].value);    \n"
867                 "    if (d == 'fileinto') {                                             \n"
868                 "      $('div_fileinto'+i).style.display = 'block';                     \n"
869                 "      $('div_redirect'+i).style.display = 'none';                      \n"
870                 "      $('div_automsg'+i).style.display = 'none';                       \n"
871                 "    } else if (d == 'redirect') {                                      \n"
872                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
873                 "      $('div_redirect'+i).style.display = 'block';                     \n"
874                 "      $('div_automsg'+i).style.display = 'none';                       \n"
875                 "    } else if ((d == 'reject') || (d == 'vacation'))  {                \n"
876                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
877                 "      $('div_redirect'+i).style.display = 'none';                      \n"
878                 "      $('div_automsg'+i).style.display = 'block';                      \n"
879                 "    } else {                                                           \n"
880                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
881                 "      $('div_redirect'+i).style.display = 'none';                      \n"
882                 "      $('div_automsg'+i).style.display = 'none';                       \n"
883                 "    }                                                                  \n"
884                 "    if (highest_active_rule < %d) {                                    \n", MAX_RULES-1 );
885         wprintf("      $('div_addrule').style.display = 'block';                        \n"
886                 "    } else {                                                           \n"
887                 "      $('div_addrule').style.display = 'none';                         \n"
888                 "    }                                                                  \n"
889                 "  }                                                                    \n"
890                 "}                                                                      \n"
891 /*
892  * Add a rule (really, just un-hide it)
893  */
894                 "function AddRule() {                                                   \n"
895                 "  highest_active_rule = highest_active_rule + 1;                       \n"
896                 "  $('active'+highest_active_rule).checked = true;                      \n"
897                 "  UpdateRules();                                                       \n"
898                 "}                                                                      \n"
899 /*
900  * Swap two rules
901  */
902                 "function SwapRules(ra, rb) {                                           \n"
903                 "                                                                       \n"
904                 "  var things = new Array();                                            \n"
905                 "  things[0] = 'hfield';                                                \n"
906                 "  things[1] = 'compare';                                               \n"
907                 "  things[2] = 'htext';                                                 \n"
908                 "  things[3] = 'action';                                                \n"
909                 "  things[4] = 'fileinto';                                              \n"
910                 "  things[5] = 'redirect';                                              \n"
911                 "  things[6] = 'final';                                                 \n"
912                 "  things[7] = 'sizecomp';                                              \n"
913                 "  things[8] = 'sizeval';                                               \n"
914                 "  things[9] = 'automsg';                                               \n"
915                 "                                                                       \n"
916                 "  for (i=0; i<=9; ++i) {                                               \n"
917                 "    tempval=$(things[i]+ra).value;                                     \n"
918                 "    $(things[i]+ra).value = $(things[i]+rb).value;                     \n"
919                 "    $(things[i]+rb).value = tempval;                                   \n"
920                 "  }                                                                    \n"
921                 "}                                                                      \n"
922 /*
923  * Delete a rule (percolate the deleted rule out to the end, then deactivate it)
924  */
925                 "function DeleteRule(rd) {                                              \n"
926                 "  for (j=rd; j<=highest_active_rule; ++j) {                            \n"
927                 "    SwapRules(j, (j+1));                                               \n"
928                 "  }                                                                    \n"
929                 "  $('active'+highest_active_rule).checked = false;                     \n"
930                 "}                                                                      \n"
931                 "</script>                                                              \n"
932         );
933
934
935         wprintf("<br />");
936
937         wprintf("<table cellpadding=2 width=100%%>");
938
939         for (i=0; i<MAX_RULES; ++i) {
940
941                 /* Grab our existing values to populate */
942                 active = extract_int(rules[i], 0);
943                 extract_token(hfield, rules[i], 1, '|', sizeof hfield);
944                 extract_token(compare, rules[i], 2, '|', sizeof compare);
945                 extract_token(htext, rules[i], 3, '|', sizeof htext);
946                 extract_token(sizecomp, rules[i], 4, '|', sizeof sizecomp);
947                 sizeval = extract_int(rules[i], 5);
948                 extract_token(action, rules[i], 6, '|', sizeof action);
949                 extract_token(fileinto, rules[i], 7, '|', sizeof fileinto);
950                 extract_token(redirect, rules[i], 8, '|', sizeof redirect);
951                 extract_token(automsg, rules[i], 9, '|', sizeof automsg);
952                 extract_token(final, rules[i], 10, '|', sizeof final);
953                 
954                 /* now generate the table row */
955
956                 wprintf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
957                         i,
958                         ((i%2) ? "DDDDDD" : "FFFFFF")
959                 );
960
961                 wprintf("<td width=5%% align=\"center\">");
962
963                 wprintf("<div style=\"display:none\">");
964                 wprintf("<input type=\"checkbox\" name=\"active%d\" id=\"active%d\" %s>",
965                         i, i,
966                         (active ? "checked" : "")
967                 );
968                 wprintf("</div>");
969
970                 if (i>0) wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
971                         "<img border=\"0\" src=\"static/up_pointer.gif\" "
972                         "title=\"%s\"/></a>",
973                         i-1, i, _("Move rule up") );
974
975                 wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
976                         "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" "
977                         "title=\"%s\"/></a>",
978                         i, i+1, i, _("Move rule down") );
979
980                 wprintf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
981                         "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" "
982                         "title=\"%s\"/></a>",
983                         i, i, _("Delete rule") );
984
985                 wprintf("</td>");
986
987                 wprintf("<td width=5%% align=\"center\">");
988                 wprintf("<font size=+2>%d</font>", i+1);
989                 wprintf("</td>");
990
991                 wprintf("<td width=20%%>%s ", _("If") );
992
993                 char *hfield_values[15][2] = {
994                         {       "from",         _("From")               },
995                         {       "tocc",         _("To or Cc")           },
996                         {       "subject",      _("Subject")            },
997                         {       "replyto",      _("Reply-to")           },
998                         {       "sender",       _("Sender")             },
999                         {       "resentfrom",   _("Resent-From")        },
1000                         {       "resentto",     _("Resent-To")          },
1001                         {       "envfrom",      _("Envelope From")      },
1002                         {       "envto",        _("Envelope To")        },
1003                         {       "xmailer",      _("X-Mailer")           },
1004                         {       "xspamflag",    _("X-Spam-Flag")        },
1005                         {       "xspamstatus",  _("X-Spam-Status")      },
1006                         {       "listid",       _("List-ID")            },
1007                         {       "size",         _("Message size")       },
1008                         {       "all",          _("All")                }
1009                 };
1010
1011                 wprintf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
1012                         i, i);
1013                 for (j=0; j<15; ++j) {
1014                         wprintf("<option %s value=\"%s\">%s</option>",
1015                                 ( (!strcasecmp(hfield, hfield_values[j][0])) ? "selected" : ""),
1016                                 hfield_values[j][0],
1017                                 hfield_values[j][1]
1018                         );
1019                 }
1020
1021                 wprintf("</select>");
1022                 wprintf("</td>");
1023
1024                 wprintf("<td width=20%%>");
1025
1026                 char *compare_values[6][2] = {
1027                         {       "contains",     _("contains")           },
1028                         {       "notcontains",  _("does not contain")   },
1029                         {       "is",           _("is")                 },
1030                         {       "isnot",        _("is not")             },
1031                         {       "matches",      _("matches")            },
1032                         {       "notmatches",   _("does not match")     }
1033                 };
1034
1035                 wprintf("<div id=\"div_compare%d\">", i);
1036                 wprintf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
1037                         i, i);
1038                 for (j=0; j<6; ++j) {
1039                         wprintf("<option %s value=\"%s\">%s</option>",
1040                                 ( (!strcasecmp(compare, compare_values[j][0])) ? "selected" : ""),
1041                                 compare_values[j][0],
1042                                 compare_values[j][1]
1043                         );
1044                 }
1045                 wprintf("</select>");
1046
1047                 wprintf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\" value=\"", i, i);
1048                 escputs(htext);
1049                 wprintf("\"></div>");
1050
1051                 wprintf("<div id=\"div_nocompare%d\">", i);
1052                 wprintf("%s", _("(All messages)"));
1053                 wprintf("</div>");
1054
1055                 char *sizecomp_values[2][2] = {
1056                         {       "larger",       _("is larger than")     },
1057                         {       "smaller",      _("is smaller than")    }
1058                 };
1059
1060                 wprintf("<div id=\"div_size%d\">", i);
1061                 wprintf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
1062                         i, i);
1063                 for (j=0; j<2; ++j) {
1064                         wprintf("<option %s value=\"%s\">%s</option>",
1065                                 ( (!strcasecmp(sizecomp, sizecomp_values[j][0])) ? "selected" : ""),
1066                                 sizecomp_values[j][0],
1067                                 sizecomp_values[j][1]
1068                         );
1069                 }
1070                 wprintf("</select>");
1071
1072                 wprintf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\" value=\"%d\">",
1073                         i, i, sizeval);
1074                 wprintf("bytes");
1075                 wprintf("</div>");
1076
1077                 wprintf("</td>");
1078
1079                 char *action_values[6][2] = {
1080                         {       "keep",         _("Keep")               },
1081                         {       "discard",      _("Discard silently")   },
1082                         {       "reject",       _("Reject")             },
1083                         {       "fileinto",     _("Move message to")    },
1084                         {       "redirect",     _("Forward to")         },
1085                         {       "vacation",     _("Vacation")           }
1086                 };
1087
1088                 wprintf("<td width=20%%>");
1089                 wprintf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
1090                         i, i);
1091                 for (j=0; j<6; ++j) {
1092                         wprintf("<option %s value=\"%s\">%s</option>",
1093                                 ( (!strcasecmp(action, action_values[j][0])) ? "selected" : ""),
1094                                 action_values[j][0],
1095                                 action_values[j][1]
1096                         );
1097                 }
1098                 wprintf("</select>");
1099
1100                 wprintf("<div id=\"div_fileinto%d\">", i);
1101                 wprintf("<select name=\"fileinto%d\" id=\"fileinto%d\">", i, i);
1102                 for (j=0; j<num_roomnames; ++j) {
1103                         wprintf("<option ");
1104                         if (!strcasecmp(rooms[j].name, fileinto)) {
1105                                 wprintf("selected ");
1106                         }
1107                         wprintf("value=\"");
1108                         escputs(rooms[j].name);
1109                         wprintf("\">");
1110                         escputs(rooms[j].name);
1111                         wprintf("</option>\n");
1112                 }
1113                 wprintf("</select>\n");
1114                 wprintf("</div>");
1115
1116                 wprintf("<div id=\"div_redirect%d\">", i);
1117                 wprintf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\" value=\"", i, i);
1118                 escputs(redirect);
1119                 wprintf("\"></div>");
1120
1121                 wprintf("<div id=\"div_automsg%d\">", i);
1122                 wprintf(_("Message:"));
1123                 wprintf("<br />");
1124                 wprintf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
1125                 escputs(automsg);
1126                 wprintf("</textarea>");
1127                 wprintf("</div>");
1128
1129                 wprintf("</td>");
1130
1131                 char *final_values[2][2] = {
1132                         {       "continue",     _("continue processing")        },
1133                         {       "stop",         _("stop")                       }
1134                 };
1135
1136                 wprintf("<td width=10%% align=\"center\">%s</td>", _("and then") );
1137
1138                 wprintf("<td width=20%%>");
1139                 wprintf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
1140                         i, i);
1141                 for (j=0; j<2; ++j) {
1142                         wprintf("<option %s value=\"%s\">%s</option>",
1143                                 ( (!strcasecmp(final, final_values[j][0])) ? "selected" : ""),
1144                                 final_values[j][0],
1145                                 final_values[j][1]
1146                         );
1147                 }
1148                 wprintf("</select>");
1149                 wprintf("</td>");
1150
1151                 wprintf("</tr>\n");
1152
1153         }
1154
1155         wprintf("</table>");
1156         wprintf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">%s</a><br /></div>\n",
1157                 _("Add rule")
1158         );
1159
1160         wprintf("<script type=\"text/javascript\">                                      \n");
1161         wprintf("UpdateRules();                                                         \n");
1162         wprintf("</script>                                                              \n");
1163
1164         free(rooms);
1165 }
1166
1167 void _display_add_remove_scripts(void) {display_add_remove_scripts(NULL);}
1168
1169 void 
1170 InitModule_SIEVE
1171 (void)
1172 {
1173         WebcitAddUrlHandler(HKEY("display_sieve"), display_sieve, 0);
1174         WebcitAddUrlHandler(HKEY("save_sieve"), save_sieve, 0);
1175         WebcitAddUrlHandler(HKEY("display_add_remove_scripts"), _display_add_remove_scripts, 0);
1176         WebcitAddUrlHandler(HKEY("create_script"), create_script, 0);
1177         WebcitAddUrlHandler(HKEY("delete_script"), delete_script, 0);
1178 }