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