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