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