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