Allow email node to fetch node if required

to close #513
This commit is contained in:
Dave Conway-Jones 2019-01-31 21:00:31 +00:00
parent 2ca330fcb8
commit 2c8e4780e1
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
4 changed files with 50 additions and 12 deletions

View File

@ -121,9 +121,16 @@
<script type="text/x-red" data-template-name="e-mail in"> <script type="text/x-red" data-template-name="e-mail in">
<div class="form-row node-input-repeat"> <div class="form-row">
<label for="node-input-repeat"><i class="fa fa-repeat"></i> <span data-i18n="email.label.repeat"></span></label> <label for="node-input-fetch"><i class="fa fa-hourglass-half"></i> <span data-i18n="email.label.getmail"></span></label>
<input type="text" id="node-input-repeat" style="width: 80px"> <span data-i18n="email.label.seconds">seconds</span> <select type="text" id="node-input-fetch" style="width:130px;">
<option value="auto" data-i18n="email.label.auto"></option>
<option value="trigger" data-i18n="email.label.trigger"></option>
</select>
<span id="node-repeatTime">
<span style="margin-left:20px;" data-i18n="email.label.repeat"></span>
<input type="text" id="node-input-repeat" style="width:80px"> <span data-i18n="email.label.seconds">seconds</span>
</span>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-protocol"><i class="fa fa-envelope"></i> <span data-i18n="email.label.protocol"></span></label> <label for="node-input-protocol"><i class="fa fa-envelope"></i> <span data-i18n="email.label.protocol"></span></label>
@ -182,8 +189,8 @@
}; };
$("#node-input-useSSL").change(function(x, y) { $("#node-input-useSSL").change(function(x, y) {
console.log("useSSL: x="+ JSON.stringify(x) + ", y=" + y); // console.log("useSSL: x="+ JSON.stringify(x) + ", y=" + y);
console.log("Value: " + $("#node-input-useSSL").prop("checked")); // console.log("Value: " + $("#node-input-useSSL").prop("checked"));
checkPorts(); checkPorts();
}); });
@ -245,15 +252,17 @@
port: {value:"993",required:true}, port: {value:"993",required:true},
box: {value:"INBOX"}, // For IMAP, The mailbox to process box: {value:"INBOX"}, // For IMAP, The mailbox to process
disposition: { value: "Read" }, // For IMAP, the disposition of the read email disposition: { value: "Read" }, // For IMAP, the disposition of the read email
repeat: {value:"300",required:true} repeat: {value:"300",required:true},
fetch: {value:"auto"},
inputs: {value:0}
}, },
credentials: { credentials: {
userid: {type:"text"}, userid: {type:"text"},
password: {type: "password"}, password: {type: "password"},
global: { type:"boolean"} global: { type:"boolean"}
}, },
inputs:0, inputs: 0,
outputs:1, outputs: 1,
icon: "envelope.png", icon: "envelope.png",
paletteLabel: function() { paletteLabel: function() {
return this._("email.email"); return this._("email.email");
@ -265,6 +274,7 @@
return (this.name)?"node_label_italic":""; return (this.name)?"node_label_italic":"";
}, },
oneditprepare: function() { oneditprepare: function() {
var that = this;
if (this.credentials.global) { if (this.credentials.global) {
$('#node-tip').show(); $('#node-tip').show();
} else { } else {
@ -274,6 +284,17 @@
$("#node-input-box").val("INBOX"); $("#node-input-box").val("INBOX");
this.box = "INBOX"; this.box = "INBOX";
} }
if ($("#node-input-fetch").val() === null) { $("#node-input-fetch").val("auto"); }
$("#node-input-fetch").change(function() {
if ($("#node-input-fetch").val() === "trigger") {
$('#node-repeatTime').hide();
that.inputs = 1;
}
else {
$('#node-repeatTime').show();
that.inputs = 0;
}
});
} }
}); });
})(); })();

View File

@ -146,12 +146,14 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.name = n.name; this.name = n.name;
this.inputs = n.inputs;
this.repeat = n.repeat * 1000 || 300000; this.repeat = n.repeat * 1000 || 300000;
if (this.repeat > 2147483647) { if (this.repeat > 2147483647) {
// setTimeout/Interval has a limit of 2**31-1 Milliseconds // setTimeout/Interval has a limit of 2**31-1 Milliseconds
this.repeat = 2147483647; this.repeat = 2147483647;
this.error(RED._("email.errors.refreshtoolarge")); this.error(RED._("email.errors.refreshtoolarge"));
} }
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";
this.box = n.box || "INBOX"; this.box = n.box || "INBOX";
@ -315,10 +317,12 @@ 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;
function checkIMAP(msg) { function checkIMAP(msg) {
//node.log("Checking IMAP for new messages"); //node.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
imap.once("ready", function() { imap.once("ready", function() {
s = 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
@ -328,6 +332,7 @@ module.exports = function(RED) {
//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;
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();
@ -337,6 +342,7 @@ 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();
@ -347,6 +353,7 @@ 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();
setInputRepeatTimeout(); setInputRepeatTimeout();
@ -386,6 +393,7 @@ 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();
@ -409,6 +417,7 @@ 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
@ -416,10 +425,13 @@ 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") {
checkIMAP(msg); if (s !== false) {
checkIMAP(msg);
}
} }
} // End of checkEmail } // End of checkEmail
@ -437,8 +449,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;
node.status({fill:"red",shape:"ring",text:"email.status.connecterror"}); node.status({fill:"red",shape:"ring",text:"email.status.connecterror"});
} }
setInputRepeatTimeout()
}); });
} }
@ -462,7 +476,7 @@ module.exports = function(RED) {
} }
} }
node.emit("input",{}); if (this.inputs !== 1) { node.emit("input",{}); }
} }
RED.nodes.registerType("e-mail in",EmailInNode,{ RED.nodes.registerType("e-mail in",EmailInNode,{

View File

@ -2,13 +2,16 @@
"email": { "email": {
"email": "email", "email": "email",
"label": { "label": {
"getmail":"Get mail",
"auto": "automatically",
"trigger": "when triggered",
"to": "To", "to": "To",
"server": "Server", "server": "Server",
"port": "Port", "port": "Port",
"useSecureConnection": "Use secure connection.", "useSecureConnection": "Use secure connection.",
"userid": "Userid", "userid": "Userid",
"password": "Password", "password": "Password",
"repeat": "Refresh", "repeat": "every",
"seconds": "seconds", "seconds": "seconds",
"folder": "Folder", "folder": "Folder",
"protocol": "Protocol", "protocol": "Protocol",

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red-node-email", "name": "node-red-node-email",
"version": "1.0.7", "version": "1.1.0",
"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",