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