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