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