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