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