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