Add/remove sieve scripts in WebCit is now complete.
[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
13 /**
14  * \brief view/edit sieve config
15  */
16 void display_sieve(void)
17 {
18         char script_names[MAX_SCRIPTS][64];
19         int num_scripts = 0;
20         int active_script = (-1);
21         char buf[256];
22         int i;
23         
24
25         memset(script_names, 0, sizeof script_names);
26
27         serv_puts("MSIV listscripts");
28         serv_getln(buf, sizeof(buf));
29         if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
30                 if (num_scripts < MAX_SCRIPTS) {
31                         extract_token(script_names[num_scripts], buf, 0, '|', 64);
32                         if (extract_int(buf, 1) > 0) {
33                                 active_script = num_scripts;
34                         }
35                         ++num_scripts;
36                 }
37         }
38
39         output_headers(1, 1, 2, 0, 0, 0);
40
41         wprintf("<script type=\"text/javascript\">                                      \n"
42                 "                                                                       \n"
43                 "var previously_active_script;                                          \n"
44                 "                                                                       \n"
45                 "function ToggleSievePanels() {                                         \n"
46                 " d = ($('sieveform').bigaction.options[$('sieveform').bigaction.selectedIndex].value); \n"
47                 " for (i=0; i<3; ++i) {                                                 \n"
48                 "  if (i == d) {                                                        \n"
49                 "   $('sievediv' + i).style.display = 'block';                          \n"
50                 "  }                                                                    \n"
51                 "  else {                                                               \n"
52                 "   $('sievediv' + i).style.display = 'none';                           \n"
53                 "  }                                                                    \n"
54                 " }                                                                     \n"
55                 "}                                                                      \n"
56                 "                                                                       \n"
57                 "function ToggleScriptPanels() {                                        \n"
58                 " d = ($('sieveform').active_script.options[$('sieveform').active_script.selectedIndex].value); \n"
59                 " if ($('script_' + previously_active_script)) {                        \n"
60                 "  $('script_' + previously_active_script).style.display = 'none';      \n"
61                 " }                                                                     \n"
62                 " $('script_' + d).style.display = 'block';                             \n"
63                 " previously_active_script = d;                                         \n"
64                 "}                                                                      \n"
65                 "                                                                       \n"
66                 "</script>                                                              \n"
67         );
68
69         wprintf("<div id=\"banner\">\n");
70         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
71         wprintf("<SPAN CLASS=\"titlebar\">");
72         wprintf(_("View/edit server-side mail filters"));
73         wprintf("</SPAN>\n");
74         wprintf("</TD></TR></TABLE>\n");
75         wprintf("</div>\n<div id=\"content\">\n");
76
77         wprintf("<div class=\"fix_scrollbar_bug\">"
78                 "<table border=0 width=100%% bgcolor=\"#FFFFFF\">"
79                 "<tr><td valign=top>\n");
80
81
82         wprintf("<form id=\"sieveform\" method=\"post\" action=\"save_sieve\">\n");
83
84         wprintf(_("When new mail arrives: "));
85         wprintf("<select name=\"bigaction\" size=1 onChange=\"ToggleSievePanels();\">\n");
86
87         wprintf("<option %s value=\"0\">", ((active_script < 0) ? "selected" : ""));
88         wprintf(_("Leave it in my inbox without filtering"));
89         wprintf("</option>\n");
90
91         /*
92          *      FIXME uncomment this when we write the rules editor
93          *
94         wprintf("<option value=\"1\">");
95         wprintf(_("Filter it according to rules selected below"));
96         wprintf("</option>\n");
97          *
98          */
99
100         wprintf("<option %s value=\"2\">", ((active_script >= 0) ? "selected" : ""));
101         wprintf(_("Filter it through a manually edited script (advanced users only)"));
102         wprintf("</option>\n");
103
104         wprintf("</select>");
105
106
107
108         /* The "no filtering" div */
109
110         wprintf("<div id=\"sievediv0\" style=\"display:none\">\n");
111         wprintf("<div align=\"center\"><br /><br />");
112         wprintf(_("Your incoming mail will not be filtered through any scripts."));
113         wprintf("<br /><br /></div>\n");
114         wprintf("</div>\n");
115
116         /* The "webcit managed scripts" div */
117
118         wprintf("<div id=\"sievediv1\" style=\"display:none\">\n");
119         wprintf("<div align=\"center\"><br /><br />");
120         wprintf("FIXME");
121         wprintf("<br /><br /></div>\n");
122         wprintf("</div>\n");
123
124         /* The "I'm smart and can write my own Sieve scripts" div */
125
126         wprintf("<div id=\"sievediv2\" style=\"display:none\">\n");
127
128         if (num_scripts > 0) {
129                 wprintf(_("The currently active script is: "));
130                 wprintf("<select name=\"active_script\" size=1 onChange=\"ToggleScriptPanels();\">\n");
131                 for (i=0; i<num_scripts; ++i) {
132                         wprintf("<option %s value=\"%s\">%s</option>\n",
133                                 ((active_script == i) ? "selected" : ""),
134                                 script_names[i],
135                                 script_names[i]
136                         );
137                 }
138                 wprintf("</select>\n");
139         }
140
141         wprintf("&nbsp;&nbsp;&nbsp;");
142         wprintf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add or delete scripts"));
143
144         wprintf("<br />\n");
145
146         if (num_scripts > 0) {
147                 for (i=0; i<num_scripts; ++i) {
148                         wprintf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
149                         wprintf("<textarea name=\"text_%s\" wrap=soft rows=20 cols=80 width=80>\n", script_names[i]);
150                         serv_printf("MSIV getscript|%s", script_names[i]);
151                         serv_getln(buf, sizeof buf);
152                         if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
153                                 wprintf("%s\n", buf);
154                         }
155                         wprintf("</textarea>\n");
156                         wprintf("</div>\n");
157                 }
158         }
159
160         wprintf("<script type=\"text/javascript\">      \n"
161                 "ToggleScriptPanels();                  \n"
162                 "</script>                              \n"
163         );
164
165         wprintf("</div>\n");
166
167
168         /* The rest of this is common for all panels... */
169
170         wprintf("<div align=\"center\"><br>");
171         wprintf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
172         wprintf("&nbsp;");
173         wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
174         wprintf("</div></form>\n");
175
176         wprintf("</td></tr></table></div>\n");
177
178         wprintf("<script type=\"text/javascript\">      \n"
179                 "ToggleSievePanels();                   \n"
180                 "</script>                              \n"
181         );
182
183         wDumpContent(1);
184
185 }
186
187
188 /**
189  * \brief save sieve config
190  */
191 void save_sieve(void) {
192         int bigaction;
193         char script_names[MAX_SCRIPTS][64];
194         int num_scripts = 0;
195         int active_script = (-1);
196         int i;
197         char this_name[64];
198         char buf[256];
199
200         if (strlen(bstr("save_button")) == 0) {
201                 strcpy(WC->ImportantMessage,
202                         _("Cancelled.  Changes were not saved."));
203                 display_main_menu();
204                 return;
205         }
206
207         serv_puts("MSIV listscripts");
208         serv_getln(buf, sizeof(buf));
209         if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
210                 if (num_scripts < MAX_SCRIPTS) {
211                         extract_token(script_names[num_scripts], buf, 0, '|', 64);
212                         if (extract_int(buf, 1) > 0) {
213                                 active_script = num_scripts;
214                         }
215                         ++num_scripts;
216                 }
217         }
218
219         bigaction = atoi(bstr("bigaction"));
220
221         if (bigaction == 0) {
222                 serv_puts("MSIV setactive||");
223                 serv_getln(buf, sizeof buf);
224         }
225
226         else if (bigaction == 2) {
227                 serv_printf("MSIV setactive|%s|", bstr("active_script"));
228                 serv_getln(buf, sizeof buf);
229         }
230
231         if (num_scripts > 0) {
232                 for (i=0; i<num_scripts; ++i) {
233                         serv_printf("MSIV putscript|%s|", script_names[i]);
234                         serv_getln(buf, sizeof buf);
235                         if (buf[0] == '4') {
236                                 snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
237                                 striplt(bstr(this_name));
238                                 serv_printf("%s", bstr(this_name));
239                                 serv_puts("000");
240                         }
241                 }
242         }
243
244         strcpy(WC->ImportantMessage, _("Your changes have been saved."));
245         display_main_menu();
246         return;
247 }
248
249
250 /**
251  * \brief show a list of available scripts to add/remove them
252  */
253 void display_add_remove_scripts(char *message)
254 {
255         char buf[256];
256         char script_name[256];
257
258         output_headers(1, 1, 2, 0, 0, 0);
259         wprintf("<div id=\"banner\">\n");
260         wprintf("<table width=100%% border=0 bgcolor=#444455><tr>"
261                 "<td>"
262                 "<span class=\"titlebar\">"
263                 "<img src=\"static/usermanag_48x.gif\">");
264         wprintf(_("Add/remove Sieve scripts"));
265         wprintf("</span></td></tr></table>\n"
266                 "</div>\n<div id=\"content\">\n"
267         );
268
269         if (message != NULL) wprintf(message);
270
271         wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
272
273         svprintf("BOXTITLE", WCS_STRING, _("Add a new script"));
274         do_template("beginbox");
275
276         wprintf(_("To create a new script, enter the desired "
277                 "script name in the box below and click 'Create'."));
278         wprintf("<br /><br />");
279
280         wprintf("<center><form method=\"POST\" action=\"create_script\">\n");
281         wprintf(_("Script name: "));
282         wprintf("<input type=\"text\" name=\"script_name\"><br />\n"
283                 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
284                 "</form></center>\n", _("Create"));
285
286         do_template("endbox");
287
288         svprintf("BOXTITLE", WCS_STRING, _("Edit scripts"));
289         do_template("beginbox");
290         wprintf("<br /><div align=center><a href=\"display_sieve\">%s</a><br /><br />\n",
291                 _("Return to the script editing screen")
292         );
293         do_template("endbox");
294
295         wprintf("</td><td>");
296
297         svprintf("BOXTITLE", WCS_STRING, _("Delete scripts"));
298         do_template("beginbox");
299
300         wprintf(_("To delete an existing script, select the script "
301                 "name from the list and click 'Delete'."));
302         wprintf("<br /><br />");
303         
304         wprintf("<center>"
305                 "<form method=\"POST\" action=\"delete_script\">\n");
306         wprintf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
307
308         serv_puts("MSIV listscripts");
309         serv_getln(buf, sizeof buf);
310         if (buf[0] == '1') {
311                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
312                         extract_token(script_name, buf, 0, '|', sizeof script_name);
313                         if (extract_int(buf, 1) == 0) {
314                                 wprintf("<option>");
315                                 escputs(script_name);
316                                 wprintf("</option>\n");
317                         }
318                 }
319         }
320         wprintf("</select><br />\n");
321
322         wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
323                 "onClick=\"return confirm('%s');\">", _("Delete script"), _("Delete this script?"));
324         wprintf("</form></center>\n");
325         do_template("endbox");
326
327         wprintf("</td></tr></table>\n");
328
329         wDumpContent(1);
330 }
331
332
333
334 /**
335  * \brief delete a script
336  */
337 void delete_script(void) {
338         char buf[256];
339
340         serv_printf("MSIV deletescript|%s", bstr("script_name"));
341         serv_getln(buf, sizeof buf);
342         display_add_remove_scripts(&buf[4]);
343 }
344                 
345
346
347 /**
348  * \brief create a new script
349  * take the web environment script name and create it on the citadel server
350  */
351 void create_script(void) {
352         char buf[256];
353
354         serv_printf("MSIV getscript|%s", bstr("script_name"));
355         serv_getln(buf, sizeof buf);
356         if (buf[0] == '1') {
357                 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
358                         /* flush */
359                 }
360                 display_add_remove_scripts(_("A script by that name already exists."));
361                 return;
362         }
363         
364         serv_printf("MSIV putscript|%s", bstr("script_name"));
365         serv_getln(buf, sizeof buf);
366         if (buf[0] == '4') {
367                 serv_puts("keep;");
368                 serv_puts("000");
369                 display_add_remove_scripts(_("A new script has been created.  Return to the script editing screen to edit and activate it."));
370                 return;
371         }
372
373         display_add_remove_scripts(&buf[4]);
374 }
375
376 /*@}*/