From 71f48b538d50088f09a1fb43acc3f6142b122463 Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Sat, 30 May 2026 22:40:33 +0100 Subject: [PATCH] Fix regression on imap email-in trigger mode fixes #1132 Even if server supports IDLE if mode = triger disconnect after each request --- social/email/61-email.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/social/email/61-email.js b/social/email/61-email.js index 05c11a27..5dac29af 100644 --- a/social/email/61-email.js +++ b/social/email/61-email.js @@ -263,6 +263,7 @@ module.exports = function (RED) { this.error(RED._("email.errors.notoken")); } } + this.fetch = n.fetch || 'auto' var node = this; node.interval_id = null; @@ -439,14 +440,16 @@ module.exports = function (RED) { authTimeout: tout }); } - // Server notified about new mails during IDLE - imap.on('mail', function (numNew) { - fetchIMAP(); // new mail -> catch immediatly - }); - // Server notified about new attributes on (e.g. -> UNSEEN) during IDLE - imap.on('update', function () { - fetchIMAP(); - }); + if (node.fetch === 'auto') { + // Server notified about new mails during IDLE + imap.on('mail', function (numNew) { + fetchIMAP(); // new mail -> catch immediatly + }); + // Server notified about new attributes on (e.g. -> UNSEEN) during IDLE + imap.on('update', function () { + fetchIMAP(); + }); + } imap.on('close', function (hadError) { // var msg = 'IMAP connection closed'; if (hadError) { @@ -554,7 +557,7 @@ module.exports = function (RED) { //console.log("> search - err=%j, results=%j", err, results); if (results.length === 0) { //console.log(" [X] - Nothing to fetch"); - if (imap.serverSupports && imap.serverSupports('IDLE')) { + if (imap.serverSupports && imap.serverSupports('IDLE') && node.fetch === 'auto') { // IMAP IDLE node.status({ fill: "green", shape: "ring", text: RED._("email.status.fetched", { number: "0" } )}); node.statusErr = false; @@ -601,7 +604,7 @@ module.exports = function (RED) { // When we have fetched all the messages fetch.on('end', function () { var cleanup = function () { - if (imap.serverSupports && imap.serverSupports('IDLE')) { + if (imap.serverSupports && imap.serverSupports('IDLE') && node.fetch === 'auto') { // for IMAP IDLE, connection is expected to stay open node.status({ fill: "green", shape: "ring", text: RED._("email.status.fetched", { number: results.length.toString() } )}); node.statusErr = false; @@ -657,6 +660,7 @@ module.exports = function (RED) { } } + imap.connect(); } // End of checkIMAP @@ -666,7 +670,9 @@ module.exports = function (RED) { if (node.protocol === "POP3") { checkPOP3(msg, send, done); } else if (node.protocol === "IMAP") { - if (s === false && ss == false) { checkIMAP(msg, send, done); } + if (s === false && ss == false) { + checkIMAP(msg, send, done); + } } } // End of checkEmail