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", script_names[i]);
145 serv_printf("MSIV getscript|%s", script_names[i]);
146 serv_getln(buf, sizeof buf);
147 if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
148 wprintf("%s\n", buf);
150 wprintf("</textarea>\n");
155 wprintf("<script type=\"text/javascript\"> \n"
156 "ToggleScriptPanels(); \n"
163 /* The rest of this is common for all panels... */
165 wprintf("<div align=\"center\"><br>");
166 wprintf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
168 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
169 wprintf("</div></form>\n");
171 wprintf("</td></tr></table></div>\n");
173 wprintf("<script type=\"text/javascript\"> \n"
174 "ToggleSievePanels(); \n"
184 * \brief save sieve config
186 void save_sieve(void) {
188 char script_names[MAX_SCRIPTS][64];
190 int active_script = (-1);
195 if (strlen(bstr("save_button")) == 0) {
196 strcpy(WC->ImportantMessage,
197 _("Cancelled. Changes were not saved."));
202 serv_puts("MSIV listscripts");
203 serv_getln(buf, sizeof(buf));
204 if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
205 if (num_scripts < MAX_SCRIPTS) {
206 extract_token(script_names[num_scripts], buf, 0, '|', 64);
207 if (extract_int(buf, 1) > 0) {
208 active_script = num_scripts;
214 bigaction = atoi(bstr("bigaction"));
216 if (bigaction == 0) {
217 serv_puts("MSIV setactive||");
218 serv_getln(buf, sizeof buf);
221 else if (bigaction == 2) {
222 serv_printf("MSIV setactive|%s|", bstr("active_script"));
223 serv_getln(buf, sizeof buf);
226 if (num_scripts > 0) {
227 for (i=0; i<num_scripts; ++i) {
228 serv_printf("MSIV putscript|%s|", script_names[i]);
229 serv_getln(buf, sizeof buf);
231 snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
232 striplt(bstr(this_name));
233 serv_printf("%s", bstr(this_name));
239 strcpy(WC->ImportantMessage, _("Your changes have been saved."));
246 * \brief show a list of available scripts to add/remove them
248 void display_add_remove_scripts(char *message)
251 char script_name[256];
253 output_headers(1, 1, 2, 0, 0, 0);
254 wprintf("<div id=\"banner\">\n");
255 wprintf("<table width=100%% border=0 bgcolor=#444455><tr>"
257 "<span class=\"titlebar\">"
258 "<img src=\"static/advanpage2_48x.gif\">");
259 wprintf(_("Add or delete scripts"));
260 wprintf("</span></td></tr></table>\n"
261 "</div>\n<div id=\"content\">\n"
264 if (message != NULL) wprintf(message);
266 wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
268 svprintf("BOXTITLE", WCS_STRING, _("Add a new script"));
269 do_template("beginbox");
271 wprintf(_("To create a new script, enter the desired "
272 "script name in the box below and click 'Create'."));
273 wprintf("<br /><br />");
275 wprintf("<center><form method=\"POST\" action=\"create_script\">\n");
276 wprintf(_("Script name: "));
277 wprintf("<input type=\"text\" name=\"script_name\"><br />\n"
278 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
279 "</form></center>\n", _("Create"));
281 do_template("endbox");
283 svprintf("BOXTITLE", WCS_STRING, _("Edit scripts"));
284 do_template("beginbox");
285 wprintf("<br /><div align=center><a href=\"display_sieve\">%s</a><br /><br />\n",
286 _("Return to the script editing screen")
288 do_template("endbox");
290 wprintf("</td><td>");
292 svprintf("BOXTITLE", WCS_STRING, _("Delete scripts"));
293 do_template("beginbox");
295 wprintf(_("To delete an existing script, select the script "
296 "name from the list and click 'Delete'."));
297 wprintf("<br /><br />");
300 "<form method=\"POST\" action=\"delete_script\">\n");
301 wprintf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
303 serv_puts("MSIV listscripts");
304 serv_getln(buf, sizeof buf);
306 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
307 extract_token(script_name, buf, 0, '|', sizeof script_name);
308 if (extract_int(buf, 1) == 0) {
310 escputs(script_name);
311 wprintf("</option>\n");
315 wprintf("</select><br />\n");
317 wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
318 "onClick=\"return confirm('%s');\">", _("Delete script"), _("Delete this script?"));
319 wprintf("</form></center>\n");
320 do_template("endbox");
322 wprintf("</td></tr></table>\n");
330 * \brief delete a script
332 void delete_script(void) {
335 serv_printf("MSIV deletescript|%s", bstr("script_name"));
336 serv_getln(buf, sizeof buf);
337 display_add_remove_scripts(&buf[4]);
343 * \brief create a new script
344 * take the web environment script name and create it on the citadel server
346 void create_script(void) {
349 serv_printf("MSIV getscript|%s", bstr("script_name"));
350 serv_getln(buf, sizeof buf);
352 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
355 display_add_remove_scripts(_("A script by that name already exists."));
359 serv_printf("MSIV putscript|%s", bstr("script_name"));
360 serv_getln(buf, sizeof buf);
364 display_add_remove_scripts(_("A new script has been created. Return to the script editing screen to edit and activate it."));
368 display_add_remove_scripts(&buf[4]);
374 void display_rules_editor_inner_div(void) {
381 int num_roomnames = 0;
382 int num_roomnames_alloc = 0;
385 /* load the roomnames */
387 serv_getln(buf, sizeof buf);
389 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
391 if (num_roomnames > num_roomnames_alloc) {
392 num_roomnames_alloc += 250;
393 rooms = realloc(rooms, (num_roomnames_alloc * 128));
395 extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
401 * This script should get called by every onChange event...
404 wprintf("<script type=\"text/javascript\"> \n"
406 "var highest_active_rule = (-1); \n"
408 "function UpdateRules() { \n"
409 " for (i=0; i<%d; ++i) { \n", MAX_RULES);
410 wprintf(" d = ($('movedown'+i)); \n"
411 " if (i < highest_active_rule) { \n"
412 " d.style.display = 'block'; \n"
415 " d.style.display = 'none'; \n"
417 " d = ($('hfield'+i).options[$('hfield'+i).selectedIndex].value); \n"
418 " if (d == 'all') { \n"
419 " $('div_size'+i).style.display = 'none'; \n"
420 " $('div_compare'+i).style.display = 'none'; \n"
422 " else if (d == 'size') { \n"
423 " $('div_size'+i).style.display = 'block'; \n"
424 " $('div_compare'+i).style.display = 'none'; \n"
427 " $('div_size'+i).style.display = 'none'; \n"
428 " $('div_compare'+i).style.display = 'block'; \n"
430 " d = ($('action'+i).options[$('action'+i).selectedIndex].value); \n"
431 " if (d == 'fileinto') { \n"
432 " $('div_fileinto'+i).style.display = 'block'; \n"
433 " $('div_redirect'+i).style.display = 'none'; \n"
434 " $('div_automsg'+i).style.display = 'none'; \n"
435 " } else if (d == 'redirect') { \n"
436 " $('div_fileinto'+i).style.display = 'none'; \n"
437 " $('div_redirect'+i).style.display = 'block'; \n"
438 " $('div_automsg'+i).style.display = 'none'; \n"
439 " } else if ((d == 'reject') || (d == 'vacation')) { \n"
440 " $('div_fileinto'+i).style.display = 'none'; \n"
441 " $('div_redirect'+i).style.display = 'none'; \n"
442 " $('div_automsg'+i).style.display = 'block'; \n"
444 " $('div_fileinto'+i).style.display = 'none'; \n"
445 " $('div_redirect'+i).style.display = 'none'; \n"
446 " $('div_automsg'+i).style.display = 'none'; \n"
448 " if (highest_active_rule < %d) { \n", MAX_RULES-1 );
449 wprintf(" $('div_addrule').style.display = 'block'; \n"
451 " $('div_addrule').style.display = 'none'; \n"
456 * Show only the active rows...
458 wprintf(" highest_active_rule = (-1); \n");
459 wprintf(" for (i=0; i<%d; ++i) { \n", MAX_RULES);
460 wprintf(" if ($('active'+i).checked) { \n"
461 " $('rule' + i).style.display = 'block'; \n"
462 " highest_active_rule = i; \n"
465 " $('rule' + i).style.display = 'none'; \n"
470 * Add a rule (really, just un-hide it)
472 "function AddRule() { \n"
473 " highest_active_rule = highest_active_rule + 1; \n"
474 " $('active'+highest_active_rule).checked = true; \n"
480 "function SwapRules(ra, rb) { \n"
482 " var things = new Array(); \n"
483 " things[0] = 'hfield'; \n"
484 " things[1] = 'compare'; \n"
485 " things[2] = 'htext'; \n"
486 " things[3] = 'action'; \n"
487 " things[4] = 'fileinto'; \n"
488 " things[5] = 'redirect'; \n"
489 " things[6] = 'final'; \n"
490 " things[7] = 'sizecomp'; \n"
491 " things[8] = 'sizeval'; \n"
492 " things[9] = 'automsg'; \n"
494 " for (i=0; i<=9; ++i) { \n"
495 " tempval=$(things[i]+ra).value; \n"
496 " $(things[i]+ra).value = $(things[i]+rb).value; \n"
497 " $(things[i]+rb).value = tempval; \n"
501 * Delete a rule (percolate the deleted rule out to the end,
502 * and then decrement highest_active_rule)
504 "function DeleteRule(rd) { \n"
505 " for (i=rd; i<highest_active_rule; ++i) { \n"
506 " SwapRules(i, (i+1)); \n"
508 " $('active'+highest_active_rule).checked = false; \n"
516 wprintf("<table border=1 cellpadding=2 width=100%%>");
518 for (i=0; i<MAX_RULES; ++i) {
520 wprintf("<tr id=\"rule%d\">", i);
524 wprintf("<div style=\"display:none\">");
525 wprintf("<input type=\"checkbox\" id=\"active%d\">", i);
528 if (i>0) wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
529 "<img border=\"0\" src=\"static/up_pointer.gif\" /></a>", i-1, i);
531 wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
532 "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" /></a>",
535 wprintf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
536 "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" /></a>",
539 wprintf(" %d. %s</td>", i+1, _("If") );
543 wprintf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
545 wprintf("<option value=\"from\">%s</option>", _("From"));
546 wprintf("<option value=\"tocc\">%s</option>", _("To or Cc"));
547 wprintf("<option value=\"subject\">%s</option>", _("Subject"));
548 wprintf("<option value=\"replyto\">%s</option>", _("Reply-to"));
549 wprintf("<option value=\"sender\">%s</option>", _("Sender"));
550 wprintf("<option value=\"resentfrom\">%s</option>", _("Resent-From"));
551 wprintf("<option value=\"resentto\">%s</option>", _("Resent-To"));
552 wprintf("<option value=\"envfrom\">%s</option>", _("Envelope From"));
553 wprintf("<option value=\"envto\">%s</option>", _("Envelope To"));
554 wprintf("<option value=\"xmailer\">%s</option>", _("X-Mailer"));
555 wprintf("<option value=\"xspamflag\">%s</option>", _("X-Spam-Flag"));
556 wprintf("<option value=\"xspamstatus\">%s</option>", _("X-Spam-Status"));
557 wprintf("<option value=\"size\">%s</option>", _("Message size"));
558 wprintf("<option value=\"all\">%s</option>", _("(All messages)"));
559 wprintf("</select>");
564 wprintf("<div id=\"div_compare%d\">", i);
565 wprintf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
567 wprintf("<option value=\"contains\">%s</option>", _("contains"));
568 wprintf("<option value=\"notcontains\">%s</option>", _("does not contain"));
569 wprintf("<option value=\"is\">%s</option>", _("is"));
570 wprintf("<option value=\"isnot\">%s</option>", _("is not"));
571 wprintf("</select>");
573 wprintf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\">", i, i);
576 wprintf("<div id=\"div_size%d\">", i);
577 wprintf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
579 wprintf("<option value=\"larger\">%s</option>", _("is larger than"));
580 wprintf("<option value=\"smaller\">%s</option>", _("is smaller than"));
581 wprintf("</select>");
583 wprintf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\">", i, i);
590 wprintf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
592 wprintf("<option value=\"keep\">%s</option>", _("Keep"));
593 wprintf("<option value=\"discard\">%s</option>", _("Discard silently"));
594 wprintf("<option value=\"reject\">%s</option>", _("Reject"));
595 wprintf("<option value=\"fileinto\">%s</option>", _("Move message to"));
596 wprintf("<option value=\"redirect\">%s</option>", _("Forward to"));
597 wprintf("<option value=\"vacation\">%s</option>", _("Vacation"));
598 wprintf("</select>");
600 wprintf("<div id=\"div_fileinto%d\">", i);
601 wprintf("<select name=\"fileinto%d\" id=\"fileinto%d\">", i, i);
602 for (j=0; j<num_roomnames; ++j) {
604 if (!strcasecmp(rooms[j].name, "Mail")) {
605 wprintf("selected ");
608 urlescputs(rooms[j].name);
610 escputs(rooms[j].name);
611 wprintf("</option>\n");
613 wprintf("</select>\n");
616 wprintf("<div id=\"div_redirect%d\">", i);
617 wprintf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\">", i, i);
620 wprintf("<div id=\"div_automsg%d\">", i);
621 wprintf(_("Message:"));
622 wprintf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
623 wprintf("</textarea>");
630 wprintf("<td>%s</td>", _("and then") );
633 wprintf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
635 wprintf("<option value=\"stop\">%s</option>", _("stop"));
636 wprintf("<option value=\"continue\">%s</option>", _("continue processing"));
637 wprintf("</select>");
645 wprintf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">Add rule</a><br /></div>\n");
647 wprintf("<script type=\"text/javascript\"> \n"