]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* more of the UI overhaul
[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 room banner
280  * 2 = HTTP headers required to terminate the session (unset cookies)
281  * 3 = HTTP and HTML headers, but no room banner
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         wDumpContent(1);
505 }
506
507
508 /*
509  * Display a blank page.
510  */
511 void blank_page(void) {
512         output_headers(7);
513         wDumpContent(2);
514 }
515
516
517 void display_error(char *errormessage)
518 {
519         convenience_page("770000", "Error", errormessage);
520 }
521
522 void display_success(char *successmessage)
523 {
524         convenience_page("007700", "OK", successmessage);
525 }
526
527
528
529 void extract_action(char *actbuf, char *cmdbuf)
530 {
531         int i;
532
533         strcpy(actbuf, cmdbuf);
534         if (!strncasecmp(actbuf, "GET /", 5))
535                 strcpy(actbuf, &actbuf[5]);
536         if (!strncasecmp(actbuf, "PUT /", 5))
537                 strcpy(actbuf, &actbuf[5]);
538         if (!strncasecmp(actbuf, "POST /", 6))
539                 strcpy(actbuf, &actbuf[6]);
540
541         for (i = 0; i < strlen(actbuf); ++i) {
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                 if (actbuf[i] == '?') {
551                         actbuf[i] = 0;
552                         i = 0;
553                 }
554                 if (actbuf[i] == '&') {
555                         actbuf[i] = 0;
556                         i = 0;
557                 }
558         }
559 }
560
561
562 void upload_handler(char *name, char *filename, char *encoding,
563                     void *content, char *cbtype, size_t length)
564 {
565
566         fprintf(stderr, "UPLOAD HANDLER CALLED\n");
567         fprintf(stderr, "    name = %s\n", name);
568         fprintf(stderr, "filename = %s\n", filename);
569         fprintf(stderr, "encoding = %s\n", encoding);
570         fprintf(stderr, "    type = %s\n", cbtype);
571         fprintf(stderr, "  length = %ld\n", (long)length);
572
573         if (strlen(name) > 0) {
574                 WC->upload = malloc(length);
575                 if (WC->upload != NULL) {
576                         WC->upload_length = length;
577                         memcpy(WC->upload, content, length);
578                 }
579         }
580 }
581
582
583 /*
584  * Entry point for WebCit transaction
585  */
586 void session_loop(struct httprequest *req)
587 {
588         char cmd[256];
589         char action[256];
590         char buf[256];
591         int a, b;
592         int ContentLength = 0;
593         int BytesRead;
594         char ContentType[512];
595         char *content;
596         struct httprequest *hptr;
597         char browser_host[256];
598         char user_agent[256];
599
600         /* We stuff these with the values coming from the client cookies,
601          * so we can use them to reconnect a timed out session if we have to.
602          */
603         char c_host[256];
604         char c_port[256];
605         char c_username[256];
606         char c_password[256];
607         char c_roomname[256];
608         char cookie[256];
609
610         strcpy(c_host, defaulthost);
611         strcpy(c_port, defaultport);
612         strcpy(c_username, "");
613         strcpy(c_password, "");
614         strcpy(c_roomname, "");
615
616         WC->upload_length = 0;
617         WC->upload = NULL;
618
619         hptr = req;
620         if (hptr == NULL) return;
621
622         strcpy(cmd, hptr->line);
623         hptr = hptr->next;
624         extract_action(action, cmd);
625
626         while (hptr != NULL) {
627                 strcpy(buf, hptr->line);
628                 hptr = hptr->next;
629
630                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
631                         strcpy(cookie, &buf[15]);
632                         cookie_to_stuff(cookie, NULL,
633                                       c_username, c_password, c_roomname);
634                 }
635                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
636                         ContentLength = atoi(&buf[16]);
637                 }
638                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
639                         strcpy(ContentType, &buf[14]);
640                 }
641                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
642                         strcpy(user_agent, &buf[12]);
643                 }
644         }
645
646         if (ContentLength > 0) {
647                 fprintf(stderr, "Content length: %d\n", ContentLength);
648                 content = malloc(ContentLength + 1);
649                 memset(content, 0, ContentLength+1);
650                 BytesRead = 0;
651
652                 while (BytesRead < ContentLength) {
653                         a=read(WC->http_sock, &content[BytesRead],
654                                 ContentLength - BytesRead);
655                         if (a <= 0) BytesRead = ContentLength;
656                         else BytesRead += a;
657                 }
658
659                 if (!strncasecmp(ContentType,
660                               "application/x-www-form-urlencoded", 33)) {
661                         addurls(content);
662                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
663                         mime_parser(content, ContentLength, ContentType,
664                                     *upload_handler);
665                 }
666         } else {
667                 content = NULL;
668         }
669
670         /* If there are variables in the URL, we must grab them now */
671         for (a = 0; a < strlen(cmd); ++a)
672                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
673                         for (b = a; b < strlen(cmd); ++b)
674                                 if (isspace(cmd[b]))
675                                         cmd[b] = 0;
676                         addurls(&cmd[a + 1]);
677                         cmd[a] = 0;
678                 }
679         /*
680          * If we're not connected to a Citadel server, try to hook up the
681          * connection now.  Preference is given to the host and port specified
682          * by browser cookies, if cookies have been supplied.
683          */
684         if (!WC->connected) {
685                 if (strlen(bstr("host")) > 0)
686                         strcpy(c_host, bstr("host"));
687                 if (strlen(bstr("port")) > 0)
688                         strcpy(c_port, bstr("port"));
689
690                 if (!strcasecmp(c_host, "uds")) {
691                         /* unix domain socket */
692                         sprintf(buf, "%s/citadel.socket", c_port);
693                         WC->serv_sock = uds_connectsock(buf);
694                 }
695                 else {
696                         /* tcp socket */
697                         WC->serv_sock = tcp_connectsock(c_host, c_port);
698                 }
699
700                 if (WC->serv_sock < 0) {
701                         do_logout();
702                 }
703
704                 WC->connected = 1;
705                 serv_gets(buf); /* get the server welcome message */
706                 locate_host(browser_host, WC->http_sock);
707                 get_serv_info(browser_host, user_agent);
708         }
709
710         check_for_express_messages();
711
712         /*
713          * If we're not logged in, but we have username and password cookies
714          * supplied by the browser, try using them to log in.
715          */
716         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
717                 serv_printf("USER %s", c_username);
718                 serv_gets(buf);
719                 if (buf[0] == '3') {
720                         serv_printf("PASS %s", c_password);
721                         serv_gets(buf);
722                         if (buf[0] == '2') {
723                                 become_logged_in(c_username, c_password, buf);
724                         }
725                 }
726         }
727         /*
728          * If we don't have a current room, but a cookie specifying the
729          * current room is supplied, make an effort to go there.
730          */
731         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
732                 serv_printf("GOTO %s", c_roomname);
733                 serv_gets(buf);
734                 if (buf[0] == '2') {
735                         strcpy(WC->wc_roomname, c_roomname);
736                 }
737         }
738         if (!strcasecmp(action, "static")) {
739                 strcpy(buf, &cmd[12]);
740                 for (a = 0; a < strlen(buf); ++a)
741                         if (isspace(buf[a]))
742                                 buf[a] = 0;
743                 output_static(buf);
744         } else if (!strcasecmp(action, "image")) {
745                 output_image();
746         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
747                 do_login();
748         } else if (!WC->logged_in) {
749                 display_login(NULL);
750         }
751         /* Various commands... */
752
753         else if (!strcasecmp(action, "do_welcome")) {
754                 do_welcome();
755         } else if (!strcasecmp(action, "blank")) {
756                 blank_page();
757         } else if (!strcasecmp(action, "display_main_menu")) {
758                 display_main_menu();
759         } else if (!strcasecmp(action, "whobbs")) {
760                 whobbs();
761         } else if (!strcasecmp(action, "knrooms")) {
762                 list_all_rooms_by_floor();
763         } else if (!strcasecmp(action, "gotonext")) {
764                 slrp_highest();
765                 gotonext();
766         } else if (!strcasecmp(action, "skip")) {
767                 gotonext();
768         } else if (!strcasecmp(action, "ungoto")) {
769                 ungoto();
770         } else if (!strcasecmp(action, "dotgoto")) {
771                 slrp_highest();
772                 smart_goto(bstr("room"));
773         } else if (!strcasecmp(action, "termquit")) {
774                 do_logout();
775         } else if (!strcasecmp(action, "readnew")) {
776                 readloop("readnew");
777         } else if (!strcasecmp(action, "readold")) {
778                 readloop("readold");
779         } else if (!strcasecmp(action, "readfwd")) {
780                 readloop("readfwd");
781         } else if (!strcasecmp(action, "headers")) {
782                 readloop("headers");
783         } else if (!strcasecmp(action, "display_enter")) {
784                 display_enter();
785         } else if (!strcasecmp(action, "post")) {
786                 post_message();
787         } else if (!strcasecmp(action, "delete_msg")) {
788                 delete_msg();
789         } else if (!strcasecmp(action, "confirm_move_msg")) {
790                 confirm_move_msg();
791         } else if (!strcasecmp(action, "move_msg")) {
792                 move_msg();
793         } else if (!strcasecmp(action, "userlist")) {
794                 userlist();
795         } else if (!strcasecmp(action, "showuser")) {
796                 showuser();
797         } else if (!strcasecmp(action, "display_page")) {
798                 display_page();
799         } else if (!strcasecmp(action, "page_user")) {
800                 page_user();
801         } else if (!strcasecmp(action, "chat")) {
802                 do_chat();
803         } else if (!strcasecmp(action, "display_private")) {
804                 display_private("", 0);
805         } else if (!strcasecmp(action, "goto_private")) {
806                 goto_private();
807         } else if (!strcasecmp(action, "zapped_list")) {
808                 zapped_list();
809         } else if (!strcasecmp(action, "display_zap")) {
810                 display_zap();
811         } else if (!strcasecmp(action, "zap")) {
812                 zap();
813         } else if (!strcasecmp(action, "display_entroom")) {
814                 display_entroom();
815         } else if (!strcasecmp(action, "entroom")) {
816                 entroom();
817         } else if (!strcasecmp(action, "display_editroom")) {
818                 display_editroom();
819         } else if (!strcasecmp(action, "editroom")) {
820                 editroom();
821         } else if (!strcasecmp(action, "display_whok")) {
822                 display_whok();
823         } else if (!strcasecmp(action, "display_editinfo")) {
824                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
825         } else if (!strcasecmp(action, "editinfo")) {
826                 save_edit("Room info", "EINF 1", 1);
827         } else if (!strcasecmp(action, "display_editbio")) {
828                 sprintf(buf, "RBIO %s", WC->wc_username);
829                 display_edit("Your bio", "NOOP", buf, "editbio");
830         } else if (!strcasecmp(action, "editbio")) {
831                 save_edit("Your bio", "EBIO", 0);
832         } else if (!strcasecmp(action, "confirm_delete_room")) {
833                 confirm_delete_room();
834         } else if (!strcasecmp(action, "delete_room")) {
835                 delete_room();
836         } else if (!strcasecmp(action, "validate")) {
837                 validate();
838         } else if (!strcasecmp(action, "display_editpic")) {
839                 display_graphics_upload("your photo",
840                                         "UIMG 0|_userpic_",
841                                         "/editpic");
842         } else if (!strcasecmp(action, "editpic")) {
843                 do_graphics_upload("UIMG 1|_userpic_");
844         } else if (!strcasecmp(action, "display_editroompic")) {
845                 display_graphics_upload("the graphic for this room",
846                                         "UIMG 0|_roompic_",
847                                         "/editroompic");
848         } else if (!strcasecmp(action, "editroompic")) {
849                 do_graphics_upload("UIMG 1|_roompic_");
850         } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
851                 select_floor_to_edit_pic();
852         } else if (!strcasecmp(action, "display_editfloorpic")) {
853                 sprintf(buf, "UIMG 0|_floorpic_|%s",
854                         bstr("which_floor"));
855                 display_graphics_upload("the graphic for this floor",
856                                         buf,
857                                         "/editfloorpic");
858         } else if (!strcasecmp(action, "editfloorpic")) {
859                 sprintf(buf, "UIMG 1|_floorpic_|%s",
860                         bstr("which_floor"));
861                 do_graphics_upload(buf);
862         } else if (!strcasecmp(action, "display_reg")) {
863                 display_reg(0);
864         } else if (!strcasecmp(action, "register")) {
865                 register_user();
866         } else if (!strcasecmp(action, "display_changepw")) {
867                 display_changepw();
868         } else if (!strcasecmp(action, "changepw")) {
869                 changepw();
870         } else if (!strcasecmp(action, "display_edit_node")) {
871                 display_edit_node();
872         } else if (!strcasecmp(action, "display_netconf")) {
873                 display_netconf();
874         } else if (!strcasecmp(action, "display_confirm_unshare")) {
875                 display_confirm_unshare();
876         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
877                 display_confirm_delete_node();
878         } else if (!strcasecmp(action, "delete_node")) {
879                 delete_node();
880         } else if (!strcasecmp(action, "unshare")) {
881                 unshare();
882         } else if (!strcasecmp(action, "display_add_node")) {
883                 display_add_node();
884         } else if (!strcasecmp(action, "add_node")) {
885                 add_node();
886         } else if (!strcasecmp(action, "display_share")) {
887                 display_share();
888         } else if (!strcasecmp(action, "share")) {
889                 share();
890         } else if (!strcasecmp(action, "terminate_session")) {
891                 slrp_highest();
892                 terminate_session();
893         } else if (!strcasecmp(action, "edit_me")) {
894                 edit_me();
895         } else if (!strcasecmp(action, "display_siteconfig")) {
896                 display_siteconfig();
897         } else if (!strcasecmp(action, "page_popup")) {
898                 page_popup();
899         } else if (!strcasecmp(action, "siteconfig")) {
900                 siteconfig();
901         } else if (!strcasecmp(action, "display_generic")) {
902                 display_generic();
903         } else if (!strcasecmp(action, "do_generic")) {
904                 do_generic();
905         } else if (!strcasecmp(action, "display_menubar")) {
906                 display_menubar(1);
907         } else if (!strcasecmp(action, "diagnostics")) {
908                 output_headers(1);
909
910                 wprintf("You're in session %d<HR>\n", WC->wc_session);
911                 wprintf("Command: <BR><PRE>\n");
912                 escputs(cmd);
913                 wprintf("</PRE><HR>\n");
914                 wprintf("Variables: <BR><PRE>\n");
915                 dump_vars();
916                 wprintf("</PRE><HR>\n");
917                 wDumpContent(1);
918         }
919         /* When all else fais, display the main menu. */
920         else {
921                 display_main_menu();
922         }
923
924         fflush(stdout);
925         if (content != NULL) {
926                 free(content);
927                 content = NULL;
928         }
929         free_urls();
930         if (WC->upload_length > 0) {
931                 free(WC->upload);
932                 WC->upload_length = 0;
933         }
934 }