4 * Manage user preferences with a little help from the Citadel server.
13 void load_preferences(void) {
17 serv_printf("GOTO %s", USERCONFIGROOM);
18 serv_getln(buf, sizeof buf);
19 if (buf[0] != '2') return;
21 serv_puts("MSGS ALL|0|1");
22 serv_getln(buf, sizeof buf);
24 serv_puts("subj|__ WebCit Preferences __");
27 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
32 serv_printf("MSG0 %ld", msgnum);
33 serv_getln(buf, sizeof buf);
35 while (serv_getln(buf, sizeof buf),
36 (strcmp(buf, "text") && strcmp(buf, "000"))) {
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, "");
45 WC->preferences = realloc(
47 strlen(WC->preferences)
51 strcat(WC->preferences, buf);
52 strcat(WC->preferences, "\n");
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);
64 * Goto the user's configuration room, creating it if necessary.
65 * Returns 0 on success or nonzero upon failure.
67 int goto_config_room(void) {
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);
83 void save_preferences(void) {
87 if (goto_config_room() != 0) return; /* oh well. */
88 serv_puts("MSGS ALL|0|1");
89 serv_getln(buf, sizeof buf);
91 serv_puts("subj|__ WebCit Preferences __");
94 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
99 serv_printf("DELE %ld", msgnum);
100 serv_getln(buf, sizeof buf);
103 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
104 serv_getln(buf, sizeof buf);
106 serv_puts(WC->preferences);
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);
116 void get_preference(char *key, char *value, size_t value_len) {
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);
134 void set_preference(char *key, char *value, int save_to_server) {
139 char *newprefs = NULL;
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");
157 if (newprefs == NULL) newprefs = strdup("");
158 newprefs = realloc(newprefs, strlen(newprefs) + SIZ);
159 sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
161 free(WC->preferences);
162 WC->preferences = newprefs;
164 if (save_to_server) save_preferences();
171 * display form for changing your preferences and settings
173 void display_preferences(void)
175 output_headers(1, 1, 2, 0, 0, 0, 0);
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\"> Preferences and settings");
182 wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
184 wprintf("</TD></TR></TABLE>\n");
186 "<div id=\"content\">\n");
188 wprintf("<div id=\"fix_scrollbar_bug\">"
189 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
193 "<form name=\"prefform\" action=\"set_preferences\" "
195 "<table border=0 cellspacing=5 cellpadding=5>\n");
198 get_preference("roomlistview", buf, sizeof buf);
199 wprintf("<tr><td>Room list view</td><td>");
201 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
202 if (!strcasecmp(buf, "folders")) wprintf(" checked");
203 wprintf(">Tree (folders) view<br></input>\n");
205 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
206 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
207 wprintf(">Table (rooms) view<br></input>\n");
209 wprintf("</td></tr>\n");
212 get_preference("calhourformat", buf, sizeof buf);
213 if (buf[0] == 0) strcpy(buf, "12");
214 wprintf("<tr><td>Calendar hour format</td><td>");
216 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
217 if (!strcasecmp(buf, "12")) wprintf(" checked");
218 wprintf(">12 hour (am/pm)<br></input>\n");
220 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
221 if (!strcasecmp(buf, "24")) wprintf(" checked");
222 wprintf(">24 hour<br></input>\n");
224 wprintf("</td></tr>\n");
227 "<input type=\"submit\" name=\"action\" value=\"Change\">"
229 "<INPUT type=\"submit\" name=\"action\" value=\"Cancel\">\n");
231 wprintf("</form></center>\n");
236 wprintf("</td></tr></table></div>\n");
241 * Commit new preferences and settings
243 void set_preferences(void)
245 if (strcmp(bstr("action"), "Change")) {
246 safestrncpy(WC->ImportantMessage,
247 "Cancelled. No settings were changed.",
248 sizeof WC->ImportantMessage);
253 /* Set the last argument to 1 only for the final setting, so
254 * we don't send the prefs file to the server repeatedly
256 set_preference("roomlistview", bstr("roomlistview"), 0);
257 set_preference("calhourformat", bstr("calhourformat"), 1);