* add more module handlers:
[citadel.git] / webcit / webcit.c
index f304e6506c69c532c1cda18315ed757be49b6680..e1e9c5ded3184482631f9182f979f41e40acd405 100644 (file)
@@ -19,7 +19,7 @@
  * the exact minute.  :)
  */
 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
-
+StrBuf *csslocal = NULL;
 HashList *HandlerHash = NULL;
 
 void WebcitAddUrlHandler(const char * UrlString, 
@@ -168,21 +168,34 @@ void output_headers(      int do_httpheaders,     /* 1 = output HTTP headers
                do_template("head", NULL);
 
                /* check for ImportantMessages (these display in a div overlaying the main screen) */
-               if (!IsEmptyStr(WC->ImportantMessage)) {
+               if (!IsEmptyStr(WCC->ImportantMessage)) {
                        wprintf("<div id=\"important_message\">\n"
                                "<span class=\"imsg\">");
-                       escputs(WC->ImportantMessage);
+                       StrEscAppend(WCC->WBuf, NULL, WCC->ImportantMessage, 0, 0);
                        wprintf("</span><br />\n"
                                "</div>\n"
                        );
-                       StrBufAppendPrintf(WCC->trailing_javascript,
-                               "setTimeout('hide_imsg_popup()', 5000); \n"
+                       StrBufAppendBufPlain(WCC->trailing_javascript,
+                                            HKEY("setTimeout('hide_imsg_popup()', 5000);       \n"), 
+                                            0
                        );
                        WCC->ImportantMessage[0] = 0;
                }
-
+               else if (StrLength(WCC->ImportantMsg) > 0) {
+                       wprintf("<div id=\"important_message\">\n"
+                               "<span class=\"imsg\">");
+                       StrEscAppend(WCC->WBuf, WCC->ImportantMsg, NULL, 0, 0);
+                       wprintf("</span><br />\n"
+                               "</div>\n"
+                       );
+                       StrBufAppendBufPlain(WCC->trailing_javascript,
+                                            HKEY("setTimeout('hide_imsg_popup()', 5000);       \n"),
+                                            0
+                       );
+                       FlushStrBuf(WCC->ImportantMsg);
+               }
                if ( (WCC->logged_in) && (!unset_cookies) ) {
-                 //DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
+                       /*DoTemplate(HKEY("iconbar"), NULL, &NoCtx);*/
                        page_popup();
                }
 
@@ -378,7 +391,6 @@ void url_do_template(void) {
        const StrBuf *MimeType;
        const StrBuf *Tmpl = sbstr("template");
        begin_burst();
-       output_headers(0, 0, 0, 0, 1, 0);
        MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
        http_transmit_thing(ChrPtr(MimeType), 0);
 }
@@ -464,12 +476,11 @@ void ajax_servcmd(void)
                }
        case 1:
                while (!Done) {
-                       StrBuf_ServGetlnBuffered(Buf);
+                       StrBuf_ServGetln(Buf);
                        if ( (StrLength(Buf)==3) && 
                             !strcmp(ChrPtr(Buf), "000")) {
                                Done = 1;
                        }
-                       lprintf (1,"ajax: [%s]\n",ChrPtr(Buf));
                        StrBufAppendBuf(WCC->WBuf, Buf, 0);
                        StrBufAppendBufPlain(WCC->WBuf, HKEY("\n"), 0);
                }
@@ -552,8 +563,7 @@ int is_mobile_ua(char *user_agent) {
 /*
  * Entry point for WebCit transaction
  */
-void session_loop(HashList *HTTPHeaders, 
-                 StrBuf *ReqLine, 
+void session_loop(StrBuf *ReqLine, 
                  StrBuf *request_method, 
                  StrBuf *ReadBuf,
                  const char **Pos)
@@ -606,13 +616,6 @@ void session_loop(HashList *HTTPHeaders,
        c_httpauth_pass = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
 
        WCC= WC;
-       if (WCC->WBuf == NULL)
-               WC->WBuf = NewStrBufPlain(NULL, 32768);
-       FlushStrBuf(WCC->WBuf);
-
-       if (WCC->HBuf == NULL)
-               WCC->HBuf = NewStrBuf();
-       FlushStrBuf(WCC->HBuf);
 
        WCC->upload_length = 0;
        WCC->upload = NULL;
@@ -643,28 +646,28 @@ void session_loop(HashList *HTTPHeaders,
        }
 
 
-       if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) && 
+       if (GetHash(WCC->headers, HKEY("COOKIE"), &vLine) && 
            (vLine != NULL)){
                cookie_to_stuff((StrBuf *)vLine, NULL,
                                c_username,
                                c_password,
                                c_roomname);
        }
-       if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) &&
+       if (GetHash(WCC->headers, HKEY("AUTHORIZATION"), &vLine) &&
            (vLine!=NULL)) {
                StrBufDecodeBase64((StrBuf*)vLine);
                StrBufExtract_token(c_httpauth_user, (StrBuf*)vLine, 0, ':');
                StrBufExtract_token(c_httpauth_pass, (StrBuf*)vLine, 1, ':');
        }
-       if (GetHash(HTTPHeaders, HKEY("CONTENT-LENGTH"), &vLine) &&
+       if (GetHash(WCC->headers, HKEY("CONTENT-LENGTH"), &vLine) &&
            (vLine!=NULL)) {
                ContentLength = StrToi((StrBuf*)vLine);
        }
-       if (GetHash(HTTPHeaders, HKEY("CONTENT-TYPE"), &vLine) &&
+       if (GetHash(WCC->headers, HKEY("CONTENT-TYPE"), &vLine) &&
            (vLine!=NULL)) {
                ContentType = (StrBuf*)vLine;
        }
-       if (GetHash(HTTPHeaders, HKEY("USER-AGENT"), &vLine) &&
+       if (GetHash(WCC->headers, HKEY("USER-AGENT"), &vLine) &&
            (vLine!=NULL)) {
                safestrncpy(user_agent, ChrPtr((StrBuf*)vLine), sizeof user_agent);
 #ifdef TECH_PREVIEW
@@ -677,17 +680,17 @@ void session_loop(HashList *HTTPHeaders,
 #endif
        }
        if ((follow_xff) &&
-           GetHash(HTTPHeaders, HKEY("X-FORWARDED-HOST"), &vLine) &&
+           GetHash(WCC->headers, HKEY("X-FORWARDED-HOST"), &vLine) &&
            (vLine != NULL)) {
                WCC->http_host = (StrBuf*)vLine;
        }
        if ((StrLength(WCC->http_host) == 0) && 
-           GetHash(HTTPHeaders, HKEY("HOST"), &vLine) &&
+           GetHash(WCC->headers, HKEY("HOST"), &vLine) &&
            (vLine!=NULL)) {
                WCC->http_host = (StrBuf*)vLine;
        }
 
-       if (GetHash(HTTPHeaders, HKEY("X-FORWARDED-FOR"), &vLine) &&
+       if (GetHash(WCC->headers, HKEY("X-FORWARDED-FOR"), &vLine) &&
            (vLine!=NULL)) {
                browser_host = (StrBuf*) vLine;
 
@@ -794,7 +797,7 @@ void session_loop(HashList *HTTPHeaders,
        }
 
        /* If the client sent a nonce that is incorrect, kill the request. */
-       if (strlen(bstr("nonce")) > 0) {
+       if (havebstr("nonce")) {
                lprintf(9, "Comparing supplied nonce %s to session nonce %ld\n", 
                        bstr("nonce"), WCC->nonce);
                if (ibstr("nonce") != WCC->nonce) {
@@ -850,7 +853,7 @@ void session_loop(HashList *HTTPHeaders,
                        if ( (!follow_xff) || (StrLength(browser_host) == 0) ) {
                                if (browser_host == NULL) {
                                        browser_host = NewStrBuf();
-                                       Put(HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"), 
+                                       Put(WCC->headers, HKEY("FreeMeWithTheOtherHeaders"), 
                                            browser_host, HFreeStrBuf);
                                }
                                locate_host(browser_host, WCC->http_sock);
@@ -951,7 +954,7 @@ void session_loop(HashList *HTTPHeaders,
         * our session's authentication.
         */
        if (!strncasecmp(action, "groupdav", 8)) {
-               groupdav_main(HTTPHeaders, 
+               groupdav_main(WCC->headers, 
                              ReqLine, request_method,
                              ContentType, /* do GroupDAV methods */
                              ContentLength, content, body_start);
@@ -967,7 +970,7 @@ void session_loop(HashList *HTTPHeaders,
         * POST to the GroupDAV code as well.
         */
        if ((strcasecmp(ChrPtr(request_method), "GET")) && (strcasecmp(ChrPtr(request_method), "POST"))) {
-               groupdav_main(HTTPHeaders, ReqLine, 
+               groupdav_main(WCC->headers, ReqLine, 
                              request_method, ContentType, /** do GroupDAV methods */
                              ContentLength, content, body_start);
                if (!WCC->logged_in) {
@@ -1109,7 +1112,8 @@ int ConditionalImportantMesage(StrBuf *Target, WCTemplputParams *TP)
 {
        wcsession *WCC = WC;
        if (WCC != NULL)
-               return (!IsEmptyStr(WCC->ImportantMessage));
+               return ((!IsEmptyStr(WCC->ImportantMessage)) || 
+                       (StrLength(WCC->ImportantMsg) > 0));
        else
                return 0;
 }
@@ -1119,12 +1123,14 @@ void tmplput_importantmessage(StrBuf *Target, WCTemplputParams *TP)
        wcsession *WCC = WC;
        
        if (WCC != NULL) {
-/*
-               StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType,
-                                    WCC->ImportantMessage, 0);
-*/
-               StrEscAppend(Target, NULL, WCC->ImportantMessage, 0, 0);
-               WCC->ImportantMessage[0] = '\0';
+               if (!IsEmptyStr(WCC->ImportantMessage)) {
+                       StrEscAppend(Target, NULL, WCC->ImportantMessage, 0, 0);
+                       WCC->ImportantMessage[0] = '\0';
+               }
+               else if (StrLength(WCC->ImportantMsg) > 0) {
+                       StrEscAppend(Target, WCC->ImportantMsg, NULL, 0, 0);
+                       FlushStrBuf(WCC->ImportantMsg);
+               }
        }
 }
 
@@ -1138,18 +1144,17 @@ void tmplput_trailing_javascript(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_csslocal(StrBuf *Target, WCTemplputParams *TP)
 {
-       extern StrBuf *csslocal;
        StrBufAppendBuf(Target, 
                        csslocal, 0);
 }
 
-
-
+extern char static_local_dir[PATH_MAX];
 
 void 
 InitModule_WEBCIT
 (void)
 {
+       char dir[SIZ];
        WebcitAddUrlHandler(HKEY("blank"), blank_page, ANONYMOUS);
        WebcitAddUrlHandler(HKEY("do_template"), url_do_template, ANONYMOUS);
        WebcitAddUrlHandler(HKEY("sslg"), seconds_since_last_gexp, AJAX);
@@ -1159,4 +1164,68 @@ InitModule_WEBCIT
        RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, CTX_NONE);
        RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage, CTX_NONE);
        RegisterNamespace("TRAILING_JAVASCRIPT", 0, 0, tmplput_trailing_javascript, CTX_NONE);
+
+       snprintf(dir, SIZ, "%s/static.local/webcit.css", static_local_dir);
+       if (!access(dir, R_OK)) {
+               lprintf(9, "Using local Stylesheet [%s]\n", dir);
+               csslocal = NewStrBufPlain(HKEY("<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"));
+       }
+       else
+               lprintf(9, "Didn't find site local Stylesheet [%s]\n", dir);
+
 }
+
+void
+ServerStartModule_WEBCIT
+(void)
+{
+       HandlerHash = NewHash(1, NULL);
+}
+
+
+void 
+ServerShutdownModule_WEBCIT
+(void)
+{
+       FreeStrBuf(&csslocal);
+       DeleteHash(&HandlerHash);
+}
+
+
+
+void
+SessionNewModule_WEBCIT
+(wcsession *sess)
+{
+       sess->ImportantMsg = NewStrBuf();
+       sess->WBuf = NewStrBuf();
+       sess->HBuf = NewStrBuf();
+}
+
+void
+SessionDetachModule_WEBCIT
+(wcsession *sess)
+{
+       if (StrLength(sess->WBuf) > SIZ * 30) /* Bigger than 120K? release. */
+       {
+               FreeStrBuf(&sess->WBuf);
+               sess->WBuf = NewStrBuf();
+       }
+       else
+               FlushStrBuf(sess->WBuf);
+       FlushStrBuf(sess->HBuf);
+}
+
+void 
+SessionDestroyModule_WEBCIT
+(wcsession *sess)
+{
+       FreeStrBuf(&sess->WBuf);
+       FreeStrBuf(&sess->HBuf);
+       FreeStrBuf(&sess->UrlFragment1);
+       FreeStrBuf(&sess->UrlFragment2);
+       FreeStrBuf(&sess->UrlFragment3);
+       FreeStrBuf(&sess->UrlFragment4);
+       FreeStrBuf(&sess->ImportantMsg);
+}
+