1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Stop email node producing duplicates on reconnect

to close #514
This commit is contained in:
Dave Conway-Jones 2019-02-01 00:00:16 +00:00
parent 2c8e4780e1
commit 88f6784267
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4

View File

@ -317,12 +317,15 @@ module.exports = function(RED) {
// checkIMAP // checkIMAP
// //
// Check the email sever using the IMAP protocol for new messages. // Check the email sever using the IMAP protocol for new messages.
var s = true; var s = false;
var ss = false;
function checkIMAP(msg) { function checkIMAP(msg) {
//node.log("Checking IMAP for new messages"); //console.log("Checking IMAP for new messages");
// We get back a 'ready' event once we have connected to imap // We get back a 'ready' event once we have connected to imap
s = true;
imap.once("ready", function() { imap.once("ready", function() {
s = true; if (ss === true) { return; }
ss = true;
node.status({fill:"blue", shape:"dot", text:"email.status.fetching"}); node.status({fill:"blue", shape:"dot", text:"email.status.fetching"});
//console.log("> ready"); //console.log("> ready");
// Open the inbox folder // Open the inbox folder
@ -342,10 +345,10 @@ module.exports = function(RED) {
else { else {
imap.search([ 'UNSEEN' ], function(err, results) { imap.search([ 'UNSEEN' ], function(err, results) {
if (err) { if (err) {
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();
s = false;
setInputRepeatTimeout(); setInputRepeatTimeout();
return; return;
} }
@ -353,9 +356,9 @@ module.exports = function(RED) {
//console.log("> search - err=%j, results=%j", err, results); //console.log("> search - err=%j, results=%j", err, results);
if (results.length === 0) { if (results.length === 0) {
//console.log(" [X] - Nothing to fetch"); //console.log(" [X] - Nothing to fetch");
s = true;
node.status({}); node.status({});
imap.end(); imap.end();
s = false;
setInputRepeatTimeout(); setInputRepeatTimeout();
return; return;
} }
@ -393,10 +396,10 @@ module.exports = function(RED) {
// When we have fetched all the messages, we don't need the imap connection any more. // When we have fetched all the messages, we don't need the imap connection any more.
fetch.on('end', function() { fetch.on('end', function() {
s = true;
node.status({}); node.status({});
var cleanup = function() { var cleanup = function() {
imap.end(); imap.end();
s = false;
}; };
if (this.disposition === "Delete") { if (this.disposition === "Delete") {
imap.addFlags(results, "\Deleted", cleanup); imap.addFlags(results, "\Deleted", cleanup);
@ -417,7 +420,6 @@ module.exports = function(RED) {
} }
}); // End of imap->openInbox }); // End of imap->openInbox
}); // End of imap->ready }); // End of imap->ready
s = false;
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();
} // End of checkIMAP } // End of checkIMAP
@ -425,13 +427,10 @@ module.exports = function(RED) {
// Perform a check of the email inboxes using either POP3 or IMAP // Perform a check of the email inboxes using either POP3 or IMAP
function checkEmail(msg) { function checkEmail(msg) {
console.log("STAT",s);
if (node.protocol === "POP3") { if (node.protocol === "POP3") {
checkPOP3(msg); checkPOP3(msg);
} else if (node.protocol === "IMAP") { } else if (node.protocol === "IMAP") {
if (s !== false) { if (s === false) { checkIMAP(msg); }
checkIMAP(msg);
}
} }
} // End of checkEmail } // End of checkEmail
@ -449,10 +448,10 @@ module.exports = function(RED) {
imap.on('error', function(err) { imap.on('error', function(err) {
if (err.errno !== "ECONNRESET") { if (err.errno !== "ECONNRESET") {
node.log(err); node.log(err);
s = true; s = false;
node.status({fill:"red",shape:"ring",text:"email.status.connecterror"}); node.status({fill:"red",shape:"ring",text:"email.status.connecterror"});
} }
setInputRepeatTimeout() setInputRepeatTimeout();
}); });
} }
@ -474,6 +473,7 @@ module.exports = function(RED) {
node.emit("input",{}); node.emit("input",{});
}, node.repeat ); }, node.repeat );
} }
ss = false;
} }
if (this.inputs !== 1) { node.emit("input",{}); } if (this.inputs !== 1) { node.emit("input",{}); }