use the same way to display all banners and services contents
[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[4096];
429         char my_addresses[4096];
430         
431         /* Enumerate my email addresses in case they are needed for a vacation rule */
432         my_addresses[0] = 0;
433         serv_puts("GVEA");
434         serv_getln(buf, sizeof buf);
435         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
436                 if (!IsEmptyStr(my_addresses)) {
437                         strcat(my_addresses, ",\n");
438                 }
439                 strcat(my_addresses, "\"");
440                 strcat(my_addresses, buf);
441                 strcat(my_addresses, "\"");
442         }
443
444         /* Now generate the script and write it to the Citadel server */
445         serv_printf("MSIV putscript|%s|", RULES_SCRIPT);
446         serv_getln(buf, sizeof buf);
447         if (buf[0] != '4') {
448                 return;
449         }
450
451         serv_puts("# THIS SCRIPT WAS AUTOMATICALLY GENERATED BY WEBCIT.");
452         serv_puts("# ");
453         serv_puts("# Do not attempt to manually edit it.  If you do so,");
454         serv_puts("# your changes will be overwritten the next time WebCit");
455         serv_puts("# saves its mail filtering rule set.  If you really want");
456         serv_puts("# to use these rules as the basis for another script,");
457         serv_puts("# copy them to another script and save that instead.");
458         serv_puts("");
459         serv_puts("require \"fileinto\";");
460         serv_puts("require \"reject\";");
461         serv_puts("require \"vacation\";");
462         serv_puts("require \"envelope\";");
463         serv_puts("");
464
465         for (i=0; i<MAX_RULES; ++i) {
466                 
467                 strcpy(rule, "");
468
469                 sprintf(fname, "active%d", i);
470                 active = !strcasecmp(bstr(fname), "on") ;
471
472                 if (active) {
473
474                         sprintf(fname, "hfield%d", i);
475                         safestrncpy(hfield, bstr(fname), sizeof hfield);
476         
477                         sprintf(fname, "compare%d", i);
478                         safestrncpy(compare, bstr(fname), sizeof compare);
479         
480                         sprintf(fname, "htext%d", i);
481                         safestrncpy(htext, bstr(fname), sizeof htext);
482         
483                         sprintf(fname, "sizecomp%d", i);
484                         safestrncpy(sizecomp, bstr(fname), sizeof sizecomp);
485         
486                         sprintf(fname, "sizeval%d", i);
487                         sizeval = atoi(bstr(fname));
488         
489                         sprintf(fname, "action%d", i);
490                         safestrncpy(action, bstr(fname), sizeof action);
491         
492                         sprintf(fname, "fileinto%d", i);
493                         safestrncpy(fileinto, bstr(fname), sizeof fileinto);
494         
495                         sprintf(fname, "redirect%d", i);
496                         safestrncpy(redirect, bstr(fname), sizeof redirect);
497         
498                         sprintf(fname, "automsg%d", i);
499                         safestrncpy(automsg, bstr(fname), sizeof automsg);
500         
501                         sprintf(fname, "final%d", i);
502                         safestrncpy(final, bstr(fname), sizeof final);
503         
504                         snprintf(rule, sizeof rule, "%d|%s|%s|%s|%s|%d|%s|%s|%s|%s|%s",
505                                 active, hfield, compare, htext, sizecomp, sizeval, action, fileinto,
506                                 redirect, automsg, final
507                         );
508         
509                         CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1, 0);
510                         serv_printf("# WEBCIT_RULE|%d|%s|", i, encoded_rule);
511                         output_sieve_rule(hfield, compare, htext, sizecomp, sizeval,
512                                         action, fileinto, redirect, automsg, final, my_addresses);
513                         serv_printf("");
514                 }
515
516
517         }
518
519         serv_puts("stop;");
520         serv_puts("000");
521
522 }
523
524
525
526 /**
527  * \brief save sieve config
528  */
529 void save_sieve(void) {
530         int bigaction;
531         char script_names[MAX_SCRIPTS][64];
532         int num_scripts = 0;
533         int active_script = (-1);
534         int i;
535         char this_name[64];
536         char buf[256];
537
538         if (IsEmptyStr(bstr("save_button"))) {
539                 strcpy(WC->ImportantMessage,
540                         _("Cancelled.  Changes were not saved."));
541                 display_main_menu();
542                 return;
543         }
544
545         parse_fields_from_rule_editor();
546
547         serv_puts("MSIV listscripts");
548         serv_getln(buf, sizeof(buf));
549         if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
550                 if (num_scripts < MAX_SCRIPTS) {
551                         extract_token(script_names[num_scripts], buf, 0, '|', 64);
552                         if (extract_int(buf, 1) > 0) {
553                                 active_script = num_scripts;
554                         }
555                         ++num_scripts;
556                 }
557         }
558
559         bigaction = atoi(bstr("bigaction"));
560
561         if (bigaction == 0) {
562                 serv_puts("MSIV setactive||");
563                 serv_getln(buf, sizeof buf);
564         }
565
566         else if (bigaction == 1) {
567                 serv_printf("MSIV setactive|%s|", RULES_SCRIPT);
568                 serv_getln(buf, sizeof buf);
569         }
570
571         else if (bigaction == 2) {
572                 serv_printf("MSIV setactive|%s|", bstr("active_script"));
573                 serv_getln(buf, sizeof buf);
574         }
575
576         if (num_scripts > 0) {
577                 for (i=0; i<num_scripts; ++i) {
578                         /*
579                          * We only want to save the scripts from the "manually edited scripts"
580                          * screen.  The script that WebCit generates from its ruleset will be
581                          * auto-generated by parse_fields_from_rule_editor() and saved there.
582                          */
583                         if (strcasecmp(script_names[i], RULES_SCRIPT)) {
584                                 serv_printf("MSIV putscript|%s|", script_names[i]);
585                                 serv_getln(buf, sizeof buf);
586                                 if (buf[0] == '4') {
587                                         snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
588                                         striplt(bstr(this_name));
589                                         serv_printf("%s", bstr(this_name));
590                                         serv_puts("000");
591                                 }
592                         }
593                 }
594         }
595
596         strcpy(WC->ImportantMessage, _("Your changes have been saved."));
597         display_main_menu();
598         return;
599 }
600
601
602 /**
603  * \brief show a list of available scripts to add/remove them
604  */
605 void display_add_remove_scripts(char *message)
606 {
607         char buf[256];
608         char script_name[256];
609
610         output_headers(1, 1, 2, 0, 0, 0);
611         wprintf("<div id=\"banner\">\n");
612         wprintf("<img src=\"static/advanpage2_48x.gif\">");
613         wprintf(_("Add or delete scripts"));
614         wprintf("</h1>\n");
615         wprintf("</div>\n");
616         
617         wprintf("<div id=\"content\" class=\"service\">\n");
618
619         if (message != NULL) wprintf(message);
620
621         wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
622
623         svprintf("BOXTITLE", WCS_STRING, _("Add a new script"));
624         do_template("beginbox");
625
626         wprintf(_("To create a new script, enter the desired "
627                 "script name in the box below and click 'Create'."));
628         wprintf("<br /><br />");
629
630         wprintf("<center><form method=\"POST\" action=\"create_script\">\n");
631         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
632         wprintf(_("Script name: "));
633         wprintf("<input type=\"text\" name=\"script_name\"><br />\n"
634                 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
635                 "</form></center>\n", _("Create"));
636
637         do_template("endbox");
638
639         svprintf("BOXTITLE", WCS_STRING, _("Edit scripts"));
640         do_template("beginbox");
641         wprintf("<br /><div align=center><a href=\"display_sieve\">%s</a><br /><br />\n",
642                 _("Return to the script editing screen")
643         );
644         do_template("endbox");
645
646         wprintf("</td><td>");
647
648         svprintf("BOXTITLE", WCS_STRING, _("Delete scripts"));
649         do_template("beginbox");
650
651         wprintf(_("To delete an existing script, select the script "
652                 "name from the list and click 'Delete'."));
653         wprintf("<br /><br />");
654         
655         wprintf("<center>"
656                 "<form method=\"POST\" action=\"delete_script\">\n");
657         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
658         wprintf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
659
660         serv_puts("MSIV listscripts");
661         serv_getln(buf, sizeof buf);
662         if (buf[0] == '1') {
663                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
664                         extract_token(script_name, buf, 0, '|', sizeof script_name);
665                         if ( (extract_int(buf, 1) == 0) && (strcasecmp(script_name, RULES_SCRIPT)) ) {
666                                 wprintf("<option>");
667                                 escputs(script_name);
668                                 wprintf("</option>\n");
669                         }
670                 }
671         }
672         wprintf("</select><br />\n");
673
674         wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
675                 "onClick=\"return confirm('%s');\">", _("Delete script"), _("Delete this script?"));
676         wprintf("</form></center>\n");
677         do_template("endbox");
678
679         wprintf("</td></tr></table>\n");
680
681         wDumpContent(1);
682 }
683
684
685
686 /**
687  * \brief delete a script
688  */
689 void delete_script(void) {
690         char buf[256];
691
692         serv_printf("MSIV deletescript|%s", bstr("script_name"));
693         serv_getln(buf, sizeof buf);
694         display_add_remove_scripts(&buf[4]);
695 }
696                 
697
698
699 /**
700  * \brief create a new script
701  * take the web environment script name and create it on the citadel server
702  */
703 void create_script(void) {
704         char buf[256];
705
706         serv_printf("MSIV getscript|%s", bstr("script_name"));
707         serv_getln(buf, sizeof buf);
708         if (buf[0] == '1') {
709                 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
710                         /* flush */
711                 }
712                 display_add_remove_scripts(_("A script by that name already exists."));
713                 return;
714         }
715         
716         serv_printf("MSIV putscript|%s", bstr("script_name"));
717         serv_getln(buf, sizeof buf);
718         if (buf[0] == '4') {
719                 serv_puts("keep;");
720                 serv_puts("000");
721                 display_add_remove_scripts(_("A new script has been created.  Return to the script editing screen to edit and activate it."));
722                 return;
723         }
724
725         display_add_remove_scripts(&buf[4]);
726 }
727
728
729
730
731 void display_rules_editor_inner_div(void) {
732         int i, j;
733         char buf[4096];
734         char rules[MAX_RULES][2048];
735
736         struct {
737                 char name[128];
738         } *rooms = NULL;
739         int num_roomnames = 0;
740         int num_roomnames_alloc = 0;
741
742         int active;
743         char hfield[256];
744         char compare[32];
745         char htext[256];
746         char sizecomp[32];
747         int sizeval;
748         char action[32];
749         char fileinto[128];
750         char redirect[256];
751         char automsg[1024];
752         char final[32];
753
754         /* load the rules */
755         memset(rules, 0, sizeof rules);
756         serv_printf("MSIV getscript|%s", RULES_SCRIPT);
757         serv_getln(buf, sizeof buf);
758         if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
759                 if (!strncasecmp(buf, "# WEBCIT_RULE|", 14)) {
760                         j = extract_int(buf, 1);
761                         remove_token(buf, 0, '|');
762                         remove_token(buf, 0, '|');
763                         CtdlDecodeBase64(rules[j], buf, strlen(buf));
764                 }
765         }
766
767         /* load the roomnames */
768         serv_puts("LKRA");
769         serv_getln(buf, sizeof buf);
770         if (buf[0] == '1') {
771                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
772                         ++num_roomnames;
773                         if (num_roomnames > num_roomnames_alloc) {
774                                 num_roomnames_alloc += 250;
775                                 rooms = realloc(rooms, (num_roomnames_alloc * 128));
776                         }
777                         extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
778                 }
779         }
780
781
782 /*
783  * This script should get called by every onChange event...
784  *
785  */
786         wprintf("<script type=\"text/javascript\">                                      \n"
787                 "                                                                       \n"
788                 "var highest_active_rule = (-1);                                        \n"
789                 "                                                                       \n"
790                 "function UpdateRules() {                                               \n");
791 /*
792  * Show only the active rows...
793  */
794         wprintf("  highest_active_rule = (-1);                                          \n");
795         wprintf("  for (i=0; i<%d; ++i) {                                               \n", MAX_RULES);
796         wprintf("   if ($('active'+i).checked) {                                        \n"
797                 "     $('rule' + i).style.display = 'block';                            \n"
798                 "     highest_active_rule = i;                                          \n"
799                 "   }                                                                   \n"
800                 "   else {                                                              \n"
801                 "     $('rule' + i).style.display = 'none';                             \n"
802                 "   }                                                                   \n"
803                 "  }                                                                    \n");
804 /*
805  * Show only the fields relevant to the rules...
806  */
807         wprintf("  for (i=0; i<=highest_active_rule; ++i) {                             \n"
808                 "    d = ($('movedown'+i));                                             \n"
809                 "    if (i < highest_active_rule) {                                     \n"
810                 "      d.style.display = 'block';                                       \n"
811                 "    }                                                                  \n"
812                 "    else {                                                             \n"
813                 "      d.style.display = 'none';                                        \n"
814                 "    }                                                                  \n"
815                 "    d = ($('hfield'+i).options[$('hfield'+i).selectedIndex].value);    \n"
816                 "    if (d == 'all') {                                                  \n"
817                 "      $('div_size'+i).style.display = 'none';                          \n"
818                 "      $('div_compare'+i).style.display = 'none';                       \n"
819                 "      $('div_nocompare'+i).style.display = 'block';                    \n"
820                 "    }                                                                  \n"
821                 "    else if (d == 'size') {                                            \n"
822                 "      $('div_size'+i).style.display = 'block';                         \n"
823                 "      $('div_compare'+i).style.display = 'none';                       \n"
824                 "      $('div_nocompare'+i).style.display = 'none';                     \n"
825                 "    }                                                                  \n"
826                 "    else {                                                             \n"
827                 "      $('div_size'+i).style.display = 'none';                          \n"
828                 "      $('div_compare'+i).style.display = 'block';                      \n"
829                 "      $('div_nocompare'+i).style.display = 'none';                     \n"
830                 "    }                                                                  \n"
831                 "    d = ($('action'+i).options[$('action'+i).selectedIndex].value);    \n"
832                 "    if (d == 'fileinto') {                                             \n"
833                 "      $('div_fileinto'+i).style.display = 'block';                     \n"
834                 "      $('div_redirect'+i).style.display = 'none';                      \n"
835                 "      $('div_automsg'+i).style.display = 'none';                       \n"
836                 "    } else if (d == 'redirect') {                                      \n"
837                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
838                 "      $('div_redirect'+i).style.display = 'block';                     \n"
839                 "      $('div_automsg'+i).style.display = 'none';                       \n"
840                 "    } else if ((d == 'reject') || (d == 'vacation'))  {                \n"
841                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
842                 "      $('div_redirect'+i).style.display = 'none';                      \n"
843                 "      $('div_automsg'+i).style.display = 'block';                      \n"
844                 "    } else {                                                           \n"
845                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
846                 "      $('div_redirect'+i).style.display = 'none';                      \n"
847                 "      $('div_automsg'+i).style.display = 'none';                       \n"
848                 "    }                                                                  \n"
849                 "    if (highest_active_rule < %d) {                                    \n", MAX_RULES-1 );
850         wprintf("      $('div_addrule').style.display = 'block';                        \n"
851                 "    } else {                                                           \n"
852                 "      $('div_addrule').style.display = 'none';                         \n"
853                 "    }                                                                  \n"
854                 "  }                                                                    \n"
855                 "}                                                                      \n"
856 /*
857  * Add a rule (really, just un-hide it)
858  */
859                 "function AddRule() {                                                   \n"
860                 "  highest_active_rule = highest_active_rule + 1;                       \n"
861                 "  $('active'+highest_active_rule).checked = true;                      \n"
862                 "  UpdateRules();                                                       \n"
863                 "}                                                                      \n"
864 /*
865  * Swap two rules
866  */
867                 "function SwapRules(ra, rb) {                                           \n"
868                 "                                                                       \n"
869                 "  var things = new Array();                                            \n"
870                 "  things[0] = 'hfield';                                                \n"
871                 "  things[1] = 'compare';                                               \n"
872                 "  things[2] = 'htext';                                                 \n"
873                 "  things[3] = 'action';                                                \n"
874                 "  things[4] = 'fileinto';                                              \n"
875                 "  things[5] = 'redirect';                                              \n"
876                 "  things[6] = 'final';                                                 \n"
877                 "  things[7] = 'sizecomp';                                              \n"
878                 "  things[8] = 'sizeval';                                               \n"
879                 "  things[9] = 'automsg';                                               \n"
880                 "                                                                       \n"
881                 "  for (i=0; i<=9; ++i) {                                               \n"
882                 "    tempval=$(things[i]+ra).value;                                     \n"
883                 "    $(things[i]+ra).value = $(things[i]+rb).value;                     \n"
884                 "    $(things[i]+rb).value = tempval;                                   \n"
885                 "  }                                                                    \n"
886                 "}                                                                      \n"
887 /*
888  * Delete a rule (percolate the deleted rule out to the end, then deactivate it)
889  */
890                 "function DeleteRule(rd) {                                              \n"
891                 "  for (j=rd; j<=highest_active_rule; ++j) {                            \n"
892                 "    SwapRules(j, (j+1));                                               \n"
893                 "  }                                                                    \n"
894                 "  $('active'+highest_active_rule).checked = false;                     \n"
895                 "}                                                                      \n"
896                 "</script>                                                              \n"
897         );
898
899
900         wprintf("<br />");
901
902         wprintf("<table cellpadding=2 width=100%%>");
903
904         for (i=0; i<MAX_RULES; ++i) {
905
906                 /* Grab our existing values to populate */
907                 active = extract_int(rules[i], 0);
908                 extract_token(hfield, rules[i], 1, '|', sizeof hfield);
909                 extract_token(compare, rules[i], 2, '|', sizeof compare);
910                 extract_token(htext, rules[i], 3, '|', sizeof htext);
911                 extract_token(sizecomp, rules[i], 4, '|', sizeof sizecomp);
912                 sizeval = extract_int(rules[i], 5);
913                 extract_token(action, rules[i], 6, '|', sizeof action);
914                 extract_token(fileinto, rules[i], 7, '|', sizeof fileinto);
915                 extract_token(redirect, rules[i], 8, '|', sizeof redirect);
916                 extract_token(automsg, rules[i], 9, '|', sizeof automsg);
917                 extract_token(final, rules[i], 10, '|', sizeof final);
918                 
919                 /* now generate the table row */
920
921                 wprintf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
922                         i,
923                         ((i%2) ? "DDDDDD" : "FFFFFF")
924                 );
925
926                 wprintf("<td width=5%% align=\"center\">");
927
928                 wprintf("<div style=\"display:none\">");
929                 wprintf("<input type=\"checkbox\" name=\"active%d\" id=\"active%d\" %s>",
930                         i, i,
931                         (active ? "checked" : "")
932                 );
933                 wprintf("</div>");
934
935                 if (i>0) wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
936                         "<img border=\"0\" src=\"static/up_pointer.gif\" "
937                         "title=\"%s\"/></a>",
938                         i-1, i, _("Move rule up") );
939
940                 wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
941                         "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" "
942                         "title=\"%s\"/></a>",
943                         i, i+1, i, _("Move rule down") );
944
945                 wprintf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
946                         "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" "
947                         "title=\"%s\"/></a>",
948                         i, i, _("Delete rule") );
949
950                 wprintf("</td>");
951
952                 wprintf("<td width=5%% align=\"center\">");
953                 wprintf("<font size=+2>%d</font>", i+1);
954                 wprintf("</td>");
955
956                 wprintf("<td width=20%%>%s ", _("If") );
957
958                 char *hfield_values[14][2] = {
959                         {       "from",         _("From")               },
960                         {       "tocc",         _("To or Cc")           },
961                         {       "subject",      _("Subject")            },
962                         {       "replyto",      _("Reply-to")           },
963                         {       "sender",       _("Sender")             },
964                         {       "resentfrom",   _("Resent-From")        },
965                         {       "resentto",     _("Resent-To")          },
966                         {       "envfrom",      _("Envelope From")      },
967                         {       "envto",        _("Envelope To")        },
968                         {       "xmailer",      _("X-Mailer")           },
969                         {       "xspamflag",    _("X-Spam-Flag")        },
970                         {       "xspamstatus",  _("X-Spam-Status")      },
971                         {       "size",         _("Message size")       },
972                         {       "all",          _("All")                }
973                 };
974
975                 wprintf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
976                         i, i);
977                 for (j=0; j<14; ++j) {
978                         wprintf("<option %s value=\"%s\">%s</option>",
979                                 ( (!strcasecmp(hfield, hfield_values[j][0])) ? "selected" : ""),
980                                 hfield_values[j][0],
981                                 hfield_values[j][1]
982                         );
983                 }
984
985                 wprintf("</select>");
986                 wprintf("</td>");
987
988                 wprintf("<td width=20%%>");
989
990                 char *compare_values[6][2] = {
991                         {       "contains",     _("contains")           },
992                         {       "notcontains",  _("does not contain")   },
993                         {       "is",           _("is")                 },
994                         {       "isnot",        _("is not")             },
995                         {       "matches",      _("matches")            },
996                         {       "notmatches",   _("does not match")     }
997                 };
998
999                 wprintf("<div id=\"div_compare%d\">", i);
1000                 wprintf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
1001                         i, i);
1002                 for (j=0; j<6; ++j) {
1003                         wprintf("<option %s value=\"%s\">%s</option>",
1004                                 ( (!strcasecmp(compare, compare_values[j][0])) ? "selected" : ""),
1005                                 compare_values[j][0],
1006                                 compare_values[j][1]
1007                         );
1008                 }
1009                 wprintf("</select>");
1010
1011                 wprintf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\" value=\"", i, i);
1012                 escputs(htext);
1013                 wprintf("\"></div>");
1014
1015                 wprintf("<div id=\"div_nocompare%d\">", i);
1016                 wprintf("%s", _("(All messages)"));
1017                 wprintf("</div>");
1018
1019                 char *sizecomp_values[2][2] = {
1020                         {       "larger",       _("is larger than")     },
1021                         {       "smaller",      _("is smaller than")    }
1022                 };
1023
1024                 wprintf("<div id=\"div_size%d\">", i);
1025                 wprintf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
1026                         i, i);
1027                 for (j=0; j<2; ++j) {
1028                         wprintf("<option %s value=\"%s\">%s</option>",
1029                                 ( (!strcasecmp(sizecomp, sizecomp_values[j][0])) ? "selected" : ""),
1030                                 sizecomp_values[j][0],
1031                                 sizecomp_values[j][1]
1032                         );
1033                 }
1034                 wprintf("</select>");
1035
1036                 wprintf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\" value=\"%d\">",
1037                         i, i, sizeval);
1038                 wprintf("bytes");
1039                 wprintf("</div>");
1040
1041                 wprintf("</td>");
1042
1043                 char *action_values[6][2] = {
1044                         {       "keep",         _("Keep")               },
1045                         {       "discard",      _("Discard silently")   },
1046                         {       "reject",       _("Reject")             },
1047                         {       "fileinto",     _("Move message to")    },
1048                         {       "redirect",     _("Forward to")         },
1049                         {       "vacation",     _("Vacation")           }
1050                 };
1051
1052                 wprintf("<td width=20%%>");
1053                 wprintf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
1054                         i, i);
1055                 for (j=0; j<6; ++j) {
1056                         wprintf("<option %s value=\"%s\">%s</option>",
1057                                 ( (!strcasecmp(action, action_values[j][0])) ? "selected" : ""),
1058                                 action_values[j][0],
1059                                 action_values[j][1]
1060                         );
1061                 }
1062                 wprintf("</select>");
1063
1064                 wprintf("<div id=\"div_fileinto%d\">", i);
1065                 wprintf("<select name=\"fileinto%d\" id=\"fileinto%d\">", i, i);
1066                 for (j=0; j<num_roomnames; ++j) {
1067                         wprintf("<option ");
1068                         if (!strcasecmp(rooms[j].name, fileinto)) {
1069                                 wprintf("selected ");
1070                         }
1071                         wprintf("value=\"");
1072                         escputs(rooms[j].name);
1073                         wprintf("\">");
1074                         escputs(rooms[j].name);
1075                         wprintf("</option>\n");
1076                 }
1077                 wprintf("</select>\n");
1078                 wprintf("</div>");
1079
1080                 wprintf("<div id=\"div_redirect%d\">", i);
1081                 wprintf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\" value=\"", i, i);
1082                 escputs(redirect);
1083                 wprintf("\"></div>");
1084
1085                 wprintf("<div id=\"div_automsg%d\">", i);
1086                 wprintf(_("Message:"));
1087                 wprintf("<br />");
1088                 wprintf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
1089                 escputs(automsg);
1090                 wprintf("</textarea>");
1091                 wprintf("</div>");
1092
1093                 wprintf("</td>");
1094
1095                 char *final_values[2][2] = {
1096                         {       "continue",     _("continue processing")        },
1097                         {       "stop",         _("stop")                       }
1098                 };
1099
1100                 wprintf("<td width=10%% align=\"center\">%s</td>", _("and then") );
1101
1102                 wprintf("<td width=20%%>");
1103                 wprintf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
1104                         i, i);
1105                 for (j=0; j<2; ++j) {
1106                         wprintf("<option %s value=\"%s\">%s</option>",
1107                                 ( (!strcasecmp(final, final_values[j][0])) ? "selected" : ""),
1108                                 final_values[j][0],
1109                                 final_values[j][1]
1110                         );
1111                 }
1112                 wprintf("</select>");
1113                 wprintf("</td>");
1114
1115                 wprintf("</tr>\n");
1116
1117         }
1118
1119         wprintf("</table>");
1120         wprintf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">%s</a><br /></div>\n",
1121                 _("Add rule")
1122         );
1123
1124         wprintf("<script type=\"text/javascript\">                                      \n");
1125         wprintf("UpdateRules();                                                         \n");
1126         wprintf("</script>                                                              \n");
1127
1128         free(rooms);
1129 }
1130
1131
1132
1133
1134
1135 /*@}*/