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