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