* made *bstr things const
[citadel.git] / webcit / webcit.c
index 723bc0db9113acb70e403c13f887a5d24d656a7a..34a66f3158209b21b0bad7ec32a854978265a55e 100644 (file)
@@ -173,7 +173,7 @@ void dump_vars(void)
        urlcontent *u;
        void *U;
        long HKLen;
-       char *HKey;
+       const char *HKey;
        HashPos *Cursor;
        
        Cursor = GetNewHashPos ();
@@ -187,7 +187,7 @@ void dump_vars(void)
  * Return the value of a variable supplied to the current web page (from the url or a form)
  */
 
-const char *XBstr(char *key, size_t keylen, size_t *len)
+const char *XBstr(const char *key, size_t keylen, size_t *len)
 {
        void *U;
 
@@ -202,7 +202,7 @@ const char *XBstr(char *key, size_t keylen, size_t *len)
        }
 }
 
-const char *XBSTR(char *key, size_t *len)
+const char *XBSTR(const char *key, size_t *len)
 {
        void *U;
 
@@ -218,7 +218,7 @@ const char *XBSTR(char *key, size_t *len)
 }
 
 
-const char *BSTR(char *key)
+const char *BSTR(const char *key)
 {
        void *U;
 
@@ -229,7 +229,7 @@ const char *BSTR(char *key)
                return ("");
 }
 
-const char *Bstr(char *key, size_t keylen)
+const char *Bstr(const char *key, size_t keylen)
 {
        void *U;
 
@@ -262,7 +262,7 @@ const StrBuf *SBstr(const char *key, size_t keylen)
                return NULL;
 }
 
-long LBstr(char *key, size_t keylen)
+long LBstr(const char *key, size_t keylen)
 {
        void *U;
 
@@ -273,7 +273,7 @@ long LBstr(char *key, size_t keylen)
                return (0);
 }
 
-long LBSTR(char *key)
+long LBSTR(const char *key)
 {
        void *U;
 
@@ -284,7 +284,7 @@ long LBSTR(char *key)
                return (0);
 }
 
-int IBstr(char *key, size_t keylen)
+int IBstr(const char *key, size_t keylen)
 {
        void *U;
 
@@ -295,7 +295,7 @@ int IBstr(char *key, size_t keylen)
                return (0);
 }
 
-int IBSTR(char *key)
+int IBSTR(const char *key)
 {
        void *U;
 
@@ -306,7 +306,7 @@ int IBSTR(char *key)
                return (0);
 }
 
-int HaveBstr(char *key, size_t keylen)
+int HaveBstr(const char *key, size_t keylen)
 {
        void *U;
 
@@ -317,7 +317,7 @@ int HaveBstr(char *key, size_t keylen)
                return (0);
 }
 
-int HAVEBSTR(char *key)
+int HAVEBSTR(const char *key)
 {
        void *U;
 
@@ -329,7 +329,7 @@ int HAVEBSTR(char *key)
 }
 
 
-int YesBstr(char *key, size_t keylen)
+int YesBstr(const char *key, size_t keylen)
 {
        void *U;
 
@@ -340,7 +340,7 @@ int YesBstr(char *key, size_t keylen)
                return (0);
 }
 
-int YESBSTR(char *key)
+int YESBSTR(const char *key)
 {
        void *U;
 
@@ -409,89 +409,89 @@ void wDumpContent(int print_standard_html_footer)
 }
 
 
 /*
  * Copy a string, escaping characters which have meaning in HTML.  
  *
- * target              target buffer
- * strbuf              source buffer
- * nbsp                        If nonzero, spaces are converted to non-breaking spaces.
- * nolinebreaks                if set, linebreaks are removed from the string.
+ * target              target buffer
+ * strbuf              source buffer
+ * nbsp                        If nonzero, spaces are converted to non-breaking spaces.
+ * nolinebreaks                if set, linebreaks are removed from the string.
  */
 long stresc(char *target, long tSize, char *strbuf, int nbsp, int nolinebreaks)
 {
-       char *aptr, *bptr, *eptr;
-
-       *target = '\0';
-       aptr = strbuf;
-       bptr = target;
-       eptr = target + tSize - 6; // our biggest unit to put in... 
-
-       while ((bptr < eptr) && !IsEmptyStr(aptr) ){
-               if (*aptr == '<') {
-                       memcpy(bptr, "&lt;", 4);
-                       bptr += 4;
-               }
-               else if (*aptr == '>') {
-                       memcpy(bptr, "&gt;", 4);
-                       bptr += 4;
-               }
-               else if (*aptr == '&') {
-                       memcpy(bptr, "&amp;", 5);
-                       bptr += 5;
-               }
-               else if (*aptr == '\"') {
-                       memcpy(bptr, "&quot;", 6);
-                       bptr += 6;
-               }
-               else if (*aptr == '\'') {
-                       memcpy(bptr, "&#39;", 5);
-                       bptr += 5;
-               }
-               else if (*aptr == LB) {
-                       *bptr = '<';
-                       bptr ++;
-               }
-               else if (*aptr == RB) {
-                       *bptr = '>';
-                       bptr ++;
-               }
-               else if (*aptr == QU) {
-                       *bptr ='"';
-                       bptr ++;
-               }
-               else if ((*aptr == 32) && (nbsp == 1)) {
-                       memcpy(bptr, "&nbsp;", 6);
-                       bptr += 6;
-               }
-               else if ((*aptr == '\n') && (nolinebreaks)) {
-                       *bptr='\0';     /* nothing */
-               }
-               else if ((*aptr == '\r') && (nolinebreaks)) {
-                       *bptr='\0';     /* nothing */
-               }
-               else{
-                       *bptr = *aptr;
-                       bptr++;
-               }
-               aptr ++;
-       }
-       *bptr = '\0';
-       if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
-               return -1;
-       return (bptr - target);
+        char *aptr, *bptr, *eptr;
+        *target = '\0';
+        aptr = strbuf;
+        bptr = target;
+        eptr = target + tSize - 6; // our biggest unit to put in... 
+        while ((bptr < eptr) && !IsEmptyStr(aptr) ){
+                if (*aptr == '<') {
+                        memcpy(bptr, "&lt;", 4);
+                        bptr += 4;
+                }
+                else if (*aptr == '>') {
+                        memcpy(bptr, "&gt;", 4);
+                        bptr += 4;
+                }
+                else if (*aptr == '&') {
+                        memcpy(bptr, "&amp;", 5);
+                        bptr += 5;
+                }
+                else if (*aptr == '\"') {
+                        memcpy(bptr, "&quot;", 6);
+                        bptr += 6;
+                }
+                else if (*aptr == '\'') {
+                        memcpy(bptr, "&#39;", 5);
+                        bptr += 5;
+                }
+                else if (*aptr == LB) {
+                        *bptr = '<';
+                        bptr ++;
+                }
+                else if (*aptr == RB) {
+                        *bptr = '>';
+                        bptr ++;
+                }
+                else if (*aptr == QU) {
+                        *bptr ='"';
+                        bptr ++;
+                }
+                else if ((*aptr == 32) && (nbsp == 1)) {
+                        memcpy(bptr, "&nbsp;", 6);
+                        bptr += 6;
+                }
+                else if ((*aptr == '\n') && (nolinebreaks)) {
+                        *bptr='\0';     /* nothing */
+                }
+                else if ((*aptr == '\r') && (nolinebreaks)) {
+                        *bptr='\0';     /* nothing */
+                }
+                else{
+                        *bptr = *aptr;
+                        bptr++;
+                }
+                aptr ++;
+        }
+        *bptr = '\0';
+        if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
+                return -1;
+        return (bptr - target);
 }
 
+
 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
 {
-       char *buf;
-       long Siz;
+       StrEscAppend(WC->WBuf, NULL, strbuf, nbsp, nolinebreaks);
+}
 
-       if (strbuf == NULL) return;
-       Siz = (3 * strlen(strbuf)) + SIZ ;
-       buf = malloc(Siz);
-       stresc(buf, Siz, strbuf, nbsp, nolinebreaks);
-       wprintf("%s", buf);
-       free(buf);
+void StrEscputs1(const StrBuf *strbuf, int nbsp, int nolinebreaks)
+{
+       StrEscAppend(WC->WBuf, strbuf, NULL, nbsp, nolinebreaks);
 }
 
 /* 
@@ -503,24 +503,37 @@ void escputs(char *strbuf)
 }
 
 
+/* 
+ * static wrapper for ecsputs1
+ */
+void StrEscPuts(const StrBuf *strbuf)
+{
+       StrEscputs1(strbuf, 0, 0);
+}
+
+
 /*
  * urlescape buffer and print it to the client
  */
-void urlescputs(char *strbuf)
+void urlescputs(const char *strbuf)
 {
-       char outbuf[SIZ];
-       
-       urlesc(outbuf, SIZ, strbuf);
-       wprintf("%s", outbuf);
+       StrBufUrlescAppend(WC->WBuf, NULL, strbuf);
 }
+
+/*
+ * urlescape buffer and print it to the client
+ */
+void UrlescPutStrBuf(const StrBuf *strbuf)
+{
+       StrBufUrlescAppend(WC->WBuf, strbuf, NULL);
+}
+
 /**
  * urlescape buffer and print it as header 
  */
-void hurlescputs(char *strbuf) {
-       char outbuf[SIZ];
-       
-       urlesc(outbuf, SIZ, strbuf);
-       hprintf("%s", outbuf);
+void hurlescputs(const char *strbuf) 
+{
+       StrBufUrlescAppend(WC->HBuf, NULL, strbuf);
 }
 
 
@@ -593,81 +606,26 @@ void jsescputs(char *strbuf)
        wprintf("%s", outbuf);
 }
 
-/*
- * Copy a string, escaping characters for message text hold
- */
-void msgesc(char *target, size_t tlen, char *strbuf)
-{
-       int len;
-       char *tend;
-       char *send;
-       char *tptr;
-       char *sptr;
-
-       target[0]='\0';
-       len = strlen (strbuf);
-       send = strbuf + len;
-       tend = target + tlen;
-       sptr = strbuf;
-       tptr = target;
-
-       while (!IsEmptyStr(sptr) && 
-              (sptr < send) &&
-              (tptr < tend)) {
-              
-               if (*sptr == '\n')
-                       *tptr = ' ';
-               else if (*sptr == '\r')
-                       *tptr = ' ';
-               else if (*sptr == '\'') {
-                       if (tend - tptr < 8)
-                               return;
-                       *(tptr++) = '&';
-                       *(tptr++) = '#';
-                       *(tptr++) = '3';
-                       *(tptr++) = '9';
-                       *tptr = ';';
-               } else {
-                       *tptr = *sptr;
-               }
-               tptr++; sptr++;
-       }
-       *tptr = '\0';
-}
-
 /*
  * print a string to the client after cleaning it with msgesc() and stresc()
  */
 void msgescputs1( char *strbuf)
 {
-       char *outbuf;
-       char *outbuf2;
-       int buflen;
+       StrBuf *OutBuf = NewStrBuf();
 
-       if (strbuf == NULL) return;
-       buflen = 3 * strlen(strbuf) + SIZ;
-       outbuf = malloc( buflen);
-       outbuf2 = malloc( buflen);
-       msgesc(outbuf, buflen, strbuf);
-       stresc(outbuf2, buflen, outbuf, 0, 0);
-       wprintf("%s", outbuf2);
-       free(outbuf);
-       free(outbuf2);
+       if ((strbuf == NULL) || IsEmptyStr(strbuf))
+               return;
+       StrMsgEscAppend(OutBuf, NULL, strbuf);
+       StrEscAppend(WC->WBuf, OutBuf, NULL, 0, 0);
+       FreeStrBuf(&OutBuf);
 }
 
 /*
  * print a string to the client after cleaning it with msgesc()
  */
 void msgescputs(char *strbuf) {
-       char *outbuf;
-       size_t len;
-
-       if (strbuf == NULL) return;
-       len =  (3 * strlen(strbuf)) + SIZ;
-       outbuf = malloc(len);
-       msgesc(outbuf, len, strbuf);
-       wprintf("%s", outbuf);
-       free(outbuf);
+       if ((strbuf != NULL) && !IsEmptyStr(strbuf))
+               StrMsgEscAppend(WC->WBuf, NULL, strbuf);
 }
 
 
@@ -747,7 +705,7 @@ void output_headers(        int do_httpheaders,     /* 1 = output HTTP headers
 
        /* ICONBAR */
        if (do_htmlhead) {
-
+               begin_burst();
 
                /* check for ImportantMessages (these display in a div overlaying the main screen) */
                if (!IsEmptyStr(WC->ImportantMessage)) {
@@ -999,13 +957,11 @@ void mimepart(const char *msgnum, const char *partnum, int force_download)
        char buf[256];
        off_t bytes;
        char content_type[256];
-       char *content = NULL;
        
        serv_printf("OPNA %s|%s", msgnum, partnum);
        serv_getln(buf, sizeof buf);
        if (buf[0] == '2') {
                bytes = extract_long(&buf[4], 0);
-               content = malloc(bytes + 2);
                if (force_download) {
                        strcpy(content_type, "application/octet-stream");
                }
@@ -1025,7 +981,6 @@ void mimepart(const char *msgnum, const char *partnum, int force_download)
                wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
                end_burst();
        }
-
 }
 
 
@@ -1054,7 +1009,6 @@ char *load_mimepart(long msgnum, char *partnum)
        else {
                return(NULL);
        }
-
 }
 
 
@@ -1094,7 +1048,11 @@ void blank_page(void) {
  * A template has been requested
  */
 void url_do_template(void) {
-       do_template(bstr("template"), NULL);
+       const StrBuf *Tmpl = sbstr("template");
+       begin_burst();
+       output_headers(1, 0, 0, 0, 1, 0);
+       DoTemplate(ChrPtr(Tmpl), StrLength(Tmpl), NULL, NULL);
+       end_burst();
 }
 
 
@@ -1229,14 +1187,14 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
  * Convenience functions to wrap around asynchronous ajax responses
  */
 void begin_ajax_response(void) {
+       struct wcsession *WCC = WC;
+
+       FlushStrBuf(WCC->HBuf);
         output_headers(0, 0, 0, 0, 0, 0);
 
         hprintf("Content-type: text/html; charset=UTF-8\r\n"
                 "Server: %s\r\n"
                 "Connection: close\r\n"
-                "Pragma: no-cache\r\n"
-                "Cache-Control: no-cache\r\n"
-               "Expires: -1\r\n"
                ,
                 PACKAGE_STRING);
         begin_burst();
@@ -1315,7 +1273,6 @@ void seconds_since_last_gexp(void)
 {
        char buf[256];
 
-       begin_ajax_response();
        if ( (time(NULL) - WC->last_pager_check) < 30) {
                wprintf("NO\n");
        }
@@ -1329,7 +1286,6 @@ void seconds_since_last_gexp(void)
                        wprintf("NO");
                }
        }
-       end_ajax_response();
 }
 
 /**
@@ -1499,12 +1455,17 @@ void session_loop(struct httprequest *req)
                content = malloc(BuffSize);
                memset(content, 0, BuffSize);
                snprintf(content,  BuffSize, "Content-type: %s\n"
-                               "Content-length: %d\n\n",
-                               ContentType, ContentLength);
+                        "Content-length: %d\n\n",
+                        ContentType, ContentLength);
+/*
+               hprintf("Content-type: %s\n"
+                       "Content-length: %d\n\n",
+                       ContentType, ContentLength);
+*/
                body_start = strlen(content);
 
                /** Read the entire input data at once. */
-               client_read(WCC->http_sock, &content[body_start], ContentLength);
+               client_read(&WCC->http_sock, &content[body_start], ContentLength);
 
                if (!strncasecmp(ContentType, "application/x-www-form-urlencoded", 33)) {
                        StrBuf *Content;
@@ -1539,6 +1500,7 @@ void session_loop(struct httprequest *req)
                        //cmd[len - a] = '\0';
                        Params = _NewConstStrBuf(&cmd[a + 1], len - a);
                        addurls(Params);
+                       FreeStrBuf(&Params);
                        cmd[a] = 0;
                        len = a - 1;
                }
@@ -1831,9 +1793,9 @@ void diagnostics(void)
        output_headers(1, 1, 1, 0, 0, 0);
        wprintf("Session: %d<hr />\n", WC->wc_session);
        wprintf("Command: <br /><PRE>\n");
-       escputs(WC->UrlFragment1);
+       StrEscPuts(WC->UrlFragment1);
        wprintf("<br />\n");
-       escputs(WC->UrlFragment2);
+       StrEscPuts(WC->UrlFragment2);
        wprintf("</PRE><hr />\n");
        wprintf("Variables: <br /><PRE>\n");
        dump_vars();
@@ -1853,6 +1815,31 @@ void download_mimepart(void) {
                 1);
 }
 
+
+int ConditionalImportantMesage(WCTemplateToken *Tokens, void *Context)
+{
+       struct wcsession *WCC = WC;
+       if (WCC != NULL)
+               return (!IsEmptyStr(WCC->ImportantMessage));
+       else
+               return 0;
+}
+
+void tmplput_importantmessage(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+       struct wcsession *WCC = WC;
+       
+       if (WCC != NULL) {
+               StrEscAppend(Target, NULL, WCC->ImportantMessage, 0, 0);
+                       WCC->ImportantMessage[0] = '\0';
+       }
+}
+
+void tmplput_offer_start_page(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+       offer_start_page();
+}
+
 void 
 InitModule_WEBCIT
 (void)
@@ -1867,4 +1854,7 @@ InitModule_WEBCIT
        WebcitAddUrlHandler(HKEY("mimepart"), view_mimepart, NEED_URL);
        WebcitAddUrlHandler(HKEY("mimepart_download"), download_mimepart, NEED_URL);
        WebcitAddUrlHandler(HKEY("diagnostics"), diagnostics, NEED_URL);
+       RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage);
+       RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage);
+       RegisterNamespace("OFFERSTARTPAGE", 0, 0, tmplput_offer_start_page);
 }