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