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