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

Tidy up xmpp node formatting

(diff looks awful… it isn’t really)
This commit is contained in:
Dave Conway-Jones 2015-09-24 18:58:24 +01:00
parent 4e63566dbf
commit c5db30d0c4
2 changed files with 166 additions and 164 deletions

View File

@ -19,194 +19,196 @@ module.exports = function(RED) {
var XMPP = require('simple-xmpp'); var XMPP = require('simple-xmpp');
function XMPPServerNode(n) { function XMPPServerNode(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.nickname = n.nickname; this.nickname = n.nickname;
var credentials = this.credentials; var credentials = this.credentials;
if (credentials) { if (credentials) {
this.username = credentials.user; this.username = credentials.user;
this.password = credentials.password; this.password = credentials.password;
}
} }
}
RED.nodes.registerType("xmpp-server",XMPPServerNode,{ RED.nodes.registerType("xmpp-server",XMPPServerNode,{
credentials: { credentials: {
user: {type:"text"}, user: {type:"text"},
password: {type: "password"} 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);
} }
}); });
xmpp.on('chat', function(from, message) { function XmppInNode(n) {
var msg = { topic:from, payload:message }; RED.nodes.createNode(this,n);
node.send([msg,null]); this.server = n.server;
});
xmpp.on('groupchat', function(conference, from, message, stamp) { this.serverConfig = RED.nodes.getNode(this.server);
var msg = { topic:from, payload:message, room:conference, ts:stamp }; this.host = this.serverConfig.server;
if (from != node.nick) { node.send([msg,null]); } 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) { this.join = n.join || false;
//console.log('%s is currently %s', from, state); this.sendAll = n.sendObject;
//var msg = { topic:from, payload:state }; this.to = n.to || "";
//node.send([null,msg]); var node = this;
//});
xmpp.on('buddy', function(jid, state, statusText) { var xmpp = new XMPP.SimpleXMPP();
node.log(jid+" is "+state+" : "+statusText);
var msg = { topic:jid, payload: { presence:state, status:statusText} };
node.send([null,msg]);
});
xmpp.on('error', function(err) { xmpp.on('online', function() {
console.error("error",err); node.log('connected to '+node.host+":"+node.port);
}); node.status({fill:"green",shape:"dot",text:"connected"});
//xmpp.setPresence('online', node.nick+' online');
xmpp.on('close', function() { if (node.join) {
node.log('connection closed'); xmpp.join(node.to+'/'+node.nick);
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.on('chat', function(from, message) {
//xmpp.setPresence('offline'); var msg = { topic:from, payload:message };
if (xmpp.conn) { xmpp.conn.end(); } node.send([msg,null]);
xmpp = null; });
done();
}); 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); RED.nodes.registerType("xmpp in",XmppInNode);
function XmppOutNode(n) { function XmppOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.server = n.server; this.server = n.server;
this.serverConfig = RED.nodes.getNode(this.server); this.serverConfig = RED.nodes.getNode(this.server);
this.host = this.serverConfig.server; this.host = this.serverConfig.server;
this.port = this.serverConfig.port; this.port = this.serverConfig.port;
this.nick = this.serverConfig.nickname || "Node-RED"; this.nick = this.serverConfig.nickname || "Node-RED";
this.userid = this.serverConfig.username; this.userid = this.serverConfig.username;
this.password = this.serverConfig.password; this.password = this.serverConfig.password;
this.join = n.join || false; this.join = n.join || false;
this.sendAll = n.sendObject; this.sendAll = n.sendObject;
this.to = n.to || ""; this.to = n.to || "";
var node = this; var node = this;
var xmpp = new XMPP.SimpleXMPP(); var xmpp = new XMPP.SimpleXMPP();
xmpp.on('online', function() { xmpp.on('online', function() {
node.log('connected to '+node.host+":"+node.port); node.log('connected to '+node.host+":"+node.port);
node.status({fill:"green",shape:"dot",text:"connected"}); node.status({fill:"green",shape:"dot",text:"connected"});
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('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("input", function(msg) { xmpp.on('error', function(err) {
if (msg.presence) { console.error("error",err);
if (['away', 'dnd', 'xa','chat'].indexOf(msg.presence) > -1 ) { });
xmpp.setPresence(msg.presence, msg.payload);
} xmpp.on('close', function() {
else { node.warn("Can't set presence - invalid value"); } 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; node.on("input", function(msg) {
if (node.to !== "") { to = node.to; } if (msg.presence) {
if (node.sendAll) { if (['away', 'dnd', 'xa','chat'].indexOf(msg.presence) > -1 ) {
xmpp.send(to, JSON.stringify(msg), node.join); xmpp.setPresence(msg.presence, msg.payload);
}
else { node.warn("Can't set presence - invalid value"); }
} }
else if (msg.payload) { else {
if (typeof(msg.payload) === "object") { var to = msg.topic;
xmpp.send(to, JSON.stringify(msg.payload), node.join); if (node.to !== "") { to = node.to; }
} else { if (node.sendAll) {
xmpp.send(to, msg.payload.toString(), node.join); 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() { node.on("close", function() {
xmpp.setPresence('offline'); xmpp.setPresence('offline');
if (xmpp.conn) { xmpp.conn.end(); } xmpp.disconnect();
xmpp = null; if (xmpp.conn) { xmpp.conn.end(); }
}); xmpp = null;
} });
}
RED.nodes.registerType("xmpp out",XmppOutNode); RED.nodes.registerType("xmpp out",XmppOutNode);
} }

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-xmpp", "name" : "node-red-node-xmpp",
"version" : "0.0.5", "version" : "0.0.6",
"description" : "A Node-RED node to talk to an XMPP server", "description" : "A Node-RED node to talk to an XMPP server",
"dependencies" : { "dependencies" : {
"simple-xmpp" : "0.1.92" "simple-xmpp" : "0.1.92"