2013-10-30 21:38:23 +01:00
|
|
|
/**
|
2015-02-06 22:10:14 +01:00
|
|
|
* Copyright 2013,2015 IBM Corp.
|
2013-10-30 21:38:23 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
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');
|
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);
|
|
|
|
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;
|
|
|
|
}
|
2014-05-29 19:31:36 +02:00
|
|
|
}
|
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
RED.nodes.registerType("xmpp-server",XMPPServerNode,{
|
|
|
|
credentials: {
|
|
|
|
user: {type:"text"},
|
|
|
|
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);
|
|
|
|
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();
|
|
|
|
|
2016-01-29 18:58:01 +01:00
|
|
|
xmpp.on('online', function(data) {
|
2015-09-24 19:58:24 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.on('chat', function(from, message) {
|
|
|
|
var msg = { topic:from, payload:message };
|
|
|
|
node.send([msg,null]);
|
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2015-09-24 19:58:24 +02:00
|
|
|
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]); }
|
|
|
|
});
|
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);
|
|
|
|
//var msg = { topic:from, payload:state };
|
|
|
|
//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
|
|
|
|
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); }
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (err.errno === "ETIMEDOUT") { node.error("Timeout connecting to server",err); }
|
|
|
|
else { node.error(err.errno,err); }
|
|
|
|
}
|
|
|
|
node.status({fill:"red",shape:"ring",text:"error"});
|
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('close', function() {
|
|
|
|
node.log('connection closed');
|
2016-01-29 18:58:01 +01:00
|
|
|
//node.status({fill:"grey",shape:"ring",text:"not connected"});
|
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"});
|
2015-09-24 19:58:24 +02:00
|
|
|
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) {
|
2016-02-01 00:28:05 +01:00
|
|
|
xmpp.setPresence('offline');
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.disconnect();
|
|
|
|
if (xmpp.conn) { xmpp.conn.end(); }
|
|
|
|
xmpp = null;
|
2016-01-29 18:58:01 +01:00
|
|
|
node.status({});
|
2015-09-24 19:58:24 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
RED.nodes.registerType("xmpp in",XmppInNode);
|
2014-05-29 19:31:36 +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);
|
|
|
|
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();
|
|
|
|
|
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"});
|
2016-01-29 18:58:01 +01:00
|
|
|
node.log('connected to '+node.host+":"+node.port);
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.setPresence('online', node.nick+' online');
|
|
|
|
if (node.join) {
|
|
|
|
xmpp.join(node.to+'/'+node.nick);
|
|
|
|
}
|
|
|
|
});
|
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); }
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (err.errno === "ETIMEDOUT") { node.error("Timeout connecting to server",err); }
|
|
|
|
else { node.error(err.errno,err); }
|
|
|
|
}
|
2016-01-29 18:58:01 +01:00
|
|
|
node.status({fill:"red",shape:"ring",text:"error"});
|
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('close', function() {
|
|
|
|
node.log('connection closed');
|
2016-01-29 18:58:01 +01:00
|
|
|
//node.status({fill:"grey",shape:"ring",text:"not connected"});
|
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('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"});
|
2015-09-24 19:58:24 +02:00
|
|
|
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"});
|
2013-11-15 22:28:18 +01:00
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
|
|
|
|
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"); }
|
2014-05-29 19:31:36 +02:00
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
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);
|
|
|
|
}
|
2014-05-29 19:31:36 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 19:58:24 +02:00
|
|
|
});
|
2013-11-15 22:28:18 +01:00
|
|
|
|
2016-01-29 18:58:01 +01:00
|
|
|
node.on("close", function(done) {
|
2015-09-24 19:58:24 +02:00
|
|
|
xmpp.setPresence('offline');
|
|
|
|
xmpp.disconnect();
|
|
|
|
if (xmpp.conn) { xmpp.conn.end(); }
|
|
|
|
xmpp = null;
|
2016-01-29 18:58:01 +01:00
|
|
|
node.status({});
|
|
|
|
done();
|
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
|
|
|
}
|