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