1
0
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:
Nick O'Leary 2014-08-18 17:15:14 +01:00
parent c0842455b9
commit 8ddfa9eb29
2 changed files with 91 additions and 150 deletions

View File

@ -53,26 +53,25 @@
<input type="text" id="node-input-port" placeholder="465">
</div>
<div class="form-row">
<label for="node-config-input-userid"><i class="icon-user"></i> Userid</label>
<input type="text" id="node-config-input-userid">
<label for="node-input-userid"><i class="icon-user"></i> Userid</label>
<input type="text" id="node-input-userid">
</div>
<div class="form-row">
<label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
<input type="password" id="node-config-input-password">
<label for="node-input-password"><i class="icon-lock"></i> Password</label>
<input type="password" id="node-input-password">
</div>
<br/>
<div class="form-row">
<label for="node-input-dname"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-dname" placeholder="Name">
</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 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>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>Uses the nodemailer module.</p>
</script>
<script type="text/javascript">
@ -86,6 +85,12 @@
name: {value:"",required:true},
dname: {value:""}
},
credentials: {
userid: {type:"text"},
password: {type: "password"},
global: { type:"boolean"}
},
inputs:1,
outputs:0,
icon: "envelope.png",
@ -97,40 +102,11 @@
return (this.dname||!this.topic)?"node_label_italic":"";
},
oneditprepare: function() {
$.getJSON('email/'+this.id,function(data) {
if (data.userid) {
$('#node-config-input-userid').val(data.userid);
}
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) {}
});
if (this.credentials.global) {
$('#node-tip').show();
} else {
$('#node-tip').hide();
};
}
});
})();
@ -151,19 +127,19 @@
<input type="text" id="node-input-port" placeholder="993">
</div>
<div class="form-row">
<label for="node-config-input-userid"><i class="icon-user"></i> Userid</label>
<input type="text" id="node-config-input-userid">
<label for="node-input-userid"><i class="icon-user"></i> Userid</label>
<input type="text" id="node-input-userid">
</div>
<div class="form-row">
<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>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</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>
</script>
@ -186,6 +162,11 @@
port: {value:"993",required:true},
name: {value:""}
},
credentials: {
userid: {type:"text"},
password: {type: "password"},
global: { type:"boolean"}
},
inputs:0,
outputs:1,
icon: "envelope.png",
@ -196,40 +177,11 @@
return (this.name||!this.topic)?"node_label_italic":"";
},
oneditprepare: function() {
$.getJSON('email/'+this.id,function(data) {
if (data.userid) {
$('#node-config-input-userid').val(data.userid);
}
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) {}
});
if (this.credentials.global) {
$('#node-tip').show();
} else {
$('#node-tip').hide();
};
}
});
})();

View File

@ -22,8 +22,10 @@ module.exports = function(RED) {
//console.log(nodemailer.Transport.transports.SMTP.wellKnownHosts);
try { var globalkeys = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js"); }
catch(err) { }
try {
var globalkeys = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
} catch(err) {
}
function EmailNode(n) {
RED.nodes.createNode(this,n);
@ -32,18 +34,29 @@ module.exports = function(RED) {
this.outserver = n.server;
this.outport = n.port;
var flag = false;
var credentials = RED.nodes.getCredentials(n.id);
if ((credentials) && (credentials.hasOwnProperty("userid"))) { this.userid = credentials.userid; }
else {
if (globalkeys) { this.userid = globalkeys.user; flag = true; }
else { this.error("No e-mail userid set"); }
if (this.credentials && this.credentials.hasOwnProperty("userid")) {
this.userid = this.credentials.userid;
} else {
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; }
else {
if (globalkeys) { this.password = globalkeys.pass; flag = true; }
else { this.error("No e-mail password set"); }
if (this.credentials && this.credentials.hasOwnProperty("password")) {
this.password = this.credentials.password;
} else {
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 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) {
RED.nodes.createNode(this,n);
@ -97,18 +116,31 @@ module.exports = function(RED) {
this.inserver = n.server || emailkey.server || "imap.gmail.com";
this.inport = n.port || emailkey.port || "993";
var flag = false;
var credentials = RED.nodes.getCredentials(n.id);
if ((credentials) && (credentials.hasOwnProperty("userid"))) { this.userid = credentials.userid; }
else {
if (globalkeys) { this.userid = globalkeys.user; flag = true; }
else { this.error("No e-mail userid set"); }
if (this.credentials && this.credentials.hasOwnProperty("userid")) {
this.userid = this.credentials.userid;
} else {
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; }
else {
if (globalkeys) { this.password = globalkeys.pass; flag = true; }
else { this.error("No e-mail password set"); }
if (this.credentials && this.credentials.hasOwnProperty("password")) {
this.password = this.credentials.password;
} else {
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;
this.interval_id = null;
var oldmail = {};
@ -215,54 +247,11 @@ module.exports = function(RED) {
node.emit("input",{});
}
RED.nodes.registerType("e-mail in",EmailInNode);
var querystring = require('querystring');
RED.httpAdmin.get('/email/global',function(req,res) {
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);
});
RED.nodes.registerType("e-mail in",EmailInNode,{
credentials: {
userid: {type:"text"},
password: {type: "password"},
global: { type:"boolean"}
}
});
}