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