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