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