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