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).
This commit is contained in:
Oswald Buddenhagen 2010-02-06 10:34:41 +01:00
parent 09dfddb36b
commit d94dadbaeb
3 changed files with 50 additions and 38 deletions

View File

@ -1250,7 +1250,7 @@ maildir_parse_store( conffile_t *cfg, store_conf_t **storep, int *err )
} }
struct driver maildir_driver = { struct driver maildir_driver = {
0, 0, /* XXX DRV_CRLF? */
maildir_parse_store, maildir_parse_store,
maildir_cleanup_drv, maildir_cleanup_drv,
maildir_open_store, maildir_open_store,

View File

@ -174,6 +174,11 @@ typedef struct {
/* All memory belongs to the driver's user. */ /* 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 DRV_CRLF 1
#define TUIDL 12 #define TUIDL 12

View File

@ -211,7 +211,7 @@ msg_fetched( int sts, void *aux )
copy_vars_t *vars = (copy_vars_t *)aux; copy_vars_t *vars = (copy_vars_t *)aux;
SVARS(vars->aux) SVARS(vars->aux)
char *fmap, *buf; 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; int start, sbreak = 0, ebreak = 0;
char c; char c;
@ -224,29 +224,24 @@ msg_fetched( int sts, void *aux )
if (vars->srec || scr != tcr) { if (vars->srec || scr != tcr) {
fmap = vars->data.data; fmap = vars->data.data;
len = vars->data.len; len = vars->data.len;
cra = crd = 0; extra = lines = crs = i = 0;
if (scr > tcr)
crd = -1;
else if (scr < tcr)
cra = 1;
extra = 0, i = 0;
if (vars->srec) { if (vars->srec) {
extra += 8 + TUIDL + 1 + tcr;
nloop: nloop:
start = i; start = i;
crds = 0; lcrs = 0;
while (i < len) { while (i < len) {
c = fmap[i++]; c = fmap[i++];
if (c == '\r') if (c == '\r')
crds += crd; lcrs++;
else if (c == '\n') { else if (c == '\n') {
if (!memcmp( fmap + start, "X-TUID: ", 8 )) { if (!memcmp( fmap + start, "X-TUID: ", 8 )) {
extra -= (ebreak = i) - (sbreak = start); extra = (sbreak = start) - (ebreak = i);
goto oke; goto oke;
} }
extra += cra + crds; lines++;
if (i - 1 - scr == start) { crs += lcrs;
sbreak = ebreak = i - 1 - scr; if (i - lcrs - 1 == start) {
sbreak = ebreak = start;
goto oke; goto oke;
} }
goto nloop; goto nloop;
@ -257,54 +252,66 @@ msg_fetched( int sts, void *aux )
vars->msg->uid, str_ms[1-t] ); vars->msg->uid, str_ms[1-t] );
free( fmap ); free( fmap );
return vars->cb( SYNC_NOGOOD, 0, vars ); return vars->cb( SYNC_NOGOOD, 0, vars );
oke:
extra += 8 + TUIDL + 1 + (tcr && crs);
} }
oke: if (tcr != scr) {
if (cra || crd)
for (; i < len; i++) { for (; i < len; i++) {
c = fmap[i]; c = fmap[i];
if (c == '\r') if (c == '\r')
extra += crd; crs++;
else if (c == '\n') else if (c == '\n')
extra += cra; lines++;
} }
extra -= crs;
if (tcr)
extra += lines;
}
vars->data.len = len + extra; vars->data.len = len + extra;
buf = vars->data.data = nfmalloc( vars->data.len ); buf = vars->data.data = nfmalloc( vars->data.len );
i = 0; i = 0;
if (vars->srec) { if (vars->srec) {
if (cra) { if (tcr != scr) {
for (; i < sbreak; i++) { if (tcr) {
if (fmap[i] == '\n') for (; i < sbreak; i++)
*buf++ = '\r'; if ((c = fmap[i]) != '\r') {
*buf++ = fmap[i]; 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 { } else {
memcpy( buf, fmap, sbreak ); memcpy( buf, fmap, sbreak );
buf += sbreak; buf += sbreak;
} }
memcpy( buf, "X-TUID: ", 8 ); memcpy( buf, "X-TUID: ", 8 );
buf += 8; buf += 8;
memcpy( buf, vars->srec->tuid, TUIDL ); memcpy( buf, vars->srec->tuid, TUIDL );
buf += TUIDL; buf += TUIDL;
if (tcr) if (tcr && crs)
*buf++ = '\r'; *buf++ = '\r';
*buf++ = '\n'; *buf++ = '\n';
i = ebreak; i = ebreak;
} }
if (cra) { if (tcr != scr) {
for (; i < len; i++) { if (tcr) {
if (fmap[i] == '\n') for (; i < len; i++)
*buf++ = '\r'; if ((c = fmap[i]) != '\r') {
*buf++ = fmap[i]; 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 } else
memcpy( buf, fmap + i, len - i ); memcpy( buf, fmap + i, len - i );