2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 module.exports = function(RED) {
19 var irc = require("irc");
21 // The Server Definition - this opens (and closes) the connection
22 function IRCServerNode(n) {
23 RED.nodes.createNode(this,n);
24 this.server = n.server;
25 this.channel = n.channel;
26 this.nickname = n.nickname;
28 this.ircclient = null;
29 this.on("close", function() {
30 if (this.ircclient != null) {
31 this.ircclient.removeAllListeners();
32 this.ircclient.disconnect();
36 RED.nodes.registerType("irc-server",IRCServerNode);
40 function IrcInNode(n) {
41 RED.nodes.createNode(this,n);
42 this.ircserver = n.ircserver;
43 this.serverConfig = RED.nodes.getNode(this.ircserver);
44 this.channel = n.channel || this.serverConfig.channel;
46 if (node.serverConfig.ircclient === null) {
47 node.log("Connecting to "+node.serverConfig.server);
48 node.status({fill:"grey",shape:"dot",text:"connecting"});
49 node.serverConfig.ircclient = new irc.Client(node.serverConfig.server, node.serverConfig.nickname,{autoConnect:false,retryDelay:20000});
50 node.serverConfig.ircclient.setMaxListeners(0);
51 node.serverConfig.ircclient.addListener('error', function(message) {
52 node.log(JSON.stringify(message));
54 node.serverConfig.ircclient.addListener('netError', function(message) {
55 node.log(JSON.stringify("NET "+message));
56 node.serverConfig.lastseen = Date.now();
58 node.serverConfig.ircclient.addListener('connect', function() {
59 node.serverConfig.lastseen = Date.now();
61 node.serverConfig.ircclient.addListener('ping', function(server) {
62 node.serverConfig.lastseen = Date.now();
63 //node.log("PING "+JSON.stringify(server));
65 node.recon = setInterval( function() {
66 //console.log("CHK ",(Date.now()-node.serverConfig.lastseen)/1000);
67 if ((Date.now()-node.serverConfig.lastseen) > 300000) { // if more than 5 mins since last seen
68 node.ircclient.send.apply(node.ircclient,["TIME"]); // request time to check link
70 if ((Date.now()-node.serverConfig.lastseen) > 400000) { // If more than 6.5 mins
71 node.serverConfig.ircclient.disconnect();
72 node.serverConfig.ircclient.connect();
73 node.log("reconnect"); // then retry
75 node.ircclient.send.apply(node.ircclient,["TIME"]); // request time to check link
76 }, 60000); // check every 1 min
78 else { node.status({text:""}); }
79 node.ircclient = node.serverConfig.ircclient;
81 node.ircclient.addListener('registered', function(message) {
82 //node.log(node.ircclient.nick+" ONLINE");
83 node.status({fill:"yellow",shape:"dot",text:"connected"});
84 node.ircclient.join( node.channel, function(data) {
85 // node.log(data+" JOINED "+node.channel);
86 node.status({fill:"green",shape:"dot",text:"joined"});
89 node.ircclient.addListener('message', function (from, to, message) {
90 //node.log(from + ' => ' + to + ' : ' + message);
91 if (~node.channel.toLowerCase().indexOf(to.toLowerCase())) {
92 var msg = { "topic":from, "from":from, "to":to, "payload":message };
93 node.send([msg,null]);
95 //else { console.log(node.channel,to); }
97 node.ircclient.addListener('pm', function(from, message) {
98 //node.log("PM => "+from + ': ' + message);
99 var msg = { "topic":from, "from":from, "to":"PRIV", "payload":message };
100 node.send([msg,null]);
102 node.ircclient.addListener('join', function(channel, who) {
103 var msg = { "payload": { "type":"join", "who":who, "channel":channel } };
104 node.send([null,msg]);
105 //node.log(who+' has joined '+channel);
107 node.ircclient.addListener('invite', function(channel, from, message) {
108 var msg = { "payload": { "type":"invite", "who":from, "channel":channel, "message":message } };
109 node.send([null,msg]);
110 //node.log(from+' sent invite to '+channel+': '+message);
112 node.ircclient.addListener('part', function(channel, who, reason) {
113 var msg = { "payload": { "type":"part", "who":who, "channel":channel, "reason":reason } };
114 node.send([null,msg]);
115 //node.log(who+' has left '+channel+': '+reason);
117 node.ircclient.addListener('quit', function(nick, reason, channels, message) {
118 var msg = { "payload": { "type":"quit", "who":nick, "channel":channels, "reason":reason } };
119 node.send([null,msg]);
120 //node.log(nick+' has quit '+channels+': '+reason);
122 node.ircclient.addListener('kick', function(channel, who, by, reason) {
123 var msg = { "payload": { "type":"kick", "who":who, "channel":channel, "by":by, "reason":reason } };
124 node.send([null,msg]);
125 //node.log(who+' was kicked from '+channel+' by '+by+': '+reason);
127 node.ircclient.addListener('names', function (channel, nicks) {
128 var msg = { "payload": { "type": "names", "channel": channel, "names": nicks} };
129 node.send([null, msg]);
131 node.ircclient.addListener('raw', function (message) { // any message means we are alive
132 node.serverConfig.lastseen = Date.now();
134 node.on("close", function() {
135 node.ircclient.removeAllListeners();
136 if (node.recon) { clearInterval(node.recon); }
139 RED.nodes.registerType("irc in",IrcInNode);
143 function IrcOutNode(n) {
144 RED.nodes.createNode(this,n);
145 this.sendFlag = n.sendObject;
146 this.ircserver = n.ircserver;
147 this.serverConfig = RED.nodes.getNode(this.ircserver);
148 this.channel = n.channel || this.serverConfig.channel;
150 if (node.serverConfig.ircclient === null) {
151 node.log("connecting to "+node.serverConfig.server);
152 node.status({fill:"grey",shape:"dot",text:"connecting"});
153 node.serverConfig.ircclient = new irc.Client(node.serverConfig.server, node.serverConfig.nickname,{autoConnect:false,retryDelay:20000});
154 node.serverConfig.ircclient.setMaxListeners(0);
155 node.serverConfig.ircclient.addListener('error', function(message) {
156 node.log(JSON.stringify(message));
158 node.serverConfig.ircclient.addListener('netError', function(message) {
159 node.log(JSON.stringify("NET "+message));
160 node.serverConfig.lastseen = Date.now();
162 node.serverConfig.ircclient.addListener('connect', function() {
163 node.serverConfig.lastseen = Date.now();
165 node.serverConfig.ircclient.addListener('ping', function(server) {
166 node.serverConfig.lastseen = Date.now();
167 //node.log("PING "+JSON.stringify(server));
169 node.serverConfig.ircclient.addListener('raw', function (message) { // any message received means we are alive
170 if (message.commandType === "reply") { node.serverConfig.lastseen = Date.now(); }
172 node.recon = setInterval( function() {
173 //console.log("CHK ",(Date.now()-node.serverConfig.lastseen)/1000);
174 if ((Date.now()-node.serverConfig.lastseen) > 300000) { // if more than 5 mins since last seen
175 node.ircclient.send.apply(node.ircclient,["TIME"]); // request time to check link
177 if ((Date.now()-node.serverConfig.lastseen) > 400000) { // If more than 6.5 mins
178 node.serverConfig.ircclient.disconnect();
179 node.serverConfig.ircclient.connect();
180 node.log("reconnect"); // then retry
182 node.ircclient.send.apply(node.ircclient,["TIME"]); // request time to check link
183 }, 60000); // check every 1 min
184 node.serverConfig.ircclient.connect();
186 else { node.status({text:""}); }
187 node.ircclient = node.serverConfig.ircclient;
189 node.ircclient.addListener('registered', function(message) {
190 node.log(node.ircclient.nick+" ONLINE");
191 node.status({fill:"yellow",shape:"dot",text:"connected"});
192 node.ircclient.join( node.channel, function(data) {
193 //node.log(data+" JOINED "+node.channel);
194 node.status({fill:"green",shape:"dot",text:"joined"});
198 node.on("input", function(msg) {
199 if (Object.prototype.toString.call( msg.raw ) === '[object Array]') {
200 node.log("RAW command:"+msg.raw);
201 node.ircclient.send.apply(node.ircclient,msg.raw);
203 //for (var i = 0; i < 10; i++) {
204 //if (typeof m[i] !== "string") { m[i] = ""; }
205 //m[i] = m[i].replace(/"/g, "");
207 //node.log("RAW command:"+m);
208 //node.ircclient.send(m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],m[8],m[9]);
211 if (msg._topic) { delete msg._topic; }
212 var ch = node.channel.split(","); // split on , so we can send to multiple
213 if (node.sendFlag == "true") { // override channels with msg.topic
214 if ((msg.hasOwnProperty('topic'))&&(typeof msg.topic === "string")) {
215 ch = msg.topic.split(","); // split on , so we can send to multiple
217 else { node.warn("msg.topic not set"); }
219 for (var c = 0; c < ch.length; c++) {
220 if (node.sendFlag == "false") { // send whole message object to each channel
221 node.ircclient.say(ch[c], JSON.stringify(msg));
223 else { // send just the payload to each channel
224 if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
225 node.ircclient.say(ch[c], msg.payload);
231 node.on("close", function() {
232 node.ircclient.removeAllListeners();
233 if (node.recon) { clearInterval(node.recon); }
236 RED.nodes.registerType("irc out",IrcOutNode);