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