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