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