5 * \defgroup Sieve view/edit sieve config
6 * \ingroup WebcitDisplayItems
11 #define MAX_SCRIPTS 100
15 * \brief view/edit sieve config
17 void display_sieve(void)
19 char script_names[MAX_SCRIPTS][64];
21 int active_script = (-1);
26 memset(script_names, 0, sizeof script_names);
28 serv_puts("MSIV listscripts");
29 serv_getln(buf, sizeof(buf));
30 if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
31 if (num_scripts < MAX_SCRIPTS) {
32 extract_token(script_names[num_scripts], buf, 0, '|', 64);
33 if (extract_int(buf, 1) > 0) {
34 active_script = num_scripts;
40 output_headers(1, 1, 2, 0, 0, 0);
42 wprintf("<script type=\"text/javascript\"> \n"
44 "var previously_active_script; \n"
46 "function ToggleSievePanels() { \n"
47 " d = ($('sieveform').bigaction.options[$('sieveform').bigaction.selectedIndex].value); \n"
48 " for (i=0; i<3; ++i) { \n"
50 " $('sievediv' + i).style.display = 'block'; \n"
53 " $('sievediv' + i).style.display = 'none'; \n"
58 "function ToggleScriptPanels() { \n"
59 " d = ($('sieveform').active_script.options[$('sieveform').active_script.selectedIndex].value); \n"
60 " if ($('script_' + previously_active_script)) { \n"
61 " $('script_' + previously_active_script).style.display = 'none'; \n"
63 " $('script_' + d).style.display = 'block'; \n"
64 " previously_active_script = d; \n"
70 wprintf("<div id=\"banner\">\n");
71 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
72 wprintf("<SPAN CLASS=\"titlebar\">"
73 "<img src=\"static/advanpage2_48x.gif\">");
74 wprintf(_("View/edit server-side mail filters"));
76 wprintf("</TD></TR></TABLE>\n");
77 wprintf("</div>\n<div id=\"content\">\n");
79 wprintf("<div class=\"fix_scrollbar_bug\">"
80 "<table border=0 width=100%% bgcolor=\"#FFFFFF\">"
81 "<tr><td valign=top>\n");
84 wprintf("<form id=\"sieveform\" method=\"post\" action=\"save_sieve\">\n");
86 wprintf(_("When new mail arrives: "));
87 wprintf("<select name=\"bigaction\" size=1 onChange=\"ToggleSievePanels();\">\n");
89 wprintf("<option %s value=\"0\">", ((active_script < 0) ? "selected" : ""));
90 wprintf(_("Leave it in my inbox without filtering"));
91 wprintf("</option>\n");
93 wprintf("<option value=\"1\">");
94 wprintf(_("Filter it according to rules selected below"));
95 wprintf("</option>\n");
97 wprintf("<option %s value=\"2\">", ((active_script >= 0) ? "selected" : ""));
98 wprintf(_("Filter it through a manually edited script (advanced users only)"));
99 wprintf("</option>\n");
101 wprintf("</select>");
105 /* The "no filtering" div */
107 wprintf("<div id=\"sievediv0\" style=\"display:none\">\n");
108 wprintf("<div align=\"center\"><br /><br />");
109 wprintf(_("Your incoming mail will not be filtered through any scripts."));
110 wprintf("<br /><br /></div>\n");
113 /* The "webcit managed scripts" div */
115 wprintf("<div id=\"sievediv1\" style=\"display:none\">\n");
116 display_rules_editor_inner_div();
119 /* The "I'm smart and can write my own Sieve scripts" div */
121 wprintf("<div id=\"sievediv2\" style=\"display:none\">\n");
123 if (num_scripts > 0) {
124 wprintf(_("The currently active script is: "));
125 wprintf("<select name=\"active_script\" size=1 onChange=\"ToggleScriptPanels();\">\n");
126 for (i=0; i<num_scripts; ++i) {
127 wprintf("<option %s value=\"%s\">%s</option>\n",
128 ((active_script == i) ? "selected" : ""),
133 wprintf("</select>\n");
136 wprintf(" ");
137 wprintf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add or delete scripts"));
141 if (num_scripts > 0) {
142 for (i=0; i<num_scripts; ++i) {
143 wprintf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
144 wprintf("<textarea name=\"text_%s\" wrap=soft rows=20 cols=80 width=80>\n",
146 serv_printf("MSIV getscript|%s", script_names[i]);
147 serv_getln(buf, sizeof buf);
148 if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
149 wprintf("%s\n", buf);
151 wprintf("</textarea>\n");
156 wprintf("<script type=\"text/javascript\"> \n"
157 "ToggleScriptPanels(); \n"
164 /* The rest of this is common for all panels... */
166 wprintf("<div align=\"center\"><br>");
167 wprintf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
169 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
170 wprintf("</div></form>\n");
172 wprintf("</td></tr></table></div>\n");
174 wprintf("<script type=\"text/javascript\"> \n"
175 "ToggleSievePanels(); \n"
186 * \brief Translate the fields from the rule editor into something we can save...
188 void parse_fields_from_rule_editor(void) {
205 char encoded_rule[4096];
207 for (i=0; i<MAX_RULES; ++i) {
211 sprintf(fname, "active%d", i);
212 active = atoi(bstr(fname));
214 sprintf(fname, "hfield%d", i);
215 safestrncpy(hfield, bstr(fname), sizeof hfield);
217 sprintf(fname, "compare%d", i);
218 safestrncpy(compare, bstr(fname), sizeof compare);
220 sprintf(fname, "htext%d", i);
221 safestrncpy(htext, bstr(fname), sizeof htext);
223 sprintf(fname, "sizecomp%d", i);
224 safestrncpy(sizecomp, bstr(fname), sizeof sizecomp);
226 sprintf(fname, "sizeval%d", i);
227 sizeval = atoi(bstr(fname));
229 sprintf(fname, "action%d", i);
230 safestrncpy(action, bstr(fname), sizeof action);
232 sprintf(fname, "fileinto%d", i);
233 safestrncpy(fileinto, bstr(fname), sizeof fileinto);
235 sprintf(fname, "redirect%d", i);
236 safestrncpy(redirect, bstr(fname), sizeof redirect);
238 sprintf(fname, "automsg%d", i);
239 safestrncpy(automsg, bstr(fname), sizeof automsg);
241 sprintf(fname, "final%d", i);
242 safestrncpy(final, bstr(fname), sizeof final);
244 snprintf(rule, sizeof rule, "%d|%s|%s|%s|%s|%d|%s|%s|%s|%s|%s",
245 active, hfield, compare, htext, sizecomp, sizeval, action, fileinto,
246 redirect, automsg, final
249 lprintf(9, "Rule:\n%s\n", rule);
250 CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1);
251 lprintf(9, "Encoded rule:\n%s\n", encoded_rule);
259 * \brief save sieve config
261 void save_sieve(void) {
263 char script_names[MAX_SCRIPTS][64];
265 int active_script = (-1);
270 if (strlen(bstr("save_button")) == 0) {
271 strcpy(WC->ImportantMessage,
272 _("Cancelled. Changes were not saved."));
277 parse_fields_from_rule_editor();
279 serv_puts("MSIV listscripts");
280 serv_getln(buf, sizeof(buf));
281 if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
282 if (num_scripts < MAX_SCRIPTS) {
283 extract_token(script_names[num_scripts], buf, 0, '|', 64);
284 if (extract_int(buf, 1) > 0) {
285 active_script = num_scripts;
291 bigaction = atoi(bstr("bigaction"));
293 if (bigaction == 0) {
294 serv_puts("MSIV setactive||");
295 serv_getln(buf, sizeof buf);
298 else if (bigaction == 2) {
299 serv_printf("MSIV setactive|%s|", bstr("active_script"));
300 serv_getln(buf, sizeof buf);
303 if (num_scripts > 0) {
304 for (i=0; i<num_scripts; ++i) {
305 serv_printf("MSIV putscript|%s|", script_names[i]);
306 serv_getln(buf, sizeof buf);
308 snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
309 striplt(bstr(this_name));
310 serv_printf("%s", bstr(this_name));
316 strcpy(WC->ImportantMessage, _("Your changes have been saved."));
323 * \brief show a list of available scripts to add/remove them
325 void display_add_remove_scripts(char *message)
328 char script_name[256];
330 output_headers(1, 1, 2, 0, 0, 0);
331 wprintf("<div id=\"banner\">\n");
332 wprintf("<table width=100%% border=0 bgcolor=#444455><tr>"
334 "<span class=\"titlebar\">"
335 "<img src=\"static/advanpage2_48x.gif\">");
336 wprintf(_("Add or delete scripts"));
337 wprintf("</span></td></tr></table>\n"
338 "</div>\n<div id=\"content\">\n"
341 if (message != NULL) wprintf(message);
343 wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
345 svprintf("BOXTITLE", WCS_STRING, _("Add a new script"));
346 do_template("beginbox");
348 wprintf(_("To create a new script, enter the desired "
349 "script name in the box below and click 'Create'."));
350 wprintf("<br /><br />");
352 wprintf("<center><form method=\"POST\" action=\"create_script\">\n");
353 wprintf(_("Script name: "));
354 wprintf("<input type=\"text\" name=\"script_name\"><br />\n"
355 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
356 "</form></center>\n", _("Create"));
358 do_template("endbox");
360 svprintf("BOXTITLE", WCS_STRING, _("Edit scripts"));
361 do_template("beginbox");
362 wprintf("<br /><div align=center><a href=\"display_sieve\">%s</a><br /><br />\n",
363 _("Return to the script editing screen")
365 do_template("endbox");
367 wprintf("</td><td>");
369 svprintf("BOXTITLE", WCS_STRING, _("Delete scripts"));
370 do_template("beginbox");
372 wprintf(_("To delete an existing script, select the script "
373 "name from the list and click 'Delete'."));
374 wprintf("<br /><br />");
377 "<form method=\"POST\" action=\"delete_script\">\n");
378 wprintf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
380 serv_puts("MSIV listscripts");
381 serv_getln(buf, sizeof buf);
383 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
384 extract_token(script_name, buf, 0, '|', sizeof script_name);
385 if (extract_int(buf, 1) == 0) {
387 escputs(script_name);
388 wprintf("</option>\n");
392 wprintf("</select><br />\n");
394 wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
395 "onClick=\"return confirm('%s');\">", _("Delete script"), _("Delete this script?"));
396 wprintf("</form></center>\n");
397 do_template("endbox");
399 wprintf("</td></tr></table>\n");
407 * \brief delete a script
409 void delete_script(void) {
412 serv_printf("MSIV deletescript|%s", bstr("script_name"));
413 serv_getln(buf, sizeof buf);
414 display_add_remove_scripts(&buf[4]);
420 * \brief create a new script
421 * take the web environment script name and create it on the citadel server
423 void create_script(void) {
426 serv_printf("MSIV getscript|%s", bstr("script_name"));
427 serv_getln(buf, sizeof buf);
429 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
432 display_add_remove_scripts(_("A script by that name already exists."));
436 serv_printf("MSIV putscript|%s", bstr("script_name"));
437 serv_getln(buf, sizeof buf);
441 display_add_remove_scripts(_("A new script has been created. Return to the script editing screen to edit and activate it."));
445 display_add_remove_scripts(&buf[4]);
451 void display_rules_editor_inner_div(void) {
458 int num_roomnames = 0;
459 int num_roomnames_alloc = 0;
462 /* load the roomnames */
464 serv_getln(buf, sizeof buf);
466 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
468 if (num_roomnames > num_roomnames_alloc) {
469 num_roomnames_alloc += 250;
470 rooms = realloc(rooms, (num_roomnames_alloc * 128));
472 extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
478 * This script should get called by every onChange event...
481 wprintf("<script type=\"text/javascript\"> \n"
483 "var highest_active_rule = (-1); \n"
485 "function UpdateRules() { \n"
486 " for (i=0; i<%d; ++i) { \n", MAX_RULES);
487 wprintf(" d = ($('movedown'+i)); \n"
488 " if (i < highest_active_rule) { \n"
489 " d.style.display = 'block'; \n"
492 " d.style.display = 'none'; \n"
494 " d = ($('hfield'+i).options[$('hfield'+i).selectedIndex].value); \n"
495 " if (d == 'all') { \n"
496 " $('div_size'+i).style.display = 'none'; \n"
497 " $('div_compare'+i).style.display = 'none'; \n"
498 " $('div_nocompare'+i).style.display = 'block'; \n"
500 " else if (d == 'size') { \n"
501 " $('div_size'+i).style.display = 'block'; \n"
502 " $('div_compare'+i).style.display = 'none'; \n"
503 " $('div_nocompare'+i).style.display = 'none'; \n"
506 " $('div_size'+i).style.display = 'none'; \n"
507 " $('div_compare'+i).style.display = 'block'; \n"
508 " $('div_nocompare'+i).style.display = 'none'; \n"
510 " d = ($('action'+i).options[$('action'+i).selectedIndex].value); \n"
511 " if (d == 'fileinto') { \n"
512 " $('div_fileinto'+i).style.display = 'block'; \n"
513 " $('div_redirect'+i).style.display = 'none'; \n"
514 " $('div_automsg'+i).style.display = 'none'; \n"
515 " } else if (d == 'redirect') { \n"
516 " $('div_fileinto'+i).style.display = 'none'; \n"
517 " $('div_redirect'+i).style.display = 'block'; \n"
518 " $('div_automsg'+i).style.display = 'none'; \n"
519 " } else if ((d == 'reject') || (d == 'vacation')) { \n"
520 " $('div_fileinto'+i).style.display = 'none'; \n"
521 " $('div_redirect'+i).style.display = 'none'; \n"
522 " $('div_automsg'+i).style.display = 'block'; \n"
524 " $('div_fileinto'+i).style.display = 'none'; \n"
525 " $('div_redirect'+i).style.display = 'none'; \n"
526 " $('div_automsg'+i).style.display = 'none'; \n"
528 " if (highest_active_rule < %d) { \n", MAX_RULES-1 );
529 wprintf(" $('div_addrule').style.display = 'block'; \n"
531 " $('div_addrule').style.display = 'none'; \n"
536 * Show only the active rows...
538 wprintf(" highest_active_rule = (-1); \n");
539 wprintf(" for (i=0; i<%d; ++i) { \n", MAX_RULES);
540 wprintf(" if ($('active'+i).checked) { \n"
541 " $('rule' + i).style.display = 'block'; \n"
542 " highest_active_rule = i; \n"
545 " $('rule' + i).style.display = 'none'; \n"
550 * Add a rule (really, just un-hide it)
552 "function AddRule() { \n"
553 " highest_active_rule = highest_active_rule + 1; \n"
554 " $('active'+highest_active_rule).checked = true; \n"
560 "function SwapRules(ra, rb) { \n"
562 " var things = new Array(); \n"
563 " things[0] = 'hfield'; \n"
564 " things[1] = 'compare'; \n"
565 " things[2] = 'htext'; \n"
566 " things[3] = 'action'; \n"
567 " things[4] = 'fileinto'; \n"
568 " things[5] = 'redirect'; \n"
569 " things[6] = 'final'; \n"
570 " things[7] = 'sizecomp'; \n"
571 " things[8] = 'sizeval'; \n"
572 " things[9] = 'automsg'; \n"
574 " for (i=0; i<=9; ++i) { \n"
575 " tempval=$(things[i]+ra).value; \n"
576 " $(things[i]+ra).value = $(things[i]+rb).value; \n"
577 " $(things[i]+rb).value = tempval; \n"
581 * Delete a rule (percolate the deleted rule out to the end,
582 * and then decrement highest_active_rule)
584 "function DeleteRule(rd) { \n"
585 " for (i=rd; i<highest_active_rule; ++i) { \n"
586 " SwapRules(i, (i+1)); \n"
588 " $('active'+highest_active_rule).checked = false; \n"
596 wprintf("<table cellpadding=2 width=100%%>");
598 for (i=0; i<MAX_RULES; ++i) {
600 wprintf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
602 ((i%2) ? "DDDDDD" : "FFFFFF")
605 wprintf("<td width=5%% align=\"center\">");
607 wprintf("<div style=\"display:none\">");
608 wprintf("<input type=\"checkbox\" id=\"active%d\">", i);
611 if (i>0) wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
612 "<img border=\"0\" src=\"static/up_pointer.gif\" "
613 "title=\"%s\"/></a>",
614 i-1, i, _("Move rule up") );
616 wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
617 "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" "
618 "title=\"%s\"/></a>",
619 i, i+1, i, _("Move rule down") );
621 wprintf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
622 "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" "
623 "title=\"%s\"/></a>",
624 i, i, _("Delete rule") );
628 wprintf("<td width=5%% align=\"center\">");
629 wprintf("<font size=+2>%d</font>", i+1);
632 wprintf("<td width=20%%>%s ", _("If") );
634 wprintf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
636 wprintf("<option value=\"from\">%s</option>", _("From"));
637 wprintf("<option value=\"tocc\">%s</option>", _("To or Cc"));
638 wprintf("<option value=\"subject\">%s</option>", _("Subject"));
639 wprintf("<option value=\"replyto\">%s</option>", _("Reply-to"));
640 wprintf("<option value=\"sender\">%s</option>", _("Sender"));
641 wprintf("<option value=\"resentfrom\">%s</option>", _("Resent-From"));
642 wprintf("<option value=\"resentto\">%s</option>", _("Resent-To"));
643 wprintf("<option value=\"envfrom\">%s</option>", _("Envelope From"));
644 wprintf("<option value=\"envto\">%s</option>", _("Envelope To"));
645 wprintf("<option value=\"xmailer\">%s</option>", _("X-Mailer"));
646 wprintf("<option value=\"xspamflag\">%s</option>", _("X-Spam-Flag"));
647 wprintf("<option value=\"xspamstatus\">%s</option>", _("X-Spam-Status"));
648 wprintf("<option value=\"size\">%s</option>", _("Message size"));
649 wprintf("<option value=\"all\">%s</option>", _("All"));
650 wprintf("</select>");
653 wprintf("<td width=20%%>");
655 wprintf("<div id=\"div_compare%d\">", i);
656 wprintf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
658 wprintf("<option value=\"contains\">%s</option>", _("contains"));
659 wprintf("<option value=\"notcontains\">%s</option>", _("does not contain"));
660 wprintf("<option value=\"is\">%s</option>", _("is"));
661 wprintf("<option value=\"isnot\">%s</option>", _("is not"));
662 wprintf("</select>");
664 wprintf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\">", i, i);
667 wprintf("<div id=\"div_nocompare%d\">", i);
668 wprintf("%s", _("(All messages)"));
671 wprintf("<div id=\"div_size%d\">", i);
672 wprintf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
674 wprintf("<option value=\"larger\">%s</option>", _("is larger than"));
675 wprintf("<option value=\"smaller\">%s</option>", _("is smaller than"));
676 wprintf("</select>");
678 wprintf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\">", i, i);
684 wprintf("<td width=20%%>");
685 wprintf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
687 wprintf("<option value=\"keep\">%s</option>", _("Keep"));
688 wprintf("<option value=\"discard\">%s</option>", _("Discard silently"));
689 wprintf("<option value=\"reject\">%s</option>", _("Reject"));
690 wprintf("<option value=\"fileinto\">%s</option>", _("Move message to"));
691 wprintf("<option value=\"redirect\">%s</option>", _("Forward to"));
692 wprintf("<option value=\"vacation\">%s</option>", _("Vacation"));
693 wprintf("</select>");
695 wprintf("<div id=\"div_fileinto%d\">", i);
696 wprintf("<select name=\"fileinto%d\" id=\"fileinto%d\">", i, i);
697 for (j=0; j<num_roomnames; ++j) {
699 if (!strcasecmp(rooms[j].name, "Mail")) {
700 wprintf("selected ");
703 urlescputs(rooms[j].name);
705 escputs(rooms[j].name);
706 wprintf("</option>\n");
708 wprintf("</select>\n");
711 wprintf("<div id=\"div_redirect%d\">", i);
712 wprintf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\">", i, i);
715 wprintf("<div id=\"div_automsg%d\">", i);
716 wprintf(_("Message:"));
718 wprintf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
719 wprintf("</textarea>");
726 wprintf("<td width=10%% align=\"center\">%s</td>", _("and then") );
728 wprintf("<td width=20%%>");
729 wprintf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
731 wprintf("<option value=\"continue\">%s</option>", _("continue processing"));
732 wprintf("<option value=\"stop\">%s</option>", _("stop"));
733 wprintf("</select>");
741 wprintf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">Add rule</a><br /></div>\n");
743 wprintf("<script type=\"text/javascript\"> \n"