slight adjust email retry timeout

This commit is contained in:
Dave Conway-Jones 2020-01-31 21:46:42 +00:00
parent 90a00279c0
commit 5780b2a5c3
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
2 changed files with 81 additions and 78 deletions

View File

@ -158,6 +158,9 @@ module.exports = function(RED) {
this.repeat = 2147483647; this.repeat = 2147483647;
this.error(RED._("email.errors.refreshtoolarge")); this.error(RED._("email.errors.refreshtoolarge"));
} }
if (this.repeat < 1500) {
this.repeat = 1500;
}
if (this.inputs === 1) { this.repeat = 0; } if (this.inputs === 1) { this.repeat = 0; }
this.inserver = n.server || (globalkeys && globalkeys.server) || "imap.gmail.com"; this.inserver = n.server || (globalkeys && globalkeys.server) || "imap.gmail.com";
this.inport = n.port || (globalkeys && globalkeys.port) || "993"; this.inport = n.port || (globalkeys && globalkeys.port) || "993";
@ -344,90 +347,90 @@ module.exports = function(RED) {
function(err, box) { function(err, box) {
//console.log("> Inbox err : %j", err); //console.log("> Inbox err : %j", err);
//console.log("> Inbox open: %j", box); //console.log("> Inbox open: %j", box);
if (err) { if (err) {
s = false; s = false;
node.status({fill:"red", shape:"ring", text:"email.status.foldererror"}); node.status({fill:"red", shape:"ring", text:"email.status.foldererror"});
node.error(RED._("email.errors.fetchfail", {folder:node.box}),err); node.error(RED._("email.errors.fetchfail", {folder:node.box}),err);
imap.end(); imap.end();
setInputRepeatTimeout(); setInputRepeatTimeout();
return; return;
} }
else { else {
var criteria = ((node.criteria === '_msg_')? var criteria = ((node.criteria === '_msg_')?
(msg.criteria || ["UNSEEN"]): (msg.criteria || ["UNSEEN"]):
([node.criteria])); ([node.criteria]));
imap.search(criteria, function(err, results) { imap.search(criteria, function(err, results) {
if (err) { if (err) {
node.status({fill:"red", shape:"ring", text:"email.status.foldererror"}); node.status({fill:"red", shape:"ring", text:"email.status.foldererror"});
node.error(RED._("email.errors.fetchfail", {folder:node.box}),err); node.error(RED._("email.errors.fetchfail", {folder:node.box}),err);
imap.end();
s = false;
setInputRepeatTimeout();
return;
}
else {
//console.log("> search - err=%j, results=%j", err, results);
if (results.length === 0) {
//console.log(" [X] - Nothing to fetch");
node.status({results:0});
imap.end(); imap.end();
s = false; s = false;
setInputRepeatTimeout(); setInputRepeatTimeout();
return; return;
} }
else {
var marks = false; //console.log("> search - err=%j, results=%j", err, results);
if (node.disposition === "Read") { marks = true; } if (results.length === 0) {
// We have the search results that contain the list of unseen messages and can now fetch those messages. //console.log(" [X] - Nothing to fetch");
var fetch = imap.fetch(results, { node.status({results:0});
bodies: '',
struct: true,
markSeen: marks
});
// For each fetched message returned ...
fetch.on('message', function(imapMessage, seqno) {
//node.log(RED._("email.status.message",{number:seqno}));
//console.log("> Fetch message - msg=%j, seqno=%d", imapMessage, seqno);
imapMessage.on('body', function(stream, info) {
//console.log("> message - body - stream=?, info=%j", info);
SimpleParser(stream, {}, function(err, parsed) {
if (err) {
node.status({fill:"red", shape:"ring", text:"email.status.parseerror"});
node.error(RED._("email.errors.parsefail", {folder:node.box}),err);
}
else {
processNewMessage(msg, parsed);
}
});
}); // End of msg->body
}); // End of fetch->message
// When we have fetched all the messages, we don't need the imap connection any more.
fetch.on('end', function() {
node.status({results:results.length});
var cleanup = function() {
imap.end(); imap.end();
s = false; s = false;
}; setInputRepeatTimeout();
if (this.disposition === "Delete") { return;
imap.addFlags(results, "\Deleted", cleanup);
} else if (this.disposition === "Read") {
imap.addFlags(results, "\Seen", cleanup);
} else {
cleanup();
} }
setInputRepeatTimeout();
});
fetch.once('error', function(err) { var marks = false;
console.log('Fetch error: ' + err); if (node.disposition === "Read") { marks = true; }
setInputRepeatTimeout(); // We have the search results that contain the list of unseen messages and can now fetch those messages.
}); var fetch = imap.fetch(results, {
} bodies: '',
}); // End of imap->search struct: true,
} markSeen: marks
}); // End of imap->openInbox });
// For each fetched message returned ...
fetch.on('message', function(imapMessage, seqno) {
//node.log(RED._("email.status.message",{number:seqno}));
//console.log("> Fetch message - msg=%j, seqno=%d", imapMessage, seqno);
imapMessage.on('body', function(stream, info) {
//console.log("> message - body - stream=?, info=%j", info);
SimpleParser(stream, {}, function(err, parsed) {
if (err) {
node.status({fill:"red", shape:"ring", text:"email.status.parseerror"});
node.error(RED._("email.errors.parsefail", {folder:node.box}),err);
}
else {
processNewMessage(msg, parsed);
}
});
}); // End of msg->body
}); // End of fetch->message
// When we have fetched all the messages, we don't need the imap connection any more.
fetch.on('end', function() {
node.status({results:results.length});
var cleanup = function() {
imap.end();
s = false;
};
if (this.disposition === "Delete") {
imap.addFlags(results, "\Deleted", cleanup);
} else if (this.disposition === "Read") {
imap.addFlags(results, "\Seen", cleanup);
} else {
cleanup();
}
setInputRepeatTimeout();
});
fetch.once('error', function(err) {
console.log('Fetch error: ' + err);
setInputRepeatTimeout();
});
}
}); // End of imap->search
}
}); // End of imap->openInbox
}); // End of imap->ready }); // End of imap->ready
node.status({fill:"grey",shape:"dot",text:"node-red:common.status.connecting"}); node.status({fill:"grey",shape:"dot",text:"node-red:common.status.connecting"});
imap.connect(); imap.connect();
@ -451,8 +454,8 @@ module.exports = function(RED) {
port: node.inport, port: node.inport,
tls: node.useSSL, tls: node.useSSL,
tlsOptions: { rejectUnauthorized: false }, tlsOptions: { rejectUnauthorized: false },
connTimeout: node.repeat, connTimeout: node.repeat - 500,
authTimeout: node.repeat authTimeout: node.repeat - 500
}); });
imap.on('error', function(err) { imap.on('error', function(err) {
if (err.errno !== "ECONNRESET") { if (err.errno !== "ECONNRESET") {

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red-node-email", "name": "node-red-node-email",
"version": "1.7.5", "version": "1.7.6",
"description": "Node-RED nodes to send and receive simple emails", "description": "Node-RED nodes to send and receive simple emails",
"dependencies": { "dependencies": {
"imap": "^0.8.19", "imap": "^0.8.19",