diff --git a/src/client.js b/src/client.js index 46845a2e..9ea04c19 100644 --- a/src/client.js +++ b/src/client.js @@ -48,7 +48,14 @@ var inputs = [ "quit", "raw", "topic", -]; +].reduce(function(plugins, name) { + var path = "./plugins/inputs/" + name; + var plugin = require(path); + plugin.commands.forEach(function(command) { + plugins[command] = plugin.input; + }); + return plugins; +}, {}); function Client(manager, name, config) { _.merge(this, { @@ -281,22 +288,9 @@ Client.prototype.input = function(data) { var args = text.split(" "); var cmd = args.shift().toLowerCase(); - var result = inputs.some(function(plugin) { - try { - var path = "./plugins/inputs/" + plugin; - var fn = require(path); - return fn.apply(client, [ - target.network, - target.chan, - cmd, - args - ]); - } catch (e) { - console.log(path + ": " + e); - } - }); - - if (result !== true) { + if (cmd in inputs) { + inputs[cmd].apply(client, [target.network, target.chan, cmd, args]); + } else { target.network.irc.write(text); } }; diff --git a/src/plugins/inputs/action.js b/src/plugins/inputs/action.js index 9ed6860c..bdb85d25 100644 --- a/src/plugins/inputs/action.js +++ b/src/plugins/inputs/action.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "slap" && cmd !== "me") { - return; - } +exports.commands = ["slap", "me"]; +exports.input = function(network, chan, cmd, args) { var irc = network.irc; switch (cmd) { diff --git a/src/plugins/inputs/connect.js b/src/plugins/inputs/connect.js index f2945f5c..0281f41a 100644 --- a/src/plugins/inputs/connect.js +++ b/src/plugins/inputs/connect.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "connect" && cmd !== "server") { - return; - } +exports.commands = ["connect", "server"]; +exports.input = function(network, chan, cmd, args) { if (args.length !== 0) { var client = this; client.connect({ diff --git a/src/plugins/inputs/invite.js b/src/plugins/inputs/invite.js index 7663849a..b81ae2c8 100644 --- a/src/plugins/inputs/invite.js +++ b/src/plugins/inputs/invite.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "invite") { - return; - } +exports.commands = ["invite"]; +exports.input = function(network, chan, cmd, args) { var irc = network.irc; if (args.length === 2) { diff --git a/src/plugins/inputs/join.js b/src/plugins/inputs/join.js index 4a8f0258..5ca98b4a 100644 --- a/src/plugins/inputs/join.js +++ b/src/plugins/inputs/join.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "join") { - return; - } +exports.commands = ["join"]; +exports.input = function(network, chan, cmd, args) { if (args.length !== 0) { var irc = network.irc; irc.join(args[0], args[1]); diff --git a/src/plugins/inputs/kick.js b/src/plugins/inputs/kick.js index 5844c8f5..a57d6cd4 100644 --- a/src/plugins/inputs/kick.js +++ b/src/plugins/inputs/kick.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "kick") { - return; - } +exports.commands = ["kick"]; +exports.input = function(network, chan, cmd, args) { if (args.length !== 0) { var irc = network.irc; irc.kick(chan.name, args[0]); diff --git a/src/plugins/inputs/mode.js b/src/plugins/inputs/mode.js index dba13a4f..5ca106be 100644 --- a/src/plugins/inputs/mode.js +++ b/src/plugins/inputs/mode.js @@ -1,7 +1,7 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "mode" && cmd !== "op" && cmd !== "voice" && cmd !== "deop" && cmd !== "devoice") { - return; - } else if (args.length === 0) { +exports.commands = ["mode", "op", "voice", "deop", "devoice"]; + +exports.input = function(network, chan, cmd, args) { + if (args.length === 0) { return; } diff --git a/src/plugins/inputs/msg.js b/src/plugins/inputs/msg.js index d4e3b268..27a27db3 100644 --- a/src/plugins/inputs/msg.js +++ b/src/plugins/inputs/msg.js @@ -1,9 +1,8 @@ var _ = require("lodash"); -module.exports = function(network, chan, cmd, args) { - if (cmd !== "say" && cmd !== "msg") { - return; - } +exports.commands = ["msg", "say"]; + +exports.input = function(network, chan, cmd, args) { if (args.length === 0 || args[0] === "") { return true; } diff --git a/src/plugins/inputs/nick.js b/src/plugins/inputs/nick.js index d3729154..14f44dc9 100644 --- a/src/plugins/inputs/nick.js +++ b/src/plugins/inputs/nick.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "nick") { - return; - } +exports.commands = ["nick"]; +exports.input = function(network, chan, cmd, args) { if (args.length !== 0) { var irc = network.irc; irc.nick(args[0]); diff --git a/src/plugins/inputs/notice.js b/src/plugins/inputs/notice.js index 291b2ca8..4c5981e5 100644 --- a/src/plugins/inputs/notice.js +++ b/src/plugins/inputs/notice.js @@ -1,8 +1,10 @@ var _ = require("lodash"); var Msg = require("../../models/msg"); -module.exports = function(network, chan, cmd, args) { - if (cmd !== "notice" || !args[1]) { +exports.commands = ["notice"]; + +exports.input = function(network, chan, cmd, args) { + if (!args[1]) { return; } diff --git a/src/plugins/inputs/part.js b/src/plugins/inputs/part.js index 94e99801..7941f55e 100644 --- a/src/plugins/inputs/part.js +++ b/src/plugins/inputs/part.js @@ -1,10 +1,8 @@ var _ = require("lodash"); -module.exports = function(network, chan, cmd, args) { - if (cmd !== "part" && cmd !== "leave" && cmd !== "close") { - return; - } +exports.commands = ["close", "leave", "part"]; +exports.input = function(network, chan, cmd, args) { if (chan.type !== "query") { var irc = network.irc; if (args.length === 0) { diff --git a/src/plugins/inputs/quit.js b/src/plugins/inputs/quit.js index dd6298b7..fb208b2f 100644 --- a/src/plugins/inputs/quit.js +++ b/src/plugins/inputs/quit.js @@ -1,10 +1,8 @@ var _ = require("lodash"); -module.exports = function(network, chan, cmd, args) { - if (cmd !== "quit" && cmd !== "disconnect") { - return; - } +exports.commands = ["quit", "disconnect"]; +exports.input = function(network, chan, cmd, args) { var client = this; var irc = network.irc; var quitMessage = args[0] ? args.join(" ") : ""; diff --git a/src/plugins/inputs/raw.js b/src/plugins/inputs/raw.js index 40f1f0bd..cab47bd8 100644 --- a/src/plugins/inputs/raw.js +++ b/src/plugins/inputs/raw.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "raw" && cmd !== "send" && cmd !== "quote") { - return; - } +exports.commands = ["raw", "send", "quote"]; +exports.input = function(network, chan, cmd, args) { if (args.length !== 0) { var irc = network.irc; irc.write(args.join(" ")); diff --git a/src/plugins/inputs/topic.js b/src/plugins/inputs/topic.js index 1e7b3c31..d342223f 100644 --- a/src/plugins/inputs/topic.js +++ b/src/plugins/inputs/topic.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "topic") { - return; - } +exports.commands = ["topic"]; +exports.input = function(network, chan, cmd, args) { var msg = "TOPIC"; msg += " " + chan.name; msg += args[0] ? (" :" + args.join(" ")) : ""; diff --git a/src/plugins/inputs/whois.js b/src/plugins/inputs/whois.js index bbd7d074..f9b127d1 100644 --- a/src/plugins/inputs/whois.js +++ b/src/plugins/inputs/whois.js @@ -1,8 +1,6 @@ -module.exports = function(network, chan, cmd, args) { - if (cmd !== "whois" && cmd !== "query") { - return; - } +exports.commands = ["query", "whois"]; +exports.input = function(network, chan, cmd, args) { if (args.length !== 0) { var irc = network.irc; irc.whois(args[0]);