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