]> code.citadel.org Git - citadel.git/blob - webcit/preferences.c
* Began implementing the "signature" feature. It doesn't work yet.
[citadel.git] / webcit / preferences.c
1 /*
2  * $Id$
3  *
4  * Manage user preferences with a little help from the Citadel server.
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10
11
12
13 void load_preferences(void) {
14         char buf[SIZ];
15         long msgnum = 0L;
16
17         serv_printf("GOTO %s", USERCONFIGROOM);
18         serv_getln(buf, sizeof buf);
19         if (buf[0] != '2') return;
20         
21         serv_puts("MSGS ALL|0|1");
22         serv_getln(buf, sizeof buf);
23         if (buf[0] == '8') {
24                 serv_puts("subj|__ WebCit Preferences __");
25                 serv_puts("000");
26         }
27         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
28                 msgnum = atol(buf);
29         }
30
31         if (msgnum > 0L) {
32                 serv_printf("MSG0 %ld", msgnum);
33                 serv_getln(buf, sizeof buf);
34                 if (buf[0] == '1') {
35                         while (serv_getln(buf, sizeof buf),
36                                 (strcmp(buf, "text") && strcmp(buf, "000"))) {
37                         }
38                         if (!strcmp(buf, "text")) {
39                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
40                                         if (WC->preferences == NULL) {
41                                                 WC->preferences = malloc(SIZ);
42                                                 strcpy(WC->preferences, "");
43                                         }
44                                         else {
45                                                 WC->preferences = realloc(
46                                                         WC->preferences,
47                                                         strlen(WC->preferences)
48                                                         +SIZ
49                                                 );
50                                         }
51                                         strcat(WC->preferences, buf);
52                                         strcat(WC->preferences, "\n");
53                                 }
54                         }
55                 }
56         }
57
58         /* Go back to the room we're supposed to be in */
59         serv_printf("GOTO %s", WC->wc_roomname);
60         serv_getln(buf, sizeof buf);
61 }
62
63 /*
64  * Goto the user's configuration room, creating it if necessary.
65  * Returns 0 on success or nonzero upon failure.
66  */
67 int goto_config_room(void) {
68         char buf[SIZ];
69
70         serv_printf("GOTO %s", USERCONFIGROOM);
71         serv_getln(buf, sizeof buf);
72         if (buf[0] != '2') { /* try to create the config room if not there */
73                 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
74                 serv_getln(buf, sizeof buf);
75                 serv_printf("GOTO %s", USERCONFIGROOM);
76                 serv_getln(buf, sizeof buf);
77                 if (buf[0] != '2') return(1);
78         }
79         return(0);
80 }
81
82
83 void save_preferences(void) {
84         char buf[SIZ];
85         long msgnum = 0L;
86
87         if (goto_config_room() != 0) return;    /* oh well. */
88         serv_puts("MSGS ALL|0|1");
89         serv_getln(buf, sizeof buf);
90         if (buf[0] == '8') {
91                 serv_puts("subj|__ WebCit Preferences __");
92                 serv_puts("000");
93         }
94         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
95                 msgnum = atol(buf);
96         }
97
98         if (msgnum > 0L) {
99                 serv_printf("DELE %ld", msgnum);
100                 serv_getln(buf, sizeof buf);
101         }
102
103         serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
104         serv_getln(buf, sizeof buf);
105         if (buf[0] == '4') {
106                 serv_puts(WC->preferences);
107                 serv_puts("");
108                 serv_puts("000");
109         }
110
111         /* Go back to the room we're supposed to be in */
112         serv_printf("GOTO %s", WC->wc_roomname);
113         serv_getln(buf, sizeof buf);
114 }
115
116 void get_preference(char *key, char *value, size_t value_len) {
117         int num_prefs;
118         int i;
119         char buf[SIZ];
120         char thiskey[SIZ];
121
122         strcpy(value, "");
123
124         num_prefs = num_tokens(WC->preferences, '\n');
125         for (i=0; i<num_prefs; ++i) {
126                 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
127                 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
128                 if (!strcasecmp(thiskey, key)) {
129                         extract_token(value, buf, 1, '|', value_len);
130                 }
131         }
132 }
133
134 void set_preference(char *key, char *value, int save_to_server) {
135         int num_prefs;
136         int i;
137         char buf[SIZ];
138         char thiskey[SIZ];
139         char *newprefs = NULL;
140
141         num_prefs = num_tokens(WC->preferences, '\n');
142         for (i=0; i<num_prefs; ++i) {
143                 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
144                 if (num_tokens(buf, '|') == 2) {
145                         extract_token(thiskey, buf, 0, '|', sizeof thiskey);
146                         if (strcasecmp(thiskey, key)) {
147                                 if (newprefs == NULL) newprefs = strdup("");
148                                 newprefs = realloc(newprefs,
149                                         strlen(newprefs) + SIZ );
150                                 strcat(newprefs, buf);
151                                 strcat(newprefs, "\n");
152                         }
153                 }
154         }
155
156
157         if (newprefs == NULL) newprefs = strdup("");
158         newprefs = realloc(newprefs, strlen(newprefs) + SIZ);
159         sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
160
161         free(WC->preferences);
162         WC->preferences = newprefs;
163
164         if (save_to_server) save_preferences();
165 }
166
167
168
169
170 /* 
171  * display form for changing your preferences and settings
172  */
173 void display_preferences(void)
174 {
175         output_headers(1, 1, 2, 0, 0, 0);
176         char buf[256];
177
178         wprintf("<div id=\"banner\">\n");
179         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
180         wprintf("<IMG SRC=\"/static/advanpage2_48x.gif\" ALT=\" \" ALIGN=MIDDLE>");
181         wprintf("<SPAN CLASS=\"titlebar\">&nbsp;");
182         wprintf(_("Preferences and settings"));
183         wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
184         offer_start_page();
185         wprintf("</TD></TR></TABLE>\n");
186         wprintf("</div>\n"
187                 "<div id=\"content\">\n");
188
189         wprintf("<div id=\"fix_scrollbar_bug\">"
190                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
191
192         /* begin form */
193         wprintf("<center>\n"
194                 "<form name=\"prefform\" action=\"set_preferences\" "
195                 "method=\"post\">\n"
196                 "<table border=0 cellspacing=5 cellpadding=5>\n");
197
198         /*
199          * Room list view
200          */
201         get_preference("roomlistview", buf, sizeof buf);
202         wprintf("<tr><td>");
203         wprintf(_("Room list view"));
204         wprintf("</td><td>");
205
206         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
207         if (!strcasecmp(buf, "folders")) wprintf(" checked");
208         wprintf(">");
209         wprintf(_("Tree (folders) view"));
210         wprintf("<br></input>\n");
211
212         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
213         if (!strcasecmp(buf, "rooms")) wprintf(" checked");
214         wprintf(">");
215         wprintf(_("Table (rooms) view"));
216         wprintf("<br></input>\n");
217
218         wprintf("</td></tr>\n");
219
220         /*
221          * Calendar hour format
222          */
223         get_preference("calhourformat", buf, sizeof buf);
224         if (buf[0] == 0) strcpy(buf, "12");
225         wprintf("<tr><td>");
226         wprintf(_("Calendar hour format"));
227         wprintf("</td><td>");
228
229         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
230         if (!strcasecmp(buf, "12")) wprintf(" checked");
231         wprintf(">");
232         wprintf(_("12 hour (am/pm)"));
233         wprintf("<br></input>\n");
234
235         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
236         if (!strcasecmp(buf, "24")) wprintf(" checked");
237         wprintf(">");
238         wprintf(_("24 hour"));
239         wprintf("<br></input>\n");
240
241         wprintf("</td></tr>\n");
242
243         /*
244          * Calendar hour format
245          */
246         get_preference("use_sig", buf, sizeof buf);
247         if (buf[0] == 0) strcpy(buf, "no");
248         wprintf("<tr><td>");
249         wprintf(_("Attach signature to email messages?"));
250         wprintf("</td><td>");
251
252         wprintf("<input type=\"radio\" name=\"use_sig\" VALUE=\"no\"");
253         if (!strcasecmp(buf, "no")) wprintf(" checked");
254         wprintf(">");
255         wprintf(_("No signature"));
256         wprintf("<br></input>\n");
257
258         wprintf("<input type=\"radio\" name=\"use_sig\" VALUE=\"yes\"");
259         if (!strcasecmp(buf, "yes")) wprintf(" checked");
260         wprintf(">");
261         wprintf(_("Use this signature:"));
262         wprintf("<div id=\"signature_box\">"
263                 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
264         );
265         get_preference("signature", buf, sizeof buf);
266         msgescputs(buf);
267         wprintf("</textarea>"
268                 "</div>"
269         );
270
271         wprintf("<br></input>\n");
272
273         wprintf("</td></tr>\n");
274
275
276
277         wprintf("</table>\n"
278                 "<input type=\"submit\" name=\"change_button\" value=\"%s\">"
279                 "&nbsp;"
280                 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
281                 _("Change"),
282                 _("Cancel")
283         );
284
285         wprintf("</form></center>\n");
286
287         /* end form */
288
289
290         wprintf("</td></tr></table></div>\n");
291         wDumpContent(1);
292 }
293
294 /*
295  * Commit new preferences and settings
296  */
297 void set_preferences(void)
298 {
299         if (strlen(bstr("change_button")) == 0) {
300                 safestrncpy(WC->ImportantMessage, 
301                         _("Cancelled.  No settings were changed."),
302                         sizeof WC->ImportantMessage);
303                 display_main_menu();
304                 return;
305         }
306
307         /* Set the last argument to 1 only for the final setting, so
308          * we don't send the prefs file to the server repeatedly
309          */
310         set_preference("roomlistview", bstr("roomlistview"), 0);
311         set_preference("calhourformat", bstr("calhourformat"), 0);
312         set_preference("use_sig", bstr("use_sig"), 0);
313         set_preference("signature", bstr("signature"), 1);
314
315         display_main_menu();
316 }