]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
72da8375e86356ad457c45e97eccac94f0b449c4
[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 void output_headers(int controlcode)
288 {
289         char cookie[256];
290         int print_standard_html_head = 0;
291         int refresh30 = 0;
292         char httpnow[256];
293
294         print_standard_html_head        =       controlcode & 0x03;
295         refresh30                       =       ((controlcode & 0x04) >> 2);
296
297         wprintf("HTTP/1.0 200 OK\n");
298
299         httpdate(httpnow, time(NULL));
300
301         if (print_standard_html_head > 0) {
302                 wprintf("Content-type: text/html\n");
303                 wprintf("Server: %s\n", SERVER);
304                 wprintf("Connection: close\n");
305                 wprintf("Pragma: no-cache\n");
306                 wprintf("Cache-Control: no-store\n");
307                 wprintf("Date: %s\n"
308                         "Last-modified: %s\n", httpnow, httpnow);
309         }
310         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
311                         WC->wc_password, WC->wc_roomname);
312         if (print_standard_html_head == 2) {
313                 wprintf("Set-cookie: webcit=%s\n", unset);
314         } else {
315                 wprintf("Set-cookie: webcit=%s\n", cookie);
316         }
317
318         if (print_standard_html_head > 0) {
319                 wprintf("\n");
320                 wprintf("<HTML><HEAD><TITLE>");
321                 escputs(serv_info.serv_humannode);
322                 wprintf("</TITLE>\n"
323                         "<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">\n"
324                         "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
325                 if (refresh30) wprintf(
326                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
327                 wprintf("</HEAD>\n");
328                 if (WC->ExpressMessages != NULL) {
329                         wprintf("<SCRIPT language=\"javascript\">\n");
330                         wprintf("function ExpressMessage() {\n");
331                         wprintf(" alert(\"");
332                         escputs(WC->ExpressMessages);
333                         wprintf("\")\n");
334                         wprintf(" }\n </SCRIPT>\n");
335                 }
336
337
338
339                 /* JavaScript key-based navigation would go here if it
340                  * were finished
341                  */
342
343                 wprintf("<BODY ");
344                 if (WC->ExpressMessages != NULL) {
345                         wprintf("onload=\"ExpressMessage()\" ");
346                         free(WC->ExpressMessages);
347                         WC->ExpressMessages = NULL;
348                 }
349                 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
350         
351         
352         if (print_standard_html_head == 1) {
353                 wprintf("<A NAME=\"TheTop\"></A>"
354                         "<TABLE border=0 width=100%>"
355                         "<TR VALIGN=TOP><TD VALIGN=LEFT CELLPADDING=0>");
356
357                 display_menubar(0);
358
359                 wprintf("</TD><TD VALIGN=TOP>"
360                         "<TABLE border=0 width=100%><TR VALIGN=TOP>"
361                         "<TD>\n");
362
363                 embed_room_banner(NULL);
364
365                 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
366                 
367                 WC->fake_frames = 1;
368                 }
369         }
370 }
371
372
373
374 void ExpressMessageCat(char *buf) {
375         if (WC->ExpressMessages == NULL) {
376                 WC->ExpressMessages = malloc(strlen(buf) + 4);
377                 strcpy(WC->ExpressMessages, "");
378         } else {
379                 WC->ExpressMessages = realloc(WC->ExpressMessages,
380                         (strlen(WC->ExpressMessages) + strlen(buf) + 4));
381         }
382         strcat(WC->ExpressMessages, buf);
383         strcat(WC->ExpressMessages, "\\n");
384 }
385
386
387 void check_for_express_messages()
388 {
389         char buf[256];
390
391         serv_puts("PEXP");
392         serv_gets(buf);
393         if (buf[0] == '1') {
394                 while (serv_gets(buf), strcmp(buf, "000")) {
395                         ExpressMessageCat(buf);
396                 }
397         }
398 }
399
400
401
402
403 void output_static(char *what)
404 {
405         char buf[4096];
406         char datebuf[256];
407         long thisblock;
408         FILE *fp;
409         struct stat statbuf;
410         off_t bytes;
411
412         sprintf(buf, "static/%s", what);
413         fp = fopen(buf, "rb");
414         if (fp == NULL) {
415                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
416                 wprintf("Content-Type: text/plain\n");
417                 wprintf("\n");
418                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
419         } else {
420                 output_headers(0);
421
422                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
423                         wprintf("Content-type: image/gif\n");
424                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
425                         wprintf("Content-type: text/plain\n");
426                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
427                         wprintf("Content-type: image/jpeg\n");
428                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
429                         wprintf("Content-type: text/html\n");
430                 else
431                         wprintf("Content-type: application/octet-stream\n");
432
433                 fstat(fileno(fp), &statbuf);
434                 bytes = statbuf.st_size;
435                 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
436                 wprintf("Content-length: %ld\n", (long) bytes);
437
438                 httpdate(datebuf, time(NULL));
439                 wprintf("Date: %s\n", datebuf);
440
441                 httpdate(datebuf, statbuf.st_mtime);
442                 wprintf("Last-modified: %s\n", datebuf);
443
444                 wprintf("\n");
445                 while (bytes > 0) {
446                         thisblock = sizeof(buf);
447                         if (thisblock > bytes) thisblock = bytes;
448                         fread(buf, thisblock, 1, fp);
449                         write(WC->http_sock, buf, thisblock);
450                         bytes = bytes - thisblock;
451                 }
452                 fclose(fp);
453         }
454         if (!strcasecmp(bstr("force_close_session"), "yes")) {
455                 end_webcit_session();
456         }
457 }
458
459 /*
460  * When the browser requests an image file from the Citadel server,
461  * this function is called to transmit it.
462  */
463 void output_image()
464 {
465         char buf[256];
466         char xferbuf[4096];
467         off_t bytes;
468         off_t thisblock;
469         off_t accomplished = 0L;
470
471
472         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
473         serv_gets(buf);
474         if (buf[0] == '2') {
475                 bytes = extract_long(&buf[4], 0);
476                 output_headers(0);
477                 wprintf("Content-type: image/gif\n");
478                 wprintf("Content-length: %ld\n", (long) bytes);
479                 wprintf("\n");
480
481                 while (bytes > (off_t) 0) {
482                         thisblock = (off_t) sizeof(xferbuf);
483                         if (thisblock > bytes)
484                                 thisblock = bytes;
485                         serv_printf("READ %ld|%ld", accomplished, thisblock);
486                         serv_gets(buf);
487                         if (buf[0] == '6')
488                                 thisblock = extract_long(&buf[4], 0);
489                         serv_read(xferbuf, (int) thisblock);
490                         write(WC->http_sock, xferbuf, thisblock);
491                         bytes = bytes - thisblock;
492                         accomplished = accomplished + thisblock;
493                 }
494                 serv_puts("CLOS");
495                 serv_gets(buf);
496         } else {
497                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
498                 output_headers(0);
499                 wprintf("Content-Type: text/plain\n");
500                 wprintf("\n");
501                 wprintf("Error retrieving image\n");
502         }
503
504 }
505
506
507 /*
508  * Convenience functions to display a page containing only a string
509  */
510 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
511 {
512         wprintf("HTTP/1.0 200 OK\n");
513         output_headers(1);
514         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
515         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
516         wprintf("<B>%s</B>\n", titlebarmsg);
517         wprintf("</FONT></TD></TR></TABLE><BR>\n");
518         escputs(messagetext);
519
520         wprintf("<HR>\n");
521         embed_main_menu();
522         wDumpContent(1);
523 }
524
525 void display_error(char *errormessage)
526 {
527         convenience_page("770000", "Error", errormessage);
528 }
529
530 void display_success(char *successmessage)
531 {
532         convenience_page("007700", "OK", successmessage);
533 }
534
535
536
537 void extract_action(char *actbuf, char *cmdbuf)
538 {
539         int i;
540
541         strcpy(actbuf, cmdbuf);
542         if (!strncasecmp(actbuf, "GET /", 5))
543                 strcpy(actbuf, &actbuf[5]);
544         if (!strncasecmp(actbuf, "PUT /", 5))
545                 strcpy(actbuf, &actbuf[5]);
546         if (!strncasecmp(actbuf, "POST /", 6))
547                 strcpy(actbuf, &actbuf[6]);
548
549         for (i = 0; i < strlen(actbuf); ++i) {
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                 if (actbuf[i] == '?') {
559                         actbuf[i] = 0;
560                         i = 0;
561                 }
562                 if (actbuf[i] == '&') {
563                         actbuf[i] = 0;
564                         i = 0;
565                 }
566         }
567 }
568
569
570 void upload_handler(char *name, char *filename, char *encoding,
571                     void *content, char *cbtype, size_t length)
572 {
573
574         fprintf(stderr, "UPLOAD HANDLER CALLED\n");
575         fprintf(stderr, "    name = %s\n", name);
576         fprintf(stderr, "filename = %s\n", filename);
577         fprintf(stderr, "encoding = %s\n", encoding);
578         fprintf(stderr, "    type = %s\n", cbtype);
579         fprintf(stderr, "  length = %ld\n", (long)length);
580
581         if (strlen(name) > 0) {
582                 WC->upload = malloc(length);
583                 if (WC->upload != NULL) {
584                         WC->upload_length = length;
585                         memcpy(WC->upload, content, length);
586                 }
587         }
588 }
589
590
591 /*
592  * Entry point for WebCit transaction
593  */
594 void session_loop(struct httprequest *req)
595 {
596         char cmd[256];
597         char action[256];
598         char buf[256];
599         int a, b;
600         int ContentLength = 0;
601         int BytesRead;
602         char ContentType[512];
603         char *content;
604         struct httprequest *hptr;
605         char browser_host[256];
606         char user_agent[256];
607
608         /* We stuff these with the values coming from the client cookies,
609          * so we can use them to reconnect a timed out session if we have to.
610          */
611         char c_host[256];
612         char c_port[256];
613         char c_username[256];
614         char c_password[256];
615         char c_roomname[256];
616         char cookie[256];
617
618         strcpy(c_host, defaulthost);
619         strcpy(c_port, defaultport);
620         strcpy(c_username, "");
621         strcpy(c_password, "");
622         strcpy(c_roomname, "");
623
624         WC->upload_length = 0;
625         WC->upload = NULL;
626
627         hptr = req;
628         if (hptr == NULL) return;
629
630         strcpy(cmd, hptr->line);
631         hptr = hptr->next;
632         extract_action(action, cmd);
633
634         while (hptr != NULL) {
635                 strcpy(buf, hptr->line);
636                 hptr = hptr->next;
637
638                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
639                         strcpy(cookie, &buf[15]);
640                         cookie_to_stuff(cookie, NULL,
641                                       c_username, c_password, c_roomname);
642                 }
643                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
644                         ContentLength = atoi(&buf[16]);
645                 }
646                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
647                         strcpy(ContentType, &buf[14]);
648                 }
649                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
650                         strcpy(user_agent, &buf[12]);
651                 }
652         }
653
654         if (ContentLength > 0) {
655                 fprintf(stderr, "Content length: %d\n", ContentLength);
656                 content = malloc(ContentLength + 1);
657                 memset(content, 0, ContentLength+1);
658                 BytesRead = 0;
659
660                 while (BytesRead < ContentLength) {
661                         a=read(WC->http_sock, &content[BytesRead],
662                                 ContentLength - BytesRead);
663                         if (a <= 0) BytesRead = ContentLength;
664                         else BytesRead += a;
665                 }
666
667                 if (!strncasecmp(ContentType,
668                               "application/x-www-form-urlencoded", 33)) {
669                         addurls(content);
670                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
671                         mime_parser(content, ContentLength, ContentType,
672                                     *upload_handler);
673                 }
674         } else {
675                 content = NULL;
676         }
677
678         /* If there are variables in the URL, we must grab them now */
679         for (a = 0; a < strlen(cmd); ++a)
680                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
681                         for (b = a; b < strlen(cmd); ++b)
682                                 if (isspace(cmd[b]))
683                                         cmd[b] = 0;
684                         addurls(&cmd[a + 1]);
685                         cmd[a] = 0;
686                 }
687         /*
688          * If we're not connected to a Citadel server, try to hook up the
689          * connection now.  Preference is given to the host and port specified
690          * by browser cookies, if cookies have been supplied.
691          */
692         if (!WC->connected) {
693                 if (strlen(bstr("host")) > 0)
694                         strcpy(c_host, bstr("host"));
695                 if (strlen(bstr("port")) > 0)
696                         strcpy(c_port, bstr("port"));
697
698                 if (!strcasecmp(c_host, "uds")) {
699                         /* unix domain socket */
700                         sprintf(buf, "%s/citadel.socket", c_port);
701                         WC->serv_sock = uds_connectsock(buf);
702                 }
703                 else {
704                         /* tcp socket */
705                         WC->serv_sock = tcp_connectsock(c_host, c_port);
706                 }
707
708                 if (WC->serv_sock < 0) {
709                         do_logout();
710                 }
711
712                 WC->connected = 1;
713                 serv_gets(buf); /* get the server welcome message */
714                 locate_host(browser_host, WC->http_sock);
715                 get_serv_info(browser_host, user_agent);
716         }
717         check_for_express_messages();
718
719         /*
720          * If we're not logged in, but we have username and password cookies
721          * supplied by the browser, try using them to log in.
722          */
723         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
724                 serv_printf("USER %s", c_username);
725                 serv_gets(buf);
726                 if (buf[0] == '3') {
727                         serv_printf("PASS %s", c_password);
728                         serv_gets(buf);
729                         if (buf[0] == '2') {
730                                 become_logged_in(c_username, c_password, buf);
731                         }
732                 }
733         }
734         /*
735          * If we don't have a current room, but a cookie specifying the
736          * current room is supplied, make an effort to go there.
737          */
738         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
739                 serv_printf("GOTO %s", c_roomname);
740                 serv_gets(buf);
741                 if (buf[0] == '2') {
742                         strcpy(WC->wc_roomname, c_roomname);
743                 }
744         }
745         if (!strcasecmp(action, "static")) {
746                 strcpy(buf, &cmd[12]);
747                 for (a = 0; a < strlen(buf); ++a)
748                         if (isspace(buf[a]))
749                                 buf[a] = 0;
750                 output_static(buf);
751         } else if (!strcasecmp(action, "image")) {
752                 output_image();
753         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
754                 do_login();
755         } else if (!WC->logged_in) {
756                 display_login(NULL);
757         }
758         /* Various commands... */
759
760         else if (!strcasecmp(action, "do_welcome")) {
761                 do_welcome();
762         } else if (!strcasecmp(action, "display_main_menu")) {
763                 display_main_menu();
764         } else if (!strcasecmp(action, "advanced")) {
765                 display_advanced_menu();
766         } else if (!strcasecmp(action, "whobbs")) {
767                 whobbs();
768         } else if (!strcasecmp(action, "knrooms")) {
769                 list_all_rooms_by_floor();
770         } else if (!strcasecmp(action, "gotonext")) {
771                 slrp_highest();
772                 gotonext();
773         } else if (!strcasecmp(action, "skip")) {
774                 gotonext();
775         } else if (!strcasecmp(action, "ungoto")) {
776                 ungoto();
777         } else if (!strcasecmp(action, "dotgoto")) {
778                 slrp_highest();
779                 smart_goto(bstr("room"));
780         } else if (!strcasecmp(action, "termquit")) {
781                 do_logout();
782         } else if (!strcasecmp(action, "readnew")) {
783                 readloop("readnew");
784         } else if (!strcasecmp(action, "readold")) {
785                 readloop("readold");
786         } else if (!strcasecmp(action, "readfwd")) {
787                 readloop("readfwd");
788         } else if (!strcasecmp(action, "display_enter")) {
789                 display_enter();
790         } else if (!strcasecmp(action, "post")) {
791                 post_message();
792         } else if (!strcasecmp(action, "confirm_delete_msg")) {
793                 confirm_delete_msg();
794         } else if (!strcasecmp(action, "delete_msg")) {
795                 delete_msg();
796         } else if (!strcasecmp(action, "confirm_move_msg")) {
797                 confirm_move_msg();
798         } else if (!strcasecmp(action, "move_msg")) {
799                 move_msg();
800         } else if (!strcasecmp(action, "userlist")) {
801                 userlist();
802         } else if (!strcasecmp(action, "showuser")) {
803                 showuser();
804         } else if (!strcasecmp(action, "display_page")) {
805                 display_page();
806         } else if (!strcasecmp(action, "page_user")) {
807                 page_user();
808         } else if (!strcasecmp(action, "chat")) {
809                 do_chat();
810         } else if (!strcasecmp(action, "display_private")) {
811                 display_private("", 0);
812         } else if (!strcasecmp(action, "goto_private")) {
813                 goto_private();
814         } else if (!strcasecmp(action, "zapped_list")) {
815                 zapped_list();
816         } else if (!strcasecmp(action, "display_zap")) {
817                 display_zap();
818         } else if (!strcasecmp(action, "zap")) {
819                 zap();
820         } else if (!strcasecmp(action, "display_entroom")) {
821                 display_entroom();
822         } else if (!strcasecmp(action, "entroom")) {
823                 entroom();
824         } else if (!strcasecmp(action, "display_editroom")) {
825                 display_editroom();
826         } else if (!strcasecmp(action, "editroom")) {
827                 editroom();
828         } else if (!strcasecmp(action, "display_editinfo")) {
829                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
830         } else if (!strcasecmp(action, "editinfo")) {
831                 save_edit("Room info", "EINF 1", 1);
832         } else if (!strcasecmp(action, "display_editbio")) {
833                 sprintf(buf, "RBIO %s", WC->wc_username);
834                 display_edit("Your bio", "NOOP", buf, "editbio");
835         } else if (!strcasecmp(action, "editbio")) {
836                 save_edit("Your bio", "EBIO", 0);
837         } else if (!strcasecmp(action, "confirm_delete_room")) {
838                 confirm_delete_room();
839         } else if (!strcasecmp(action, "delete_room")) {
840                 delete_room();
841         } else if (!strcasecmp(action, "validate")) {
842                 validate();
843         } else if (!strcasecmp(action, "display_editpic")) {
844                 display_graphics_upload("your photo",
845                                         "UIMG 0|_userpic_",
846                                         "/editpic");
847         } else if (!strcasecmp(action, "editpic")) {
848                 do_graphics_upload("UIMG 1|_userpic_");
849         } else if (!strcasecmp(action, "display_editroompic")) {
850                 display_graphics_upload("the graphic for this room",
851                                         "UIMG 0|_roompic_",
852                                         "/editroompic");
853         } else if (!strcasecmp(action, "editroompic")) {
854                 do_graphics_upload("UIMG 1|_roompic_");
855         } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
856                 select_floor_to_edit_pic();
857         } else if (!strcasecmp(action, "display_editfloorpic")) {
858                 sprintf(buf, "UIMG 0|_floorpic_|%s",
859                         bstr("which_floor"));
860                 display_graphics_upload("the graphic for this floor",
861                                         buf,
862                                         "/editfloorpic");
863         } else if (!strcasecmp(action, "editfloorpic")) {
864                 sprintf(buf, "UIMG 1|_floorpic_|%s",
865                         bstr("which_floor"));
866                 do_graphics_upload(buf);
867         } else if (!strcasecmp(action, "display_reg")) {
868                 display_reg(0);
869         } else if (!strcasecmp(action, "register")) {
870                 register_user();
871         } else if (!strcasecmp(action, "display_changepw")) {
872                 display_changepw();
873         } else if (!strcasecmp(action, "changepw")) {
874                 changepw();
875         } else if (!strcasecmp(action, "display_edit_node")) {
876                 display_edit_node();
877         } else if (!strcasecmp(action, "display_netconf")) {
878                 display_netconf();
879         } else if (!strcasecmp(action, "display_confirm_unshare")) {
880                 display_confirm_unshare();
881         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
882                 display_confirm_delete_node();
883         } else if (!strcasecmp(action, "delete_node")) {
884                 delete_node();
885         } else if (!strcasecmp(action, "unshare")) {
886                 unshare();
887         } else if (!strcasecmp(action, "display_add_node")) {
888                 display_add_node();
889         } else if (!strcasecmp(action, "add_node")) {
890                 add_node();
891         } else if (!strcasecmp(action, "display_share")) {
892                 display_share();
893         } else if (!strcasecmp(action, "share")) {
894                 share();
895         } else if (!strcasecmp(action, "terminate_session")) {
896                 slrp_highest();
897                 terminate_session();
898         } else if (!strcasecmp(action, "edit_me")) {
899                 edit_me();
900         } else if (!strcasecmp(action, "display_siteconfig")) {
901                 display_siteconfig();
902         } else if (!strcasecmp(action, "siteconfig")) {
903                 siteconfig();
904         } else if (!strcasecmp(action, "display_generic")) {
905                 display_generic();
906         } else if (!strcasecmp(action, "do_generic")) {
907                 do_generic();
908         } else if (!strcasecmp(action, "display_menubar")) {
909                 display_menubar(1);
910         } else if (!strcasecmp(action, "diagnostics")) {
911                 output_headers(1);
912
913                 wprintf("You're in session %d<HR>\n", WC->wc_session);
914                 wprintf("Command: <BR><PRE>\n");
915                 escputs(cmd);
916                 wprintf("</PRE><HR>\n");
917                 wprintf("Variables: <BR><PRE>\n");
918                 dump_vars();
919                 wprintf("</PRE><HR>\n");
920                 wDumpContent(1);
921         }
922         /* When all else fais, display the main menu. */
923         else {
924                 display_main_menu();
925         }
926
927         fflush(stdout);
928         if (content != NULL) {
929                 free(content);
930                 content = NULL;
931         }
932         free_urls();
933         if (WC->upload_length > 0) {
934                 free(WC->upload);
935                 WC->upload_length = 0;
936         }
937 }