749dcb7a69de31aefa399e3dd12dab0ab3fff060
[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_host[256];
26 char wc_port[256];
27 char wc_username[256];
28 char wc_password[256];
29 char wc_roomname[256];
30 int TransactionCount = 0;
31 int connected = 0;
32 int logged_in = 0;
33 int axlevel;
34 char *ExpressMessages = NULL;
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 void output_headers(int print_standard_html_head) {
265
266         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
267
268         printf("Server: %s\n", SERVER);
269         printf("Connection: close\n");
270         printf("Set-cookie: wc_session=%d\n", wc_session);
271
272         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
273         else printf("Set-cookie: wc_host=%s\n", unset);
274
275         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
276         else printf("Set-cookie: wc_port=%s\n", unset);
277
278         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
279                 wc_username);
280         else printf("Set-cookie: wc_username=%s\n", unset);
281
282         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
283                 wc_password);
284         else printf("Set-cookie: wc_password=%s\n", unset);
285
286         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
287                 wc_roomname);
288         else printf("Set-cookie: wc_roomname=%s\n", unset);
289
290         if (print_standard_html_head) {
291                 wprintf("<HTML><HEAD><TITLE>");
292                 escputs("WebCit");       /* FIX -- add BBS name here */
293                 wprintf("</TITLE></HEAD>");
294                 if (ExpressMessages != NULL) {
295                         wprintf("<SCRIPT language=\"javascript\">\n");
296                         wprintf("function ExpressMessage() {\n");
297                         wprintf(" alert(\"");
298                         escputs(ExpressMessages);
299                         wprintf("\")\n");
300                         wprintf(" }\n </SCRIPT>\n");
301                         }
302                 wprintf("<BODY ");
303                 if (ExpressMessages != NULL) {
304                         wprintf("onload=\"ExpressMessage()\" ");
305                         free(ExpressMessages);
306                         ExpressMessages = NULL;
307                         }
308                 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
309                 }
310
311         }
312
313
314
315
316 void check_for_express_messages() {
317         char buf[256];
318
319         serv_puts("PEXP");
320         serv_gets(buf);
321         if (buf[0]=='1') {
322                 while (serv_gets(buf), strcmp(buf, "000")) {
323                         if (ExpressMessages == NULL) {
324                                 ExpressMessages = malloc(strlen(buf) + 4);
325                                 strcpy(ExpressMessages, "");
326                                 }
327                         else {
328                                 ExpressMessages = realloc(ExpressMessages,
329                                  (strlen(ExpressMessages) + strlen(buf) + 4) );
330                                 }
331                         strcat(ExpressMessages, buf);
332                         strcat(ExpressMessages, "\\n");
333                         }
334                 }
335         }
336
337
338
339
340 void output_static(char *what) {
341         char buf[256];
342         FILE *fp;
343         struct stat statbuf;
344         off_t bytes;
345
346         sprintf(buf, "static/%s", what);
347         fp = fopen(buf, "rb");
348         if (fp == NULL) {
349                 printf("HTTP/1.0 404 %s\n", strerror(errno));
350                 output_headers(0);
351                 printf("Content-Type: text/plain\n");
352                 sprintf(buf, "%s: %s\n", what, strerror(errno));
353                 printf("Content-length: %d\n", strlen(buf));
354                 printf("\n");
355                 fwrite(buf, strlen(buf), 1, stdout);
356                 }
357         else {
358                 printf("HTTP/1.0 200 OK\n");
359                 output_headers(0);
360
361                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
362                         printf("Content-type: image/gif\n");
363                 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
364                         printf("Content-type: image/jpeg\n");
365                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
366                         printf("Content-type: text/html\n");
367                 else
368                         printf("Content-type: application/octet-stream\n");
369
370                 fstat(fileno(fp), &statbuf);
371                 bytes = statbuf.st_size;
372                 printf("Content-length: %ld\n", (long)bytes);
373                 printf("\n");
374                 while (bytes--) {
375                         putc(getc(fp), stdout);
376                         }
377                 fflush(stdout);
378                 fclose(fp);
379                 }
380         }
381
382 void output_image() {
383         char buf[256];
384         char xferbuf[4096];
385         off_t bytes;
386         off_t thisblock;
387         off_t accomplished = 0L;
388
389
390         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
391         serv_gets(buf);
392         if (buf[0]=='2') {
393                 bytes = extract_long(&buf[4], 0);
394                 printf("HTTP/1.0 200 OK\n");
395                 output_headers(0);
396                 printf("Content-type: image/gif\n");
397                 printf("Content-length: %ld\n", bytes);
398                 printf("\n");
399
400                 while (bytes > (off_t)0) {
401                         thisblock = (off_t)sizeof(xferbuf);
402                         if (thisblock > bytes) thisblock = bytes;
403                         serv_printf("READ %ld|%ld", accomplished, thisblock);
404                         serv_gets(buf);
405                         if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
406                         serv_read(xferbuf, (int)thisblock);
407                         fwrite(xferbuf, thisblock, 1, stdout);
408                         bytes = bytes - thisblock;
409                         accomplished = accomplished + thisblock;
410                         }
411                 fflush(stdout);
412                 serv_puts("CLOS");
413                 serv_gets(buf);
414                 }
415         else {
416                 printf("HTTP/1.0 404 %s\n", strerror(errno));
417                 output_headers(0);
418                 printf("Content-Type: text/plain\n");
419                 sprintf(buf, "Error retrieving image\n");
420                 printf("Content-length: %d\n", strlen(buf));
421                 printf("\n");
422                 fwrite(buf, strlen(buf), 1, stdout);
423                 }
424
425         }
426
427
428 /*
429  * Convenience function to display a page containing only an error string
430  */
431 void display_error(char *errormessage) {
432         printf("HTTP/1.0 200 OK\n");
433         output_headers(1);
434         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
435         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
436         wprintf("<B>Error</B>\n");
437         wprintf("</FONT></TD></TR></TABLE><BR>\n");
438         escputs(errormessage);
439         wprintf("</BODY></HTML>\n");
440         wDumpContent();
441         }
442
443
444
445
446 void extract_action(char *actbuf, char *cmdbuf) {
447         int i;
448
449         strcpy(actbuf, cmdbuf);
450         if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
451         if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
452         if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
453
454         for (i=0; i<strlen(actbuf); ++i) {
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                 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
459                 }
460         }
461
462
463 void session_loop(void) {
464         char cmd[256];
465         char action[256];
466         char buf[256];
467         int a, b;
468         int ContentLength = 0;
469         char ContentType[512];
470         char *content;
471
472         /* We stuff these with the values coming from the client cookies,
473          * so we can use them to reconnect a timed out session if we have to.
474          */
475         char c_host[256];
476         char c_port[256];
477         char c_username[256];
478         char c_password[256];
479         char c_roomname[256];
480
481         strcpy(c_host, defaulthost);
482         strcpy(c_port, defaultport);
483         strcpy(c_username, "");
484         strcpy(c_password, "");
485         strcpy(c_roomname, "");
486
487         upload_length = 0;
488         upload = NULL;
489
490         getz(cmd);
491         extract_action(action, cmd);
492
493         do {
494                 getz(buf);
495
496                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
497                         strcpy(c_host, &buf[16]);
498                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
499                         strcpy(c_port, &buf[16]);
500                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
501                         strcpy(c_username, &buf[20]);
502                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
503                         strcpy(c_password, &buf[20]);
504                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
505                         strcpy(c_roomname, &buf[20]);
506                 if (!strncasecmp(buf, "Content-length: ", 16)) {
507                         ContentLength = atoi(&buf[16]);
508                         }
509                 if (!strncasecmp(buf, "Content-type: ", 14)) {
510                         strcpy(ContentType, &buf[14]);
511                         }
512                 } while(strlen(buf)>0);
513
514         ++TransactionCount;
515
516         if (ContentLength > 0) {
517                 content = malloc(ContentLength+1);
518                 fread(content, ContentLength, 1, stdin);
519
520                 content[ContentLength] = 0;
521
522                 if (!strncasecmp(ContentType,
523                    "application/x-www-form-urlencoded", 33)) {
524                         addurls(content);
525                         }
526                 else if (!strncasecmp(ContentType, "multipart", 9)) {
527                         mime_parser(content, ContentLength, ContentType);
528                         }
529                 }
530         else {
531                 content = NULL;
532                 }
533
534         /* If there are variables in the URL, we must grab them now */  
535         for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
536                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
537                 addurls(&cmd[a+1]);
538                 cmd[a] = 0;
539                 }
540
541         /*
542          * If we're not connected to a Citadel server, try to hook up the
543          * connection now.  Preference is given to the host and port specified
544          * by browser cookies, if cookies have been supplied.
545          */
546         if (!connected) {
547                 if (strlen(bstr("host"))>0) strcpy(c_host, bstr("host"));
548                 if (strlen(bstr("port"))>0) strcpy(c_port, bstr("port"));
549                 serv_sock = connectsock(c_host, c_port, "tcp");
550                 connected = 1;
551                 serv_gets(buf); /* get the server welcome message */
552                 strcpy(wc_host, c_host);
553                 strcpy(wc_port, c_port);
554                 get_serv_info();
555                 }
556
557         check_for_express_messages();
558
559         /*
560          * If we're not logged in, but we have username and password cookies
561          * supplied by the browser, try using them to log in.
562          */
563         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
564                 serv_printf("USER %s", c_username);
565                 serv_gets(buf);
566                 if (buf[0]=='3') {
567                         serv_printf("PASS %s", c_password);
568                         serv_gets(buf);
569                         if (buf[0]=='2') {
570                                 become_logged_in(c_username, c_password, buf);
571                                 }
572                         }
573                 }
574
575         /*
576          * If we don't have a current room, but a cookie specifying the
577          * current room is supplied, make an effort to go there.
578          */
579         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
580                 serv_printf("GOTO %s", c_roomname);
581                 serv_gets(buf);
582                 if (buf[0]=='2') {
583                         strcpy(wc_roomname, c_roomname);
584                         }
585                 }
586
587         /* FIX unfix etc. here's where the addurls() WAS... */
588         if (!strcasecmp(action, "static")) {
589                 strcpy(buf, &cmd[12]);
590                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
591                 output_static(buf);
592                 }
593
594         else if (!strcasecmp(action, "image")) {
595                 output_image();
596                 }
597
598         else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
599                 do_login();
600                 }
601
602         else if (!logged_in) {
603                 display_login(NULL);
604                 }
605
606         /* Various commands... */
607         
608         else if (!strcasecmp(action, "do_welcome")) {
609                 do_welcome();
610                 }
611
612         else if (!strcasecmp(action, "display_main_menu")) {
613                 display_main_menu();
614                 }
615
616         else if (!strcasecmp(action, "advanced")) {
617                 display_advanced_menu();
618                 }
619
620         else if (!strcasecmp(action, "whobbs")) {
621                 whobbs();
622                 }
623
624         else if (!strcasecmp(action, "knrooms")) {
625                 list_all_rooms_by_floor();
626                 }
627
628         else if (!strcasecmp(action, "gotonext")) {
629                 slrp_highest();
630                 gotonext();
631                 }
632
633         else if (!strcasecmp(action, "skip")) {
634                 gotonext();
635                 }
636
637         else if (!strcasecmp(action, "ungoto")) {
638                 ungoto();
639                 }
640
641         else if (!strcasecmp(action, "dotgoto")) {
642                 slrp_highest();
643                 gotoroom(bstr("room"), 1);
644                 }
645
646         else if (!strcasecmp(action, "termquit")) {
647                 do_logout();
648                 }
649
650         else if (!strcasecmp(action, "readnew")) {
651                 readloop("readnew");
652                 }
653
654         else if (!strcasecmp(action, "readold")) {
655                 readloop("readold");
656                 }
657
658         else if (!strcasecmp(action, "readfwd")) {
659                 readloop("readfwd");
660                 }
661
662         else if (!strcasecmp(action, "display_enter")) {
663                 display_enter();
664                 }
665
666         else if (!strcasecmp(action, "post")) {
667                 post_message();
668                 }
669
670         else if (!strcasecmp(action, "confirm_delete_msg")) {
671                 confirm_delete_msg();
672                 }
673
674         else if (!strcasecmp(action, "delete_msg")) {
675                 delete_msg();
676                 }
677
678         else if (!strcasecmp(action, "confirm_move_msg")) {
679                 confirm_move_msg();
680                 }
681
682         else if (!strcasecmp(action, "move_msg")) {
683                 move_msg();
684                 }
685
686         else if (!strcasecmp(action, "userlist")) {
687                 userlist();
688                 }
689
690         else if (!strcasecmp(action, "showuser")) {
691                 showuser();
692                 }
693
694         else if (!strcasecmp(action, "display_page")) {
695                 display_page();
696                 }
697
698         else if (!strcasecmp(action, "page_user")) {
699                 page_user();
700                 }
701
702         else if (!strcasecmp(action, "chat")) {
703                 do_chat();
704                 }
705
706         else if (!strcasecmp(action, "display_private")) {
707                 display_private("", 0);
708                 }
709
710         else if (!strcasecmp(action, "goto_private")) {
711                 goto_private();
712                 }
713
714         else if (!strcasecmp(action, "zapped_list")) {
715                 zapped_list();
716                 }
717
718         else if (!strcasecmp(action, "display_zap")) {
719                 display_zap();
720                 }
721
722         else if (!strcasecmp(action, "zap")) {
723                 zap();
724                 }
725
726         else if (!strcasecmp(action, "display_entroom")) {
727                 display_entroom();
728                 }
729
730         else if (!strcasecmp(action, "entroom")) {
731                 entroom();
732                 }
733
734         else if (!strcasecmp(action, "display_editroom")) {
735                 display_editroom();
736                 }
737
738         else if (!strcasecmp(action, "editroom")) {
739                 editroom();
740                 }
741
742         else if (!strcasecmp(action, "display_editinfo")) {
743                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
744                 }
745
746         else if (!strcasecmp(action, "editinfo")) {
747                 save_edit("Room info", "EINF 1", 1);
748                 }
749
750         else if (!strcasecmp(action, "display_editbio")) {
751                 sprintf(buf, "RBIO %s", wc_username);
752                 display_edit("Your bio", "NOOP", buf, "editbio");
753                 }
754
755         else if (!strcasecmp(action, "editbio")) {
756                 save_edit("Your bio", "EBIO", 0);
757                 }
758
759         else if (!strcasecmp(action, "confirm_delete_room")) {
760                 confirm_delete_room();
761                 }
762
763         else if (!strcasecmp(action, "delete_room")) {
764                 delete_room();
765                 }
766
767         else if (!strcasecmp(action, "validate")) {
768                 validate();
769                 }
770
771         else if (!strcasecmp(action, "display_editpic")) {
772                 display_graphics_upload("your photo",
773                                         "UIMG 0|_userpic_",
774                                         "/editpic");
775                 }
776
777         else if (!strcasecmp(action, "editpic")) {
778                 do_graphics_upload("UIMG 1|_userpic_");
779                 }
780
781         else if (!strcasecmp(action, "display_editroompic")) {
782                 display_graphics_upload("the graphic for this room",
783                                         "UIMG 0|_roompic_",
784                                         "/editroompic");
785                 }
786
787         else if (!strcasecmp(action, "editroompic")) {
788                 do_graphics_upload("UIMG 1|_roompic_");
789                 }
790
791         else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
792                 select_floor_to_edit_pic();
793                 }
794
795         else if (!strcasecmp(action, "display_editfloorpic")) {
796                 sprintf(buf, "UIMG 0|_floorpic_|%s",
797                         bstr("which_floor"));
798                 display_graphics_upload("the graphic for this floor",
799                                         buf,
800                                         "/editfloorpic");
801                 }
802
803         else if (!strcasecmp(action, "editfloorpic")) {
804                 sprintf(buf, "UIMG 1|_floorpic_|%s",
805                         bstr("which_floor"));
806                 do_graphics_upload(buf);
807                 }
808
809         else if (!strcasecmp(action, "display_reg")) {
810                 display_reg(0);
811                 }
812
813         else if (!strcasecmp(action, "register")) {
814                 register_user();
815                 }
816
817         /* When all else fails... */
818         else {
819                 printf("HTTP/1.0 200 OK\n");
820                 output_headers(1);
821         
822                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
823                 wprintf("You're in session %d<HR>\n", wc_session);
824                 wprintf("Command: <BR><PRE>\n");
825                 escputs(cmd);
826                 wprintf("</PRE><HR>\n");
827                 wprintf("Variables: <BR><PRE>\n");
828                 dump_vars();
829                 wprintf("</PRE><HR>\n");
830                 wprintf("</BODY></HTML>\n");
831                 wDumpContent();
832                 }
833
834         fflush(stdout);
835         if (content != NULL) {
836                 free(content);
837                 content = NULL;
838                 }
839         free_urls();
840         if (upload_length > 0) {
841                 free(upload);
842                 upload_length = 0;
843                 }
844         }
845
846 int main(int argc, char *argv[]) {
847
848         if (argc < 2 || argc > 4) {
849                 fprintf(stderr,
850                         "webcit: usage: webcit <session_id> [host [port]]\n");
851                 return 1;
852                 }
853
854         wc_session = atoi(argv[1]);
855
856         if (argc > 2) {
857                 defaulthost = argv[2];
858                 if (argc > 3)
859                         defaultport = argv[3];
860                 }
861
862         strcpy(wc_host, "");
863         strcpy(wc_port, "");
864         strcpy(wc_username, "");
865         strcpy(wc_password, "");
866         strcpy(wc_roomname, "");
867
868         while (1) {
869                 session_loop();
870                 }
871         }