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