Started building the easy rules editor
[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       8
13
14 /**
15  * \brief view/edit sieve config
16  */
17 void display_sieve(void)
18 {
19         char script_names[MAX_SCRIPTS][64];
20         int num_scripts = 0;
21         int active_script = (-1);
22         char buf[256];
23         int i;
24         
25
26         memset(script_names, 0, sizeof script_names);
27
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;
35                         }
36                         ++num_scripts;
37                 }
38         }
39
40         output_headers(1, 1, 2, 0, 0, 0);
41
42         wprintf("<script type=\"text/javascript\">                                      \n"
43                 "                                                                       \n"
44                 "var previously_active_script;                                          \n"
45                 "                                                                       \n"
46                 "function ToggleSievePanels() {                                         \n"
47                 " d = ($('sieveform').bigaction.options[$('sieveform').bigaction.selectedIndex].value); \n"
48                 " for (i=0; i<3; ++i) {                                                 \n"
49                 "  if (i == d) {                                                        \n"
50                 "   $('sievediv' + i).style.display = 'block';                          \n"
51                 "  }                                                                    \n"
52                 "  else {                                                               \n"
53                 "   $('sievediv' + i).style.display = 'none';                           \n"
54                 "  }                                                                    \n"
55                 " }                                                                     \n"
56                 "}                                                                      \n"
57                 "                                                                       \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"
62                 " }                                                                     \n"
63                 " $('script_' + d).style.display = 'block';                             \n"
64                 " previously_active_script = d;                                         \n"
65                 "}                                                                      \n"
66                 "                                                                       \n"
67                 "</script>                                                              \n"
68         );
69
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"));
75         wprintf("</SPAN>\n");
76         wprintf("</TD></TR></TABLE>\n");
77         wprintf("</div>\n<div id=\"content\">\n");
78
79         wprintf("<div class=\"fix_scrollbar_bug\">"
80                 "<table border=0 width=100%% bgcolor=\"#FFFFFF\">"
81                 "<tr><td valign=top>\n");
82
83
84         wprintf("<form id=\"sieveform\" method=\"post\" action=\"save_sieve\">\n");
85
86         wprintf(_("When new mail arrives: "));
87         wprintf("<select name=\"bigaction\" size=1 onChange=\"ToggleSievePanels();\">\n");
88
89         wprintf("<option %s value=\"0\">", ((active_script < 0) ? "selected" : ""));
90         wprintf(_("Leave it in my inbox without filtering"));
91         wprintf("</option>\n");
92
93         wprintf("<option value=\"1\">");
94         wprintf(_("Filter it according to rules selected below"));
95         wprintf("</option>\n");
96
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");
100
101         wprintf("</select>");
102
103
104
105         /* The "no filtering" div */
106
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");
111         wprintf("</div>\n");
112
113         /* The "webcit managed scripts" div */
114
115         wprintf("<div id=\"sievediv1\" style=\"display:none\">\n");
116         display_rules_editor_inner_div();
117         wprintf("</div>\n");
118
119         /* The "I'm smart and can write my own Sieve scripts" div */
120
121         wprintf("<div id=\"sievediv2\" style=\"display:none\">\n");
122
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" : ""),
129                                 script_names[i],
130                                 script_names[i]
131                         );
132                 }
133                 wprintf("</select>\n");
134         }
135
136         wprintf("&nbsp;&nbsp;&nbsp;");
137         wprintf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add or delete scripts"));
138
139         wprintf("<br />\n");
140
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);
149                         }
150                         wprintf("</textarea>\n");
151                         wprintf("</div>\n");
152                 }
153         }
154
155         wprintf("<script type=\"text/javascript\">      \n"
156                 "ToggleScriptPanels();                  \n"
157                 "</script>                              \n"
158         );
159
160         wprintf("</div>\n");
161
162
163         /* The rest of this is common for all panels... */
164
165         wprintf("<div align=\"center\"><br>");
166         wprintf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
167         wprintf("&nbsp;");
168         wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
169         wprintf("</div></form>\n");
170
171         wprintf("</td></tr></table></div>\n");
172
173         wprintf("<script type=\"text/javascript\">      \n"
174                 "ToggleSievePanels();                   \n"
175                 "</script>                              \n"
176         );
177
178         wDumpContent(1);
179
180 }
181
182
183 /**
184  * \brief save sieve config
185  */
186 void save_sieve(void) {
187         int bigaction;
188         char script_names[MAX_SCRIPTS][64];
189         int num_scripts = 0;
190         int active_script = (-1);
191         int i;
192         char this_name[64];
193         char buf[256];
194
195         if (strlen(bstr("save_button")) == 0) {
196                 strcpy(WC->ImportantMessage,
197                         _("Cancelled.  Changes were not saved."));
198                 display_main_menu();
199                 return;
200         }
201
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;
209                         }
210                         ++num_scripts;
211                 }
212         }
213
214         bigaction = atoi(bstr("bigaction"));
215
216         if (bigaction == 0) {
217                 serv_puts("MSIV setactive||");
218                 serv_getln(buf, sizeof buf);
219         }
220
221         else if (bigaction == 2) {
222                 serv_printf("MSIV setactive|%s|", bstr("active_script"));
223                 serv_getln(buf, sizeof buf);
224         }
225
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);
230                         if (buf[0] == '4') {
231                                 snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
232                                 striplt(bstr(this_name));
233                                 serv_printf("%s", bstr(this_name));
234                                 serv_puts("000");
235                         }
236                 }
237         }
238
239         strcpy(WC->ImportantMessage, _("Your changes have been saved."));
240         display_main_menu();
241         return;
242 }
243
244
245 /**
246  * \brief show a list of available scripts to add/remove them
247  */
248 void display_add_remove_scripts(char *message)
249 {
250         char buf[256];
251         char script_name[256];
252
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>"
256                 "<td>"
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"
262         );
263
264         if (message != NULL) wprintf(message);
265
266         wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
267
268         svprintf("BOXTITLE", WCS_STRING, _("Add a new script"));
269         do_template("beginbox");
270
271         wprintf(_("To create a new script, enter the desired "
272                 "script name in the box below and click 'Create'."));
273         wprintf("<br /><br />");
274
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"));
280
281         do_template("endbox");
282
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")
287         );
288         do_template("endbox");
289
290         wprintf("</td><td>");
291
292         svprintf("BOXTITLE", WCS_STRING, _("Delete scripts"));
293         do_template("beginbox");
294
295         wprintf(_("To delete an existing script, select the script "
296                 "name from the list and click 'Delete'."));
297         wprintf("<br /><br />");
298         
299         wprintf("<center>"
300                 "<form method=\"POST\" action=\"delete_script\">\n");
301         wprintf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
302
303         serv_puts("MSIV listscripts");
304         serv_getln(buf, sizeof buf);
305         if (buf[0] == '1') {
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) {
309                                 wprintf("<option>");
310                                 escputs(script_name);
311                                 wprintf("</option>\n");
312                         }
313                 }
314         }
315         wprintf("</select><br />\n");
316
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");
321
322         wprintf("</td></tr></table>\n");
323
324         wDumpContent(1);
325 }
326
327
328
329 /**
330  * \brief delete a script
331  */
332 void delete_script(void) {
333         char buf[256];
334
335         serv_printf("MSIV deletescript|%s", bstr("script_name"));
336         serv_getln(buf, sizeof buf);
337         display_add_remove_scripts(&buf[4]);
338 }
339                 
340
341
342 /**
343  * \brief create a new script
344  * take the web environment script name and create it on the citadel server
345  */
346 void create_script(void) {
347         char buf[256];
348
349         serv_printf("MSIV getscript|%s", bstr("script_name"));
350         serv_getln(buf, sizeof buf);
351         if (buf[0] == '1') {
352                 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
353                         /* flush */
354                 }
355                 display_add_remove_scripts(_("A script by that name already exists."));
356                 return;
357         }
358         
359         serv_printf("MSIV putscript|%s", bstr("script_name"));
360         serv_getln(buf, sizeof buf);
361         if (buf[0] == '4') {
362                 serv_puts("keep;");
363                 serv_puts("000");
364                 display_add_remove_scripts(_("A new script has been created.  Return to the script editing screen to edit and activate it."));
365                 return;
366         }
367
368         display_add_remove_scripts(&buf[4]);
369 }
370
371
372
373
374 void display_rules_editor_inner_div(void) {
375         int i;
376
377
378         wprintf("<br />");
379         wprintf("<table class=\"mailbox_summary\" rules=rows cellpadding=2 style=\"width:100%%;-moz-user-select:none;\">");
380
381         for (i=0; i<MAX_RULES; ++i) {
382                 
383                 wprintf("<tr id=\"rule%d\">", i);
384
385                 wprintf("<td>%d. %s</td>", i+1, _("If") );
386
387                 wprintf("<td>");
388                 wprintf("<select name=\"hfield%d\" size=1>", i);
389                 wprintf("<option value=\"sender\">%s</option>", _("Sender"));
390                 wprintf("<option value=\"recipient\">%s</option>", _("Recipient"));
391                 wprintf("</select>");
392                 wprintf("</td>");
393
394                 wprintf("<td>");
395                 wprintf("<select name=\"compare%d\" size=1>", i);
396                 wprintf("<option value=\"match\">%s</option>", _("matches"));
397                 wprintf("<option value=\"notmatch\">%s</option>", _("does not match"));
398                 wprintf("</select>");
399                 wprintf("</td>");
400
401                 wprintf("<td>");
402                 wprintf("<input type=\"text\" name=\"htext%d\">", i);
403                 wprintf("</td>");
404
405                 wprintf("<td>");
406                 wprintf("<select name=\"action%d\" size=1>", i);
407                 wprintf("<option value=\"fileinto\">%s</option>", _("file into"));
408                 wprintf("<option value=\"redirect\">%s</option>", _("forward to"));
409                 wprintf("<option value=\"reject\">%s</option>", _("reject"));
410                 wprintf("</select>");
411                 wprintf("</td>");
412
413                 wprintf("<td>%s</td>", _("and then") );
414
415                 wprintf("<td>");
416                 wprintf("<select name=\"final%d\" size=1>", i);
417                 wprintf("<option value=\"stop\">%s</option>", _("stop"));
418                 wprintf("<option value=\"continue\">%s</option>", _("continue processing"));
419                 wprintf("</select>");
420                 wprintf("</td>");
421
422                 wprintf("</tr>\n");
423
424         }
425
426         wprintf("</table>");
427
428
429
430 /*
431
432 Show/hide alternating rows.  This is obviously bogus, it's just there to test the show/hide logic.
433
434  */
435
436         wprintf("<script type=\"text/javascript\">                                      \n"
437                 "                                                                       \n"
438                 "function UpdateRules() {                                               \n"
439                 "  for (i=0; i<%d; ++i) {                                               \n", MAX_RULES);
440         wprintf("   if ( (i % 2) == 0 )  {                                              \n"
441                 "     $('rule' + i).style.display = 'block';                            \n"
442                 "   }                                                                   \n"
443                 "   else {                                                              \n"
444                 "     $('rule' + i).style.display = 'none';                             \n"
445                 "   }                                                                   \n"
446                 "  }                                                                    \n"
447                 "}                                                                      \n"
448                 "                                                                       \n"
449                 "UpdateRules();                                                         \n"
450                 "</script>                                                              \n"
451         );
452
453 }
454
455
456
457
458
459
460 /*@}*/