]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
34e97a88a6f2a6f39a031e7f57012059e513413c
[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         /*
535          * If we're not connected to a Citadel server, try to hook up the
536          * connection now.  Preference is given to the host and port specified
537          * by browser cookies, if cookies have been supplied.
538          */
539         if (!connected) {
540                 serv_sock = connectsock(c_host, c_port, "tcp");
541                 connected = 1;
542                 serv_gets(buf); /* get the server welcome message */
543                 strcpy(wc_host, c_host);
544                 strcpy(wc_port, c_port);
545                 get_serv_info();
546                 }
547
548         check_for_express_messages();
549
550         /*
551          * If we're not logged in, but we have username and password cookies
552          * supplied by the browser, try using them to log in.
553          */
554         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
555                 serv_printf("USER %s", c_username);
556                 serv_gets(buf);
557                 if (buf[0]=='3') {
558                         serv_printf("PASS %s", c_password);
559                         serv_gets(buf);
560                         if (buf[0]=='2') {
561                                 become_logged_in(c_username, c_password, buf);
562                                 }
563                         }
564                 }
565
566         /*
567          * If we don't have a current room, but a cookie specifying the
568          * current room is supplied, make an effort to go there.
569          */
570         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
571                 serv_printf("GOTO %s", c_roomname);
572                 serv_gets(buf);
573                 if (buf[0]=='2') {
574                         strcpy(wc_roomname, c_roomname);
575                         }
576                 }
577
578         /* If there are variables in the URL, we must grab them now */  
579         for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
580                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
581                 addurls(&cmd[a+1]);
582                 cmd[a] = 0;
583                 }
584
585         if (!strcasecmp(action, "static")) {
586                 strcpy(buf, &cmd[12]);
587                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
588                 output_static(buf);
589                 }
590
591         else if (!strcasecmp(action, "image")) {
592                 output_image();
593                 }
594
595         else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
596                 do_login();
597                 }
598
599         else if (!logged_in) {
600                 display_login(NULL);
601                 }
602
603         /* Various commands... */
604         
605         else if (!strcasecmp(action, "do_welcome")) {
606                 do_welcome();
607                 }
608
609         else if (!strcasecmp(action, "display_main_menu")) {
610                 display_main_menu();
611                 }
612
613         else if (!strcasecmp(action, "advanced")) {
614                 display_advanced_menu();
615                 }
616
617         else if (!strcasecmp(action, "whobbs")) {
618                 whobbs();
619                 }
620
621         else if (!strcasecmp(action, "knrooms")) {
622                 list_all_rooms_by_floor();
623                 }
624
625         else if (!strcasecmp(action, "gotonext")) {
626                 slrp_highest();
627                 gotonext();
628                 }
629
630         else if (!strcasecmp(action, "skip")) {
631                 gotonext();
632                 }
633
634         else if (!strcasecmp(action, "ungoto")) {
635                 ungoto();
636                 }
637
638         else if (!strcasecmp(action, "dotgoto")) {
639                 slrp_highest();
640                 gotoroom(bstr("room"), 1);
641                 }
642
643         else if (!strcasecmp(action, "termquit")) {
644                 do_logout();
645                 }
646
647         else if (!strcasecmp(action, "readnew")) {
648                 readloop("readnew");
649                 }
650
651         else if (!strcasecmp(action, "readold")) {
652                 readloop("readold");
653                 }
654
655         else if (!strcasecmp(action, "readfwd")) {
656                 readloop("readfwd");
657                 }
658
659         else if (!strcasecmp(action, "display_enter")) {
660                 display_enter();
661                 }
662
663         else if (!strcasecmp(action, "post")) {
664                 post_message();
665                 }
666
667         else if (!strcasecmp(action, "confirm_delete_msg")) {
668                 confirm_delete_msg();
669                 }
670
671         else if (!strcasecmp(action, "delete_msg")) {
672                 delete_msg();
673                 }
674
675         else if (!strcasecmp(action, "confirm_move_msg")) {
676                 confirm_move_msg();
677                 }
678
679         else if (!strcasecmp(action, "move_msg")) {
680                 move_msg();
681                 }
682
683         else if (!strcasecmp(action, "userlist")) {
684                 userlist();
685                 }
686
687         else if (!strcasecmp(action, "showuser")) {
688                 showuser();
689                 }
690
691         else if (!strcasecmp(action, "display_page")) {
692                 display_page();
693                 }
694
695         else if (!strcasecmp(action, "page_user")) {
696                 page_user();
697                 }
698
699         else if (!strcasecmp(action, "chat")) {
700                 do_chat();
701                 }
702
703         else if (!strcasecmp(action, "display_private")) {
704                 display_private("", 0);
705                 }
706
707         else if (!strcasecmp(action, "goto_private")) {
708                 goto_private();
709                 }
710
711         else if (!strcasecmp(action, "zapped_list")) {
712                 zapped_list();
713                 }
714
715         else if (!strcasecmp(action, "display_zap")) {
716                 display_zap();
717                 }
718
719         else if (!strcasecmp(action, "zap")) {
720                 zap();
721                 }
722
723         else if (!strcasecmp(action, "display_entroom")) {
724                 display_entroom();
725                 }
726
727         else if (!strcasecmp(action, "entroom")) {
728                 entroom();
729                 }
730
731         else if (!strcasecmp(action, "display_editroom")) {
732                 display_editroom();
733                 }
734
735         else if (!strcasecmp(action, "editroom")) {
736                 editroom();
737                 }
738
739         else if (!strcasecmp(action, "display_editinfo")) {
740                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
741                 }
742
743         else if (!strcasecmp(action, "editinfo")) {
744                 save_edit("Room info", "EINF 1", 1);
745                 }
746
747         else if (!strcasecmp(action, "display_editbio")) {
748                 sprintf(buf, "RBIO %s", wc_username);
749                 display_edit("Your bio", "NOOP", buf, "editbio");
750                 }
751
752         else if (!strcasecmp(action, "editbio")) {
753                 save_edit("Your bio", "EBIO", 0);
754                 }
755
756         else if (!strcasecmp(action, "confirm_delete_room")) {
757                 confirm_delete_room();
758                 }
759
760         else if (!strcasecmp(action, "delete_room")) {
761                 delete_room();
762                 }
763
764         else if (!strcasecmp(action, "validate")) {
765                 validate();
766                 }
767
768         else if (!strcasecmp(action, "display_editpic")) {
769                 display_graphics_upload("your photo",
770                                         "UIMG 0|_userpic_",
771                                         "/editpic");
772                 }
773
774         else if (!strcasecmp(action, "editpic")) {
775                 do_graphics_upload("UIMG 1|_userpic_");
776                 }
777
778         else if (!strcasecmp(action, "display_editroompic")) {
779                 display_graphics_upload("the graphic for this room",
780                                         "UIMG 0|_roompic_",
781                                         "/editroompic");
782                 }
783
784         else if (!strcasecmp(action, "editroompic")) {
785                 do_graphics_upload("UIMG 1|_roompic_");
786                 }
787
788         else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
789                 select_floor_to_edit_pic();
790                 }
791
792         else if (!strcasecmp(action, "display_editfloorpic")) {
793                 sprintf(buf, "UIMG 0|_floorpic_|%s",
794                         bstr("which_floor"));
795                 display_graphics_upload("the graphic for this floor",
796                                         buf,
797                                         "/editfloorpic");
798                 }
799
800         else if (!strcasecmp(action, "editfloorpic")) {
801                 sprintf(buf, "UIMG 1|_floorpic_|%s",
802                         bstr("which_floor"));
803                 do_graphics_upload(buf);
804                 }
805
806         /* When all else fails... */
807         else {
808                 printf("HTTP/1.0 200 OK\n");
809                 output_headers(1);
810         
811                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
812                 wprintf("You're in session %d<HR>\n", wc_session);
813                 wprintf("Command: <BR><PRE>\n");
814                 escputs(cmd);
815                 wprintf("</PRE><HR>\n");
816                 wprintf("Variables: <BR><PRE>\n");
817                 dump_vars();
818                 wprintf("</PRE><HR>\n");
819                 wprintf("</BODY></HTML>\n");
820                 wDumpContent();
821                 }
822
823         fflush(stdout);
824         if (content != NULL) {
825                 free(content);
826                 content = NULL;
827                 }
828         free_urls();
829         if (upload_length > 0) {
830                 free(upload);
831                 upload_length = 0;
832                 }
833         }
834
835 int main(int argc, char *argv[]) {
836
837         if (argc < 2 || argc > 4) {
838                 fprintf(stderr,
839                         "webcit: usage: webcit <session_id> [host [port]]\n");
840                 return 1;
841                 }
842
843         wc_session = atoi(argv[1]);
844
845         if (argc > 2) {
846                 defaulthost = argv[2];
847                 if (argc > 3)
848                         defaultport = argv[3];
849                 }
850
851         strcpy(wc_host, "");
852         strcpy(wc_port, "");
853         strcpy(wc_username, "");
854         strcpy(wc_password, "");
855         strcpy(wc_roomname, "");
856
857         while (1) {
858                 session_loop();
859                 }
860         }