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