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