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