]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* The "forward" button is now working, and it forwards the attachments.
[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()', 3000); \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>");
441         wprintf("Go <A HREF=\"%s\">here</A>.", whichpage);
442         wprintf("</body></html>\n");
443 }
444
445
446
447 void check_for_instant_messages()
448 {
449         char buf[SIZ];
450
451         serv_puts("NOOP");
452         serv_getln(buf, sizeof buf);
453         if (buf[3] == '*') WC->HaveInstantMessages = 1;
454 }
455
456
457
458
459 /* 
460  * Output a piece of content to the web browser
461  */
462 void http_transmit_thing(char *thing, size_t length, char *content_type,
463                          int is_static) {
464
465         output_headers(0, 0, 0, 0, 0, 0, is_static);
466
467         wprintf("Content-type: %s\r\n"
468                 "Server: %s\r\n"
469                 "Connection: close\r\n",
470                 content_type,
471                 SERVER);
472
473 #ifdef HAVE_ZLIB
474         /* If we can send the data out compressed, please do so. */
475         if (WC->gzip_ok) {
476                 char *compressed_data = NULL;
477                 uLongf compressed_len;
478
479                 compressed_len = (uLongf) ((length * 101) / 100) + 100;
480                 compressed_data = malloc(compressed_len);
481
482                 if (compress_gzip((Bytef *) compressed_data,
483                                   &compressed_len,
484                                   (Bytef *) thing,
485                                   (uLongf) length, Z_BEST_SPEED) == Z_OK) {
486                         wprintf("Content-encoding: gzip\r\n"
487                                 "Content-length: %ld\r\n"
488                                 "\r\n",
489                                 (long) compressed_len
490                         );
491                         client_write(compressed_data, (size_t)compressed_len);
492                         free(compressed_data);
493                         return;
494                 }
495         }
496 #endif
497
498         /* No compression ... just send it out as-is */
499         wprintf("Content-length: %ld\r\n"
500                 "\r\n",
501                 (long) length
502         );
503         client_write(thing, (size_t)length);
504 }
505
506
507
508
509 void output_static(char *what)
510 {
511         char buf[256];
512         FILE *fp;
513         struct stat statbuf;
514         off_t bytes;
515         char *bigbuffer;
516         char content_type[128];
517
518         sprintf(buf, "static/%s", what);
519         fp = fopen(buf, "rb");
520         if (fp == NULL) {
521                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
522                 wprintf("Content-Type: text/plain\r\n");
523                 wprintf("\r\n");
524                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
525         } else {
526                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
527                         safestrncpy(content_type, "image/gif", sizeof content_type);
528                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
529                         safestrncpy(content_type, "text/plain", sizeof content_type);
530                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
531                         safestrncpy(content_type, "text/css", sizeof content_type);
532                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
533                         safestrncpy(content_type, "image/jpeg", sizeof content_type);
534                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
535                         safestrncpy(content_type, "image/png", sizeof content_type);
536                 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
537                         safestrncpy(content_type, "image/x-icon", sizeof content_type);
538                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
539                         safestrncpy(content_type, "text/html", sizeof content_type);
540                 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
541                         safestrncpy(content_type, "text/html", sizeof content_type);
542                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
543                         safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
544                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
545                         safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
546                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
547                         safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
548                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
549                         safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
550                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
551                         safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
552                 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
553                         safestrncpy(content_type, "text/javascript", sizeof content_type);
554                 else
555                         safestrncpy(content_type, "application/octet-stream", sizeof content_type);
556
557                 fstat(fileno(fp), &statbuf);
558                 bytes = statbuf.st_size;
559                 bigbuffer = malloc(bytes + 2);
560                 fread(bigbuffer, bytes, 1, fp);
561                 fclose(fp);
562
563                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
564                 free(bigbuffer);
565         }
566         if (!strcasecmp(bstr("force_close_session"), "yes")) {
567                 end_webcit_session();
568         }
569 }
570
571
572 /*
573  * When the browser requests an image file from the Citadel server,
574  * this function is called to transmit it.
575  */
576 void output_image()
577 {
578         char buf[SIZ];
579         char *xferbuf = NULL;
580         off_t bytes;
581
582         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
583         serv_getln(buf, sizeof buf);
584         if (buf[0] == '2') {
585                 bytes = extract_long(&buf[4], 0);
586                 xferbuf = malloc(bytes + 2);
587
588                 /* Read it from the server */
589                 read_server_binary(xferbuf, bytes);
590                 serv_puts("CLOS");
591                 serv_getln(buf, sizeof buf);
592
593                 /* Write it to the browser */
594                 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
595                 free(xferbuf);
596
597         } else {
598                 /* Instead of an ugly 404, send a 1x1 transparent GIF
599                  * when there's no such image on the server.
600                  */
601                 output_static("blank.gif");
602         }
603
604
605
606 }
607
608 /*
609  * Generic function to output an arbitrary MIME part from an arbitrary
610  * message number on the server.
611  */
612 void output_mimepart()
613 {
614         char buf[SIZ];
615         off_t bytes;
616         char content_type[SIZ];
617         char *content = NULL;
618         
619         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
620         serv_getln(buf, sizeof buf);
621         if (buf[0] == '2') {
622                 bytes = extract_long(&buf[4], 0);
623                 content = malloc(bytes + 2);
624                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
625                 output_headers(0, 0, 0, 0, 0, 0, 0);
626                 read_server_binary(content, bytes);
627                 serv_puts("CLOS");
628                 serv_getln(buf, sizeof buf);
629                 http_transmit_thing(content, bytes, content_type, 0);
630                 free(content);
631         } else {
632                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
633                 output_headers(0, 0, 0, 0, 0, 0, 0);
634                 wprintf("Content-Type: text/plain\r\n");
635                 wprintf("\r\n");
636                 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
637         }
638
639 }
640
641
642 /*
643  * Read any MIME part of a message, from the server, into memory.
644  */
645 char *load_mimepart(long msgnum, char *partnum)
646 {
647         char buf[SIZ];
648         off_t bytes;
649         char content_type[SIZ];
650         char *content;
651         
652         serv_printf("OPNA %ld|%s", msgnum, partnum);
653         serv_getln(buf, sizeof buf);
654         if (buf[0] == '2') {
655                 bytes = extract_long(&buf[4], 0);
656                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
657
658                 content = malloc(bytes + 2);
659                 read_server_binary(content, bytes);
660
661                 serv_puts("CLOS");
662                 serv_getln(buf, sizeof buf);
663                 content[bytes] = 0;     /* null terminate for good measure */
664                 return(content);
665         }
666         else {
667                 return(NULL);
668         }
669
670 }
671
672
673 /*
674  * Convenience functions to display a page containing only a string
675  */
676 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
677 {
678         wprintf("HTTP/1.0 200 OK\n");
679         output_headers(1, 1, 2, 0, 0, 0, 0);
680         wprintf("<div id=\"banner\">\n");
681         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
682         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
683         wprintf("</TD></TR></TABLE>\n");
684         wprintf("</div>\n<div id=\"content\">\n");
685         escputs(messagetext);
686
687         wprintf("<hr />\n");
688         wDumpContent(1);
689 }
690
691
692 /*
693  * Display a blank page.
694  */
695 void blank_page(void) {
696         output_headers(1, 1, 0, 0, 1, 0, 0);
697         wDumpContent(2);
698 }
699
700
701 /*
702  * A template has been requested
703  */
704 void url_do_template(void) {
705         do_template(bstr("template"));
706 }
707
708
709
710 /*
711  * Offer to make any page the user's "start page."
712  */
713 void offer_start_page(void) {
714         wprintf("<A HREF=\"/change_start_page?startpage=");
715         urlescputs(WC->this_page);
716         wprintf("\"><FONT SIZE=-2 COLOR=\"#AAAAAA\">");
717         wprintf(_("Make this my start page"));
718         wprintf("</FONT></A>");
719 }
720
721
722 /* 
723  * Change the user's start page
724  */
725 void change_start_page(void) {
726
727         if (bstr("startpage") == NULL) {
728                 safestrncpy(WC->ImportantMessage,
729                         _("You no longer have a start page selected."),
730                         sizeof WC->ImportantMessage);
731                 display_main_menu();
732                 return;
733         }
734
735         set_preference("startpage", bstr("startpage"), 1);
736
737         output_headers(1, 1, 0, 0, 0, 0, 0);
738         do_template("newstartpage");
739         wDumpContent(1);
740 }
741
742
743
744
745 void display_success(char *successmessage)
746 {
747         convenience_page("007700", "OK", successmessage);
748 }
749
750
751 /* Authorization required page */
752 /* This is probably temporary and should be revisited */
753 void authorization_required(const char *message)
754 {
755         wprintf("HTTP/1.0 401 Authorization Required\r\n");
756         wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
757         wprintf("Content-Type: text/html\r\n\r\n");
758         wprintf("<h1>");
759         wprintf(_("Authorization Required"));
760         wprintf("</h1>\r\n");
761         wprintf(_("The resource you requested requires a valid username and password. "
762                 "You could not be logged in: %s\n"), message);
763         wDumpContent(0);
764 }
765
766
767 void upload_handler(char *name, char *filename, char *partnum, char *disp,
768                         void *content, char *cbtype, char *cbcharset,
769                         size_t length, char *encoding, void *userdata)
770 {
771         struct urlcontent *u;
772
773         lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
774                 name, cbtype, length);
775
776         /* Form fields */
777         if ( (length > 0) && (strlen(cbtype) == 0) ) {
778                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
779                 u->next = WC->urlstrings;
780                 WC->urlstrings = u;
781                 safestrncpy(u->url_key, name, sizeof(u->url_key));
782                 u->url_data = malloc(length + 1);
783                 memcpy(u->url_data, content, length);
784                 u->url_data[length] = 0;
785         }
786
787         /* Uploaded files */
788         if ( (length > 0) && (strlen(cbtype) > 0) ) {
789                 WC->upload = malloc(length);
790                 if (WC->upload != NULL) {
791                         WC->upload_length = length;
792                         safestrncpy(WC->upload_filename, filename,
793                                         sizeof(WC->upload_filename));
794                         safestrncpy(WC->upload_content_type, cbtype,
795                                         sizeof(WC->upload_content_type));
796                         memcpy(WC->upload, content, length);
797                 }
798                 else {
799                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
800                 }
801         }
802
803 }
804
805
806
807
808 /*
809  * Entry point for WebCit transaction
810  */
811 void session_loop(struct httprequest *req)
812 {
813         char cmd[1024];
814         char action[128];
815         char arg1[128];
816         char arg2[128];
817         char arg3[128];
818         char buf[SIZ];
819         int a, b;
820         int ContentLength = 0;
821         int BytesRead = 0;
822         char ContentType[512];
823         char *content = NULL;
824         char *content_end = NULL;
825         struct httprequest *hptr;
826         char browser_host[SIZ];
827         char user_agent[SIZ];
828         int body_start = 0;
829
830         /* We stuff these with the values coming from the client cookies,
831          * so we can use them to reconnect a timed out session if we have to.
832          */
833         char c_username[SIZ];
834         char c_password[SIZ];
835         char c_roomname[SIZ];
836         char c_httpauth_string[SIZ];
837         char c_httpauth_user[SIZ];
838         char c_httpauth_pass[SIZ];
839         char cookie[SIZ];
840
841         safestrncpy(c_username, "", sizeof c_username);
842         safestrncpy(c_password, "", sizeof c_password);
843         safestrncpy(c_roomname, "", sizeof c_roomname);
844         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
845         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
846         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
847
848         WC->upload_length = 0;
849         WC->upload = NULL;
850         WC->vars = NULL;
851
852         WC->is_wap = 0;
853
854         hptr = req;
855         if (hptr == NULL) return;
856
857         safestrncpy(cmd, hptr->line, sizeof cmd);
858         hptr = hptr->next;
859         extract_token(request_method, cmd, 0, ' ', sizeof request_method);
860
861         /* Figure out the action */
862         extract_token(action, cmd, 1, '/', sizeof action);
863         if (strstr(action, "?")) *strstr(action, "?") = 0;
864         if (strstr(action, "&")) *strstr(action, "&") = 0;
865         if (strstr(action, " ")) *strstr(action, " ") = 0;
866
867         extract_token(arg1, cmd, 2, '/', sizeof arg1);
868         if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
869         if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
870         if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
871
872         extract_token(arg2, cmd, 3, '/', sizeof arg2);
873         if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
874         if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
875         if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
876
877         extract_token(arg3, cmd, 4, '/', sizeof arg3);
878         if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
879         if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
880         if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
881
882         while (hptr != NULL) {
883                 safestrncpy(buf, hptr->line, sizeof buf);
884                 hptr = hptr->next;
885
886                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
887                         safestrncpy(cookie, &buf[15], sizeof cookie);
888                         cookie_to_stuff(cookie, NULL,
889                                         c_username, sizeof c_username,
890                                         c_password, sizeof c_password,
891                                         c_roomname, sizeof c_roomname);
892                 }
893                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
894                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
895                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
896                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
897                 }
898                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
899                         ContentLength = atoi(&buf[16]);
900                 }
901                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
902                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
903                 }
904                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
905                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
906                 }
907                 else if (!strncasecmp(buf, "Host: ", 6)) {
908                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
909                 }
910                 /* Only WAP gateways explicitly name this content-type */
911                 else if (strstr(buf, "text/vnd.wap.wml")) {
912                         WC->is_wap = 1;
913                 }
914         }
915
916         if (ContentLength > 0) {
917                 content = malloc(ContentLength + SIZ);
918                 memset(content, 0, ContentLength + SIZ);
919                 sprintf(content, "Content-type: %s\n"
920                                 "Content-length: %d\n\n",
921                                 ContentType, ContentLength);
922                 body_start = strlen(content);
923
924                 /* Read the entire input data at once. */
925                 client_read(WC->http_sock, &content[BytesRead+body_start],
926                         ContentLength);
927
928                 if (!strncasecmp(ContentType,
929                               "application/x-www-form-urlencoded", 33)) {
930                         addurls(&content[body_start]);
931                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
932                         content_end = content + ContentLength + body_start;
933                         mime_parser(content, content_end, *upload_handler,
934                                         NULL, NULL, NULL, 0);
935                 }
936         } else {
937                 content = NULL;
938         }
939
940         /* make a note of where we are in case the user wants to save it */
941         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
942         remove_token(WC->this_page, 2, ' ');
943         remove_token(WC->this_page, 0, ' ');
944
945         /* If there are variables in the URL, we must grab them now */
946         for (a = 0; a < strlen(cmd); ++a) {
947                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
948                         for (b = a; b < strlen(cmd); ++b)
949                                 if (isspace(cmd[b]))
950                                         cmd[b] = 0;
951                         addurls(&cmd[a + 1]);
952                         cmd[a] = 0;
953                 }
954         }
955
956
957         /* Static content can be sent without connecting to Citadel. */
958         if (!strcasecmp(action, "static")) {
959                 safestrncpy(buf, arg1, sizeof buf);
960                 for (a = 0; a < strlen(buf); ++a)
961                         if (isspace(buf[a]))
962                                 buf[a] = 0;
963                 output_static(buf);
964                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
965         }
966
967         /*
968          * If we're not connected to a Citadel server, try to hook up the
969          * connection now.
970          */
971         if (!WC->connected) {
972                 if (!strcasecmp(ctdlhost, "uds")) {
973                         /* unix domain socket */
974                         sprintf(buf, "%s/citadel.socket", ctdlport);
975                         WC->serv_sock = uds_connectsock(buf);
976                 }
977                 else {
978                         /* tcp socket */
979                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
980                 }
981
982                 if (WC->serv_sock < 0) {
983                         do_logout();
984                         goto SKIP_ALL_THIS_CRAP;
985                 }
986                 else {
987                         WC->connected = 1;
988                         serv_getln(buf, sizeof buf);    /* get the server welcome message */
989                         locate_host(browser_host, WC->http_sock);
990                         get_serv_info(browser_host, user_agent);
991                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
992                                 wprintf(_("You are connected to a Citadel "
993                                         "server running Citadel %d.%02d. \n"
994                                         "In order to run this version of WebCit "
995                                         "you must also have Citadel %d.%02d or"
996                                         " newer.\n\n\n"),
997                                                 serv_info.serv_rev_level / 100,
998                                                 serv_info.serv_rev_level % 100,
999                                                 MINIMUM_CIT_VERSION / 100,
1000                                                 MINIMUM_CIT_VERSION % 100
1001                                         );
1002                                 end_webcit_session();
1003                                 goto SKIP_ALL_THIS_CRAP;
1004                         }
1005                 }
1006         }
1007
1008         /*
1009          * Functions which can be performed without logging in
1010          */
1011         if (!strcasecmp(action, "listsub")) {
1012                 do_listsub();
1013                 goto SKIP_ALL_THIS_CRAP;
1014         }
1015 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1016         if (!strcasecmp(action, "freebusy")) {
1017                 do_freebusy(cmd);
1018                 goto SKIP_ALL_THIS_CRAP;
1019         }
1020 #endif
1021
1022         /*
1023          * If we're not logged in, but we have HTTP Authentication data,
1024          * try logging in to Citadel using that.
1025          */
1026         if ((!WC->logged_in)
1027            && (strlen(c_httpauth_user) > 0)
1028            && (strlen(c_httpauth_pass) > 0)) {
1029                 serv_printf("USER %s", c_httpauth_user);
1030                 serv_getln(buf, sizeof buf);
1031                 if (buf[0] == '3') {
1032                         serv_printf("PASS %s", c_httpauth_pass);
1033                         serv_getln(buf, sizeof buf);
1034                         if (buf[0] == '2') {
1035                                 become_logged_in(c_httpauth_user,
1036                                                 c_httpauth_pass, buf);
1037                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1038                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1039                         } else {
1040                                 /* Should only display when password is wrong */
1041                                 authorization_required(&buf[4]);
1042                                 goto SKIP_ALL_THIS_CRAP;
1043                         }
1044                 }
1045         }
1046
1047         /* This needs to run early */
1048         if (!strcasecmp(action, "rss")) {
1049                 display_rss(bstr("room"));
1050                 goto SKIP_ALL_THIS_CRAP;
1051         }
1052
1053         /* 
1054          * The GroupDAV stuff relies on HTTP authentication instead of
1055          * our session's authentication.
1056          */
1057         if (!strncasecmp(action, "groupdav", 8)) {
1058                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1059                         ContentLength, content+body_start);
1060                 if (!WC->logged_in) {
1061                         WC->killthis = 1;       /* If not logged in, don't */
1062                 }                               /* keep the session active */
1063                 goto SKIP_ALL_THIS_CRAP;
1064         }
1065
1066
1067         /*
1068          * Automatically send requests with any method other than GET or
1069          * POST to the GroupDAV code as well.
1070          */
1071         if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1072                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1073                         ContentLength, content+body_start);
1074                 if (!WC->logged_in) {
1075                         WC->killthis = 1;       /* If not logged in, don't */
1076                 }                               /* keep the session active */
1077                 goto SKIP_ALL_THIS_CRAP;
1078         }
1079
1080         /*
1081          * If we're not logged in, but we have username and password cookies
1082          * supplied by the browser, try using them to log in.
1083          */
1084         if ((!WC->logged_in)
1085            && (strlen(c_username) > 0)
1086            && (strlen(c_password) > 0)) {
1087                 serv_printf("USER %s", c_username);
1088                 serv_getln(buf, sizeof buf);
1089                 if (buf[0] == '3') {
1090                         serv_printf("PASS %s", c_password);
1091                         serv_getln(buf, sizeof buf);
1092                         if (buf[0] == '2') {
1093                                 become_logged_in(c_username, c_password, buf);
1094                         }
1095                 }
1096         }
1097         /*
1098          * If we don't have a current room, but a cookie specifying the
1099          * current room is supplied, make an effort to go there.
1100          */
1101         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1102                 serv_printf("GOTO %s", c_roomname);
1103                 serv_getln(buf, sizeof buf);
1104                 if (buf[0] == '2') {
1105                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1106                 }
1107         }
1108
1109         /*
1110          * If there are instant messages waiting, retrieve them for display.
1111          */
1112         check_for_instant_messages();
1113
1114         if (!strcasecmp(action, "image")) {
1115                 output_image();
1116
1117         /*
1118          * All functions handled below this point ... make sure we log in
1119          * before doing anything else!
1120          */
1121         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1122                 do_login();
1123         } else if (!WC->logged_in) {
1124                 display_login(NULL);
1125         }
1126
1127         /*
1128          * Various commands...
1129          */
1130
1131         else if (!strcasecmp(action, "do_welcome")) {
1132                 do_welcome();
1133         } else if (!strcasecmp(action, "blank")) {
1134                 blank_page();
1135         } else if (!strcasecmp(action, "do_template")) {
1136                 url_do_template();
1137         } else if (!strcasecmp(action, "display_aide_menu")) {
1138                 display_aide_menu();
1139         } else if (!strcasecmp(action, "display_main_menu")) {
1140                 display_main_menu();
1141         } else if (!strcasecmp(action, "who")) {
1142                 who();
1143         } else if (!strcasecmp(action, "who_inner_html")) {
1144                 who_inner_html();
1145         } else if (!strcasecmp(action, "knrooms")) {
1146                 knrooms();
1147         } else if (!strcasecmp(action, "gotonext")) {
1148                 slrp_highest();
1149                 gotonext();
1150         } else if (!strcasecmp(action, "skip")) {
1151                 gotonext();
1152         } else if (!strcasecmp(action, "ungoto")) {
1153                 ungoto();
1154         } else if (!strcasecmp(action, "dotgoto")) {
1155                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1156                         slrp_highest();
1157                 }
1158                 smart_goto(bstr("room"));
1159         } else if (!strcasecmp(action, "dotskip")) {
1160                 smart_goto(bstr("room"));
1161         } else if (!strcasecmp(action, "termquit")) {
1162                 do_logout();
1163         } else if (!strcasecmp(action, "readnew")) {
1164                 readloop("readnew");
1165         } else if (!strcasecmp(action, "readold")) {
1166                 readloop("readold");
1167         } else if (!strcasecmp(action, "readfwd")) {
1168                 readloop("readfwd");
1169         } else if (!strcasecmp(action, "headers")) {
1170                 readloop("headers");
1171         } else if (!strcasecmp(action, "msg")) {
1172                 embed_message();
1173         } else if (!strcasecmp(action, "display_enter")) {
1174                 display_enter();
1175         } else if (!strcasecmp(action, "post")) {
1176                 post_message();
1177         } else if (!strcasecmp(action, "move_msg")) {
1178                 move_msg();
1179         } else if (!strcasecmp(action, "delete_msg")) {
1180                 delete_msg();
1181         } else if (!strcasecmp(action, "userlist")) {
1182                 userlist();
1183         } else if (!strcasecmp(action, "showuser")) {
1184                 showuser();
1185         } else if (!strcasecmp(action, "display_page")) {
1186                 display_page();
1187         } else if (!strcasecmp(action, "page_user")) {
1188                 page_user();
1189         } else if (!strcasecmp(action, "chat")) {
1190                 do_chat();
1191         } else if (!strcasecmp(action, "display_private")) {
1192                 display_private("", 0);
1193         } else if (!strcasecmp(action, "goto_private")) {
1194                 goto_private();
1195         } else if (!strcasecmp(action, "zapped_list")) {
1196                 zapped_list();
1197         } else if (!strcasecmp(action, "display_zap")) {
1198                 display_zap();
1199         } else if (!strcasecmp(action, "zap")) {
1200                 zap();
1201         } else if (!strcasecmp(action, "display_entroom")) {
1202                 display_entroom();
1203         } else if (!strcasecmp(action, "entroom")) {
1204                 entroom();
1205         } else if (!strcasecmp(action, "display_whok")) {
1206                 display_whok();
1207         } else if (!strcasecmp(action, "do_invt_kick")) {
1208                 do_invt_kick();
1209         } else if (!strcasecmp(action, "display_editroom")) {
1210                 display_editroom();
1211         } else if (!strcasecmp(action, "netedit")) {
1212                 netedit();
1213         } else if (!strcasecmp(action, "editroom")) {
1214                 editroom();
1215         } else if (!strcasecmp(action, "display_editinfo")) {
1216                 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1217         } else if (!strcasecmp(action, "editinfo")) {
1218                 save_edit(_("Room info"), "EINF 1", 1);
1219         } else if (!strcasecmp(action, "display_editbio")) {
1220                 sprintf(buf, "RBIO %s", WC->wc_username);
1221                 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1222         } else if (!strcasecmp(action, "editbio")) {
1223                 save_edit(_("Your bio"), "EBIO", 0);
1224         } else if (!strcasecmp(action, "confirm_move_msg")) {
1225                 confirm_move_msg();
1226         } else if (!strcasecmp(action, "delete_room")) {
1227                 delete_room();
1228         } else if (!strcasecmp(action, "validate")) {
1229                 validate();
1230         } else if (!strcasecmp(action, "display_editpic")) {
1231                 display_graphics_upload(_("your photo"),
1232                                         "UIMG 0|_userpic_",
1233                                         "/editpic");
1234         } else if (!strcasecmp(action, "editpic")) {
1235                 do_graphics_upload("UIMG 1|_userpic_");
1236         } else if (!strcasecmp(action, "display_editroompic")) {
1237                 display_graphics_upload(_("the icon for this room"),
1238                                         "UIMG 0|_roompic_",
1239                                         "/editroompic");
1240         } else if (!strcasecmp(action, "editroompic")) {
1241                 do_graphics_upload("UIMG 1|_roompic_");
1242         } else if (!strcasecmp(action, "delete_floor")) {
1243                 delete_floor();
1244         } else if (!strcasecmp(action, "rename_floor")) {
1245                 rename_floor();
1246         } else if (!strcasecmp(action, "create_floor")) {
1247                 create_floor();
1248         } else if (!strcasecmp(action, "display_editfloorpic")) {
1249                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1250                         bstr("which_floor"));
1251                 display_graphics_upload(_("the icon for this floor"),
1252                                         buf,
1253                                         "/editfloorpic");
1254         } else if (!strcasecmp(action, "editfloorpic")) {
1255                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1256                         bstr("which_floor"));
1257                 do_graphics_upload(buf);
1258         } else if (!strcasecmp(action, "display_reg")) {
1259                 display_reg(0);
1260         } else if (!strcasecmp(action, "display_changepw")) {
1261                 display_changepw();
1262         } else if (!strcasecmp(action, "changepw")) {
1263                 changepw();
1264         } else if (!strcasecmp(action, "display_edit_node")) {
1265                 display_edit_node();
1266         } else if (!strcasecmp(action, "edit_node")) {
1267                 edit_node();
1268         } else if (!strcasecmp(action, "display_netconf")) {
1269                 display_netconf();
1270         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1271                 display_confirm_delete_node();
1272         } else if (!strcasecmp(action, "delete_node")) {
1273                 delete_node();
1274         } else if (!strcasecmp(action, "display_add_node")) {
1275                 display_add_node();
1276         } else if (!strcasecmp(action, "add_node")) {
1277                 add_node();
1278         } else if (!strcasecmp(action, "terminate_session")) {
1279                 slrp_highest();
1280                 terminate_session();
1281         } else if (!strcasecmp(action, "edit_me")) {
1282                 edit_me();
1283         } else if (!strcasecmp(action, "display_siteconfig")) {
1284                 display_siteconfig();
1285         } else if (!strcasecmp(action, "chat_recv")) {
1286                 chat_recv();
1287         } else if (!strcasecmp(action, "chat_send")) {
1288                 chat_send();
1289         } else if (!strcasecmp(action, "siteconfig")) {
1290                 siteconfig();
1291         } else if (!strcasecmp(action, "display_generic")) {
1292                 display_generic();
1293         } else if (!strcasecmp(action, "do_generic")) {
1294                 do_generic();
1295         } else if (!strcasecmp(action, "display_menubar")) {
1296                 display_menubar(1);
1297         } else if (!strcasecmp(action, "output_mimepart")) {
1298                 output_mimepart();
1299         } else if (!strcasecmp(action, "edit_vcard")) {
1300                 edit_vcard();
1301         } else if (!strcasecmp(action, "submit_vcard")) {
1302                 submit_vcard();
1303         } else if (!strcasecmp(action, "select_user_to_edit")) {
1304                 select_user_to_edit(NULL, NULL);
1305         } else if (!strcasecmp(action, "display_edituser")) {
1306                 display_edituser(NULL, 0);
1307         } else if (!strcasecmp(action, "edituser")) {
1308                 edituser();
1309         } else if (!strcasecmp(action, "create_user")) {
1310                 create_user();
1311         } else if (!strcasecmp(action, "changeview")) {
1312                 change_view();
1313         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1314                 do_stuff_to_msgs();
1315         } else if (!strcasecmp(action, "change_start_page")) {
1316                 change_start_page();
1317         } else if (!strcasecmp(action, "display_floorconfig")) {
1318                 display_floorconfig(NULL);
1319         } else if (!strcasecmp(action, "toggle_self_service")) {
1320                 toggle_self_service();
1321 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1322         } else if (!strcasecmp(action, "display_edit_task")) {
1323                 display_edit_task();
1324         } else if (!strcasecmp(action, "save_task")) {
1325                 save_task();
1326         } else if (!strcasecmp(action, "display_edit_event")) {
1327                 display_edit_event();
1328         } else if (!strcasecmp(action, "save_event")) {
1329                 save_event();
1330         } else if (!strcasecmp(action, "respond_to_request")) {
1331                 respond_to_request();
1332         } else if (!strcasecmp(action, "handle_rsvp")) {
1333                 handle_rsvp();
1334 #endif
1335         } else if (!strcasecmp(action, "summary")) {
1336                 summary();
1337         } else if (!strcasecmp(action, "iconbar")) {
1338                 do_iconbar();
1339         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1340                 display_customize_iconbar();
1341         } else if (!strcasecmp(action, "commit_iconbar")) {
1342                 commit_iconbar();
1343         } else if (!strcasecmp(action, "set_room_policy")) {
1344                 set_room_policy();
1345         } else if (!strcasecmp(action, "display_inetconf")) {
1346                 display_inetconf();
1347         } else if (!strcasecmp(action, "save_inetconf")) {
1348                 save_inetconf();
1349         } else if (!strcasecmp(action, "setup_wizard")) {
1350                 do_setup_wizard();
1351         } else if (!strcasecmp(action, "display_preferences")) {
1352                 display_preferences();
1353         } else if (!strcasecmp(action, "set_preferences")) {
1354                 set_preferences();
1355         } else if (!strcasecmp(action, "recp_autocomplete")) {
1356                 recp_autocomplete();
1357         } else if (!strcasecmp(action, "diagnostics")) {
1358                 output_headers(1, 1, 1, 0, 0, 0, 0);
1359                 wprintf("Session: %d<hr />\n", WC->wc_session);
1360                 wprintf("Command: <br /><PRE>\n");
1361                 escputs(cmd);
1362                 wprintf("</PRE><hr />\n");
1363                 wprintf("Variables: <br /><PRE>\n");
1364                 dump_vars();
1365                 wprintf("</PRE><hr />\n");
1366                 wDumpContent(1);
1367         }
1368
1369         /* When all else fais, display the main menu. */
1370         else {
1371                 display_main_menu();
1372         }
1373
1374 SKIP_ALL_THIS_CRAP:
1375         fflush(stdout);
1376         if (content != NULL) {
1377                 free(content);
1378                 content = NULL;
1379         }
1380         free_urls();
1381         if (WC->upload_length > 0) {
1382                 free(WC->upload);
1383                 WC->upload_length = 0;
1384         }
1385
1386 }