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