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