always pass any response text code and message for tagged responses to the callback as the last argument

fork
mscdex 12 years ago
parent 6b0b8ff8b4
commit 69d23e446f

@ -24,6 +24,8 @@ var CRLF = '\r\n',
}, },
RE_LITHEADER = /(?:((?:BODY\[.*\](?:<\d+>)?)?|[^ ]+) )?\{(\d+)\}$/i, RE_LITHEADER = /(?:((?:BODY\[.*\](?:<\d+>)?)?|[^ ]+) )?\{(\d+)\}$/i,
RE_UNRESP = /^\* (OK|PREAUTH|NO|BAD)(?:\r\n| (?:\[(.+)\] )?(.+))$/i, RE_UNRESP = /^\* (OK|PREAUTH|NO|BAD)(?:\r\n| (?:\[(.+)\] )?(.+))$/i,
RE_TAGGED_RESP = /^A\d+ (OK|NO|BAD) (?:\[(.+?)\] )?(.+)$/i,
RE_TEXT_CODE = /([^ ]+)(?: (.*))?$/,
//RE_ISPARTIAL = /<(\d+)>$/, //RE_ISPARTIAL = /<(\d+)>$/,
RE_DBLQ = /"/g, RE_DBLQ = /"/g,
RE_CMD = /^([^ ]+)(?: |$)/, RE_CMD = /^([^ ]+)(?: |$)/,
@ -539,23 +541,29 @@ ImapConnection.prototype.connect = function(loginCb) {
case 'OK': case 'OK':
if (m[2] === undefined && m[3] === undefined) if (m[2] === undefined && m[3] === undefined)
break; break;
var code = m[2]; var code, codeval;
if (m[2]) {
code = RE_TEXT_CODE.exec(m[2]);
codeval = code[2];
code = code[1].toUpperCase();
}
if (state.status === STATES.NOAUTH) { if (state.status === STATES.NOAUTH) {
if (!state.isReady) { if (!state.isReady) {
clearTimeout(state.tmrConn); clearTimeout(state.tmrConn);
state.isReady = true; state.isReady = true;
state.conn.emit('ready'); state.conn.emit('ready');
} }
} else if (/^ALERT$/i.test(code)) } else if (code === 'ALERT')
self.emit('alert', m[3]); self.emit('alert', m[3]);
else if (state.status === STATES.BOXSELECTING) { else if (state.status === STATES.BOXSELECTING) {
if (m = /^UIDVALIDITY (\d+)/i.exec(code)) if (code === 'UIDVALIDITY')
state.box.uidvalidity = parseInt(m[1], 10); state.box.uidvalidity = parseInt(codeval, 10);
else if (m = /^UIDNEXT (\d+)/i.exec(code)) else if (code === 'UIDNEXT')
state.box.uidnext = parseInt(m[1], 10); state.box.uidnext = parseInt(codeval, 10);
else if (m = /^PERMANENTFLAGS \((.*)\)/i.exec(code)) { else if (code === 'PERMANENTFLAGS') {
var idx, permFlags, keywords; var idx, permFlags, keywords;
state.box.permFlags = permFlags = m[1].split(' '); codeval = codeval.substr(1, codeval.length - 2);
state.box.permFlags = permFlags = codeval.split(' ');
if ((idx = state.box.permFlags.indexOf('\\*')) > -1) { if ((idx = state.box.permFlags.indexOf('\\*')) > -1) {
state.box.newKeywords = true; state.box.newKeywords = true;
permFlags.splice(idx, 1); permFlags.splice(idx, 1);
@ -570,8 +578,8 @@ ImapConnection.prototype.connect = function(loginCb) {
}); });
} }
} else if (state.status === STATES.BOXSELECTED) { } else if (state.status === STATES.BOXSELECTED) {
if (m = /^UIDVALIDITY (\d+)/i.exec(code)) { if (code === 'UIDVALIDITY') {
state.box.uidvalidity = parseInt(m[1], 10); state.box.uidvalidity = parseInt(codeval, 10);
self.emit('uidvalidity', state.box.uidvalidity); self.emit('uidvalidity', state.box.uidvalidity);
} }
} }
@ -648,10 +656,11 @@ ImapConnection.prototype.connect = function(loginCb) {
} }
if (typeof requests[0].callback === 'function') { if (typeof requests[0].callback === 'function') {
m = RE_TAGGED_RESP.exec(line);
var err = null; var err = null;
var args = requests[0].cbargs, var args = requests[0].cbargs,
cmdstr = requests[0].cmdstr; cmdstr = requests[0].cmdstr;
if (line[0] === '+') { if (!m) {
if (requests[0].cmd !== 'APPEND') { if (requests[0].cmd !== 'APPEND') {
err = new Error('Unexpected continuation'); err = new Error('Unexpected continuation');
err.level = 'protocol'; err.level = 'protocol';
@ -659,7 +668,7 @@ ImapConnection.prototype.connect = function(loginCb) {
err.request = cmdstr; err.request = cmdstr;
} else } else
return requests[0].callback(); return requests[0].callback();
} else if (m = /^A\d+ (NO|BAD) (?:\[(.+?)\] )?(.+)$/i.exec(line)) { } else if (m[1] !== 'OK') {
// m[1]: error type // m[1]: error type
// m[2]: resp-text-code // m[2]: resp-text-code
// m[3]: message // m[3]: message
@ -680,6 +689,19 @@ ImapConnection.prototype.connect = function(loginCb) {
) && args.length === 0) ) && args.length === 0)
args.unshift([]); args.unshift([]);
} }
if (m) {
var msg = m[3], info;
if (m[2]) {
m = RE_TEXT_CODE.exec(m[2]);
info = {
code: m[1].toUpperCase(),
codeval: m[2],
message: msg
};
} else
info = { message: msg };
args.push(info);
}
args.unshift(err); args.unshift(err);
requests[0].callback.apply(self, args); requests[0].callback.apply(self, args);
} }

Loading…
Cancel
Save