]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/imap/imap_fetch.c
* imap_load_part() is expecting the desired_section to be a char*, not a ChrPtr....
[citadel.git] / citadel / modules / imap / imap_fetch.c
index 1fa4916b960186f085b5c226a0afe79b7b20a1c5..66dd774fa03b5a6bd5154da1544f6584b5bc922e 100644 (file)
@@ -55,7 +55,6 @@
 #include "support.h"
 #include "config.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
@@ -103,7 +102,7 @@ void imap_fetch_flags(int seq) {
 
 
 void imap_fetch_internaldate(struct CtdlMessage *msg) {
-       char buf[SIZ];
+       char datebuf[64];
        time_t msgdate;
 
        if (!msg) return;
@@ -114,8 +113,8 @@ void imap_fetch_internaldate(struct CtdlMessage *msg) {
                msgdate = time(NULL);
        }
 
-       datestring(buf, sizeof buf, msgdate, DATESTRING_IMAP);
-       cprintf("INTERNALDATE \"%s\"", buf);
+       datestring(datebuf, sizeof datebuf, msgdate, DATESTRING_IMAP);
+       cprintf("INTERNALDATE \"%s\"", datebuf);
 }
 
 
@@ -129,13 +128,13 @@ void imap_fetch_internaldate(struct CtdlMessage *msg) {
  *     "RFC822.TEXT"   body only (without leading blank line)
  */
 void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
-       char buf[SIZ];
        const char *ptr = NULL;
        size_t headers_size, text_size, total_size;
        size_t bytes_to_send = 0;
        struct MetaData smi;
        int need_to_rewrite_metadata = 0;
        int need_body = 0;
+       CitContext *CCC = CC;
 
        /* Determine whether this particular fetch operation requires
         * us to fetch the message body from disk.  If not, we can save
@@ -165,38 +164,37 @@ void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
         * client requests something that involves reading the message
         * body, but we haven't fetched the body yet.
         */
-       if ((IMAP->cached_rfc822_data != NULL)
+       if ((IMAP->cached_rfc822 != NULL)
           && (IMAP->cached_rfc822_msgnum == msgnum)
           && (IMAP->cached_rfc822_withbody || (!need_body)) ) {
                /* Good to go! */
        }
-       else if (IMAP->cached_rfc822_data != NULL) {
+       else if (IMAP->cached_rfc822 != NULL) {
                /* Some other message is cached -- free it */
-               free(IMAP->cached_rfc822_data);
-               IMAP->cached_rfc822_data = NULL;
+               FreeStrBuf(&IMAP->cached_rfc822);
                IMAP->cached_rfc822_msgnum = (-1);
-               IMAP->cached_rfc822_len = 0;
        }
 
        /* At this point, we now can fetch and convert the message iff it's not
         * the one we had cached.
         */
-       if (IMAP->cached_rfc822_data == NULL) {
+       if (IMAP->cached_rfc822 == NULL) {
                /*
                 * Load the message into memory for translation & measurement
                 */
-               CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
+               CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
                CtdlOutputMsg(msgnum, MT_RFC822,
                        (need_body ? HEADERS_ALL : HEADERS_FAST),
                        0, 1, NULL, SUPPRESS_ENV_TO
                );
                if (!need_body) cprintf("\r\n");        /* extra trailing newline */
-               IMAP->cached_rfc822_len = StrLength(CC->redirect_buffer);
-               IMAP->cached_rfc822_data = SmashStrBuf(&CC->redirect_buffer);
+               IMAP->cached_rfc822 = CCC->redirect_buffer;
+               CCC->redirect_buffer = NULL;
                IMAP->cached_rfc822_msgnum = msgnum;
                IMAP->cached_rfc822_withbody = need_body;
-               if ( (need_to_rewrite_metadata) && (IMAP->cached_rfc822_len > 0) ) {
-                       smi.meta_rfc822_length = (long)IMAP->cached_rfc822_len;
+               if ( (need_to_rewrite_metadata) && 
+                    (StrLength(IMAP->cached_rfc822) > 0) ) {
+                       smi.meta_rfc822_length = StrLength(IMAP->cached_rfc822);
                        PutMetaData(&smi);
                }
        }
@@ -210,23 +208,30 @@ void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
        total_size = 0;
 
        if (need_body) {
-               ptr = IMAP->cached_rfc822_data;
+               StrBuf *Line = NewStrBuf();
+               ptr = NULL;
                do {
-                       ptr = memreadline(ptr, buf, sizeof buf);
-                       if (*ptr != 0) {
-                               striplt(buf);
-                               if (IsEmptyStr(buf)) {
-                                       headers_size = ptr - IMAP->cached_rfc822_data;
+                       StrBufSipLine(Line, IMAP->cached_rfc822, &ptr);
+
+                       if ((StrLength(Line) != 0)  && (ptr != StrBufNOTNULL))
+                       {
+                               StrBufTrim(Line);
+                               if ((StrLength(Line) != 0) && 
+                                   (ptr != StrBufNOTNULL)    )
+                               {
+                                       headers_size = ptr - ChrPtr(IMAP->cached_rfc822);
                                }
                        }
-               } while ( (headers_size == 0) && (*ptr != 0) );
+               } while ( (headers_size == 0)    && 
+                         (ptr != StrBufNOTNULL) );
 
-               total_size = IMAP->cached_rfc822_len;
+               total_size = StrLength(IMAP->cached_rfc822);
                text_size = total_size - headers_size;
+               FreeStrBuf(&Line);
        }
        else {
-               headers_size = IMAP->cached_rfc822_len;
-               total_size = IMAP->cached_rfc822_len;
+               headers_size = 
+                       total_size = StrLength(IMAP->cached_rfc822);
                text_size = 0;
        }
 
@@ -242,17 +247,17 @@ void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
        }
 
        else if (!strcasecmp(whichfmt, "RFC822")) {
-               ptr = IMAP->cached_rfc822_data;
+               ptr = ChrPtr(IMAP->cached_rfc822);
                bytes_to_send = total_size;
        }
 
        else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
-               ptr = IMAP->cached_rfc822_data;
+               ptr = ChrPtr(IMAP->cached_rfc822);
                bytes_to_send = headers_size;
        }
 
        else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
-               ptr = &IMAP->cached_rfc822_data[headers_size];
+               ptr = &ChrPtr(IMAP->cached_rfc822)[headers_size];
                bytes_to_send = text_size;
        }
 
@@ -274,18 +279,22 @@ void imap_load_part(char *name, char *filename, char *partnum, char *disp,
                    void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                    char *cbid, void *cbuserdata)
 {
-       char mbuf2[SIZ];
+       char mimebuf2[SIZ];
        char *desired_section;
 
        desired_section = (char *)cbuserdata;
+       CtdlLogPrintf(CTDL_DEBUG, "imap_load_part() looking for %s, found %s\n",
+               desired_section,
+               partnum
+       );
 
        if (!strcasecmp(partnum, desired_section)) {
                client_write(content, length);
        }
 
-       snprintf(mbuf2, sizeof mbuf2, "%s.MIME", partnum);
+       snprintf(mimebuf2, sizeof mimebuf2, "%s.MIME", partnum);
 
-       if (!strcasecmp(desired_section, mbuf2)) {
+       if (!strcasecmp(desired_section, mimebuf2)) {
                cprintf("Content-type: %s", cbtype);
                if (!IsEmptyStr(cbcharset))
                        cprintf("; charset=\"%s\"", cbcharset);
@@ -589,6 +598,7 @@ void imap_strip_headers(StrBuf *section) {
        FreeStrBuf(&CCC->redirect_buffer);
        CCC->redirect_buffer = boiled_headers;
 
+       free(Cmd.Params);
        FreeStrBuf(&which_fields);
        FreeStrBuf(&Line);
 }
@@ -614,9 +624,8 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        if (strchr(ChrPtr(section), '[') != NULL) {
                StrBufStripAllBut(section, '[', ']');
        }
-       CtdlLogPrintf(CTDL_DEBUG, "Section is: %s%s\n", 
-                     ChrPtr(section), 
-                     (StrLength(section) == 0) ? "(empty)" : ""
+       CtdlLogPrintf(CTDL_DEBUG, "Section is: [%s]\n", 
+             (StrLength(section) == 0) ? "(empty)" : ChrPtr(section)
        );
 
        /* Burn the cache if we don't have the same section of the 
@@ -648,11 +657,9 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
                StrBufStripAllBut(partial, '<', '>');
                is_partial = 1;
        }
-       if (is_partial == 0) 
-               if (StrLength(partial) > 0) 
-                       CtdlLogPrintf(CTDL_DEBUG, 
-                                     "Partial is %s\n", 
-                                     ChrPtr(partial));
+       if ( (is_partial == 1) && (StrLength(partial) > 0) ) {
+               CtdlLogPrintf(CTDL_DEBUG, "Partial is <%s>\n", ChrPtr(partial));
+       }
 
        if (IMAP->cached_body == NULL) {
                CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
@@ -701,8 +708,9 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        else {
                mime_parser(msg->cm_fields['M'], NULL,
                                *imap_load_part, NULL, NULL,
-                               section,
-                               1);
+                               ChrPtr(section),
+                               1
+               );
        }
 
        if (loading_body_now) {
@@ -739,6 +747,7 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        if (is_peek == 0) {
                CtdlSetSeen(&msgnum, 1, 1, ctdlsetseen_seen, NULL, NULL);
        }
+       FreeStrBuf(&section);
 }
 
 /*
@@ -939,7 +948,7 @@ void imap_fetch_bodystructure (long msgnum, const char *item,
 
                ptr = rfc822;
                do {
-                       ptr = memreadline(ptr, buf, sizeof buf);
+                       ptr = cmemreadline(ptr, buf, sizeof buf);
                        ++lines;
                        if ((IsEmptyStr(buf)) && (rfc822_body == NULL)) {
                                rfc822_body = ptr;