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