From cbeb2b95c6d7b6a1cf2439280941966e01b58787 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sun, 6 Mar 2011 09:34:16 -0500 Subject: [PATCH] Fix retrieval of utf8-encoded message bodies and fetching of only message structures. --- imap.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/imap.js b/imap.js index b02b7ef..1b45a26 100644 --- a/imap.js +++ b/imap.js @@ -130,24 +130,24 @@ ImapConnection.prototype.connect = function(loginCb) { if (self._state.curExpected > 0) { var extra = '', curReq = self._state.requests[0]; if (!curReq._done) { - self._state.curXferred += data.length; + self._state.curXferred += Buffer.byteLength(data, 'utf8'); if (self._state.curXferred <= self._state.curExpected) { if (curReq._msgtype === 'headers') - // buffer headers since they're generally not large and need to be + // buffer headers since they're generally not large and are // processed anyway self._state.curData += data; else curReq._msg.emit('data', data); return; } - var pos = data.length-(self._state.curXferred-self._state.curExpected); - extra = data.substr(pos); + var pos = Buffer.byteLength(data, 'utf8')-(self._state.curXferred-self._state.curExpected); + extra = (new Buffer(data)).slice(pos).toString('utf8'); if (pos > 0) { if (curReq._msgtype === 'headers') { - self._state.curData += data.substr(0, pos); + self._state.curData += (new Buffer(data)).slice(0, pos).toString('utf8'); curReq._msgheaders = self._state.curData; } else - curReq._msg.emit('data', data.substr(0, pos)); + curReq._msg.emit('data', (new Buffer(data)).slice(0, pos).toString('utf8')); } self._state.curData = ''; data = extra; @@ -320,6 +320,15 @@ ImapConnection.prototype.connect = function(loginCb) { if (self._state.box.messages.total > 0) self._state.box.messages.total--; break; + default: + if (/^FETCH/.test(data[2])) { // fetches without header or body (part) retrievals + var curReq = self._state.requests[0], + msg = new ImapMessage(); + parseFetch(data[2].substring(data[2].indexOf("(")+1, data[2].lastIndexOf(")")), "", msg); + curReq._fetcher.emit('message', msg); + msg.emit('end'); + } + break; } } } @@ -1422,4 +1431,4 @@ function pipe(pair, socket) { socket.on('close', onclose); return cleartext; -} +} \ No newline at end of file