4 * Manage user preferences with a little help from the Citadel server.
14 #include <sys/types.h>
16 #include <sys/socket.h>
19 #include <netinet/in.h>
28 #include "webserver.h"
32 void load_preferences(void) {
36 serv_printf("GOTO %s", USERCONFIGROOM);
37 serv_getln(buf, sizeof buf);
38 if (buf[0] != '2') return;
40 serv_puts("MSGS ALL|0|1");
41 serv_getln(buf, sizeof buf);
43 serv_puts("subj|__ WebCit Preferences __");
46 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
51 serv_printf("MSG0 %ld", msgnum);
52 serv_getln(buf, sizeof buf);
54 while (serv_getln(buf, sizeof buf),
55 (strcmp(buf, "text") && strcmp(buf, "000"))) {
57 if (!strcmp(buf, "text")) {
58 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
59 if (WC->preferences == NULL) {
60 WC->preferences = malloc(SIZ);
61 strcpy(WC->preferences, "");
64 WC->preferences = realloc(
66 strlen(WC->preferences)
70 strcat(WC->preferences, buf);
71 strcat(WC->preferences, "\n");
77 /* Go back to the room we're supposed to be in */
78 serv_printf("GOTO %s", WC->wc_roomname);
79 serv_getln(buf, sizeof buf);
83 * Goto the user's configuration room, creating it if necessary.
84 * Returns 0 on success or nonzero upon failure.
86 int goto_config_room(void) {
89 serv_printf("GOTO %s", USERCONFIGROOM);
90 serv_getln(buf, sizeof buf);
91 if (buf[0] != '2') { /* try to create the config room if not there */
92 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
93 serv_getln(buf, sizeof buf);
94 serv_printf("GOTO %s", USERCONFIGROOM);
95 serv_getln(buf, sizeof buf);
96 if (buf[0] != '2') return(1);
102 void save_preferences(void) {
106 if (goto_config_room() != 0) return; /* oh well. */
107 serv_puts("MSGS ALL|0|1");
108 serv_getln(buf, sizeof buf);
110 serv_puts("subj|__ WebCit Preferences __");
113 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
118 serv_printf("DELE %ld", msgnum);
119 serv_getln(buf, sizeof buf);
122 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
123 serv_getln(buf, sizeof buf);
125 serv_puts(WC->preferences);
130 /* Go back to the room we're supposed to be in */
131 serv_printf("GOTO %s", WC->wc_roomname);
132 serv_getln(buf, sizeof buf);
135 void get_preference(char *key, char *value, size_t value_len) {
143 num_prefs = num_tokens(WC->preferences, '\n');
144 for (i=0; i<num_prefs; ++i) {
145 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
146 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
147 if (!strcasecmp(thiskey, key)) {
148 extract_token(value, buf, 1, '|', value_len);
153 void set_preference(char *key, char *value, int save_to_server) {
158 char *newprefs = NULL;
160 num_prefs = num_tokens(WC->preferences, '\n');
161 for (i=0; i<num_prefs; ++i) {
162 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
163 if (num_tokens(buf, '|') == 2) {
164 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
165 if (strcasecmp(thiskey, key)) {
166 if (newprefs == NULL) newprefs = strdup("");
167 newprefs = realloc(newprefs,
168 strlen(newprefs) + SIZ );
169 strcat(newprefs, buf);
170 strcat(newprefs, "\n");
176 if (newprefs == NULL) newprefs = strdup("");
177 newprefs = realloc(newprefs, strlen(newprefs) + SIZ);
178 sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
180 free(WC->preferences);
181 WC->preferences = newprefs;
183 if (save_to_server) save_preferences();
190 * display form for changing your preferences and settings
192 void display_preferences(void)
194 output_headers(1, 1, 2, 0, 0, 0, 0);
197 wprintf("<div id=\"banner\">\n");
198 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
199 wprintf("<IMG SRC=\"/static/advanpage2_48x.gif\" ALT=\" \" ALIGN=MIDDLE>");
200 wprintf("<SPAN CLASS=\"titlebar\"> Preferences and settings");
201 wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
203 wprintf("</TD></TR></TABLE>\n");
205 "<div id=\"content\">\n");
207 wprintf("<div id=\"fix_scrollbar_bug\">"
208 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
212 "<form name=\"prefform\" action=\"set_preferences\" "
214 "<table border=0 cellspacing=5 cellpadding=5>\n");
217 get_preference("roomlistview", buf, sizeof buf);
218 wprintf("<tr><td>Room list view</td><td>");
220 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
221 if (!strcasecmp(buf, "folders")) wprintf(" checked");
222 wprintf(">Tree (folders) view<br></input>\n");
224 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
225 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
226 wprintf(">Table (rooms) view<br></input>\n");
228 wprintf("</td></tr>\n");
231 get_preference("calhourformat", buf, sizeof buf);
232 if (buf[0] == 0) strcpy(buf, "12");
233 wprintf("<tr><td>Calendar hour format</td><td>");
235 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
236 if (!strcasecmp(buf, "12")) wprintf(" checked");
237 wprintf(">12 hour (am/pm)<br></input>\n");
239 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
240 if (!strcasecmp(buf, "24")) wprintf(" checked");
241 wprintf(">24 hour<br></input>\n");
243 wprintf("</td></tr>\n");
246 "<input type=\"submit\" name=\"action\" value=\"Change\">"
248 "<INPUT type=\"submit\" name=\"action\" value=\"Cancel\">\n");
250 wprintf("</form></center>\n");
255 wprintf("</td></tr></table></div>\n");
260 * Commit new preferences and settings
262 void set_preferences(void)
264 if (strcmp(bstr("action"), "Change")) {
265 safestrncpy(WC->ImportantMessage,
266 "Cancelled. No settings were changed.",
267 sizeof WC->ImportantMessage);
272 /* Set the last argument to 1 only for the final setting, so
273 * we don't send the prefs file to the server repeatedly
275 set_preference("roomlistview", bstr("roomlistview"), 0);
276 set_preference("calhourformat", bstr("calhourformat"), 1);