]> code.citadel.org Git - citadel.git/blobdiff - webcit/webcit.c
* migrate message creation to templates (citing still missing)
[citadel.git] / webcit / webcit.c
index 20b9342e2943e055232eb63b0f3378ff15e7c6a0..c4fd3648c431acc66e7a9bf098c6cb35c8ebf812 100644 (file)
@@ -95,19 +95,15 @@ void free_url(void *U)
 /*
  * Extract variables from the URL.
  */
-void addurls(StrBuf *url)
+void ParseURLParams(StrBuf *url)
 {
        const char *aptr, *bptr, *eptr, *up;
-///    char *buf;
        int len, keylen;
        urlcontent *u;
        struct wcsession *WCC = WC;
 
        if (WCC->urlstrings == NULL)
                WCC->urlstrings = NewHash(1, NULL);
-//     buf = (char*) malloc (ulen + 1);
-//     memcpy(buf, url, ulen);
-///    buf[ulen] = '\0';
        eptr = ChrPtr(url) + StrLength(url);
        up = ChrPtr(url);
        while ((up < eptr) && (!IsEmptyStr(up))) {
@@ -115,17 +111,14 @@ void addurls(StrBuf *url)
                while ((aptr < eptr) && (*aptr != '\0') && (*aptr != '='))
                        aptr++;
                if (*aptr != '=') {
-                       ///free(buf);
                        return;
                }
-               ///*aptr = '\0';
                aptr++;
                bptr = aptr;
                while ((bptr < eptr) && (*bptr != '\0')
                      && (*bptr != '&') && (*bptr != '?') && (*bptr != ' ')) {
                        bptr++;
                }
-               //*bptr = '\0';
                keylen = aptr - up - 1; /* -1 -> '=' */
                if(keylen > sizeof(u->url_key)) {
                        lprintf(1, "URLkey to long! [%s]", up);
@@ -155,7 +148,6 @@ void addurls(StrBuf *url)
                        ChrPtr(u->url_data)); 
 #endif
        }
-       //free(buf);
 }
 
 /*
@@ -390,18 +382,22 @@ void hprintf(const char *format,...)
 }
 
 
+void put_trailing_javascript(void) {
+       wprintf("%s", ChrPtr(WC->trailing_javascript));
+}
+
 /*
  * wrap up an HTTP session, closes tags, etc.
  *
  * print_standard_html_footer should be set to:
- * 0 to transmit only,
- * 1 to append the main menu and closing tags,
- * 2 to append the closing tags only.
+ * 0           - to transmit only,
+ * nonzero     - to append the closing tags
  */
 void wDumpContent(int print_standard_html_footer)
 {
        if (print_standard_html_footer) {
-               wprintf("</div>\n");    /* end of "text" div */
+               wprintf("</div> <!-- end of 'content' div -->\n");
+               svcallback("TRAILING_JAVASCRIPT", put_trailing_javascript);
                do_template("trailing", NULL);
        }
 
@@ -699,11 +695,6 @@ void output_headers(       int do_httpheaders,     /* 1 = output HTTP headers
 
        if (do_htmlhead) {
                begin_burst();
-               if (!access("static.local/webcit.css", R_OK)) {
-                       svprintf(HKEY("CSSLOCAL"), WCS_STRING,
-                          "<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"
-                       );
-               }
                do_template("head", NULL);
        }
 
@@ -767,6 +758,10 @@ void http_redirect(const char *whichpage) {
 void http_transmit_thing(const char *content_type,
                         int is_static) {
 
+       lprintf(9, "http_transmit_thing(%s)%s\n",
+               content_type,
+               (is_static ? " (static)" : "")
+       );
        output_headers(0, 0, 0, 0, 0, is_static);
 
        hprintf("Content-type: %s\r\n"
@@ -861,9 +856,7 @@ void output_static(char *what)
 
 
                close(fd);
-#ifndef TECH_PREVIEW
                lprintf(9, "output_static('%s')  %s\n", what, content_type);
-#endif
                http_transmit_thing(content_type, 1);
        }
        if (yesbstr("force_close_session")) {
@@ -950,6 +943,43 @@ void display_vcard_photo_img(void)
        free(photosrc);
 }
 
+/*
+ * Generic function to output an arbitrary MIME attachment from
+ * message being composed
+ *
+ * partnum             The MIME part to be output
+ * filename            Fake filename to give
+ * force_download      Nonzero to force set the Content-Type: header to "application/octet-stream"
+ */
+void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
+{
+       void *vPart;
+       char content_type[256];
+       wc_attachment *part;
+       
+       if (GetHash(WC->attachments, SKEY(partnum), &vPart) &&
+           (vPart != NULL)) {
+               part = (wc_attachment*) vPart;
+               if (force_download) {
+                       strcpy(content_type, "application/octet-stream");
+               }
+               else {
+                       strncpy(content_type, ChrPtr(part->content_type), sizeof content_type);
+               }
+               output_headers(0, 0, 0, 0, 0, 0);
+               StrBufAppendBufPlain(WC->WBuf, part->data, part->length, 0);
+               http_transmit_thing(content_type, 0);
+       } else {
+               hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
+               output_headers(0, 0, 0, 0, 0, 0);
+               hprintf("Content-Type: text/plain\r\n");
+               wprintf(_("An error occurred while retrieving this part: %s/%s\n"), 
+                       ChrPtr(partnum), ChrPtr(filename));
+               end_burst();
+       }
+}
+
+
 /*
  * Generic function to output an arbitrary MIME part from an arbitrary
  * message number on the server.
@@ -1017,6 +1047,30 @@ char *load_mimepart(long msgnum, char *partnum)
        }
 }
 
+/*
+ * Read any MIME part of a message, from the server, into memory.
+ */
+void MimeLoadData(wc_mime_attachment *Mime)
+{
+       char buf[SIZ];
+       off_t bytes;
+//// TODO: is there a chance the contenttype is different  to the one we know? 
+       serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '6') {
+               bytes = extract_long(&buf[4], 0);
+
+               if (Mime->Data == NULL)
+                       Mime->Data = NewStrBufPlain(NULL, bytes);
+               StrBuf_ServGetBLOB(Mime->Data, bytes);
+
+       }
+       else {
+               FlushStrBuf(Mime->Data);
+               /// TODO XImportant message
+       }
+}
+
 
 /*
  * Convenience functions to display a page containing only a string
@@ -1057,7 +1111,7 @@ void url_do_template(void) {
        const StrBuf *Tmpl = sbstr("template");
        begin_burst();
        output_headers(1, 0, 0, 0, 1, 0);
-       DoTemplate(ChrPtr(Tmpl), StrLength(Tmpl), NULL, NULL);
+       DoTemplate(ChrPtr(Tmpl), StrLength(Tmpl), NULL, NULL, 0);
        end_burst();
 }
 
@@ -1066,7 +1120,7 @@ void url_do_template(void) {
 /*
  * Offer to make any page the user's "start page."
  */
-void offer_start_page(void) {
+void offer_start_page(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType) {
        wprintf("<a href=\"change_start_page?startpage=");
        urlescputs(WC->this_page);
        wprintf("\">");
@@ -1145,11 +1199,12 @@ void authorization_required(const char *message)
  * cbcharset   Character set
  * length      Content length
  * encoding    MIME encoding type (not needed)
+ * cbid                Content ID (not needed)
  * userdata    Not used here
  */
 void upload_handler(char *name, char *filename, char *partnum, char *disp,
                        void *content, char *cbtype, char *cbcharset,
-                       size_t length, char *encoding, void *userdata)
+                       size_t length, char *encoding, char *cbid, void *userdata)
 {
        urlcontent *u;
 #ifdef DEBUG_URLSTRINGS
@@ -1285,6 +1340,7 @@ void seconds_since_last_gexp(void)
                wprintf("NO\n");
        }
        else {
+               memset(buf, 5, 0);
                serv_puts("NOOP");
                serv_getln(buf, sizeof buf);
                if (buf[3] == '*') {
@@ -1330,7 +1386,8 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
        char buf[SIZ];
        int a, nBackDots, nEmpty;
        int ContentLength = 0;
-       StrBuf *ContentType, *UrlLine;
+       StrBuf *ContentType = NULL;
+       StrBuf *UrlLine = NULL;
        StrBuf *content = NULL;
        const char *content_end = NULL;
        char browser_host[256];
@@ -1371,6 +1428,7 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
        WCC->upload_length = 0;
        WCC->upload = NULL;
        WCC->is_mobile = 0;
+       WCC->trailing_javascript = NewStrBuf();
 
        /** Figure out the action */
        index[0] = action;
@@ -1432,7 +1490,9 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
        if ((follow_xff) &&
            GetHash(HTTPHeaders, HKEY("X-FORWARDED-HOST"), &vLine) &&
            (vLine != NULL)) {
-               safestrncpy(WCC->http_host, &buf[18], sizeof WCC->http_host);
+               safestrncpy(WCC->http_host, 
+                           ChrPtr((StrBuf*)vLine), 
+                           sizeof WCC->http_host);
        }
        if (IsEmptyStr(WCC->http_host) && 
            GetHash(HTTPHeaders, HKEY("HOST"), &vLine) &&
@@ -1466,11 +1526,11 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
                body_start = StrLength(content);
 
                /** Read the entire input data at once. */
-               client_read(&WCC->http_sock, content, ReadBuf, ContentLength);
+               client_read(&WCC->http_sock, content, ReadBuf, ContentLength + body_start);
 
                if (!strncasecmp(ChrPtr(ContentType), "application/x-www-form-urlencoded", 33)) {
                        StrBufCutLeft(content, body_start);
-                       addurls(content);
+                       ParseURLParams(content);
                } else if (!strncasecmp(ChrPtr(ContentType), "multipart", 9)) {
                        content_end = ChrPtr(content) + ContentLength + body_start;
                        mime_parser(ChrPtr(content), content_end, *upload_handler, NULL, NULL, NULL, 0);
@@ -1492,7 +1552,7 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
        while (pch < pche) {
                if ((*pch == '?') || (*pch == '&')) {
                        StrBufCutLeft(UrlLine, pch - pchs + 1);
-                       addurls(UrlLine);
+                       ParseURLParams(UrlLine);
                        break;
                }
                pch ++;
@@ -1594,6 +1654,7 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
 
                        get_serv_info(browser_host, user_agent);
                        if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
+                               begin_burst();
                                wprintf(_("You are connected to a Citadel "
                                        "server running Citadel %d.%02d. \n"
                                        "In order to run this version of WebCit "
@@ -1604,6 +1665,7 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
                                                MINIMUM_CIT_VERSION / 100,
                                                MINIMUM_CIT_VERSION % 100
                                        );
+                               end_burst();
                                end_webcit_session();
                                goto SKIP_ALL_THIS_CRAP;
                        }
@@ -1704,6 +1766,7 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
                                        set_selected_language(ChrPtr(Lang));
                                        go_selected_language();         /* set locale */
                                }
+                               get_preference("default_header_charset", &WCC->DefaultCharset);
                        }
                }
        }
@@ -1769,6 +1832,7 @@ SKIP_ALL_THIS_CRAP:
                free(WCC->upload);
                WCC->upload_length = 0;
        }
+       FreeStrBuf(&WCC->trailing_javascript);
 }
 
 
@@ -1811,8 +1875,20 @@ void download_mimepart(void) {
                 1);
 }
 
+void view_postpart(void) {
+       postpart(WC->UrlFragment1,
+                WC->UrlFragment2,
+                0);
+}
+
+void download_postpart(void) {
+       postpart(WC->UrlFragment1,
+                WC->UrlFragment2,
+                1);
+}
+
 
-int ConditionalImportantMesage(WCTemplateToken *Tokens, void *Context)
+int ConditionalImportantMesage(WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        struct wcsession *WCC = WC;
        if (WCC != NULL)
@@ -1821,7 +1897,7 @@ int ConditionalImportantMesage(WCTemplateToken *Tokens, void *Context)
                return 0;
 }
 
-void tmplput_importantmessage(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_importantmessage(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        struct wcsession *WCC = WC;
        
@@ -1831,30 +1907,37 @@ void tmplput_importantmessage(StrBuf *Target, int nArgs, WCTemplateToken *Tokens
        }
 }
 
-void tmplput_offer_start_page(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+int ConditionalBstr(WCTemplateToken *Tokens, void *Context, int ContextType)
 {
-       offer_start_page();
-}
-
-
-int ConditionalBstr(WCTemplateToken *Tokens, void *Context)
-{
-       if(Tokens->nParameters == 1)
-               return HaveBstr(Tokens->Params[0]->Start, 
-                               Tokens->Params[0]->len);
+       if(Tokens->nParameters == 3)
+               return HaveBstr(Tokens->Params[2]->Start, 
+                               Tokens->Params[2]->len);
        else
-               return strcmp(Bstr(Tokens->Params[0]->Start, 
-                                  Tokens->Params[0]->len),
-                             Tokens->Params[1]->Start) == 0;
+               return strcmp(Bstr(Tokens->Params[2]->Start, 
+                                  Tokens->Params[2]->len),
+                             Tokens->Params[3]->Start) == 0;
 }
 
-void tmplput_bstr(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_bstr(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrBufAppendBuf(Target, 
                        SBstr(Tokens->Params[0]->Start, 
                              Tokens->Params[0]->len), 0);
 }
 
+
+void tmplput_csslocal(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       extern StrBuf *csslocal;
+       StrBufAppendBuf(Target, 
+                       csslocal, 0);
+}
+
+
+
+
+
+
 void 
 InitModule_WEBCIT
 (void)
@@ -1868,10 +1951,14 @@ InitModule_WEBCIT
        WebcitAddUrlHandler(HKEY("vcardphoto"), display_vcard_photo_img, NEED_URL);
        WebcitAddUrlHandler(HKEY("mimepart"), view_mimepart, NEED_URL);
        WebcitAddUrlHandler(HKEY("mimepart_download"), download_mimepart, NEED_URL);
+       WebcitAddUrlHandler(HKEY("postpart"), view_postpart, NEED_URL);
+       WebcitAddUrlHandler(HKEY("postpart_download"), download_postpart, NEED_URL);
        WebcitAddUrlHandler(HKEY("diagnostics"), diagnostics, NEED_URL);
-       RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage);
-       RegisterConditional(HKEY("COND:BSTR"), 1, ConditionalBstr);
-       RegisterNamespace("BSTR", 1, 2, tmplput_bstr);
-       RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage);
-       RegisterNamespace("OFFERSTARTPAGE", 0, 0, tmplput_offer_start_page);
+
+       RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage, CTX_NONE);
+       RegisterConditional(HKEY("COND:BSTR"), 1, ConditionalBstr, CTX_NONE);
+       RegisterNamespace("BSTR", 1, 2, tmplput_bstr, CTX_NONE);
+       RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, CTX_NONE);
+       RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage, CTX_NONE);
+       RegisterNamespace("OFFERSTARTPAGE", 0, 0, offer_start_page, CTX_NONE);
 }