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