2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2014-10-18 21:36:49 +02:00
|
|
|
* Copyright 2013,2014 IBM Corp.
|
2013-09-05 16:02:48 +02: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-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-05-29 23:13:21 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var irc = require("irc");
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// The Server Definition - this opens (and closes) the connection
|
|
|
|
function IRCServerNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = n.server;
|
2015-04-09 10:55:34 +02:00
|
|
|
this.port = n.port || 6667;
|
|
|
|
this.ssl = n.ssl || false;
|
|
|
|
this.cert = n.cert || false;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.channel = n.channel;
|
|
|
|
this.nickname = n.nickname;
|
2014-10-03 22:14:17 +02:00
|
|
|
this.lastseen = 0;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.ircclient = null;
|
|
|
|
this.on("close", function() {
|
|
|
|
if (this.ircclient != null) {
|
2014-10-05 22:36:42 +02:00
|
|
|
this.ircclient.removeAllListeners();
|
2014-05-04 00:32:04 +02:00
|
|
|
this.ircclient.disconnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("irc-server",IRCServerNode);
|
2014-05-29 23:13:21 +02:00
|
|
|
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// The Input Node
|
|
|
|
function IrcInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.ircserver = n.ircserver;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.ircserver);
|
|
|
|
this.channel = n.channel || this.serverConfig.channel;
|
2014-08-29 17:41:15 +02:00
|
|
|
var node = this;
|
2014-10-05 22:36:42 +02:00
|
|
|
if (node.serverConfig.ircclient === null) {
|
2014-12-19 00:22:36 +01:00
|
|
|
node.log("CONNECT: "+node.serverConfig.server);
|
2014-08-30 11:41:12 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"connecting"});
|
2015-04-09 10:55:34 +02:00
|
|
|
var options = {autoConnect:true,autoRejoin:false,floodProtection:true,secure:node.serverConfig.ssl,selfSigned:node.serverConfig.cert,port:node.serverConfig.port,retryDelay:20000};
|
|
|
|
node.serverConfig.ircclient = new irc.Client(node.serverConfig.server, node.serverConfig.nickname, options);
|
2014-10-05 22:36:42 +02:00
|
|
|
node.serverConfig.ircclient.setMaxListeners(0);
|
2014-08-29 17:41:15 +02:00
|
|
|
node.serverConfig.ircclient.addListener('error', function(message) {
|
2014-12-19 00:22:36 +01:00
|
|
|
if (RED.settings.verbose) { node.log("ERR: "+JSON.stringify(message)); }
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-10-03 22:14:17 +02:00
|
|
|
node.serverConfig.ircclient.addListener('netError', function(message) {
|
2014-10-03 23:27:58 +02:00
|
|
|
node.serverConfig.lastseen = Date.now();
|
2014-12-19 00:22:36 +01:00
|
|
|
if (RED.settings.verbose) { node.log("NET: "+JSON.stringify(message)); }
|
|
|
|
node.status({fill:"red",shape:"ring",text:"net error"});
|
2014-10-03 22:14:17 +02:00
|
|
|
});
|
|
|
|
node.serverConfig.ircclient.addListener('connect', function() {
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
2014-12-19 00:22:36 +01:00
|
|
|
if (RED.settings.verbose) { node.log("CONNECTED "); }
|
|
|
|
});
|
|
|
|
node.serverConfig.ircclient.addListener('registered', function(message) {
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
|
|
|
node.log(node.serverConfig.ircclient.nick+" ONLINE: "+message.server);
|
|
|
|
node.status({fill:"yellow",shape:"dot",text:"connected"});
|
|
|
|
node.serverConfig.ircclient.join( node.channel, function(data) {
|
|
|
|
node.log(data+" JOINED: "+node.channel);
|
|
|
|
node.status({fill:"green",shape:"dot",text:"joined"});
|
|
|
|
});
|
2014-10-03 22:14:17 +02:00
|
|
|
});
|
|
|
|
node.serverConfig.ircclient.addListener('ping', function(server) {
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
2014-12-19 00:22:36 +01:00
|
|
|
if (RED.settings.verbose) { node.log("PING from "+JSON.stringify(server)); }
|
|
|
|
node.status({fill:"green",shape:"dot",text:"ok"});
|
|
|
|
});
|
|
|
|
node.serverConfig.ircclient.addListener('quit', function(nick, reason, channels, message) {
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
|
|
|
if (RED.settings.verbose) { node.log("QUIT: "+nick+" "+reason+" "+channels+" "+JSON.stringify(message)); }
|
|
|
|
node.status({fill:"grey",shape:"ring",text:"quit"});
|
|
|
|
//node.serverConfig.ircclient.disconnect( function() {
|
|
|
|
// node.serverConfig.ircclient.connect();
|
|
|
|
//});
|
|
|
|
//if (RED.settings.verbose) { node.log("restart"); } // then retry
|
|
|
|
});
|
|
|
|
node.serverConfig.ircclient.addListener('raw', function (message) { // any message received means we are alive
|
|
|
|
//console.log("RAW:"+JSON.stringify(message));
|
|
|
|
if (message.commandType === "reply") {
|
|
|
|
//console.log("RAW:"+JSON.stringify(message));
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
|
|
|
}
|
2014-10-03 22:14:17 +02:00
|
|
|
});
|
|
|
|
node.recon = setInterval( function() {
|
|
|
|
//console.log("CHK ",(Date.now()-node.serverConfig.lastseen)/1000);
|
2014-12-19 00:22:36 +01:00
|
|
|
if ((Date.now()-node.serverConfig.lastseen) > 240000) { // if more than 4 mins since last seen
|
2014-10-09 21:50:02 +02:00
|
|
|
node.serverConfig.ircclient.send.apply(node.serverConfig.ircclient,["TIME"]); // request time to check link
|
2014-10-03 22:14:17 +02:00
|
|
|
}
|
2014-12-19 00:22:36 +01:00
|
|
|
if ((Date.now()-node.serverConfig.lastseen) > 300000) { // If more than 5 mins
|
|
|
|
//node.serverConfig.ircclient.disconnect();
|
|
|
|
//node.serverConfig.ircclient.connect();
|
|
|
|
node.status({fill:"grey",shape:"ring",text:"no connection"});
|
|
|
|
if (RED.settings.verbose) { node.log("CONNECTION LOST ?"); }
|
2014-10-03 22:14:17 +02:00
|
|
|
}
|
2014-12-19 00:22:36 +01:00
|
|
|
//node.serverConfig.ircclient.send.apply(node.serverConfig.ircclient,["TIME"]); // request time to check link
|
2014-10-03 22:14:17 +02:00
|
|
|
}, 60000); // check every 1 min
|
2014-12-19 00:22:36 +01:00
|
|
|
//node.serverConfig.ircclient.connect();
|
2013-11-02 13:12:47 +01:00
|
|
|
}
|
2014-08-30 14:24:09 +02:00
|
|
|
else { node.status({text:""}); }
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient = node.serverConfig.ircclient;
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.addListener('registered', function(message) {
|
2014-10-06 11:20:40 +02:00
|
|
|
//node.log(node.ircclient.nick+" ONLINE");
|
2014-08-30 11:41:12 +02:00
|
|
|
node.status({fill:"yellow",shape:"dot",text:"connected"});
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.join( node.channel, function(data) {
|
|
|
|
// node.log(data+" JOINED "+node.channel);
|
2014-08-30 11:41:12 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"joined"});
|
2014-08-29 17:41:15 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
node.ircclient.addListener('message', function (from, to, message) {
|
|
|
|
//node.log(from + ' => ' + to + ' : ' + message);
|
2014-08-30 11:41:12 +02:00
|
|
|
if (~node.channel.toLowerCase().indexOf(to.toLowerCase())) {
|
2014-08-29 17:41:15 +02:00
|
|
|
var msg = { "topic":from, "from":from, "to":to, "payload":message };
|
|
|
|
node.send([msg,null]);
|
|
|
|
}
|
2014-10-06 11:20:40 +02:00
|
|
|
//else { console.log(node.channel,to); }
|
2013-11-02 13:12:47 +01:00
|
|
|
});
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.addListener('pm', function(from, message) {
|
|
|
|
//node.log("PM => "+from + ': ' + message);
|
2014-05-04 00:32:04 +02:00
|
|
|
var msg = { "topic":from, "from":from, "to":"PRIV", "payload":message };
|
|
|
|
node.send([msg,null]);
|
2013-11-02 13:12:47 +01:00
|
|
|
});
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.addListener('join', function(channel, who) {
|
2014-05-04 00:32:04 +02:00
|
|
|
var msg = { "payload": { "type":"join", "who":who, "channel":channel } };
|
|
|
|
node.send([null,msg]);
|
2014-10-06 11:20:40 +02:00
|
|
|
//node.log(who+' has joined '+channel);
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.addListener('invite', function(channel, from, message) {
|
2014-05-04 00:32:04 +02:00
|
|
|
var msg = { "payload": { "type":"invite", "who":from, "channel":channel, "message":message } };
|
|
|
|
node.send([null,msg]);
|
2014-10-06 11:20:40 +02:00
|
|
|
//node.log(from+' sent invite to '+channel+': '+message);
|
2013-11-02 13:12:47 +01:00
|
|
|
});
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.addListener('part', function(channel, who, reason) {
|
2014-05-04 00:32:04 +02:00
|
|
|
var msg = { "payload": { "type":"part", "who":who, "channel":channel, "reason":reason } };
|
|
|
|
node.send([null,msg]);
|
2014-10-06 11:20:40 +02:00
|
|
|
//node.log(who+' has left '+channel+': '+reason);
|
2013-11-02 13:12:47 +01:00
|
|
|
});
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.addListener('quit', function(nick, reason, channels, message) {
|
2014-05-04 00:32:04 +02:00
|
|
|
var msg = { "payload": { "type":"quit", "who":nick, "channel":channels, "reason":reason } };
|
|
|
|
node.send([null,msg]);
|
2014-10-06 11:20:40 +02:00
|
|
|
//node.log(nick+' has quit '+channels+': '+reason);
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.addListener('kick', function(channel, who, by, reason) {
|
2014-05-04 00:32:04 +02:00
|
|
|
var msg = { "payload": { "type":"kick", "who":who, "channel":channel, "by":by, "reason":reason } };
|
|
|
|
node.send([null,msg]);
|
2014-10-06 11:20:40 +02:00
|
|
|
//node.log(who+' was kicked from '+channel+' by '+by+': '+reason);
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.addListener('names', function (channel, nicks) {
|
2014-05-09 10:48:41 +02:00
|
|
|
var msg = { "payload": { "type": "names", "channel": channel, "names": nicks} };
|
|
|
|
node.send([null, msg]);
|
|
|
|
});
|
2014-10-03 22:14:17 +02:00
|
|
|
node.ircclient.addListener('raw', function (message) { // any message means we are alive
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
|
|
|
});
|
|
|
|
node.on("close", function() {
|
2014-10-05 22:36:42 +02:00
|
|
|
node.ircclient.removeAllListeners();
|
2014-10-03 22:14:17 +02:00
|
|
|
if (node.recon) { clearInterval(node.recon); }
|
|
|
|
});
|
2013-11-02 13:12:47 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("irc in",IrcInNode);
|
2014-05-29 23:13:21 +02:00
|
|
|
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// The Output Node
|
|
|
|
function IrcOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2014-08-30 11:41:12 +02:00
|
|
|
this.sendFlag = n.sendObject;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.ircserver = n.ircserver;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.ircserver);
|
|
|
|
this.channel = n.channel || this.serverConfig.channel;
|
2014-08-29 17:41:15 +02:00
|
|
|
var node = this;
|
2014-10-05 22:36:42 +02:00
|
|
|
if (node.serverConfig.ircclient === null) {
|
2014-12-19 00:22:36 +01:00
|
|
|
node.log("CONNECT: "+node.serverConfig.server);
|
2014-08-30 11:41:12 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"connecting"});
|
2015-04-09 10:55:34 +02:00
|
|
|
var options = {autoConnect:true,autoRejoin:false,floodProtection:true,secure:node.serverConfig.ssl,selfSigned:node.serverConfig.cert,port:node.serverConfig.port,retryDelay:20000};
|
|
|
|
node.serverConfig.ircclient = new irc.Client(node.serverConfig.server, node.serverConfig.nickname, options);
|
2014-10-05 22:36:42 +02:00
|
|
|
node.serverConfig.ircclient.setMaxListeners(0);
|
2014-08-29 17:41:15 +02:00
|
|
|
node.serverConfig.ircclient.addListener('error', function(message) {
|
2014-12-19 00:22:36 +01:00
|
|
|
if (RED.settings.verbose) { node.log("ERR: "+JSON.stringify(message)); }
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-10-03 22:14:17 +02:00
|
|
|
node.serverConfig.ircclient.addListener('netError', function(message) {
|
2014-10-03 23:27:58 +02:00
|
|
|
node.serverConfig.lastseen = Date.now();
|
2014-12-19 00:22:36 +01:00
|
|
|
if (RED.settings.verbose) { node.log("NET: "+JSON.stringify(message)); }
|
|
|
|
node.status({fill:"red",shape:"ring",text:"net error"});
|
2014-10-03 22:14:17 +02:00
|
|
|
});
|
|
|
|
node.serverConfig.ircclient.addListener('connect', function() {
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
2014-12-19 00:22:36 +01:00
|
|
|
if (RED.settings.verbose) { node.log("CONNECTED "); }
|
2014-10-05 22:36:42 +02:00
|
|
|
});
|
2014-10-18 21:36:49 +02:00
|
|
|
node.serverConfig.ircclient.addListener('registered', function(message) {
|
2014-12-19 00:22:36 +01:00
|
|
|
node.serverConfig.lastseen = Date.now();
|
|
|
|
node.log(node.serverConfig.ircclient.nick+" ONLINE: "+message.server);
|
2014-10-18 21:36:49 +02:00
|
|
|
node.status({fill:"yellow",shape:"dot",text:"connected"});
|
|
|
|
node.serverConfig.ircclient.join( node.channel, function(data) {
|
2014-12-19 00:22:36 +01:00
|
|
|
node.log(data+" JOINED: "+node.channel);
|
2014-10-18 21:36:49 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"joined"});
|
|
|
|
});
|
|
|
|
});
|
2014-12-19 00:22:36 +01:00
|
|
|
node.serverConfig.ircclient.addListener('ping', function(server) {
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
|
|
|
if (RED.settings.verbose) { node.log("PING from "+JSON.stringify(server)); }
|
|
|
|
node.status({fill:"green",shape:"dot",text:"ok"});
|
|
|
|
});
|
|
|
|
node.serverConfig.ircclient.addListener('quit', function(nick, reason, channels, message) {
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
|
|
|
if (RED.settings.verbose) { node.log("QUIT: "+nick+" "+reason+" "+channels+" "+JSON.stringify(message)); }
|
|
|
|
node.status({fill:"grey",shape:"ring",text:"quit"});
|
|
|
|
//node.serverConfig.ircclient.disconnect( function() {
|
|
|
|
// node.serverConfig.ircclient.connect();
|
|
|
|
//});
|
|
|
|
//if (RED.settings.verbose) { node.log("restart"); } // then retry
|
|
|
|
});
|
|
|
|
node.serverConfig.ircclient.addListener('raw', function (message) { // any message received means we are alive
|
|
|
|
//console.log("RAW:"+JSON.stringify(message));
|
|
|
|
if (message.commandType === "reply") {
|
|
|
|
//console.log("RAW:"+JSON.stringify(message));
|
|
|
|
node.serverConfig.lastseen = Date.now();
|
|
|
|
}
|
|
|
|
});
|
2014-10-03 22:14:17 +02:00
|
|
|
node.recon = setInterval( function() {
|
|
|
|
//console.log("CHK ",(Date.now()-node.serverConfig.lastseen)/1000);
|
2014-12-19 00:22:36 +01:00
|
|
|
if ((Date.now()-node.serverConfig.lastseen) > 240000) { // if more than 4 mins since last seen
|
2014-10-18 21:36:49 +02:00
|
|
|
node.serverConfig.ircclient.send.apply(node.serverConfig.ircclient,["TIME"]); // request time to check link
|
2014-10-03 22:14:17 +02:00
|
|
|
}
|
2014-12-19 00:22:36 +01:00
|
|
|
if ((Date.now()-node.serverConfig.lastseen) > 300000) { // If more than 5 mins
|
|
|
|
//node.serverConfig.ircclient.disconnect();
|
|
|
|
//node.serverConfig.ircclient.connect();
|
|
|
|
node.status({fill:"grey",shape:"ring",text:"no connection"});
|
|
|
|
if (RED.settings.verbose) { node.log("CONNECTION LOST ?"); }
|
2014-10-03 22:14:17 +02:00
|
|
|
}
|
2014-12-19 00:22:36 +01:00
|
|
|
//node.serverConfig.ircclient.send.apply(node.serverConfig.ircclient,["TIME"]); // request time to check link
|
2014-10-03 22:14:17 +02:00
|
|
|
}, 60000); // check every 1 min
|
2014-12-19 00:22:36 +01:00
|
|
|
//node.serverConfig.ircclient.connect();
|
2013-11-02 13:12:47 +01:00
|
|
|
}
|
2014-08-30 14:24:09 +02:00
|
|
|
else { node.status({text:""}); }
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient = node.serverConfig.ircclient;
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-08-29 17:41:15 +02:00
|
|
|
node.on("input", function(msg) {
|
2014-05-04 00:32:04 +02:00
|
|
|
if (Object.prototype.toString.call( msg.raw ) === '[object Array]') {
|
2014-12-19 00:22:36 +01:00
|
|
|
if (RED.settings.verbose) { node.log("RAW command:"+msg.raw); }
|
2014-09-01 00:50:56 +02:00
|
|
|
node.ircclient.send.apply(node.ircclient,msg.raw);
|
2013-11-02 13:12:47 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-05-04 00:32:04 +02:00
|
|
|
if (msg._topic) { delete msg._topic; }
|
2014-08-30 11:41:12 +02:00
|
|
|
var ch = node.channel.split(","); // split on , so we can send to multiple
|
|
|
|
if (node.sendFlag == "true") { // override channels with msg.topic
|
|
|
|
if ((msg.hasOwnProperty('topic'))&&(typeof msg.topic === "string")) {
|
|
|
|
ch = msg.topic.split(","); // split on , so we can send to multiple
|
|
|
|
}
|
|
|
|
else { node.warn("msg.topic not set"); }
|
|
|
|
}
|
|
|
|
for (var c = 0; c < ch.length; c++) {
|
|
|
|
if (node.sendFlag == "false") { // send whole message object to each channel
|
2014-08-29 17:41:15 +02:00
|
|
|
node.ircclient.say(ch[c], JSON.stringify(msg));
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-08-30 11:41:12 +02:00
|
|
|
else { // send just the payload to each channel
|
2014-08-29 17:41:15 +02:00
|
|
|
if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
|
2014-08-30 11:41:12 +02:00
|
|
|
node.ircclient.say(ch[c], msg.payload);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-01-02 12:15:03 +01:00
|
|
|
}
|
2013-11-02 13:12:47 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-10-03 22:14:17 +02:00
|
|
|
|
|
|
|
node.on("close", function() {
|
2014-10-05 22:36:42 +02:00
|
|
|
node.ircclient.removeAllListeners();
|
2014-10-03 22:14:17 +02:00
|
|
|
if (node.recon) { clearInterval(node.recon); }
|
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("irc out",IrcOutNode);
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|