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