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