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