started add/remove scripts
[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         wprintf("<option value=\"1\">");
92         wprintf(_("Filter it according to rules selected below"));
93         wprintf("</option>\n");
94
95         wprintf("<option %s value=\"2\">", ((active_script >= 0) ? "selected" : ""));
96         wprintf(_("Filter it through a manually edited script (advanced users only)"));
97         wprintf("</option>\n");
98
99         wprintf("</select>");
100
101
102
103         /* The "no filtering" div */
104
105         wprintf("<div id=\"sievediv0\" style=\"display:none\">\n");
106         wprintf("<div align=\"center\"><br /><br />");
107         wprintf(_("Your incoming mail will not be filtered through any scripts."));
108         wprintf("<br /><br /></div>\n");
109         wprintf("</div>\n");
110
111         /* The "webcit managed scripts" div */
112
113         wprintf("<div id=\"sievediv1\" style=\"display:none\">\n");
114         wprintf("<div align=\"center\"><br /><br />");
115         wprintf("FIXME div 1 isn't finished yet");
116         wprintf("<br /><br /></div>\n");
117         wprintf("</div>\n");
118
119
120         /* The "I'm smart and can write my own Sieve scripts" div */
121
122         wprintf("<div id=\"sievediv2\" style=\"display:none\">\n");
123
124         if (num_scripts > 0) {
125                 wprintf(_("The currently active script is: "));
126                 wprintf("<select name=\"active_script\" size=1 onChange=\"ToggleScriptPanels();\">\n");
127                 for (i=0; i<num_scripts; ++i) {
128                         wprintf("<option %s value=\"%s\">%s</option>\n",
129                                 ((active_script == i) ? "selected" : ""),
130                                 script_names[i],
131                                 script_names[i]
132                         );
133                 }
134                 wprintf("</select>\n");
135         }
136
137         wprintf("&nbsp;&nbsp;&nbsp;");
138         wprintf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add/remove scripts"));
139
140         wprintf("<br />\n");
141
142         if (num_scripts > 0) {
143                 for (i=0; i<num_scripts; ++i) {
144                         wprintf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
145                         wprintf("<textarea name=\"text_%s\" wrap=soft rows=20 cols=80 width=80>\n", script_names[i]);
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);
150                         }
151                         wprintf("</textarea>\n");
152                         wprintf("</div>\n");
153                 }
154         }
155
156         wprintf("<script type=\"text/javascript\">      \n"
157                 "ToggleScriptPanels();                  \n"
158                 "</script>                              \n"
159         );
160
161         wprintf("</div>\n");
162
163
164         /* The rest of this is common for all panels... */
165
166         wprintf("<div align=\"center\"><br>");
167         wprintf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
168         wprintf("&nbsp;");
169         wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
170         wprintf("</div></form>\n");
171
172         wprintf("</td></tr></table></div>\n");
173
174         wprintf("<script type=\"text/javascript\">      \n"
175                 "ToggleSievePanels();                   \n"
176                 "</script>                              \n"
177         );
178
179         wDumpContent(1);
180
181 }
182
183
184 /**
185  * \brief save sieve config
186  */
187 void save_sieve(void) {
188         int bigaction;
189         char script_names[MAX_SCRIPTS][64];
190         int num_scripts = 0;
191         int active_script = (-1);
192         int i;
193         char this_name[64];
194         char buf[256];
195
196         if (strlen(bstr("save_button")) == 0) {
197                 strcpy(WC->ImportantMessage,
198                         _("Cancelled.  Changes were not saved."));
199                 display_main_menu();
200                 return;
201         }
202
203         serv_puts("MSIV listscripts");
204         serv_getln(buf, sizeof(buf));
205         if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
206                 if (num_scripts < MAX_SCRIPTS) {
207                         extract_token(script_names[num_scripts], buf, 0, '|', 64);
208                         if (extract_int(buf, 1) > 0) {
209                                 active_script = num_scripts;
210                         }
211                         ++num_scripts;
212                 }
213         }
214
215         bigaction = atoi(bstr("bigaction"));
216
217         if (bigaction == 0) {
218                 serv_puts("MSIV setactive||");
219                 serv_getln(buf, sizeof buf);
220         }
221
222         else if (bigaction == 2) {
223                 serv_printf("MSIV setactive|%s|", bstr("active_script"));
224                 serv_getln(buf, sizeof buf);
225         }
226
227         if (num_scripts > 0) {
228                 for (i=0; i<num_scripts; ++i) {
229                         serv_printf("MSIV putscript|%s|", script_names[i]);
230                         serv_getln(buf, sizeof buf);
231                         if (buf[0] == '4') {
232                                 snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
233                                 striplt(bstr(this_name));
234                                 serv_printf("%s", bstr(this_name));
235                                 serv_puts("000");
236                         }
237                 }
238         }
239
240         strcpy(WC->ImportantMessage, _("Your changes have been saved."));
241         display_main_menu();
242         return;
243 }
244
245
246 void display_add_remove_scripts(void) {
247         output_headers(1, 1, 2, 0, 0, 0);
248
249         wprintf("<div id=\"banner\">\n");
250         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
251         wprintf("<SPAN CLASS=\"titlebar\">");
252         wprintf(_("Add/remove Sieve scripts"));
253         wprintf("</SPAN>\n");
254         wprintf("</TD></TR></TABLE>\n");
255         wprintf("</div>\n<div id=\"content\">\n");
256
257         wprintf("<div class=\"fix_scrollbar_bug\">"
258                 "<table border=0 width=100%% bgcolor=\"#FFFFFF\">"
259                 "<tr><td valign=top>\n");
260
261
262         /* blah blah go here FIXME */
263
264         wprintf("</td></tr></table></div>\n");
265
266         wprintf("<script type=\"text/javascript\">      \n"
267                 "ToggleSievePanels();                   \n"
268                 "</script>                              \n"
269         );
270
271         wDumpContent(1);
272
273 }
274
275 /*@}*/