]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
First-draft of RSS support.
[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 /* Authorization required page */
761 /* This is probably temporary and should be revisited */
762 void authorization_required(const char *message)
763 {
764         wprintf("HTTP/1.0 401 Authorization Required\r\n");
765         wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
766         wprintf("Content-Type: text/html\r\n\r\n");
767         wprintf("<h1>Authorization Required</h1>\r\n");
768         wprintf("The resource you requested requires a valid username and password.");
769         wprintf("I could not log you 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 method[128];
822         char action[128];
823         char arg1[128];
824         char arg2[128];
825         char arg3[128];
826         char buf[SIZ];
827         int a, b;
828         int ContentLength = 0;
829         int BytesRead = 0;
830         char ContentType[512];
831         char *content = NULL;
832         char *content_end = NULL;
833         struct httprequest *hptr;
834         char browser_host[SIZ];
835         char user_agent[SIZ];
836         int body_start = 0;
837
838         /* We stuff these with the values coming from the client cookies,
839          * so we can use them to reconnect a timed out session if we have to.
840          */
841         char c_username[SIZ];
842         char c_password[SIZ];
843         char c_roomname[SIZ];
844         char c_httpauth_string[SIZ];
845         char c_httpauth_user[SIZ];
846         char c_httpauth_pass[SIZ];
847         char cookie[SIZ];
848
849         safestrncpy(c_username, "", sizeof c_username);
850         safestrncpy(c_password, "", sizeof c_password);
851         safestrncpy(c_roomname, "", sizeof c_roomname);
852         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
853         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
854         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
855
856         WC->upload_length = 0;
857         WC->upload = NULL;
858         WC->vars = NULL;
859
860         WC->is_wap = 0;
861
862         hptr = req;
863         if (hptr == NULL) return;
864
865         safestrncpy(cmd, hptr->line, sizeof cmd);
866         hptr = hptr->next;
867         extract_token(method, cmd, 0, ' ', sizeof method);
868
869         /* Figure out the action */
870         extract_token(action, cmd, 1, '/', sizeof action);
871         if (strstr(action, "?")) *strstr(action, "?") = 0;
872         if (strstr(action, "&")) *strstr(action, "&") = 0;
873         if (strstr(action, " ")) *strstr(action, " ") = 0;
874
875         extract_token(arg1, cmd, 2, '/', sizeof arg1);
876         if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
877         if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
878         if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
879
880         extract_token(arg2, cmd, 3, '/', sizeof arg2);
881         if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
882         if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
883         if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
884
885         extract_token(arg3, cmd, 4, '/', sizeof arg3);
886         if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
887         if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
888         if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
889
890         while (hptr != NULL) {
891                 safestrncpy(buf, hptr->line, sizeof buf);
892                 hptr = hptr->next;
893
894                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
895                         safestrncpy(cookie, &buf[15], sizeof cookie);
896                         cookie_to_stuff(cookie, NULL,
897                                         c_username, sizeof c_username,
898                                         c_password, sizeof c_password,
899                                         c_roomname, sizeof c_roomname);
900                 }
901                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
902                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
903                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
904                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
905                 }
906                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
907                         ContentLength = atoi(&buf[16]);
908                 }
909                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
910                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
911                 }
912                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
913                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
914                 }
915                 else if (!strncasecmp(buf, "Host: ", 6)) {
916                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
917                 }
918                 /* Only WAP gateways explicitly name this content-type */
919                 else if (strstr(buf, "text/vnd.wap.wml")) {
920                         WC->is_wap = 1;
921                 }
922         }
923
924         if (ContentLength > 0) {
925                 content = malloc(ContentLength + SIZ);
926                 memset(content, 0, ContentLength + SIZ);
927                 sprintf(content, "Content-type: %s\n"
928                                 "Content-length: %d\n\n",
929                                 ContentType, ContentLength);
930                 body_start = strlen(content);
931
932                 /* Read the entire input data at once. */
933                 client_read(WC->http_sock, &content[BytesRead+body_start],
934                         ContentLength);
935
936                 if (!strncasecmp(ContentType,
937                               "application/x-www-form-urlencoded", 33)) {
938                         addurls(&content[body_start]);
939                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
940                         content_end = content + ContentLength + body_start;
941                         mime_parser(content, content_end, *upload_handler,
942                                         NULL, NULL, NULL, 0);
943                 }
944         } else {
945                 content = NULL;
946         }
947
948         /* make a note of where we are in case the user wants to save it */
949         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
950         remove_token(WC->this_page, 2, ' ');
951         remove_token(WC->this_page, 0, ' ');
952
953         /* If there are variables in the URL, we must grab them now */
954         for (a = 0; a < strlen(cmd); ++a) {
955                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
956                         for (b = a; b < strlen(cmd); ++b)
957                                 if (isspace(cmd[b]))
958                                         cmd[b] = 0;
959                         addurls(&cmd[a + 1]);
960                         cmd[a] = 0;
961                 }
962         }
963
964
965         /* Static content can be sent without connecting to Citadel. */
966         if (!strcasecmp(action, "static")) {
967                 safestrncpy(buf, arg1, sizeof buf);
968                 for (a = 0; a < strlen(buf); ++a)
969                         if (isspace(buf[a]))
970                                 buf[a] = 0;
971                 output_static(buf);
972                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
973         }
974
975         /*
976          * If we're not connected to a Citadel server, try to hook up the
977          * connection now.
978          */
979         if (!WC->connected) {
980                 if (!strcasecmp(ctdlhost, "uds")) {
981                         /* unix domain socket */
982                         sprintf(buf, "%s/citadel.socket", ctdlport);
983                         WC->serv_sock = uds_connectsock(buf);
984                 }
985                 else {
986                         /* tcp socket */
987                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
988                 }
989
990                 if (WC->serv_sock < 0) {
991                         do_logout();
992                         goto SKIP_ALL_THIS_CRAP;
993                 }
994                 else {
995                         WC->connected = 1;
996                         serv_getln(buf, sizeof buf);    /* get the server welcome message */
997                         locate_host(browser_host, WC->http_sock);
998                         get_serv_info(browser_host, user_agent);
999                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1000                                 wprintf("You are connected to a Citadel "
1001                                         "server running Citadel %d.%02d;\nin "
1002                                         "order to run this version of WebCit "
1003                                         "you must also have Citadel %d.%02d or"
1004                                         " newer.\n\n\n",
1005                                                 serv_info.serv_rev_level / 100,
1006                                                 serv_info.serv_rev_level % 100,
1007                                                 MINIMUM_CIT_VERSION / 100,
1008                                                 MINIMUM_CIT_VERSION % 100
1009                                         );
1010                                 end_webcit_session();
1011                                 goto SKIP_ALL_THIS_CRAP;
1012                         }
1013                 }
1014         }
1015
1016         /*
1017          * Functions which can be performed without logging in
1018          */
1019         if (!strcasecmp(action, "listsub")) {
1020                 do_listsub();
1021                 goto SKIP_ALL_THIS_CRAP;
1022         }
1023 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1024         if (!strcasecmp(action, "freebusy")) {
1025                 do_freebusy(cmd);
1026                 goto SKIP_ALL_THIS_CRAP;
1027         }
1028 #endif
1029
1030         /*
1031          * If we're not logged in, but we have HTTP Authentication data,
1032          * try logging in to Citadel using that.
1033          */
1034         if ((!WC->logged_in)
1035            && (strlen(c_httpauth_user) > 0)
1036            && (strlen(c_httpauth_pass) > 0)) {
1037                 serv_printf("USER %s", c_httpauth_user);
1038                 serv_getln(buf, sizeof buf);
1039                 if (buf[0] == '3') {
1040                         serv_printf("PASS %s", c_httpauth_pass);
1041                         serv_getln(buf, sizeof buf);
1042                         if (buf[0] == '2') {
1043                                 become_logged_in(c_httpauth_user,
1044                                                 c_httpauth_pass, buf);
1045                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1046                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1047                         } else {
1048                                 /* Should only display when password is wrong */
1049                                 authorization_required(&buf[4]);
1050                                 goto SKIP_ALL_THIS_CRAP;
1051                         }
1052                 }
1053         }
1054
1055         /* This needs to run early */
1056         if (!strcasecmp(action, "rss")) {
1057                 display_rss(bstr("room"));
1058                 goto SKIP_ALL_THIS_CRAP;
1059         }
1060
1061         /* 
1062          * The GroupDAV stuff relies on HTTP authentication instead of
1063          * our session's authentication.
1064          */
1065         if (!strncasecmp(action, "groupdav", 8)) {
1066                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1067                         ContentLength, content+body_start);
1068                 if (!WC->logged_in) {
1069                         WC->killthis = 1;       /* If not logged in, don't */
1070                 }                               /* keep the session active */
1071                 goto SKIP_ALL_THIS_CRAP;
1072         }
1073
1074
1075         /*
1076          * Automatically send requests with any method other than GET or
1077          * POST to the GroupDAV code as well.
1078          */
1079         if ((strcasecmp(method, "GET")) && (strcasecmp(method, "POST"))) {
1080                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1081                         ContentLength, content+body_start);
1082                 if (!WC->logged_in) {
1083                         WC->killthis = 1;       /* If not logged in, don't */
1084                 }                               /* keep the session active */
1085                 goto SKIP_ALL_THIS_CRAP;
1086         }
1087
1088         /*
1089          * If we're not logged in, but we have username and password cookies
1090          * supplied by the browser, try using them to log in.
1091          */
1092         if ((!WC->logged_in)
1093            && (strlen(c_username) > 0)
1094            && (strlen(c_password) > 0)) {
1095                 serv_printf("USER %s", c_username);
1096                 serv_getln(buf, sizeof buf);
1097                 if (buf[0] == '3') {
1098                         serv_printf("PASS %s", c_password);
1099                         serv_getln(buf, sizeof buf);
1100                         if (buf[0] == '2') {
1101                                 become_logged_in(c_username, c_password, buf);
1102                         }
1103                 }
1104         }
1105         /*
1106          * If we don't have a current room, but a cookie specifying the
1107          * current room is supplied, make an effort to go there.
1108          */
1109         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1110                 serv_printf("GOTO %s", c_roomname);
1111                 serv_getln(buf, sizeof buf);
1112                 if (buf[0] == '2') {
1113                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1114                 }
1115         }
1116
1117         /*
1118          * If there are instant messages waiting, retrieve them for display.
1119          */
1120         check_for_instant_messages();
1121
1122         if (!strcasecmp(action, "image")) {
1123                 output_image();
1124
1125         /*
1126          * All functions handled below this point ... make sure we log in
1127          * before doing anything else!
1128          */
1129         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1130                 do_login();
1131         } else if (!WC->logged_in) {
1132                 display_login(NULL);
1133         }
1134
1135         /*
1136          * Various commands...
1137          */
1138
1139         else if (!strcasecmp(action, "do_welcome")) {
1140                 do_welcome();
1141         } else if (!strcasecmp(action, "blank")) {
1142                 blank_page();
1143         } else if (!strcasecmp(action, "do_template")) {
1144                 url_do_template();
1145         } else if (!strcasecmp(action, "display_aide_menu")) {
1146                 display_aide_menu();
1147         } else if (!strcasecmp(action, "display_main_menu")) {
1148                 display_main_menu();
1149         } else if (!strcasecmp(action, "who")) {
1150                 who();
1151         } else if (!strcasecmp(action, "who_inner_html")) {
1152                 who_inner_html();
1153         } else if (!strcasecmp(action, "knrooms")) {
1154                 knrooms();
1155         } else if (!strcasecmp(action, "gotonext")) {
1156                 slrp_highest();
1157                 gotonext();
1158         } else if (!strcasecmp(action, "skip")) {
1159                 gotonext();
1160         } else if (!strcasecmp(action, "ungoto")) {
1161                 ungoto();
1162         } else if (!strcasecmp(action, "dotgoto")) {
1163                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1164                         slrp_highest();
1165                 }
1166                 smart_goto(bstr("room"));
1167         } else if (!strcasecmp(action, "dotskip")) {
1168                 smart_goto(bstr("room"));
1169         } else if (!strcasecmp(action, "termquit")) {
1170                 do_logout();
1171         } else if (!strcasecmp(action, "readnew")) {
1172                 readloop("readnew");
1173         } else if (!strcasecmp(action, "readold")) {
1174                 readloop("readold");
1175         } else if (!strcasecmp(action, "readfwd")) {
1176                 readloop("readfwd");
1177         } else if (!strcasecmp(action, "headers")) {
1178                 readloop("headers");
1179         } else if (!strcasecmp(action, "msg")) {
1180                 embed_message();
1181         } else if (!strcasecmp(action, "display_enter")) {
1182                 display_enter();
1183         } else if (!strcasecmp(action, "post")) {
1184                 post_message();
1185         } else if (!strcasecmp(action, "move_msg")) {
1186                 move_msg();
1187         } else if (!strcasecmp(action, "delete_msg")) {
1188                 delete_msg();
1189         } else if (!strcasecmp(action, "userlist")) {
1190                 userlist();
1191         } else if (!strcasecmp(action, "showuser")) {
1192                 showuser();
1193         } else if (!strcasecmp(action, "display_page")) {
1194                 display_page();
1195         } else if (!strcasecmp(action, "page_user")) {
1196                 page_user();
1197         } else if (!strcasecmp(action, "chat")) {
1198                 do_chat();
1199         } else if (!strcasecmp(action, "display_private")) {
1200                 display_private("", 0);
1201         } else if (!strcasecmp(action, "goto_private")) {
1202                 goto_private();
1203         } else if (!strcasecmp(action, "zapped_list")) {
1204                 zapped_list();
1205         } else if (!strcasecmp(action, "display_zap")) {
1206                 display_zap();
1207         } else if (!strcasecmp(action, "zap")) {
1208                 zap();
1209         } else if (!strcasecmp(action, "display_entroom")) {
1210                 display_entroom();
1211         } else if (!strcasecmp(action, "entroom")) {
1212                 entroom();
1213         } else if (!strcasecmp(action, "display_whok")) {
1214                 display_whok();
1215         } else if (!strcasecmp(action, "do_invt_kick")) {
1216                 do_invt_kick();
1217         } else if (!strcasecmp(action, "display_editroom")) {
1218                 display_editroom();
1219         } else if (!strcasecmp(action, "netedit")) {
1220                 netedit();
1221         } else if (!strcasecmp(action, "editroom")) {
1222                 editroom();
1223         } else if (!strcasecmp(action, "display_editinfo")) {
1224                 display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
1225         } else if (!strcasecmp(action, "editinfo")) {
1226                 save_edit("Room info", "EINF 1", 1);
1227         } else if (!strcasecmp(action, "display_editbio")) {
1228                 sprintf(buf, "RBIO %s", WC->wc_username);
1229                 display_edit("Your bio", "NOOP", buf, "editbio", 3);
1230         } else if (!strcasecmp(action, "editbio")) {
1231                 save_edit("Your bio", "EBIO", 0);
1232         } else if (!strcasecmp(action, "confirm_move_msg")) {
1233                 confirm_move_msg();
1234         } else if (!strcasecmp(action, "delete_room")) {
1235                 delete_room();
1236         } else if (!strcasecmp(action, "validate")) {
1237                 validate();
1238         } else if (!strcasecmp(action, "display_editpic")) {
1239                 display_graphics_upload("your photo",
1240                                         "UIMG 0|_userpic_",
1241                                         "/editpic");
1242         } else if (!strcasecmp(action, "editpic")) {
1243                 do_graphics_upload("UIMG 1|_userpic_");
1244         } else if (!strcasecmp(action, "display_editroompic")) {
1245                 display_graphics_upload("the icon for this room",
1246                                         "UIMG 0|_roompic_",
1247                                         "/editroompic");
1248         } else if (!strcasecmp(action, "editroompic")) {
1249                 do_graphics_upload("UIMG 1|_roompic_");
1250         } else if (!strcasecmp(action, "delete_floor")) {
1251                 delete_floor();
1252         } else if (!strcasecmp(action, "rename_floor")) {
1253                 rename_floor();
1254         } else if (!strcasecmp(action, "create_floor")) {
1255                 create_floor();
1256         } else if (!strcasecmp(action, "display_editfloorpic")) {
1257                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1258                         bstr("which_floor"));
1259                 display_graphics_upload("the icon for this floor",
1260                                         buf,
1261                                         "/editfloorpic");
1262         } else if (!strcasecmp(action, "editfloorpic")) {
1263                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1264                         bstr("which_floor"));
1265                 do_graphics_upload(buf);
1266         } else if (!strcasecmp(action, "display_reg")) {
1267                 display_reg(0);
1268         } else if (!strcasecmp(action, "display_changepw")) {
1269                 display_changepw();
1270         } else if (!strcasecmp(action, "changepw")) {
1271                 changepw();
1272         } else if (!strcasecmp(action, "display_edit_node")) {
1273                 display_edit_node();
1274         } else if (!strcasecmp(action, "edit_node")) {
1275                 edit_node();
1276         } else if (!strcasecmp(action, "display_netconf")) {
1277                 display_netconf();
1278         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1279                 display_confirm_delete_node();
1280         } else if (!strcasecmp(action, "delete_node")) {
1281                 delete_node();
1282         } else if (!strcasecmp(action, "display_add_node")) {
1283                 display_add_node();
1284         } else if (!strcasecmp(action, "add_node")) {
1285                 add_node();
1286         } else if (!strcasecmp(action, "terminate_session")) {
1287                 slrp_highest();
1288                 terminate_session();
1289         } else if (!strcasecmp(action, "edit_me")) {
1290                 edit_me();
1291         } else if (!strcasecmp(action, "display_siteconfig")) {
1292                 display_siteconfig();
1293         } else if (!strcasecmp(action, "chat_recv")) {
1294                 chat_recv();
1295         } else if (!strcasecmp(action, "chat_send")) {
1296                 chat_send();
1297         } else if (!strcasecmp(action, "siteconfig")) {
1298                 siteconfig();
1299         } else if (!strcasecmp(action, "display_generic")) {
1300                 display_generic();
1301         } else if (!strcasecmp(action, "do_generic")) {
1302                 do_generic();
1303         } else if (!strcasecmp(action, "display_menubar")) {
1304                 display_menubar(1);
1305         } else if (!strcasecmp(action, "output_mimepart")) {
1306                 output_mimepart();
1307         } else if (!strcasecmp(action, "edit_vcard")) {
1308                 edit_vcard();
1309         } else if (!strcasecmp(action, "submit_vcard")) {
1310                 submit_vcard();
1311         } else if (!strcasecmp(action, "select_user_to_edit")) {
1312                 select_user_to_edit(NULL, NULL);
1313         } else if (!strcasecmp(action, "display_edituser")) {
1314                 display_edituser(NULL, 0);
1315         } else if (!strcasecmp(action, "edituser")) {
1316                 edituser();
1317         } else if (!strcasecmp(action, "create_user")) {
1318                 create_user();
1319         } else if (!strcasecmp(action, "changeview")) {
1320                 change_view();
1321         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1322                 do_stuff_to_msgs();
1323         } else if (!strcasecmp(action, "change_start_page")) {
1324                 change_start_page();
1325         } else if (!strcasecmp(action, "display_floorconfig")) {
1326                 display_floorconfig(NULL);
1327         } else if (!strcasecmp(action, "toggle_self_service")) {
1328                 toggle_self_service();
1329 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1330         } else if (!strcasecmp(action, "display_edit_task")) {
1331                 display_edit_task();
1332         } else if (!strcasecmp(action, "save_task")) {
1333                 save_task();
1334         } else if (!strcasecmp(action, "display_edit_event")) {
1335                 display_edit_event();
1336         } else if (!strcasecmp(action, "save_event")) {
1337                 save_event();
1338         } else if (!strcasecmp(action, "respond_to_request")) {
1339                 respond_to_request();
1340         } else if (!strcasecmp(action, "handle_rsvp")) {
1341                 handle_rsvp();
1342 #endif
1343         } else if (!strcasecmp(action, "summary")) {
1344                 summary();
1345         } else if (!strcasecmp(action, "iconbar")) {
1346                 do_iconbar();
1347         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1348                 display_customize_iconbar();
1349         } else if (!strcasecmp(action, "commit_iconbar")) {
1350                 commit_iconbar();
1351         } else if (!strcasecmp(action, "set_room_policy")) {
1352                 set_room_policy();
1353         } else if (!strcasecmp(action, "display_inetconf")) {
1354                 display_inetconf();
1355         } else if (!strcasecmp(action, "save_inetconf")) {
1356                 save_inetconf();
1357         } else if (!strcasecmp(action, "setup_wizard")) {
1358                 do_setup_wizard();
1359         } else if (!strcasecmp(action, "display_preferences")) {
1360                 display_preferences();
1361         } else if (!strcasecmp(action, "set_preferences")) {
1362                 set_preferences();
1363         } else if (!strcasecmp(action, "diagnostics")) {
1364                 output_headers(1, 1, 1, 0, 0, 0, 0);
1365
1366                 wprintf("You're in session %d<hr />\n", WC->wc_session);
1367                 wprintf("Command: <br /><PRE>\n");
1368                 escputs(cmd);
1369                 wprintf("</PRE><hr />\n");
1370                 wprintf("Variables: <br /><PRE>\n");
1371                 dump_vars();
1372                 wprintf("</PRE><hr />\n");
1373                 wDumpContent(1);
1374         }
1375         /* When all else fais, display the main menu. */
1376         else {
1377                 display_main_menu();
1378         }
1379
1380 SKIP_ALL_THIS_CRAP:
1381         fflush(stdout);
1382         if (content != NULL) {
1383                 free(content);
1384                 content = NULL;
1385         }
1386         free_urls();
1387         if (WC->upload_length > 0) {
1388                 free(WC->upload);
1389                 WC->upload_length = 0;
1390         }
1391
1392 }