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

Added INFO abour settings for xmpp. (Apologies for missing in first place...).

NOTE:... there is still a bug in the underlying simple-xmpp npm.. see
https://github.com/simple-xmpp/node-simple-xmpp/issues/12
for both the probem and the fix (not yet pulled).
This commit is contained in:
Dave C-J 2013-11-15 21:28:18 +00:00
parent 336ad7893d
commit ea398f9e66
2 changed files with 90 additions and 80 deletions

View File

@ -47,11 +47,17 @@
</script> </script>
<script type="text/x-red" data-help-name="xmpp"> <script type="text/x-red" data-help-name="xmpp">
<p>Connects to an XMPP server to send and receive messages.</p> <p>Connects to an XMPP server to send and receive messages.</p>
<p>Incoming messages will appear as <b>msg.payload</b> on the first output, while <b>msg.topic</b> will contain who it is from.</p> <p>Incoming messages will appear as <b>msg.payload</b> on the first output, while <b>msg.topic</b> will contain who it is from.</p>
<p>The second output will user presence and status in <b>msg.payload</b>.</p> <p>The second output will user presence and status in <b>msg.payload</b>.</p>
<p>The <b>To</b> field is optional. If not set uses the <b>msg.topic</b> property of the message.</p> <p>The <b>To</b> field is optional. If not set uses the <b>msg.topic</b> property of the message.</p>
<p>If you are joining a room then the <b>To</b> field must be filled in.</p> <p>If you are joining a room then the <b>To</b> field must be filled in.</p>
<p>Uses the simple-xmpp module - you may also need to pre-configure your xmpp settings as per below.</p>
<p>Either add to your settings.js file...</p>
<p><pre>xmpp : { jid : "yourid", password: "password" },</pre></p>
<p>Or create a file xmppkeys.js containing</p>
<p><pre>module.exports = { jid: "yourid", password: "password" }</pre></p>
<p>This <b>must</b> be located in the directory above node-red.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -16,10 +16,10 @@
var orig=console.warn; var orig=console.warn;
console.warn=(function() { // suppress warning from stringprep when not needed) console.warn=(function() { // suppress warning from stringprep when not needed)
var orig=console.warn; var orig=console.warn;
return function() { return function() {
//orig.apply(console, arguments); //orig.apply(console, arguments);
}; };
})(); })();
var RED = require(process.env.NODE_RED_HOME+"/red/red"); var RED = require(process.env.NODE_RED_HOME+"/red/red");
@ -27,92 +27,96 @@ var xmpp = require('simple-xmpp');
console.warn = orig; console.warn = orig;
try { try {
var xmppkey = require(process.env.NODE_RED_HOME+"/settings").xmpp || require(process.env.NODE_RED_HOME+"/../xmppkeys.js"); var xmppkey = require(process.env.NODE_RED_HOME+"/settings").xmpp || require(process.env.NODE_RED_HOME+"/../xmppkeys.js");
} catch(err) { } catch(err) {
throw new Error("Failed to load XMPP credentials"); throw new Error("Failed to load XMPP credentials");
} }
function XmppNode(n) { function XmppNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.server = n.server; this.server = n.server;
this.port = n.port; this.port = n.port;
this.join = n.join || false; this.join = n.join || false;
this.nick = n.nick || "Node-RED"; this.nick = n.nick || "Node-RED";
this.sendAll = n.sendObject; this.sendAll = n.sendObject;
this.to = n.to || ""; this.to = n.to || "";
var node = this; var node = this;
setTimeout(function() { setTimeout(function() {
xmpp.connect({ xmpp.connect({
jid : xmppkey.jid, jid : xmppkey.jid,
password : xmppkey.password, password : xmppkey.password,
host : this.server, host : this.server,
port : this.port, port : this.port,
skipPresence : true, skipPresence : true,
reconnect : false reconnect : false
}); });
}, 5000); }, 5000);
xmpp.on('online', function() { xmpp.on('online', function() {
node.log('connected to '+node.server); node.log('connected to '+node.server);
xmpp.setPresence('online', node.nick+' online'); xmpp.setPresence('online', node.nick+' online');
if (node.join) { if (node.join) {
xmpp.join(node.to+'/'+node.nick); xmpp.join(node.to+'/'+node.nick);
} }
}); });
xmpp.on('chat', function(from, message) { xmpp.on('chat', function(from, message) {
var msg = { topic:from, payload:message }; var msg = { topic:from, payload:message };
node.send([msg,null]); node.send([msg,null]);
}); });
xmpp.on('groupchat', function(conference, from, message, stamp) { xmpp.on('groupchat', function(conference, from, message, stamp) {
var msg = { topic:from, payload:message, room:conference }; var msg = { topic:from, payload:message, room:conference };
if (from != node.nick) { node.send([msg,null]); } if (from != node.nick) { node.send([msg,null]); }
}); });
//xmpp.on('chatstate', function(from, state) { //xmpp.on('chatstate', function(from, state) {
//console.log('%s is currently %s', from, state); //console.log('%s is currently %s', from, state);
//var msg = { topic:from, payload:state }; //var msg = { topic:from, payload:state };
//node.send([null,msg]); //node.send([null,msg]);
//}); //});
xmpp.on('buddy', function(jid, state, statusText) { xmpp.on('buddy', function(jid, state, statusText) {
node.log(jid+" is "+state+" : "+statusText); node.log(jid+" is "+state+" : "+statusText);
var msg = { topic:jid, payload: { presence:state, status:statusText} }; var msg = { topic:jid, payload: { presence:state, status:statusText} };
node.send([null,msg]); node.send([null,msg]);
}); });
xmpp.on('error', function(err) { xmpp.on('error', function(err) {
console.error(err); console.error(err);
}); });
xmpp.on('close', function(err) { xmpp.on('close', function(err) {
node.log('connection closed'); node.log('connection closed');
}); });
xmpp.on('subscribe', function(from) { xmpp.on('subscribe', function(from) {
xmpp.acceptSubscription(from); xmpp.acceptSubscription(from);
}); });
this.on("input", function(msg) { this.on("input", function(msg) {
var to = msg.topic; var to = msg.topic;
if (node.to != "") { to = node.to; } if (node.to != "") { to = node.to; }
if (node.sendAll) { if (node.sendAll) {
xmpp.send(to, JSON.stringify(msg), node.join); xmpp.send(to, JSON.stringify(msg), node.join);
} }
else { else {
xmpp.send(to, msg.payload, node.join); xmpp.send(to, msg.payload, node.join);
} }
}); });
this.on("close", function() { this.on("close", function() {
xmpp.setPresence('offline'); xmpp.setPresence('offline');
//xmpp.conn.end(); try {
// TODO - DCJ NOTE... this is not good. It leaves the connection up over a restart - which will end up with bad things happening... xmpp.disconnect();
// (but requires the underlying xmpp lib to be fixed (which does have an open bug request on fixing the close method)). // TODO - DCJ NOTE... this is not good. It leaves the connection up over a restart - which will end up with bad things happening...
this.warn("Due to an underlying bug in the xmpp library this does not disconnect old sessions. This is bad... A restart would be better."); // (but requires the underlying xmpp lib to be fixed, which does have an open bug request on fixing the close method - and a work around.
}); // see - https://github.com/simple-xmpp/node-simple-xmpp/issues/12 for the fix
} catch(e) {
this.warn("Due to an underlying bug in the xmpp library this does not disconnect old sessions. This is bad... A restart would be better.");
}
});
} }
RED.nodes.registerType("xmpp",XmppNode); RED.nodes.registerType("xmpp",XmppNode);