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