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