]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* Merged in Nick's bit of JavaScript to handle key commands.
[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("</TITLE></HEAD>");
385                 if (ExpressMessages != NULL) {
386                         wprintf("<SCRIPT language=\"javascript\">\n");
387                         wprintf("function ExpressMessage() {\n");
388                         wprintf(" alert(\"");
389                         escputs(ExpressMessages);
390                         wprintf("\")\n");
391                         wprintf(" }\n </SCRIPT>\n");
392                 }
393
394                 /* Print JavaScript key capture
395                  *  for key-based navigation
396                  */
397                 wprintf(
398                 "<SCRIPT LANGUAGE=\"JavaScript\">\n"
399                 "function nav(e) {\n"
400                 "var keyChar = String.fromCharCode(e.which);\n"
401                 "switch(keyChar.toLowerCase()) {\n"
402                 );
403                 wprintf(
404                 "case \"g\": document.location=\"/gotonext\";\n"
405                 "break;\n"
406                 "case \"e\": document.location=\"/display_enter\";\n"
407                 "break;\n"
408                 "case \"k\": document.location=\"/knrooms\";\n"
409                 "break;\n"
410                 "case \"s\": document.location=\"/skip\";\n"
411                 "break;\n"
412                 );
413                 wprintf(
414                 "case \"u\": document.location=\"/ungoto\";\n"
415                 "break;\n"
416                 "case \"n\": document.location=\"/readnew\";\n"
417                 "break;\n"
418                 "case \"w\": document.location=\"/whobbs\";\n"
419                 "break;\n"
420                 );
421                 wprintf(
422                 "case \"p\": document.location=\"/display_page\";\n"
423                 "break;\n"
424                 "case \"c\": document.location=\"/chat\";\n"
425                 "break;\n"
426                 "case \"z\": document.location=\"/display_zap\";\n"
427                 "break;\n"
428                 );
429                 wprintf(
430                 "}\n"
431                 "}\n"
432                 "document.captureEvents(Event.KEYPRESS);\n"
433                 "document.onKeyPress = nav;\n"
434                 "</SCRIPT>\n"
435                 );
436
437                 /* end JavaScript-based key navigation */
438
439
440                 wprintf("<BODY ");
441                 if (ExpressMessages != NULL) {
442                         wprintf("onload=\"ExpressMessage()\" ");
443                         free(ExpressMessages);
444                         ExpressMessages = NULL;
445                 }
446                 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
447         
448         
449         if (print_standard_html_head == 1) {
450                 wprintf("<A NAME=\"TheTop\"></A>"
451                         "<TABLE border=0 width=100%>"
452                         "<TR VALIGN=TOP><TD>");
453
454                 display_menubar(0);
455
456                 wprintf("</TD><TD VALIGN=TOP>"
457                         "<TABLE border=0 width=100%><TR VALIGN=TOP>"
458                         "<TD>\n");
459
460                 embed_room_banner(NULL);
461
462                 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
463                 
464                 fake_frames = 1;
465                 }
466         }
467 }
468
469
470
471 void ExpressMessageCat(char *buf) {
472         if (ExpressMessages == NULL) {
473                 ExpressMessages = malloc(strlen(buf) + 4);
474                 strcpy(ExpressMessages, "");
475         } else {
476                 ExpressMessages = realloc(ExpressMessages,
477                         (strlen(ExpressMessages) + strlen(buf) + 4));
478         }
479         strcat(ExpressMessages, buf);
480         strcat(ExpressMessages, "\\n");
481 }
482
483
484 void check_for_express_messages()
485 {
486         char buf[256];
487
488         serv_puts("PEXP");
489         serv_gets(buf);
490         if (buf[0] == '1') {
491                 while (serv_gets(buf), strcmp(buf, "000")) {
492                         ExpressMessageCat(buf);
493                 }
494         }
495 }
496
497
498
499
500 void output_static(char *what)
501 {
502         char buf[256];
503         FILE *fp;
504         struct stat statbuf;
505         off_t bytes;
506
507         sprintf(buf, "static/%s", what);
508         fp = fopen(buf, "rb");
509         if (fp == NULL) {
510                 printf("HTTP/1.0 404 %s\n", strerror(errno));
511                 output_headers(0);
512                 printf("Content-Type: text/plain\n");
513                 sprintf(buf, "%s: %s\n", what, strerror(errno));
514                 printf("Content-length: %d\n", strlen(buf));
515                 printf("\n");
516                 fwrite(buf, strlen(buf), 1, stdout);
517         } else {
518                 printf("HTTP/1.0 200 OK\n");
519                 output_headers(0);
520
521                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
522                         printf("Content-type: image/gif\n");
523                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
524                         printf("Content-type: text/plain\n");
525                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
526                         printf("Content-type: image/jpeg\n");
527                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
528                         printf("Content-type: text/html\n");
529                 else
530                         printf("Content-type: application/octet-stream\n");
531
532                 fstat(fileno(fp), &statbuf);
533                 bytes = statbuf.st_size;
534                 printf("Content-length: %ld\n", (long) bytes);
535                 printf("\n");
536                 while (bytes--) {
537                         putc(getc(fp), stdout);
538                 }
539                 fflush(stdout);
540                 fclose(fp);
541         }
542 }
543
544 void output_image()
545 {
546         char buf[256];
547         char xferbuf[4096];
548         off_t bytes;
549         off_t thisblock;
550         off_t accomplished = 0L;
551
552
553         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
554         serv_gets(buf);
555         if (buf[0] == '2') {
556                 bytes = extract_long(&buf[4], 0);
557                 printf("HTTP/1.0 200 OK\n");
558                 output_headers(0);
559                 printf("Content-type: image/gif\n");
560                 printf("Content-length: %ld\n", (long) bytes);
561                 printf("\n");
562
563                 while (bytes > (off_t) 0) {
564                         thisblock = (off_t) sizeof(xferbuf);
565                         if (thisblock > bytes)
566                                 thisblock = bytes;
567                         serv_printf("READ %ld|%ld", accomplished, thisblock);
568                         serv_gets(buf);
569                         if (buf[0] == '6')
570                                 thisblock = extract_long(&buf[4], 0);
571                         serv_read(xferbuf, (int) thisblock);
572                         fwrite(xferbuf, thisblock, 1, stdout);
573                         bytes = bytes - thisblock;
574                         accomplished = accomplished + thisblock;
575                 }
576                 fflush(stdout);
577                 serv_puts("CLOS");
578                 serv_gets(buf);
579         } else {
580                 printf("HTTP/1.0 404 %s\n", strerror(errno));
581                 output_headers(0);
582                 printf("Content-Type: text/plain\n");
583                 sprintf(buf, "Error retrieving image\n");
584                 printf("Content-length: %d\n", strlen(buf));
585                 printf("\n");
586                 fwrite(buf, strlen(buf), 1, stdout);
587         }
588
589 }
590
591
592 /*
593  * Convenience functions to display a page containing only a string
594  */
595 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
596 {
597         printf("HTTP/1.0 200 OK\n");
598         output_headers(1);
599         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
600         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
601         wprintf("<B>%s</B>\n", titlebarmsg);
602         wprintf("</FONT></TD></TR></TABLE><BR>\n");
603         escputs(messagetext);
604
605         wprintf("<HR>\n");
606         embed_main_menu();
607         wDumpContent(1);
608 }
609
610 void display_error(char *errormessage)
611 {
612         convenience_page("770000", "Error", errormessage);
613 }
614
615 void display_success(char *successmessage)
616 {
617         convenience_page("007700", "OK", successmessage);
618 }
619
620
621
622 void extract_action(char *actbuf, char *cmdbuf)
623 {
624         int i;
625
626         strcpy(actbuf, cmdbuf);
627         if (!strncasecmp(actbuf, "GET /", 5))
628                 strcpy(actbuf, &actbuf[5]);
629         if (!strncasecmp(actbuf, "PUT /", 5))
630                 strcpy(actbuf, &actbuf[5]);
631         if (!strncasecmp(actbuf, "POST /", 6))
632                 strcpy(actbuf, &actbuf[6]);
633
634         for (i = 0; i < strlen(actbuf); ++i) {
635                 if (actbuf[i] == ' ') {
636                         actbuf[i] = 0;
637                         i = 0;
638                 }
639                 if (actbuf[i] == '/') {
640                         actbuf[i] = 0;
641                         i = 0;
642                 }
643                 if (actbuf[i] == '?') {
644                         actbuf[i] = 0;
645                         i = 0;
646                 }
647                 if (actbuf[i] == '&') {
648                         actbuf[i] = 0;
649                         i = 0;
650                 }
651         }
652 }
653
654
655 void upload_handler(char *name, char *filename, char *encoding,
656                     void *content, char *cbtype, size_t length)
657 {
658
659         fprintf(stderr, "UPLOAD HANDLER CALLED\n");
660         fprintf(stderr, "    name = %s\n", name);
661         fprintf(stderr, "filename = %s\n", filename);
662         fprintf(stderr, "encoding = %s\n", encoding);
663         fprintf(stderr, "    type = %s\n", cbtype);
664         fprintf(stderr, "  length = %d\n", length);
665
666         if (strlen(name) > 0) {
667                 upload = malloc(length);
668                 if (upload != NULL) {
669                         upload_length = length;
670                         memcpy(upload, content, length);
671                 }
672         }
673 }
674
675
676 void session_loop(char *browser_host, char *user_agent)
677 {
678         char cmd[256];
679         char action[256];
680         char buf[256];
681         int a, b;
682         int ContentLength = 0;
683         char ContentType[512];
684         char *content;
685
686         /* We stuff these with the values coming from the client cookies,
687          * so we can use them to reconnect a timed out session if we have to.
688          */
689         char c_host[256];
690         char c_port[256];
691         char c_username[256];
692         char c_password[256];
693         char c_roomname[256];
694         char cookie[256];
695
696         strcpy(c_host, defaulthost);
697         strcpy(c_port, defaultport);
698         strcpy(c_username, "");
699         strcpy(c_password, "");
700         strcpy(c_roomname, "");
701
702         upload_length = 0;
703         upload = NULL;
704
705         if (getz(cmd) == NULL)
706                 return;
707         extract_action(action, cmd);
708
709         do {
710                 if (getz(buf) == NULL)
711                         return;
712
713                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
714                         strcpy(cookie, &buf[15]);
715                         cookie_to_stuff(cookie, NULL,
716                                       c_username, c_password, c_roomname);
717                 }
718                 if (!strncasecmp(buf, "Content-length: ", 16)) {
719                         ContentLength = atoi(&buf[16]);
720                 }
721                 if (!strncasecmp(buf, "Content-type: ", 14)) {
722                         strcpy(ContentType, &buf[14]);
723                 }
724         } while (strlen(buf) > 0);
725
726         ++TransactionCount;
727
728         if (ContentLength > 0) {
729                 content = malloc(ContentLength + 1);
730                 fread(content, ContentLength, 1, stdin);
731
732                 content[ContentLength] = 0;
733
734                 if (!strncasecmp(ContentType,
735                               "application/x-www-form-urlencoded", 33)) {
736                         addurls(content);
737                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
738                         mime_parser(content, ContentLength, ContentType,
739                                     *upload_handler);
740                 }
741         } else {
742                 content = NULL;
743         }
744
745         /* If there are variables in the URL, we must grab them now */
746         for (a = 0; a < strlen(cmd); ++a)
747                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
748                         for (b = a; b < strlen(cmd); ++b)
749                                 if (isspace(cmd[b]))
750                                         cmd[b] = 0;
751                         addurls(&cmd[a + 1]);
752                         cmd[a] = 0;
753                 }
754         /*
755          * If we're not connected to a Citadel server, try to hook up the
756          * connection now.  Preference is given to the host and port specified
757          * by browser cookies, if cookies have been supplied.
758          */
759         if (!connected) {
760                 if (strlen(bstr("host")) > 0)
761                         strcpy(c_host, bstr("host"));
762                 if (strlen(bstr("port")) > 0)
763                         strcpy(c_port, bstr("port"));
764                 serv_sock = connectsock(c_host, c_port, "tcp");
765                 if (serv_sock < 0) {
766                         do_logout();
767                 }
768
769                 connected = 1;
770                 serv_gets(buf); /* get the server welcome message */
771                 get_serv_info(browser_host, user_agent);
772         }
773         check_for_express_messages();
774
775         /*
776          * If we're not logged in, but we have username and password cookies
777          * supplied by the browser, try using them to log in.
778          */
779         if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
780                 serv_printf("USER %s", c_username);
781                 serv_gets(buf);
782                 if (buf[0] == '3') {
783                         serv_printf("PASS %s", c_password);
784                         serv_gets(buf);
785                         if (buf[0] == '2') {
786                                 become_logged_in(c_username, c_password, buf);
787                         }
788                 }
789         }
790         /*
791          * If we don't have a current room, but a cookie specifying the
792          * current room is supplied, make an effort to go there.
793          */
794         if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
795                 serv_printf("GOTO %s", c_roomname);
796                 serv_gets(buf);
797                 if (buf[0] == '2') {
798                         strcpy(wc_roomname, c_roomname);
799                 }
800         }
801         if (!strcasecmp(action, "static")) {
802                 strcpy(buf, &cmd[12]);
803                 for (a = 0; a < strlen(buf); ++a)
804                         if (isspace(buf[a]))
805                                 buf[a] = 0;
806                 output_static(buf);
807         } else if (!strcasecmp(action, "image")) {
808                 output_image();
809         } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
810                 do_login();
811         } else if (!logged_in) {
812                 display_login(NULL);
813         }
814         /* Various commands... */
815
816         else if (!strcasecmp(action, "do_welcome")) {
817                 do_welcome();
818         } else if (!strcasecmp(action, "display_main_menu")) {
819                 display_main_menu();
820         } else if (!strcasecmp(action, "advanced")) {
821                 display_advanced_menu();
822         } else if (!strcasecmp(action, "whobbs")) {
823                 whobbs();
824         } else if (!strcasecmp(action, "knrooms")) {
825                 list_all_rooms_by_floor();
826         } else if (!strcasecmp(action, "gotonext")) {
827                 slrp_highest();
828                 gotonext();
829         } else if (!strcasecmp(action, "skip")) {
830                 gotonext();
831         } else if (!strcasecmp(action, "ungoto")) {
832                 ungoto();
833         } else if (!strcasecmp(action, "dotgoto")) {
834                 slrp_highest();
835                 smart_goto(bstr("room"));
836         } else if (!strcasecmp(action, "termquit")) {
837                 do_logout();
838         } else if (!strcasecmp(action, "readnew")) {
839                 readloop("readnew");
840         } else if (!strcasecmp(action, "readold")) {
841                 readloop("readold");
842         } else if (!strcasecmp(action, "readfwd")) {
843                 readloop("readfwd");
844         } else if (!strcasecmp(action, "display_enter")) {
845                 display_enter();
846         } else if (!strcasecmp(action, "post")) {
847                 post_message();
848         } else if (!strcasecmp(action, "confirm_delete_msg")) {
849                 confirm_delete_msg();
850         } else if (!strcasecmp(action, "delete_msg")) {
851                 delete_msg();
852         } else if (!strcasecmp(action, "confirm_move_msg")) {
853                 confirm_move_msg();
854         } else if (!strcasecmp(action, "move_msg")) {
855                 move_msg();
856         } else if (!strcasecmp(action, "userlist")) {
857                 userlist();
858         } else if (!strcasecmp(action, "showuser")) {
859                 showuser();
860         } else if (!strcasecmp(action, "display_page")) {
861                 display_page();
862         } else if (!strcasecmp(action, "page_user")) {
863                 page_user();
864         } else if (!strcasecmp(action, "chat")) {
865                 do_chat();
866         } else if (!strcasecmp(action, "display_private")) {
867                 display_private("", 0);
868         } else if (!strcasecmp(action, "goto_private")) {
869                 goto_private();
870         } else if (!strcasecmp(action, "zapped_list")) {
871                 zapped_list();
872         } else if (!strcasecmp(action, "display_zap")) {
873                 display_zap();
874         } else if (!strcasecmp(action, "zap")) {
875                 zap();
876         } else if (!strcasecmp(action, "display_entroom")) {
877                 display_entroom();
878         } else if (!strcasecmp(action, "entroom")) {
879                 entroom();
880         } else if (!strcasecmp(action, "display_editroom")) {
881                 display_editroom();
882         } else if (!strcasecmp(action, "editroom")) {
883                 editroom();
884         } else if (!strcasecmp(action, "display_editinfo")) {
885                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
886         } else if (!strcasecmp(action, "editinfo")) {
887                 save_edit("Room info", "EINF 1", 1);
888         } else if (!strcasecmp(action, "display_editbio")) {
889                 sprintf(buf, "RBIO %s", wc_username);
890                 display_edit("Your bio", "NOOP", buf, "editbio");
891         } else if (!strcasecmp(action, "editbio")) {
892                 save_edit("Your bio", "EBIO", 0);
893         } else if (!strcasecmp(action, "confirm_delete_room")) {
894                 confirm_delete_room();
895         } else if (!strcasecmp(action, "delete_room")) {
896                 delete_room();
897         } else if (!strcasecmp(action, "validate")) {
898                 validate();
899         } else if (!strcasecmp(action, "display_editpic")) {
900                 display_graphics_upload("your photo",
901                                         "UIMG 0|_userpic_",
902                                         "/editpic");
903         } else if (!strcasecmp(action, "editpic")) {
904                 do_graphics_upload("UIMG 1|_userpic_");
905         } else if (!strcasecmp(action, "display_editroompic")) {
906                 display_graphics_upload("the graphic for this room",
907                                         "UIMG 0|_roompic_",
908                                         "/editroompic");
909         } else if (!strcasecmp(action, "editroompic")) {
910                 do_graphics_upload("UIMG 1|_roompic_");
911         } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
912                 select_floor_to_edit_pic();
913         } else if (!strcasecmp(action, "display_editfloorpic")) {
914                 sprintf(buf, "UIMG 0|_floorpic_|%s",
915                         bstr("which_floor"));
916                 display_graphics_upload("the graphic for this floor",
917                                         buf,
918                                         "/editfloorpic");
919         } else if (!strcasecmp(action, "editfloorpic")) {
920                 sprintf(buf, "UIMG 1|_floorpic_|%s",
921                         bstr("which_floor"));
922                 do_graphics_upload(buf);
923         } else if (!strcasecmp(action, "display_reg")) {
924                 display_reg(0);
925         } else if (!strcasecmp(action, "register")) {
926                 register_user();
927         } else if (!strcasecmp(action, "display_changepw")) {
928                 display_changepw();
929         } else if (!strcasecmp(action, "changepw")) {
930                 changepw();
931         } else if (!strcasecmp(action, "display_edit_node")) {
932                 display_edit_node();
933         } else if (!strcasecmp(action, "display_netconf")) {
934                 display_netconf();
935         } else if (!strcasecmp(action, "display_confirm_unshare")) {
936                 display_confirm_unshare();
937         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
938                 display_confirm_delete_node();
939         } else if (!strcasecmp(action, "delete_node")) {
940                 delete_node();
941         } else if (!strcasecmp(action, "unshare")) {
942                 unshare();
943         } else if (!strcasecmp(action, "display_add_node")) {
944                 display_add_node();
945         } else if (!strcasecmp(action, "add_node")) {
946                 add_node();
947         } else if (!strcasecmp(action, "display_share")) {
948                 display_share();
949         } else if (!strcasecmp(action, "share")) {
950                 share();
951         } else if (!strcasecmp(action, "terminate_session")) {
952                 slrp_highest();
953                 terminate_session();
954         } else if (!strcasecmp(action, "edit_me")) {
955                 edit_me();
956         } else if (!strcasecmp(action, "display_siteconfig")) {
957                 display_siteconfig();
958         } else if (!strcasecmp(action, "siteconfig")) {
959                 siteconfig();
960         } else if (!strcasecmp(action, "display_generic")) {
961                 display_generic();
962         } else if (!strcasecmp(action, "do_generic")) {
963                 do_generic();
964         } else if (!strcasecmp(action, "display_menubar")) {
965                 display_menubar(1);
966         } else if (!strcasecmp(action, "diagnostics")) {
967                 printf("HTTP/1.0 200 OK\n");
968                 output_headers(1);
969
970                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
971                 wprintf("You're in session %d<HR>\n", wc_session);
972                 wprintf("Command: <BR><PRE>\n");
973                 escputs(cmd);
974                 wprintf("</PRE><HR>\n");
975                 wprintf("Variables: <BR><PRE>\n");
976                 dump_vars();
977                 wprintf("</PRE><HR>\n");
978                 wDumpContent(1);
979         }
980         /* When all else fais, display the main menu. */
981         else {
982                 display_main_menu();
983         }
984
985         fflush(stdout);
986         if (content != NULL) {
987                 free(content);
988                 content = NULL;
989         }
990         free_urls();
991         if (upload_length > 0) {
992                 free(upload);
993                 upload_length = 0;
994         }
995 }
996
997 int main(int argc, char *argv[])
998 {
999
1000         char browser[256];
1001         int bd;
1002
1003         if (argc != 6) {
1004                 fprintf(stderr,
1005                         "webcit: usage error (argc must be 6, not %d)\n",
1006                         argc);
1007                 return 1;
1008         }
1009
1010         wc_session = atoi(argv[1]);
1011         defaulthost = argv[2];
1012         defaultport = argv[3];
1013
1014         strcpy(wc_username, "");
1015         strcpy(wc_password, "");
1016         strcpy(wc_roomname, "");
1017
1018         /* Clear out serv_info and temporarily set the value of serv_humannode
1019          * to a default value, because it'll be used in HTML page titles
1020          */
1021         memset(&serv_info, 0, sizeof(serv_info));
1022         strcpy(serv_info.serv_humannode, "WebCit");
1023
1024         strcpy(browser, argv[5]);
1025         bd = browser_braindamage_check(browser);
1026
1027         while (1) {
1028                 session_loop(argv[4], browser);
1029         }
1030 }