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