Added sieve.c - sieve config screen will go here.
[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("DLAT %ld|%s", msgnum, partnum);
696         serv_getln(buf, sizeof buf);
697         if (buf[0] == '6') {
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                 serv_read(content, bytes);
703
704                 content[bytes] = 0;     /* null terminate for good measure */
705                 return(content);
706         }
707         else {
708                 return(NULL);
709         }
710
711 }
712
713
714 /**
715  * \brief Convenience functions to display a page containing only a string
716  * \param titlebarcolor color of the titlebar of the frame
717  * \param titlebarmsg text to display in the title bar
718  * \param messagetext body of the box
719  */
720 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
721 {
722         wprintf("HTTP/1.1 200 OK\n");
723         output_headers(1, 1, 2, 0, 0, 0);
724         wprintf("<div id=\"banner\">\n");
725         wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
726         wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
727         wprintf("</td></tr></table>\n");
728         wprintf("</div>\n<div id=\"content\">\n");
729         escputs(messagetext);
730
731         wprintf("<hr />\n");
732         wDumpContent(1);
733 }
734
735
736 /**
737  * \brief Display a blank page.
738  */
739 void blank_page(void) {
740         output_headers(1, 1, 0, 0, 0, 0);
741         wDumpContent(2);
742 }
743
744
745 /**
746  * \brief A template has been requested
747  */
748 void url_do_template(void) {
749         do_template(bstr("template"));
750 }
751
752
753
754 /**
755  * \brief Offer to make any page the user's "start page."
756  */
757 void offer_start_page(void) {
758         wprintf("<a href=\"change_start_page?startpage=");
759         urlescputs(WC->this_page);
760         wprintf("\"><font size=-2 color=\"#AAAAAA\">");
761         wprintf(_("Make this my start page"));
762         wprintf("</font></a>");
763 /*
764         wprintf("<br/><a href=\"rss?room=");
765         urlescputs(WC->wc_roomname);
766         wprintf("\" title=\"RSS 2.0 feed for ");
767         escputs(WC->wc_roomname);
768         wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
769 */
770 }
771
772
773 /**
774  * \brief Change the user's start page
775  */
776 void change_start_page(void) {
777
778         if (bstr("startpage") == NULL) {
779                 safestrncpy(WC->ImportantMessage,
780                         _("You no longer have a start page selected."),
781                         sizeof WC->ImportantMessage);
782                 display_main_menu();
783                 return;
784         }
785
786         set_preference("startpage", bstr("startpage"), 1);
787
788         output_headers(1, 1, 0, 0, 0, 0);
789         do_template("newstartpage");
790         wDumpContent(1);
791 }
792
793
794
795 /**
796  * \brief convenience function to indicate success
797  * \param successmessage the mesage itself
798  */
799 void display_success(char *successmessage)
800 {
801         convenience_page("007700", "OK", successmessage);
802 }
803
804
805 /**
806  * \brief Authorization required page 
807  * This is probably temporary and should be revisited 
808  * \param message message to put in header
809 */
810 void authorization_required(const char *message)
811 {
812         wprintf("HTTP/1.1 401 Authorization Required\r\n");
813         wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
814         wprintf("Content-Type: text/html\r\n\r\n");
815         wprintf("<h1>");
816         wprintf(_("Authorization Required"));
817         wprintf("</h1>\r\n");
818         wprintf(_("The resource you requested requires a valid username and password. "
819                 "You could not be logged in: %s\n"), message);
820         wDumpContent(0);
821 }
822
823 /**
824  * \brief This function is called by the MIME parser to handle data uploaded by
825  *        the browser.  Form data, uploaded files, and the data from HTTP PUT
826  *        operations (such as those found in GroupDAV) all arrive this way.
827  *
828  * \param name Name of the item being uploaded
829  * \param filename Filename of the item being uploaded
830  * \param partnum MIME part identifier (not needed)
831  * \param disp MIME content disposition (not needed)
832  * \param content The actual data
833  * \param cbtype MIME content-type
834  * \param cbcharset Character set
835  * \param length Content length
836  * \param encoding MIME encoding type (not needed)
837  * \param userdata Not used here
838  */
839 void upload_handler(char *name, char *filename, char *partnum, char *disp,
840                         void *content, char *cbtype, char *cbcharset,
841                         size_t length, char *encoding, void *userdata)
842 {
843         struct urlcontent *u;
844
845         /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
846                 name, cbtype, length); */
847
848         /* Form fields */
849         if ( (length > 0) && (strlen(cbtype) == 0) ) {
850                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
851                 u->next = WC->urlstrings;
852                 WC->urlstrings = u;
853                 safestrncpy(u->url_key, name, sizeof(u->url_key));
854                 u->url_data = malloc(length + 1);
855                 memcpy(u->url_data, content, length);
856                 u->url_data[length] = 0;
857         }
858
859         /** Uploaded files */
860         if ( (length > 0) && (strlen(cbtype) > 0) ) {
861                 WC->upload = malloc(length);
862                 if (WC->upload != NULL) {
863                         WC->upload_length = length;
864                         safestrncpy(WC->upload_filename, filename,
865                                         sizeof(WC->upload_filename));
866                         safestrncpy(WC->upload_content_type, cbtype,
867                                         sizeof(WC->upload_content_type));
868                         memcpy(WC->upload, content, length);
869                 }
870                 else {
871                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
872                 }
873         }
874
875 }
876
877 /**
878  * \brief Convenience functions to wrap around asynchronous ajax responses
879  */
880 void begin_ajax_response(void) {
881         output_headers(0, 0, 0, 0, 0, 0);
882
883         wprintf("Content-type: text/html; charset=UTF-8\r\n"
884                 "Server: %s\r\n"
885                 "Connection: close\r\n"
886                 "Pragma: no-cache\r\n"
887                 "Cache-Control: no-cache\r\n",
888                 SERVER);
889         begin_burst();
890 }
891
892 /**
893  * \brief print ajax response footer 
894  */
895 void end_ajax_response(void) {
896         wprintf("\r\n");
897         wDumpContent(0);
898 }
899
900 /**
901  * \brief Wraps a Citadel server command in an AJAX transaction.
902  */
903 void ajax_servcmd(void)
904 {
905         char buf[1024];
906         char gcontent[1024];
907         char *junk;
908         size_t len;
909
910         begin_ajax_response();
911
912         serv_printf("%s", bstr("g_cmd"));
913         serv_getln(buf, sizeof buf);
914         wprintf("%s\n", buf);
915
916         if (buf[0] == '8') {
917                 serv_printf("\n\n000");
918         }
919         if ((buf[0] == '1') || (buf[0] == '8')) {
920                 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
921                         wprintf("%s\n", gcontent);
922                 }
923                 wprintf("000");
924         }
925         if (buf[0] == '4') {
926                 text_to_server(bstr("g_input"));
927                 serv_puts("000");
928         }
929         if (buf[0] == '6') {
930                 len = atol(&buf[4]);
931                 junk = malloc(len);
932                 serv_read(junk, len);
933                 free(junk);
934         }
935         if (buf[0] == '7') {
936                 len = atol(&buf[4]);
937                 junk = malloc(len);
938                 memset(junk, 0, len);
939                 serv_write(junk, len);
940                 free(junk);
941         }
942
943         end_ajax_response();
944         
945         /**
946          * This is kind of an ugly hack, but this is the only place it can go.
947          * If the command was GEXP, then the instant messenger window must be
948          * running, so reset the "last_pager_check" watchdog timer so
949          * that page_popup() doesn't try to open it a second time.
950          */
951         if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
952                 WC->last_pager_check = time(NULL);
953         }
954 }
955
956
957 /**
958  * \brief Helper function for the asynchronous check to see if we need
959  * to open the instant messenger window.
960  */
961 void seconds_since_last_gexp(void)
962 {
963         char buf[256];
964
965         begin_ajax_response();
966         if ( (time(NULL) - WC->last_pager_check) < 30) {
967                 wprintf("NO\n");
968         }
969         else {
970                 serv_puts("NOOP");
971                 serv_getln(buf, sizeof buf);
972                 if (buf[3] == '*') {
973                         wprintf("YES");
974                 }
975                 else {
976                         wprintf("NO");
977                 }
978         }
979         end_ajax_response();
980 }
981
982
983
984
985 /**
986  * \brief Entry point for WebCit transaction
987  */
988 void session_loop(struct httprequest *req)
989 {
990         char cmd[1024];
991         char action[1024];
992         char arg1[128];
993         char arg2[128];
994         char arg3[128];
995         char arg4[128];
996         char arg5[128];
997         char arg6[128];
998         char arg7[128];
999         char buf[SIZ];
1000         char request_method[128];
1001         char pathname[1024];
1002         int a, b;
1003         int ContentLength = 0;
1004         int BytesRead = 0;
1005         char ContentType[512];
1006         char *content = NULL;
1007         char *content_end = NULL;
1008         struct httprequest *hptr;
1009         char browser_host[256];
1010         char user_agent[256];
1011         int body_start = 0;
1012         int is_static = 0;
1013
1014         /**
1015          * We stuff these with the values coming from the client cookies,
1016          * so we can use them to reconnect a timed out session if we have to.
1017          */
1018         char c_username[SIZ];
1019         char c_password[SIZ];
1020         char c_roomname[SIZ];
1021         char c_httpauth_string[SIZ];
1022         char c_httpauth_user[SIZ];
1023         char c_httpauth_pass[SIZ];
1024         char cookie[SIZ];
1025
1026         safestrncpy(c_username, "", sizeof c_username);
1027         safestrncpy(c_password, "", sizeof c_password);
1028         safestrncpy(c_roomname, "", sizeof c_roomname);
1029         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1030         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1031         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1032         strcpy(browser_host, "");
1033
1034         WC->upload_length = 0;
1035         WC->upload = NULL;
1036         WC->vars = NULL;
1037         WC->is_wap = 0;
1038
1039         hptr = req;
1040         if (hptr == NULL) return;
1041
1042         safestrncpy(cmd, hptr->line, sizeof cmd);
1043         hptr = hptr->next;
1044         extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1045         extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1046
1047         /** Figure out the action */
1048         extract_token(action, pathname, 1, '/', sizeof action);
1049         if (strstr(action, "?")) *strstr(action, "?") = 0;
1050         if (strstr(action, "&")) *strstr(action, "&") = 0;
1051         if (strstr(action, " ")) *strstr(action, " ") = 0;
1052
1053         extract_token(arg1, pathname, 2, '/', sizeof arg1);
1054         if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
1055         if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
1056         if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
1057
1058         extract_token(arg2, pathname, 3, '/', sizeof arg2);
1059         if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
1060         if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
1061         if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
1062
1063         extract_token(arg3, pathname, 4, '/', sizeof arg3);
1064         if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
1065         if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
1066         if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
1067
1068         extract_token(arg4, pathname, 5, '/', sizeof arg4);
1069         if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
1070         if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
1071         if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
1072
1073         extract_token(arg5, pathname, 6, '/', sizeof arg5);
1074         if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
1075         if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
1076         if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
1077
1078         extract_token(arg6, pathname, 7, '/', sizeof arg6);
1079         if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
1080         if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
1081         if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
1082
1083         extract_token(arg7, pathname, 8, '/', sizeof arg7);
1084         if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
1085         if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
1086         if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
1087
1088         while (hptr != NULL) {
1089                 safestrncpy(buf, hptr->line, sizeof buf);
1090                 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1091                 hptr = hptr->next;
1092
1093                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1094                         safestrncpy(cookie, &buf[15], sizeof cookie);
1095                         cookie_to_stuff(cookie, NULL,
1096                                         c_username, sizeof c_username,
1097                                         c_password, sizeof c_password,
1098                                         c_roomname, sizeof c_roomname);
1099                 }
1100                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1101                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1102                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1103                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1104                 }
1105                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1106                         ContentLength = atoi(&buf[16]);
1107                 }
1108                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1109                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
1110                 }
1111                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1112                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
1113                 }
1114                 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1115                         if (follow_xff) {
1116                                 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1117                         }
1118                 }
1119                 else if (!strncasecmp(buf, "Host: ", 6)) {
1120                         if (strlen(WC->http_host) == 0) {
1121                                 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1122                         }
1123                 }
1124                 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1125                         safestrncpy(browser_host, &buf[17], sizeof browser_host);
1126                         while (num_tokens(browser_host, ',') > 1) {
1127                                 remove_token(browser_host, 0, ',');
1128                         }
1129                         striplt(browser_host);
1130                 }
1131                 /** Only WAP gateways explicitly name this content-type */
1132                 else if (strstr(buf, "text/vnd.wap.wml")) {
1133                         WC->is_wap = 1;
1134                 }
1135         }
1136
1137         if (ContentLength > 0) {
1138                 content = malloc(ContentLength + SIZ);
1139                 memset(content, 0, ContentLength + SIZ);
1140                 sprintf(content, "Content-type: %s\n"
1141                                 "Content-length: %d\n\n",
1142                                 ContentType, ContentLength);
1143                 body_start = strlen(content);
1144
1145                 /** Read the entire input data at once. */
1146                 client_read(WC->http_sock, &content[BytesRead+body_start],
1147                         ContentLength);
1148
1149                 if (!strncasecmp(ContentType,
1150                               "application/x-www-form-urlencoded", 33)) {
1151                         addurls(&content[body_start]);
1152                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1153                         content_end = content + ContentLength + body_start;
1154                         mime_parser(content, content_end, *upload_handler,
1155                                         NULL, NULL, NULL, 0);
1156                 }
1157         } else {
1158                 content = NULL;
1159         }
1160
1161         /** make a note of where we are in case the user wants to save it */
1162         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1163         remove_token(WC->this_page, 2, ' ');
1164         remove_token(WC->this_page, 0, ' ');
1165
1166         /** If there are variables in the URL, we must grab them now */
1167         for (a = 0; a < strlen(cmd); ++a) {
1168                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1169                         for (b = a; b < strlen(cmd); ++b)
1170                                 if (isspace(cmd[b]))
1171                                         cmd[b] = 0;
1172                         addurls(&cmd[a + 1]);
1173                         cmd[a] = 0;
1174                 }
1175         }
1176
1177         /** If it's a "force 404" situation then display the error and bail. */
1178         if (!strcmp(action, "404")) {
1179                 wprintf("HTTP/1.1 404 Not found\r\n");
1180                 wprintf("Content-Type: text/plain\r\n");
1181                 wprintf("\r\n");
1182                 wprintf("Not found\r\n");
1183                 goto SKIP_ALL_THIS_CRAP;
1184         }
1185
1186         /** Static content can be sent without connecting to Citadel. */
1187         is_static = 0;
1188         for (a=0; a<ndirs; ++a) {
1189                 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1190                         is_static = 1;
1191                 }
1192         }
1193         if (is_static) {
1194                 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1195                         action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1196                 for (a=0; a<8; ++a) {
1197                         if (buf[strlen(buf)-1] == '/') {
1198                                 buf[strlen(buf)-1] = 0;
1199                         }
1200                 }
1201                 for (a = 0; a < strlen(buf); ++a) {
1202                         if (isspace(buf[a])) {
1203                                 buf[a] = 0;
1204                         }
1205                 }
1206                 output_static(buf);
1207                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
1208         }
1209
1210         /**
1211          * If we're not connected to a Citadel server, try to hook up the
1212          * connection now.
1213          */
1214         if (!WC->connected) {
1215                 if (!strcasecmp(ctdlhost, "uds")) {
1216                         /* unix domain socket */
1217                         sprintf(buf, "%s/citadel.socket", ctdlport);
1218                         WC->serv_sock = uds_connectsock(buf);
1219                 }
1220                 else {
1221                         /* tcp socket */
1222                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1223                 }
1224
1225                 if (WC->serv_sock < 0) {
1226                         do_logout();
1227                         goto SKIP_ALL_THIS_CRAP;
1228                 }
1229                 else {
1230                         WC->connected = 1;
1231                         serv_getln(buf, sizeof buf);    /** get the server welcome message */
1232
1233                         /**
1234                          * From what host is our user connecting?  Go with
1235                          * the host at the other end of the HTTP socket,
1236                          * unless we are following X-Forwarded-For: headers
1237                          * and such a header has already turned up something.
1238                          */
1239                         if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1240                                 locate_host(browser_host, WC->http_sock);
1241                         }
1242
1243                         get_serv_info(browser_host, user_agent);
1244                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1245                                 wprintf(_("You are connected to a Citadel "
1246                                         "server running Citadel %d.%02d. \n"
1247                                         "In order to run this version of WebCit "
1248                                         "you must also have Citadel %d.%02d or"
1249                                         " newer.\n\n\n"),
1250                                                 serv_info.serv_rev_level / 100,
1251                                                 serv_info.serv_rev_level % 100,
1252                                                 MINIMUM_CIT_VERSION / 100,
1253                                                 MINIMUM_CIT_VERSION % 100
1254                                         );
1255                                 end_webcit_session();
1256                                 goto SKIP_ALL_THIS_CRAP;
1257                         }
1258                 }
1259         }
1260
1261         /**
1262          * Functions which can be performed without logging in
1263          */
1264         if (!strcasecmp(action, "listsub")) {
1265                 do_listsub();
1266                 goto SKIP_ALL_THIS_CRAP;
1267         }
1268 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1269         if (!strcasecmp(action, "freebusy")) {
1270                 do_freebusy(cmd);
1271                 goto SKIP_ALL_THIS_CRAP;
1272         }
1273 #endif
1274
1275         /**
1276          * If we're not logged in, but we have HTTP Authentication data,
1277          * try logging in to Citadel using that.
1278          */
1279         if ((!WC->logged_in)
1280            && (strlen(c_httpauth_user) > 0)
1281            && (strlen(c_httpauth_pass) > 0)) {
1282                 serv_printf("USER %s", c_httpauth_user);
1283                 serv_getln(buf, sizeof buf);
1284                 if (buf[0] == '3') {
1285                         serv_printf("PASS %s", c_httpauth_pass);
1286                         serv_getln(buf, sizeof buf);
1287                         if (buf[0] == '2') {
1288                                 become_logged_in(c_httpauth_user,
1289                                                 c_httpauth_pass, buf);
1290                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1291                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1292                         } else {
1293                                 /** Should only display when password is wrong */
1294                                 authorization_required(&buf[4]);
1295                                 goto SKIP_ALL_THIS_CRAP;
1296                         }
1297                 }
1298         }
1299
1300         /** This needs to run early */
1301         if (!strcasecmp(action, "rss")) {
1302                 display_rss(bstr("room"), request_method);
1303                 goto SKIP_ALL_THIS_CRAP;
1304         }
1305
1306         /** 
1307          * The GroupDAV stuff relies on HTTP authentication instead of
1308          * our session's authentication.
1309          */
1310         if (!strncasecmp(action, "groupdav", 8)) {
1311                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1312                         ContentLength, content+body_start);
1313                 if (!WC->logged_in) {
1314                         WC->killthis = 1;       /* If not logged in, don't */
1315                 }                               /* keep the session active */
1316                 goto SKIP_ALL_THIS_CRAP;
1317         }
1318
1319
1320         /**
1321          * Automatically send requests with any method other than GET or
1322          * POST to the GroupDAV code as well.
1323          */
1324         if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1325                 groupdav_main(req, ContentType, /** do GroupDAV methods */
1326                         ContentLength, content+body_start);
1327                 if (!WC->logged_in) {
1328                         WC->killthis = 1;       /** If not logged in, don't */
1329                 }                               /** keep the session active */
1330                 goto SKIP_ALL_THIS_CRAP;
1331         }
1332
1333         /**
1334          * If we're not logged in, but we have username and password cookies
1335          * supplied by the browser, try using them to log in.
1336          */
1337         if ((!WC->logged_in)
1338            && (strlen(c_username) > 0)
1339            && (strlen(c_password) > 0)) {
1340                 serv_printf("USER %s", c_username);
1341                 serv_getln(buf, sizeof buf);
1342                 if (buf[0] == '3') {
1343                         serv_printf("PASS %s", c_password);
1344                         serv_getln(buf, sizeof buf);
1345                         if (buf[0] == '2') {
1346                                 become_logged_in(c_username, c_password, buf);
1347                         }
1348                 }
1349         }
1350         /**
1351          * If we don't have a current room, but a cookie specifying the
1352          * current room is supplied, make an effort to go there.
1353          */
1354         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1355                 serv_printf("GOTO %s", c_roomname);
1356                 serv_getln(buf, sizeof buf);
1357                 if (buf[0] == '2') {
1358                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1359                 }
1360         }
1361
1362         if (!strcasecmp(action, "image")) {
1363                 output_image();
1364
1365                 /**
1366                  * All functions handled below this point ... make sure we log in
1367                  * before doing anything else!
1368                  */
1369         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1370                 do_login();
1371         } else if (!WC->logged_in) {
1372                 display_login(NULL);
1373         }
1374
1375         /**
1376          * Various commands...
1377          */
1378
1379         else if (!strcasecmp(action, "do_welcome")) {
1380                 do_welcome();
1381         } else if (!strcasecmp(action, "blank")) {
1382                 blank_page();
1383         } else if (!strcasecmp(action, "do_template")) {
1384                 url_do_template();
1385         } else if (!strcasecmp(action, "display_aide_menu")) {
1386                 display_aide_menu();
1387         } else if (!strcasecmp(action, "display_main_menu")) {
1388                 display_main_menu();
1389         } else if (!strcasecmp(action, "who")) {
1390                 who();
1391         } else if (!strcasecmp(action, "sslg")) {
1392                 seconds_since_last_gexp();
1393         } else if (!strcasecmp(action, "who_inner_html")) {
1394                 begin_ajax_response();
1395                 who_inner_div();
1396                 end_ajax_response();
1397         } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1398                 begin_ajax_response();
1399                 do_iconbar();
1400                 end_ajax_response();
1401         } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1402                 begin_ajax_response();
1403                 do_iconbar_roomlist();
1404                 end_ajax_response();
1405         } else if (!strcasecmp(action, "knrooms")) {
1406                 knrooms();
1407         } else if (!strcasecmp(action, "gotonext")) {
1408                 slrp_highest();
1409                 gotonext();
1410         } else if (!strcasecmp(action, "skip")) {
1411                 gotonext();
1412         } else if (!strcasecmp(action, "ungoto")) {
1413                 ungoto();
1414         } else if (!strcasecmp(action, "dotgoto")) {
1415                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1416                         slrp_highest();
1417                 }
1418                 smart_goto(bstr("room"));
1419         } else if (!strcasecmp(action, "dotskip")) {
1420                 smart_goto(bstr("room"));
1421         } else if (!strcasecmp(action, "termquit")) {
1422                 do_logout();
1423         } else if (!strcasecmp(action, "readnew")) {
1424                 readloop("readnew");
1425         } else if (!strcasecmp(action, "readold")) {
1426                 readloop("readold");
1427         } else if (!strcasecmp(action, "readfwd")) {
1428                 readloop("readfwd");
1429         } else if (!strcasecmp(action, "headers")) {
1430                 readloop("headers");
1431         } else if (!strcasecmp(action, "do_search")) {
1432                 readloop("do_search");
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, "display_smtpqueue")) {
1622                 display_smtpqueue();
1623         } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
1624                 display_smtpqueue_inner_div();
1625         } else if (!strcasecmp(action, "display_sieve")) {
1626                 display_sieve();
1627         } else if (!strcasecmp(action, "setup_wizard")) {
1628                 do_setup_wizard();
1629         } else if (!strcasecmp(action, "display_preferences")) {
1630                 display_preferences();
1631         } else if (!strcasecmp(action, "set_preferences")) {
1632                 set_preferences();
1633         } else if (!strcasecmp(action, "recp_autocomplete")) {
1634                 recp_autocomplete(bstr("recp"));
1635         } else if (!strcasecmp(action, "cc_autocomplete")) {
1636                 recp_autocomplete(bstr("cc"));
1637         } else if (!strcasecmp(action, "bcc_autocomplete")) {
1638                 recp_autocomplete(bstr("bcc"));
1639         } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1640                 set_floordiv_expanded(arg1);
1641         } else if (!strcasecmp(action, "diagnostics")) {
1642                 output_headers(1, 1, 1, 0, 0, 0);
1643                 wprintf("Session: %d<hr />\n", WC->wc_session);
1644                 wprintf("Command: <br /><PRE>\n");
1645                 escputs(cmd);
1646                 wprintf("</PRE><hr />\n");
1647                 wprintf("Variables: <br /><PRE>\n");
1648                 dump_vars();
1649                 wprintf("</PRE><hr />\n");
1650                 wDumpContent(1);
1651         } else if (!strcasecmp(action, "updatenote")) {
1652                 updatenote();
1653         }
1654
1655         /** When all else fais, display the main menu. */
1656         else {
1657                 display_main_menu();
1658         }
1659
1660 SKIP_ALL_THIS_CRAP:
1661         fflush(stdout);
1662         if (content != NULL) {
1663                 free(content);
1664                 content = NULL;
1665         }
1666         free_urls();
1667         if (WC->upload_length > 0) {
1668                 free(WC->upload);
1669                 WC->upload_length = 0;
1670         }
1671 }
1672
1673
1674 /*@}*/