* Room info popup now uses Scriptactulous appear and fade effects
[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("\">");
767         wprintf(_("Make this my start page"));
768         wprintf("</a>");
769 /*
770         wprintf("<br/><a href=\"rss?room=");
771         urlescputs(WC->wc_roomname);
772         wprintf("\" title=\"RSS 2.0 feed for ");
773         escputs(WC->wc_roomname);
774         wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
775 */
776 }
777
778
779 /**
780  * \brief Change the user's start page
781  */
782 void change_start_page(void) {
783
784         if (bstr("startpage") == NULL) {
785                 safestrncpy(WC->ImportantMessage,
786                         _("You no longer have a start page selected."),
787                         sizeof WC->ImportantMessage);
788                 display_main_menu();
789                 return;
790         }
791
792         set_preference("startpage", bstr("startpage"), 1);
793
794         output_headers(1, 1, 0, 0, 0, 0);
795         do_template("newstartpage");
796         wDumpContent(1);
797 }
798
799
800
801 /**
802  * \brief convenience function to indicate success
803  * \param successmessage the mesage itself
804  */
805 void display_success(char *successmessage)
806 {
807         convenience_page("007700", "OK", successmessage);
808 }
809
810
811 /**
812  * \brief Authorization required page 
813  * This is probably temporary and should be revisited 
814  * \param message message to put in header
815 */
816 void authorization_required(const char *message)
817 {
818         wprintf("HTTP/1.1 401 Authorization Required\r\n");
819         wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
820         wprintf("Content-Type: text/html\r\n\r\n");
821         wprintf("<h1>");
822         wprintf(_("Authorization Required"));
823         wprintf("</h1>\r\n");
824         wprintf(_("The resource you requested requires a valid username and password. "
825                 "You could not be logged in: %s\n"), message);
826         wDumpContent(0);
827 }
828
829 /**
830  * \brief This function is called by the MIME parser to handle data uploaded by
831  *        the browser.  Form data, uploaded files, and the data from HTTP PUT
832  *        operations (such as those found in GroupDAV) all arrive this way.
833  *
834  * \param name Name of the item being uploaded
835  * \param filename Filename of the item being uploaded
836  * \param partnum MIME part identifier (not needed)
837  * \param disp MIME content disposition (not needed)
838  * \param content The actual data
839  * \param cbtype MIME content-type
840  * \param cbcharset Character set
841  * \param length Content length
842  * \param encoding MIME encoding type (not needed)
843  * \param userdata Not used here
844  */
845 void upload_handler(char *name, char *filename, char *partnum, char *disp,
846                         void *content, char *cbtype, char *cbcharset,
847                         size_t length, char *encoding, void *userdata)
848 {
849         struct urlcontent *u;
850
851         lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n", name, cbtype, length);
852
853         /* Form fields */
854         if ( (length > 0) && (strlen(cbtype) == 0) ) {
855                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
856                 u->next = WC->urlstrings;
857                 WC->urlstrings = u;
858                 safestrncpy(u->url_key, name, sizeof(u->url_key));
859                 u->url_data = malloc(length + 1);
860                 memcpy(u->url_data, content, length);
861                 u->url_data[length] = 0;
862         }
863
864         /** Uploaded files */
865         if ( (length > 0) && (strlen(cbtype) > 0) ) {
866                 WC->upload = malloc(length);
867                 if (WC->upload != NULL) {
868                         WC->upload_length = length;
869                         safestrncpy(WC->upload_filename, filename,
870                                         sizeof(WC->upload_filename));
871                         safestrncpy(WC->upload_content_type, cbtype,
872                                         sizeof(WC->upload_content_type));
873                         memcpy(WC->upload, content, length);
874                 }
875                 else {
876                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
877                 }
878         }
879
880 }
881
882 /**
883  * \brief Convenience functions to wrap around asynchronous ajax responses
884  */
885 void begin_ajax_response(void) {
886         output_headers(0, 0, 0, 0, 0, 0);
887
888         wprintf("Content-type: text/html; charset=UTF-8\r\n"
889                 "Server: %s\r\n"
890                 "Connection: close\r\n"
891                 "Pragma: no-cache\r\n"
892                 "Cache-Control: no-cache\r\n",
893                 SERVER);
894         begin_burst();
895 }
896
897 /**
898  * \brief print ajax response footer 
899  */
900 void end_ajax_response(void) {
901         wprintf("\r\n");
902         wDumpContent(0);
903 }
904
905 /**
906  * \brief Wraps a Citadel server command in an AJAX transaction.
907  */
908 void ajax_servcmd(void)
909 {
910         char buf[1024];
911         char gcontent[1024];
912         char *junk;
913         size_t len;
914
915         begin_ajax_response();
916
917         serv_printf("%s", bstr("g_cmd"));
918         serv_getln(buf, sizeof buf);
919         wprintf("%s\n", buf);
920
921         if (buf[0] == '8') {
922                 serv_printf("\n\n000");
923         }
924         if ((buf[0] == '1') || (buf[0] == '8')) {
925                 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
926                         wprintf("%s\n", gcontent);
927                 }
928                 wprintf("000");
929         }
930         if (buf[0] == '4') {
931                 text_to_server(bstr("g_input"));
932                 serv_puts("000");
933         }
934         if (buf[0] == '6') {
935                 len = atol(&buf[4]);
936                 junk = malloc(len);
937                 serv_read(junk, len);
938                 free(junk);
939         }
940         if (buf[0] == '7') {
941                 len = atol(&buf[4]);
942                 junk = malloc(len);
943                 memset(junk, 0, len);
944                 serv_write(junk, len);
945                 free(junk);
946         }
947
948         end_ajax_response();
949         
950         /**
951          * This is kind of an ugly hack, but this is the only place it can go.
952          * If the command was GEXP, then the instant messenger window must be
953          * running, so reset the "last_pager_check" watchdog timer so
954          * that page_popup() doesn't try to open it a second time.
955          */
956         if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
957                 WC->last_pager_check = time(NULL);
958         }
959 }
960
961
962 /**
963  * \brief Helper function for the asynchronous check to see if we need
964  * to open the instant messenger window.
965  */
966 void seconds_since_last_gexp(void)
967 {
968         char buf[256];
969
970         begin_ajax_response();
971         if ( (time(NULL) - WC->last_pager_check) < 30) {
972                 wprintf("NO\n");
973         }
974         else {
975                 serv_puts("NOOP");
976                 serv_getln(buf, sizeof buf);
977                 if (buf[3] == '*') {
978                         wprintf("YES");
979                 }
980                 else {
981                         wprintf("NO");
982                 }
983         }
984         end_ajax_response();
985 }
986
987
988
989
990 /**
991  * \brief Entry point for WebCit transaction
992  */
993 void session_loop(struct httprequest *req)
994 {
995         char cmd[1024];
996         char action[1024];
997         char arg[8][128];
998         size_t sizes[10];
999         char *index[10];
1000         char buf[SIZ];
1001         char request_method[128];
1002         char pathname[1024];
1003         int a, b, nBackDots, nEmpty;
1004         int ContentLength = 0;
1005         int BytesRead = 0;
1006         char ContentType[512];
1007         char *content = NULL;
1008         char *content_end = NULL;
1009         struct httprequest *hptr;
1010         char browser_host[256];
1011         char user_agent[256];
1012         int body_start = 0;
1013         int is_static = 0;
1014         int n_static = 0;
1015         /**
1016          * We stuff these with the values coming from the client cookies,
1017          * so we can use them to reconnect a timed out session if we have to.
1018          */
1019         char c_username[SIZ];
1020         char c_password[SIZ];
1021         char c_roomname[SIZ];
1022         char c_httpauth_string[SIZ];
1023         char c_httpauth_user[SIZ];
1024         char c_httpauth_pass[SIZ];
1025         char cookie[SIZ];
1026
1027         safestrncpy(c_username, "", sizeof c_username);
1028         safestrncpy(c_password, "", sizeof c_password);
1029         safestrncpy(c_roomname, "", sizeof c_roomname);
1030         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1031         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1032         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1033         strcpy(browser_host, "");
1034
1035         WC->upload_length = 0;
1036         WC->upload = NULL;
1037         WC->vars = NULL;
1038         WC->is_wap = 0;
1039
1040         hptr = req;
1041         if (hptr == NULL) return;
1042
1043         safestrncpy(cmd, hptr->line, sizeof cmd);
1044         hptr = hptr->next;
1045         extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1046         extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1047
1048         /** Figure out the action */
1049         index[0] = action;
1050         sizes[0] = sizeof action;
1051         for (a=1; a<9; a++)
1052         {
1053                 index[a] = arg[a-1];
1054                 sizes[a] = sizeof arg[a-1];
1055         }
1056 ////    index[9] = &foo; todo
1057         nBackDots = 0;
1058         nEmpty = 0;
1059         for ( a = 0; a < 9; ++a)
1060         {
1061                 extract_token(index[a], pathname, a + 1, '/', sizes[a]);
1062                 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
1063                 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
1064                 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
1065                 if ((index[a][0] == '.') && (index[a][1] == '.'))
1066                         nBackDots++;
1067                 if (index[a][0] == '\0')
1068                         nEmpty++;
1069         }
1070
1071         while (hptr != NULL) {
1072                 safestrncpy(buf, hptr->line, sizeof buf);
1073                 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1074                 hptr = hptr->next;
1075
1076                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1077                         safestrncpy(cookie, &buf[15], sizeof cookie);
1078                         cookie_to_stuff(cookie, NULL,
1079                                         c_username, sizeof c_username,
1080                                         c_password, sizeof c_password,
1081                                         c_roomname, sizeof c_roomname);
1082                 }
1083                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1084                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1085                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1086                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1087                 }
1088                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1089                         ContentLength = atoi(&buf[16]);
1090                 }
1091                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1092                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
1093                 }
1094                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1095                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
1096                 }
1097                 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1098                         if (follow_xff) {
1099                                 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1100                         }
1101                 }
1102                 else if (!strncasecmp(buf, "Host: ", 6)) {
1103                         if (strlen(WC->http_host) == 0) {
1104                                 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1105                         }
1106                 }
1107                 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1108                         safestrncpy(browser_host, &buf[17], sizeof browser_host);
1109                         while (num_tokens(browser_host, ',') > 1) {
1110                                 remove_token(browser_host, 0, ',');
1111                         }
1112                         striplt(browser_host);
1113                 }
1114                 /** Only WAP gateways explicitly name this content-type */
1115                 else if (strstr(buf, "text/vnd.wap.wml")) {
1116                         WC->is_wap = 1;
1117                 }
1118         }
1119
1120         if (ContentLength > 0) {
1121                 content = malloc(ContentLength + SIZ);
1122                 memset(content, 0, ContentLength + SIZ);
1123                 sprintf(content, "Content-type: %s\n"
1124                                 "Content-length: %d\n\n",
1125                                 ContentType, ContentLength);
1126                 body_start = strlen(content);
1127
1128                 /** Read the entire input data at once. */
1129                 client_read(WC->http_sock, &content[BytesRead+body_start], ContentLength);
1130
1131                 if (!strncasecmp(ContentType, "application/x-www-form-urlencoded", 33)) {
1132                         addurls(&content[body_start]);
1133                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1134                         content_end = content + ContentLength + body_start;
1135                         mime_parser(content, content_end, *upload_handler, NULL, NULL, NULL, 0);
1136                 }
1137         } else {
1138                 content = NULL;
1139         }
1140
1141         /** make a note of where we are in case the user wants to save it */
1142         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1143         remove_token(WC->this_page, 2, ' ');
1144         remove_token(WC->this_page, 0, ' ');
1145
1146         /** If there are variables in the URL, we must grab them now */
1147         for (a = 0; a < strlen(cmd); ++a) {
1148                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1149                         for (b = a; b < strlen(cmd); ++b)
1150                                 if (isspace(cmd[b]))
1151                                         cmd[b] = 0;
1152                         addurls(&cmd[a + 1]);
1153                         cmd[a] = 0;
1154                 }
1155         }
1156
1157         /** If it's a "force 404" situation then display the error and bail. */
1158         if (!strcmp(action, "404")) {
1159                 wprintf("HTTP/1.1 404 Not found\r\n");
1160                 wprintf("Content-Type: text/plain\r\n");
1161                 wprintf("\r\n");
1162                 wprintf("Not found\r\n");
1163                 goto SKIP_ALL_THIS_CRAP;
1164         }
1165
1166         /** Static content can be sent without connecting to Citadel. */
1167         is_static = 0;
1168         for (a=0; a<ndirs; ++a) {
1169                 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1170                         is_static = 1;
1171                         n_static = a;
1172                 }
1173         }
1174         if (is_static) {
1175                 if (nBackDots < 2)
1176                 {
1177                         snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1178                                  static_dirs[n_static], 
1179                                  index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
1180                         for (a=0; a<8; ++a) {
1181                                 if (buf[strlen(buf)-1] == '/') {
1182                                         buf[strlen(buf)-1] = 0;
1183                                 }
1184                         }
1185                         for (a = 0; a < strlen(buf); ++a) {
1186                                 if (isspace(buf[a])) {
1187                                         buf[a] = 0;
1188                                 }
1189                         }
1190                         output_static(buf);
1191                 }
1192                 else 
1193                 {
1194                         lprintf(9, "Suspicious request. Ignoring.");
1195                         wprintf("HTTP/1.1 404 Not found. Don't try to Trick me DUDE!\r\n");
1196                         wprintf("Content-Type: text/plain\r\n");
1197                         wprintf("\r\n");
1198                         wprintf("Not found. Don't play games on me!\r\n");
1199                 }
1200                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
1201         }
1202
1203         /**
1204          * If we're not connected to a Citadel server, try to hook up the
1205          * connection now.
1206          */
1207         if (!WC->connected) {
1208                 if (!strcasecmp(ctdlhost, "uds")) {
1209                         /* unix domain socket */
1210                         sprintf(buf, "%s/citadel.socket", ctdlport);
1211                         WC->serv_sock = uds_connectsock(buf);
1212                 }
1213                 else {
1214                         /* tcp socket */
1215                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1216                 }
1217
1218                 if (WC->serv_sock < 0) {
1219                         do_logout();
1220                         goto SKIP_ALL_THIS_CRAP;
1221                 }
1222                 else {
1223                         WC->connected = 1;
1224                         serv_getln(buf, sizeof buf);    /** get the server welcome message */
1225
1226                         /**
1227                          * From what host is our user connecting?  Go with
1228                          * the host at the other end of the HTTP socket,
1229                          * unless we are following X-Forwarded-For: headers
1230                          * and such a header has already turned up something.
1231                          */
1232                         if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1233                                 locate_host(browser_host, WC->http_sock);
1234                         }
1235
1236                         get_serv_info(browser_host, user_agent);
1237                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1238                                 wprintf(_("You are connected to a Citadel "
1239                                         "server running Citadel %d.%02d. \n"
1240                                         "In order to run this version of WebCit "
1241                                         "you must also have Citadel %d.%02d or"
1242                                         " newer.\n\n\n"),
1243                                                 serv_info.serv_rev_level / 100,
1244                                                 serv_info.serv_rev_level % 100,
1245                                                 MINIMUM_CIT_VERSION / 100,
1246                                                 MINIMUM_CIT_VERSION % 100
1247                                         );
1248                                 end_webcit_session();
1249                                 goto SKIP_ALL_THIS_CRAP;
1250                         }
1251                 }
1252         }
1253
1254         /**
1255          * Functions which can be performed without logging in
1256          */
1257         if (!strcasecmp(action, "listsub")) {
1258                 do_listsub();
1259                 goto SKIP_ALL_THIS_CRAP;
1260         }
1261 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1262         if (!strcasecmp(action, "freebusy")) {
1263                 do_freebusy(cmd);
1264                 goto SKIP_ALL_THIS_CRAP;
1265         }
1266 #endif
1267
1268         /**
1269          * If we're not logged in, but we have HTTP Authentication data,
1270          * try logging in to Citadel using that.
1271          */
1272         if ((!WC->logged_in)
1273            && (strlen(c_httpauth_user) > 0)
1274            && (strlen(c_httpauth_pass) > 0)) {
1275                 serv_printf("USER %s", c_httpauth_user);
1276                 serv_getln(buf, sizeof buf);
1277                 if (buf[0] == '3') {
1278                         serv_printf("PASS %s", c_httpauth_pass);
1279                         serv_getln(buf, sizeof buf);
1280                         if (buf[0] == '2') {
1281                                 become_logged_in(c_httpauth_user,
1282                                                 c_httpauth_pass, buf);
1283                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1284                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1285                         } else {
1286                                 /** Should only display when password is wrong */
1287                                 authorization_required(&buf[4]);
1288                                 goto SKIP_ALL_THIS_CRAP;
1289                         }
1290                 }
1291         }
1292
1293         /** This needs to run early */
1294         if (!strcasecmp(action, "rss")) {
1295                 display_rss(bstr("room"), request_method);
1296                 goto SKIP_ALL_THIS_CRAP;
1297         }
1298
1299         /** 
1300          * The GroupDAV stuff relies on HTTP authentication instead of
1301          * our session's authentication.
1302          */
1303         if (!strncasecmp(action, "groupdav", 8)) {
1304                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1305                         ContentLength, content+body_start);
1306                 if (!WC->logged_in) {
1307                         WC->killthis = 1;       /* If not logged in, don't */
1308                 }                               /* keep the session active */
1309                 goto SKIP_ALL_THIS_CRAP;
1310         }
1311
1312
1313         /**
1314          * Automatically send requests with any method other than GET or
1315          * POST to the GroupDAV code as well.
1316          */
1317         if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1318                 groupdav_main(req, ContentType, /** do GroupDAV methods */
1319                         ContentLength, content+body_start);
1320                 if (!WC->logged_in) {
1321                         WC->killthis = 1;       /** If not logged in, don't */
1322                 }                               /** keep the session active */
1323                 goto SKIP_ALL_THIS_CRAP;
1324         }
1325
1326         /**
1327          * If we're not logged in, but we have username and password cookies
1328          * supplied by the browser, try using them to log in.
1329          */
1330         if ((!WC->logged_in)
1331            && (strlen(c_username) > 0)
1332            && (strlen(c_password) > 0)) {
1333                 serv_printf("USER %s", c_username);
1334                 serv_getln(buf, sizeof buf);
1335                 if (buf[0] == '3') {
1336                         serv_printf("PASS %s", c_password);
1337                         serv_getln(buf, sizeof buf);
1338                         if (buf[0] == '2') {
1339                                 become_logged_in(c_username, c_password, buf);
1340                         }
1341                 }
1342         }
1343         /**
1344          * If we don't have a current room, but a cookie specifying the
1345          * current room is supplied, make an effort to go there.
1346          */
1347         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1348                 serv_printf("GOTO %s", c_roomname);
1349                 serv_getln(buf, sizeof buf);
1350                 if (buf[0] == '2') {
1351                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1352                 }
1353         }
1354
1355         if (!strcasecmp(action, "image")) {
1356                 output_image();
1357
1358                 /**
1359                  * All functions handled below this point ... make sure we log in
1360                  * before doing anything else!
1361                  */
1362         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1363                 do_login();
1364         } else if (!WC->logged_in) {
1365                 display_login(NULL);
1366         }
1367
1368         /**
1369          * Various commands...
1370          */
1371
1372         else if (!strcasecmp(action, "do_welcome")) {
1373                 do_welcome();
1374         } else if (!strcasecmp(action, "blank")) {
1375                 blank_page();
1376         } else if (!strcasecmp(action, "do_template")) {
1377                 url_do_template();
1378         } else if (!strcasecmp(action, "display_aide_menu")) {
1379                 display_aide_menu();
1380         } else if (!strcasecmp(action, "display_main_menu")) {
1381                 display_main_menu();
1382         } else if (!strcasecmp(action, "who")) {
1383                 who();
1384         } else if (!strcasecmp(action, "sslg")) {
1385                 seconds_since_last_gexp();
1386         } else if (!strcasecmp(action, "who_inner_html")) {
1387                 begin_ajax_response();
1388                 who_inner_div();
1389                 end_ajax_response();
1390         } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1391                 begin_ajax_response();
1392                 do_iconbar();
1393                 end_ajax_response();
1394         } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1395                 begin_ajax_response();
1396                 do_iconbar_roomlist();
1397                 end_ajax_response();
1398         } else if (!strcasecmp(action, "knrooms")) {
1399                 knrooms();
1400         } else if (!strcasecmp(action, "gotonext")) {
1401                 slrp_highest();
1402                 gotonext();
1403         } else if (!strcasecmp(action, "skip")) {
1404                 gotonext();
1405         } else if (!strcasecmp(action, "ungoto")) {
1406                 ungoto();
1407         } else if (!strcasecmp(action, "dotgoto")) {
1408                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1409                         slrp_highest();
1410                 }
1411                 smart_goto(bstr("room"));
1412         } else if (!strcasecmp(action, "dotskip")) {
1413                 smart_goto(bstr("room"));
1414         } else if (!strcasecmp(action, "termquit")) {
1415                 do_logout();
1416         } else if (!strcasecmp(action, "readnew")) {
1417                 readloop("readnew");
1418         } else if (!strcasecmp(action, "readold")) {
1419                 readloop("readold");
1420         } else if (!strcasecmp(action, "readfwd")) {
1421                 readloop("readfwd");
1422         } else if (!strcasecmp(action, "headers")) {
1423                 readloop("headers");
1424         } else if (!strcasecmp(action, "do_search")) {
1425                 readloop("do_search");
1426         } else if (!strcasecmp(action, "msg")) {
1427                 embed_message(index[1]);
1428         } else if (!strcasecmp(action, "printmsg")) {
1429                 print_message(index[1]);
1430         } else if (!strcasecmp(action, "msgheaders")) {
1431                 display_headers(index[1]);
1432         } else if (!strcasecmp(action, "wiki")) {
1433                 display_wiki_page();
1434         } else if (!strcasecmp(action, "display_enter")) {
1435                 display_enter();
1436         } else if (!strcasecmp(action, "post")) {
1437                 post_message();
1438         } else if (!strcasecmp(action, "move_msg")) {
1439                 move_msg();
1440         } else if (!strcasecmp(action, "delete_msg")) {
1441                 delete_msg();
1442         } else if (!strcasecmp(action, "userlist")) {
1443                 userlist();
1444         } else if (!strcasecmp(action, "showuser")) {
1445                 showuser();
1446         } else if (!strcasecmp(action, "display_page")) {
1447                 display_page();
1448         } else if (!strcasecmp(action, "page_user")) {
1449                 page_user();
1450         } else if (!strcasecmp(action, "chat")) {
1451                 do_chat();
1452         } else if (!strcasecmp(action, "display_private")) {
1453                 display_private("", 0);
1454         } else if (!strcasecmp(action, "goto_private")) {
1455                 goto_private();
1456         } else if (!strcasecmp(action, "zapped_list")) {
1457                 zapped_list();
1458         } else if (!strcasecmp(action, "display_zap")) {
1459                 display_zap();
1460         } else if (!strcasecmp(action, "zap")) {
1461                 zap();
1462         } else if (!strcasecmp(action, "display_entroom")) {
1463                 display_entroom();
1464         } else if (!strcasecmp(action, "entroom")) {
1465                 entroom();
1466         } else if (!strcasecmp(action, "display_whok")) {
1467                 display_whok();
1468         } else if (!strcasecmp(action, "do_invt_kick")) {
1469                 do_invt_kick();
1470         } else if (!strcasecmp(action, "display_editroom")) {
1471                 display_editroom();
1472         } else if (!strcasecmp(action, "netedit")) {
1473                 netedit();
1474         } else if (!strcasecmp(action, "editroom")) {
1475                 editroom();
1476         } else if (!strcasecmp(action, "display_editinfo")) {
1477                 display_edit(_("Room info"), "EINF 0", "RINF", "editinfo", 1);
1478         } else if (!strcasecmp(action, "editinfo")) {
1479                 save_edit(_("Room info"), "EINF 1", 1);
1480         } else if (!strcasecmp(action, "display_editbio")) {
1481                 sprintf(buf, "RBIO %s", WC->wc_fullname);
1482                 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1483         } else if (!strcasecmp(action, "editbio")) {
1484                 save_edit(_("Your bio"), "EBIO", 0);
1485         } else if (!strcasecmp(action, "confirm_move_msg")) {
1486                 confirm_move_msg();
1487         } else if (!strcasecmp(action, "delete_room")) {
1488                 delete_room();
1489         } else if (!strcasecmp(action, "validate")) {
1490                 validate();
1491         } else if (!strcasecmp(action, "display_editpic")) {
1492                 display_graphics_upload(_("your photo"),
1493                                         "UIMG 0|_userpic_",
1494                                         "editpic");
1495         } else if (!strcasecmp(action, "editpic")) {
1496                 do_graphics_upload("UIMG 1|_userpic_");
1497         } else if (!strcasecmp(action, "display_editroompic")) {
1498                 display_graphics_upload(_("the icon for this room"),
1499                                         "UIMG 0|_roompic_",
1500                                         "editroompic");
1501         } else if (!strcasecmp(action, "editroompic")) {
1502                 do_graphics_upload("UIMG 1|_roompic_");
1503         } else if (!strcasecmp(action, "delete_floor")) {
1504                 delete_floor();
1505         } else if (!strcasecmp(action, "rename_floor")) {
1506                 rename_floor();
1507         } else if (!strcasecmp(action, "create_floor")) {
1508                 create_floor();
1509         } else if (!strcasecmp(action, "display_editfloorpic")) {
1510                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1511                         bstr("which_floor"));
1512                 display_graphics_upload(_("the icon for this floor"),
1513                                         buf,
1514                                         "editfloorpic");
1515         } else if (!strcasecmp(action, "editfloorpic")) {
1516                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1517                         bstr("which_floor"));
1518                 do_graphics_upload(buf);
1519         } else if (!strcasecmp(action, "display_reg")) {
1520                 display_reg(0);
1521         } else if (!strcasecmp(action, "display_changepw")) {
1522                 display_changepw();
1523         } else if (!strcasecmp(action, "changepw")) {
1524                 changepw();
1525         } else if (!strcasecmp(action, "display_edit_node")) {
1526                 display_edit_node();
1527         } else if (!strcasecmp(action, "edit_node")) {
1528                 edit_node();
1529         } else if (!strcasecmp(action, "display_netconf")) {
1530                 display_netconf();
1531         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1532                 display_confirm_delete_node();
1533         } else if (!strcasecmp(action, "delete_node")) {
1534                 delete_node();
1535         } else if (!strcasecmp(action, "display_add_node")) {
1536                 display_add_node();
1537         } else if (!strcasecmp(action, "add_node")) {
1538                 add_node();
1539         } else if (!strcasecmp(action, "terminate_session")) {
1540                 slrp_highest();
1541                 terminate_session();
1542         } else if (!strcasecmp(action, "edit_me")) {
1543                 edit_me();
1544         } else if (!strcasecmp(action, "display_siteconfig")) {
1545                 display_siteconfig();
1546         } else if (!strcasecmp(action, "chat_recv")) {
1547                 chat_recv();
1548         } else if (!strcasecmp(action, "chat_send")) {
1549                 chat_send();
1550         } else if (!strcasecmp(action, "siteconfig")) {
1551                 siteconfig();
1552         } else if (!strcasecmp(action, "display_generic")) {
1553                 display_generic();
1554         } else if (!strcasecmp(action, "do_generic")) {
1555                 do_generic();
1556         } else if (!strcasecmp(action, "ajax_servcmd")) {
1557                 ajax_servcmd();
1558         } else if (!strcasecmp(action, "display_menubar")) {
1559                 display_menubar(1);
1560         } else if (!strcasecmp(action, "mimepart")) {
1561                 mimepart(index[1], index[2], 0);
1562         } else if (!strcasecmp(action, "mimepart_download")) {
1563                 mimepart(index[1], index[2], 1);
1564         } else if (!strcasecmp(action, "edit_vcard")) {
1565                 edit_vcard();
1566         } else if (!strcasecmp(action, "submit_vcard")) {
1567                 submit_vcard();
1568         } else if (!strcasecmp(action, "select_user_to_edit")) {
1569                 select_user_to_edit(NULL, NULL);
1570         } else if (!strcasecmp(action, "display_edituser")) {
1571                 display_edituser(NULL, 0);
1572         } else if (!strcasecmp(action, "edituser")) {
1573                 edituser();
1574         } else if (!strcasecmp(action, "create_user")) {
1575                 create_user();
1576         } else if (!strcasecmp(action, "changeview")) {
1577                 change_view();
1578         } else if (!strcasecmp(action, "change_start_page")) {
1579                 change_start_page();
1580         } else if (!strcasecmp(action, "display_floorconfig")) {
1581                 display_floorconfig(NULL);
1582         } else if (!strcasecmp(action, "toggle_self_service")) {
1583                 toggle_self_service();
1584 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1585         } else if (!strcasecmp(action, "display_edit_task")) {
1586                 display_edit_task();
1587         } else if (!strcasecmp(action, "save_task")) {
1588                 save_task();
1589         } else if (!strcasecmp(action, "display_edit_event")) {
1590                 display_edit_event();
1591         } else if (!strcasecmp(action, "save_event")) {
1592                 save_event();
1593         } else if (!strcasecmp(action, "respond_to_request")) {
1594                 respond_to_request();
1595         } else if (!strcasecmp(action, "handle_rsvp")) {
1596                 handle_rsvp();
1597 #endif
1598         } else if (!strcasecmp(action, "summary")) {
1599                 summary();
1600         } else if (!strcasecmp(action, "summary_inner_div")) {
1601                 begin_ajax_response();
1602                 summary_inner_div();
1603                 end_ajax_response();
1604         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1605                 display_customize_iconbar();
1606         } else if (!strcasecmp(action, "commit_iconbar")) {
1607                 commit_iconbar();
1608         } else if (!strcasecmp(action, "set_room_policy")) {
1609                 set_room_policy();
1610         } else if (!strcasecmp(action, "display_inetconf")) {
1611                 display_inetconf();
1612         } else if (!strcasecmp(action, "save_inetconf")) {
1613                 save_inetconf();
1614         } else if (!strcasecmp(action, "display_smtpqueue")) {
1615                 display_smtpqueue();
1616         } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
1617                 display_smtpqueue_inner_div();
1618         } else if (!strcasecmp(action, "display_sieve")) {
1619                 display_sieve();
1620         } else if (!strcasecmp(action, "save_sieve")) {
1621                 save_sieve();
1622         } else if (!strcasecmp(action, "display_add_remove_scripts")) {
1623                 display_add_remove_scripts(NULL);
1624         } else if (!strcasecmp(action, "create_script")) {
1625                 create_script();
1626         } else if (!strcasecmp(action, "delete_script")) {
1627                 delete_script();
1628         } else if (!strcasecmp(action, "setup_wizard")) {
1629                 do_setup_wizard();
1630         } else if (!strcasecmp(action, "display_preferences")) {
1631                 display_preferences();
1632         } else if (!strcasecmp(action, "set_preferences")) {
1633                 set_preferences();
1634         } else if (!strcasecmp(action, "recp_autocomplete")) {
1635                 recp_autocomplete(bstr("recp"));
1636         } else if (!strcasecmp(action, "cc_autocomplete")) {
1637                 recp_autocomplete(bstr("cc"));
1638         } else if (!strcasecmp(action, "bcc_autocomplete")) {
1639                 recp_autocomplete(bstr("bcc"));
1640         } else if (!strcasecmp(action, "display_address_book_inner_div")) {
1641                 display_address_book_inner_div();
1642         } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1643                 set_floordiv_expanded(index[1]);
1644         } else if (!strcasecmp(action, "diagnostics")) {
1645                 output_headers(1, 1, 1, 0, 0, 0);
1646                 wprintf("Session: %d<hr />\n", WC->wc_session);
1647                 wprintf("Command: <br /><PRE>\n");
1648                 escputs(cmd);
1649                 wprintf("</PRE><hr />\n");
1650                 wprintf("Variables: <br /><PRE>\n");
1651                 dump_vars();
1652                 wprintf("</PRE><hr />\n");
1653                 wDumpContent(1);
1654         } else if (!strcasecmp(action, "updatenote")) {
1655                 updatenote();
1656         } else if (!strcasecmp(action, "display_room_directory")) {
1657                 display_room_directory();
1658         } else if (!strcasecmp(action, "download_file")) {
1659                 download_file(index[1]);
1660         } else if (!strcasecmp(action, "upload_file")) {
1661                 upload_file();
1662         }
1663
1664         /** When all else fais, display the main menu. */
1665         else {
1666                 display_main_menu();
1667         }
1668
1669 SKIP_ALL_THIS_CRAP:
1670         fflush(stdout);
1671         if (content != NULL) {
1672                 free(content);
1673                 content = NULL;
1674         }
1675         free_urls();
1676         if (WC->upload_length > 0) {
1677                 free(WC->upload);
1678                 WC->upload_length = 0;
1679         }
1680 }
1681
1682
1683 /*@}*/