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