From e5536b848a5647fd9829e9dcf13a889c195f9af1 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Thu, 2 Jan 2014 11:15:03 +0000 Subject: [PATCH] Changes to IRC node addresses fix for #106 moved channel to node properties (won't break but will flag as undeployed changes) changed defaults to be irc.freenode.net allow input of a msg.raw as an array of a raw irc command (undocumented) --- nodes/core/social/91-irc.html | 52 +++++++++++++++++++-------- nodes/core/social/91-irc.js | 66 +++++++++++++++++++++++++++-------- 2 files changed, 89 insertions(+), 29 deletions(-) diff --git a/nodes/core/social/91-irc.html b/nodes/core/social/91-irc.html index da605dde0..825163a4e 100644 --- a/nodes/core/social/91-irc.html +++ b/nodes/core/social/91-irc.html @@ -19,15 +19,21 @@ +
+ + +
+
The channel to join must start with a # (as per normal irc rules...)
+ + diff --git a/nodes/core/social/91-irc.js b/nodes/core/social/91-irc.js index 697bd9ab6..9b8a1ed30 100644 --- a/nodes/core/social/91-irc.js +++ b/nodes/core/social/91-irc.js @@ -31,17 +31,18 @@ function IRCServerNode(n) { } }); } - RED.nodes.registerType("irc-server",IRCServerNode); + // 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; if (this.serverConfig.ircclient == null) { this.serverConfig.ircclient = new irc.Client(this.serverConfig.server, this.serverConfig.nickname, { - channels: [this.serverConfig.channel] + channels: [this.channel] }); this.serverConfig.ircclient.addListener('error', function(message) { util.log('[irc] '+ JSON.stringify(message)); @@ -52,23 +53,48 @@ function IrcInNode(n) { this.ircclient.addListener('message', function (from, to, message) { //util.log(from + ' => ' + to + ': ' + message); - var msg = { "topic":from, "to":to, "payload":message }; - node.send(msg); + 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]); + }); + 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); + }); + 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); }); } RED.nodes.registerType("irc in",IrcInNode); + // The Output Node function IrcOutNode(n) { RED.nodes.createNode(this,n); this.sendAll = n.sendObject; this.ircserver = n.ircserver; this.serverConfig = RED.nodes.getNode(this.ircserver); - this.channel = this.serverConfig.channel; + this.channel = n.channel || this.serverConfig.channel; if (this.serverConfig.ircclient == null) { this.serverConfig.ircclient = new irc.Client(this.serverConfig.server, this.serverConfig.nickname, { - channels: [this.serverConfig.channel] + channels: [this.channel] }); this.serverConfig.ircclient.addListener('error', function(message) { util.log('[irc] '+ JSON.stringify(message)); @@ -78,19 +104,29 @@ function IrcOutNode(n) { var node = this; this.on("input", function(msg) { - //console.log(msg,node.channel); - if (msg._topic) { delete msg._topic; } - if (node.sendAll == "false") { - node.ircclient.say(node.channel, JSON.stringify(msg)); + 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]); } else { - if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); } - if (node.sendAll == "pay") { - node.ircclient.say(node.channel, msg.payload); + if (msg._topic) { delete msg._topic; } + if (node.sendAll == "false") { + node.ircclient.say(node.channel, JSON.stringify(msg)); } else { - var to = msg.topic || node.channel; - node.ircclient.say(to, msg.payload); + 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); + } } } });