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