Done with doxygenizing
[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 BackendWebServer
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 add urls???
61  * \param url what???
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 what???
145  * \param key what???
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 to copy to
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 outputbuffer
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 message to the client ???
367  * \param strbuf message to print???
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[SIZ];
398         char httpnow[SIZ];
399
400         wprintf("HTTP/1.1 200 OK\n");
401         httpdate(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  * \param msgnum number of the item on the citadel server
651  * \param partnum the partnumber???
652  */
653 void mimepart(char *msgnum, char *partnum)
654 {
655         char buf[SIZ];
656         off_t bytes;
657         char content_type[SIZ];
658         char *content = NULL;
659         
660         serv_printf("OPNA %s|%s", msgnum, partnum);
661         serv_getln(buf, sizeof buf);
662         if (buf[0] == '2') {
663                 bytes = extract_long(&buf[4], 0);
664                 content = malloc(bytes + 2);
665                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
666                 output_headers(0, 0, 0, 0, 0, 0);
667                 read_server_binary(content, bytes);
668                 serv_puts("CLOS");
669                 serv_getln(buf, sizeof buf);
670                 http_transmit_thing(content, bytes, content_type, 0);
671                 free(content);
672         } else {
673                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
674                 output_headers(0, 0, 0, 0, 0, 0);
675                 wprintf("Content-Type: text/plain\r\n");
676                 wprintf("\r\n");
677                 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
678         }
679
680 }
681
682
683 /**
684  * \brief Read any MIME part of a message, from the server, into memory.
685  * \param msgnum number of the message on the citadel server
686  * \param partnum the part number???
687  */
688 char *load_mimepart(long msgnum, char *partnum)
689 {
690         char buf[SIZ];
691         off_t bytes;
692         char content_type[SIZ];
693         char *content;
694         
695         serv_printf("OPNA %ld|%s", msgnum, partnum);
696         serv_getln(buf, sizeof buf);
697         if (buf[0] == '2') {
698                 bytes = extract_long(&buf[4], 0);
699                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
700
701                 content = malloc(bytes + 2);
702                 read_server_binary(content, bytes);
703
704                 serv_puts("CLOS");
705                 serv_getln(buf, sizeof buf);
706                 content[bytes] = 0;     /* null terminate for good measure */
707                 return(content);
708         }
709         else {
710                 return(NULL);
711         }
712
713 }
714
715
716 /**
717  * \brief Convenience functions to display a page containing only a string
718  * \param titlebarcolor color of the titlebar of the frame
719  * \param titlebarmsg text to display in the title bar
720  * \param messagetext body of the box
721  */
722 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
723 {
724         wprintf("HTTP/1.1 200 OK\n");
725         output_headers(1, 1, 2, 0, 0, 0);
726         wprintf("<div id=\"banner\">\n");
727         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
728         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
729         wprintf("</TD></TR></TABLE>\n");
730         wprintf("</div>\n<div id=\"content\">\n");
731         escputs(messagetext);
732
733         wprintf("<hr />\n");
734         wDumpContent(1);
735 }
736
737
738 /**
739  * \brief Display a blank page.
740  */
741 void blank_page(void) {
742         output_headers(1, 1, 0, 0, 0, 0);
743         wDumpContent(2);
744 }
745
746
747 /**
748  * \brief A template has been requested
749  */
750 void url_do_template(void) {
751         do_template(bstr("template"));
752 }
753
754
755
756 /**
757  * \brief Offer to make any page the user's "start page."
758  */
759 void offer_start_page(void) {
760         wprintf("<a href=\"change_start_page?startpage=");
761         urlescputs(WC->this_page);
762         wprintf("\"><FONT SIZE=-2 COLOR=\"#AAAAAA\">");
763         wprintf(_("Make this my start page"));
764         wprintf("</FONT></A>");
765 /*
766         wprintf("<br/><a href=\"rss?room=");
767         urlescputs(WC->wc_roomname);
768         wprintf("\" title=\"RSS 2.0 feed for ");
769         escputs(WC->wc_roomname);
770         wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
771 */
772 }
773
774
775 /**
776  * \brief Change the user's start page
777  */
778 void change_start_page(void) {
779
780         if (bstr("startpage") == NULL) {
781                 safestrncpy(WC->ImportantMessage,
782                         _("You no longer have a start page selected."),
783                         sizeof WC->ImportantMessage);
784                 display_main_menu();
785                 return;
786         }
787
788         set_preference("startpage", bstr("startpage"), 1);
789
790         output_headers(1, 1, 0, 0, 0, 0);
791         do_template("newstartpage");
792         wDumpContent(1);
793 }
794
795
796
797 /**
798  * \brief display the successfull edit or s.th. lisket hat ??? 
799  * \param successmessage the mesage itself
800  */
801 void display_success(char *successmessage)
802 {
803         convenience_page("007700", "OK", successmessage);
804 }
805
806
807 /**
808  * \brief Authorization required page 
809  * This is probably temporary and should be revisited 
810  * \param message message to put in header
811 */
812 void authorization_required(const char *message)
813 {
814         wprintf("HTTP/1.1 401 Authorization Required\r\n");
815         wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
816         wprintf("Content-Type: text/html\r\n\r\n");
817         wprintf("<h1>");
818         wprintf(_("Authorization Required"));
819         wprintf("</h1>\r\n");
820         wprintf(_("The resource you requested requires a valid username and password. "
821                 "You could not be logged in: %s\n"), message);
822         wDumpContent(0);
823 }
824
825 /**
826  * \brief handle file uploads 
827  * \param name the url the upload is done to
828  * \param filename the name of the file being uploaded
829  * \param partnum item number on the citadel server ???
830  * \param disp what???
831  * \param content the file contents???
832  * \param cbtype what???
833  * \param cbcharset the character set of cb??
834  * \param length the size of the file ???
835  * \param encoding charset encoding of the file??
836  * \param userdata what???
837  */
838 void upload_handler(char *name, char *filename, char *partnum, char *disp,
839                         void *content, char *cbtype, char *cbcharset,
840                         size_t length, char *encoding, void *userdata)
841 {
842         struct urlcontent *u;
843
844         /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
845                 name, cbtype, length); */
846
847         /* Form fields */
848         if ( (length > 0) && (strlen(cbtype) == 0) ) {
849                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
850                 u->next = WC->urlstrings;
851                 WC->urlstrings = u;
852                 safestrncpy(u->url_key, name, sizeof(u->url_key));
853                 u->url_data = malloc(length + 1);
854                 memcpy(u->url_data, content, length);
855                 u->url_data[length] = 0;
856         }
857
858         /** Uploaded files */
859         if ( (length > 0) && (strlen(cbtype) > 0) ) {
860                 WC->upload = malloc(length);
861                 if (WC->upload != NULL) {
862                         WC->upload_length = length;
863                         safestrncpy(WC->upload_filename, filename,
864                                         sizeof(WC->upload_filename));
865                         safestrncpy(WC->upload_content_type, cbtype,
866                                         sizeof(WC->upload_content_type));
867                         memcpy(WC->upload, content, length);
868                 }
869                 else {
870                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
871                 }
872         }
873
874 }
875
876 /**
877  * \brief Convenience functions to wrap around asynchronous ajax responses
878  */
879 void begin_ajax_response(void) {
880         output_headers(0, 0, 0, 0, 0, 0);
881
882         wprintf("Content-type: text/html; charset=UTF-8\r\n"
883                 "Server: %s\r\n"
884                 "Connection: close\r\n"
885                 "Pragma: no-cache\r\n"
886                 "Cache-Control: no-cache\r\n",
887                 SERVER);
888         begin_burst();
889 }
890
891 /**
892  * \brief print ajax response footer 
893  */
894 void end_ajax_response(void) {
895         wprintf("\r\n");
896         wDumpContent(0);
897 }
898
899 /**
900  * \brief parse server command to execute on the citadel
901  */
902 void ajax_servcmd(void)
903 {
904         char buf[1024];
905         char gcontent[1024];
906         char *junk;
907         size_t len;
908
909         begin_ajax_response();
910
911         serv_printf("%s", bstr("g_cmd"));
912         serv_getln(buf, sizeof buf);
913         wprintf("%s\n", buf);
914
915         if (buf[0] == '8') {
916                 serv_printf("\n\n000");
917         }
918         if ((buf[0] == '1') || (buf[0] == '8')) {
919                 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
920                         wprintf("%s\n", gcontent);
921                 }
922                 wprintf("000");
923         }
924         if (buf[0] == '4') {
925                 text_to_server(bstr("g_input"), 0);
926                 serv_puts("000");
927         }
928         if (buf[0] == '6') {
929                 len = atol(&buf[4]);
930                 junk = malloc(len);
931                 serv_read(junk, len);
932                 free(junk);
933         }
934         if (buf[0] == '7') {
935                 len = atol(&buf[4]);
936                 junk = malloc(len);
937                 memset(junk, 0, len);
938                 serv_write(junk, len);
939                 free(junk);
940         }
941
942         end_ajax_response();
943         
944         /**
945          * This is kind of an ugly hack, but this is the only place it can go.
946          * If the command was GEXP, then the instant messenger window must be
947          * running, so reset the "last_pager_check" watchdog timer so
948          * that page_popup() doesn't try to open it a second time.
949          */
950         if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
951                 WC->last_pager_check = time(NULL);
952         }
953 }
954
955
956 /**
957  * \brief Helper function for the asynchronous check to see if we need
958  * to open the instant messenger window.
959  */
960 void seconds_since_last_gexp(void)
961 {
962         char buf[256];
963
964         begin_ajax_response();
965         if ( (time(NULL) - WC->last_pager_check) < 30) {
966                 wprintf("NO\n");
967         }
968         else {
969                 serv_puts("NOOP");
970                 serv_getln(buf, sizeof buf);
971                 if (buf[3] == '*') {
972                         wprintf("YES");
973                 }
974                 else {
975                         wprintf("NO");
976                 }
977         }
978         end_ajax_response();
979 }
980
981
982
983
984 /**
985  * \brief Entry point for WebCit transaction
986  */
987 void session_loop(struct httprequest *req)
988 {
989         char cmd[1024];
990         char action[1024];
991         char arg1[128];
992         char arg2[128];
993         char arg3[128];
994         char arg4[128];
995         char arg5[128];
996         char arg6[128];
997         char arg7[128];
998         char buf[SIZ];
999         char request_method[128];
1000         char pathname[1024];
1001         int a, b;
1002         int ContentLength = 0;
1003         int BytesRead = 0;
1004         char ContentType[512];
1005         char *content = NULL;
1006         char *content_end = NULL;
1007         struct httprequest *hptr;
1008         char browser_host[256];
1009         char user_agent[256];
1010         int body_start = 0;
1011         int is_static = 0;
1012
1013         /**
1014          * We stuff these with the values coming from the client cookies,
1015          * so we can use them to reconnect a timed out session if we have to.
1016          */
1017         char c_username[SIZ];
1018         char c_password[SIZ];
1019         char c_roomname[SIZ];
1020         char c_httpauth_string[SIZ];
1021         char c_httpauth_user[SIZ];
1022         char c_httpauth_pass[SIZ];
1023         char cookie[SIZ];
1024
1025         safestrncpy(c_username, "", sizeof c_username);
1026         safestrncpy(c_password, "", sizeof c_password);
1027         safestrncpy(c_roomname, "", sizeof c_roomname);
1028         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1029         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1030         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1031         strcpy(browser_host, "");
1032
1033         WC->upload_length = 0;
1034         WC->upload = NULL;
1035         WC->vars = NULL;
1036         WC->is_wap = 0;
1037
1038         hptr = req;
1039         if (hptr == NULL) return;
1040
1041         safestrncpy(cmd, hptr->line, sizeof cmd);
1042         hptr = hptr->next;
1043         extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1044         extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1045
1046         /** Figure out the action */
1047         extract_token(action, pathname, 1, '/', sizeof action);
1048         if (strstr(action, "?")) *strstr(action, "?") = 0;
1049         if (strstr(action, "&")) *strstr(action, "&") = 0;
1050         if (strstr(action, " ")) *strstr(action, " ") = 0;
1051
1052         extract_token(arg1, pathname, 2, '/', sizeof arg1);
1053         if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
1054         if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
1055         if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
1056
1057         extract_token(arg2, pathname, 3, '/', sizeof arg2);
1058         if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
1059         if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
1060         if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
1061
1062         extract_token(arg3, pathname, 4, '/', sizeof arg3);
1063         if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
1064         if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
1065         if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
1066
1067         extract_token(arg4, pathname, 5, '/', sizeof arg4);
1068         if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
1069         if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
1070         if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
1071
1072         extract_token(arg5, pathname, 6, '/', sizeof arg5);
1073         if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
1074         if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
1075         if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
1076
1077         extract_token(arg6, pathname, 7, '/', sizeof arg6);
1078         if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
1079         if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
1080         if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
1081
1082         extract_token(arg7, pathname, 8, '/', sizeof arg7);
1083         if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
1084         if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
1085         if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
1086
1087         while (hptr != NULL) {
1088                 safestrncpy(buf, hptr->line, sizeof buf);
1089                 hptr = hptr->next;
1090
1091                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1092                         safestrncpy(cookie, &buf[15], sizeof cookie);
1093                         cookie_to_stuff(cookie, NULL,
1094                                         c_username, sizeof c_username,
1095                                         c_password, sizeof c_password,
1096                                         c_roomname, sizeof c_roomname);
1097                 }
1098                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1099                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1100                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1101                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1102                 }
1103                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1104                         ContentLength = atoi(&buf[16]);
1105                 }
1106                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1107                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
1108                 }
1109                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1110                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
1111                 }
1112                 else if (!strncasecmp(buf, "Host: ", 6)) {
1113                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1114                 }
1115                 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1116                         safestrncpy(browser_host, &buf[17], sizeof browser_host);
1117                         while (num_tokens(browser_host, ',') > 1) {
1118                                 remove_token(browser_host, 0, ',');
1119                         }
1120                         striplt(browser_host);
1121                 }
1122                 /** Only WAP gateways explicitly name this content-type */
1123                 else if (strstr(buf, "text/vnd.wap.wml")) {
1124                         WC->is_wap = 1;
1125                 }
1126         }
1127
1128         if (ContentLength > 0) {
1129                 content = malloc(ContentLength + SIZ);
1130                 memset(content, 0, ContentLength + SIZ);
1131                 sprintf(content, "Content-type: %s\n"
1132                                 "Content-length: %d\n\n",
1133                                 ContentType, ContentLength);
1134                 body_start = strlen(content);
1135
1136                 /** Read the entire input data at once. */
1137                 client_read(WC->http_sock, &content[BytesRead+body_start],
1138                         ContentLength);
1139
1140                 if (!strncasecmp(ContentType,
1141                               "application/x-www-form-urlencoded", 33)) {
1142                         addurls(&content[body_start]);
1143                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1144                         content_end = content + ContentLength + body_start;
1145                         mime_parser(content, content_end, *upload_handler,
1146                                         NULL, NULL, NULL, 0);
1147                 }
1148         } else {
1149                 content = NULL;
1150         }
1151
1152         /** make a note of where we are in case the user wants to save it */
1153         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1154         remove_token(WC->this_page, 2, ' ');
1155         remove_token(WC->this_page, 0, ' ');
1156
1157         /** If there are variables in the URL, we must grab them now */
1158         for (a = 0; a < strlen(cmd); ++a) {
1159                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1160                         for (b = a; b < strlen(cmd); ++b)
1161                                 if (isspace(cmd[b]))
1162                                         cmd[b] = 0;
1163                         addurls(&cmd[a + 1]);
1164                         cmd[a] = 0;
1165                 }
1166         }
1167
1168
1169         /** Static content can be sent without connecting to Citadel. */
1170         is_static = 0;
1171         for (a=0; a<(sizeof(static_content_dirs) / sizeof(char *)); ++a) {
1172                 if (!strcasecmp(action, static_content_dirs[a])) {
1173                         is_static = 1;
1174                 }
1175         }
1176         if (is_static) {
1177                 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1178                         action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
1179                 for (a=0; a<8; ++a) {
1180                         if (buf[strlen(buf)-1] == '/') {
1181                                 buf[strlen(buf)-1] = 0;
1182                         }
1183                 }
1184                 for (a = 0; a < strlen(buf); ++a) {
1185                         if (isspace(buf[a])) {
1186                                 buf[a] = 0;
1187                         }
1188                 }
1189                 output_static(buf);
1190                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
1191         }
1192
1193         /**
1194          * If we're not connected to a Citadel server, try to hook up the
1195          * connection now.
1196          */
1197         if (!WC->connected) {
1198                 if (!strcasecmp(ctdlhost, "uds")) {
1199                         /* unix domain socket */
1200                         sprintf(buf, "%s/citadel.socket", ctdlport);
1201                         WC->serv_sock = uds_connectsock(buf);
1202                 }
1203                 else {
1204                         /* tcp socket */
1205                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1206                 }
1207
1208                 if (WC->serv_sock < 0) {
1209                         do_logout();
1210                         goto SKIP_ALL_THIS_CRAP;
1211                 }
1212                 else {
1213                         WC->connected = 1;
1214                         serv_getln(buf, sizeof buf);    /** get the server welcome message */
1215
1216                         /**
1217                          * From what host is our user connecting?  Go with
1218                          * the host at the other end of the HTTP socket,
1219                          * unless we are following X-Forwarded-For: headers
1220                          * and such a header has already turned up something.
1221                          */
1222                         if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1223                                 locate_host(browser_host, WC->http_sock);
1224                         }
1225
1226                         get_serv_info(browser_host, user_agent);
1227                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1228                                 wprintf(_("You are connected to a Citadel "
1229                                         "server running Citadel %d.%02d. \n"
1230                                         "In order to run this version of WebCit "
1231                                         "you must also have Citadel %d.%02d or"
1232                                         " newer.\n\n\n"),
1233                                                 serv_info.serv_rev_level / 100,
1234                                                 serv_info.serv_rev_level % 100,
1235                                                 MINIMUM_CIT_VERSION / 100,
1236                                                 MINIMUM_CIT_VERSION % 100
1237                                         );
1238                                 end_webcit_session();
1239                                 goto SKIP_ALL_THIS_CRAP;
1240                         }
1241                 }
1242         }
1243
1244         /**
1245          * Functions which can be performed without logging in
1246          */
1247         if (!strcasecmp(action, "listsub")) {
1248                 do_listsub();
1249                 goto SKIP_ALL_THIS_CRAP;
1250         }
1251 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1252         if (!strcasecmp(action, "freebusy")) {
1253                 do_freebusy(cmd);
1254                 goto SKIP_ALL_THIS_CRAP;
1255         }
1256 #endif
1257
1258         /**
1259          * If we're not logged in, but we have HTTP Authentication data,
1260          * try logging in to Citadel using that.
1261          */
1262         if ((!WC->logged_in)
1263            && (strlen(c_httpauth_user) > 0)
1264            && (strlen(c_httpauth_pass) > 0)) {
1265                 serv_printf("USER %s", c_httpauth_user);
1266                 serv_getln(buf, sizeof buf);
1267                 if (buf[0] == '3') {
1268                         serv_printf("PASS %s", c_httpauth_pass);
1269                         serv_getln(buf, sizeof buf);
1270                         if (buf[0] == '2') {
1271                                 become_logged_in(c_httpauth_user,
1272                                                 c_httpauth_pass, buf);
1273                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1274                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1275                         } else {
1276                                 /** Should only display when password is wrong */
1277                                 authorization_required(&buf[4]);
1278                                 goto SKIP_ALL_THIS_CRAP;
1279                         }
1280                 }
1281         }
1282
1283         /** This needs to run early */
1284         if (!strcasecmp(action, "rss")) {
1285                 display_rss(bstr("room"), request_method);
1286                 goto SKIP_ALL_THIS_CRAP;
1287         }
1288
1289         /** 
1290          * The GroupDAV stuff relies on HTTP authentication instead of
1291          * our session's authentication.
1292          */
1293         if (!strncasecmp(action, "groupdav", 8)) {
1294                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1295                         ContentLength, content+body_start);
1296                 if (!WC->logged_in) {
1297                         WC->killthis = 1;       /* If not logged in, don't */
1298                 }                               /* keep the session active */
1299                 goto SKIP_ALL_THIS_CRAP;
1300         }
1301
1302
1303         /**
1304          * Automatically send requests with any method other than GET or
1305          * POST to the GroupDAV code as well.
1306          */
1307         if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1308                 groupdav_main(req, ContentType, /** do GroupDAV methods */
1309                         ContentLength, content+body_start);
1310                 if (!WC->logged_in) {
1311                         WC->killthis = 1;       /** If not logged in, don't */
1312                 }                               /** keep the session active */
1313                 goto SKIP_ALL_THIS_CRAP;
1314         }
1315
1316         /**
1317          * If we're not logged in, but we have username and password cookies
1318          * supplied by the browser, try using them to log in.
1319          */
1320         if ((!WC->logged_in)
1321            && (strlen(c_username) > 0)
1322            && (strlen(c_password) > 0)) {
1323                 serv_printf("USER %s", c_username);
1324                 serv_getln(buf, sizeof buf);
1325                 if (buf[0] == '3') {
1326                         serv_printf("PASS %s", c_password);
1327                         serv_getln(buf, sizeof buf);
1328                         if (buf[0] == '2') {
1329                                 become_logged_in(c_username, c_password, buf);
1330                         }
1331                 }
1332         }
1333         /**
1334          * If we don't have a current room, but a cookie specifying the
1335          * current room is supplied, make an effort to go there.
1336          */
1337         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1338                 serv_printf("GOTO %s", c_roomname);
1339                 serv_getln(buf, sizeof buf);
1340                 if (buf[0] == '2') {
1341                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1342                 }
1343         }
1344
1345         if (!strcasecmp(action, "image")) {
1346                 output_image();
1347
1348                 /**
1349                  * All functions handled below this point ... make sure we log in
1350                  * before doing anything else!
1351                  */
1352         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1353                 do_login();
1354         } else if (!WC->logged_in) {
1355                 display_login(NULL);
1356         }
1357
1358         /**
1359          * Various commands...
1360          */
1361
1362         else if (!strcasecmp(action, "do_welcome")) {
1363                 do_welcome();
1364         } else if (!strcasecmp(action, "blank")) {
1365                 blank_page();
1366         } else if (!strcasecmp(action, "do_template")) {
1367                 url_do_template();
1368         } else if (!strcasecmp(action, "display_aide_menu")) {
1369                 display_aide_menu();
1370         } else if (!strcasecmp(action, "display_main_menu")) {
1371                 display_main_menu();
1372         } else if (!strcasecmp(action, "who")) {
1373                 who();
1374         } else if (!strcasecmp(action, "sslg")) {
1375                 seconds_since_last_gexp();
1376         } else if (!strcasecmp(action, "who_inner_html")) {
1377                 begin_ajax_response();
1378                 who_inner_div();
1379                 end_ajax_response();
1380         } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1381                 begin_ajax_response();
1382                 do_iconbar();
1383                 end_ajax_response();
1384         } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1385                 begin_ajax_response();
1386                 do_iconbar_roomlist();
1387                 end_ajax_response();
1388         } else if (!strcasecmp(action, "knrooms")) {
1389                 knrooms();
1390         } else if (!strcasecmp(action, "gotonext")) {
1391                 slrp_highest();
1392                 gotonext();
1393         } else if (!strcasecmp(action, "skip")) {
1394                 gotonext();
1395         } else if (!strcasecmp(action, "ungoto")) {
1396                 ungoto();
1397         } else if (!strcasecmp(action, "dotgoto")) {
1398                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1399                         slrp_highest();
1400                 }
1401                 smart_goto(bstr("room"));
1402         } else if (!strcasecmp(action, "dotskip")) {
1403                 smart_goto(bstr("room"));
1404         } else if (!strcasecmp(action, "termquit")) {
1405                 do_logout();
1406         } else if (!strcasecmp(action, "readnew")) {
1407                 readloop("readnew");
1408         } else if (!strcasecmp(action, "readold")) {
1409                 readloop("readold");
1410         } else if (!strcasecmp(action, "readfwd")) {
1411                 readloop("readfwd");
1412         } else if (!strcasecmp(action, "headers")) {
1413                 readloop("headers");
1414         } else if (!strcasecmp(action, "msg")) {
1415                 embed_message(arg1);
1416         } else if (!strcasecmp(action, "printmsg")) {
1417                 print_message(arg1);
1418         } else if (!strcasecmp(action, "msgheaders")) {
1419                 display_headers(arg1);
1420         } else if (!strcasecmp(action, "wiki")) {
1421                 display_wiki_page();
1422         } else if (!strcasecmp(action, "display_enter")) {
1423                 display_enter();
1424         } else if (!strcasecmp(action, "post")) {
1425                 post_message();
1426         } else if (!strcasecmp(action, "move_msg")) {
1427                 move_msg();
1428         } else if (!strcasecmp(action, "delete_msg")) {
1429                 delete_msg();
1430         } else if (!strcasecmp(action, "userlist")) {
1431                 userlist();
1432         } else if (!strcasecmp(action, "showuser")) {
1433                 showuser();
1434         } else if (!strcasecmp(action, "display_page")) {
1435                 display_page();
1436         } else if (!strcasecmp(action, "page_user")) {
1437                 page_user();
1438         } else if (!strcasecmp(action, "chat")) {
1439                 do_chat();
1440         } else if (!strcasecmp(action, "display_private")) {
1441                 display_private("", 0);
1442         } else if (!strcasecmp(action, "goto_private")) {
1443                 goto_private();
1444         } else if (!strcasecmp(action, "zapped_list")) {
1445                 zapped_list();
1446         } else if (!strcasecmp(action, "display_zap")) {
1447                 display_zap();
1448         } else if (!strcasecmp(action, "zap")) {
1449                 zap();
1450         } else if (!strcasecmp(action, "display_entroom")) {
1451                 display_entroom();
1452         } else if (!strcasecmp(action, "entroom")) {
1453                 entroom();
1454         } else if (!strcasecmp(action, "display_whok")) {
1455                 display_whok();
1456         } else if (!strcasecmp(action, "do_invt_kick")) {
1457                 do_invt_kick();
1458         } else if (!strcasecmp(action, "display_editroom")) {
1459                 display_editroom();
1460         } else if (!strcasecmp(action, "netedit")) {
1461                 netedit();
1462         } else if (!strcasecmp(action, "editroom")) {
1463                 editroom();
1464         } else if (!strcasecmp(action, "display_editinfo")) {
1465                 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1466         } else if (!strcasecmp(action, "editinfo")) {
1467                 save_edit(_("Room info"), "EINF 1", 1);
1468         } else if (!strcasecmp(action, "display_editbio")) {
1469                 sprintf(buf, "RBIO %s", WC->wc_fullname);
1470                 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1471         } else if (!strcasecmp(action, "editbio")) {
1472                 save_edit(_("Your bio"), "EBIO", 0);
1473         } else if (!strcasecmp(action, "confirm_move_msg")) {
1474                 confirm_move_msg();
1475         } else if (!strcasecmp(action, "delete_room")) {
1476                 delete_room();
1477         } else if (!strcasecmp(action, "validate")) {
1478                 validate();
1479         } else if (!strcasecmp(action, "display_editpic")) {
1480                 display_graphics_upload(_("your photo"),
1481                                         "UIMG 0|_userpic_",
1482                                         "editpic");
1483         } else if (!strcasecmp(action, "editpic")) {
1484                 do_graphics_upload("UIMG 1|_userpic_");
1485         } else if (!strcasecmp(action, "display_editroompic")) {
1486                 display_graphics_upload(_("the icon for this room"),
1487                                         "UIMG 0|_roompic_",
1488                                         "editroompic");
1489         } else if (!strcasecmp(action, "editroompic")) {
1490                 do_graphics_upload("UIMG 1|_roompic_");
1491         } else if (!strcasecmp(action, "delete_floor")) {
1492                 delete_floor();
1493         } else if (!strcasecmp(action, "rename_floor")) {
1494                 rename_floor();
1495         } else if (!strcasecmp(action, "create_floor")) {
1496                 create_floor();
1497         } else if (!strcasecmp(action, "display_editfloorpic")) {
1498                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1499                         bstr("which_floor"));
1500                 display_graphics_upload(_("the icon for this floor"),
1501                                         buf,
1502                                         "editfloorpic");
1503         } else if (!strcasecmp(action, "editfloorpic")) {
1504                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1505                         bstr("which_floor"));
1506                 do_graphics_upload(buf);
1507         } else if (!strcasecmp(action, "display_reg")) {
1508                 display_reg(0);
1509         } else if (!strcasecmp(action, "display_changepw")) {
1510                 display_changepw();
1511         } else if (!strcasecmp(action, "changepw")) {
1512                 changepw();
1513         } else if (!strcasecmp(action, "display_edit_node")) {
1514                 display_edit_node();
1515         } else if (!strcasecmp(action, "edit_node")) {
1516                 edit_node();
1517         } else if (!strcasecmp(action, "display_netconf")) {
1518                 display_netconf();
1519         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1520                 display_confirm_delete_node();
1521         } else if (!strcasecmp(action, "delete_node")) {
1522                 delete_node();
1523         } else if (!strcasecmp(action, "display_add_node")) {
1524                 display_add_node();
1525         } else if (!strcasecmp(action, "add_node")) {
1526                 add_node();
1527         } else if (!strcasecmp(action, "terminate_session")) {
1528                 slrp_highest();
1529                 terminate_session();
1530         } else if (!strcasecmp(action, "edit_me")) {
1531                 edit_me();
1532         } else if (!strcasecmp(action, "display_siteconfig")) {
1533                 display_siteconfig();
1534         } else if (!strcasecmp(action, "chat_recv")) {
1535                 chat_recv();
1536         } else if (!strcasecmp(action, "chat_send")) {
1537                 chat_send();
1538         } else if (!strcasecmp(action, "siteconfig")) {
1539                 siteconfig();
1540         } else if (!strcasecmp(action, "display_generic")) {
1541                 display_generic();
1542         } else if (!strcasecmp(action, "do_generic")) {
1543                 do_generic();
1544         } else if (!strcasecmp(action, "ajax_servcmd")) {
1545                 ajax_servcmd();
1546         } else if (!strcasecmp(action, "display_menubar")) {
1547                 display_menubar(1);
1548         } else if (!strcasecmp(action, "mimepart")) {
1549                 mimepart(arg1, arg2);
1550         } else if (!strcasecmp(action, "edit_vcard")) {
1551                 edit_vcard();
1552         } else if (!strcasecmp(action, "submit_vcard")) {
1553                 submit_vcard();
1554         } else if (!strcasecmp(action, "select_user_to_edit")) {
1555                 select_user_to_edit(NULL, NULL);
1556         } else if (!strcasecmp(action, "display_edituser")) {
1557                 display_edituser(NULL, 0);
1558         } else if (!strcasecmp(action, "edituser")) {
1559                 edituser();
1560         } else if (!strcasecmp(action, "create_user")) {
1561                 create_user();
1562         } else if (!strcasecmp(action, "changeview")) {
1563                 change_view();
1564         } else if (!strcasecmp(action, "change_start_page")) {
1565                 change_start_page();
1566         } else if (!strcasecmp(action, "display_floorconfig")) {
1567                 display_floorconfig(NULL);
1568         } else if (!strcasecmp(action, "toggle_self_service")) {
1569                 toggle_self_service();
1570 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1571         } else if (!strcasecmp(action, "display_edit_task")) {
1572                 display_edit_task();
1573         } else if (!strcasecmp(action, "save_task")) {
1574                 save_task();
1575         } else if (!strcasecmp(action, "display_edit_event")) {
1576                 display_edit_event();
1577         } else if (!strcasecmp(action, "save_event")) {
1578                 save_event();
1579         } else if (!strcasecmp(action, "respond_to_request")) {
1580                 respond_to_request();
1581         } else if (!strcasecmp(action, "handle_rsvp")) {
1582                 handle_rsvp();
1583 #endif
1584         } else if (!strcasecmp(action, "summary")) {
1585                 summary();
1586         } else if (!strcasecmp(action, "summary_inner_div")) {
1587                 begin_ajax_response();
1588                 summary_inner_div();
1589                 end_ajax_response();
1590         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1591                 display_customize_iconbar();
1592         } else if (!strcasecmp(action, "commit_iconbar")) {
1593                 commit_iconbar();
1594         } else if (!strcasecmp(action, "set_room_policy")) {
1595                 set_room_policy();
1596         } else if (!strcasecmp(action, "display_inetconf")) {
1597                 display_inetconf();
1598         } else if (!strcasecmp(action, "save_inetconf")) {
1599                 save_inetconf();
1600         } else if (!strcasecmp(action, "setup_wizard")) {
1601                 do_setup_wizard();
1602         } else if (!strcasecmp(action, "display_preferences")) {
1603                 display_preferences();
1604         } else if (!strcasecmp(action, "set_preferences")) {
1605                 set_preferences();
1606         } else if (!strcasecmp(action, "recp_autocomplete")) {
1607                 recp_autocomplete(bstr("recp"));
1608         } else if (!strcasecmp(action, "cc_autocomplete")) {
1609                 recp_autocomplete(bstr("cc"));
1610         } else if (!strcasecmp(action, "bcc_autocomplete")) {
1611                 recp_autocomplete(bstr("bcc"));
1612         } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1613                 set_floordiv_expanded(arg1);
1614         } else if (!strcasecmp(action, "diagnostics")) {
1615                 output_headers(1, 1, 1, 0, 0, 0);
1616                 wprintf("Session: %d<hr />\n", WC->wc_session);
1617                 wprintf("Command: <br /><PRE>\n");
1618                 escputs(cmd);
1619                 wprintf("</PRE><hr />\n");
1620                 wprintf("Variables: <br /><PRE>\n");
1621                 dump_vars();
1622                 wprintf("</PRE><hr />\n");
1623                 wDumpContent(1);
1624         }
1625
1626         /** When all else fais, display the main menu. */
1627         else {
1628                 display_main_menu();
1629         }
1630
1631 SKIP_ALL_THIS_CRAP:
1632         fflush(stdout);
1633         if (content != NULL) {
1634                 free(content);
1635                 content = NULL;
1636         }
1637         free_urls();
1638         if (WC->upload_length > 0) {
1639                 free(WC->upload);
1640                 WC->upload_length = 0;
1641         }
1642 }
1643
1644
1645 /*@}*/