diff --git a/lib/Connection.js b/lib/Connection.js index f2d2546..fa5d19d 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -6,7 +6,8 @@ var tls = require('tls'), isDate = require('util').isDate, utf7 = require('utf7').imap; -var Parser = require('./Parser').Parser; +var Parser = require('./Parser').Parser, + parseHeader = require('./Parser').parseHeader; var MAX_INT = 9007199254740992, KEEPALIVE_INTERVAL = 10000, @@ -1135,6 +1136,8 @@ Connection.prototype._enqueue = function(fullcmd, promote, cb) { } }; +Connection.parseHeader = parseHeader; // from Parser.js + module.exports = Connection; // utilities ------------------------------------------------------------------- diff --git a/lib/Parser.js b/lib/Parser.js index e5f521d..216026d 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -15,7 +15,9 @@ var CH_LF = 10, RE_LITERAL = /\{(\d+)\}$/, RE_UNTAGGED = /^\* (?:(OK|NO|BAD|BYE|FLAGS|LIST|LSUB|SEARCH|STATUS|CAPABILITY|NAMESPACE|PREAUTH|SORT)|(\d+) (EXPUNGE|FETCH|RECENT|EXISTS))(?: (?:\[([^\]]+)\] )?(.+))?$/i, RE_TAGGED = /^A(\d+) (OK|NO|BAD) (?:\[([^\]]+)\] )?(.+)$/i, - RE_CONTINUE = /^\+ (?:\[([^\]]+)\] )?(.+)$/i; + RE_CONTINUE = /^\+ (?:\[([^\]]+)\] )?(.+)$/i, + RE_CRLF = /\r\n/g, + RE_HDR = /^([^:]+):[ \t]?(.+)?$/; function Parser(stream, debug) { if (!(this instanceof Parser)) @@ -576,7 +578,42 @@ function convStr(str, literals) { return str; } +function parseHeader(str) { + var lines = str.split(RE_CRLF), + len = lines.length, + modded = false, + header = {}, + m, h; + + for (var i = 0; i < len; ++i) { + if (lines[i].length === 0) + continue; + if (lines[i][0] === '\t' || lines[i][0] === ' ') { + // folded header content + // RFC2822 says to just remove the CRLF and not the whitespace following + // it, so we follow the RFC and include the leading whitespace ... + header[h][header[h].length - 1] += lines[i]; + } else { + m = RE_HDR.exec(lines[i]); + if (m) { + h = m[1].toLowerCase(); + if (m[2]) { + if (header[h] === undefined) + header[h] = [m[2]]; + else + header[h].push(m[2]); + } else + header[h] = ['']; + } else + break; + } + } + + return header; +} + exports.Parser = Parser; exports.parseExpr = parseExpr; exports.parseEnvelopeAddresses = parseEnvelopeAddresses; exports.parseBodyStructure = parseBodyStructure; +exports.parseHeader = parseHeader;