2013-10-30 21:38:23 +01:00
|
|
|
|
2014-05-29 19:31:36 +02:00
|
|
|
module.exports = function(RED) {
|
2015-05-11 20:25:39 +02:00
|
|
|
"use strict";
|
|
|
|
var XMPP = require('simple-xmpp');
|
2018-09-28 15:59:22 +02:00
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
|
2014-05-29 19:31:36 +02:00
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
function XMPPServerNode(n) {
|
2015-09-24 19:58:24 +02:00
|
|
|
RED.nodes.createNode(this,n);
|
2018-09-28 15:59:22 +02:00
|
|
|
// this.server = n.server;
|
|
|
|
// this.port = n.port;
|
2015-09-24 19:58:24 +02:00
|
|
|
this.nickname = n.nickname;
|
2018-09-28 15:59:22 +02:00
|
|
|
this.username = n.user;
|
2015-09-24 19:58:24 +02:00
|
|
|
var credentials = this.credentials;
|
|
|
|
if (credentials) {
|
|
|
|
this.password = credentials.password;
|
|
|
|
}
|
2018-09-28 15:59:22 +02:00
|
|
|
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();
|
|
|
|
});
|
2014-05-29 19:31:36 +02:00
|
|
|
}
|
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
RED.nodes.registerType("xmpp-server",XMPPServerNode,{
|
|
|
|
credentials: {
|
|
|
|
password: {type: "password"}
|
2013-11-15 22:28:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
function XmppInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = n.server;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.server);
|
2018-09-29 16:17:18 +02:00
|
|
|
this.nick = this.serverConfig.nickname || this.serverConfig.username.split("@")[0];
|
2015-09-24 19:58:24 +02:00
|
|
|
this.join = n.join || false;
|
|
|
|
this.sendAll = n.sendObject;
|
2018-09-28 15:59:22 +02:00
|
|
|
this.from = n.to || "";
|
2015-09-24 19:58:24 +02:00
|
|
|
var node = this;
|
|
|
|
|
2018-09-28 15:59:22 +02:00
|
|
|
var xmpp = this.serverConfig.client;
|
2015-09-24 19:58:24 +02:00
|
|
|
|
2016-01-29 18:58:01 +01:00
|
|
|
xmpp.on('online', function(data) {
|
2015-09-24 19:58:24 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2018-09-28 15:59:22 +02:00
|
|
|
if ((node.join) && (node.from !== "")) {
|
2018-09-28 22:42:36 +02:00
|
|
|
// disable chat history
|
|
|
|
var to = node.to+'/'+node.nick;
|
|
|
|
var stanza = new xmpp.Element('presence', {"to": to}).
|
|
|
|
c('x', { xmlns: 'http://jabber.org/protocol/muc' }).
|
|
|
|
c('history', { maxstanzas:0, seconds:1 });
|
|
|
|
xmpp.conn.send(stanza);
|
|
|
|
xmpp.join(to);
|
2015-09-24 19:58:24 +02:00
|
|
|
}
|
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2018-09-29 16:17:18 +02:00
|
|
|
// xmpp.on('chat', function(from, message) {
|
|
|
|
// var msg = { topic:from, payload:message };
|
|
|
|
// if (!node.join && ((node.from === "") || (node.from === from))) {
|
|
|
|
// node.send([msg,null]);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
|
|
|
xmpp.on('stanza', function(stanza) {
|
|
|
|
if (stanza.is('message')) {
|
|
|
|
if (stanza.attrs.type == 'chat') {
|
|
|
|
//console.log(stanza);
|
|
|
|
var body = stanza.getChild('body');
|
|
|
|
if (body) {
|
|
|
|
var msg = { payload:body.getText() };
|
|
|
|
var ids = stanza.attrs.from.split('/');
|
|
|
|
if (ids[1].length !== 36) {
|
|
|
|
msg.topic = stanza.attrs.from
|
|
|
|
}
|
|
|
|
else { msg.topic = ids[0]; }
|
|
|
|
if (!node.join && ((node.from === "") || (node.from === from))) {
|
|
|
|
node.send([msg,null]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 22:42:36 +02:00
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.on('groupchat', function(conference, from, message, stamp) {
|
2018-09-29 16:17:18 +02:00
|
|
|
var msg = { topic:from, payload:message, room:conference };
|
2018-09-28 22:42:36 +02:00
|
|
|
if (from != node.nick) {
|
|
|
|
if ((node.join) && (node.from === conference)) {
|
|
|
|
node.send([msg,null]);
|
|
|
|
}
|
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
//xmpp.on('chatstate', function(from, state) {
|
|
|
|
//console.log('%s is currently %s', from, state);
|
2018-09-29 16:17:18 +02:00
|
|
|
//var msg = { topic:from, payload: {presence:state} };
|
2015-09-24 19:58:24 +02:00
|
|
|
//node.send([null,msg]);
|
|
|
|
//});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
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]);
|
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2018-09-29 16:17:18 +02:00
|
|
|
// xmpp.on('groupbuddy', function(conference, from, state, statusText) {
|
|
|
|
// //console.log('%s: %s is in %s state - %s',conference, from, state, statusText);
|
|
|
|
// var msg = { topic:from, payload: { presence:state, status:statusText}, room:conference };
|
|
|
|
// });
|
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.on('error', function(err) {
|
2016-01-29 18:58:01 +01:00
|
|
|
if (RED.settings.verbose) { node.log(err); }
|
|
|
|
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); }
|
2018-09-28 15:59:22 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"bad login"});
|
2016-01-29 18:58:01 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-09-28 15:59:22 +02:00
|
|
|
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"});
|
|
|
|
}
|
2016-01-29 18:58:01 +01:00
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.on('subscribe', function(from) {
|
|
|
|
xmpp.acceptSubscription(from);
|
2014-05-29 19:31:36 +02:00
|
|
|
});
|
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
// Now actually make the connection
|
|
|
|
try {
|
2016-01-29 18:58:01 +01:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"connecting"});
|
2018-09-28 15:59:22 +02:00
|
|
|
xmpp.con();
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2015-09-24 19:58:24 +02:00
|
|
|
node.error("Bad xmpp configuration");
|
|
|
|
node.status({fill:"red",shape:"ring",text:"not connected"});
|
|
|
|
}
|
|
|
|
|
2018-09-28 15:59:22 +02:00
|
|
|
node.on("close", function() {
|
2016-01-29 18:58:01 +01:00
|
|
|
node.status({});
|
2015-09-24 19:58:24 +02:00
|
|
|
});
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
RED.nodes.registerType("xmpp in",XmppInNode);
|
2014-05-29 19:31:36 +02:00
|
|
|
|
2018-09-28 15:59:22 +02:00
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
function XmppOutNode(n) {
|
2015-09-24 19:58:24 +02:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = n.server;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.server);
|
2018-09-29 16:17:18 +02:00
|
|
|
this.nick = this.serverConfig.nickname || this.serverConfig.username.split("@")[0];
|
2015-09-24 19:58:24 +02:00
|
|
|
this.join = n.join || false;
|
|
|
|
this.sendAll = n.sendObject;
|
|
|
|
this.to = n.to || "";
|
|
|
|
var node = this;
|
|
|
|
|
2018-09-28 15:59:22 +02:00
|
|
|
var xmpp = this.serverConfig.client;
|
2015-09-24 19:58:24 +02:00
|
|
|
|
2016-01-29 18:58:01 +01:00
|
|
|
xmpp.on('online', function(data) {
|
2015-09-24 19:58:24 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2018-09-28 15:59:22 +02:00
|
|
|
if ((node.join) && (node.from !== "")) {
|
2018-09-28 22:42:36 +02:00
|
|
|
// disable chat history
|
|
|
|
var to = node.to+'/'+node.nick;
|
|
|
|
var stanza = new xmpp.Element('presence', {"to": to}).
|
|
|
|
c('x', { xmlns: 'http://jabber.org/protocol/muc' }).
|
|
|
|
c('history', { maxstanzas:0, seconds:1 });
|
|
|
|
xmpp.conn.send(stanza);
|
|
|
|
xmpp.join(to);
|
2015-09-24 19:58:24 +02:00
|
|
|
}
|
|
|
|
});
|
2014-05-29 19:31:36 +02:00
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.on('error', function(err) {
|
2016-01-29 19:06:42 +01:00
|
|
|
if (RED.settings.verbose) { node.log(err); }
|
|
|
|
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); }
|
2018-09-28 15:59:22 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"bad login"});
|
2016-01-29 19:06:42 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-09-28 15:59:22 +02:00
|
|
|
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"});
|
|
|
|
}
|
2016-01-29 19:06:42 +01:00
|
|
|
}
|
2014-05-29 19:31:36 +02:00
|
|
|
});
|
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
// Now actually make the connection
|
|
|
|
try {
|
2016-01-29 18:58:01 +01:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"connecting"});
|
2018-09-28 15:59:22 +02:00
|
|
|
xmpp.con();
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2015-09-24 19:58:24 +02:00
|
|
|
node.error("Bad xmpp configuration");
|
|
|
|
node.status({fill:"red",shape:"ring",text:"not connected"});
|
2013-11-15 22:28:18 +01:00
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (msg.presence) {
|
2018-09-28 15:59:22 +02:00
|
|
|
if (['away', 'dnd', 'xa', 'chat'].indexOf(msg.presence) > -1 ) {
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.setPresence(msg.presence, msg.payload);
|
|
|
|
}
|
2018-09-29 16:17:18 +02:00
|
|
|
else { node.warn("Can't set presence - invalid value: "+msg.presence); }
|
2014-05-29 19:31:36 +02:00
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
else {
|
2018-09-29 16:17:18 +02:00
|
|
|
var to = node.to || msg.topic || "";
|
|
|
|
if (to !== "") {
|
|
|
|
if (node.sendAll) {
|
|
|
|
xmpp.send(to, JSON.stringify(msg), node.join);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
2018-09-29 16:17:18 +02:00
|
|
|
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);
|
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
}
|
2014-05-29 19:31:36 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2018-09-28 15:59:22 +02:00
|
|
|
node.on("close", function() {
|
2016-01-29 18:58:01 +01:00
|
|
|
node.status({});
|
2015-09-24 19:58:24 +02:00
|
|
|
});
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
RED.nodes.registerType("xmpp out",XmppOutNode);
|
2014-05-29 19:31:36 +02:00
|
|
|
}
|