]> 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 5b80dd97431251c1eb5c48defb6fa6e30f2806d9..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,43 +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 = malloc(SIZ);
-               CC->redirect_len = 0;
-               CC->redirect_alloc = 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_data = CC->redirect_buffer;
-               IMAP->cached_rfc822_len = CC->redirect_len;
+               IMAP->cached_rfc822 = CCC->redirect_buffer;
+               CCC->redirect_buffer = NULL;
                IMAP->cached_rfc822_msgnum = msgnum;
                IMAP->cached_rfc822_withbody = need_body;
-               CC->redirect_buffer = NULL;
-               CC->redirect_len = 0;
-               CC->redirect_alloc = 0;
-               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);
                }
        }
@@ -215,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;
        }
 
@@ -247,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;
        }
 
@@ -279,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;
@@ -288,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);
@@ -507,107 +508,122 @@ void imap_fetch_envelope(struct CtdlMessage *msg) {
  * RFC822 headers with no body attached.  Its job is to strip that set of
  * headers down to *only* the ones we're interested in.
  */
-void imap_strip_headers(char *section) {
-       char buf[SIZ];
-       char *which_fields = NULL;
+void imap_strip_headers(StrBuf *section) {
+       citimap_command Cmd;
+       StrBuf *which_fields = NULL;
        int doing_headers = 0;
        int headers_not = 0;
-       char *parms[SIZ];
-        int num_parms = 0;
+       int num_parms = 0;
        int i;
-       char *boiled_headers = NULL;
+       StrBuf *boiled_headers = NULL;
+       StrBuf *Line;
        int ok = 0;
        int done_headers = 0;
-       const char *ptr = NULL;
+       const char *Ptr = NULL;
+       CitContext *CCC = CC;
 
-       if (CC->redirect_buffer == NULL) return;
+       if (CCC->redirect_buffer == NULL) return;
 
-       which_fields = strdup(section);
+       which_fields = NewStrBufDup(section);
 
-       if (!strncasecmp(which_fields, "HEADER.FIELDS", 13))
+       if (!strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS", 13))
                doing_headers = 1;
-       if (!strncasecmp(which_fields, "HEADER.FIELDS.NOT", 17))
+       if (doing_headers && 
+           !strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS.NOT", 17))
                headers_not = 1;
 
-       for (i=0; which_fields[i]; ++i) {
-               if (which_fields[i]=='(')
-                       strcpy(which_fields, &which_fields[i+1]);
+       for (i=0; i < StrLength(which_fields); ++i) {
+               if (ChrPtr(which_fields)[i]=='(')
+                       StrBufReplaceToken(which_fields, i, 1, HKEY(""));
        }
-       for (i=0; which_fields[i]; ++i) {
-               if (which_fields[i]==')') {
-                       which_fields[i] = 0;
+       for (i=0; i < StrLength(which_fields); ++i) {
+               if (ChrPtr(which_fields)[i]==')') {
+                       StrBufCutAt(which_fields, i, NULL);
                        break;
                }
        }
-       num_parms = old_imap_parameterize(parms, which_fields);
-
-       boiled_headers = malloc(CC->redirect_alloc);
-       strcpy(boiled_headers, "");
+       memset(&Cmd, 0, sizeof(citimap_command));
+       Cmd.CmdBuf = which_fields;
+       num_parms = imap_parameterize(&Cmd);
 
-       ptr = CC->redirect_buffer;
+       boiled_headers = NewStrBufPlain(NULL, StrLength(CCC->redirect_buffer));
+       Line = NewStrBufPlain(NULL, SIZ);
+       Ptr = NULL;
        ok = 0;
        do {
-               ptr = memreadline(ptr, buf, sizeof buf);
-               if (!isspace(buf[0])) {
+               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, parms[i],
-                                          strlen(parms[i]))) &&
-                                          (buf[strlen(parms[i])]==':') ) {
-                                               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;
                                        }
                                }
                        }
                }
 
                if (ok) {
-                       strcat(boiled_headers, buf);
-                       strcat(boiled_headers, "\r\n");
+                       StrBufAppendBuf(boiled_headers, Line, 0);
+                       StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
                }
 
-               if (IsEmptyStr(buf)) done_headers = 1;
-               if (buf[0]=='\r') done_headers = 1;
-               if (buf[0]=='\n') done_headers = 1;
-               if (*ptr == 0) done_headers = 1;
+               if ((Ptr == StrBufNOTNULL)  ||
+                   (StrLength(Line) == 0)  ||
+                   (ChrPtr(Line)[0]=='\r') ||
+                   (ChrPtr(Line)[0]=='\n')   ) done_headers = 1;
        } while (!done_headers);
 
-       strcat(boiled_headers, "\r\n");
+       StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
 
        /* Now save it back (it'll always be smaller) */
-       strcpy(CC->redirect_buffer, boiled_headers);
-       CC->redirect_len = strlen(boiled_headers);
+       FreeStrBuf(&CCC->redirect_buffer);
+       CCC->redirect_buffer = boiled_headers;
 
-       free(which_fields);
-       free(boiled_headers);
+       free(Cmd.Params);
+       FreeStrBuf(&which_fields);
+       FreeStrBuf(&Line);
 }
 
 
 /*
  * Implements the BODY and BODY.PEEK fetch items
  */
-void imap_fetch_body(long msgnum, const char *item, int is_peek) {
+void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
        struct CtdlMessage *msg = NULL;
-       char section[SIZ];
-       char partial[SIZ];
+       StrBuf *section;
+       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 */
-       safestrncpy(section, item, sizeof section);
-       if (strchr(section, '[') != NULL) {
-               stripallbut(section, '[', ']');
+       section = NewStrBufPlain(CKEY(item));
+       
+       if (strchr(ChrPtr(section), '[') != NULL) {
+               StrBufStripAllBut(section, '[', ']');
        }
        CtdlLogPrintf(CTDL_DEBUG, "Section is: %s%s\n", 
-               section
-               IsEmptyStr(section) ? "(empty)" : ""
+                     ChrPtr(section)
+                     (StrLength(section) == 0) ? "(empty)" : ""
        );
 
        /* Burn the cache if we don't have the same section of the 
@@ -620,7 +636,7 @@ void imap_fetch_body(long msgnum, const char *item, int is_peek) {
                else if ( (!IMAP->cached_body_withbody) && (need_body) ) {
                        burn_the_cache = 1;
                }
-               else if (strcasecmp(IMAP->cached_bodypart, section)) {
+               else if (strcasecmp(IMAP->cached_bodypart, ChrPtr(section))) {
                        burn_the_cache = 1;
                }
                if (burn_the_cache) {
@@ -634,18 +650,19 @@ void imap_fetch_body(long msgnum, const char *item, int is_peek) {
        }
 
        /* extract partial */
-       safestrncpy(partial, item, 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 = malloc(SIZ);
-               CC->redirect_len = 0;
-               CC->redirect_alloc = SIZ;
+               CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
                loading_body_now = 1;
                msg = CtdlFetchMessage(msgnum, (need_body ? 1 : 0));
        }
@@ -656,11 +673,11 @@ void imap_fetch_body(long msgnum, const char *item, int is_peek) {
                /* What we want is already in memory */
        }
 
-       else if ( (!strcmp(section, "1")) && (msg->cm_format_type != 4) ) {
+       else if ( (!strcmp(ChrPtr(section), "1")) && (msg->cm_format_type != 4) ) {
                CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_NONE, 0, 1, SUPPRESS_ENV_TO);
        }
 
-       else if (!strcmp(section, "")) {
+       else if (StrLength(section) == 0) {
                CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, SUPPRESS_ENV_TO);
        }
 
@@ -668,7 +685,7 @@ void imap_fetch_body(long msgnum, const char *item, int is_peek) {
         * If the client asked for just headers, or just particular header
         * fields, strip it down.
         */
-       else if (!strncasecmp(section, "HEADER", 6)) {
+       else if (!strncasecmp(ChrPtr(section), "HEADER", 6)) {
                /* This used to work with HEADERS_FAST, but then Apple got stupid with their
                 * IMAP library and this broke Mail.App and iPhone Mail, so we had to change it
                 * to HEADERS_ONLY so the trendy hipsters with their iPhones can read mail.
@@ -680,7 +697,7 @@ void imap_fetch_body(long msgnum, const char *item, int is_peek) {
        /*
         * Strip it down if the client asked for everything _except_ headers.
         */
-       else if (!strncasecmp(section, "TEXT", 4)) {
+       else if (!strncasecmp(ChrPtr(section), "TEXT", 4)) {
                CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_NONE, 0, 1, SUPPRESS_ENV_TO);
        }
 
@@ -696,29 +713,28 @@ void imap_fetch_body(long msgnum, const char *item, int is_peek) {
        }
 
        if (loading_body_now) {
-               IMAP->cached_body = CC->redirect_buffer;
-               IMAP->cached_body_len = CC->redirect_len;
+               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, section);
-               CC->redirect_buffer = NULL;
-               CC->redirect_len = 0;
-               CC->redirect_alloc = 0;
+               strcpy(IMAP->cached_bodypart, ChrPtr(section));
        }
 
        if (is_partial == 0) {
-               cprintf("BODY[%s] {" SIZE_T_FMT "}\r\n", section, IMAP->cached_body_len);
+               cprintf("BODY[%s] {" SIZE_T_FMT "}\r\n", ChrPtr(section), IMAP->cached_body_len);
                pstart = 0;
                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", section, pstart, pbytes);
+               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);
 
@@ -730,6 +746,7 @@ void imap_fetch_body(long msgnum, const char *item, int is_peek) {
        if (is_peek == 0) {
                CtdlSetSeen(&msgnum, 1, 1, ctdlsetseen_seen, NULL, NULL);
        }
+       FreeStrBuf(&section);
 }
 
 /*
@@ -923,15 +940,10 @@ void imap_fetch_bodystructure (long msgnum, const char *item,
                 * to measure it.  FIXME use smi cached fields if possible
                 */
 
-               CC->redirect_buffer = malloc(SIZ);
-               CC->redirect_len = 0;
-               CC->redirect_alloc = SIZ;
+               CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
                CtdlOutputPreLoadedMsg(msg, MT_RFC822, 0, 0, 1, SUPPRESS_ENV_TO);
-               rfc822 = pch = CC->redirect_buffer;
-               rfc822_len = CC->redirect_len;
-               CC->redirect_buffer = NULL;
-               CC->redirect_len = 0;
-               CC->redirect_alloc = 0;
+               rfc822_len = StrLength(CC->redirect_buffer);
+               rfc822 = pch = SmashStrBuf(&CC->redirect_buffer);
 
                ptr = rfc822;
                do {
@@ -1010,10 +1022,10 @@ void imap_do_fetch_msg(int seq, citimap_command *Cmd) {
 
                /* BODY fetches do their own fetching and caching too. */
                else if (!strncasecmp(Cmd->Params[i].Key, "BODY[", 5)) {
-                       imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i].Key, 0);
+                       imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 0);
                }
                else if (!strncasecmp(Cmd->Params[i].Key, "BODY.PEEK[", 10)) {
-                       imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i].Key, 1);
+                       imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 1);
                }
 
                /* Otherwise, load the message into memory.