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