2013-09-05 16:02:48 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2013 IBM Corp.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
2013-11-14 16:44:54 +01:00
|
|
|
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
2013-09-05 16:02:48 +02:00
|
|
|
var irc = require("irc");
|
2013-10-02 22:15:12 +02:00
|
|
|
var util = require("util");
|
2013-09-05 16:02:48 +02:00
|
|
|
|
|
|
|
// The Server Definition - this opens (and closes) the connection
|
|
|
|
function IRCServerNode(n) {
|
2013-11-02 13:12:47 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = n.server;
|
|
|
|
this.channel = n.channel;
|
|
|
|
this.nickname = n.nickname;
|
|
|
|
this.ircclient = null;
|
|
|
|
this.on("close", function() {
|
|
|
|
if (this.ircclient != null) {
|
|
|
|
this.ircclient.disconnect();
|
|
|
|
}
|
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("irc-server",IRCServerNode);
|
|
|
|
|
2014-01-02 12:15:03 +01:00
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
// The Input Node
|
|
|
|
function IrcInNode(n) {
|
2013-11-02 13:12:47 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.ircserver = n.ircserver;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.ircserver);
|
2014-01-02 12:15:03 +01:00
|
|
|
this.channel = n.channel || this.serverConfig.channel;
|
2013-11-02 13:12:47 +01:00
|
|
|
if (this.serverConfig.ircclient == null) {
|
|
|
|
this.serverConfig.ircclient = new irc.Client(this.serverConfig.server, this.serverConfig.nickname, {
|
2014-01-02 12:15:03 +01:00
|
|
|
channels: [this.channel]
|
2013-11-02 13:12:47 +01:00
|
|
|
});
|
|
|
|
this.serverConfig.ircclient.addListener('error', function(message) {
|
|
|
|
util.log('[irc] '+ JSON.stringify(message));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.ircclient = this.serverConfig.ircclient;
|
|
|
|
var node = this;
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-11-02 13:12:47 +01:00
|
|
|
this.ircclient.addListener('message', function (from, to, message) {
|
|
|
|
//util.log(from + ' => ' + to + ': ' + message);
|
2014-01-02 12:15:03 +01:00
|
|
|
var msg = { "topic":from, "from":from, "to":to, "payload":message };
|
|
|
|
node.send([msg,null]);
|
|
|
|
});
|
|
|
|
this.ircclient.addListener('pm', function(from, message) {
|
|
|
|
var msg = { "topic":from, "from":from, "to":"PRIV", "payload":message };
|
|
|
|
node.send([msg,null]);
|
|
|
|
});
|
2014-01-03 11:57:49 +01:00
|
|
|
|
2014-01-02 12:15:03 +01:00
|
|
|
this.ircclient.addListener('join', function(channel, who) {
|
|
|
|
var msg = { "payload": { "type":"join", "who":who, "channel":channel } };
|
|
|
|
node.send([null,msg]);
|
|
|
|
node.log(who+' has joined '+channel);
|
|
|
|
});
|
2014-01-03 11:57:49 +01:00
|
|
|
this.ircclient.addListener('invite', function(channel, from, message) {
|
|
|
|
var msg = { "payload": { "type":"invite", "who":from, "channel":channel, "message":message } };
|
|
|
|
node.send([null,msg]);
|
|
|
|
node.log(from+' sent invite to '+channel+': '+message);
|
|
|
|
});
|
2014-01-02 12:15:03 +01:00
|
|
|
this.ircclient.addListener('part', function(channel, who, reason) {
|
|
|
|
var msg = { "payload": { "type":"part", "who":who, "channel":channel, "reason":reason } };
|
|
|
|
node.send([null,msg]);
|
|
|
|
node.log(who+'has left '+channel+': '+reason);
|
|
|
|
});
|
|
|
|
this.ircclient.addListener('quit', function(nick, reason, channels, message) {
|
|
|
|
var msg = { "payload": { "type":"quit", "who":nick, "channel":channels, "reason":reason } };
|
|
|
|
node.send([null,msg]);
|
|
|
|
node.log(nick+'has quit '+channels+': '+reason);
|
|
|
|
});
|
|
|
|
this.ircclient.addListener('kick', function(channel, who, by, reason) {
|
|
|
|
var msg = { "payload": { "type":"kick", "who":who, "channel":channel, "by":by, "reason":reason } };
|
|
|
|
node.send([null,msg]);
|
|
|
|
node.log(who+' was kicked from '+channel+' by '+by+': '+reason);
|
2013-11-02 13:12:47 +01:00
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("irc in",IrcInNode);
|
|
|
|
|
2014-01-02 12:15:03 +01:00
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
// The Output Node
|
|
|
|
function IrcOutNode(n) {
|
2013-11-02 13:12:47 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.sendAll = n.sendObject;
|
|
|
|
this.ircserver = n.ircserver;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.ircserver);
|
2014-01-02 12:15:03 +01:00
|
|
|
this.channel = n.channel || this.serverConfig.channel;
|
2013-11-02 13:12:47 +01:00
|
|
|
if (this.serverConfig.ircclient == null) {
|
|
|
|
this.serverConfig.ircclient = new irc.Client(this.serverConfig.server, this.serverConfig.nickname, {
|
2014-01-02 12:15:03 +01:00
|
|
|
channels: [this.channel]
|
2013-11-02 13:12:47 +01:00
|
|
|
});
|
|
|
|
this.serverConfig.ircclient.addListener('error', function(message) {
|
|
|
|
util.log('[irc] '+ JSON.stringify(message));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.ircclient = this.serverConfig.ircclient;
|
|
|
|
var node = this;
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-11-02 13:12:47 +01:00
|
|
|
this.on("input", function(msg) {
|
2014-01-02 12:15:03 +01:00
|
|
|
if (Object.prototype.toString.call( msg.raw ) === '[object Array]') {
|
|
|
|
var m = msg.raw;
|
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
if (typeof m[i] !== "string") { m[i] = ""; }
|
|
|
|
m[i] = m[i].replace(/"/g, "");
|
|
|
|
}
|
|
|
|
util.log("[irc] RAW command:"+m);
|
|
|
|
node.ircclient.send(m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],m[8],m[9]);
|
2013-11-02 13:12:47 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-01-02 12:15:03 +01:00
|
|
|
if (msg._topic) { delete msg._topic; }
|
|
|
|
if (node.sendAll == "false") {
|
|
|
|
node.ircclient.say(node.channel, JSON.stringify(msg));
|
2013-11-02 13:12:47 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-01-02 12:15:03 +01:00
|
|
|
if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
|
|
|
|
if (node.sendAll == "pay") {
|
|
|
|
node.ircclient.say(node.channel, msg.payload);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var to = msg.topic || node.channel;
|
|
|
|
node.ircclient.say(to, msg.payload);
|
|
|
|
}
|
2013-11-02 13:12:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("irc out",IrcOutNode);
|