]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* User interface overhaul now in progress. WARNING: incomplete system.
[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("</TABLE>\n");
177                 WC->fake_frames = 0;
178         }
179
180         if (print_standard_html_footer) {
181                 if (print_standard_html_footer != 2) {
182                         wprintf("<BR>");
183                 }
184                 wprintf("</BODY></HTML>\n");
185         }
186
187
188 }
189
190
191 /*
192  * Copy a string, escaping characters which have meaning in HTML.  If
193  * nbsp is nonzero, spaces are converted to non-breaking spaces.
194  */
195 void stresc(char *target, char *strbuf, int nbsp)
196 {
197         int a;
198         strcpy(target, "");
199
200         for (a = 0; a < strlen(strbuf); ++a) {
201                 if (strbuf[a] == '<')
202                         strcat(target, "&lt;");
203                 else if (strbuf[a] == '>')
204                         strcat(target, "&gt;");
205                 else if (strbuf[a] == '&')
206                         strcat(target, "&amp;");
207                 else if (strbuf[a] == '\"')
208                         strcat(target, "&quot;");
209                 else if (strbuf[a] == '\'') 
210                         strcat(target, "&#39;");
211                 else if (strbuf[a] == LB)
212                         strcat(target, "<");
213                 else if (strbuf[a] == RB)
214                         strcat(target, ">");
215                 else if (strbuf[a] == QU)
216                         strcat(target, "\"");
217                 else if ((strbuf[a] == 32) && (nbsp == 1)) {
218                         strcat(target, "&nbsp;");
219                 } else {
220                         strncat(target, &strbuf[a], 1);
221                 }
222         }
223 }
224
225 void escputs1(char *strbuf, int nbsp)
226 {
227         char buf[1024];
228         stresc(buf, strbuf, nbsp);
229         wprintf("%s", buf);
230 }
231
232 void escputs(char *strbuf)
233 {
234         escputs1(strbuf, 0);
235 }
236
237 /*
238  * Escape a string for feeding out as a URL.
239  * FIXME this is not threadsafe!
240  */
241 char *urlesc(char *strbuf)
242 {
243         int a, b, c;
244         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
245         static char outbuf[1024];
246
247         strcpy(outbuf, "");
248
249         for (a = 0; a < strlen(strbuf); ++a) {
250                 c = 0;
251                 for (b = 0; b < strlen(ec); ++b) {
252                         if (strbuf[a] == ec[b])
253                                 c = 1;
254                 }
255                 b = strlen(outbuf);
256                 if (c == 1)
257                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
258                 else
259                         sprintf(&outbuf[b], "%c", strbuf[a]);
260         }
261         return (outbuf);
262 }
263
264 void urlescputs(char *strbuf)
265 {
266         wprintf("%s", urlesc(strbuf));
267 }
268
269
270
271
272 /*
273  * Output all that important stuff that the browser will want to see
274  *
275  * control codes:
276  * 
277  * Bits 0 and 1:
278  * 0 = Nothing.  Do not display any leading HTTP or HTML.
279  * 1 = HTTP headers plus the "fake frames" found in most windows.
280  * 2 = HTTP headers required to terminate the session (unset cookies)
281  * 3 = HTTP and HTML headers, but no 'fake frames'
282  *
283  * Bit 2: Set to 1 to auto-refresh page every 30 seconds
284  *
285  * Bit 3: suppress check for express messages
286  */
287 void output_headers(int controlcode)
288 {
289         char cookie[256];
290         int print_standard_html_head = 0;
291         int refresh30 = 0;
292         int suppress_check = 0;
293         char httpnow[256];
294         static int pageseq = 0;
295
296         print_standard_html_head        =       controlcode & 0x03;
297         refresh30                       =       ((controlcode & 0x04) >> 2);
298         suppress_check                  =       ((controlcode & 0x08) >> 3);
299
300         wprintf("HTTP/1.0 200 OK\n");
301
302         httpdate(httpnow, time(NULL));
303
304         if (print_standard_html_head > 0) {
305                 wprintf("Content-type: text/html\n");
306                 wprintf("Server: %s\n", SERVER);
307                 wprintf("Connection: close\n");
308                 wprintf("Pragma: no-cache\n");
309                 wprintf("Cache-Control: no-store\n");
310         }
311         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
312                         WC->wc_password, WC->wc_roomname);
313         if (print_standard_html_head == 2) {
314                 wprintf("Set-cookie: webcit=%s\n", unset);
315         } else {
316                 wprintf("Set-cookie: webcit=%s\n", cookie);
317         }
318
319         if (print_standard_html_head > 0) {
320                 wprintf("\n");
321                 wprintf("<HTML><HEAD><TITLE>");
322                 escputs(serv_info.serv_humannode);
323                 wprintf("</TITLE>\n"
324                         "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">\n"
325                         "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
326                 if (refresh30) wprintf(
327                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
328                 wprintf("</HEAD>\n");
329
330                 /* script for checking for express msgs (not always launch) */
331                 wprintf("<SCRIPT LANGUAGE=\"JavaScript\">\n");
332                 wprintf("function launch_page_popup() {\n");
333                 wprintf("pwin = window.open('/page_popup', 'CitaPage%d', 'toolbar=no,location=no,copyhistory=no,status=no,scrollbars=yes,resizable=no,height=150,width=400');\n");
334                 wprintf("}\n");
335                 wprintf("</SCRIPT>\n", ++pageseq);
336                 /* end script */
337
338                 /* JavaScript keyboard-based navigation would go here if it
339                  * were finished
340                  */
341
342                 if (!suppress_check) if (WC->HaveExpressMessages) {
343                         svprintf("extrabodyparms", WCS_STRING, "%s", 
344                                 "onload=\"launch_page_popup()\" ");
345                         WC->HaveExpressMessages = 0;
346                 }
347                 do_template("background.html");
348                 clear_local_substs();
349
350         if (print_standard_html_head == 1) {
351                 wprintf("<A NAME=\"TheTop\"></A>");
352
353                 wprintf("<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                 WC->fake_frames = 1;
361                 }
362         }
363 }
364
365
366 /*
367  *
368  */
369 void http_redirect(char *whichpage) {
370         wprintf("HTTP/1.0 302 Moved Temporarily\n");
371         wprintf("Location: %s\n", whichpage);
372         wprintf("URI: %s\n", whichpage);
373         wprintf("Content-type: text/html\n\n");
374         wprintf("<html><body>\n");
375         wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
376                 whichpage);
377         wprintf("</body></html>\n");
378 }
379
380
381
382 void check_for_express_messages()
383 {
384         char buf[256];
385
386         serv_puts("NOOP");
387         serv_gets(buf);
388         if (buf[3] == '*') WC->HaveExpressMessages = 1;
389 }
390
391
392
393
394 void output_static(char *what)
395 {
396         char buf[4096];
397         long thisblock;
398         FILE *fp;
399         struct stat statbuf;
400         off_t bytes;
401
402         sprintf(buf, "static/%s", what);
403         fp = fopen(buf, "rb");
404         if (fp == NULL) {
405                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
406                 wprintf("Content-Type: text/plain\n");
407                 wprintf("\n");
408                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
409         } else {
410                 output_headers(0);
411
412                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
413                         wprintf("Content-type: image/gif\n");
414                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
415                         wprintf("Content-type: text/plain\n");
416                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
417                         wprintf("Content-type: image/jpeg\n");
418                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
419                         wprintf("Content-type: text/html\n");
420                 else
421                         wprintf("Content-type: application/octet-stream\n");
422
423                 fstat(fileno(fp), &statbuf);
424                 bytes = statbuf.st_size;
425                 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
426                 wprintf("Content-length: %ld\n", (long) bytes);
427                 wprintf("\n");
428                 while (bytes > 0) {
429                         thisblock = sizeof(buf);
430                         if (thisblock > bytes) thisblock = bytes;
431                         fread(buf, thisblock, 1, fp);
432                         write(WC->http_sock, buf, thisblock);
433                         bytes = bytes - thisblock;
434                 }
435                 fclose(fp);
436         }
437         if (!strcasecmp(bstr("force_close_session"), "yes")) {
438                 end_webcit_session();
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                 output_headers(0);
460                 wprintf("Content-type: image/gif\n");
461                 wprintf("Content-length: %ld\n", (long) bytes);
462                 wprintf("\n");
463
464                 while (bytes > (off_t) 0) {
465                         thisblock = (off_t) sizeof(xferbuf);
466                         if (thisblock > bytes)
467                                 thisblock = bytes;
468                         serv_printf("READ %ld|%ld", accomplished, thisblock);
469                         serv_gets(buf);
470                         if (buf[0] == '6')
471                                 thisblock = extract_long(&buf[4], 0);
472                         serv_read(xferbuf, (int) thisblock);
473                         write(WC->http_sock, xferbuf, thisblock);
474                         bytes = bytes - thisblock;
475                         accomplished = accomplished + thisblock;
476                 }
477                 serv_puts("CLOS");
478                 serv_gets(buf);
479         } else {
480                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
481                 output_headers(0);
482                 wprintf("Content-Type: text/plain\n");
483                 wprintf("\n");
484                 wprintf("Error retrieving image\n");
485         }
486
487 }
488
489
490 /*
491  * Convenience functions to display a page containing only a string
492  */
493 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
494 {
495         wprintf("HTTP/1.0 200 OK\n");
496         output_headers(1);
497         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
498         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
499         wprintf("<B>%s</B>\n", titlebarmsg);
500         wprintf("</FONT></TD></TR></TABLE><BR>\n");
501         escputs(messagetext);
502
503         wprintf("<HR>\n");
504         embed_main_menu();
505         wDumpContent(1);
506 }
507
508 void display_error(char *errormessage)
509 {
510         convenience_page("770000", "Error", errormessage);
511 }
512
513 void display_success(char *successmessage)
514 {
515         convenience_page("007700", "OK", successmessage);
516 }
517
518
519
520 void extract_action(char *actbuf, char *cmdbuf)
521 {
522         int i;
523
524         strcpy(actbuf, cmdbuf);
525         if (!strncasecmp(actbuf, "GET /", 5))
526                 strcpy(actbuf, &actbuf[5]);
527         if (!strncasecmp(actbuf, "PUT /", 5))
528                 strcpy(actbuf, &actbuf[5]);
529         if (!strncasecmp(actbuf, "POST /", 6))
530                 strcpy(actbuf, &actbuf[6]);
531
532         for (i = 0; i < strlen(actbuf); ++i) {
533                 if (actbuf[i] == ' ') {
534                         actbuf[i] = 0;
535                         i = 0;
536                 }
537                 if (actbuf[i] == '/') {
538                         actbuf[i] = 0;
539                         i = 0;
540                 }
541                 if (actbuf[i] == '?') {
542                         actbuf[i] = 0;
543                         i = 0;
544                 }
545                 if (actbuf[i] == '&') {
546                         actbuf[i] = 0;
547                         i = 0;
548                 }
549         }
550 }
551
552
553 void upload_handler(char *name, char *filename, char *encoding,
554                     void *content, char *cbtype, size_t length)
555 {
556
557         fprintf(stderr, "UPLOAD HANDLER CALLED\n");
558         fprintf(stderr, "    name = %s\n", name);
559         fprintf(stderr, "filename = %s\n", filename);
560         fprintf(stderr, "encoding = %s\n", encoding);
561         fprintf(stderr, "    type = %s\n", cbtype);
562         fprintf(stderr, "  length = %ld\n", (long)length);
563
564         if (strlen(name) > 0) {
565                 WC->upload = malloc(length);
566                 if (WC->upload != NULL) {
567                         WC->upload_length = length;
568                         memcpy(WC->upload, content, length);
569                 }
570         }
571 }
572
573
574 /*
575  * Entry point for WebCit transaction
576  */
577 void session_loop(struct httprequest *req)
578 {
579         char cmd[256];
580         char action[256];
581         char buf[256];
582         int a, b;
583         int ContentLength = 0;
584         int BytesRead;
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                 fprintf(stderr, "Content length: %d\n", ContentLength);
639                 content = malloc(ContentLength + 1);
640                 memset(content, 0, ContentLength+1);
641                 BytesRead = 0;
642
643                 while (BytesRead < ContentLength) {
644                         a=read(WC->http_sock, &content[BytesRead],
645                                 ContentLength - BytesRead);
646                         if (a <= 0) BytesRead = ContentLength;
647                         else BytesRead += a;
648                 }
649
650                 if (!strncasecmp(ContentType,
651                               "application/x-www-form-urlencoded", 33)) {
652                         addurls(content);
653                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
654                         mime_parser(content, ContentLength, ContentType,
655                                     *upload_handler);
656                 }
657         } else {
658                 content = NULL;
659         }
660
661         /* If there are variables in the URL, we must grab them now */
662         for (a = 0; a < strlen(cmd); ++a)
663                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
664                         for (b = a; b < strlen(cmd); ++b)
665                                 if (isspace(cmd[b]))
666                                         cmd[b] = 0;
667                         addurls(&cmd[a + 1]);
668                         cmd[a] = 0;
669                 }
670         /*
671          * If we're not connected to a Citadel server, try to hook up the
672          * connection now.  Preference is given to the host and port specified
673          * by browser cookies, if cookies have been supplied.
674          */
675         if (!WC->connected) {
676                 if (strlen(bstr("host")) > 0)
677                         strcpy(c_host, bstr("host"));
678                 if (strlen(bstr("port")) > 0)
679                         strcpy(c_port, bstr("port"));
680
681                 if (!strcasecmp(c_host, "uds")) {
682                         /* unix domain socket */
683                         sprintf(buf, "%s/citadel.socket", c_port);
684                         WC->serv_sock = uds_connectsock(buf);
685                 }
686                 else {
687                         /* tcp socket */
688                         WC->serv_sock = tcp_connectsock(c_host, c_port);
689                 }
690
691                 if (WC->serv_sock < 0) {
692                         do_logout();
693                 }
694
695                 WC->connected = 1;
696                 serv_gets(buf); /* get the server welcome message */
697                 locate_host(browser_host, WC->http_sock);
698                 get_serv_info(browser_host, user_agent);
699         }
700
701         check_for_express_messages();
702
703         /*
704          * If we're not logged in, but we have username and password cookies
705          * supplied by the browser, try using them to log in.
706          */
707         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
708                 serv_printf("USER %s", c_username);
709                 serv_gets(buf);
710                 if (buf[0] == '3') {
711                         serv_printf("PASS %s", c_password);
712                         serv_gets(buf);
713                         if (buf[0] == '2') {
714                                 become_logged_in(c_username, c_password, buf);
715                         }
716                 }
717         }
718         /*
719          * If we don't have a current room, but a cookie specifying the
720          * current room is supplied, make an effort to go there.
721          */
722         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
723                 serv_printf("GOTO %s", c_roomname);
724                 serv_gets(buf);
725                 if (buf[0] == '2') {
726                         strcpy(WC->wc_roomname, c_roomname);
727                 }
728         }
729         if (!strcasecmp(action, "static")) {
730                 strcpy(buf, &cmd[12]);
731                 for (a = 0; a < strlen(buf); ++a)
732                         if (isspace(buf[a]))
733                                 buf[a] = 0;
734                 output_static(buf);
735         } else if (!strcasecmp(action, "image")) {
736                 output_image();
737         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
738                 do_login();
739         } else if (!WC->logged_in) {
740                 display_login(NULL);
741         }
742         /* Various commands... */
743
744         else if (!strcasecmp(action, "do_welcome")) {
745                 do_welcome();
746         } else if (!strcasecmp(action, "display_main_menu")) {
747                 display_main_menu();
748         } else if (!strcasecmp(action, "advanced")) {
749                 display_advanced_menu();
750         } else if (!strcasecmp(action, "whobbs")) {
751                 whobbs();
752         } else if (!strcasecmp(action, "knrooms")) {
753                 list_all_rooms_by_floor();
754         } else if (!strcasecmp(action, "gotonext")) {
755                 slrp_highest();
756                 gotonext();
757         } else if (!strcasecmp(action, "skip")) {
758                 gotonext();
759         } else if (!strcasecmp(action, "ungoto")) {
760                 ungoto();
761         } else if (!strcasecmp(action, "dotgoto")) {
762                 slrp_highest();
763                 smart_goto(bstr("room"));
764         } else if (!strcasecmp(action, "termquit")) {
765                 do_logout();
766         } else if (!strcasecmp(action, "readnew")) {
767                 readloop("readnew");
768         } else if (!strcasecmp(action, "readold")) {
769                 readloop("readold");
770         } else if (!strcasecmp(action, "readfwd")) {
771                 readloop("readfwd");
772         } else if (!strcasecmp(action, "headers")) {
773                 readloop("headers");
774         } else if (!strcasecmp(action, "display_enter")) {
775                 display_enter();
776         } else if (!strcasecmp(action, "post")) {
777                 post_message();
778         } else if (!strcasecmp(action, "delete_msg")) {
779                 delete_msg();
780         } else if (!strcasecmp(action, "confirm_move_msg")) {
781                 confirm_move_msg();
782         } else if (!strcasecmp(action, "move_msg")) {
783                 move_msg();
784         } else if (!strcasecmp(action, "userlist")) {
785                 userlist();
786         } else if (!strcasecmp(action, "showuser")) {
787                 showuser();
788         } else if (!strcasecmp(action, "display_page")) {
789                 display_page();
790         } else if (!strcasecmp(action, "page_user")) {
791                 page_user();
792         } else if (!strcasecmp(action, "chat")) {
793                 do_chat();
794         } else if (!strcasecmp(action, "display_private")) {
795                 display_private("", 0);
796         } else if (!strcasecmp(action, "goto_private")) {
797                 goto_private();
798         } else if (!strcasecmp(action, "zapped_list")) {
799                 zapped_list();
800         } else if (!strcasecmp(action, "display_zap")) {
801                 display_zap();
802         } else if (!strcasecmp(action, "zap")) {
803                 zap();
804         } else if (!strcasecmp(action, "display_entroom")) {
805                 display_entroom();
806         } else if (!strcasecmp(action, "entroom")) {
807                 entroom();
808         } else if (!strcasecmp(action, "display_editroom")) {
809                 display_editroom();
810         } else if (!strcasecmp(action, "editroom")) {
811                 editroom();
812         } else if (!strcasecmp(action, "display_whok")) {
813                 display_whok();
814         } else if (!strcasecmp(action, "display_editinfo")) {
815                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
816         } else if (!strcasecmp(action, "editinfo")) {
817                 save_edit("Room info", "EINF 1", 1);
818         } else if (!strcasecmp(action, "display_editbio")) {
819                 sprintf(buf, "RBIO %s", WC->wc_username);
820                 display_edit("Your bio", "NOOP", buf, "editbio");
821         } else if (!strcasecmp(action, "editbio")) {
822                 save_edit("Your bio", "EBIO", 0);
823         } else if (!strcasecmp(action, "confirm_delete_room")) {
824                 confirm_delete_room();
825         } else if (!strcasecmp(action, "delete_room")) {
826                 delete_room();
827         } else if (!strcasecmp(action, "validate")) {
828                 validate();
829         } else if (!strcasecmp(action, "display_editpic")) {
830                 display_graphics_upload("your photo",
831                                         "UIMG 0|_userpic_",
832                                         "/editpic");
833         } else if (!strcasecmp(action, "editpic")) {
834                 do_graphics_upload("UIMG 1|_userpic_");
835         } else if (!strcasecmp(action, "display_editroompic")) {
836                 display_graphics_upload("the graphic for this room",
837                                         "UIMG 0|_roompic_",
838                                         "/editroompic");
839         } else if (!strcasecmp(action, "editroompic")) {
840                 do_graphics_upload("UIMG 1|_roompic_");
841         } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
842                 select_floor_to_edit_pic();
843         } else if (!strcasecmp(action, "display_editfloorpic")) {
844                 sprintf(buf, "UIMG 0|_floorpic_|%s",
845                         bstr("which_floor"));
846                 display_graphics_upload("the graphic for this floor",
847                                         buf,
848                                         "/editfloorpic");
849         } else if (!strcasecmp(action, "editfloorpic")) {
850                 sprintf(buf, "UIMG 1|_floorpic_|%s",
851                         bstr("which_floor"));
852                 do_graphics_upload(buf);
853         } else if (!strcasecmp(action, "display_reg")) {
854                 display_reg(0);
855         } else if (!strcasecmp(action, "register")) {
856                 register_user();
857         } else if (!strcasecmp(action, "display_changepw")) {
858                 display_changepw();
859         } else if (!strcasecmp(action, "changepw")) {
860                 changepw();
861         } else if (!strcasecmp(action, "display_edit_node")) {
862                 display_edit_node();
863         } else if (!strcasecmp(action, "display_netconf")) {
864                 display_netconf();
865         } else if (!strcasecmp(action, "display_confirm_unshare")) {
866                 display_confirm_unshare();
867         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
868                 display_confirm_delete_node();
869         } else if (!strcasecmp(action, "delete_node")) {
870                 delete_node();
871         } else if (!strcasecmp(action, "unshare")) {
872                 unshare();
873         } else if (!strcasecmp(action, "display_add_node")) {
874                 display_add_node();
875         } else if (!strcasecmp(action, "add_node")) {
876                 add_node();
877         } else if (!strcasecmp(action, "display_share")) {
878                 display_share();
879         } else if (!strcasecmp(action, "share")) {
880                 share();
881         } else if (!strcasecmp(action, "terminate_session")) {
882                 slrp_highest();
883                 terminate_session();
884         } else if (!strcasecmp(action, "edit_me")) {
885                 edit_me();
886         } else if (!strcasecmp(action, "display_siteconfig")) {
887                 display_siteconfig();
888         } else if (!strcasecmp(action, "page_popup")) {
889                 page_popup();
890         } else if (!strcasecmp(action, "siteconfig")) {
891                 siteconfig();
892         } else if (!strcasecmp(action, "display_generic")) {
893                 display_generic();
894         } else if (!strcasecmp(action, "do_generic")) {
895                 do_generic();
896         } else if (!strcasecmp(action, "display_menubar")) {
897                 display_menubar(1);
898         } else if (!strcasecmp(action, "diagnostics")) {
899                 output_headers(1);
900
901                 wprintf("You're in session %d<HR>\n", WC->wc_session);
902                 wprintf("Command: <BR><PRE>\n");
903                 escputs(cmd);
904                 wprintf("</PRE><HR>\n");
905                 wprintf("Variables: <BR><PRE>\n");
906                 dump_vars();
907                 wprintf("</PRE><HR>\n");
908                 wDumpContent(1);
909         }
910         /* When all else fais, display the main menu. */
911         else {
912                 display_main_menu();
913         }
914
915         fflush(stdout);
916         if (content != NULL) {
917                 free(content);
918                 content = NULL;
919         }
920         free_urls();
921         if (WC->upload_length > 0) {
922                 free(WC->upload);
923                 WC->upload_length = 0;
924         }
925 }