Parser: check for non-literal bodies in FETCH

fork
Brian White 11 years ago
parent 8cb0babba6
commit d4e7eb6257

@ -11,6 +11,7 @@ var CH_LF = 10,
RE_INTEGER = /^\d+$/,
RE_PRECEDING = /^(?:(?:\*|A\d+) )|\+ ?/,
RE_BODYLITERAL = /BODY\[(.*)\] \{(\d+)\}$/i,
RE_BODYINLINEKEY = /^BODY\[(.*)\]$/i,
RE_SEQNO = /^\* (\d+)/,
RE_LISTCONTENT = /^\((.*)\)$/,
RE_LITERAL = /\{(\d+)\}$/,
@ -254,7 +255,7 @@ Parser.prototype._resUntagged = function() {
else if (type === 'status')
val = parseStatus(m[5], this._literals);
else if (type === 'fetch')
val = parseFetch(m[5], this._literals);
val = parseFetch.call(this, m[5], this._literals, num);
else if (type === 'namespace')
val = parseNamespaces(m[5], this._literals);
else if (type === 'esearch')
@ -418,8 +419,8 @@ function parseStatus(text, literals) {
};
}
function parseFetch(text, literals) {
var list = parseExpr(text, literals)[0], attrs = {};
function parseFetch(text, literals, seqno) {
var list = parseExpr(text, literals)[0], attrs = {}, m, body;
// list is [KEY1, VAL1, KEY2, VAL2, .... KEYn, VALn]
for (var i = 0, len = list.length, key, val; i < len; i += 2) {
key = list[i].toLowerCase();
@ -432,6 +433,19 @@ function parseFetch(text, literals) {
val = ''+val[0];
else if (key === 'body' || key === 'bodystructure')
val = parseBodyStructure(val);
else if (m = RE_BODYINLINEKEY.exec(list[i])) {
// a body was sent as a non-literal
val = new Buffer(''+val);
body = new ReadableStream();
body._read = EMPTY_READCB;
this.emit('body', body, {
seqno: seqno,
which: m[1],
size: val.length
});
body.push(val);
body.push(null);
}
attrs[key] = val;
}
return attrs;

@ -0,0 +1,104 @@
var assert = require('assert'),
net = require('net'),
Imap = require('../lib/Connection');
var result, body = '', bodyInfo;
var CRLF = '\r\n';
var RESPONSES = [
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN',
'A0 OK Thats all she wrote!',
''
].join(CRLF),
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN UIDPLUS MOVE',
'A1 OK authenticated (Success)',
''
].join(CRLF),
['* NAMESPACE (("" "/")) NIL NIL',
'A2 OK Success',
''
].join(CRLF),
['* LIST (\\Noselect) "/" "/"',
'A3 OK Success',
''
].join(CRLF),
['* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)',
'* OK [PERMANENTFLAGS ()] Flags permitted.',
'* OK [UIDVALIDITY 2] UIDs valid.',
'* 685 EXISTS',
'* 0 RECENT',
'* OK [UIDNEXT 4422] Predicted next UID.',
'A4 OK [READ-ONLY] INBOX selected. (Success)',
''
].join(CRLF),
['* 1 FETCH (UID 1)',
'* 1 FETCH (INTERNALDATE "05-Sep-2004 00:38:03 +0000" UID 1000)',
'* 1 FETCH (BODY[TEXT] "IMAP is terrible")',
'* 1 FETCH (FLAGS (\\Seen))',
'A5 OK Success',
''
].join(CRLF),
['* BYE LOGOUT Requested',
'A6 OK good day (Success)',
''
].join(CRLF)
];
var srv = net.createServer(function(sock) {
sock.write('* OK asdf\r\n');
var buf = '', lines;
sock.on('data', function(data) {
buf += data.toString('utf8');
if (buf.indexOf(CRLF) > -1) {
lines = buf.split(CRLF);
buf = lines.pop();
lines.forEach(function() {
sock.write(RESPONSES.shift());
});
}
});
});
srv.listen(0, '127.0.0.1', function() {
var port = srv.address().port;
var imap = new Imap({
user: 'foo',
password: 'bar',
host: '127.0.0.1',
port: port,
keepalive: false
});
imap.on('ready', function() {
imap.openBox('INBOX', true, function() {
var f = imap.seq.fetch(1, { bodies: ['TEXT'] });
f.on('message', function(m) {
m.on('body', function(stream, info) {
bodyInfo = info;
stream.on('data', function(chunk) { body += chunk.toString('utf8'); });
});
m.on('attributes', function(attrs) {
result = attrs;
});
});
f.on('end', function() {
srv.close();
imap.end();
});
});
});
imap.connect();
});
process.once('exit', function() {
assert.deepEqual(result, {
uid: 1,
date: new Date('05-Sep-2004 00:38:03 +0000'),
flags: [ '\\Seen' ]
});
assert.equal(body, 'IMAP is terrible');
assert.deepEqual(bodyInfo, {
seqno: 1,
which: 'TEXT',
size: 16
});
});
Loading…
Cancel
Save