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