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