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