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