From 70251b00c3a198b6becabaf48bcd9136cd567385 Mon Sep 17 00:00:00 2001 From: Brian White Date: Thu, 3 Jan 2013 23:09:13 -0500 Subject: [PATCH] more fetch() fixes --- lib/imap.js | 159 ++++++++++++++++++++++++++++---------------- lib/imap.parsers.js | 22 +++++- 2 files changed, 120 insertions(+), 61 deletions(-) diff --git a/lib/imap.js b/lib/imap.js index 23e61aa..8fdde45 100644 --- a/lib/imap.js +++ b/lib/imap.js @@ -208,16 +208,43 @@ ImapConnection.prototype.connect = function(loginCb) { } } + function emitLitData(key, data) { + var fetches = requests[0].fetchers[key]; + for (var i=0, len=fetches.length; i= b.length) return; self.debug&&self.debug('\n<== ' + inspect(b.toString('binary', b.p)) + '\n'); - var r, m, litType, i, len, msg, fetches; + var r, m, litType, i, len, msg, fetches, f, lenf; if (indata.expect > 0) { r = read(b); if (indata.streaming) { - requests[0].msg.emit('data', r); + //requests[0].fetchers[requests[0].key].msg.emit('data', r); + emitLitData(requests[0].key, r); if (indata.expect === 0) indata.streaming = false; } else { @@ -248,16 +275,22 @@ ImapConnection.prototype.connect = function(loginCb) { if (indata.expect > -1) { if ((m = /\* (\d+) FETCH/i.exec(indata.line)) && /^BODY\[/i.test(litType)) { - if (!requests[0].msg) { + /*if (!requests[0].fetchers[litType].msg) { msg = new ImapMessage(); msg.seqno = parseInt(m[1], 10); - requests[0].msg = msg; var fetches = requests[0].fetchers[litType]; - for (var f = 0, lenf = fetches.length; f < lenf; ++f) + fetches.msg = msg; + for (f = 0, lenf = fetches.length; f < lenf; ++f) fetches[f].emit('message', msg); - } + }*/ + + msg = new ImapMessage(); + msg.seqno = parseInt(m[1], 10); + fetches = requests[0].fetchers[litType]; + emitLitMsg(litType, msg); + requests[0].key = litType; - indata.streaming = (!RE_ISHEADER.test(litType)); + indata.streaming = !RE_ISHEADER.test(litType); if (indata.streaming) indata.literals.push(indata.expect); } else if (indata.expect === 0) @@ -278,61 +311,71 @@ ImapConnection.prototype.connect = function(loginCb) { switch (m.type) { case 'FETCH': // m.info = message details - var data, parsed, headers, f, lenf; - msg = (requests[0] && requests[0].msg - ? requests[0].msg - : new ImapMessage()); - parsers.parseFetch(m.info, indata.literals, msg); - - if (typeof msg.body === 'number') { - // we streamed a body, e.g. {3}\r\nfoo - delete msg.body; - msg.emit('end'); - } else { - // no body was streamed - msg.seqno = parseInt(m.num, 10); - if (isUnsolicited) - self.emit('msgupdate', msg); - else { - if (typeof msg.body === 'string') { - // a body was given as a non-literal string, e.g. "foo" - fetches = requests[0].fetchers[requests[0].key]; - if (RE_ISHEADER.test(requests[0].key)) { - var parsed, data, headers; - for (f = 0, lenf = fetches.length; f < lenf; ++f) { - if (fetches[f]._parse) { - if (parsed === undefined) - parsed = parsers.parseHeaders(msg.body); - headers = parsed; - } else { - if (data === undefined) - data = new Buffer(msg.body, 'binary'); - headers = data; - } - delete msg.body; - msg.emit('headers', headers); - msg.emit('end'); - } - } else { - var data = new Buffer(msg.body, 'binary'); - delete msg.body; - for (f = 0, lenf = fetches.length; f < lenf; ++f) { - msg.emit('data', data); - msg.emit('end'); - } - } + var data, parsed, headers, f, lenf, body, lenb, msg, bodies, + details, val; + bodies = parsers.parseFetchBodies(m.info, indata.literals); + details = new ImapMessage(); + parsers.parseFetch(m.info, indata.literals, details); + details.seqno = parseInt(m.num, 10); + + if (requests[0].fetchers[''] !== undefined) { + // account for non-body fetches + if (bodies) { + bodies.push(''); + bodies.push(null); + } else + bodies = ['', null]; + } + + for (body = 0, lenb = bodies.length; body < lenb; body += 2) { + fetches = requests[0].fetchers[bodies[body]]; + val = bodies[body + 1]; + + for (var i=0, len=fetches.length; i