From c5db30d0c45785371c3b7f5bba8217d614f79fb6 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Thu, 24 Sep 2015 18:58:24 +0100 Subject: [PATCH] Tidy up xmpp node formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (diff looks awful… it isn’t really) --- social/xmpp/92-xmpp.js | 328 ++++++++++++++++++++------------------- social/xmpp/package.json | 2 +- 2 files changed, 166 insertions(+), 164 deletions(-) diff --git a/social/xmpp/92-xmpp.js b/social/xmpp/92-xmpp.js index 75c06315..6ca92bd4 100644 --- a/social/xmpp/92-xmpp.js +++ b/social/xmpp/92-xmpp.js @@ -19,194 +19,196 @@ module.exports = function(RED) { var XMPP = require('simple-xmpp'); function XMPPServerNode(n) { - RED.nodes.createNode(this,n); - this.server = n.server; - this.port = n.port; - this.nickname = n.nickname; - var credentials = this.credentials; - if (credentials) { - this.username = credentials.user; - this.password = credentials.password; + RED.nodes.createNode(this,n); + this.server = n.server; + this.port = n.port; + this.nickname = n.nickname; + var credentials = this.credentials; + if (credentials) { + this.username = credentials.user; + this.password = credentials.password; + } } -} + RED.nodes.registerType("xmpp-server",XMPPServerNode,{ - credentials: { - user: {type:"text"}, - password: {type: "password"} - } -}); - - - function XmppInNode(n) { - RED.nodes.createNode(this,n); - this.server = n.server; - - this.serverConfig = RED.nodes.getNode(this.server); - this.host = this.serverConfig.server; - this.port = this.serverConfig.port; - this.nick = this.serverConfig.nickname || "Node-RED"; - this.userid = this.serverConfig.username; - this.password = this.serverConfig.password; - - this.join = n.join || false; - this.sendAll = n.sendObject; - this.to = n.to || ""; - var node = this; - - var xmpp = new XMPP.SimpleXMPP(); - - xmpp.on('online', function() { - node.log('connected to '+node.host+":"+node.port); - node.status({fill:"green",shape:"dot",text:"connected"}); - //xmpp.setPresence('online', node.nick+' online'); - if (node.join) { - xmpp.join(node.to+'/'+node.nick); + credentials: { + user: {type:"text"}, + password: {type: "password"} } }); - xmpp.on('chat', function(from, message) { - var msg = { topic:from, payload:message }; - node.send([msg,null]); - }); + function XmppInNode(n) { + RED.nodes.createNode(this,n); + this.server = n.server; - xmpp.on('groupchat', function(conference, from, message, stamp) { - var msg = { topic:from, payload:message, room:conference, ts:stamp }; - if (from != node.nick) { node.send([msg,null]); } - }); + this.serverConfig = RED.nodes.getNode(this.server); + this.host = this.serverConfig.server; + this.port = this.serverConfig.port; + this.nick = this.serverConfig.nickname || "Node-RED"; + this.userid = this.serverConfig.username; + this.password = this.serverConfig.password; - //xmpp.on('chatstate', function(from, state) { - //console.log('%s is currently %s', from, state); - //var msg = { topic:from, payload:state }; - //node.send([null,msg]); - //}); + this.join = n.join || false; + this.sendAll = n.sendObject; + this.to = n.to || ""; + var node = this; - xmpp.on('buddy', function(jid, state, statusText) { - node.log(jid+" is "+state+" : "+statusText); - var msg = { topic:jid, payload: { presence:state, status:statusText} }; - node.send([null,msg]); - }); + var xmpp = new XMPP.SimpleXMPP(); - xmpp.on('error', function(err) { - console.error("error",err); - }); - - xmpp.on('close', function() { - node.log('connection closed'); - node.status({fill:"red",shape:"ring",text:"not connected"}); - }); - - xmpp.on('subscribe', function(from) { - xmpp.acceptSubscription(from); - }); - - // Now actually make the connection - try { - xmpp.connect({ - jid : node.userid, - password : node.password, - host : node.host, - port : node.port, - skipPresence : true, - reconnect : false + xmpp.on('online', function() { + node.log('connected to '+node.host+":"+node.port); + node.status({fill:"green",shape:"dot",text:"connected"}); + //xmpp.setPresence('online', node.nick+' online'); + if (node.join) { + xmpp.join(node.to+'/'+node.nick); + } }); - } catch(e) { - node.error("Bad xmpp configuration"); - node.status({fill:"red",shape:"ring",text:"not connected"}); - } - node.on("close", function(done) { - //xmpp.setPresence('offline'); - if (xmpp.conn) { xmpp.conn.end(); } - xmpp = null; - done(); - }); -} + xmpp.on('chat', function(from, message) { + var msg = { topic:from, payload:message }; + node.send([msg,null]); + }); + + xmpp.on('groupchat', function(conference, from, message, stamp) { + var msg = { topic:from, payload:message, room:conference, ts:stamp }; + if (from != node.nick) { node.send([msg,null]); } + }); + + //xmpp.on('chatstate', function(from, state) { + //console.log('%s is currently %s', from, state); + //var msg = { topic:from, payload:state }; + //node.send([null,msg]); + //}); + + xmpp.on('buddy', function(jid, state, statusText) { + node.log(jid+" is "+state+" : "+statusText); + var msg = { topic:jid, payload: { presence:state, status:statusText} }; + node.send([null,msg]); + }); + + xmpp.on('error', function(err) { + console.error("error",err); + }); + + xmpp.on('close', function() { + node.log('connection closed'); + node.status({fill:"red",shape:"ring",text:"not connected"}); + }); + + xmpp.on('subscribe', function(from) { + xmpp.acceptSubscription(from); + }); + + // Now actually make the connection + try { + xmpp.connect({ + jid : node.userid, + password : node.password, + host : node.host, + port : node.port, + skipPresence : true, + reconnect : false + }); + } catch(e) { + node.error("Bad xmpp configuration"); + node.status({fill:"red",shape:"ring",text:"not connected"}); + } + + node.on("close", function(done) { + //xmpp.setPresence('offline'); + xmpp.disconnect(); + if (xmpp.conn) { xmpp.conn.end(); } + xmpp = null; + done(); + }); + } RED.nodes.registerType("xmpp in",XmppInNode); function XmppOutNode(n) { - RED.nodes.createNode(this,n); - this.server = n.server; + RED.nodes.createNode(this,n); + this.server = n.server; - this.serverConfig = RED.nodes.getNode(this.server); - this.host = this.serverConfig.server; - this.port = this.serverConfig.port; - this.nick = this.serverConfig.nickname || "Node-RED"; - this.userid = this.serverConfig.username; - this.password = this.serverConfig.password; + this.serverConfig = RED.nodes.getNode(this.server); + this.host = this.serverConfig.server; + this.port = this.serverConfig.port; + this.nick = this.serverConfig.nickname || "Node-RED"; + this.userid = this.serverConfig.username; + this.password = this.serverConfig.password; - this.join = n.join || false; - this.sendAll = n.sendObject; - this.to = n.to || ""; - var node = this; + this.join = n.join || false; + this.sendAll = n.sendObject; + this.to = n.to || ""; + var node = this; - var xmpp = new XMPP.SimpleXMPP(); + var xmpp = new XMPP.SimpleXMPP(); - xmpp.on('online', function() { - node.log('connected to '+node.host+":"+node.port); - node.status({fill:"green",shape:"dot",text:"connected"}); - xmpp.setPresence('online', node.nick+' online'); - if (node.join) { - xmpp.join(node.to+'/'+node.nick); - } - }); - - xmpp.on('error', function(err) { - console.error("error",err); - }); - - xmpp.on('close', function() { - node.log('connection closed'); - node.status({fill:"red",shape:"ring",text:"not connected"}); - }); - - xmpp.on('subscribe', function(from) { - xmpp.acceptSubscription(from); - }); - - // Now actually make the connection - try { - xmpp.connect({ - jid : node.userid, - password : node.password, - host : node.host, - port : node.port, - skipPresence : true, - reconnect : false + xmpp.on('online', function() { + node.log('connected to '+node.host+":"+node.port); + node.status({fill:"green",shape:"dot",text:"connected"}); + xmpp.setPresence('online', node.nick+' online'); + if (node.join) { + xmpp.join(node.to+'/'+node.nick); + } }); - } catch(e) { - node.error("Bad xmpp configuration"); - node.status({fill:"red",shape:"ring",text:"not connected"}); - } - node.on("input", function(msg) { - if (msg.presence) { - if (['away', 'dnd', 'xa','chat'].indexOf(msg.presence) > -1 ) { - xmpp.setPresence(msg.presence, msg.payload); - } - else { node.warn("Can't set presence - invalid value"); } + xmpp.on('error', function(err) { + console.error("error",err); + }); + + xmpp.on('close', function() { + node.log('connection closed'); + node.status({fill:"red",shape:"ring",text:"not connected"}); + }); + + xmpp.on('subscribe', function(from) { + xmpp.acceptSubscription(from); + }); + + // Now actually make the connection + try { + xmpp.connect({ + jid : node.userid, + password : node.password, + host : node.host, + port : node.port, + skipPresence : true, + reconnect : false + }); + } catch(e) { + node.error("Bad xmpp configuration"); + node.status({fill:"red",shape:"ring",text:"not connected"}); } - else { - var to = msg.topic; - if (node.to !== "") { to = node.to; } - if (node.sendAll) { - xmpp.send(to, JSON.stringify(msg), node.join); + + node.on("input", function(msg) { + if (msg.presence) { + if (['away', 'dnd', 'xa','chat'].indexOf(msg.presence) > -1 ) { + xmpp.setPresence(msg.presence, msg.payload); + } + else { node.warn("Can't set presence - invalid value"); } } - else if (msg.payload) { - if (typeof(msg.payload) === "object") { - xmpp.send(to, JSON.stringify(msg.payload), node.join); - } else { - xmpp.send(to, msg.payload.toString(), node.join); + else { + var to = msg.topic; + if (node.to !== "") { to = node.to; } + if (node.sendAll) { + xmpp.send(to, JSON.stringify(msg), node.join); + } + else if (msg.payload) { + if (typeof(msg.payload) === "object") { + xmpp.send(to, JSON.stringify(msg.payload), node.join); + } else { + xmpp.send(to, msg.payload.toString(), node.join); + } } } - } - }); + }); - node.on("close", function() { - xmpp.setPresence('offline'); - if (xmpp.conn) { xmpp.conn.end(); } - xmpp = null; - }); -} + node.on("close", function() { + xmpp.setPresence('offline'); + xmpp.disconnect(); + if (xmpp.conn) { xmpp.conn.end(); } + xmpp = null; + }); + } RED.nodes.registerType("xmpp out",XmppOutNode); } diff --git a/social/xmpp/package.json b/social/xmpp/package.json index 24e815fb..29071bcf 100644 --- a/social/xmpp/package.json +++ b/social/xmpp/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-xmpp", - "version" : "0.0.5", + "version" : "0.0.6", "description" : "A Node-RED node to talk to an XMPP server", "dependencies" : { "simple-xmpp" : "0.1.92"