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