* All OS-level includes are now included from webcit.h instead of from
[citadel.git] / webcit / webcit.c
1 /*
2  * $Id$
3  *
4  * This is the main transaction loop of the web service.  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  */
9
10 #include "webcit.h"
11 #include "groupdav.h"
12 #include "webserver.h"
13 #include "mime_parser.h"
14
15 /*
16  * String to unset the cookie.
17  * Any date "in the past" will work, so I chose my birthday, right down to
18  * the exact minute.  :)
19  */
20 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
21
22 void unescape_input(char *buf)
23 {
24         int a, b;
25         char hex[3];
26
27         while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
28                 buf[strlen(buf) - 1] = 0;
29
30         for (a = 0; a < strlen(buf); ++a) {
31                 if (buf[a] == '+')
32                         buf[a] = ' ';
33                 if (buf[a] == '%') {
34                         hex[0] = buf[a + 1];
35                         hex[1] = buf[a + 2];
36                         hex[2] = 0;
37                         b = 0;
38                         sscanf(hex, "%02x", &b);
39                         buf[a] = (char) b;
40                         strcpy(&buf[a + 1], &buf[a + 3]);
41                 }
42         }
43
44 }
45
46
47 void addurls(char *url)
48 {
49         char *up, *ptr;
50         char buf[SIZ];
51         int a, b;
52         struct urlcontent *u;
53
54         up = url;
55         while (strlen(up) > 0) {
56
57                 /* locate the = sign */
58                 safestrncpy(buf, up, sizeof buf);
59                 b = (-1);
60                 for (a = 255; a >= 0; --a)
61                         if (buf[a] == '=')
62                                 b = a;
63                 if (b < 0)
64                         return;
65                 buf[b] = 0;
66
67                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
68                 u->next = WC->urlstrings;
69                 WC->urlstrings = u;
70                 safestrncpy(u->url_key, buf, sizeof u->url_key);
71
72                 /* now chop that part off */
73                 for (a = 0; a <= b; ++a)
74                         ++up;
75
76                 /* locate "&" and "?" delimiters */
77                 ptr = up;
78                 b = strlen(up);
79                 for (a = 0; a < strlen(up); ++a) {
80                         if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
81                                 b = a;
82                                 break;
83                         }
84                         ++ptr;
85                 }
86                 ptr = up;
87                 for (a = 0; a < b; ++a)
88                         ++ptr;
89                 strcpy(ptr, "");
90
91                 u->url_data = malloc(strlen(up) + 2);
92                 safestrncpy(u->url_data, up, strlen(up) + 1);
93                 u->url_data[b] = 0;
94                 unescape_input(u->url_data);
95                 up = ptr;
96                 ++up;
97         }
98 }
99
100 void free_urls(void)
101 {
102         struct urlcontent *u;
103
104         while (WC->urlstrings != NULL) {
105                 free(WC->urlstrings->url_data);
106                 u = WC->urlstrings->next;
107                 free(WC->urlstrings);
108                 WC->urlstrings = u;
109         }
110 }
111
112 /*
113  * Diagnostic function to display the contents of all variables
114  */
115 void dump_vars(void)
116 {
117         struct urlcontent *u;
118
119         for (u = WC->urlstrings; u != NULL; u = u->next) {
120                 wprintf("%38s = %s\n", u->url_key, u->url_data);
121         }
122 }
123
124 char *bstr(char *key)
125 {
126         struct urlcontent *u;
127
128         for (u = WC->urlstrings; u != NULL; u = u->next) {
129                 if (!strcasecmp(u->url_key, key))
130                         return (u->url_data);
131         }
132         return ("");
133 }
134
135
136 void wprintf(const char *format,...)
137 {
138         va_list arg_ptr;
139         char wbuf[4096];
140
141         va_start(arg_ptr, format);
142         vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
143         va_end(arg_ptr);
144
145         client_write(wbuf, strlen(wbuf));
146 }
147
148
149 /*
150  * wDumpContent() wraps up an HTTP session, closes tags, etc.
151  *
152  * print_standard_html_footer should be set to 0 to transmit only, 1 to
153  * append the main menu and closing tags, or 2 to
154  * append the closing tags only.
155  */
156 void wDumpContent(int print_standard_html_footer)
157 {
158         if (print_standard_html_footer) {
159                 wprintf("</div>\n");    /* end of "text" div */
160                 do_template("trailing");
161         }
162
163         /* If we've been saving it all up for one big output burst,
164          * go ahead and do that now.
165          */
166         end_burst();
167 }
168
169
170 /*
171  * Copy a string, escaping characters which have meaning in HTML.  If
172  * nbsp is nonzero, spaces are converted to non-breaking spaces.
173  */
174 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
175 {
176         int a;
177         strcpy(target, "");
178
179         for (a = 0; a < strlen(strbuf); ++a) {
180                 if (strbuf[a] == '<')
181                         strcat(target, "&lt;");
182                 else if (strbuf[a] == '>')
183                         strcat(target, "&gt;");
184                 else if (strbuf[a] == '&')
185                         strcat(target, "&amp;");
186                 else if (strbuf[a] == '\"')
187                         strcat(target, "&quot;");
188                 else if (strbuf[a] == '\'') 
189                         strcat(target, "&#39;");
190                 else if (strbuf[a] == LB)
191                         strcat(target, "<");
192                 else if (strbuf[a] == RB)
193                         strcat(target, ">");
194                 else if (strbuf[a] == QU)
195                         strcat(target, "\"");
196                 else if ((strbuf[a] == 32) && (nbsp == 1))
197                         strcat(target, "&nbsp;");
198                 else if ((strbuf[a] == '\n') && (nolinebreaks))
199                         strcat(target, "");     /* nothing */
200                 else if ((strbuf[a] == '\r') && (nolinebreaks))
201                         strcat(target, "");     /* nothing */
202                 else
203                         strncat(target, &strbuf[a], 1);
204         }
205 }
206
207 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
208 {
209         char *buf;
210
211         if (strbuf == NULL) return;
212         buf = malloc( (3 * strlen(strbuf)) + SIZ );
213         stresc(buf, strbuf, nbsp, nolinebreaks);
214         wprintf("%s", buf);
215         free(buf);
216 }
217
218 void escputs(char *strbuf)
219 {
220         escputs1(strbuf, 0, 0);
221 }
222
223 /*
224  * Escape a string for feeding out as a URL.
225  * Returns a pointer to a buffer that must be freed by the caller!
226  */
227 void urlesc(char *outbuf, char *strbuf)
228 {
229         int a, b, c;
230         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
231
232         strcpy(outbuf, "");
233
234         for (a = 0; a < strlen(strbuf); ++a) {
235                 c = 0;
236                 for (b = 0; b < strlen(ec); ++b) {
237                         if (strbuf[a] == ec[b])
238                                 c = 1;
239                 }
240                 b = strlen(outbuf);
241                 if (c == 1)
242                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
243                 else
244                         sprintf(&outbuf[b], "%c", strbuf[a]);
245         }
246 }
247
248 void urlescputs(char *strbuf)
249 {
250         char outbuf[SIZ];
251         
252         urlesc(outbuf, strbuf);
253         wprintf("%s", outbuf);
254 }
255
256
257 /*
258  * Copy a string, escaping characters for JavaScript strings.
259  */
260 void jsesc(char *target, char *strbuf)
261 {
262         int a;
263         strcpy(target, "");
264
265         for (a = 0; a < strlen(strbuf); ++a) {
266                 if (strbuf[a] == '<')
267                         strcat(target, "[");
268                 else if (strbuf[a] == '>')
269                         strcat(target, "]");
270                 else if (strbuf[a] == '\"')
271                         strcat(target, "&quot;");
272                 else if (strbuf[a] == '&')
273                         strcat(target, "&amp;;");
274                 else if (strbuf[a] == '\'') 
275                         strcat(target, "\\'");
276                 else {
277                         strncat(target, &strbuf[a], 1);
278                 }
279         }
280 }
281
282 void jsescputs(char *strbuf)
283 {
284         char outbuf[SIZ];
285         
286         jsesc(outbuf, strbuf);
287         wprintf("%s", outbuf);
288 }
289
290 /*
291  * Copy a string, escaping characters for message text hold
292  */
293 void msgesc(char *target, char *strbuf)
294 {
295         int a;
296         strcpy(target, "");
297
298         for (a = 0; a < strlen(strbuf); ++a) {
299                 if (strbuf[a] == '\'') 
300                         strcat(target, "\\'");
301                 else if (strbuf[a] == '\n')
302                         strcat(target, " ");
303                 else if (strbuf[a] == '\r')
304                         strcat(target, " ");
305                 else {
306                         strncat(target, &strbuf[a], 1);
307                 }
308         }
309 }
310
311 void msgescputs(char *strbuf) {
312         char *outbuf;
313
314         if (strbuf == NULL) return;
315         outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
316         msgesc(outbuf, strbuf);
317         wprintf("%s", outbuf);
318         free(outbuf);
319 }
320
321
322
323
324 /*
325  * Output all that important stuff that the browser will want to see
326  */
327 void output_headers(    int do_httpheaders,     /* 1 = output HTTP headers                          */
328                         int do_htmlhead,        /* 1 = output HTML <head> section and <body> opener */
329
330                         int do_room_banner,     /* 0=no, 1=yes,                                     */
331                                                 /* 2 = I'm going to embed my own, so don't open the */
332                                                 /*     <div id="content"> either.                   */
333
334                         int unset_cookies,      /* 1 = session is terminating, so unset the cookies */
335                         int refresh30,          /* 1 = automatically refresh page every 30 seconds  */
336                         int suppress_check,     /* 1 = suppress check for instant messages          */
337                         int cache               /* 1 = allow browser to cache this page             */
338 ) {
339         char cookie[SIZ];
340         char httpnow[SIZ];
341
342         wprintf("HTTP/1.0 200 OK\n");
343         httpdate(httpnow, time(NULL));
344
345         if (do_httpheaders) {
346                 wprintf("Content-type: text/html; charset=utf-8\r\n"
347                         "Server: %s / %s\n"
348                         "Connection: close\r\n",
349                         SERVER, serv_info.serv_software
350                 );
351         }
352
353         if (cache) {
354                 wprintf("Pragma: public\r\n"
355                         "Cache-Control: max-age=3600, must-revalidate\r\n"
356                         "Last-modified: %s\r\n",
357                         httpnow
358                 );
359         }
360         else {
361                 wprintf("Pragma: no-cache\r\n"
362                         "Cache-Control: no-store\r\n"
363                 );
364         }
365
366         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
367                         WC->wc_password, WC->wc_roomname);
368
369         if (unset_cookies) {
370                 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
371         } else {
372                 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
373                 if (server_cookie != NULL) {
374                         wprintf("%s\n", server_cookie);
375                 }
376         }
377
378         if (do_htmlhead) {
379                 /* wprintf("\n"); */
380                 begin_burst();
381
382                 if (refresh30) {
383                         svprintf("REFRESHTAG", WCS_STRING, "%s",
384                                 "<meta http-equiv=\"refresh\" content=\"30\" />\n");
385                 }
386                 else {
387                         svprintf("REFRESHTAG", WCS_STRING, "%s",
388                                 "<meta http-equiv=\"refresh\" content=\"500363689;\" />\n");
389                 }
390
391                 do_template("head");
392         }
393
394         /* ICONBAR */
395         if (do_htmlhead) {
396
397                 if (WC->HaveInstantMessages) {
398                         wprintf("<div id=\"page_popup\">\n");
399                         page_popup();
400                         wprintf("</div>\n");
401                 }
402                 if (strlen(WC->ImportantMessage) > 0) {
403                         wprintf("<div id=\"important_message\">\n");
404                         wprintf("<SPAN CLASS=\"imsg\">"
405                                 "%s</SPAN><br />\n", WC->ImportantMessage);
406                         wprintf("</div>\n");
407                         wprintf("<script type=\"text/javascript\">\n"
408                                 "        setTimeout('hide_imsg_popup()', 2000); \n"
409                                 "</script>\n");
410                         safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
411                 }
412                 if ( (WC->logged_in) && (!unset_cookies) ) {
413                         wprintf("<div id=\"iconbar\">");
414                         do_iconbar();
415                         wprintf("</div>\n");
416                 }
417                 if (do_room_banner == 1) {
418                         wprintf("<div id=\"banner\">\n");
419                         embed_room_banner(NULL, navbar_default);
420                         wprintf("</div>\n");
421                 }
422         }
423
424         if (do_room_banner == 1) {
425                 wprintf("<div id=\"content\">\n");
426         }
427 }
428
429
430 /*
431  * Generic function to do an HTTP redirect.  Easy and fun.
432  */
433 void http_redirect(char *whichpage) {
434         wprintf("HTTP/1.0 302 Moved Temporarily\n");
435         wprintf("Location: %s\r\n", whichpage);
436         wprintf("URI: %s\r\n", whichpage);
437         wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
438         wprintf("<html><body>\n");
439         wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
440                 whichpage);
441         wprintf("</body></html>\n");
442 }
443
444
445
446 void check_for_instant_messages()
447 {
448         char buf[SIZ];
449
450         serv_puts("NOOP");
451         serv_getln(buf, sizeof buf);
452         if (buf[3] == '*') WC->HaveInstantMessages = 1;
453 }
454
455
456
457
458 /* 
459  * Output a piece of content to the web browser
460  */
461 void http_transmit_thing(char *thing, size_t length, char *content_type,
462                          int is_static) {
463
464         output_headers(0, 0, 0, 0, 0, 0, is_static);
465
466         wprintf("Content-type: %s\r\n"
467                 "Server: %s\r\n"
468                 "Connection: close\r\n",
469                 content_type,
470                 SERVER);
471
472 #ifdef HAVE_ZLIB
473         /* If we can send the data out compressed, please do so. */
474         if (WC->gzip_ok) {
475                 char *compressed_data = NULL;
476                 uLongf compressed_len;
477
478                 compressed_len = (uLongf) ((length * 101) / 100) + 100;
479                 compressed_data = malloc(compressed_len);
480
481                 if (compress_gzip((Bytef *) compressed_data,
482                                   &compressed_len,
483                                   (Bytef *) thing,
484                                   (uLongf) length, Z_BEST_SPEED) == Z_OK) {
485                         wprintf("Content-encoding: gzip\r\n"
486                                 "Content-length: %ld\r\n"
487                                 "\r\n",
488                                 (long) compressed_len
489                         );
490                         client_write(compressed_data, (size_t)compressed_len);
491                         free(compressed_data);
492                         return;
493                 }
494         }
495 #endif
496
497         /* No compression ... just send it out as-is */
498         wprintf("Content-length: %ld\r\n"
499                 "\r\n",
500                 (long) length
501         );
502         client_write(thing, (size_t)length);
503 }
504
505
506
507
508 void output_static(char *what)
509 {
510         char buf[256];
511         FILE *fp;
512         struct stat statbuf;
513         off_t bytes;
514         char *bigbuffer;
515         char content_type[128];
516
517         sprintf(buf, "static/%s", what);
518         fp = fopen(buf, "rb");
519         if (fp == NULL) {
520                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
521                 wprintf("Content-Type: text/plain\r\n");
522                 wprintf("\r\n");
523                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
524         } else {
525                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
526                         safestrncpy(content_type, "image/gif", sizeof content_type);
527                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
528                         safestrncpy(content_type, "text/plain", sizeof content_type);
529                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
530                         safestrncpy(content_type, "text/css", sizeof content_type);
531                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
532                         safestrncpy(content_type, "image/jpeg", sizeof content_type);
533                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
534                         safestrncpy(content_type, "image/png", sizeof content_type);
535                 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
536                         safestrncpy(content_type, "image/x-icon", sizeof content_type);
537                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
538                         safestrncpy(content_type, "text/html", sizeof content_type);
539                 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
540                         safestrncpy(content_type, "text/html", sizeof content_type);
541                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
542                         safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
543                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
544                         safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
545                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
546                         safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
547                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
548                         safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
549                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
550                         safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
551                 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
552                         safestrncpy(content_type, "text/javascript", sizeof content_type);
553                 else
554                         safestrncpy(content_type, "application/octet-stream", sizeof content_type);
555
556                 fstat(fileno(fp), &statbuf);
557                 bytes = statbuf.st_size;
558                 bigbuffer = malloc(bytes + 2);
559                 fread(bigbuffer, bytes, 1, fp);
560                 fclose(fp);
561
562                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
563                 free(bigbuffer);
564         }
565         if (!strcasecmp(bstr("force_close_session"), "yes")) {
566                 end_webcit_session();
567         }
568 }
569
570
571 /*
572  * When the browser requests an image file from the Citadel server,
573  * this function is called to transmit it.
574  */
575 void output_image()
576 {
577         char buf[SIZ];
578         char *xferbuf = NULL;
579         off_t bytes;
580
581         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
582         serv_getln(buf, sizeof buf);
583         if (buf[0] == '2') {
584                 bytes = extract_long(&buf[4], 0);
585                 xferbuf = malloc(bytes + 2);
586
587                 /* Read it from the server */
588                 read_server_binary(xferbuf, bytes);
589                 serv_puts("CLOS");
590                 serv_getln(buf, sizeof buf);
591
592                 /* Write it to the browser */
593                 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
594                 free(xferbuf);
595
596         } else {
597
598                 /* Instead of an ugly 404, send a 1x1 transparent GIF
599                  * when there's no such image on the server.
600                  */
601                 output_static("blank.gif");
602
603                 /*
604                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
605                 output_headers(0, 0, 0, 0, 0, 0, 0);
606                 wprintf("Content-Type: text/plain\r\n"
607                         "\r\n"
608                         "Error retrieving image: %s\n",
609                         &buf[4]
610                 );
611                 */
612
613         }
614
615
616
617 }
618
619 /*
620  */
621 void output_mimepart()
622 {
623         char buf[SIZ];
624         off_t bytes;
625         char content_type[SIZ];
626         char *content = NULL;
627         
628         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
629         serv_getln(buf, sizeof buf);
630         if (buf[0] == '2') {
631                 bytes = extract_long(&buf[4], 0);
632                 content = malloc(bytes + 2);
633                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
634                 output_headers(0, 0, 0, 0, 0, 0, 0);
635                 read_server_binary(content, bytes);
636                 serv_puts("CLOS");
637                 serv_getln(buf, sizeof buf);
638                 http_transmit_thing(content, bytes, content_type, 0);
639                 free(content);
640         } else {
641                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
642                 output_headers(0, 0, 0, 0, 0, 0, 0);
643                 wprintf("Content-Type: text/plain\r\n");
644                 wprintf("\r\n");
645                 wprintf("Error retrieving part: %s\n", &buf[4]);
646         }
647
648 }
649
650
651 /*
652  */
653 char *load_mimepart(long msgnum, char *partnum)
654 {
655         char buf[SIZ];
656         off_t bytes;
657         char content_type[SIZ];
658         char *content;
659         
660         serv_printf("OPNA %ld|%s", msgnum, partnum);
661         serv_getln(buf, sizeof buf);
662         if (buf[0] == '2') {
663                 bytes = extract_long(&buf[4], 0);
664                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
665
666                 content = malloc(bytes + 2);
667                 read_server_binary(content, bytes);
668
669                 serv_puts("CLOS");
670                 serv_getln(buf, sizeof buf);
671                 content[bytes] = 0;     /* null terminate for good measure */
672                 return(content);
673         }
674         else {
675                 return(NULL);
676         }
677
678 }
679
680
681 /*
682  * Convenience functions to display a page containing only a string
683  */
684 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
685 {
686         wprintf("HTTP/1.0 200 OK\n");
687         output_headers(1, 1, 2, 0, 0, 0, 0);
688         wprintf("<div id=\"banner\">\n");
689         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
690         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
691         wprintf("</TD></TR></TABLE>\n");
692         wprintf("</div>\n<div id=\"content\">\n");
693         escputs(messagetext);
694
695         wprintf("<hr />\n");
696         wDumpContent(1);
697 }
698
699
700 /*
701  * Display a blank page.
702  */
703 void blank_page(void) {
704         output_headers(1, 1, 0, 0, 1, 0, 0);
705         wDumpContent(2);
706 }
707
708
709 /*
710  * A template has been requested
711  */
712 void url_do_template(void) {
713         do_template(bstr("template"));
714 }
715
716
717
718 /*
719  * Offer to make any page the user's "start page."
720  */
721 void offer_start_page(void) {
722         wprintf("<A HREF=\"/change_start_page?startpage=");
723         urlescputs(WC->this_page);
724         wprintf("\">"
725                 "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
726                 "</A>"
727         );
728 }
729
730
731 /* 
732  * Change the user's start page
733  */
734 void change_start_page(void) {
735
736         if (bstr("startpage") == NULL) {
737                 safestrncpy(WC->ImportantMessage,
738                         "startpage set to null",
739                         sizeof WC->ImportantMessage);
740                 display_main_menu();
741                 return;
742         }
743
744         set_preference("startpage", bstr("startpage"), 1);
745
746         output_headers(1, 1, 0, 0, 0, 0, 0);
747         do_template("newstartpage");
748         wDumpContent(1);
749 }
750
751
752
753
754 void display_success(char *successmessage)
755 {
756         convenience_page("007700", "OK", successmessage);
757 }
758
759
760
761 void upload_handler(char *name, char *filename, char *partnum, char *disp,
762                         void *content, char *cbtype, char *cbcharset,
763                         size_t length, char *encoding, void *userdata)
764 {
765         struct urlcontent *u;
766
767         lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
768                 name, cbtype, length);
769
770         /* Form fields */
771         if ( (length > 0) && (strlen(cbtype) == 0) ) {
772                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
773                 u->next = WC->urlstrings;
774                 WC->urlstrings = u;
775                 safestrncpy(u->url_key, name, sizeof(u->url_key));
776                 u->url_data = malloc(length + 1);
777                 memcpy(u->url_data, content, length);
778                 u->url_data[length] = 0;
779         }
780
781         /* Uploaded files */
782         if ( (length > 0) && (strlen(cbtype) > 0) ) {
783                 WC->upload = malloc(length);
784                 if (WC->upload != NULL) {
785                         WC->upload_length = length;
786                         safestrncpy(WC->upload_filename, filename,
787                                         sizeof(WC->upload_filename));
788                         safestrncpy(WC->upload_content_type, cbtype,
789                                         sizeof(WC->upload_content_type));
790                         memcpy(WC->upload, content, length);
791                 }
792                 else {
793                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
794                 }
795         }
796
797 }
798
799
800
801
802 /*
803  * Entry point for WebCit transaction
804  */
805 void session_loop(struct httprequest *req)
806 {
807         char cmd[1024];
808         char method[128];
809         char action[128];
810         char arg1[128];
811         char arg2[128];
812         char arg3[128];
813         char buf[SIZ];
814         int a, b;
815         int ContentLength = 0;
816         int BytesRead = 0;
817         char ContentType[512];
818         char *content = NULL;
819         char *content_end = NULL;
820         struct httprequest *hptr;
821         char browser_host[SIZ];
822         char user_agent[SIZ];
823         int body_start = 0;
824
825         /* We stuff these with the values coming from the client cookies,
826          * so we can use them to reconnect a timed out session if we have to.
827          */
828         char c_username[SIZ];
829         char c_password[SIZ];
830         char c_roomname[SIZ];
831         char c_httpauth_string[SIZ];
832         char c_httpauth_user[SIZ];
833         char c_httpauth_pass[SIZ];
834         char cookie[SIZ];
835
836         safestrncpy(c_username, "", sizeof c_username);
837         safestrncpy(c_password, "", sizeof c_password);
838         safestrncpy(c_roomname, "", sizeof c_roomname);
839         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
840         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
841         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
842
843         WC->upload_length = 0;
844         WC->upload = NULL;
845         WC->vars = NULL;
846
847         WC->is_wap = 0;
848
849         hptr = req;
850         if (hptr == NULL) return;
851
852         safestrncpy(cmd, hptr->line, sizeof cmd);
853         hptr = hptr->next;
854         extract_token(method, cmd, 0, ' ', sizeof method);
855
856         /* Figure out the action */
857         extract_token(action, cmd, 1, '/', sizeof action);
858         if (strstr(action, "?")) *strstr(action, "?") = 0;
859         if (strstr(action, "&")) *strstr(action, "&") = 0;
860         if (strstr(action, " ")) *strstr(action, " ") = 0;
861
862         extract_token(arg1, cmd, 2, '/', sizeof arg1);
863         if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
864         if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
865         if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
866
867         extract_token(arg2, cmd, 3, '/', sizeof arg2);
868         if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
869         if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
870         if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
871
872         extract_token(arg3, cmd, 4, '/', sizeof arg3);
873         if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
874         if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
875         if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
876
877         while (hptr != NULL) {
878                 safestrncpy(buf, hptr->line, sizeof buf);
879                 hptr = hptr->next;
880
881                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
882                         safestrncpy(cookie, &buf[15], sizeof cookie);
883                         cookie_to_stuff(cookie, NULL,
884                                         c_username, sizeof c_username,
885                                         c_password, sizeof c_password,
886                                         c_roomname, sizeof c_roomname);
887                 }
888                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
889                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
890                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
891                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
892                 }
893                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
894                         ContentLength = atoi(&buf[16]);
895                 }
896                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
897                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
898                 }
899                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
900                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
901                 }
902                 else if (!strncasecmp(buf, "Host: ", 6)) {
903                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
904                 }
905                 /* Only WAP gateways explicitly name this content-type */
906                 else if (strstr(buf, "text/vnd.wap.wml")) {
907                         WC->is_wap = 1;
908                 }
909         }
910
911         if (ContentLength > 0) {
912                 content = malloc(ContentLength + SIZ);
913                 memset(content, 0, ContentLength + SIZ);
914                 sprintf(content, "Content-type: %s\n"
915                                 "Content-length: %d\n\n",
916                                 ContentType, ContentLength);
917                 body_start = strlen(content);
918
919                 /* Read the entire input data at once. */
920                 client_read(WC->http_sock, &content[BytesRead+body_start],
921                         ContentLength);
922
923                 if (!strncasecmp(ContentType,
924                               "application/x-www-form-urlencoded", 33)) {
925                         addurls(&content[body_start]);
926                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
927                         content_end = content + ContentLength + body_start;
928                         mime_parser(content, content_end, *upload_handler,
929                                         NULL, NULL, NULL, 0);
930                 }
931         } else {
932                 content = NULL;
933         }
934
935         /* make a note of where we are in case the user wants to save it */
936         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
937         remove_token(WC->this_page, 2, ' ');
938         remove_token(WC->this_page, 0, ' ');
939
940         /* If there are variables in the URL, we must grab them now */
941         for (a = 0; a < strlen(cmd); ++a) {
942                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
943                         for (b = a; b < strlen(cmd); ++b)
944                                 if (isspace(cmd[b]))
945                                         cmd[b] = 0;
946                         addurls(&cmd[a + 1]);
947                         cmd[a] = 0;
948                 }
949         }
950
951
952         /* Static content can be sent without connecting to Citadel. */
953         if (!strcasecmp(action, "static")) {
954                 safestrncpy(buf, arg1, sizeof buf);
955                 for (a = 0; a < strlen(buf); ++a)
956                         if (isspace(buf[a]))
957                                 buf[a] = 0;
958                 output_static(buf);
959                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
960         }
961
962         /*
963          * If we're not connected to a Citadel server, try to hook up the
964          * connection now.
965          */
966         if (!WC->connected) {
967                 if (!strcasecmp(ctdlhost, "uds")) {
968                         /* unix domain socket */
969                         sprintf(buf, "%s/citadel.socket", ctdlport);
970                         WC->serv_sock = uds_connectsock(buf);
971                 }
972                 else {
973                         /* tcp socket */
974                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
975                 }
976
977                 if (WC->serv_sock < 0) {
978                         do_logout();
979                         goto SKIP_ALL_THIS_CRAP;
980                 }
981                 else {
982                         WC->connected = 1;
983                         serv_getln(buf, sizeof buf);    /* get the server welcome message */
984                         locate_host(browser_host, WC->http_sock);
985                         get_serv_info(browser_host, user_agent);
986                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
987                                 wprintf("You are connected to a Citadel "
988                                         "server running Citadel %d.%02d;\nin "
989                                         "order to run this version of WebCit "
990                                         "you must also have Citadel %d.%02d or"
991                                         " newer.\n\n\n",
992                                                 serv_info.serv_rev_level / 100,
993                                                 serv_info.serv_rev_level % 100,
994                                                 MINIMUM_CIT_VERSION / 100,
995                                                 MINIMUM_CIT_VERSION % 100
996                                         );
997                                 end_webcit_session();
998                                 goto SKIP_ALL_THIS_CRAP;
999                         }
1000                 }
1001         }
1002
1003         /*
1004          * Functions which can be performed without logging in
1005          */
1006         if (!strcasecmp(action, "listsub")) {
1007                 do_listsub();
1008                 goto SKIP_ALL_THIS_CRAP;
1009         }
1010 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1011         if (!strcasecmp(action, "freebusy")) {
1012                 do_freebusy(cmd);
1013                 goto SKIP_ALL_THIS_CRAP;
1014         }
1015 #endif
1016
1017         /*
1018          * If we're not logged in, but we have HTTP Authentication data,
1019          * try logging in to Citadel using that.
1020          */
1021         if ((!WC->logged_in)
1022            && (strlen(c_httpauth_user) > 0)
1023            && (strlen(c_httpauth_pass) > 0)) {
1024                 serv_printf("USER %s", c_httpauth_user);
1025                 serv_getln(buf, sizeof buf);
1026                 if (buf[0] == '3') {
1027                         serv_printf("PASS %s", c_httpauth_pass);
1028                         serv_getln(buf, sizeof buf);
1029                         if (buf[0] == '2') {
1030                                 become_logged_in(c_httpauth_user,
1031                                                 c_httpauth_pass, buf);
1032                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1033                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1034                         }
1035                 }
1036         }
1037
1038         /* 
1039          * The GroupDAV stuff relies on HTTP authentication instead of
1040          * our session's authentication.
1041          */
1042         if (!strncasecmp(action, "groupdav", 8)) {
1043                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1044                         ContentLength, content+body_start);
1045                 if (!WC->logged_in) {
1046                         WC->killthis = 1;       /* If not logged in, don't */
1047                 }                               /* keep the session active */
1048                 goto SKIP_ALL_THIS_CRAP;
1049         }
1050
1051
1052         /*
1053          * Automatically send requests with any method other than GET or
1054          * POST to the GroupDAV code as well.
1055          */
1056         if ((strcasecmp(method, "GET")) && (strcasecmp(method, "POST"))) {
1057                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1058                         ContentLength, content+body_start);
1059                 if (!WC->logged_in) {
1060                         WC->killthis = 1;       /* If not logged in, don't */
1061                 }                               /* keep the session active */
1062                 goto SKIP_ALL_THIS_CRAP;
1063         }
1064
1065         /*
1066          * If we're not logged in, but we have username and password cookies
1067          * supplied by the browser, try using them to log in.
1068          */
1069         if ((!WC->logged_in)
1070            && (strlen(c_username) > 0)
1071            && (strlen(c_password) > 0)) {
1072                 serv_printf("USER %s", c_username);
1073                 serv_getln(buf, sizeof buf);
1074                 if (buf[0] == '3') {
1075                         serv_printf("PASS %s", c_password);
1076                         serv_getln(buf, sizeof buf);
1077                         if (buf[0] == '2') {
1078                                 become_logged_in(c_username, c_password, buf);
1079                         }
1080                 }
1081         }
1082         /*
1083          * If we don't have a current room, but a cookie specifying the
1084          * current room is supplied, make an effort to go there.
1085          */
1086         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1087                 serv_printf("GOTO %s", c_roomname);
1088                 serv_getln(buf, sizeof buf);
1089                 if (buf[0] == '2') {
1090                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1091                 }
1092         }
1093
1094         /*
1095          * If there are instant messages waiting, retrieve them for display.
1096          */
1097         check_for_instant_messages();
1098
1099         if (!strcasecmp(action, "image")) {
1100                 output_image();
1101
1102         /*
1103          * All functions handled below this point ... make sure we log in
1104          * before doing anything else!
1105          */
1106         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1107                 do_login();
1108         } else if (!WC->logged_in) {
1109                 display_login(NULL);
1110         }
1111
1112         /*
1113          * Various commands...
1114          */
1115
1116         else if (!strcasecmp(action, "do_welcome")) {
1117                 do_welcome();
1118         } else if (!strcasecmp(action, "blank")) {
1119                 blank_page();
1120         } else if (!strcasecmp(action, "do_template")) {
1121                 url_do_template();
1122         } else if (!strcasecmp(action, "display_aide_menu")) {
1123                 display_aide_menu();
1124         } else if (!strcasecmp(action, "display_main_menu")) {
1125                 display_main_menu();
1126         } else if (!strcasecmp(action, "who")) {
1127                 who();
1128         } else if (!strcasecmp(action, "who_inner_html")) {
1129                 who_inner_html();
1130         } else if (!strcasecmp(action, "knrooms")) {
1131                 knrooms();
1132         } else if (!strcasecmp(action, "gotonext")) {
1133                 slrp_highest();
1134                 gotonext();
1135         } else if (!strcasecmp(action, "skip")) {
1136                 gotonext();
1137         } else if (!strcasecmp(action, "ungoto")) {
1138                 ungoto();
1139         } else if (!strcasecmp(action, "dotgoto")) {
1140                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1141                         slrp_highest();
1142                 }
1143                 smart_goto(bstr("room"));
1144         } else if (!strcasecmp(action, "dotskip")) {
1145                 smart_goto(bstr("room"));
1146         } else if (!strcasecmp(action, "termquit")) {
1147                 do_logout();
1148         } else if (!strcasecmp(action, "readnew")) {
1149                 readloop("readnew");
1150         } else if (!strcasecmp(action, "readold")) {
1151                 readloop("readold");
1152         } else if (!strcasecmp(action, "readfwd")) {
1153                 readloop("readfwd");
1154         } else if (!strcasecmp(action, "headers")) {
1155                 readloop("headers");
1156         } else if (!strcasecmp(action, "msg")) {
1157                 embed_message();
1158         } else if (!strcasecmp(action, "display_enter")) {
1159                 display_enter();
1160         } else if (!strcasecmp(action, "post")) {
1161                 post_message();
1162         } else if (!strcasecmp(action, "move_msg")) {
1163                 move_msg();
1164         } else if (!strcasecmp(action, "delete_msg")) {
1165                 delete_msg();
1166         } else if (!strcasecmp(action, "userlist")) {
1167                 userlist();
1168         } else if (!strcasecmp(action, "showuser")) {
1169                 showuser();
1170         } else if (!strcasecmp(action, "display_page")) {
1171                 display_page();
1172         } else if (!strcasecmp(action, "page_user")) {
1173                 page_user();
1174         } else if (!strcasecmp(action, "chat")) {
1175                 do_chat();
1176         } else if (!strcasecmp(action, "display_private")) {
1177                 display_private("", 0);
1178         } else if (!strcasecmp(action, "goto_private")) {
1179                 goto_private();
1180         } else if (!strcasecmp(action, "zapped_list")) {
1181                 zapped_list();
1182         } else if (!strcasecmp(action, "display_zap")) {
1183                 display_zap();
1184         } else if (!strcasecmp(action, "zap")) {
1185                 zap();
1186         } else if (!strcasecmp(action, "display_entroom")) {
1187                 display_entroom();
1188         } else if (!strcasecmp(action, "entroom")) {
1189                 entroom();
1190         } else if (!strcasecmp(action, "display_whok")) {
1191                 display_whok();
1192         } else if (!strcasecmp(action, "do_invt_kick")) {
1193                 do_invt_kick();
1194         } else if (!strcasecmp(action, "display_editroom")) {
1195                 display_editroom();
1196         } else if (!strcasecmp(action, "netedit")) {
1197                 netedit();
1198         } else if (!strcasecmp(action, "editroom")) {
1199                 editroom();
1200         } else if (!strcasecmp(action, "display_editinfo")) {
1201                 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1202         } else if (!strcasecmp(action, "editinfo")) {
1203                 save_edit("Room info", "EINF 1", 1);
1204         } else if (!strcasecmp(action, "display_editbio")) {
1205                 sprintf(buf, "RBIO %s", WC->wc_username);
1206                 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1207         } else if (!strcasecmp(action, "editbio")) {
1208                 save_edit("Your bio", "EBIO", 0);
1209         } else if (!strcasecmp(action, "confirm_move_msg")) {
1210                 confirm_move_msg();
1211         } else if (!strcasecmp(action, "delete_room")) {
1212                 delete_room();
1213         } else if (!strcasecmp(action, "validate")) {
1214                 validate();
1215         } else if (!strcasecmp(action, "display_editpic")) {
1216                 display_graphics_upload("your photo",
1217                                         "UIMG 0|_userpic_",
1218                                         "/editpic");
1219         } else if (!strcasecmp(action, "editpic")) {
1220                 do_graphics_upload("UIMG 1|_userpic_");
1221         } else if (!strcasecmp(action, "display_editroompic")) {
1222                 display_graphics_upload("the icon for this room",
1223                                         "UIMG 0|_roompic_",
1224                                         "/editroompic");
1225         } else if (!strcasecmp(action, "editroompic")) {
1226                 do_graphics_upload("UIMG 1|_roompic_");
1227         } else if (!strcasecmp(action, "delete_floor")) {
1228                 delete_floor();
1229         } else if (!strcasecmp(action, "rename_floor")) {
1230                 rename_floor();
1231         } else if (!strcasecmp(action, "create_floor")) {
1232                 create_floor();
1233         } else if (!strcasecmp(action, "display_editfloorpic")) {
1234                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1235                         bstr("which_floor"));
1236                 display_graphics_upload("the icon for this floor",
1237                                         buf,
1238                                         "/editfloorpic");
1239         } else if (!strcasecmp(action, "editfloorpic")) {
1240                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1241                         bstr("which_floor"));
1242                 do_graphics_upload(buf);
1243         } else if (!strcasecmp(action, "display_reg")) {
1244                 display_reg(0);
1245         } else if (!strcasecmp(action, "display_changepw")) {
1246                 display_changepw();
1247         } else if (!strcasecmp(action, "changepw")) {
1248                 changepw();
1249         } else if (!strcasecmp(action, "display_edit_node")) {
1250                 display_edit_node();
1251         } else if (!strcasecmp(action, "edit_node")) {
1252                 edit_node();
1253         } else if (!strcasecmp(action, "display_netconf")) {
1254                 display_netconf();
1255         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1256                 display_confirm_delete_node();
1257         } else if (!strcasecmp(action, "delete_node")) {
1258                 delete_node();
1259         } else if (!strcasecmp(action, "display_add_node")) {
1260                 display_add_node();
1261         } else if (!strcasecmp(action, "add_node")) {
1262                 add_node();
1263         } else if (!strcasecmp(action, "terminate_session")) {
1264                 slrp_highest();
1265                 terminate_session();
1266         } else if (!strcasecmp(action, "edit_me")) {
1267                 edit_me();
1268         } else if (!strcasecmp(action, "display_siteconfig")) {
1269                 display_siteconfig();
1270         } else if (!strcasecmp(action, "chat_recv")) {
1271                 chat_recv();
1272         } else if (!strcasecmp(action, "chat_send")) {
1273                 chat_send();
1274         } else if (!strcasecmp(action, "siteconfig")) {
1275                 siteconfig();
1276         } else if (!strcasecmp(action, "display_generic")) {
1277                 display_generic();
1278         } else if (!strcasecmp(action, "do_generic")) {
1279                 do_generic();
1280         } else if (!strcasecmp(action, "display_menubar")) {
1281                 display_menubar(1);
1282         } else if (!strcasecmp(action, "output_mimepart")) {
1283                 output_mimepart();
1284         } else if (!strcasecmp(action, "edit_vcard")) {
1285                 edit_vcard();
1286         } else if (!strcasecmp(action, "submit_vcard")) {
1287                 submit_vcard();
1288         } else if (!strcasecmp(action, "select_user_to_edit")) {
1289                 select_user_to_edit(NULL, NULL);
1290         } else if (!strcasecmp(action, "display_edituser")) {
1291                 display_edituser(NULL, 0);
1292         } else if (!strcasecmp(action, "edituser")) {
1293                 edituser();
1294         } else if (!strcasecmp(action, "create_user")) {
1295                 create_user();
1296         } else if (!strcasecmp(action, "changeview")) {
1297                 change_view();
1298         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1299                 do_stuff_to_msgs();
1300         } else if (!strcasecmp(action, "change_start_page")) {
1301                 change_start_page();
1302         } else if (!strcasecmp(action, "display_floorconfig")) {
1303                 display_floorconfig(NULL);
1304         } else if (!strcasecmp(action, "toggle_self_service")) {
1305                 toggle_self_service();
1306 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1307         } else if (!strcasecmp(action, "display_edit_task")) {
1308                 display_edit_task();
1309         } else if (!strcasecmp(action, "save_task")) {
1310                 save_task();
1311         } else if (!strcasecmp(action, "display_edit_event")) {
1312                 display_edit_event();
1313         } else if (!strcasecmp(action, "save_event")) {
1314                 save_event();
1315         } else if (!strcasecmp(action, "respond_to_request")) {
1316                 respond_to_request();
1317         } else if (!strcasecmp(action, "handle_rsvp")) {
1318                 handle_rsvp();
1319 #endif
1320         } else if (!strcasecmp(action, "summary")) {
1321                 summary();
1322         } else if (!strcasecmp(action, "iconbar")) {
1323                 do_iconbar();
1324         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1325                 display_customize_iconbar();
1326         } else if (!strcasecmp(action, "commit_iconbar")) {
1327                 commit_iconbar();
1328         } else if (!strcasecmp(action, "set_room_policy")) {
1329                 set_room_policy();
1330         } else if (!strcasecmp(action, "display_inetconf")) {
1331                 display_inetconf();
1332         } else if (!strcasecmp(action, "save_inetconf")) {
1333                 save_inetconf();
1334         } else if (!strcasecmp(action, "setup_wizard")) {
1335                 do_setup_wizard();
1336         } else if (!strcasecmp(action, "display_preferences")) {
1337                 display_preferences();
1338         } else if (!strcasecmp(action, "set_preferences")) {
1339                 set_preferences();
1340         } else if (!strcasecmp(action, "diagnostics")) {
1341                 output_headers(1, 1, 1, 0, 0, 0, 0);
1342
1343                 wprintf("You're in session %d<hr />\n", WC->wc_session);
1344                 wprintf("Command: <br /><PRE>\n");
1345                 escputs(cmd);
1346                 wprintf("</PRE><hr />\n");
1347                 wprintf("Variables: <br /><PRE>\n");
1348                 dump_vars();
1349                 wprintf("</PRE><hr />\n");
1350                 wDumpContent(1);
1351         }
1352         /* When all else fais, display the main menu. */
1353         else {
1354                 display_main_menu();
1355         }
1356
1357 SKIP_ALL_THIS_CRAP:
1358         fflush(stdout);
1359         if (content != NULL) {
1360                 free(content);
1361                 content = NULL;
1362         }
1363         free_urls();
1364         if (WC->upload_length > 0) {
1365                 free(WC->upload);
1366                 WC->upload_length = 0;
1367         }
1368
1369 }