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