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