]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* Repaired HTTP output for image transfers and static content
[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
35 int TransactionCount = 0;
36 char *ExpressMessages = NULL;
37
38 /* This variable is set to 1 if the room banner and menubar have been
39  * displayed, and we need to close the <TABLE> tags.
40  */
41 int fake_frames = 0;
42
43 struct urlcontent *urlstrings = NULL;
44
45 static const char *defaulthost = DEFAULT_HOST;
46 static const char *defaultport = DEFAULT_PORT;
47
48
49
50 void unescape_input(char *buf)
51 {
52         int a, b;
53         char hex[3];
54
55         while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
56                 buf[strlen(buf) - 1] = 0;
57
58         for (a = 0; a < strlen(buf); ++a) {
59                 if (buf[a] == '+')
60                         buf[a] = ' ';
61                 if (buf[a] == '%') {
62                         hex[0] = buf[a + 1];
63                         hex[1] = buf[a + 2];
64                         hex[2] = 0;
65                         sscanf(hex, "%02x", &b);
66                         buf[a] = (char) b;
67                         strcpy(&buf[a + 1], &buf[a + 3]);
68                 }
69         }
70
71 }
72
73
74 void addurls(char *url)
75 {
76         char *up, *ptr;
77         char buf[256];
78         int a, b;
79         struct urlcontent *u;
80
81         up = url;
82         while (strlen(up) > 0) {
83
84                 /* locate the = sign */
85                 strncpy(buf, up, 255);
86                 b = (-1);
87                 for (a = 255; a >= 0; --a)
88                         if (buf[a] == '=')
89                                 b = a;
90                 if (b < 0)
91                         return;
92                 buf[b] = 0;
93
94                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
95                 u->next = urlstrings;
96                 urlstrings = u;
97                 strcpy(u->url_key, buf);
98
99                 /* now chop that part off */
100                 for (a = 0; a <= b; ++a)
101                         ++up;
102
103                 /* locate the & sign */
104                 ptr = up;
105                 b = strlen(up);
106                 for (a = 0; a < strlen(up); ++a) {
107                         if (!strncmp(ptr, "&", 1)) {
108                                 b = a;
109                                 break;
110                         }
111                         ++ptr;
112                 }
113                 ptr = up;
114                 for (a = 0; a < b; ++a)
115                         ++ptr;
116                 strcpy(ptr, "");
117
118                 u->url_data = malloc(strlen(up) + 1);
119                 strcpy(u->url_data, up);
120                 u->url_data[b] = 0;
121                 unescape_input(u->url_data);
122                 up = ptr;
123                 ++up;
124         }
125 }
126
127 void free_urls(void)
128 {
129         struct urlcontent *u;
130
131         while (urlstrings != NULL) {
132                 free(urlstrings->url_data);
133                 u = urlstrings->next;
134                 free(urlstrings);
135                 urlstrings = u;
136         }
137 }
138
139 /*
140  * Diagnostic function to display the contents of all variables
141  */
142 void dump_vars(void)
143 {
144         struct urlcontent *u;
145
146         for (u = urlstrings; u != NULL; u = u->next) {
147                 wprintf("%38s = %s\n", u->url_key, u->url_data);
148         }
149 }
150
151 char *bstr(char *key)
152 {
153         struct urlcontent *u;
154
155         for (u = urlstrings; u != NULL; u = u->next) {
156                 if (!strcasecmp(u->url_key, key))
157                         return (u->url_data);
158         }
159         return ("");
160 }
161
162
163 void wprintf(const char *format,...)
164 {
165         va_list arg_ptr;
166         char wbuf[1024];
167
168         va_start(arg_ptr, format);
169         vsprintf(wbuf, format, arg_ptr);
170         va_end(arg_ptr);
171
172         write(WC->http_sock, wbuf, strlen(wbuf));
173 }
174
175
176 /*
177  * wDumpContent() wraps up an HTTP session, closes tags, etc.
178  *
179  * print_standard_html_footer should be set to 0 to transmit only, 1 to
180  * append the main menu and closing tags, or 2 to
181  * append the closing tags only.
182  */
183 void wDumpContent(int print_standard_html_footer)
184 {
185         if (fake_frames) {
186                 wprintf("<CENTER><FONT SIZE=-1>"
187                         "<TABLE border=0 width=100%><TR>"
188                         "<TD><A HREF=\"/ungoto\">"
189                         "<IMG SRC=\"/static/back.gif\" BORDER=0>"
190                         "Ungoto</A></TD>");
191                 wprintf("<TD><A HREF=\"#TheTop\">"
192                         "<IMG SRC=\"/static/up.gif\" BORDER=0>"
193                         "Top of page</A></TD>");
194                 wprintf("<TD><A HREF=\"/display_enter\">"
195                         "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
196                         "Enter a message</A></TD>");
197                 wprintf("<TD><A HREF=\"/gotonext\">"
198                         "Goto next room"
199                         "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
200                         "</TR></TABLE>"
201                         "</FONT>\n"
202                         "</TD></TR></TABLE></TABLE>\n");
203                 fake_frames = 0;
204         }
205
206         if (print_standard_html_footer) {
207                 if (print_standard_html_footer != 2) {
208                         wprintf("<BR>");
209                 }
210                 wprintf("</BODY></HTML>\n");
211         }
212
213
214 }
215
216
217 void escputs1(char *strbuf, int nbsp)
218 {
219         int a;
220
221         for (a = 0; a < strlen(strbuf); ++a) {
222                 if (strbuf[a] == '<')
223                         wprintf("&lt;");
224                 else if (strbuf[a] == '>')
225                         wprintf("&gt;");
226                 else if (strbuf[a] == '&')
227                         wprintf("&amp;");
228                 else if (strbuf[a] == '\"')
229                         wprintf("&quot;");
230                 else if (strbuf[a] == '\'') 
231                         wprintf("&#39;");
232                 else if (strbuf[a] == LB)
233                         wprintf("<");
234                 else if (strbuf[a] == RB)
235                         wprintf(">");
236                 else if (strbuf[a] == QU)
237                         wprintf("\"");
238                 else if ((strbuf[a] == 32) && (nbsp == 1)) {
239                         wprintf("&nbsp;");
240                 } else {
241                         wprintf("%c", strbuf[a]);
242                 }
243         }
244 }
245
246 void escputs(char *strbuf)
247 {
248         escputs1(strbuf, 0);
249 }
250
251
252
253 char *urlesc(char *strbuf)
254 {
255         int a, b, c;
256         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
257         static char outbuf[512];
258
259         strcpy(outbuf, "");
260
261         for (a = 0; a < strlen(strbuf); ++a) {
262                 c = 0;
263                 for (b = 0; b < strlen(ec); ++b) {
264                         if (strbuf[a] == ec[b])
265                                 c = 1;
266                 }
267                 b = strlen(outbuf);
268                 if (c == 1)
269                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
270                 else
271                         sprintf(&outbuf[b], "%c", strbuf[a]);
272         }
273         return (outbuf);
274 }
275
276 void urlescputs(char *strbuf)
277 {
278         wprintf("%s", urlesc(strbuf));
279 }
280
281
282
283
284 /*
285  * Output all that important stuff that the browser will want to see
286  *
287  * print_standard_html_head values:
288  * 0 = Nothing.  Do not display any leading HTTP or HTML.
289  * 1 = HTTP headers plus the "fake frames" found in most windows.
290  * 2 = HTTP headers required to terminate the session (unset cookies)
291  * 3 = HTTP headers only.
292  */
293 void output_headers(int print_standard_html_head)
294 {
295
296         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
297         char cookie[256];
298
299         if (print_standard_html_head > 0) {
300                 wprintf("Content-type: text/html\n");
301                 wprintf("Server: %s\n", SERVER);
302                 wprintf("Connection: close\n");
303                 wprintf("Pragma: no-cache\n");
304                 wprintf("Cache-Control: no-store\n");
305         }
306         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
307                         WC->wc_password, WC->wc_roomname);
308         if (print_standard_html_head == 2) {
309                 wprintf("X-WebCit-Session: close\n");
310                 wprintf("Set-cookie: webcit=%s\n", unset);
311         } else {
312                 wprintf("Set-cookie: webcit=%s\n", cookie);
313         }
314
315         if (print_standard_html_head > 0) {
316                 wprintf("\n");
317                 wprintf("<HTML><HEAD><TITLE>");
318                 escputs(serv_info.serv_humannode);
319                 wprintf("</TITLE>\n"
320                         "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
321                         "</HEAD>\n");
322                 if (ExpressMessages != NULL) {
323                         wprintf("<SCRIPT language=\"javascript\">\n");
324                         wprintf("function ExpressMessage() {\n");
325                         wprintf(" alert(\"");
326                         escputs(ExpressMessages);
327                         wprintf("\")\n");
328                         wprintf(" }\n </SCRIPT>\n");
329                 }
330
331
332
333                 /* JavaScript key-based navigation would go here if it
334                  * were finished
335                  */
336
337                 wprintf("<BODY ");
338                 if (ExpressMessages != NULL) {
339                         wprintf("onload=\"ExpressMessage()\" ");
340                         free(ExpressMessages);
341                         ExpressMessages = NULL;
342                 }
343                 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
344         
345         
346         if (print_standard_html_head == 1) {
347                 wprintf("<A NAME=\"TheTop\"></A>"
348                         "<TABLE border=0 width=100%>"
349                         "<TR VALIGN=TOP><TD>");
350
351                 display_menubar(0);
352
353                 wprintf("</TD><TD VALIGN=TOP>"
354                         "<TABLE border=0 width=100%><TR VALIGN=TOP>"
355                         "<TD>\n");
356
357                 embed_room_banner(NULL);
358
359                 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
360                 
361                 fake_frames = 1;
362                 }
363         }
364 }
365
366
367
368 void ExpressMessageCat(char *buf) {
369         if (ExpressMessages == NULL) {
370                 ExpressMessages = malloc(strlen(buf) + 4);
371                 strcpy(ExpressMessages, "");
372         } else {
373                 ExpressMessages = realloc(ExpressMessages,
374                         (strlen(ExpressMessages) + strlen(buf) + 4));
375         }
376         strcat(ExpressMessages, buf);
377         strcat(ExpressMessages, "\\n");
378 }
379
380
381 void check_for_express_messages()
382 {
383         char buf[256];
384
385         serv_puts("PEXP");
386         serv_gets(buf);
387         if (buf[0] == '1') {
388                 while (serv_gets(buf), strcmp(buf, "000")) {
389                         ExpressMessageCat(buf);
390                 }
391         }
392 }
393
394
395
396
397 void output_static(char *what)
398 {
399         char buf[4096];
400         long thisblock;
401         FILE *fp;
402         struct stat statbuf;
403         off_t bytes;
404
405         sprintf(buf, "static/%s", what);
406         fp = fopen(buf, "rb");
407         if (fp == NULL) {
408                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
409                 wprintf("Content-Type: text/plain\n");
410                 wprintf("\n");
411                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
412         } else {
413                 wprintf("HTTP/1.0 200 OK\n");
414                 output_headers(0);
415
416                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
417                         wprintf("Content-type: image/gif\n");
418                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
419                         wprintf("Content-type: text/plain\n");
420                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
421                         wprintf("Content-type: image/jpeg\n");
422                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
423                         wprintf("Content-type: text/html\n");
424                 else
425                         wprintf("Content-type: application/octet-stream\n");
426
427                 fstat(fileno(fp), &statbuf);
428                 bytes = statbuf.st_size;
429                 fprintf(stderr, "Static: %s, %ld bytes\n", what, bytes);
430                 wprintf("Content-length: %ld\n", (long) bytes);
431                 wprintf("\n");
432                 while (bytes > 0) {
433                         thisblock = sizeof(buf);
434                         if (thisblock > bytes) thisblock = bytes;
435                         fread(buf, thisblock, 1, fp);
436                         write(WC->http_sock, buf, thisblock);
437                         bytes = bytes - thisblock;
438                 }
439                 fclose(fp);
440         }
441 }
442
443 /*
444  * When the browser requests an image file from the Citadel server,
445  * this function is called to transmit it.
446  */
447 void output_image()
448 {
449         char buf[256];
450         char xferbuf[4096];
451         off_t bytes;
452         off_t thisblock;
453         off_t accomplished = 0L;
454
455
456         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
457         serv_gets(buf);
458         if (buf[0] == '2') {
459                 bytes = extract_long(&buf[4], 0);
460                 wprintf("HTTP/1.0 200 OK\n");
461                 output_headers(0);
462                 wprintf("Content-type: image/gif\n");
463                 wprintf("Content-length: %ld\n", (long) bytes);
464                 wprintf("\n");
465
466                 while (bytes > (off_t) 0) {
467                         thisblock = (off_t) sizeof(xferbuf);
468                         if (thisblock > bytes)
469                                 thisblock = bytes;
470                         serv_printf("READ %ld|%ld", accomplished, thisblock);
471                         serv_gets(buf);
472                         if (buf[0] == '6')
473                                 thisblock = extract_long(&buf[4], 0);
474                         serv_read(xferbuf, (int) thisblock);
475                         write(WC->http_sock, xferbuf, thisblock);
476                         bytes = bytes - thisblock;
477                         accomplished = accomplished + thisblock;
478                 }
479                 serv_puts("CLOS");
480                 serv_gets(buf);
481         } else {
482                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
483                 output_headers(0);
484                 wprintf("Content-Type: text/plain\n");
485                 wprintf("\n");
486                 wprintf("Error retrieving image\n");
487         }
488
489 }
490
491
492 /*
493  * Convenience functions to display a page containing only a string
494  */
495 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
496 {
497         wprintf("HTTP/1.0 200 OK\n");
498         output_headers(1);
499         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
500         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
501         wprintf("<B>%s</B>\n", titlebarmsg);
502         wprintf("</FONT></TD></TR></TABLE><BR>\n");
503         escputs(messagetext);
504
505         wprintf("<HR>\n");
506         embed_main_menu();
507         wDumpContent(1);
508 }
509
510 void display_error(char *errormessage)
511 {
512         convenience_page("770000", "Error", errormessage);
513 }
514
515 void display_success(char *successmessage)
516 {
517         convenience_page("007700", "OK", successmessage);
518 }
519
520
521
522 void extract_action(char *actbuf, char *cmdbuf)
523 {
524         int i;
525
526         strcpy(actbuf, cmdbuf);
527         if (!strncasecmp(actbuf, "GET /", 5))
528                 strcpy(actbuf, &actbuf[5]);
529         if (!strncasecmp(actbuf, "PUT /", 5))
530                 strcpy(actbuf, &actbuf[5]);
531         if (!strncasecmp(actbuf, "POST /", 6))
532                 strcpy(actbuf, &actbuf[6]);
533
534         for (i = 0; i < strlen(actbuf); ++i) {
535                 if (actbuf[i] == ' ') {
536                         actbuf[i] = 0;
537                         i = 0;
538                 }
539                 if (actbuf[i] == '/') {
540                         actbuf[i] = 0;
541                         i = 0;
542                 }
543                 if (actbuf[i] == '?') {
544                         actbuf[i] = 0;
545                         i = 0;
546                 }
547                 if (actbuf[i] == '&') {
548                         actbuf[i] = 0;
549                         i = 0;
550                 }
551         }
552 }
553
554
555 void upload_handler(char *name, char *filename, char *encoding,
556                     void *content, char *cbtype, size_t length)
557 {
558
559         fprintf(stderr, "UPLOAD HANDLER CALLED\n");
560         fprintf(stderr, "    name = %s\n", name);
561         fprintf(stderr, "filename = %s\n", filename);
562         fprintf(stderr, "encoding = %s\n", encoding);
563         fprintf(stderr, "    type = %s\n", cbtype);
564         fprintf(stderr, "  length = %d\n", length);
565
566         if (strlen(name) > 0) {
567                 WC->upload = malloc(length);
568                 if (WC->upload != NULL) {
569                         WC->upload_length = length;
570                         memcpy(WC->upload, content, length);
571                 }
572         }
573 }
574
575
576 /*
577  * Entry point for WebCit transaction
578  */
579 void session_loop(struct httprequest *req)
580 {
581         char cmd[256];
582         char action[256];
583         char buf[256];
584         int a, b;
585         int ContentLength = 0;
586         char ContentType[512];
587         char *content;
588         struct httprequest *hptr;
589         char browser_host[256];
590         char user_agent[256];
591
592         /* We stuff these with the values coming from the client cookies,
593          * so we can use them to reconnect a timed out session if we have to.
594          */
595         char c_host[256];
596         char c_port[256];
597         char c_username[256];
598         char c_password[256];
599         char c_roomname[256];
600         char cookie[256];
601
602         strcpy(c_host, defaulthost);
603         strcpy(c_port, defaultport);
604         strcpy(c_username, "");
605         strcpy(c_password, "");
606         strcpy(c_roomname, "");
607
608         WC->upload_length = 0;
609         WC->upload = NULL;
610
611         hptr = req;
612         if (hptr == NULL) return;
613
614         strcpy(cmd, hptr->line);
615         hptr = hptr->next;
616         extract_action(action, cmd);
617
618         while (hptr != NULL) {
619                 strcpy(buf, hptr->line);
620                 hptr = hptr->next;
621
622                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
623                         strcpy(cookie, &buf[15]);
624                         cookie_to_stuff(cookie, NULL,
625                                       c_username, c_password, c_roomname);
626                 }
627                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
628                         ContentLength = atoi(&buf[16]);
629                 }
630                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
631                         strcpy(ContentType, &buf[14]);
632                 }
633                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
634                         strcpy(user_agent, &buf[12]);
635                 }
636         }
637
638         ++TransactionCount;     /* FIX ... make session-specific OR remove */
639
640         if (ContentLength > 0) {
641                 content = malloc(ContentLength + 1);
642                 read(WC->http_sock, content, ContentLength);
643
644                 content[ContentLength] = 0;
645
646                 if (!strncasecmp(ContentType,
647                               "application/x-www-form-urlencoded", 33)) {
648                         addurls(content);
649                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
650                         mime_parser(content, ContentLength, ContentType,
651                                     *upload_handler);
652                 }
653         } else {
654                 content = NULL;
655         }
656
657         /* If there are variables in the URL, we must grab them now */
658         for (a = 0; a < strlen(cmd); ++a)
659                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
660                         for (b = a; b < strlen(cmd); ++b)
661                                 if (isspace(cmd[b]))
662                                         cmd[b] = 0;
663                         addurls(&cmd[a + 1]);
664                         cmd[a] = 0;
665                 }
666         /*
667          * If we're not connected to a Citadel server, try to hook up the
668          * connection now.  Preference is given to the host and port specified
669          * by browser cookies, if cookies have been supplied.
670          */
671         if (!WC->connected) {
672                 if (strlen(bstr("host")) > 0)
673                         strcpy(c_host, bstr("host"));
674                 if (strlen(bstr("port")) > 0)
675                         strcpy(c_port, bstr("port"));
676                 WC->serv_sock = connectsock(c_host, c_port, "tcp");
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                 wprintf("HTTP/1.0 200 OK\n");
881                 output_headers(1);
882
883                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
884                 wprintf("You're in session %d<HR>\n", WC->wc_session);
885                 wprintf("Command: <BR><PRE>\n");
886                 escputs(cmd);
887                 wprintf("</PRE><HR>\n");
888                 wprintf("Variables: <BR><PRE>\n");
889                 dump_vars();
890                 wprintf("</PRE><HR>\n");
891                 wDumpContent(1);
892         }
893         /* When all else fais, display the main menu. */
894         else {
895                 display_main_menu();
896         }
897
898         fflush(stdout);
899         if (content != NULL) {
900                 free(content);
901                 content = NULL;
902         }
903         free_urls();
904         if (WC->upload_length > 0) {
905                 free(WC->upload);
906                 WC->upload_length = 0;
907         }
908 }