1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Give XMPP node a serious kicking

try to sort out sharing single connections and better status
To close #438 and to close #492
This commit is contained in:
Dave Conway-Jones 2018-09-28 14:59:22 +01:00
parent 1f34239fbb
commit b856ee6439
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
3 changed files with 99 additions and 86 deletions

View File

@ -21,7 +21,7 @@
<script type="text/x-red" data-help-name="xmpp in">
<p>Connects to an XMPP server to receive messages.</p>
<p>The <b>Buddy</b> field is the id of the buddy or room you want to receive messages from.</p>
<p>The <b>Buddy</b> field is the id of the buddy or room you want to receive messages from. Leave blank to receive from anyone.</p>
<p>Incoming messages will appear as <code>msg.payload</code> on the first output, while <code>msg.topic</code> will contain who it is from.</p>
<p>The second output will show the presence and status of a user in <code>msg.payload</code>. Again <code>msg.topic</code> will hold the user.</p>
</script>
@ -33,7 +33,7 @@
defaults: {
name: {value:""},
server: {type:"xmpp-server",required:true},
to: {value:"",required:true},
to: {value:""},
join: {value:false}
},
inputs:0,
@ -107,20 +107,20 @@
</script>
<script type="text/x-red" data-template-name="xmpp-server">
<div class="form-row node-input-server">
<!-- <div class="form-row node-input-server">
<label for="node-config-input-server"><i class="fa fa-bookmark"></i> Server</label>
<input class="input-append-left" type="text" id="node-config-input-server" placeholder="localhost" style="width: 40%;" >
<label for="node-config-input-port" style="margin-left: 10px; width: 35px; "> Port</label>
<input type="text" id="node-config-input-port" placeholder="Port" style="width:45px">
</div> -->
<div class="form-row">
<label for="node-config-input-user"><i class="fa fa-user"></i> JID</label>
<input type="text" id="node-config-input-user">
</div>
<div class="form-row">
<label for="node-config-input-nickname"><i class="fa fa-user"></i> Nickname</label>
<input type="text" id="node-config-input-nickname" placeholder="Joe">
</div>
<div class="form-row">
<label for="node-config-input-user"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-config-input-user">
</div>
<div class="form-row">
<label for="node-config-input-pass"><i class="fa fa-lock"></i> Password</label>
<input type="password" id="node-config-input-password">
@ -131,16 +131,16 @@
RED.nodes.registerType('xmpp-server',{
category: 'config',
defaults: {
server: {required:true},
port: {value:5222,required:true,validate:RED.validators.number()},
nickname: {}
// server: {required:true},
// port: {value:5222,required:true,validate:RED.validators.number()},
nickname: {value:""},
user: {type:"text"},
},
credentials: {
user: {type:"text"},
password: {type: "password"}
},
label: function() {
return (this.nickname?this.nickname+"@":"")+this.server+":"+this.port;
return this.user;
}
});
</script>

View File

@ -2,17 +2,53 @@
module.exports = function(RED) {
"use strict";
var XMPP = require('simple-xmpp');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
function XMPPServerNode(n) {
RED.nodes.createNode(this,n);
this.server = n.server;
this.port = n.port;
// this.server = n.server;
// this.port = n.port;
this.nickname = n.nickname;
this.username = n.user;
var credentials = this.credentials;
if (credentials) {
this.username = credentials.user;
this.password = credentials.password;
}
this.client = new XMPP.SimpleXMPP();
this.connected = false;
var that = this;
this.client.con = function() {
if (that.connected === false ) {
that.connected = true;
that.client.connect({
jid : that.username,
password : that.password,
// host : node.host,
//port : node.port,
//skipPresence : true,
reconnect : true,
preferred : "PLAIN"
});
}
}
that.client.on('online', function(data) {
that.connected = true;
that.client.setPresence('online', data.jid.user+' is online');
that.log('connected as '+data.jid.user+' to '+data.jid._domain+":5222");
});
that.client.on('close', function() {
that.connected = false;
that.log('connection closed');
});
this.on("close", function(done) {
that.client.setPresence('offline');
that.client.disconnect();
if (that.client.conn) { that.client.conn.end(); }
that.client = null;
done();
});
}
RED.nodes.registerType("xmpp-server",XMPPServerNode,{
@ -27,24 +63,21 @@ module.exports = function(RED) {
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.host = this.serverConfig.server;
// this.port = this.serverConfig.port;
var pa = this.serverConfig.username.split("@");
this.nick = this.serverConfig.nickname || pa[0];
this.join = n.join || false;
this.sendAll = n.sendObject;
this.to = n.to || "";
this.from = n.to || "";
var node = this;
var xmpp = new XMPP.SimpleXMPP();
var xmpp = this.serverConfig.client;
xmpp.on('online', function(data) {
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) {
if ((node.join) && (node.from !== "")) {
xmpp.join(node.to+'/'+node.nick);
}
});
@ -76,17 +109,22 @@ module.exports = function(RED) {
if (err.hasOwnProperty("stanza")) {
if (err.stanza.name === 'stream:error') { node.error("stream:error - bad login id/pwd ?",err); }
else { node.error(err.stanza.name,err); }
node.status({fill:"red",shape:"ring",text:"bad login"});
}
else {
if (err.errno === "ETIMEDOUT") { node.error("Timeout connecting to server",err); }
else { node.error(err.errno,err); }
if (err.errno === "ETIMEDOUT") {
node.error("Timeout connecting to server",err);
node.status({fill:"red",shape:"ring",text:"timeout"});
}
else if (err === "XMPP authentication failure") {
node.error(err,err);
node.status({fill:"red",shape:"ring",text:"XMPP authentication failure"});
}
else {
node.error(err.errno,err);
node.status({fill:"red",shape:"ring",text:"error"});
}
}
node.status({fill:"red",shape:"ring",text:"error"});
});
xmpp.on('close', function() {
node.log('connection closed');
//node.status({fill:"grey",shape:"ring",text:"not connected"});
});
xmpp.on('subscribe', function(from) {
@ -96,55 +134,42 @@ module.exports = function(RED) {
// Now actually make the connection
try {
node.status({fill:"grey",shape:"dot",text:"connecting"});
xmpp.connect({
jid : node.userid,
password : node.password,
host : node.host,
port : node.port,
skipPresence : true,
reconnect : false,
preferred : "PLAIN"
});
xmpp.con();
}
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;
node.on("close", function() {
node.status({});
done();
});
}
RED.nodes.registerType("xmpp in",XmppInNode);
function XmppOutNode(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.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;
var pa = this.userid.split("@");
this.nick = this.serverConfig.nickname || pa[0];
this.join = n.join || false;
this.sendAll = n.sendObject;
this.to = n.to || "";
var node = this;
var xmpp = new XMPP.SimpleXMPP();
var xmpp = this.serverConfig.client;
xmpp.on('online', function(data) {
node.status({fill:"green",shape:"dot",text:"connected"});
node.log('connected to '+node.host+":"+node.port);
xmpp.setPresence('online', node.nick+' online');
if (node.join) {
if ((node.join) && (node.from !== "")) {
xmpp.join(node.to+'/'+node.nick);
}
});
@ -154,35 +179,28 @@ module.exports = function(RED) {
if (err.hasOwnProperty("stanza")) {
if (err.stanza.name === 'stream:error') { node.error("stream:error - bad login id/pwd ?",err); }
else { node.error(err.stanza.name,err); }
node.status({fill:"red",shape:"ring",text:"bad login"});
}
else {
if (err.errno === "ETIMEDOUT") { node.error("Timeout connecting to server",err); }
else { node.error(err.errno,err); }
if (err.errno === "ETIMEDOUT") {
node.error("Timeout connecting to server",err);
node.status({fill:"red",shape:"ring",text:"timeout"});
}
else if (err === "XMPP authentication failure") {
node.error(err,err);
node.status({fill:"red",shape:"ring",text:"XMPP authentication failure"});
}
else {
node.error(err.errno,err);
node.status({fill:"red",shape:"ring",text:"error"});
}
}
node.status({fill:"red",shape:"ring",text:"error"});
});
xmpp.on('close', function() {
node.log('connection closed');
//node.status({fill:"grey",shape:"ring",text:"not connected"});
});
xmpp.on('subscribe', function(from) {
xmpp.acceptSubscription(from);
});
// Now actually make the connection
try {
node.status({fill:"grey",shape:"dot",text:"connecting"});
xmpp.connect({
jid : node.userid,
password : node.password,
host : node.host,
port : node.port,
skipPresence : true,
reconnect : false,
preferred : "PLAIN"
});
xmpp.con();
}
catch(e) {
node.error("Bad xmpp configuration");
@ -191,7 +209,7 @@ module.exports = function(RED) {
node.on("input", function(msg) {
if (msg.presence) {
if (['away', 'dnd', 'xa','chat'].indexOf(msg.presence) > -1 ) {
if (['away', 'dnd', 'xa', 'chat'].indexOf(msg.presence) > -1 ) {
xmpp.setPresence(msg.presence, msg.payload);
}
else { node.warn("Can't set presence - invalid value"); }
@ -213,13 +231,8 @@ module.exports = function(RED) {
}
});
node.on("close", function(done) {
xmpp.setPresence('offline');
xmpp.disconnect();
if (xmpp.conn) { xmpp.conn.end(); }
xmpp = null;
node.on("close", function() {
node.status({});
done();
});
}
RED.nodes.registerType("xmpp out",XmppOutNode);

View File

@ -1,9 +1,9 @@
{
"name" : "node-red-node-xmpp",
"version" : "0.1.7",
"version" : "0.2.0",
"description" : "A Node-RED node to talk to an XMPP server",
"dependencies" : {
"simple-xmpp" : "1.3.*"
"simple-xmpp" : "^1.3.0"
},
"repository" : {
"type":"git",