commit bb3177827fc2d1d5378aa925f4cc957cc6725feb Author: Oswald Buddenhagen <o...@kde.org> Date: Sat Feb 6 10:34:41 2010 +0100
fix line ending conversion logic imap may very well store messages with LF line endings. only RFC2822 requires CRLF. consequently, preserve the line endings as much as possible unless the mailbox format does not support it (this would be the case for unix mbox - i actually have no idea about maildir). src/drv_maildir.c | 2 +- src/isync.h | 5 +++ src/sync.c | 81 ++++++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/drv_maildir.c b/src/drv_maildir.c index d8779f5..80a7cac 100644 --- a/src/drv_maildir.c +++ b/src/drv_maildir.c @@ -1250,7 +1250,7 @@ maildir_parse_store( conffile_t *cfg, store_conf_t **storep, int *err ) } struct driver maildir_driver = { - 0, + 0, /* XXX DRV_CRLF? */ maildir_parse_store, maildir_cleanup_drv, maildir_open_store, diff --git a/src/isync.h b/src/isync.h index cd3e750..8e7ec51 100644 --- a/src/isync.h +++ b/src/isync.h @@ -174,6 +174,11 @@ typedef struct { /* All memory belongs to the driver's user. */ +/* + This flag says that the driver CAN store messages with CRLFs, + not that it must. The lack of it OTOH implies that it CANNOT, + and as CRLF is the canonical format, we convert. +*/ #define DRV_CRLF 1 #define TUIDL 12 diff --git a/src/sync.c b/src/sync.c index b1c4a46..8c2f6b5 100644 --- a/src/sync.c +++ b/src/sync.c @@ -211,7 +211,7 @@ msg_fetched( int sts, void *aux ) copy_vars_t *vars = (copy_vars_t *)aux; SVARS(vars->aux) char *fmap, *buf; - int i, len, extra, cra, crd, scr, tcr, crds; + int i, len, extra, scr, tcr, lcrs, crs, lines; int start, sbreak = 0, ebreak = 0; char c; @@ -224,29 +224,24 @@ msg_fetched( int sts, void *aux ) if (vars->srec || scr != tcr) { fmap = vars->data.data; len = vars->data.len; - cra = crd = 0; - if (scr > tcr) - crd = -1; - else if (scr < tcr) - cra = 1; - extra = 0, i = 0; + extra = lines = crs = i = 0; if (vars->srec) { - extra += 8 + TUIDL + 1 + tcr; nloop: start = i; - crds = 0; + lcrs = 0; while (i < len) { c = fmap[i++]; if (c == '\r') - crds += crd; + lcrs++; else if (c == '\n') { if (!memcmp( fmap + start, "X-TUID: ", 8 )) { - extra -= (ebreak = i) - (sbreak = start); + extra = (sbreak = start) - (ebreak = i); goto oke; } - extra += cra + crds; - if (i - 1 - scr == start) { - sbreak = ebreak = i - 1 - scr; + lines++; + crs += lcrs; + if (i - lcrs - 1 == start) { + sbreak = ebreak = start; goto oke; } goto nloop; @@ -257,54 +252,66 @@ msg_fetched( int sts, void *aux ) vars->msg->uid, str_ms[1-t] ); free( fmap ); return vars->cb( SYNC_NOGOOD, 0, vars ); + oke: + extra += 8 + TUIDL + 1 + (tcr && crs); } - oke: - if (cra || crd) + if (tcr != scr) { for (; i < len; i++) { c = fmap[i]; if (c == '\r') - extra += crd; + crs++; else if (c == '\n') - extra += cra; + lines++; } + extra -= crs; + if (tcr) + extra += lines; + } vars->data.len = len + extra; buf = vars->data.data = nfmalloc( vars->data.len ); i = 0; if (vars->srec) { - if (cra) { - for (; i < sbreak; i++) { - if (fmap[i] == '\n') - *buf++ = '\r'; - *buf++ = fmap[i]; + if (tcr != scr) { + if (tcr) { + for (; i < sbreak; i++) + if ((c = fmap[i]) != '\r') { + if (c == '\n') + *buf++ = '\r'; + *buf++ = c; + } + } else { + for (; i < sbreak; i++) + if ((c = fmap[i]) != '\r') + *buf++ = c; } - } else if (crd) { - for (; i < sbreak; i++) - if (fmap[i] != '\r') - *buf++ = fmap[i]; } else { memcpy( buf, fmap, sbreak ); buf += sbreak; } + memcpy( buf, "X-TUID: ", 8 ); buf += 8; memcpy( buf, vars->srec->tuid, TUIDL ); buf += TUIDL; - if (tcr) + if (tcr && crs) *buf++ = '\r'; *buf++ = '\n'; i = ebreak; } - if (cra) { - for (; i < len; i++) { - if (fmap[i] == '\n') - *buf++ = '\r'; - *buf++ = fmap[i]; + if (tcr != scr) { + if (tcr) { + for (; i < len; i++) + if ((c = fmap[i]) != '\r') { + if (c == '\n') + *buf++ = '\r'; + *buf++ = c; + } + } else { + for (; i < len; i++) + if ((c = fmap[i]) != '\r') + *buf++ = c; } - } else if (crd) { - for (; i < len; i++) - if (fmap[i] != '\r') - *buf++ = fmap[i]; } else memcpy( buf, fmap + i, len - i ); ------------------------------------------------------------------------------ The Planet: dedicated and managed hosting, cloud storage, colocation Stay online with enterprise data centers and the best network in the business Choose flexible plans and management services without long-term contracts Personal 24x7 support from experience hosting pros just a phone call away. http://p.sf.net/sfu/theplanet-com _______________________________________________ isync-devel mailing list isync-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/isync-devel