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