]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/imap/imap_fetch.c
* imap_fetch_rfc822(): migrate to strbuf
[citadel.git] / citadel / modules / imap / imap_fetch.c
index d0afd3207b64d936c831e229b7cf1a5e50056274..ad175f20434e435acf8537c7b1bf7ecd0eefd7fa 100644 (file)
@@ -103,7 +103,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 +114,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 +129,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 +165,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 +209,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 +248,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,7 +280,7 @@ 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;
@@ -283,9 +289,9 @@ void imap_load_part(char *name, char *filename, char *partnum, char *disp,
                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);
@@ -504,7 +510,6 @@ void imap_fetch_envelope(struct CtdlMessage *msg) {
  */
 void imap_strip_headers(StrBuf *section) {
        citimap_command Cmd;
-       char buf[SIZ];
        StrBuf *which_fields = NULL;
        int doing_headers = 0;
        int headers_not = 0;
@@ -515,14 +520,16 @@ void imap_strip_headers(StrBuf *section) {
        int ok = 0;
        int done_headers = 0;
        const char *Ptr = NULL;
+       CitContext *CCC = CC;
 
-       if (CC->redirect_buffer == NULL) return;
+       if (CCC->redirect_buffer == NULL) return;
 
        which_fields = NewStrBufDup(section);
 
        if (!strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS", 13))
                doing_headers = 1;
-       if (!strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS.NOT", 17))
+       if (doing_headers && 
+           !strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS.NOT", 17))
                headers_not = 1;
 
        for (i=0; i < StrLength(which_fields); ++i) {
@@ -539,25 +546,33 @@ void imap_strip_headers(StrBuf *section) {
        Cmd.CmdBuf = which_fields;
        num_parms = imap_parameterize(&Cmd);
 
-       boiled_headers = NewStrBufPlain(NULL, StrLength(CC->redirect_buffer));
+       boiled_headers = NewStrBufPlain(NULL, StrLength(CCC->redirect_buffer));
+       Line = NewStrBufPlain(NULL, SIZ);
        Ptr = NULL;
        ok = 0;
        do {
-               StrBufSipLine(CC->redirect_buffer, Line, &Ptr);
+               StrBufSipLine(Line, CCC->redirect_buffer, &Ptr);
 
                if (!isspace(ChrPtr(Line)[0])) {
                        ok = 0;
                        if (doing_headers == 0) ok = 1;
                        else {
-                               if (headers_not) ok = 1;
-                               else ok = 0;
-                               for (i=0; i<num_parms; ++i) {
-                                       if ( (!strncasecmp(buf, 
-                                                          Cmd.Params[i].Key,
-                                                          Cmd.Params[i].len)) &&
-                                            (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
-                                               if (headers_not) ok = 0;
-                                               else ok = 1;
+                               /* we're supposed to print all headers that are not matching the filter list */
+                               if (headers_not) for (i=0, ok = 1; (i < num_parms) && (ok == 1); ++i) {
+                                               if ( (!strncasecmp(ChrPtr(Line), 
+                                                                  Cmd.Params[i].Key,
+                                                                  Cmd.Params[i].len)) &&
+                                                    (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
+                                                       ok = 0;
+                                               }
+                               }
+                               /* we're supposed to print all headers matching the filterlist */
+                               else for (i=0, ok = 0; ((i < num_parms) && (ok == 0)); ++i) {
+                                               if ( (!strncasecmp(ChrPtr(Line), 
+                                                                  Cmd.Params[i].Key,
+                                                                  Cmd.Params[i].len)) &&
+                                                    (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
+                                                       ok = 1;
                                        }
                                }
                        }
@@ -568,7 +583,7 @@ void imap_strip_headers(StrBuf *section) {
                        StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
                }
 
-               if ((Ptr != StrBufNOTNULL)  ||
+               if ((Ptr == StrBufNOTNULL)  ||
                    (StrLength(Line) == 0)  ||
                    (ChrPtr(Line)[0]=='\r') ||
                    (ChrPtr(Line)[0]=='\n')   ) done_headers = 1;
@@ -577,9 +592,10 @@ void imap_strip_headers(StrBuf *section) {
        StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
 
        /* Now save it back (it'll always be smaller) */
-       FreeStrBuf(&CC->redirect_buffer);
-       CC->redirect_buffer = boiled_headers;
+       FreeStrBuf(&CCC->redirect_buffer);
+       CCC->redirect_buffer = boiled_headers;
 
+       free(Cmd.Params);
        FreeStrBuf(&which_fields);
        FreeStrBuf(&Line);
 }
@@ -591,12 +607,13 @@ void imap_strip_headers(StrBuf *section) {
 void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        struct CtdlMessage *msg = NULL;
        StrBuf *section;
-       char partial[SIZ];
+       StrBuf *partial;
        int is_partial = 0;
        size_t pstart, pbytes;
        int loading_body_now = 0;
        int need_body = 1;
        int burn_the_cache = 0;
+       CitContext *CCC = CC;
 
        /* extract section */
        section = NewStrBufPlain(CKEY(item));
@@ -633,16 +650,19 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        }
 
        /* extract partial */
-       safestrncpy(partial, item.Key, sizeof partial);
-       if (strchr(partial, '<') != NULL) {
-               stripallbut(partial, '<', '>');
+       partial = NewStrBufPlain(CKEY(item));
+       if (strchr(ChrPtr(partial), '<') != NULL) {
+               StrBufStripAllBut(partial, '<', '>');
                is_partial = 1;
        }
-       if (is_partial == 0) strcpy(partial, "");
-       /* if (!IsEmptyStr(partial)) CtdlLogPrintf(CTDL_DEBUG, "Partial is %s\n", partial); */
+       if (is_partial == 0) 
+               if (StrLength(partial) > 0) 
+                       CtdlLogPrintf(CTDL_DEBUG, 
+                                     "Partial is %s\n", 
+                                     ChrPtr(partial));
 
        if (IMAP->cached_body == NULL) {
-               CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
+               CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
                loading_body_now = 1;
                msg = CtdlFetchMessage(msgnum, (need_body ? 1 : 0));
        }
@@ -693,8 +713,8 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        }
 
        if (loading_body_now) {
-               IMAP->cached_body_len = StrLength(CC->redirect_buffer);
-               IMAP->cached_body = SmashStrBuf(&CC->redirect_buffer);
+               IMAP->cached_body_len = StrLength(CCC->redirect_buffer);
+               IMAP->cached_body = SmashStrBuf(&CCC->redirect_buffer);
                IMAP->cached_bodymsgnum = msgnum;
                IMAP->cached_body_withbody = need_body;
                strcpy(IMAP->cached_bodypart, ChrPtr(section));
@@ -706,13 +726,15 @@ void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
                pbytes = IMAP->cached_body_len;
        }
        else {
-               sscanf(partial, SIZE_T_FMT "." SIZE_T_FMT, &pstart, &pbytes);
+               sscanf(ChrPtr(partial), SIZE_T_FMT "." SIZE_T_FMT, &pstart, &pbytes);
                if (pbytes > (IMAP->cached_body_len - pstart)) {
                        pbytes = IMAP->cached_body_len - pstart;
                }
                cprintf("BODY[%s]<" SIZE_T_FMT "> {" SIZE_T_FMT "}\r\n", ChrPtr(section), pstart, pbytes);
        }
 
+       FreeStrBuf(&partial);
+
        /* Here we go -- output it */
        client_write(&IMAP->cached_body[pstart], pbytes);
 
@@ -724,6 +746,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);
 }
 
 /*