]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* Rewrote the HTTP engine and application coupling to run in a worker thread
[citadel.git] / webcit / webcit.c
1 /*
2  * webcit.c
3  *
4  * This is the actual program called by the webserver.  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  * $Id$
9  */
10
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <sys/socket.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <limits.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include "webcit.h"
32
33
34
35 int TransactionCount = 0;
36 char *ExpressMessages = NULL;
37
38 /* This variable is set to 1 if the room banner and menubar have been
39  * displayed, and we need to close the <TABLE> tags.
40  */
41 int fake_frames = 0;
42
43 struct urlcontent *urlstrings = NULL;
44
45 static const char *defaulthost = DEFAULT_HOST;
46 static const char *defaultport = DEFAULT_PORT;
47
48
49
50 void unescape_input(char *buf)
51 {
52         int a, b;
53         char hex[3];
54
55         while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
56                 buf[strlen(buf) - 1] = 0;
57
58         for (a = 0; a < strlen(buf); ++a) {
59                 if (buf[a] == '+')
60                         buf[a] = ' ';
61                 if (buf[a] == '%') {
62                         hex[0] = buf[a + 1];
63                         hex[1] = buf[a + 2];
64                         hex[2] = 0;
65                         sscanf(hex, "%02x", &b);
66                         buf[a] = (char) b;
67                         strcpy(&buf[a + 1], &buf[a + 3]);
68                 }
69         }
70
71 }
72
73
74 void addurls(char *url)
75 {
76         char *up, *ptr;
77         char buf[256];
78         int a, b;
79         struct urlcontent *u;
80
81         up = url;
82         while (strlen(up) > 0) {
83
84                 /* locate the = sign */
85                 strncpy(buf, up, 255);
86                 b = (-1);
87                 for (a = 255; a >= 0; --a)
88                         if (buf[a] == '=')
89                                 b = a;
90                 if (b < 0)
91                         return;
92                 buf[b] = 0;
93
94                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
95                 u->next = urlstrings;
96                 urlstrings = u;
97                 strcpy(u->url_key, buf);
98
99                 /* now chop that part off */
100                 for (a = 0; a <= b; ++a)
101                         ++up;
102
103                 /* locate the & sign */
104                 ptr = up;
105                 b = strlen(up);
106                 for (a = 0; a < strlen(up); ++a) {
107                         if (!strncmp(ptr, "&", 1)) {
108                                 b = a;
109                                 break;
110                         }
111                         ++ptr;
112                 }
113                 ptr = up;
114                 for (a = 0; a < b; ++a)
115                         ++ptr;
116                 strcpy(ptr, "");
117
118                 u->url_data = malloc(strlen(up) + 1);
119                 strcpy(u->url_data, up);
120                 u->url_data[b] = 0;
121                 unescape_input(u->url_data);
122                 up = ptr;
123                 ++up;
124         }
125 }
126
127 void free_urls(void)
128 {
129         struct urlcontent *u;
130
131         while (urlstrings != NULL) {
132                 free(urlstrings->url_data);
133                 u = urlstrings->next;
134                 free(urlstrings);
135                 urlstrings = u;
136         }
137 }
138
139 /*
140  * Diagnostic function to display the contents of all variables
141  */
142 void dump_vars(void)
143 {
144         struct urlcontent *u;
145
146         for (u = urlstrings; u != NULL; u = u->next) {
147                 wprintf("%38s = %s\n", u->url_key, u->url_data);
148         }
149 }
150
151 char *bstr(char *key)
152 {
153         struct urlcontent *u;
154
155         for (u = urlstrings; u != NULL; u = u->next) {
156                 if (!strcasecmp(u->url_key, key))
157                         return (u->url_data);
158         }
159         return ("");
160 }
161
162
163 void wprintf(const char *format,...)
164 {
165         va_list arg_ptr;
166         char wbuf[1024];
167
168         va_start(arg_ptr, format);
169         vsprintf(wbuf, format, arg_ptr);
170         va_end(arg_ptr);
171
172         write(WC->http_sock, wbuf, strlen(wbuf));
173 }
174
175
176 /*
177  * wDumpContent() wraps up an HTTP session, closes tags, etc.
178  *
179  * print_standard_html_footer should be set to 0 to transmit only, 1 to
180  * append the main menu and closing tags, or 2 to
181  * append the closing tags only.
182  */
183 void wDumpContent(int print_standard_html_footer)
184 {
185         if (fake_frames) {
186                 wprintf("<CENTER><FONT SIZE=-1>"
187                         "<TABLE border=0 width=100%><TR>"
188                         "<TD><A HREF=\"/ungoto\">"
189                         "<IMG SRC=\"/static/back.gif\" BORDER=0>"
190                         "Ungoto</A></TD>");
191                 wprintf("<TD><A HREF=\"#TheTop\">"
192                         "<IMG SRC=\"/static/up.gif\" BORDER=0>"
193                         "Top of page</A></TD>");
194                 wprintf("<TD><A HREF=\"/display_enter\">"
195                         "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
196                         "Enter a message</A></TD>");
197                 wprintf("<TD><A HREF=\"/gotonext\">"
198                         "Goto next room"
199                         "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
200                         "</TR></TABLE>"
201                         "</FONT>\n"
202                         "</TD></TR></TABLE></TABLE>\n");
203                 fake_frames = 0;
204         }
205
206         if (print_standard_html_footer) {
207                 if (print_standard_html_footer != 2) {
208                         wprintf("<BR>");
209                 }
210                 wprintf("</BODY></HTML>\n");
211         }
212
213
214 }
215
216
217 void escputs1(char *strbuf, int nbsp)
218 {
219         int a;
220
221         for (a = 0; a < strlen(strbuf); ++a) {
222                 if (strbuf[a] == '<')
223                         wprintf("&lt;");
224                 else if (strbuf[a] == '>')
225                         wprintf("&gt;");
226                 else if (strbuf[a] == '&')
227                         wprintf("&amp;");
228                 else if (strbuf[a] == '\"')
229                         wprintf("&quot;");
230                 else if (strbuf[a] == '\'') 
231                         wprintf("&#39;");
232                 else if (strbuf[a] == LB)
233                         wprintf("<");
234                 else if (strbuf[a] == RB)
235                         wprintf(">");
236                 else if (strbuf[a] == QU)
237                         wprintf("\"");
238                 else if ((strbuf[a] == 32) && (nbsp == 1)) {
239                         wprintf("&nbsp;");
240                 } else {
241                         wprintf("%c", strbuf[a]);
242                 }
243         }
244 }
245
246 void escputs(char *strbuf)
247 {
248         escputs1(strbuf, 0);
249 }
250
251
252
253 char *urlesc(char *strbuf)
254 {
255         int a, b, c;
256         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
257         static char outbuf[512];
258
259         strcpy(outbuf, "");
260
261         for (a = 0; a < strlen(strbuf); ++a) {
262                 c = 0;
263                 for (b = 0; b < strlen(ec); ++b) {
264                         if (strbuf[a] == ec[b])
265                                 c = 1;
266                 }
267                 b = strlen(outbuf);
268                 if (c == 1)
269                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
270                 else
271                         sprintf(&outbuf[b], "%c", strbuf[a]);
272         }
273         return (outbuf);
274 }
275
276 void urlescputs(char *strbuf)
277 {
278         wprintf("%s", urlesc(strbuf));
279 }
280
281
282
283
284 /*
285  * Output all that important stuff that the browser will want to see
286  *
287  * print_standard_html_head values:
288  * 0 = Nothing.  Do not display any leading HTTP or HTML.
289  * 1 = HTTP headers plus the "fake frames" found in most windows.
290  * 2 = HTTP headers required to terminate the session (unset cookies)
291  * 3 = HTTP headers only.
292  */
293 void output_headers(int print_standard_html_head)
294 {
295
296         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
297         char cookie[256];
298
299         wprintf("Content-type: text/html\n");
300         wprintf("Server: %s\n", SERVER);
301         wprintf("Connection: close\n");
302
303         if (print_standard_html_head > 0) {
304                 wprintf("Pragma: no-cache\n");
305                 wprintf("Cache-Control: no-store\n");
306         }
307         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
308                         WC->wc_password, WC->wc_roomname);
309         if (print_standard_html_head == 2) {
310                 wprintf("X-WebCit-Session: close\n");
311                 wprintf("Set-cookie: webcit=%s\n", unset);
312         } else {
313                 wprintf("Set-cookie: webcit=%s\n", cookie);
314         }
315
316         wprintf("\n");
317
318         if (print_standard_html_head > 0) {
319                 wprintf("<HTML><HEAD><TITLE>");
320                 escputs(serv_info.serv_humannode);
321                 wprintf("</TITLE>\n"
322                         "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
323                         "</HEAD>\n");
324                 if (ExpressMessages != NULL) {
325                         wprintf("<SCRIPT language=\"javascript\">\n");
326                         wprintf("function ExpressMessage() {\n");
327                         wprintf(" alert(\"");
328                         escputs(ExpressMessages);
329                         wprintf("\")\n");
330                         wprintf(" }\n </SCRIPT>\n");
331                 }
332
333
334
335                 /* JavaScript key-based navigation would go here if it
336                  * were finished
337                  */
338
339                 wprintf("<BODY ");
340                 if (ExpressMessages != NULL) {
341                         wprintf("onload=\"ExpressMessage()\" ");
342                         free(ExpressMessages);
343                         ExpressMessages = NULL;
344                 }
345                 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
346         
347         
348         if (print_standard_html_head == 1) {
349                 wprintf("<A NAME=\"TheTop\"></A>"
350                         "<TABLE border=0 width=100%>"
351                         "<TR VALIGN=TOP><TD>");
352
353                 display_menubar(0);
354
355                 wprintf("</TD><TD VALIGN=TOP>"
356                         "<TABLE border=0 width=100%><TR VALIGN=TOP>"
357                         "<TD>\n");
358
359                 embed_room_banner(NULL);
360
361                 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
362                 
363                 fake_frames = 1;
364                 }
365         }
366 }
367
368
369
370 void ExpressMessageCat(char *buf) {
371         if (ExpressMessages == NULL) {
372                 ExpressMessages = malloc(strlen(buf) + 4);
373                 strcpy(ExpressMessages, "");
374         } else {
375                 ExpressMessages = realloc(ExpressMessages,
376                         (strlen(ExpressMessages) + strlen(buf) + 4));
377         }
378         strcat(ExpressMessages, buf);
379         strcat(ExpressMessages, "\\n");
380 }
381
382
383 void check_for_express_messages()
384 {
385         char buf[256];
386
387         serv_puts("PEXP");
388         serv_gets(buf);
389         if (buf[0] == '1') {
390                 while (serv_gets(buf), strcmp(buf, "000")) {
391                         ExpressMessageCat(buf);
392                 }
393         }
394 }
395
396
397
398
399 void output_static(char *what)
400 {
401         char buf[256];
402         FILE *fp;
403         struct stat statbuf;
404         off_t bytes;
405
406         sprintf(buf, "static/%s", what);
407         fp = fopen(buf, "rb");
408         if (fp == NULL) {
409                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
410                 wprintf("Content-Type: text/plain\n");
411                 wprintf("\n");
412                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
413         } else {
414                 wprintf("HTTP/1.0 200 OK\n");
415                 output_headers(0);
416
417                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
418                         wprintf("Content-type: image/gif\n");
419                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
420                         wprintf("Content-type: text/plain\n");
421                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
422                         wprintf("Content-type: image/jpeg\n");
423                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
424                         wprintf("Content-type: text/html\n");
425                 else
426                         wprintf("Content-type: application/octet-stream\n");
427
428                 fstat(fileno(fp), &statbuf);
429                 bytes = statbuf.st_size;
430                 wprintf("Content-length: %ld\n", (long) bytes);
431                 printf("\n");
432                 while (bytes--) {
433                         wprintf("%c", getc(fp) );
434                 }
435                 fclose(fp);
436         }
437 }
438
439 void output_image()
440 {
441         char buf[256];
442         char xferbuf[4096];
443         off_t bytes;
444         off_t thisblock;
445         off_t accomplished = 0L;
446
447
448         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
449         serv_gets(buf);
450         if (buf[0] == '2') {
451                 bytes = extract_long(&buf[4], 0);
452                 wprintf("HTTP/1.0 200 OK\n");
453                 output_headers(0);
454                 wprintf("Content-type: image/gif\n");
455                 wprintf("Content-length: %ld\n", (long) bytes);
456                 wprintf("\n");
457
458                 while (bytes > (off_t) 0) {
459                         thisblock = (off_t) sizeof(xferbuf);
460                         if (thisblock > bytes)
461                                 thisblock = bytes;
462                         serv_printf("READ %ld|%ld", accomplished, thisblock);
463                         serv_gets(buf);
464                         if (buf[0] == '6')
465                                 thisblock = extract_long(&buf[4], 0);
466                         serv_read(xferbuf, (int) thisblock);
467                         write(WC->http_sock, xferbuf, thisblock);
468                         bytes = bytes - thisblock;
469                         accomplished = accomplished + thisblock;
470                 }
471                 fflush(stdout);
472                 serv_puts("CLOS");
473                 serv_gets(buf);
474         } else {
475                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
476                 output_headers(0);
477                 wprintf("Content-Type: text/plain\n");
478                 wprintf("\n");
479                 wprintf("Error retrieving image\n");
480         }
481
482 }
483
484
485 /*
486  * Convenience functions to display a page containing only a string
487  */
488 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
489 {
490         wprintf("HTTP/1.0 200 OK\n");
491         output_headers(1);
492         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
493         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
494         wprintf("<B>%s</B>\n", titlebarmsg);
495         wprintf("</FONT></TD></TR></TABLE><BR>\n");
496         escputs(messagetext);
497
498         wprintf("<HR>\n");
499         embed_main_menu();
500         wDumpContent(1);
501 }
502
503 void display_error(char *errormessage)
504 {
505         convenience_page("770000", "Error", errormessage);
506 }
507
508 void display_success(char *successmessage)
509 {
510         convenience_page("007700", "OK", successmessage);
511 }
512
513
514
515 void extract_action(char *actbuf, char *cmdbuf)
516 {
517         int i;
518
519         strcpy(actbuf, cmdbuf);
520         if (!strncasecmp(actbuf, "GET /", 5))
521                 strcpy(actbuf, &actbuf[5]);
522         if (!strncasecmp(actbuf, "PUT /", 5))
523                 strcpy(actbuf, &actbuf[5]);
524         if (!strncasecmp(actbuf, "POST /", 6))
525                 strcpy(actbuf, &actbuf[6]);
526
527         for (i = 0; i < strlen(actbuf); ++i) {
528                 if (actbuf[i] == ' ') {
529                         actbuf[i] = 0;
530                         i = 0;
531                 }
532                 if (actbuf[i] == '/') {
533                         actbuf[i] = 0;
534                         i = 0;
535                 }
536                 if (actbuf[i] == '?') {
537                         actbuf[i] = 0;
538                         i = 0;
539                 }
540                 if (actbuf[i] == '&') {
541                         actbuf[i] = 0;
542                         i = 0;
543                 }
544         }
545 }
546
547
548 void upload_handler(char *name, char *filename, char *encoding,
549                     void *content, char *cbtype, size_t length)
550 {
551
552         fprintf(stderr, "UPLOAD HANDLER CALLED\n");
553         fprintf(stderr, "    name = %s\n", name);
554         fprintf(stderr, "filename = %s\n", filename);
555         fprintf(stderr, "encoding = %s\n", encoding);
556         fprintf(stderr, "    type = %s\n", cbtype);
557         fprintf(stderr, "  length = %d\n", length);
558
559         if (strlen(name) > 0) {
560                 WC->upload = malloc(length);
561                 if (WC->upload != NULL) {
562                         WC->upload_length = length;
563                         memcpy(WC->upload, content, length);
564                 }
565         }
566 }
567
568
569 /*
570  * Entry point for WebCit transaction
571  */
572 void session_loop(struct httprequest *req)
573 {
574         char cmd[256];
575         char action[256];
576         char buf[256];
577         int a, b;
578         int ContentLength = 0;
579         char ContentType[512];
580         char *content;
581         struct httprequest *hptr;
582         char browser_host[256];
583         char user_agent[256];
584
585         /* We stuff these with the values coming from the client cookies,
586          * so we can use them to reconnect a timed out session if we have to.
587          */
588         char c_host[256];
589         char c_port[256];
590         char c_username[256];
591         char c_password[256];
592         char c_roomname[256];
593         char cookie[256];
594
595         strcpy(c_host, defaulthost);
596         strcpy(c_port, defaultport);
597         strcpy(c_username, "");
598         strcpy(c_password, "");
599         strcpy(c_roomname, "");
600
601         WC->upload_length = 0;
602         WC->upload = NULL;
603
604         hptr = req;
605         if (hptr == NULL) return;
606
607         strcpy(cmd, hptr->line);
608         hptr = hptr->next;
609         extract_action(action, cmd);
610
611         while (hptr != NULL) {
612                 strcpy(buf, hptr->line);
613                 hptr = hptr->next;
614
615                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
616                         strcpy(cookie, &buf[15]);
617                         cookie_to_stuff(cookie, NULL,
618                                       c_username, c_password, c_roomname);
619                 }
620                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
621                         ContentLength = atoi(&buf[16]);
622                 }
623                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
624                         strcpy(ContentType, &buf[14]);
625                 }
626                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
627                         strcpy(user_agent, &buf[12]);
628                 }
629         }
630
631         ++TransactionCount;
632
633         if (ContentLength > 0) {
634                 content = malloc(ContentLength + 1);
635                 read(WC->http_sock, content, ContentLength);
636
637                 content[ContentLength] = 0;
638
639                 if (!strncasecmp(ContentType,
640                               "application/x-www-form-urlencoded", 33)) {
641                         addurls(content);
642                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
643                         mime_parser(content, ContentLength, ContentType,
644                                     *upload_handler);
645                 }
646         } else {
647                 content = NULL;
648         }
649
650         /* If there are variables in the URL, we must grab them now */
651         for (a = 0; a < strlen(cmd); ++a)
652                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
653                         for (b = a; b < strlen(cmd); ++b)
654                                 if (isspace(cmd[b]))
655                                         cmd[b] = 0;
656                         addurls(&cmd[a + 1]);
657                         cmd[a] = 0;
658                 }
659         /*
660          * If we're not connected to a Citadel server, try to hook up the
661          * connection now.  Preference is given to the host and port specified
662          * by browser cookies, if cookies have been supplied.
663          */
664         if (!WC->connected) {
665                 if (strlen(bstr("host")) > 0)
666                         strcpy(c_host, bstr("host"));
667                 if (strlen(bstr("port")) > 0)
668                         strcpy(c_port, bstr("port"));
669                 WC->serv_sock = connectsock(c_host, c_port, "tcp");
670                 if (WC->serv_sock < 0) {
671                         do_logout();
672                 }
673
674                 WC->connected = 1;
675                 serv_gets(buf); /* get the server welcome message */
676                 locate_host(browser_host, WC->http_sock);
677                 get_serv_info(browser_host, user_agent);
678         }
679         check_for_express_messages();
680
681         /*
682          * If we're not logged in, but we have username and password cookies
683          * supplied by the browser, try using them to log in.
684          */
685         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
686                 serv_printf("USER %s", c_username);
687                 serv_gets(buf);
688                 if (buf[0] == '3') {
689                         serv_printf("PASS %s", c_password);
690                         serv_gets(buf);
691                         if (buf[0] == '2') {
692                                 become_logged_in(c_username, c_password, buf);
693                         }
694                 }
695         }
696         /*
697          * If we don't have a current room, but a cookie specifying the
698          * current room is supplied, make an effort to go there.
699          */
700         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
701                 serv_printf("GOTO %s", c_roomname);
702                 serv_gets(buf);
703                 if (buf[0] == '2') {
704                         strcpy(WC->wc_roomname, c_roomname);
705                 }
706         }
707         if (!strcasecmp(action, "static")) {
708                 strcpy(buf, &cmd[12]);
709                 for (a = 0; a < strlen(buf); ++a)
710                         if (isspace(buf[a]))
711                                 buf[a] = 0;
712                 output_static(buf);
713         } else if (!strcasecmp(action, "image")) {
714                 output_image();
715         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
716                 do_login();
717         } else if (!WC->logged_in) {
718                 display_login(NULL);
719         }
720         /* Various commands... */
721
722         else if (!strcasecmp(action, "do_welcome")) {
723                 do_welcome();
724         } else if (!strcasecmp(action, "display_main_menu")) {
725                 display_main_menu();
726         } else if (!strcasecmp(action, "advanced")) {
727                 display_advanced_menu();
728         } else if (!strcasecmp(action, "whobbs")) {
729                 whobbs();
730         } else if (!strcasecmp(action, "knrooms")) {
731                 list_all_rooms_by_floor();
732         } else if (!strcasecmp(action, "gotonext")) {
733                 slrp_highest();
734                 gotonext();
735         } else if (!strcasecmp(action, "skip")) {
736                 gotonext();
737         } else if (!strcasecmp(action, "ungoto")) {
738                 ungoto();
739         } else if (!strcasecmp(action, "dotgoto")) {
740                 slrp_highest();
741                 smart_goto(bstr("room"));
742         } else if (!strcasecmp(action, "termquit")) {
743                 do_logout();
744         } else if (!strcasecmp(action, "readnew")) {
745                 readloop("readnew");
746         } else if (!strcasecmp(action, "readold")) {
747                 readloop("readold");
748         } else if (!strcasecmp(action, "readfwd")) {
749                 readloop("readfwd");
750         } else if (!strcasecmp(action, "display_enter")) {
751                 display_enter();
752         } else if (!strcasecmp(action, "post")) {
753                 post_message();
754         } else if (!strcasecmp(action, "confirm_delete_msg")) {
755                 confirm_delete_msg();
756         } else if (!strcasecmp(action, "delete_msg")) {
757                 delete_msg();
758         } else if (!strcasecmp(action, "confirm_move_msg")) {
759                 confirm_move_msg();
760         } else if (!strcasecmp(action, "move_msg")) {
761                 move_msg();
762         } else if (!strcasecmp(action, "userlist")) {
763                 userlist();
764         } else if (!strcasecmp(action, "showuser")) {
765                 showuser();
766         } else if (!strcasecmp(action, "display_page")) {
767                 display_page();
768         } else if (!strcasecmp(action, "page_user")) {
769                 page_user();
770         } else if (!strcasecmp(action, "chat")) {
771                 do_chat();
772         } else if (!strcasecmp(action, "display_private")) {
773                 display_private("", 0);
774         } else if (!strcasecmp(action, "goto_private")) {
775                 goto_private();
776         } else if (!strcasecmp(action, "zapped_list")) {
777                 zapped_list();
778         } else if (!strcasecmp(action, "display_zap")) {
779                 display_zap();
780         } else if (!strcasecmp(action, "zap")) {
781                 zap();
782         } else if (!strcasecmp(action, "display_entroom")) {
783                 display_entroom();
784         } else if (!strcasecmp(action, "entroom")) {
785                 entroom();
786         } else if (!strcasecmp(action, "display_editroom")) {
787                 display_editroom();
788         } else if (!strcasecmp(action, "editroom")) {
789                 editroom();
790         } else if (!strcasecmp(action, "display_editinfo")) {
791                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
792         } else if (!strcasecmp(action, "editinfo")) {
793                 save_edit("Room info", "EINF 1", 1);
794         } else if (!strcasecmp(action, "display_editbio")) {
795                 sprintf(buf, "RBIO %s", WC->wc_username);
796                 display_edit("Your bio", "NOOP", buf, "editbio");
797         } else if (!strcasecmp(action, "editbio")) {
798                 save_edit("Your bio", "EBIO", 0);
799         } else if (!strcasecmp(action, "confirm_delete_room")) {
800                 confirm_delete_room();
801         } else if (!strcasecmp(action, "delete_room")) {
802                 delete_room();
803         } else if (!strcasecmp(action, "validate")) {
804                 validate();
805         } else if (!strcasecmp(action, "display_editpic")) {
806                 display_graphics_upload("your photo",
807                                         "UIMG 0|_userpic_",
808                                         "/editpic");
809         } else if (!strcasecmp(action, "editpic")) {
810                 do_graphics_upload("UIMG 1|_userpic_");
811         } else if (!strcasecmp(action, "display_editroompic")) {
812                 display_graphics_upload("the graphic for this room",
813                                         "UIMG 0|_roompic_",
814                                         "/editroompic");
815         } else if (!strcasecmp(action, "editroompic")) {
816                 do_graphics_upload("UIMG 1|_roompic_");
817         } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
818                 select_floor_to_edit_pic();
819         } else if (!strcasecmp(action, "display_editfloorpic")) {
820                 sprintf(buf, "UIMG 0|_floorpic_|%s",
821                         bstr("which_floor"));
822                 display_graphics_upload("the graphic for this floor",
823                                         buf,
824                                         "/editfloorpic");
825         } else if (!strcasecmp(action, "editfloorpic")) {
826                 sprintf(buf, "UIMG 1|_floorpic_|%s",
827                         bstr("which_floor"));
828                 do_graphics_upload(buf);
829         } else if (!strcasecmp(action, "display_reg")) {
830                 display_reg(0);
831         } else if (!strcasecmp(action, "register")) {
832                 register_user();
833         } else if (!strcasecmp(action, "display_changepw")) {
834                 display_changepw();
835         } else if (!strcasecmp(action, "changepw")) {
836                 changepw();
837         } else if (!strcasecmp(action, "display_edit_node")) {
838                 display_edit_node();
839         } else if (!strcasecmp(action, "display_netconf")) {
840                 display_netconf();
841         } else if (!strcasecmp(action, "display_confirm_unshare")) {
842                 display_confirm_unshare();
843         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
844                 display_confirm_delete_node();
845         } else if (!strcasecmp(action, "delete_node")) {
846                 delete_node();
847         } else if (!strcasecmp(action, "unshare")) {
848                 unshare();
849         } else if (!strcasecmp(action, "display_add_node")) {
850                 display_add_node();
851         } else if (!strcasecmp(action, "add_node")) {
852                 add_node();
853         } else if (!strcasecmp(action, "display_share")) {
854                 display_share();
855         } else if (!strcasecmp(action, "share")) {
856                 share();
857         } else if (!strcasecmp(action, "terminate_session")) {
858                 slrp_highest();
859                 terminate_session();
860         } else if (!strcasecmp(action, "edit_me")) {
861                 edit_me();
862         } else if (!strcasecmp(action, "display_siteconfig")) {
863                 display_siteconfig();
864         } else if (!strcasecmp(action, "siteconfig")) {
865                 siteconfig();
866         } else if (!strcasecmp(action, "display_generic")) {
867                 display_generic();
868         } else if (!strcasecmp(action, "do_generic")) {
869                 do_generic();
870         } else if (!strcasecmp(action, "display_menubar")) {
871                 display_menubar(1);
872         } else if (!strcasecmp(action, "diagnostics")) {
873                 wprintf("HTTP/1.0 200 OK\n");
874                 output_headers(1);
875
876                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
877                 wprintf("You're in session %d<HR>\n", WC->wc_session);
878                 wprintf("Command: <BR><PRE>\n");
879                 escputs(cmd);
880                 wprintf("</PRE><HR>\n");
881                 wprintf("Variables: <BR><PRE>\n");
882                 dump_vars();
883                 wprintf("</PRE><HR>\n");
884                 wDumpContent(1);
885         }
886         /* When all else fais, display the main menu. */
887         else {
888                 display_main_menu();
889         }
890
891         fflush(stdout);
892         if (content != NULL) {
893                 free(content);
894                 content = NULL;
895         }
896         free_urls();
897         if (WC->upload_length > 0) {
898                 free(WC->upload);
899                 WC->upload_length = 0;
900         }
901 }