From c66fab06a4e9664155f46814a2d5ef247a675527 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Tue, 9 Sep 2014 12:31:23 -0700 Subject: [PATCH] Allow commands on connect --- src/client.js | 69 +++++++++++++++++++++++++++++++++++++-- src/plugins/inputs/msg.js | 12 +++---- src/server.js | 45 +------------------------ users/example/user.json | 4 +++ 4 files changed, 76 insertions(+), 54 deletions(-) diff --git a/src/client.js b/src/client.js index ef625b3..b817aad 100644 --- a/src/client.js +++ b/src/client.js @@ -26,6 +26,22 @@ var events = [ "welcome", "whois" ]; +var inputs = [ + "action", + "connect", + "invite", + "join", + "kick", + "mode", + "msg", + "nick", + "notice", + "part", + "quit", + "raw", + "topic", + "whois" +]; function Client(sockets, config) { _.merge(this, { @@ -114,12 +130,59 @@ Client.prototype.connect = function(args) { ]); }); - var join = (args.join || "#shout-irc").replace(/\,/g, " ").split(/\s+/g); - irc.on("welcome", function() { - irc.join(join); + irc.once("welcome", function() { + var delay = 1000; + var commands = args.commands; + if (Array.isArray(commands)) { + commands.forEach(function(cmd) { + setTimeout(function() { + client.input({ + target: network.channels[0].id, + text: cmd + }); + }, delay); + delay += 1000; + }); + } + setTimeout(function() { + irc.write("PING " + network.host); + }, delay); + }); + + irc.once("pong", function() { + var join = (args.join || ""); + if (join) { + join = join.replace(/\,/g, " ").split(/\s+/g); + irc.join(join); + } }); }; +Client.prototype.input = function(data) { + var client = this; + var text = data.text; + var target = client.find(data.target); + if (text.charAt(0) !== "/") { + text = "/say " + text; + } + var args = text.split(" "); + var cmd = args.shift().replace("/", "").toLowerCase(); + _.each(inputs, function(plugin) { + try { + var path = "./plugins/inputs/" + plugin; + var fn = require(path); + fn.apply(client, [ + target.network, + target.chan, + cmd, + args + ]); + } catch (e) { + console.log(path + ": " + e); + } + }); +} + Client.prototype.quit = function() { this.networks.forEach(function(network) { var irc = network.irc; diff --git a/src/plugins/inputs/msg.js b/src/plugins/inputs/msg.js index 6e0bfe2..e998c7e 100644 --- a/src/plugins/inputs/msg.js +++ b/src/plugins/inputs/msg.js @@ -21,11 +21,9 @@ module.exports = function(network, chan, cmd, args) { } var text = args.join(" "); irc.send(target, text); - if (target == chan.name && typeof chan !== "undefined") { - irc.emit("message", { - from: irc.me, - to: chan.name, - message: text - }); - } + irc.emit("message", { + from: irc.me, + to: target, + message: text + }); }; diff --git a/src/server.js b/src/server.js index d040dde..ab05fd1 100644 --- a/src/server.js +++ b/src/server.js @@ -9,23 +9,6 @@ var io = require("socket.io"); var sockets = null; var manager = new ClientManager(); -var inputs = [ - "action", - "connect", - "invite", - "join", - "kick", - "mode", - "msg", - "nick", - "notice", - "part", - "quit", - "raw", - "topic", - "whois" -]; - module.exports = function(port, host, isPublic) { config.port = port; config.host = host; @@ -78,7 +61,7 @@ function init(socket, client) { socket.on( "input", function(data) { - input(client, data); + client.input(data); } ); socket.on( @@ -124,32 +107,6 @@ function auth(data) { } } -function input(client, data) { - var text = data.text; - var target = client.find(data.target); - if (text.charAt(0) !== "/") { - text = "/say " + text; - } - - var args = text.split(" "); - var cmd = args.shift().replace("/", "").toLowerCase(); - - _.each(inputs, function(plugin) { - try { - var path = "./plugins/inputs/" + plugin; - var fn = require(path); - fn.apply(client, [ - target.network, - target.chan, - cmd, - args - ]); - } catch (e) { - console.log(path + ": " + e); - } - }); -} - function showMore(client, data) { var target = client.find(data.target); if (!target) { diff --git a/users/example/user.json b/users/example/user.json index d0e4083..7f65098 100644 --- a/users/example/user.json +++ b/users/example/user.json @@ -7,6 +7,10 @@ "password": "serverpw" "nick": "example", "realname": "Example User", + "commands": [ + "/msg NickServ identify password", + "/msg ChanServ op #chan" + ], "join": "#foo, #bar" }] }