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