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