mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Migrate email node to credentials system
This commit is contained in:
parent
c0842455b9
commit
8ddfa9eb29
@ -53,26 +53,25 @@
|
|||||||
<input type="text" id="node-input-port" placeholder="465">
|
<input type="text" id="node-input-port" placeholder="465">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-config-input-userid"><i class="icon-user"></i> Userid</label>
|
<label for="node-input-userid"><i class="icon-user"></i> Userid</label>
|
||||||
<input type="text" id="node-config-input-userid">
|
<input type="text" id="node-input-userid">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
|
<label for="node-input-password"><i class="icon-lock"></i> Password</label>
|
||||||
<input type="password" id="node-config-input-password">
|
<input type="password" id="node-input-password">
|
||||||
</div>
|
</div>
|
||||||
<br/>
|
<br/>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-dname"><i class="icon-tag"></i> Name</label>
|
<label for="node-input-dname"><i class="icon-tag"></i> Name</label>
|
||||||
<input type="text" id="node-input-dname" placeholder="Name">
|
<input type="text" id="node-input-dname" placeholder="Name">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-tips" id="node-tip"><b>Note:</b> Using credentials from global emailkeys.js file.</div>
|
<div class="form-tips" id="node-tip"><b>Note:</b> Copied credentials from global emailkeys.js file.</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="e-mail">
|
<script type="text/x-red" data-help-name="e-mail">
|
||||||
<p>Sends the <b>msg.payload</b> as an email, with a subject of <b>msg.topic</b>.</p>
|
<p>Sends the <b>msg.payload</b> as an email, with a subject of <b>msg.topic</b>.</p>
|
||||||
<p>It sends the message to the configured recipient <i>only</i>.</p>
|
<p>It sends the message to the configured recipient <i>only</i>.</p>
|
||||||
<p><b>msg.topic</b> is used to set the subject of the email, and <b>msg.payload</b> is the body text.</p>
|
<p><b>msg.topic</b> is used to set the subject of the email, and <b>msg.payload</b> is the body text.</p>
|
||||||
<p>Uses the nodemailer module.</p>
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
@ -86,6 +85,12 @@
|
|||||||
name: {value:"",required:true},
|
name: {value:"",required:true},
|
||||||
dname: {value:""}
|
dname: {value:""}
|
||||||
},
|
},
|
||||||
|
credentials: {
|
||||||
|
userid: {type:"text"},
|
||||||
|
password: {type: "password"},
|
||||||
|
global: { type:"boolean"}
|
||||||
|
},
|
||||||
|
|
||||||
inputs:1,
|
inputs:1,
|
||||||
outputs:0,
|
outputs:0,
|
||||||
icon: "envelope.png",
|
icon: "envelope.png",
|
||||||
@ -97,40 +102,11 @@
|
|||||||
return (this.dname||!this.topic)?"node_label_italic":"";
|
return (this.dname||!this.topic)?"node_label_italic":"";
|
||||||
},
|
},
|
||||||
oneditprepare: function() {
|
oneditprepare: function() {
|
||||||
$.getJSON('email/'+this.id,function(data) {
|
if (this.credentials.global) {
|
||||||
if (data.userid) {
|
$('#node-tip').show();
|
||||||
$('#node-config-input-userid').val(data.userid);
|
} else {
|
||||||
}
|
$('#node-tip').hide();
|
||||||
if (data.hasPassword) {
|
};
|
||||||
$('#node-config-input-password').val('__PWRD__');
|
|
||||||
} else {
|
|
||||||
$('#node-config-input-password').val('');
|
|
||||||
}
|
|
||||||
if (data.global) $('#node-tip').show();
|
|
||||||
else $('#node-tip').hide();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
oneditsave: function() {
|
|
||||||
var credentials = {};
|
|
||||||
var newUser = $('#node-config-input-userid').val();
|
|
||||||
var newPass = $('#node-config-input-password').val();
|
|
||||||
credentials.userid = newUser;
|
|
||||||
if (newPass != '__PWRD__') {
|
|
||||||
credentials.password = newPass;
|
|
||||||
}
|
|
||||||
$.ajax({
|
|
||||||
url: 'email/'+this.id,
|
|
||||||
type: 'POST',
|
|
||||||
data: credentials,
|
|
||||||
success: function(result){}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
ondelete: function() {
|
|
||||||
$.ajax({
|
|
||||||
url: 'email/'+this.id,
|
|
||||||
type: 'DELETE',
|
|
||||||
success: function(result) {}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
@ -151,19 +127,19 @@
|
|||||||
<input type="text" id="node-input-port" placeholder="993">
|
<input type="text" id="node-input-port" placeholder="993">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-config-input-userid"><i class="icon-user"></i> Userid</label>
|
<label for="node-input-userid"><i class="icon-user"></i> Userid</label>
|
||||||
<input type="text" id="node-config-input-userid">
|
<input type="text" id="node-input-userid">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
|
<label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
|
||||||
<input type="password" id="node-config-input-password">
|
<input type="password" id="node-input-password">
|
||||||
</div>
|
</div>
|
||||||
<br/>
|
<br/>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||||
<input type="text" id="node-input-name" placeholder="Name">
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-tips" id="node-tip"><b>Note:</b> Using credentials from global emailkeys.js file.</div>
|
<div class="form-tips" id="node-tip"><b>Note:</b> Copied credentials from global emailkeys.js file.</div>
|
||||||
<div id="node-input-tip" class="form-tips">Tip: <b>ONLY</b> retrieves the single most recent email.</div>
|
<div id="node-input-tip" class="form-tips">Tip: <b>ONLY</b> retrieves the single most recent email.</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -186,6 +162,11 @@
|
|||||||
port: {value:"993",required:true},
|
port: {value:"993",required:true},
|
||||||
name: {value:""}
|
name: {value:""}
|
||||||
},
|
},
|
||||||
|
credentials: {
|
||||||
|
userid: {type:"text"},
|
||||||
|
password: {type: "password"},
|
||||||
|
global: { type:"boolean"}
|
||||||
|
},
|
||||||
inputs:0,
|
inputs:0,
|
||||||
outputs:1,
|
outputs:1,
|
||||||
icon: "envelope.png",
|
icon: "envelope.png",
|
||||||
@ -196,40 +177,11 @@
|
|||||||
return (this.name||!this.topic)?"node_label_italic":"";
|
return (this.name||!this.topic)?"node_label_italic":"";
|
||||||
},
|
},
|
||||||
oneditprepare: function() {
|
oneditprepare: function() {
|
||||||
$.getJSON('email/'+this.id,function(data) {
|
if (this.credentials.global) {
|
||||||
if (data.userid) {
|
$('#node-tip').show();
|
||||||
$('#node-config-input-userid').val(data.userid);
|
} else {
|
||||||
}
|
$('#node-tip').hide();
|
||||||
if (data.hasPassword) {
|
};
|
||||||
$('#node-config-input-password').val('__PWRD__');
|
|
||||||
} else {
|
|
||||||
$('#node-config-input-password').val('');
|
|
||||||
}
|
|
||||||
if (data.global) $('#node-tip').show();
|
|
||||||
else $('#node-tip').hide();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
oneditsave: function() {
|
|
||||||
var credentials = {};
|
|
||||||
var newUser = $('#node-config-input-userid').val();
|
|
||||||
var newPass = $('#node-config-input-password').val();
|
|
||||||
credentials.userid = newUser;
|
|
||||||
if (newPass != '__PWRD__') {
|
|
||||||
credentials.password = newPass;
|
|
||||||
}
|
|
||||||
$.ajax({
|
|
||||||
url: 'email/'+this.id,
|
|
||||||
type: 'POST',
|
|
||||||
data: credentials,
|
|
||||||
success: function(result){}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
ondelete: function() {
|
|
||||||
$.ajax({
|
|
||||||
url: 'email/'+this.id,
|
|
||||||
type: 'DELETE',
|
|
||||||
success: function(result) {}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
@ -22,8 +22,10 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
//console.log(nodemailer.Transport.transports.SMTP.wellKnownHosts);
|
//console.log(nodemailer.Transport.transports.SMTP.wellKnownHosts);
|
||||||
|
|
||||||
try { var globalkeys = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js"); }
|
try {
|
||||||
catch(err) { }
|
var globalkeys = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
|
||||||
|
} catch(err) {
|
||||||
|
}
|
||||||
|
|
||||||
function EmailNode(n) {
|
function EmailNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
@ -32,18 +34,29 @@ module.exports = function(RED) {
|
|||||||
this.outserver = n.server;
|
this.outserver = n.server;
|
||||||
this.outport = n.port;
|
this.outport = n.port;
|
||||||
var flag = false;
|
var flag = false;
|
||||||
var credentials = RED.nodes.getCredentials(n.id);
|
if (this.credentials && this.credentials.hasOwnProperty("userid")) {
|
||||||
if ((credentials) && (credentials.hasOwnProperty("userid"))) { this.userid = credentials.userid; }
|
this.userid = this.credentials.userid;
|
||||||
else {
|
} else {
|
||||||
if (globalkeys) { this.userid = globalkeys.user; flag = true; }
|
if (globalkeys) {
|
||||||
else { this.error("No e-mail userid set"); }
|
this.userid = globalkeys.user;
|
||||||
|
flag = true;
|
||||||
|
} else {
|
||||||
|
this.error("No e-mail userid set");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ((credentials) && (credentials.hasOwnProperty("password"))) { this.password = credentials.password; }
|
if (this.credentials && this.credentials.hasOwnProperty("password")) {
|
||||||
else {
|
this.password = this.credentials.password;
|
||||||
if (globalkeys) { this.password = globalkeys.pass; flag = true; }
|
} else {
|
||||||
else { this.error("No e-mail password set"); }
|
if (globalkeys) {
|
||||||
|
this.password = globalkeys.pass;
|
||||||
|
flag = true;
|
||||||
|
} else {
|
||||||
|
this.error("No e-mail password set");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (flag) {
|
||||||
|
RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true});
|
||||||
}
|
}
|
||||||
if (flag) { RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true}); }
|
|
||||||
var node = this;
|
var node = this;
|
||||||
|
|
||||||
var smtpTransport = nodemailer.createTransport({
|
var smtpTransport = nodemailer.createTransport({
|
||||||
@ -88,7 +101,13 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("e-mail",EmailNode);
|
RED.nodes.registerType("e-mail",EmailNode,{
|
||||||
|
credentials: {
|
||||||
|
userid: {type:"text"},
|
||||||
|
password: {type: "password"},
|
||||||
|
global: { type:"boolean"}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
function EmailInNode(n) {
|
function EmailInNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
@ -97,18 +116,31 @@ module.exports = function(RED) {
|
|||||||
this.inserver = n.server || emailkey.server || "imap.gmail.com";
|
this.inserver = n.server || emailkey.server || "imap.gmail.com";
|
||||||
this.inport = n.port || emailkey.port || "993";
|
this.inport = n.port || emailkey.port || "993";
|
||||||
var flag = false;
|
var flag = false;
|
||||||
var credentials = RED.nodes.getCredentials(n.id);
|
|
||||||
if ((credentials) && (credentials.hasOwnProperty("userid"))) { this.userid = credentials.userid; }
|
if (this.credentials && this.credentials.hasOwnProperty("userid")) {
|
||||||
else {
|
this.userid = this.credentials.userid;
|
||||||
if (globalkeys) { this.userid = globalkeys.user; flag = true; }
|
} else {
|
||||||
else { this.error("No e-mail userid set"); }
|
if (globalkeys) {
|
||||||
|
this.userid = globalkeys.user;
|
||||||
|
flag = true;
|
||||||
|
} else {
|
||||||
|
this.error("No e-mail userid set");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ((credentials) && (credentials.hasOwnProperty("password"))) { this.password = credentials.password; }
|
if (this.credentials && this.credentials.hasOwnProperty("password")) {
|
||||||
else {
|
this.password = this.credentials.password;
|
||||||
if (globalkeys) { this.password = globalkeys.pass; flag = true; }
|
} else {
|
||||||
else { this.error("No e-mail password set"); }
|
if (globalkeys) {
|
||||||
|
this.password = globalkeys.pass;
|
||||||
|
flag = true;
|
||||||
|
} else {
|
||||||
|
this.error("No e-mail password set");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (flag) { RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true}); }
|
if (flag) {
|
||||||
|
RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true});
|
||||||
|
}
|
||||||
|
|
||||||
var node = this;
|
var node = this;
|
||||||
this.interval_id = null;
|
this.interval_id = null;
|
||||||
var oldmail = {};
|
var oldmail = {};
|
||||||
@ -215,54 +247,11 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
node.emit("input",{});
|
node.emit("input",{});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("e-mail in",EmailInNode);
|
RED.nodes.registerType("e-mail in",EmailInNode,{
|
||||||
|
credentials: {
|
||||||
var querystring = require('querystring');
|
userid: {type:"text"},
|
||||||
|
password: {type: "password"},
|
||||||
RED.httpAdmin.get('/email/global',function(req,res) {
|
global: { type:"boolean"}
|
||||||
res.send(JSON.stringify({hasToken:!(globalkeys && globalkeys.userid && globalkeys.password)}));
|
}
|
||||||
});
|
|
||||||
|
|
||||||
RED.httpAdmin.get('/email/:id',function(req,res) {
|
|
||||||
var credentials = RED.nodes.getCredentials(req.params.id);
|
|
||||||
if (credentials) {
|
|
||||||
res.send(JSON.stringify({userid:credentials.userid,hasPassword:(credentials.password&&credentials.password!="")}));
|
|
||||||
}
|
|
||||||
else if (globalkeys && globalkeys.user && globalkeys.pass) {
|
|
||||||
RED.nodes.addCredentials(req.params.id,{userid:globalkeys.user, password:globalkeys.pass, global:true});
|
|
||||||
credentials = RED.nodes.getCredentials(req.params.id);
|
|
||||||
res.send(JSON.stringify({userid:credentials.userid,global:credentials.global,hasPassword:(credentials.password&&credentials.password!="")}));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.send(JSON.stringify({}));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
RED.httpAdmin.delete('/email/:id',function(req,res) {
|
|
||||||
RED.nodes.deleteCredentials(req.params.id);
|
|
||||||
res.send(200);
|
|
||||||
});
|
|
||||||
|
|
||||||
RED.httpAdmin.post('/email/:id',function(req,res) {
|
|
||||||
var body = "";
|
|
||||||
req.on('data', function(chunk) {
|
|
||||||
body+=chunk;
|
|
||||||
});
|
|
||||||
req.on('end', function(){
|
|
||||||
var newCreds = querystring.parse(body);
|
|
||||||
var credentials = RED.nodes.getCredentials(req.params.id)||{};
|
|
||||||
if (newCreds.userid == null || newCreds.userid == "") {
|
|
||||||
delete credentials.userid;
|
|
||||||
} else {
|
|
||||||
credentials.userid = newCreds.userid;
|
|
||||||
}
|
|
||||||
if (newCreds.password == "") {
|
|
||||||
delete credentials.password;
|
|
||||||
} else {
|
|
||||||
credentials.password = newCreds.password||credentials.password;
|
|
||||||
}
|
|
||||||
RED.nodes.addCredentials(req.params.id,credentials);
|
|
||||||
res.send(200);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user