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