diff --git a/social/xmpp/92-xmpp.html b/social/xmpp/92-xmpp.html index b16f4c5a..344ea8b2 100644 --- a/social/xmpp/92-xmpp.html +++ b/social/xmpp/92-xmpp.html @@ -17,9 +17,7 @@ + + + + diff --git a/social/xmpp/92-xmpp.js b/social/xmpp/92-xmpp.js index f7755bc3..b1f723e6 100644 --- a/social/xmpp/92-xmpp.js +++ b/social/xmpp/92-xmpp.js @@ -22,20 +22,81 @@ console.warn=(function() { // suppress warning from stringprep when not needed) }; })(); +try { + var xmppkey = RED.settings.xmpp || require(process.env.NODE_RED_HOME+"/../xmppkeys.js"); +} catch(err) { +// throw new Error("Failed to load XMPP credentials"); +} + var RED = require(process.env.NODE_RED_HOME+"/red/red"); var xmpp = require('simple-xmpp'); console.warn = orig; -try { - var xmppkey = RED.settings.xmpp || require(process.env.NODE_RED_HOME+"/../xmppkeys.js"); -} catch(err) { - throw new Error("Failed to load XMPP credentials"); +function XMPPServerNode(n) { + RED.nodes.createNode(this,n); + this.server = n.server; + this.port = n.port; + var credentials = RED.nodes.getCredentials(n.id); + if (credentials) { + this.username = credentials.user; + this.password = credentials.password; + } } +RED.nodes.registerType("xmpp-server",XMPPServerNode); + +var querystring = require('querystring'); + +RED.httpAdmin.get('/xmpp-server/:id',function(req,res) { + var credentials = RED.nodes.getCredentials(req.params.id); + if (credentials) { + res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")})); + } else { + res.send(JSON.stringify({})); + } +}); + +RED.httpAdmin.delete('/xmpp-server/:id',function(req,res) { + RED.nodes.deleteCredentials(req.params.id); + res.send(200); +}); + +RED.httpAdmin.post('/xmpp-server/: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.user == null || newCreds.user == "") { + delete credentials.user; + } else { + credentials.user = newCreds.user; + } + if (newCreds.password == "") { + delete credentials.password; + } else { + credentials.password = newCreds.password||credentials.password; + } + RED.nodes.addCredentials(req.params.id,credentials); + res.send(200); + }); +}); + function XmppNode(n) { RED.nodes.createNode(this,n); this.server = n.server; - this.port = n.port; + this.serverConfig = RED.nodes.getNode(this.server); + if (this.serverConfig){ + this.host = this.serverConfig.server; + this.port = this.serverConfig.port; + this.jid = this.serverConfig.username; + this.password = this.serverConfig.password; + } else { + console.log("no serverConfig found"); + } + this.join = n.join || false; this.nick = n.nick || "Node-RED"; this.sendAll = n.sendObject; @@ -44,10 +105,10 @@ function XmppNode(n) { setTimeout(function() { xmpp.connect({ - jid : xmppkey.jid, - password : xmppkey.password, - host : this.server, - port : this.port, + jid : node.jid, + password : node.password, + host : node.host, + port : node.port, skipPresence : true, reconnect : false });