upas/fs: move unixheader handling to parseheaders()

parsing the unixheader in mdir fetch routine is the wrong place,
as no invalid character handling has been performed yet. also
the string is not neccesarily null terminated.

avoid duplication with plan9 mbox parsing and just do it in
parseheaders(), which already handles faking the unix headers
for pop3 and imap.
This commit is contained in:
cinap_lenrek 2020-06-29 00:23:37 +02:00
parent 30ab804b58
commit 85132f197b
3 changed files with 32 additions and 64 deletions

View file

@ -476,6 +476,20 @@ parseattachments(Message *m, Mailbox *mb)
}
}
static void
parseunix(Message *m)
{
char *s, *p;
m->unixheader = smprint("%.*s", utfnlen(m->start, m->header - m->start), m->start);
s = m->start + 5;
if((p = strchr(s, ' ')) == nil)
return;
*p = 0;
m->unixfrom = strdup(s);
*p = ' ';
}
void
parseheaders(Mailbox *mb, Message *m, int addfrom, int justmime)
{
@ -483,8 +497,20 @@ parseheaders(Mailbox *mb, Message *m, int addfrom, int justmime)
int i, i0, n;
uintptr a;
if(m->header == nil)
m->header = m->start;
/* parse unix header */
if(!justmime && !addfrom && m->unixheader == nil){
if(strncmp(m->start, "From ", 5) == 0)
if((e = memchr(m->start, '\n', m->end - m->start)) != nil){
m->header = e + 1;
parseunix(m);
}
}
/* parse mime headers */
p = m->header;
p = m->mheader = m->mhend = m->header;
i0 = 0;
if(justmime)
i0 = Mhead;
@ -520,11 +546,6 @@ parseheaders(Mailbox *mb, Message *m, int addfrom, int justmime)
m->hend = p;
m->mhend = m->header;
}
/*
* not all attachments have mime headers themselves.
*/
if(!m->mheader)
m->mhend = 0;
if(*p == '\n')
p++;
m->rbody = m->body = p;
@ -538,9 +559,10 @@ parseheaders(Mailbox *mb, Message *m, int addfrom, int justmime)
* adding the unix header all the time screws up mime-attached
* rfc822 messages.
*/
if(!addfrom && !m->unixfrom)
if(!addfrom && m->unixfrom == nil) {
free(m->unixheader);
m->unixheader = nil;
else if(m->unixheader == nil){
} else if(m->unixheader == nil){
if(m->unixfrom && strcmp(m->unixfrom, "???") != 0)
p = m->unixfrom;
else if(m->from)

View file

@ -22,24 +22,10 @@ slurp(char *f, char *b, uvlong o, long l)
return r != l ? -1: 0;
}
static void
parseunix(Message *m)
{
char *s, *p;
m->unixheader = smprint("%.*s", utfnlen(m->start, m->header - m->start), m->start);
s = m->start + 5;
if((p = strchr(s, ' ')) == nil)
return;
*p = 0;
m->unixfrom = strdup(s);
*p = ' ';
}
static int
mdirfetch(Mailbox *mb, Message *m, uvlong o, ulong l)
{
char buf[Pathlen], *x;
char buf[Pathlen];
Mdir *mdir;
mdir = mb->aux;
@ -51,17 +37,6 @@ mdirfetch(Mailbox *mb, Message *m, uvlong o, ulong l)
mdprint(mdir, "%r\n");
return -1;
}
if(m->header == nil)
m->header = m->start;
if(m->header == m->start)
if(o + l >= 36)
if(strncmp(m->start, "From ", 5) == 0)
if(x = strchr(m->start, '\n')){
m->header = x + 1;
if(m->unixfrom == nil)
parseunix(m);
}
m->mheader = m->mhend = m->header;
mdprint(mdir, "fetched [%llud, %llud]\n", o, o + l);
return 0;
}

View file

@ -50,29 +50,6 @@ chkunix(char *s, int n)
return r;
}
static char*
parseunix(Message *m)
{
char *s, *p, *q;
Tm tm;
m->unixheader = smprint("%.*s", utfnlen(m->start, m->header - m->start), m->start);
s = m->start + 5;
if((p = strchr(s, ' ')) == nil)
return s;
*p = 0;
m->unixfrom = strdup(s);
*p++ = ' ';
if(q = strchr(p, '\n'))
*q = 0;
if(strtotm(p, &tm) < 0)
return p;
if(q)
*q = '\n';
m->fileid = (uvlong)tm2sec(&tm) << 8;
return 0;
}
static void
addtomessage(Message *m, char *p, int n)
{
@ -215,7 +192,7 @@ mergemsg(Message *m, Message *x)
static char*
readmbox(Mailbox *mb, Mlock *lk)
{
char *p, *x, buf[Pathlen];
char buf[Pathlen];
Biobuf *in;
Dir *d;
Inbuf b;
@ -306,12 +283,6 @@ retry:
}
if(m == nil)
continue;
m->header = m->end;
if(x = strchr(m->start, '\n'))
m->header = x + 1;
if(p = parseunix(m))
sysfatal("%s:%lld naked From in body? [%s]", mb->path, seek(Bfildes(in), 0, 1), p);
m->mheader = m->mhend = m->header;
parse(mb, m, 0, 0);
if(m != *l && m->deleted != Dup){
logmsg(m, "new");