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