Skip to content

Commit 761bb30

Browse files
author
Dave Conway-Jones
committed
Email node - better handling of criteria errors
1 parent 2451099 commit 761bb30

File tree

4 files changed

+96
-70
lines changed

4 files changed

+96
-70
lines changed

social/email/61-email.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@
291291
that.inputs = 0;
292292
}
293293
});
294+
$("#node-input-criteria").change(function() {
295+
if ($("#node-input-criteria").val() === "_msg_") {
296+
$("#node-input-fetch").val("trigger");
297+
$("#node-input-fetch").change();
298+
}
299+
});
294300
}
295301
});
296302
})();

social/email/61-email.js

Lines changed: 84 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -393,78 +393,96 @@ module.exports = function(RED) {
393393
var criteria = ((node.criteria === '_msg_')?
394394
(msg.criteria || ["UNSEEN"]):
395395
([node.criteria]));
396-
imap.search(criteria, function(err, results) {
397-
if (err) {
398-
node.status({fill:"red", shape:"ring", text:"email.status.foldererror"});
399-
node.error(RED._("email.errors.fetchfail", {folder:node.box}),err);
400-
imap.end();
401-
s = false;
402-
setInputRepeatTimeout();
403-
return;
404-
}
405-
else {
406-
//console.log("> search - err=%j, results=%j", err, results);
407-
if (results.length === 0) {
408-
//console.log(" [X] - Nothing to fetch");
409-
node.status({results:0});
410-
imap.end();
411-
s = false;
412-
setInputRepeatTimeout();
413-
return;
414-
}
396+
if (Array.isArray(criteria)) {
397+
try {
398+
imap.search(criteria, function(err, results) {
399+
if (err) {
400+
node.status({fill:"red", shape:"ring", text:"email.status.foldererror"});
401+
node.error(RED._("email.errors.fetchfail", {folder:node.box}),err);
402+
imap.end();
403+
s = false;
404+
setInputRepeatTimeout();
405+
return;
406+
}
407+
else {
408+
//console.log("> search - err=%j, results=%j", err, results);
409+
if (results.length === 0) {
410+
//console.log(" [X] - Nothing to fetch");
411+
node.status({results:0});
412+
imap.end();
413+
s = false;
414+
setInputRepeatTimeout();
415+
return;
416+
}
417+
418+
var marks = false;
419+
if (node.disposition === "Read") { marks = true; }
420+
// We have the search results that contain the list of unseen messages and can now fetch those messages.
421+
var fetch = imap.fetch(results, {
422+
bodies: '',
423+
struct: true,
424+
markSeen: marks
425+
});
415426

416-
var marks = false;
417-
if (node.disposition === "Read") { marks = true; }
418-
// We have the search results that contain the list of unseen messages and can now fetch those messages.
419-
var fetch = imap.fetch(results, {
420-
bodies: '',
421-
struct: true,
422-
markSeen: marks
423-
});
424-
425-
// For each fetched message returned ...
426-
fetch.on('message', function(imapMessage, seqno) {
427-
//node.log(RED._("email.status.message",{number:seqno}));
428-
//console.log("> Fetch message - msg=%j, seqno=%d", imapMessage, seqno);
429-
imapMessage.on('body', function(stream, info) {
430-
//console.log("> message - body - stream=?, info=%j", info);
431-
simpleParser(stream, {}, function(err, parsed) {
432-
if (err) {
433-
node.status({fill:"red", shape:"ring", text:"email.status.parseerror"});
434-
node.error(RED._("email.errors.parsefail", {folder:node.box}),err);
435-
}
436-
else {
437-
processNewMessage(msg, parsed);
427+
// For each fetched message returned ...
428+
fetch.on('message', function(imapMessage, seqno) {
429+
//node.log(RED._("email.status.message",{number:seqno}));
430+
//console.log("> Fetch message - msg=%j, seqno=%d", imapMessage, seqno);
431+
imapMessage.on('body', function(stream, info) {
432+
//console.log("> message - body - stream=?, info=%j", info);
433+
simpleParser(stream, {}, function(err, parsed) {
434+
if (err) {
435+
node.status({fill:"red", shape:"ring", text:"email.status.parseerror"});
436+
node.error(RED._("email.errors.parsefail", {folder:node.box}),err);
437+
}
438+
else {
439+
processNewMessage(msg, parsed);
440+
}
441+
});
442+
}); // End of msg->body
443+
}); // End of fetch->message
444+
445+
// When we have fetched all the messages, we don't need the imap connection any more.
446+
fetch.on('end', function() {
447+
node.status({results:results.length});
448+
var cleanup = function() {
449+
imap.end();
450+
s = false;
451+
setInputRepeatTimeout();
452+
};
453+
if (node.disposition === "Delete") {
454+
imap.addFlags(results, "\Deleted", cleanup);
455+
} else if (node.disposition === "Read") {
456+
imap.addFlags(results, "\Seen", cleanup);
457+
} else {
458+
cleanup();
438459
}
439460
});
440-
}); // End of msg->body
441-
}); // End of fetch->message
442461

443-
// When we have fetched all the messages, we don't need the imap connection any more.
444-
fetch.on('end', function() {
445-
node.status({results:results.length});
446-
var cleanup = function() {
447-
imap.end();
448-
s = false;
449-
setInputRepeatTimeout();
450-
};
451-
if (node.disposition === "Delete") {
452-
imap.addFlags(results, "\Deleted", cleanup);
453-
} else if (node.disposition === "Read") {
454-
imap.addFlags(results, "\Seen", cleanup);
455-
} else {
456-
cleanup();
462+
fetch.once('error', function(err) {
463+
console.log('Fetch error: ' + err);
464+
imap.end();
465+
s = false;
466+
setInputRepeatTimeout();
467+
});
457468
}
458-
});
459-
460-
fetch.once('error', function(err) {
461-
console.log('Fetch error: ' + err);
462-
imap.end();
463-
s = false;
464-
setInputRepeatTimeout();
465-
});
469+
}); // End of imap->search
466470
}
467-
}); // End of imap->search
471+
catch(e) {
472+
node.status({fill:"red", shape:"ring", text:"email.status.bad_criteria"});
473+
node.error(e.toString(),e);
474+
s = ss = false;
475+
imap.end();
476+
return;
477+
}
478+
}
479+
else {
480+
node.status({fill:"red", shape:"ring", text:"email.status.bad_criteria"});
481+
node.error(RED._("email.errors.bad_criteria"),msg);
482+
s = ss = false;
483+
imap.end();
484+
return;
485+
}
468486
}
469487
}); // End of imap->openInbox
470488
}); // End of imap->ready

social/email/locales/en-US/61-email.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"sending": "sending",
5555
"sendfail": "send failed",
5656
"parseerror": "Failed to parse message",
57-
"connecterror": "connect error"
57+
"connecterror": "connect error",
58+
"bad_criteria": "Invalid criteria"
5859
},
5960
"errors": {
6061
"nouserid": "No e-mail userid set",
@@ -66,7 +67,8 @@
6667
"parsefail": "Failed to parse message",
6768
"messageerror": "Fetch message error: __error__",
6869
"refreshtoolarge": "Refresh interval too large. Limiting to 2147483 seconds",
69-
"invalidattachment": "Invalid attachment content. Must be String or buffer"
70+
"invalidattachment": "Invalid attachment content. Must be String or buffer",
71+
"bad_criteria": "Criteria must be a JSON array. See info."
7072
}
7173
}
7274
}

social/email/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "node-red-node-email",
3-
"version": "1.12.2",
3+
"version": "1.12.3",
44
"description": "Node-RED nodes to send and receive simple emails.",
55
"dependencies": {
66
"imap": "^0.8.19",
77
"poplib": "^0.1.7",
88
"mailparser": "^3.2.0",
9-
"nodemailer": "~6.6.2",
9+
"nodemailer": "~6.6.3",
1010
"smtp-server": "^3.9.0"
1111
},
1212
"bundledDependencies": [

0 commit comments

Comments
 (0)