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