f8f90085e489b647c8f9c2c00b8303284f77bca5
[citadel.git] / webcit / webcit.c
1 /*
2  * $Id$
3  *
4  * This is the main transaction loop of the web service.  It maintains a
5  * persistent session to the Citadel server, handling HTTP WebCit requests as
6  * they arrive and presenting a user interface.
7  *
8  */
9
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/socket.h>
19 #include <sys/time.h>
20 #include <sys/stat.h>
21 #include <limits.h>
22 #include <netinet/in.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include "webcit.h"
31 #include "groupdav.h"
32 #include "webserver.h"
33 #include "mime_parser.h"
34
35 /*
36  * String to unset the cookie.
37  * Any date "in the past" will work, so I chose my birthday, right down to
38  * the exact minute.  :)
39  */
40 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
41
42 void unescape_input(char *buf)
43 {
44         int a, b;
45         char hex[3];
46
47         while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
48                 buf[strlen(buf) - 1] = 0;
49
50         for (a = 0; a < strlen(buf); ++a) {
51                 if (buf[a] == '+')
52                         buf[a] = ' ';
53                 if (buf[a] == '%') {
54                         hex[0] = buf[a + 1];
55                         hex[1] = buf[a + 2];
56                         hex[2] = 0;
57                         b = 0;
58                         sscanf(hex, "%02x", &b);
59                         buf[a] = (char) b;
60                         strcpy(&buf[a + 1], &buf[a + 3]);
61                 }
62         }
63
64 }
65
66
67 void addurls(char *url)
68 {
69         char *up, *ptr;
70         char buf[SIZ];
71         int a, b;
72         struct urlcontent *u;
73
74         up = url;
75         while (strlen(up) > 0) {
76
77                 /* locate the = sign */
78                 safestrncpy(buf, up, sizeof buf);
79                 b = (-1);
80                 for (a = 255; a >= 0; --a)
81                         if (buf[a] == '=')
82                                 b = a;
83                 if (b < 0)
84                         return;
85                 buf[b] = 0;
86
87                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
88                 u->next = WC->urlstrings;
89                 WC->urlstrings = u;
90                 safestrncpy(u->url_key, buf, sizeof u->url_key);
91
92                 /* now chop that part off */
93                 for (a = 0; a <= b; ++a)
94                         ++up;
95
96                 /* locate "&" and "?" delimiters */
97                 ptr = up;
98                 b = strlen(up);
99                 for (a = 0; a < strlen(up); ++a) {
100                         if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
101                                 b = a;
102                                 break;
103                         }
104                         ++ptr;
105                 }
106                 ptr = up;
107                 for (a = 0; a < b; ++a)
108                         ++ptr;
109                 strcpy(ptr, "");
110
111                 u->url_data = malloc(strlen(up) + 2);
112                 safestrncpy(u->url_data, up, strlen(up) + 1);
113                 u->url_data[b] = 0;
114                 unescape_input(u->url_data);
115                 up = ptr;
116                 ++up;
117         }
118 }
119
120 void free_urls(void)
121 {
122         struct urlcontent *u;
123
124         while (WC->urlstrings != NULL) {
125                 free(WC->urlstrings->url_data);
126                 u = WC->urlstrings->next;
127                 free(WC->urlstrings);
128                 WC->urlstrings = u;
129         }
130 }
131
132 /*
133  * Diagnostic function to display the contents of all variables
134  */
135 void dump_vars(void)
136 {
137         struct urlcontent *u;
138
139         for (u = WC->urlstrings; u != NULL; u = u->next) {
140                 wprintf("%38s = %s\n", u->url_key, u->url_data);
141         }
142 }
143
144 char *bstr(char *key)
145 {
146         struct urlcontent *u;
147
148         for (u = WC->urlstrings; u != NULL; u = u->next) {
149                 if (!strcasecmp(u->url_key, key))
150                         return (u->url_data);
151         }
152         return ("");
153 }
154
155
156 void wprintf(const char *format,...)
157 {
158         va_list arg_ptr;
159         char wbuf[4096];
160
161         va_start(arg_ptr, format);
162         vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
163         va_end(arg_ptr);
164
165         client_write(wbuf, strlen(wbuf));
166 }
167
168
169 /*
170  * wDumpContent() wraps up an HTTP session, closes tags, etc.
171  *
172  * print_standard_html_footer should be set to 0 to transmit only, 1 to
173  * append the main menu and closing tags, or 2 to
174  * append the closing tags only.
175  */
176 void wDumpContent(int print_standard_html_footer)
177 {
178         if (print_standard_html_footer) {
179                 wprintf("</div>\n");    /* end of "text" div */
180                 do_template("trailing");
181         }
182
183         /* If we've been saving it all up for one big output burst,
184          * go ahead and do that now.
185          */
186         end_burst();
187 }
188
189
190 /*
191  * Copy a string, escaping characters which have meaning in HTML.  If
192  * nbsp is nonzero, spaces are converted to non-breaking spaces.
193  */
194 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
195 {
196         int a;
197         strcpy(target, "");
198
199         for (a = 0; a < strlen(strbuf); ++a) {
200                 if (strbuf[a] == '<')
201                         strcat(target, "&lt;");
202                 else if (strbuf[a] == '>')
203                         strcat(target, "&gt;");
204                 else if (strbuf[a] == '&')
205                         strcat(target, "&amp;");
206                 else if (strbuf[a] == '\"')
207                         strcat(target, "&quot;");
208                 else if (strbuf[a] == '\'') 
209                         strcat(target, "&#39;");
210                 else if (strbuf[a] == LB)
211                         strcat(target, "<");
212                 else if (strbuf[a] == RB)
213                         strcat(target, ">");
214                 else if (strbuf[a] == QU)
215                         strcat(target, "\"");
216                 else if ((strbuf[a] == 32) && (nbsp == 1))
217                         strcat(target, "&nbsp;");
218                 else if ((strbuf[a] == '\n') && (nolinebreaks))
219                         strcat(target, "");     /* nothing */
220                 else if ((strbuf[a] == '\r') && (nolinebreaks))
221                         strcat(target, "");     /* nothing */
222                 else
223                         strncat(target, &strbuf[a], 1);
224         }
225 }
226
227 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
228 {
229         char *buf;
230
231         if (strbuf == NULL) return;
232         buf = malloc( (3 * strlen(strbuf)) + SIZ );
233         stresc(buf, strbuf, nbsp, nolinebreaks);
234         wprintf("%s", buf);
235         free(buf);
236 }
237
238 void escputs(char *strbuf)
239 {
240         escputs1(strbuf, 0, 0);
241 }
242
243 /*
244  * Escape a string for feeding out as a URL.
245  * Returns a pointer to a buffer that must be freed by the caller!
246  */
247 void urlesc(char *outbuf, char *strbuf)
248 {
249         int a, b, c;
250         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
251
252         strcpy(outbuf, "");
253
254         for (a = 0; a < strlen(strbuf); ++a) {
255                 c = 0;
256                 for (b = 0; b < strlen(ec); ++b) {
257                         if (strbuf[a] == ec[b])
258                                 c = 1;
259                 }
260                 b = strlen(outbuf);
261                 if (c == 1)
262                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
263                 else
264                         sprintf(&outbuf[b], "%c", strbuf[a]);
265         }
266 }
267
268 void urlescputs(char *strbuf)
269 {
270         char outbuf[SIZ];
271         
272         urlesc(outbuf, strbuf);
273         wprintf("%s", outbuf);
274 }
275
276
277 /*
278  * Copy a string, escaping characters for JavaScript strings.
279  */
280 void jsesc(char *target, char *strbuf)
281 {
282         int a;
283         strcpy(target, "");
284
285         for (a = 0; a < strlen(strbuf); ++a) {
286                 if (strbuf[a] == '<')
287                         strcat(target, "[");
288                 else if (strbuf[a] == '>')
289                         strcat(target, "]");
290                 else if (strbuf[a] == '\"')
291                         strcat(target, "&quot;");
292                 else if (strbuf[a] == '&')
293                         strcat(target, "&amp;;");
294                 else if (strbuf[a] == '\'') 
295                         strcat(target, "\\'");
296                 else {
297                         strncat(target, &strbuf[a], 1);
298                 }
299         }
300 }
301
302 void jsescputs(char *strbuf)
303 {
304         char outbuf[SIZ];
305         
306         jsesc(outbuf, strbuf);
307         wprintf("%s", outbuf);
308 }
309
310 /*
311  * Copy a string, escaping characters for message text hold
312  */
313 void msgesc(char *target, char *strbuf)
314 {
315         int a;
316         strcpy(target, "");
317
318         for (a = 0; a < strlen(strbuf); ++a) {
319                 if (strbuf[a] == '\'') 
320                         strcat(target, "\\'");
321                 else if (strbuf[a] == '\n')
322                         strcat(target, " ");
323                 else if (strbuf[a] == '\r')
324                         strcat(target, " ");
325                 else {
326                         strncat(target, &strbuf[a], 1);
327                 }
328         }
329 }
330
331 void msgescputs(char *strbuf) {
332         char *outbuf;
333
334         if (strbuf == NULL) return;
335         outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
336         msgesc(outbuf, strbuf);
337         wprintf("%s", outbuf);
338         free(outbuf);
339 }
340
341
342
343
344 /*
345  * Output all that important stuff that the browser will want to see
346  */
347 void output_headers(    int do_httpheaders,     /* 1 = output HTTP headers                          */
348                         int do_htmlhead,        /* 1 = output HTML <head> section and <body> opener */
349
350                         int do_room_banner,     /* 0=no, 1=yes,                                     */
351                                                 /* 2 = I'm going to embed my own, so don't open the */
352                                                 /*     <div id="content"> either.                   */
353
354                         int unset_cookies,      /* 1 = session is terminating, so unset the cookies */
355                         int refresh30,          /* 1 = automatically refresh page every 30 seconds  */
356                         int suppress_check,     /* 1 = suppress check for instant messages          */
357                         int cache               /* 1 = allow browser to cache this page             */
358 ) {
359         char cookie[SIZ];
360         char httpnow[SIZ];
361
362         wprintf("HTTP/1.0 200 OK\n");
363         httpdate(httpnow, time(NULL));
364
365         if (do_httpheaders) {
366                 wprintf("Content-type: text/html; charset=utf-8\r\n"
367                         "Server: %s / %s\n"
368                         "Connection: close\r\n",
369                         SERVER, serv_info.serv_software
370                 );
371         }
372
373         if (cache) {
374                 wprintf("Pragma: public\r\n"
375                         "Cache-Control: max-age=3600, must-revalidate\r\n"
376                         "Last-modified: %s\r\n",
377                         httpnow
378                 );
379         }
380         else {
381                 wprintf("Pragma: no-cache\r\n"
382                         "Cache-Control: no-store\r\n"
383                 );
384         }
385
386         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
387                         WC->wc_password, WC->wc_roomname);
388
389         if (unset_cookies) {
390                 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
391         } else {
392                 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
393                 if (server_cookie != NULL) {
394                         wprintf("%s\n", server_cookie);
395                 }
396         }
397
398         if (do_htmlhead) {
399                 /* wprintf("\n"); */
400                 begin_burst();
401
402                 if (refresh30) {
403                         svprintf("REFRESHTAG", WCS_STRING, "%s",
404                                 "<meta http-equiv=\"refresh\" content=\"30\" />\n");
405                 }
406                 else {
407                         svprintf("REFRESHTAG", WCS_STRING, "%s",
408                                 "<meta http-equiv=\"refresh\" content=\"500363689;\" />\n");
409                 }
410
411                 do_template("head");
412         }
413
414         /* ICONBAR */
415         if (do_htmlhead) {
416
417                 if (WC->HaveInstantMessages) {
418                         wprintf("<div id=\"page_popup\">\n");
419                         page_popup();
420                         wprintf("</div>\n");
421                 }
422                 if (strlen(WC->ImportantMessage) > 0) {
423                         wprintf("<div id=\"important_message\">\n");
424                         wprintf("<SPAN CLASS=\"imsg\">"
425                                 "%s</SPAN><br />\n", WC->ImportantMessage);
426                         wprintf("</div>\n");
427                         wprintf("<script type=\"text/javascript\">\n"
428                                 "        setTimeout('hide_imsg_popup()', 2000); \n"
429                                 "</script>\n");
430                         safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
431                 }
432                 if ( (WC->logged_in) && (!unset_cookies) ) {
433                         wprintf("<div id=\"iconbar\">");
434                         do_iconbar();
435                         wprintf("</div>\n");
436                 }
437                 if (do_room_banner == 1) {
438                         wprintf("<div id=\"banner\">\n");
439                         embed_room_banner(NULL, navbar_default);
440                         wprintf("</div>\n");
441                 }
442         }
443
444         if (do_room_banner == 1) {
445                 wprintf("<div id=\"content\">\n");
446         }
447 }
448
449
450 /*
451  * Generic function to do an HTTP redirect.  Easy and fun.
452  */
453 void http_redirect(char *whichpage) {
454         wprintf("HTTP/1.0 302 Moved Temporarily\n");
455         wprintf("Location: %s\r\n", whichpage);
456         wprintf("URI: %s\r\n", whichpage);
457         wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
458         wprintf("<html><body>\n");
459         wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
460                 whichpage);
461         wprintf("</body></html>\n");
462 }
463
464
465
466 void check_for_instant_messages()
467 {
468         char buf[SIZ];
469
470         serv_puts("NOOP");
471         serv_getln(buf, sizeof buf);
472         if (buf[3] == '*') WC->HaveInstantMessages = 1;
473 }
474
475
476
477
478 /* 
479  * Output a piece of content to the web browser
480  */
481 void http_transmit_thing(char *thing, size_t length, char *content_type,
482                          int is_static) {
483
484         output_headers(0, 0, 0, 0, 0, 0, is_static);
485
486         wprintf("Content-type: %s\r\n"
487                 "Server: %s\r\n"
488                 "Connection: close\r\n",
489                 content_type,
490                 SERVER);
491
492 #ifdef HAVE_ZLIB
493         /* If we can send the data out compressed, please do so. */
494         if (WC->gzip_ok) {
495                 char *compressed_data = NULL;
496                 uLongf compressed_len;
497
498                 compressed_len = (uLongf) ((length * 101) / 100) + 100;
499                 compressed_data = malloc(compressed_len);
500
501                 if (compress_gzip((Bytef *) compressed_data,
502                                   &compressed_len,
503                                   (Bytef *) thing,
504                                   (uLongf) length, Z_BEST_SPEED) == Z_OK) {
505                         wprintf("Content-encoding: gzip\r\n"
506                                 "Content-length: %ld\r\n"
507                                 "\r\n",
508                                 (long) compressed_len
509                         );
510                         client_write(compressed_data, (size_t)compressed_len);
511                         free(compressed_data);
512                         return;
513                 }
514         }
515 #endif
516
517         /* No compression ... just send it out as-is */
518         wprintf("Content-length: %ld\r\n"
519                 "\r\n",
520                 (long) length
521         );
522         client_write(thing, (size_t)length);
523 }
524
525
526
527
528 void output_static(char *what)
529 {
530         char buf[256];
531         FILE *fp;
532         struct stat statbuf;
533         off_t bytes;
534         char *bigbuffer;
535         char content_type[128];
536
537         sprintf(buf, "static/%s", what);
538         fp = fopen(buf, "rb");
539         if (fp == NULL) {
540                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
541                 wprintf("Content-Type: text/plain\r\n");
542                 wprintf("\r\n");
543                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
544         } else {
545                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
546                         safestrncpy(content_type, "image/gif", sizeof content_type);
547                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
548                         safestrncpy(content_type, "text/plain", sizeof content_type);
549                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
550                         safestrncpy(content_type, "text/css", sizeof content_type);
551                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
552                         safestrncpy(content_type, "image/jpeg", sizeof content_type);
553                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
554                         safestrncpy(content_type, "image/png", sizeof content_type);
555                 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
556                         safestrncpy(content_type, "image/x-icon", sizeof content_type);
557                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
558                         safestrncpy(content_type, "text/html", sizeof content_type);
559                 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
560                         safestrncpy(content_type, "text/html", sizeof content_type);
561                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
562                         safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
563                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
564                         safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
565                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
566                         safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
567                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
568                         safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
569                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
570                         safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
571                 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
572                         safestrncpy(content_type, "text/javascript", sizeof content_type);
573                 else
574                         safestrncpy(content_type, "application/octet-stream", sizeof content_type);
575
576                 fstat(fileno(fp), &statbuf);
577                 bytes = statbuf.st_size;
578                 bigbuffer = malloc(bytes + 2);
579                 fread(bigbuffer, bytes, 1, fp);
580                 fclose(fp);
581
582                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
583                 free(bigbuffer);
584         }
585         if (!strcasecmp(bstr("force_close_session"), "yes")) {
586                 end_webcit_session();
587         }
588 }
589
590
591 /*
592  * When the browser requests an image file from the Citadel server,
593  * this function is called to transmit it.
594  */
595 void output_image()
596 {
597         char buf[SIZ];
598         char *xferbuf = NULL;
599         off_t bytes;
600
601         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
602         serv_getln(buf, sizeof buf);
603         if (buf[0] == '2') {
604                 bytes = extract_long(&buf[4], 0);
605                 xferbuf = malloc(bytes + 2);
606
607                 /* Read it from the server */
608                 read_server_binary(xferbuf, bytes);
609                 serv_puts("CLOS");
610                 serv_getln(buf, sizeof buf);
611
612                 /* Write it to the browser */
613                 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
614                 free(xferbuf);
615
616         } else {
617
618                 /* Instead of an ugly 404, send a 1x1 transparent GIF
619                  * when there's no such image on the server.
620                  */
621                 output_static("blank.gif");
622
623                 /*
624                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
625                 output_headers(0, 0, 0, 0, 0, 0, 0);
626                 wprintf("Content-Type: text/plain\r\n"
627                         "\r\n"
628                         "Error retrieving image: %s\n",
629                         &buf[4]
630                 );
631                 */
632
633         }
634
635
636
637 }
638
639 /*
640  */
641 void output_mimepart()
642 {
643         char buf[SIZ];
644         off_t bytes;
645         char content_type[SIZ];
646         char *content = NULL;
647         
648         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
649         serv_getln(buf, sizeof buf);
650         if (buf[0] == '2') {
651                 bytes = extract_long(&buf[4], 0);
652                 content = malloc(bytes + 2);
653                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
654                 output_headers(0, 0, 0, 0, 0, 0, 0);
655                 read_server_binary(content, bytes);
656                 serv_puts("CLOS");
657                 serv_getln(buf, sizeof buf);
658                 http_transmit_thing(content, bytes, content_type, 0);
659                 free(content);
660         } else {
661                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
662                 output_headers(0, 0, 0, 0, 0, 0, 0);
663                 wprintf("Content-Type: text/plain\r\n");
664                 wprintf("\r\n");
665                 wprintf("Error retrieving part: %s\n", &buf[4]);
666         }
667
668 }
669
670
671 /*
672  */
673 char *load_mimepart(long msgnum, char *partnum)
674 {
675         char buf[SIZ];
676         off_t bytes;
677         char content_type[SIZ];
678         char *content;
679         
680         serv_printf("OPNA %ld|%s", msgnum, partnum);
681         serv_getln(buf, sizeof buf);
682         if (buf[0] == '2') {
683                 bytes = extract_long(&buf[4], 0);
684                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
685
686                 content = malloc(bytes + 2);
687                 read_server_binary(content, bytes);
688
689                 serv_puts("CLOS");
690                 serv_getln(buf, sizeof buf);
691                 content[bytes] = 0;     /* null terminate for good measure */
692                 return(content);
693         }
694         else {
695                 return(NULL);
696         }
697
698 }
699
700
701 /*
702  * Convenience functions to display a page containing only a string
703  */
704 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
705 {
706         wprintf("HTTP/1.0 200 OK\n");
707         output_headers(1, 1, 2, 0, 0, 0, 0);
708         wprintf("<div id=\"banner\">\n");
709         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
710         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
711         wprintf("</TD></TR></TABLE>\n");
712         wprintf("</div>\n<div id=\"content\">\n");
713         escputs(messagetext);
714
715         wprintf("<hr />\n");
716         wDumpContent(1);
717 }
718
719
720 /*
721  * Display a blank page.
722  */
723 void blank_page(void) {
724         output_headers(1, 1, 0, 0, 1, 0, 0);
725         wDumpContent(2);
726 }
727
728
729 /*
730  * A template has been requested
731  */
732 void url_do_template(void) {
733         do_template(bstr("template"));
734 }
735
736
737
738 /*
739  * Offer to make any page the user's "start page."
740  */
741 void offer_start_page(void) {
742         wprintf("<A HREF=\"/change_start_page?startpage=");
743         urlescputs(WC->this_page);
744         wprintf("\">"
745                 "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
746                 "</A>"
747         );
748 }
749
750
751 /* 
752  * Change the user's start page
753  */
754 void change_start_page(void) {
755
756         if (bstr("startpage") == NULL) {
757                 safestrncpy(WC->ImportantMessage,
758                         "startpage set to null",
759                         sizeof WC->ImportantMessage);
760                 display_main_menu();
761                 return;
762         }
763
764         set_preference("startpage", bstr("startpage"), 1);
765
766         output_headers(1, 1, 0, 0, 0, 0, 0);
767         do_template("newstartpage");
768         wDumpContent(1);
769 }
770
771
772
773
774 void display_success(char *successmessage)
775 {
776         convenience_page("007700", "OK", successmessage);
777 }
778
779
780
781 void upload_handler(char *name, char *filename, char *partnum, char *disp,
782                         void *content, char *cbtype, char *cbcharset,
783                         size_t length, char *encoding, void *userdata)
784 {
785         struct urlcontent *u;
786
787         lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
788                 name, cbtype, length);
789
790         /* Form fields */
791         if ( (length > 0) && (strlen(cbtype) == 0) ) {
792                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
793                 u->next = WC->urlstrings;
794                 WC->urlstrings = u;
795                 safestrncpy(u->url_key, name, sizeof(u->url_key));
796                 u->url_data = malloc(length + 1);
797                 memcpy(u->url_data, content, length);
798                 u->url_data[length] = 0;
799         }
800
801         /* Uploaded files */
802         if ( (length > 0) && (strlen(cbtype) > 0) ) {
803                 WC->upload = malloc(length);
804                 if (WC->upload != NULL) {
805                         WC->upload_length = length;
806                         safestrncpy(WC->upload_filename, filename,
807                                         sizeof(WC->upload_filename));
808                         safestrncpy(WC->upload_content_type, cbtype,
809                                         sizeof(WC->upload_content_type));
810                         memcpy(WC->upload, content, length);
811                 }
812                 else {
813                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
814                 }
815         }
816
817 }
818
819
820
821
822 /*
823  * Entry point for WebCit transaction
824  */
825 void session_loop(struct httprequest *req)
826 {
827         char cmd[1024];
828         char method[128];
829         char action[128];
830         char arg1[128];
831         char arg2[128];
832         char arg3[128];
833         char buf[SIZ];
834         int a, b;
835         int ContentLength = 0;
836         int BytesRead = 0;
837         char ContentType[512];
838         char *content = NULL;
839         char *content_end = NULL;
840         struct httprequest *hptr;
841         char browser_host[SIZ];
842         char user_agent[SIZ];
843         int body_start = 0;
844
845         /* We stuff these with the values coming from the client cookies,
846          * so we can use them to reconnect a timed out session if we have to.
847          */
848         char c_username[SIZ];
849         char c_password[SIZ];
850         char c_roomname[SIZ];
851         char c_httpauth_string[SIZ];
852         char c_httpauth_user[SIZ];
853         char c_httpauth_pass[SIZ];
854         char cookie[SIZ];
855
856         safestrncpy(c_username, "", sizeof c_username);
857         safestrncpy(c_password, "", sizeof c_password);
858         safestrncpy(c_roomname, "", sizeof c_roomname);
859         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
860         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
861         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
862
863         WC->upload_length = 0;
864         WC->upload = NULL;
865         WC->vars = NULL;
866
867         WC->is_wap = 0;
868
869         hptr = req;
870         if (hptr == NULL) return;
871
872         safestrncpy(cmd, hptr->line, sizeof cmd);
873         hptr = hptr->next;
874         extract_token(method, cmd, 0, ' ', sizeof method);
875
876         /* Figure out the action */
877         extract_token(action, cmd, 1, '/', sizeof action);
878         if (strstr(action, "?")) *strstr(action, "?") = 0;
879         if (strstr(action, "&")) *strstr(action, "&") = 0;
880         if (strstr(action, " ")) *strstr(action, " ") = 0;
881
882         extract_token(arg1, cmd, 2, '/', sizeof arg1);
883         if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
884         if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
885         if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
886
887         extract_token(arg2, cmd, 3, '/', sizeof arg2);
888         if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
889         if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
890         if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
891
892         extract_token(arg3, cmd, 4, '/', sizeof arg3);
893         if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
894         if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
895         if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
896
897         while (hptr != NULL) {
898                 safestrncpy(buf, hptr->line, sizeof buf);
899                 hptr = hptr->next;
900
901                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
902                         safestrncpy(cookie, &buf[15], sizeof cookie);
903                         cookie_to_stuff(cookie, NULL,
904                                         c_username, sizeof c_username,
905                                         c_password, sizeof c_password,
906                                         c_roomname, sizeof c_roomname);
907                 }
908                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
909                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
910                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
911                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
912                 }
913                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
914                         ContentLength = atoi(&buf[16]);
915                 }
916                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
917                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
918                 }
919                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
920                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
921                 }
922                 else if (!strncasecmp(buf, "Host: ", 6)) {
923                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
924                 }
925                 /* Only WAP gateways explicitly name this content-type */
926                 else if (strstr(buf, "text/vnd.wap.wml")) {
927                         WC->is_wap = 1;
928                 }
929         }
930
931         if (ContentLength > 0) {
932                 content = malloc(ContentLength + SIZ);
933                 memset(content, 0, ContentLength + SIZ);
934                 sprintf(content, "Content-type: %s\n"
935                                 "Content-length: %d\n\n",
936                                 ContentType, ContentLength);
937                 body_start = strlen(content);
938
939                 /* Read the entire input data at once. */
940                 client_read(WC->http_sock, &content[BytesRead+body_start],
941                         ContentLength);
942
943                 if (!strncasecmp(ContentType,
944                               "application/x-www-form-urlencoded", 33)) {
945                         addurls(&content[body_start]);
946                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
947                         content_end = content + ContentLength + body_start;
948                         mime_parser(content, content_end, *upload_handler,
949                                         NULL, NULL, NULL, 0);
950                 }
951         } else {
952                 content = NULL;
953         }
954
955         /* make a note of where we are in case the user wants to save it */
956         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
957         remove_token(WC->this_page, 2, ' ');
958         remove_token(WC->this_page, 0, ' ');
959
960         /* If there are variables in the URL, we must grab them now */
961         for (a = 0; a < strlen(cmd); ++a) {
962                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
963                         for (b = a; b < strlen(cmd); ++b)
964                                 if (isspace(cmd[b]))
965                                         cmd[b] = 0;
966                         addurls(&cmd[a + 1]);
967                         cmd[a] = 0;
968                 }
969         }
970
971
972         /* Static content can be sent without connecting to Citadel. */
973         if (!strcasecmp(action, "static")) {
974                 safestrncpy(buf, arg1, sizeof buf);
975                 for (a = 0; a < strlen(buf); ++a)
976                         if (isspace(buf[a]))
977                                 buf[a] = 0;
978                 output_static(buf);
979                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
980         }
981
982         /*
983          * If we're not connected to a Citadel server, try to hook up the
984          * connection now.
985          */
986         if (!WC->connected) {
987                 if (!strcasecmp(ctdlhost, "uds")) {
988                         /* unix domain socket */
989                         sprintf(buf, "%s/citadel.socket", ctdlport);
990                         WC->serv_sock = uds_connectsock(buf);
991                 }
992                 else {
993                         /* tcp socket */
994                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
995                 }
996
997                 if (WC->serv_sock < 0) {
998                         do_logout();
999                         goto SKIP_ALL_THIS_CRAP;
1000                 }
1001                 else {
1002                         WC->connected = 1;
1003                         serv_getln(buf, sizeof buf);    /* get the server welcome message */
1004                         locate_host(browser_host, WC->http_sock);
1005                         get_serv_info(browser_host, user_agent);
1006                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1007                                 wprintf("You are connected to a Citadel "
1008                                         "server running Citadel %d.%02d;\nin "
1009                                         "order to run this version of WebCit "
1010                                         "you must also have Citadel %d.%02d or"
1011                                         " newer.\n\n\n",
1012                                                 serv_info.serv_rev_level / 100,
1013                                                 serv_info.serv_rev_level % 100,
1014                                                 MINIMUM_CIT_VERSION / 100,
1015                                                 MINIMUM_CIT_VERSION % 100
1016                                         );
1017                                 end_webcit_session();
1018                                 goto SKIP_ALL_THIS_CRAP;
1019                         }
1020                 }
1021         }
1022
1023         /*
1024          * Functions which can be performed without logging in
1025          */
1026         if (!strcasecmp(action, "listsub")) {
1027                 do_listsub();
1028                 goto SKIP_ALL_THIS_CRAP;
1029         }
1030 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1031         if (!strcasecmp(action, "freebusy")) {
1032                 do_freebusy(cmd);
1033                 goto SKIP_ALL_THIS_CRAP;
1034         }
1035 #endif
1036
1037         /*
1038          * If we're not logged in, but we have HTTP Authentication data,
1039          * try logging in to Citadel using that.
1040          */
1041         if ((!WC->logged_in)
1042            && (strlen(c_httpauth_user) > 0)
1043            && (strlen(c_httpauth_pass) > 0)) {
1044                 serv_printf("USER %s", c_httpauth_user);
1045                 serv_getln(buf, sizeof buf);
1046                 if (buf[0] == '3') {
1047                         serv_printf("PASS %s", c_httpauth_pass);
1048                         serv_getln(buf, sizeof buf);
1049                         if (buf[0] == '2') {
1050                                 become_logged_in(c_httpauth_user,
1051                                                 c_httpauth_pass, buf);
1052                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1053                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1054                         }
1055                 }
1056         }
1057
1058         /* 
1059          * The GroupDAV stuff relies on HTTP authentication instead of
1060          * our session's authentication.
1061          */
1062         if (!strncasecmp(action, "groupdav", 8)) {
1063                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1064                         ContentLength, content+body_start);
1065                 if (!WC->logged_in) {
1066                         WC->killthis = 1;       /* If not logged in, don't */
1067                 }                               /* keep the session active */
1068                 goto SKIP_ALL_THIS_CRAP;
1069         }
1070
1071
1072         /*
1073          * Automatically send requests with any method other than GET or
1074          * POST to the GroupDAV code as well.
1075          */
1076         if ((strcasecmp(method, "GET")) && (strcasecmp(method, "POST"))) {
1077                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1078                         ContentLength, content+body_start);
1079                 if (!WC->logged_in) {
1080                         WC->killthis = 1;       /* If not logged in, don't */
1081                 }                               /* keep the session active */
1082                 goto SKIP_ALL_THIS_CRAP;
1083         }
1084
1085         /*
1086          * If we're not logged in, but we have username and password cookies
1087          * supplied by the browser, try using them to log in.
1088          */
1089         if ((!WC->logged_in)
1090            && (strlen(c_username) > 0)
1091            && (strlen(c_password) > 0)) {
1092                 serv_printf("USER %s", c_username);
1093                 serv_getln(buf, sizeof buf);
1094                 if (buf[0] == '3') {
1095                         serv_printf("PASS %s", c_password);
1096                         serv_getln(buf, sizeof buf);
1097                         if (buf[0] == '2') {
1098                                 become_logged_in(c_username, c_password, buf);
1099                         }
1100                 }
1101         }
1102         /*
1103          * If we don't have a current room, but a cookie specifying the
1104          * current room is supplied, make an effort to go there.
1105          */
1106         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1107                 serv_printf("GOTO %s", c_roomname);
1108                 serv_getln(buf, sizeof buf);
1109                 if (buf[0] == '2') {
1110                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1111                 }
1112         }
1113
1114         /*
1115          * If there are instant messages waiting, retrieve them for display.
1116          */
1117         check_for_instant_messages();
1118
1119         if (!strcasecmp(action, "image")) {
1120                 output_image();
1121
1122         /*
1123          * All functions handled below this point ... make sure we log in
1124          * before doing anything else!
1125          */
1126         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1127                 do_login();
1128         } else if (!WC->logged_in) {
1129                 display_login(NULL);
1130         }
1131
1132         /*
1133          * Various commands...
1134          */
1135
1136         else if (!strcasecmp(action, "do_welcome")) {
1137                 do_welcome();
1138         } else if (!strcasecmp(action, "blank")) {
1139                 blank_page();
1140         } else if (!strcasecmp(action, "do_template")) {
1141                 url_do_template();
1142         } else if (!strcasecmp(action, "display_aide_menu")) {
1143                 display_aide_menu();
1144         } else if (!strcasecmp(action, "display_main_menu")) {
1145                 display_main_menu();
1146         } else if (!strcasecmp(action, "who")) {
1147                 who();
1148         } else if (!strcasecmp(action, "who_inner_html")) {
1149                 who_inner_html();
1150         } else if (!strcasecmp(action, "knrooms")) {
1151                 knrooms();
1152         } else if (!strcasecmp(action, "gotonext")) {
1153                 slrp_highest();
1154                 gotonext();
1155         } else if (!strcasecmp(action, "skip")) {
1156                 gotonext();
1157         } else if (!strcasecmp(action, "ungoto")) {
1158                 ungoto();
1159         } else if (!strcasecmp(action, "dotgoto")) {
1160                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1161                         slrp_highest();
1162                 }
1163                 smart_goto(bstr("room"));
1164         } else if (!strcasecmp(action, "dotskip")) {
1165                 smart_goto(bstr("room"));
1166         } else if (!strcasecmp(action, "termquit")) {
1167                 do_logout();
1168         } else if (!strcasecmp(action, "readnew")) {
1169                 readloop("readnew");
1170         } else if (!strcasecmp(action, "readold")) {
1171                 readloop("readold");
1172         } else if (!strcasecmp(action, "readfwd")) {
1173                 readloop("readfwd");
1174         } else if (!strcasecmp(action, "headers")) {
1175                 readloop("headers");
1176         } else if (!strcasecmp(action, "msg")) {
1177                 embed_message();
1178         } else if (!strcasecmp(action, "display_enter")) {
1179                 display_enter();
1180         } else if (!strcasecmp(action, "post")) {
1181                 post_message();
1182         } else if (!strcasecmp(action, "move_msg")) {
1183                 move_msg();
1184         } else if (!strcasecmp(action, "delete_msg")) {
1185                 delete_msg();
1186         } else if (!strcasecmp(action, "userlist")) {
1187                 userlist();
1188         } else if (!strcasecmp(action, "showuser")) {
1189                 showuser();
1190         } else if (!strcasecmp(action, "display_page")) {
1191                 display_page();
1192         } else if (!strcasecmp(action, "page_user")) {
1193                 page_user();
1194         } else if (!strcasecmp(action, "chat")) {
1195                 do_chat();
1196         } else if (!strcasecmp(action, "display_private")) {
1197                 display_private("", 0);
1198         } else if (!strcasecmp(action, "goto_private")) {
1199                 goto_private();
1200         } else if (!strcasecmp(action, "zapped_list")) {
1201                 zapped_list();
1202         } else if (!strcasecmp(action, "display_zap")) {
1203                 display_zap();
1204         } else if (!strcasecmp(action, "zap")) {
1205                 zap();
1206         } else if (!strcasecmp(action, "display_entroom")) {
1207                 display_entroom();
1208         } else if (!strcasecmp(action, "entroom")) {
1209                 entroom();
1210         } else if (!strcasecmp(action, "display_whok")) {
1211                 display_whok();
1212         } else if (!strcasecmp(action, "do_invt_kick")) {
1213                 do_invt_kick();
1214         } else if (!strcasecmp(action, "display_editroom")) {
1215                 display_editroom();
1216         } else if (!strcasecmp(action, "netedit")) {
1217                 netedit();
1218         } else if (!strcasecmp(action, "editroom")) {
1219                 editroom();
1220         } else if (!strcasecmp(action, "display_editinfo")) {
1221                 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1222         } else if (!strcasecmp(action, "editinfo")) {
1223                 save_edit("Room info", "EINF 1", 1);
1224         } else if (!strcasecmp(action, "display_editbio")) {
1225                 sprintf(buf, "RBIO %s", WC->wc_username);
1226                 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1227         } else if (!strcasecmp(action, "editbio")) {
1228                 save_edit("Your bio", "EBIO", 0);
1229         } else if (!strcasecmp(action, "confirm_move_msg")) {
1230                 confirm_move_msg();
1231         } else if (!strcasecmp(action, "delete_room")) {
1232                 delete_room();
1233         } else if (!strcasecmp(action, "validate")) {
1234                 validate();
1235         } else if (!strcasecmp(action, "display_editpic")) {
1236                 display_graphics_upload("your photo",
1237                                         "UIMG 0|_userpic_",
1238                                         "/editpic");
1239         } else if (!strcasecmp(action, "editpic")) {
1240                 do_graphics_upload("UIMG 1|_userpic_");
1241         } else if (!strcasecmp(action, "display_editroompic")) {
1242                 display_graphics_upload("the icon for this room",
1243                                         "UIMG 0|_roompic_",
1244                                         "/editroompic");
1245         } else if (!strcasecmp(action, "editroompic")) {
1246                 do_graphics_upload("UIMG 1|_roompic_");
1247         } else if (!strcasecmp(action, "delete_floor")) {
1248                 delete_floor();
1249         } else if (!strcasecmp(action, "rename_floor")) {
1250                 rename_floor();
1251         } else if (!strcasecmp(action, "create_floor")) {
1252                 create_floor();
1253         } else if (!strcasecmp(action, "display_editfloorpic")) {
1254                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1255                         bstr("which_floor"));
1256                 display_graphics_upload("the icon for this floor",
1257                                         buf,
1258                                         "/editfloorpic");
1259         } else if (!strcasecmp(action, "editfloorpic")) {
1260                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1261                         bstr("which_floor"));
1262                 do_graphics_upload(buf);
1263         } else if (!strcasecmp(action, "display_reg")) {
1264                 display_reg(0);
1265         } else if (!strcasecmp(action, "display_changepw")) {
1266                 display_changepw();
1267         } else if (!strcasecmp(action, "changepw")) {
1268                 changepw();
1269         } else if (!strcasecmp(action, "display_edit_node")) {
1270                 display_edit_node();
1271         } else if (!strcasecmp(action, "edit_node")) {
1272                 edit_node();
1273         } else if (!strcasecmp(action, "display_netconf")) {
1274                 display_netconf();
1275         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1276                 display_confirm_delete_node();
1277         } else if (!strcasecmp(action, "delete_node")) {
1278                 delete_node();
1279         } else if (!strcasecmp(action, "display_add_node")) {
1280                 display_add_node();
1281         } else if (!strcasecmp(action, "add_node")) {
1282                 add_node();
1283         } else if (!strcasecmp(action, "terminate_session")) {
1284                 slrp_highest();
1285                 terminate_session();
1286         } else if (!strcasecmp(action, "edit_me")) {
1287                 edit_me();
1288         } else if (!strcasecmp(action, "display_siteconfig")) {
1289                 display_siteconfig();
1290         } else if (!strcasecmp(action, "chat_recv")) {
1291                 chat_recv();
1292         } else if (!strcasecmp(action, "chat_send")) {
1293                 chat_send();
1294         } else if (!strcasecmp(action, "siteconfig")) {
1295                 siteconfig();
1296         } else if (!strcasecmp(action, "display_generic")) {
1297                 display_generic();
1298         } else if (!strcasecmp(action, "do_generic")) {
1299                 do_generic();
1300         } else if (!strcasecmp(action, "display_menubar")) {
1301                 display_menubar(1);
1302         } else if (!strcasecmp(action, "output_mimepart")) {
1303                 output_mimepart();
1304         } else if (!strcasecmp(action, "edit_vcard")) {
1305                 edit_vcard();
1306         } else if (!strcasecmp(action, "submit_vcard")) {
1307                 submit_vcard();
1308         } else if (!strcasecmp(action, "select_user_to_edit")) {
1309                 select_user_to_edit(NULL, NULL);
1310         } else if (!strcasecmp(action, "display_edituser")) {
1311                 display_edituser(NULL, 0);
1312         } else if (!strcasecmp(action, "edituser")) {
1313                 edituser();
1314         } else if (!strcasecmp(action, "create_user")) {
1315                 create_user();
1316         } else if (!strcasecmp(action, "changeview")) {
1317                 change_view();
1318         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1319                 do_stuff_to_msgs();
1320         } else if (!strcasecmp(action, "change_start_page")) {
1321                 change_start_page();
1322         } else if (!strcasecmp(action, "display_floorconfig")) {
1323                 display_floorconfig(NULL);
1324         } else if (!strcasecmp(action, "toggle_self_service")) {
1325                 toggle_self_service();
1326 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1327         } else if (!strcasecmp(action, "display_edit_task")) {
1328                 display_edit_task();
1329         } else if (!strcasecmp(action, "save_task")) {
1330                 save_task();
1331         } else if (!strcasecmp(action, "display_edit_event")) {
1332                 display_edit_event();
1333         } else if (!strcasecmp(action, "save_event")) {
1334                 save_event();
1335         } else if (!strcasecmp(action, "respond_to_request")) {
1336                 respond_to_request();
1337         } else if (!strcasecmp(action, "handle_rsvp")) {
1338                 handle_rsvp();
1339 #endif
1340         } else if (!strcasecmp(action, "summary")) {
1341                 summary();
1342         } else if (!strcasecmp(action, "iconbar")) {
1343                 do_iconbar();
1344         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1345                 display_customize_iconbar();
1346         } else if (!strcasecmp(action, "commit_iconbar")) {
1347                 commit_iconbar();
1348         } else if (!strcasecmp(action, "set_room_policy")) {
1349                 set_room_policy();
1350         } else if (!strcasecmp(action, "display_inetconf")) {
1351                 display_inetconf();
1352         } else if (!strcasecmp(action, "save_inetconf")) {
1353                 save_inetconf();
1354         } else if (!strcasecmp(action, "setup_wizard")) {
1355                 do_setup_wizard();
1356         } else if (!strcasecmp(action, "display_preferences")) {
1357                 display_preferences();
1358         } else if (!strcasecmp(action, "set_preferences")) {
1359                 set_preferences();
1360         } else if (!strcasecmp(action, "diagnostics")) {
1361                 output_headers(1, 1, 1, 0, 0, 0, 0);
1362
1363                 wprintf("You're in session %d<hr />\n", WC->wc_session);
1364                 wprintf("Command: <br /><PRE>\n");
1365                 escputs(cmd);
1366                 wprintf("</PRE><hr />\n");
1367                 wprintf("Variables: <br /><PRE>\n");
1368                 dump_vars();
1369                 wprintf("</PRE><hr />\n");
1370                 wDumpContent(1);
1371         }
1372         /* When all else fais, display the main menu. */
1373         else {
1374                 display_main_menu();
1375         }
1376
1377 SKIP_ALL_THIS_CRAP:
1378         fflush(stdout);
1379         if (content != NULL) {
1380                 free(content);
1381                 content = NULL;
1382         }
1383         free_urls();
1384         if (WC->upload_length > 0) {
1385                 free(WC->upload);
1386                 WC->upload_length = 0;
1387         }
1388
1389 }