5629c9ec124fd471d6a5582912bc03fe7a3681d4
[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 #define SHOW_ME_VAPPEND_PRINTF
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include "webcit.h"
12 #include "groupdav.h"
13 #include "webserver.h"
14
15
16 /*
17  * String to unset the cookie.
18  * Any date "in the past" will work, so I chose my birthday, right down to
19  * the exact minute.  :)
20  */
21 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
22 StrBuf *csslocal = NULL;
23 HashList *HandlerHash = NULL;
24
25
26 void DeleteWebcitHandler(void *vHandler)
27 {
28         WebcitHandler *Handler = (WebcitHandler*) vHandler;
29         FreeStrBuf(&Handler->Name);
30         free (Handler);
31
32 }
33
34 void WebcitAddUrlHandler(const char * UrlString, 
35                          long UrlSLen, 
36                          WebcitHandlerFunc F, 
37                          long Flags)
38 {
39         WebcitHandler *NewHandler;      
40         NewHandler = (WebcitHandler*) malloc(sizeof(WebcitHandler));
41         NewHandler->F = F;
42         NewHandler->Flags = Flags;
43         NewHandler->Name = NewStrBufPlain(UrlString, UrlSLen);
44         Put(HandlerHash, UrlString, UrlSLen, NewHandler, DeleteWebcitHandler);
45 }
46
47
48 /*
49  * web-printing funcion. uses our vsnprintf wrapper
50  */
51 void wprintf(const char *format,...)
52 {
53         wcsession *WCC = WC;
54         va_list arg_ptr;
55
56         if (WCC->WBuf == NULL)
57                 WCC->WBuf = NewStrBuf();
58
59         va_start(arg_ptr, format);
60         StrBufVAppendPrintf(WCC->WBuf, format, arg_ptr);
61         va_end(arg_ptr);
62 }
63
64 /*
65  * http-header-printing funcion. uses our vsnprintf wrapper
66  */
67 void hprintf(const char *format,...)
68 {
69         wcsession *WCC = WC;
70         va_list arg_ptr;
71
72         va_start(arg_ptr, format);
73         StrBufVAppendPrintf(WCC->HBuf, format, arg_ptr);
74         va_end(arg_ptr);
75 }
76
77
78
79 /*
80  * wrap up an HTTP session, closes tags, etc.
81  *
82  * print_standard_html_footer should be set to:
83  * 0            - to transmit only,
84  * nonzero      - to append the closing tags
85  */
86 void wDumpContent(int print_standard_html_footer)
87 {
88         if (print_standard_html_footer) {
89                 wprintf("</div> <!-- end of 'content' div -->\n");
90                 do_template("trailing", NULL);
91         }
92
93         /* If we've been saving it all up for one big output burst,
94          * go ahead and do that now.
95          */
96         end_burst();
97 }
98
99
100  
101
102 /*
103  * Output HTTP headers and leading HTML for a page
104  */
105 void output_headers(    int do_httpheaders,     /* 1 = output HTTP headers                          */
106                         int do_htmlhead,        /* 1 = output HTML <head> section and <body> opener */
107
108                         int do_room_banner,     /* 0=no, 1=yes,                                     
109                                                  * 2 = I'm going to embed my own, so don't open the 
110                                                  *     <div id="content"> either.                   
111                                                  */
112
113                         int unset_cookies,      /* 1 = session is terminating, so unset the cookies */
114                         int suppress_check,     /* 1 = suppress check for instant messages          */
115                         int cache               /* 1 = allow browser to cache this page             */
116 ) {
117         wcsession *WCC = WC;
118         char cookie[1024];
119         char httpnow[128];
120
121         hprintf("HTTP/1.1 200 OK\n");
122         http_datestring(httpnow, sizeof httpnow, time(NULL));
123
124         if (do_httpheaders) {
125                 if (WCC->serv_info != NULL)
126                         hprintf("Content-type: text/html; charset=utf-8\r\n"
127                                 "Server: %s / %s\n"
128                                 "Connection: close\r\n",
129                                 PACKAGE_STRING, 
130                                 ChrPtr(WCC->serv_info->serv_software));
131                 else
132                         hprintf("Content-type: text/html; charset=utf-8\r\n"
133                                 "Server: %s / [n/a]\n"
134                                 "Connection: close\r\n",
135                                 PACKAGE_STRING);
136         }
137
138         if (cache) {
139                 char httpTomorow[128];
140
141                 http_datestring(httpTomorow, sizeof httpTomorow, 
142                                 time(NULL) + 60 * 60 * 24 * 2);
143
144                 hprintf("Pragma: public\r\n"
145                         "Cache-Control: max-age=3600, must-revalidate\r\n"
146                         "Last-modified: %s\r\n"
147                         "Expires: %s\r\n",
148                         httpnow,
149                         httpTomorow
150                 );
151         }
152         else {
153                 hprintf("Pragma: no-cache\r\n"
154                         "Cache-Control: no-store\r\n"
155                         "Expires: -1\r\n"
156                 );
157         }
158
159         stuff_to_cookie(cookie, 1024, 
160                         WCC->wc_session,
161                         WCC->wc_username,
162                         WCC->wc_password,
163                         WCC->wc_roomname,
164                         get_selected_language()
165         );
166
167         if (unset_cookies) {
168                 hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
169         } else {
170                 hprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
171                 if (server_cookie != NULL) {
172                         hprintf("%s\n", server_cookie);
173                 }
174         }
175
176         if (do_htmlhead) {
177                 begin_burst();
178                 do_template("head", NULL);
179
180                 /* check for ImportantMessages (these display in a div overlaying the main screen) */
181                 if (!IsEmptyStr(WCC->ImportantMessage)) {
182                         wprintf("<div id=\"important_message\">\n"
183                                 "<span class=\"imsg\">");
184                         StrEscAppend(WCC->WBuf, NULL, WCC->ImportantMessage, 0, 0);
185                         wprintf("</span><br />\n"
186                                 "</div>\n"
187                         );
188                         StrBufAppendBufPlain(WCC->trailing_javascript,
189                                              HKEY("setTimeout('hide_imsg_popup()', 5000);       \n"), 
190                                              0
191                         );
192                         WCC->ImportantMessage[0] = 0;
193                 }
194                 else if (StrLength(WCC->ImportantMsg) > 0) {
195                         wprintf("<div id=\"important_message\">\n"
196                                 "<span class=\"imsg\">");
197                         StrEscAppend(WCC->WBuf, WCC->ImportantMsg, NULL, 0, 0);
198                         wprintf("</span><br />\n"
199                                 "</div>\n"
200                         );
201                         StrBufAppendBufPlain(WCC->trailing_javascript,
202                                              HKEY("setTimeout('hide_imsg_popup()', 5000);       \n"),
203                                              0
204                         );
205                         FlushStrBuf(WCC->ImportantMsg);
206                 }
207                 if ( (WCC->logged_in) && (!unset_cookies) ) {
208                         /*DoTemplate(HKEY("iconbar"), NULL, &NoCtx);*/
209                         page_popup();
210                 }
211
212                 if (do_room_banner == 1) {
213                         wprintf("<div id=\"banner\">\n");
214                         embed_room_banner(NULL, navbar_default);
215                         wprintf("</div>\n");
216                 }
217         }
218
219         if (do_room_banner == 1) {
220                 wprintf("<div id=\"content\">\n");
221         }
222 }
223
224 void output_custom_content_header(const char *ctype) {
225   hprintf("HTTP/1.1 200 OK\r\n");
226   hprintf("Content-type: %s; charset=utf-8\r\n",ctype);
227   hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
228   hprintf("Connection: close\r\n");
229 }
230
231
232 /*
233  * Generic function to do an HTTP redirect.  Easy and fun.
234  */
235 void http_redirect(const char *whichpage) {
236         hprintf("HTTP/1.1 302 Moved Temporarily\n");
237         hprintf("Location: %s\r\n", whichpage);
238         hprintf("URI: %s\r\n", whichpage);
239         hprintf("Content-type: text/html; charset=utf-8\r\n");
240         wprintf("<html><body>");
241         wprintf("Go <a href=\"%s\">here</A>.", whichpage);
242         wprintf("</body></html>\n");
243         end_burst();
244 }
245
246
247
248 /*
249  * Output a piece of content to the web browser using conformant HTTP and MIME semantics
250  */
251 void http_transmit_thing(const char *content_type,
252                          int is_static) {
253
254 #ifndef TECH_PREVIEW
255         lprintf(9, "http_transmit_thing(%s)%s\n",
256                 content_type,
257                 (is_static ? " (static)" : "")
258         );
259 #endif
260         output_headers(0, 0, 0, 0, 0, is_static);
261
262         hprintf("Content-type: %s\r\n"
263                 "Server: %s\r\n"
264                 "Connection: close\r\n",
265                 content_type,
266                 PACKAGE_STRING);
267
268         end_burst();
269 }
270
271 /*
272  * print menu box like used in the floor view or admin interface.
273  * This function takes pair of strings as va_args, 
274  * Title        Title string of the box
275  * Class        CSS Class for the box
276  * nLines       How many string pairs should we print? (URL, UrlText)
277  * ...          Pairs of URL Strings and their Names
278  */
279 void print_menu_box(char* Title, char *Class, int nLines, ...)
280 {
281         va_list arg_list;
282         long i;
283         
284         svput("BOXTITLE", WCS_STRING, Title);
285         do_template("beginboxx", NULL);
286         
287         wprintf("<ul class=\"%s\">", Class);
288         
289         va_start(arg_list, nLines);
290         for (i = 0; i < nLines; ++i)
291         { 
292                 wprintf("<li><a href=\"%s\">", va_arg(arg_list, char *));
293                 wprintf((char *) va_arg(arg_list, char *));
294                 wprintf("</a></li>\n");
295         }
296         va_end (arg_list);
297         
298         wprintf("</a></li>\n");
299         
300         wprintf("</ul>");
301         
302         do_template("endbox", NULL);
303 }
304
305
306
307 /*
308  * Convenience functions to display a page containing only a string
309  *
310  * titlebarcolor        color of the titlebar of the frame
311  * titlebarmsg          text to display in the title bar
312  * messagetext          body of the box
313  */
314 void convenience_page(const char *titlebarcolor, const char *titlebarmsg, const char *messagetext)
315 {
316         hprintf("HTTP/1.1 200 OK\n");
317         output_headers(1, 1, 2, 0, 0, 0);
318         wprintf("<div id=\"banner\">\n");
319         wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
320         wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
321         wprintf("</td></tr></table>\n");
322         wprintf("</div>\n<div id=\"content\">\n");
323         escputs(messagetext);
324
325         wprintf("<hr />\n");
326         wDumpContent(1);
327 }
328
329
330 /*
331  * Display a blank page.
332  */
333 void blank_page(void) {
334         output_headers(1, 1, 0, 0, 0, 0);
335         wDumpContent(2);
336 }
337
338
339 /*
340  * A template has been requested
341  */
342 void url_do_template(void) {
343         const StrBuf *MimeType;
344         const StrBuf *Tmpl = sbstr("template");
345         begin_burst();
346         MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
347         http_transmit_thing(ChrPtr(MimeType), 0);
348 }
349
350
351
352 /*
353  * convenience function to indicate success
354  */
355 void display_success(char *successmessage)
356 {
357         convenience_page("007700", "OK", successmessage);
358 }
359
360
361 /*
362  * Authorization required page 
363  * This is probably temporary and should be revisited 
364  */
365 void authorization_required(const char *message)
366 {
367         hprintf("HTTP/1.1 401 Authorization Required\r\n");
368         hprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n", ChrPtr(WC->serv_info->serv_humannode));
369         hprintf("Content-Type: text/html\r\n");
370         wprintf("<h1>");
371         wprintf(_("Authorization Required"));
372         wprintf("</h1>\r\n");
373         wprintf(_("The resource you requested requires a valid username and password. "
374                 "You could not be logged in: %s\n"), message);
375         wDumpContent(0);
376         
377 }
378
379 /*
380  * Convenience functions to wrap around asynchronous ajax responses
381  */
382 void begin_ajax_response(void) {
383         wcsession *WCC = WC;
384
385         FlushStrBuf(WCC->HBuf);
386         output_headers(0, 0, 0, 0, 0, 0);
387
388         hprintf("Content-type: text/html; charset=UTF-8\r\n"
389                 "Server: %s\r\n"
390                 "Connection: close\r\n"
391                 ,
392                 PACKAGE_STRING);
393         begin_burst();
394 }
395
396 /*
397  * print ajax response footer 
398  */
399 void end_ajax_response(void) {
400         wDumpContent(0);
401 }
402
403         /* If it's a "force 404" situation then display the error and bail. */
404 void do_404(void)
405 {
406         hprintf("HTTP/1.1 404 Not found\r\n");
407         hprintf("Content-Type: text/plain\r\n");
408         wprintf("Not found\r\n");
409         end_burst();
410 }
411
412
413 /*
414  * Wraps a Citadel server command in an AJAX transaction.
415  */
416 void ajax_servcmd(void)
417 {
418         wcsession *WCC = WC;
419         int Done = 0;
420         StrBuf *Buf;
421         char *junk;
422         size_t len;
423
424         begin_ajax_response();
425         Buf = NewStrBuf();
426         serv_puts(bstr("g_cmd"));
427         StrBuf_ServGetln(Buf);
428         StrBufAppendBuf(WCC->WBuf, Buf, 0);
429         StrBufAppendBufPlain(WCC->WBuf, HKEY("\n"), 0);
430         
431         switch (GetServerStatus(Buf, NULL)) {
432         case 8:
433                 serv_puts("\n\n000");
434                 if ( (StrLength(Buf)==3) && 
435                      !strcmp(ChrPtr(Buf), "000")) {
436                         StrBufAppendBufPlain(WCC->WBuf, HKEY("\000"), 0);
437                         break;
438                 }
439         case 1:
440                 while (!Done) {
441                         StrBuf_ServGetln(Buf);
442                         if ( (StrLength(Buf)==3) && 
443                              !strcmp(ChrPtr(Buf), "000")) {
444                                 Done = 1;
445                         }
446                         StrBufAppendBuf(WCC->WBuf, Buf, 0);
447                         StrBufAppendBufPlain(WCC->WBuf, HKEY("\n"), 0);
448                 }
449                 break;
450         case 4:
451                 text_to_server(bstr("g_input"));
452                 serv_puts("000");
453                 break;
454         case 6:
455                 len = atol(&ChrPtr(Buf)[4]);
456                 StrBuf_ServGetBLOBBuffered(Buf, len);
457                 break;
458         case 7:
459                 len = atol(&ChrPtr(Buf)[4]);
460                 junk = malloc(len);
461                 memset(junk, 0, len);
462                 serv_write(junk, len);
463                 free(junk);
464         }
465         
466         end_ajax_response();
467         
468         /*
469          * This is kind of an ugly hack, but this is the only place it can go.
470          * If the command was GEXP, then the instant messenger window must be
471          * running, so reset the "last_pager_check" watchdog timer so
472          * that page_popup() doesn't try to open it a second time.
473          */
474         if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
475                 WCC->last_pager_check = time(NULL);
476         }
477         FreeStrBuf(&Buf);
478 }
479
480
481 /*
482  * Helper function for the asynchronous check to see if we need
483  * to open the instant messenger window.
484  */
485 void seconds_since_last_gexp(void)
486 {
487         char buf[256];
488
489         if ( (time(NULL) - WC->last_pager_check) < 30) {
490                 wprintf("NO\n");
491         }
492         else {
493                 memset(buf, 0, 5);
494                 serv_puts("NOOP");
495                 serv_getln(buf, sizeof buf);
496                 if (buf[3] == '*') {
497                         wprintf("YES");
498                 }
499                 else {
500                         wprintf("NO");
501                 }
502         }
503 }
504
505
506
507 void ReadPostData(void)
508 {
509         const char *content_end = NULL;
510         int body_start = 0;
511         wcsession *WCC = WC;
512         StrBuf *content = NULL;
513         
514         content = NewStrBuf();
515
516         StrBufPrintf(content, 
517                      "Content-type: %s\n"
518                      "Content-length: %ld\n\n",
519                      ChrPtr(WCC->Hdr->ContentType), 
520                              WCC->Hdr->ContentLength);
521 /*
522   hprintf("Content-type: %s\n"
523   "Content-length: %d\n\n",
524   ContentType, ContentLength);
525 */
526         body_start = StrLength(content);
527
528         /** Read the entire input data at once. */
529         client_read_to(&WCC->Hdr->http_sock, 
530                        content, 
531                        WCC->Hdr->ReadBuf, &WCC->Hdr->Pos,
532                        WCC->Hdr->ContentLength,
533                        SLEEPING);
534         
535         if (!strncasecmp(ChrPtr(WCC->Hdr->ContentType), "application/x-www-form-urlencoded", 33)) {
536                 StrBufCutLeft(content, body_start);
537                 ParseURLParams(content);
538         } else if (!strncasecmp(ChrPtr(WCC->Hdr->ContentType), "multipart", 9)) {
539                 content_end = ChrPtr(content) + 
540                         WCC->Hdr->ContentLength + 
541                         body_start;
542                 mime_parser(ChrPtr(content), content_end, *upload_handler, NULL, NULL, NULL, 0);
543         }
544         FreeStrBuf(&content);
545 }
546
547
548 /*
549  * Entry point for WebCit transaction
550  */
551 void session_loop(void)
552 {
553         int Flags = 0;
554         int xhttp;
555         StrBuf *Buf;
556         
557         char buf[SIZ];
558
559         /*
560          * We stuff these with the values coming from the client cookies,
561          * so we can use them to reconnect a timed out session if we have to.
562          */
563         wcsession *WCC;
564
565         
566         Buf = NewStrBuf();
567
568         WCC= WC;
569
570         WCC->upload_length = 0;
571         WCC->upload = NULL;
572         WCC->is_mobile = 0;
573         WCC->trailing_javascript = NewStrBuf();
574         WCC->Hdr->nWildfireHeaders = 0;
575         if (WCC->Hdr->Handler != NULL)
576                 Flags = WCC->Hdr->Handler->Flags; /* so we can temporarily add our own... */
577
578         if (WCC->Hdr->ContentLength > 0) {
579                 ReadPostData();
580         }
581
582         /* If there are variables in the URL, we must grab them now */
583         if (WCC->Hdr->PlainArgs != NULL)
584                 ParseURLParams(WCC->Hdr->PlainArgs);
585
586         /* If the client sent a nonce that is incorrect, kill the request. */
587         if (havebstr("nonce")) {
588                 lprintf(9, "Comparing supplied nonce %s to session nonce %ld\n", 
589                         bstr("nonce"), WCC->nonce);
590                 if (ibstr("nonce") != WCC->nonce) {
591                         lprintf(9, "Ignoring request with mismatched nonce.\n");
592                         hprintf("HTTP/1.1 404 Security check failed\r\n");
593                         hprintf("Content-Type: text/plain\r\n\r\n");
594                         wprintf("Security check failed.\r\n");
595                         end_burst();
596                         goto SKIP_ALL_THIS_CRAP;
597                 }
598         }
599
600         /*
601          * If we're not connected to a Citadel server, try to hook up the
602          * connection now.
603          */
604         if (!WCC->connected) {
605                 if (GetConnected ())
606                         goto SKIP_ALL_THIS_CRAP;
607         }
608
609
610         /*
611          * If we're not logged in, but we have authentication data (either from
612          * a cookie or from http-auth), try logging in to Citadel using that.
613          */
614         if ((!WCC->logged_in)
615             && (StrLength(WCC->Hdr->c_username) > 0)
616             && (StrLength(WCC->Hdr->c_password) > 0))
617         {
618                 FlushStrBuf(Buf);
619                 serv_printf("USER %s", ChrPtr(WCC->Hdr->c_username));
620                 StrBuf_ServGetln(Buf);
621                 if (GetServerStatus(Buf, NULL) == 3) {
622                         serv_printf("PASS %s", ChrPtr(WCC->Hdr->c_password));
623                         StrBuf_ServGetln(Buf);
624                         if (GetServerStatus(Buf, NULL) == 2) {
625                                 become_logged_in(WCC->Hdr->c_username,
626                                                  WCC->Hdr->c_password, Buf);
627                         } else {
628                                 /* Should only display when password is wrong */
629                                 authorization_required(&buf[4]);
630                                 FreeStrBuf(&Buf);
631                                 goto SKIP_ALL_THIS_CRAP;
632                         }
633                 }
634         }
635
636         xhttp = (WCC->Hdr->eReqType != eGET) &&
637                 (WCC->Hdr->eReqType != ePOST) &&
638                 (WCC->Hdr->eReqType != eHEAD);
639
640         /*
641          * If a 'gotofirst' parameter has been specified, attempt to goto that room
642          * prior to doing anything else.
643          */
644         if (havebstr("gotofirst")) {
645                 int ret;
646                 ret = gotoroom(sbstr("gotofirst"));     /* do quietly to avoid session output! */
647                 if ((ret/100) != 2) {
648                         lprintf(1, "GOTOFIRST: Unable to change to [%s]; Reason: %d\n",
649                                 bstr("gotofirst"), ret);
650                 }
651         }
652
653         /*
654          * If we aren't in any room yet, but we have cookie data telling us where we're
655          * supposed to be, and 'gotofirst' was not specified, then go there.
656          */
657         else if ( (StrLength(WCC->wc_roomname) == 0) && ( (StrLength(WCC->Hdr->c_roomname) > 0) )) {
658                 lprintf(9, "We are in '%s' but cookie indicates '%s', going there...\n",
659                         ChrPtr(WCC->wc_roomname),
660                         ChrPtr(WCC->Hdr->c_roomname)
661                 );
662                 int ret;
663                 ret = gotoroom(WCC->Hdr->c_roomname);   /* do quietly to avoid session output! */
664                 if ((ret/100) != 2) {
665                         lprintf(1, "COOKIEGOTO: Unable to change to [%s]; Reason: %d\n",
666                                 ChrPtr(WCC->Hdr->c_roomname), ret);
667                 }
668         }
669
670         if (WCC->Hdr->Handler != NULL) {
671                 if (!WCC->logged_in && ((WCC->Hdr->Handler->Flags & ANONYMOUS) == 0)) {
672                         display_login(NULL);
673                 }
674                 else {
675                         if ((WCC->Hdr->Handler->Flags & AJAX) != 0)
676                                 begin_ajax_response();
677                         WCC->Hdr->Handler->F();
678                         if ((WCC->Hdr->Handler->Flags & AJAX) != 0)
679                                 end_ajax_response();
680                 }
681         }
682         /* When all else fais, display the main menu. */
683         else {
684                 if (!WCC->logged_in) 
685                         display_login(NULL);
686                 else
687                         display_main_menu();
688         }
689
690 SKIP_ALL_THIS_CRAP:
691         FreeStrBuf(&Buf);
692         fflush(stdout);
693         WCC->Hdr->http_host = NULL;
694 }
695
696
697 /*
698  * Replacement for sleep() that uses select() in order to avoid SIGALRM
699  */
700 void sleeeeeeeeeep(int seconds)
701 {
702         struct timeval tv;
703
704         tv.tv_sec = seconds;
705         tv.tv_usec = 0;
706         select(0, NULL, NULL, NULL, &tv);
707 }
708
709
710 int ConditionalImportantMesage(StrBuf *Target, WCTemplputParams *TP)
711 {
712         wcsession *WCC = WC;
713         if (WCC != NULL)
714                 return ((!IsEmptyStr(WCC->ImportantMessage)) || 
715                         (StrLength(WCC->ImportantMsg) > 0));
716         else
717                 return 0;
718 }
719
720 void tmplput_importantmessage(StrBuf *Target, WCTemplputParams *TP)
721 {
722         wcsession *WCC = WC;
723         
724         if (WCC != NULL) {
725                 if (!IsEmptyStr(WCC->ImportantMessage)) {
726                         StrEscAppend(Target, NULL, WCC->ImportantMessage, 0, 0);
727                         WCC->ImportantMessage[0] = '\0';
728                 }
729                 else if (StrLength(WCC->ImportantMsg) > 0) {
730                         StrEscAppend(Target, WCC->ImportantMsg, NULL, 0, 0);
731                         FlushStrBuf(WCC->ImportantMsg);
732                 }
733         }
734 }
735
736 void tmplput_trailing_javascript(StrBuf *Target, WCTemplputParams *TP)
737 {
738         wcsession *WCC = WC;
739
740         if (WCC != NULL)
741                 StrBufAppendTemplate(Target, TP, WCC->trailing_javascript, 0);
742 }
743
744 void tmplput_csslocal(StrBuf *Target, WCTemplputParams *TP)
745 {
746         StrBufAppendBuf(Target, 
747                         csslocal, 0);
748 }
749
750 extern char static_local_dir[PATH_MAX];
751
752
753 void 
754 InitModule_WEBCIT
755 (void)
756 {
757         char dir[SIZ];
758         WebcitAddUrlHandler(HKEY("404"), do_404, ANONYMOUS|COOKIEUNNEEDED);
759         WebcitAddUrlHandler(HKEY("blank"), blank_page, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC);
760
761
762         WebcitAddUrlHandler(HKEY("blank"), blank_page, ANONYMOUS);
763         WebcitAddUrlHandler(HKEY("do_template"), url_do_template, ANONYMOUS);
764         WebcitAddUrlHandler(HKEY("sslg"), seconds_since_last_gexp, AJAX|LOGCHATTY);
765         WebcitAddUrlHandler(HKEY("ajax_servcmd"), ajax_servcmd, 0);
766
767         RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage, CTX_NONE);
768         RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, CTX_NONE);
769         RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage, CTX_NONE);
770         RegisterNamespace("TRAILING_JAVASCRIPT", 0, 0, tmplput_trailing_javascript, CTX_NONE);
771
772         snprintf(dir, SIZ, "%s/static.local/webcit.css", static_local_dir);
773         if (!access(dir, R_OK)) {
774                 lprintf(9, "Using local Stylesheet [%s]\n", dir);
775                 csslocal = NewStrBufPlain(HKEY("<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"));
776         }
777         else
778                 lprintf(9, "Didn't find site local Stylesheet [%s]\n", dir);
779
780 }
781
782 void
783 ServerStartModule_WEBCIT
784 (void)
785 {
786         HandlerHash = NewHash(1, NULL);
787 }
788
789
790 void 
791 ServerShutdownModule_WEBCIT
792 (void)
793 {
794         FreeStrBuf(&csslocal);
795         DeleteHash(&HandlerHash);
796 }
797
798
799
800 void
801 SessionNewModule_WEBCIT
802 (wcsession *sess)
803 {
804         sess->ImportantMsg = NewStrBuf();
805         sess->WBuf = NewStrBuf();
806         sess->HBuf = NewStrBuf();
807 }
808
809 void
810 SessionDetachModule_WEBCIT
811 (wcsession *sess)
812 {
813         DeleteHash(&sess->Hdr->urlstrings);// TODO?
814         if (sess->upload_length > 0) {
815                 free(sess->upload);
816                 sess->upload_length = 0;
817         }
818         FreeStrBuf(&sess->trailing_javascript);
819
820         if (StrLength(sess->WBuf) > SIZ * 30) /* Bigger than 120K? release. */
821         {
822                 FreeStrBuf(&sess->WBuf);
823                 sess->WBuf = NewStrBuf();
824         }
825         else
826                 FlushStrBuf(sess->WBuf);
827         FlushStrBuf(sess->HBuf);
828 }
829
830 void 
831 SessionDestroyModule_WEBCIT
832 (wcsession *sess)
833 {
834         FreeStrBuf(&sess->WBuf);
835         FreeStrBuf(&sess->HBuf);
836         FreeStrBuf(&sess->ImportantMsg);
837 }
838