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