From 8ddfa9eb29de41757d7e7e4ff1b4281f75e1b418 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 18 Aug 2014 17:15:14 +0100 Subject: [PATCH] Migrate email node to credentials system --- nodes/core/social/61-email.html | 108 +++++++------------------- nodes/core/social/61-email.js | 133 +++++++++++++++----------------- 2 files changed, 91 insertions(+), 150 deletions(-) diff --git a/nodes/core/social/61-email.html b/nodes/core/social/61-email.html index be08b44fa..a1de5f8dc 100644 --- a/nodes/core/social/61-email.html +++ b/nodes/core/social/61-email.html @@ -53,26 +53,25 @@
- - + +
- - + +

-
Note: Using credentials from global emailkeys.js file.
+
Note: Copied credentials from global emailkeys.js file.
@@ -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(); + }; } }); })(); diff --git a/nodes/core/social/61-email.js b/nodes/core/social/61-email.js index e7a4a56a9..2f45a0a6e 100644 --- a/nodes/core/social/61-email.js +++ b/nodes/core/social/61-email.js @@ -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"} + } }); }