Load input plugins at startup and call them directly when a command is received

This commit is contained in:
Jérémie Astori 2016-03-14 00:21:42 -04:00
parent f81235900b
commit 93a393f10d
15 changed files with 44 additions and 71 deletions

View File

@ -48,7 +48,14 @@ var inputs = [
"quit", "quit",
"raw", "raw",
"topic", "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) { function Client(manager, name, config) {
_.merge(this, { _.merge(this, {
@ -281,22 +288,9 @@ Client.prototype.input = function(data) {
var args = text.split(" "); var args = text.split(" ");
var cmd = args.shift().toLowerCase(); var cmd = args.shift().toLowerCase();
var result = inputs.some(function(plugin) { if (cmd in inputs) {
try { inputs[cmd].apply(client, [target.network, target.chan, cmd, args]);
var path = "./plugins/inputs/" + plugin; } else {
var fn = require(path);
return fn.apply(client, [
target.network,
target.chan,
cmd,
args
]);
} catch (e) {
console.log(path + ": " + e);
}
});
if (result !== true) {
target.network.irc.write(text); target.network.irc.write(text);
} }
}; };

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["slap", "me"];
if (cmd !== "slap" && cmd !== "me") {
return;
}
exports.input = function(network, chan, cmd, args) {
var irc = network.irc; var irc = network.irc;
switch (cmd) { switch (cmd) {

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["connect", "server"];
if (cmd !== "connect" && cmd !== "server") {
return;
}
exports.input = function(network, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var client = this; var client = this;
client.connect({ client.connect({

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["invite"];
if (cmd !== "invite") {
return;
}
exports.input = function(network, chan, cmd, args) {
var irc = network.irc; var irc = network.irc;
if (args.length === 2) { if (args.length === 2) {

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["join"];
if (cmd !== "join") {
return;
}
exports.input = function(network, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc; var irc = network.irc;
irc.join(args[0], args[1]); irc.join(args[0], args[1]);

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["kick"];
if (cmd !== "kick") {
return;
}
exports.input = function(network, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc; var irc = network.irc;
irc.kick(chan.name, args[0]); irc.kick(chan.name, args[0]);

View File

@ -1,7 +1,7 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["mode", "op", "voice", "deop", "devoice"];
if (cmd !== "mode" && cmd !== "op" && cmd !== "voice" && cmd !== "deop" && cmd !== "devoice") {
return; exports.input = function(network, chan, cmd, args) {
} else if (args.length === 0) { if (args.length === 0) {
return; return;
} }

View File

@ -1,9 +1,8 @@
var _ = require("lodash"); var _ = require("lodash");
module.exports = function(network, chan, cmd, args) { exports.commands = ["msg", "say"];
if (cmd !== "say" && cmd !== "msg") {
return; exports.input = function(network, chan, cmd, args) {
}
if (args.length === 0 || args[0] === "") { if (args.length === 0 || args[0] === "") {
return true; return true;
} }

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["nick"];
if (cmd !== "nick") {
return;
}
exports.input = function(network, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc; var irc = network.irc;
irc.nick(args[0]); irc.nick(args[0]);

View File

@ -1,8 +1,10 @@
var _ = require("lodash"); var _ = require("lodash");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
module.exports = function(network, chan, cmd, args) { exports.commands = ["notice"];
if (cmd !== "notice" || !args[1]) {
exports.input = function(network, chan, cmd, args) {
if (!args[1]) {
return; return;
} }

View File

@ -1,10 +1,8 @@
var _ = require("lodash"); var _ = require("lodash");
module.exports = function(network, chan, cmd, args) { exports.commands = ["close", "leave", "part"];
if (cmd !== "part" && cmd !== "leave" && cmd !== "close") {
return;
}
exports.input = function(network, chan, cmd, args) {
if (chan.type !== "query") { if (chan.type !== "query") {
var irc = network.irc; var irc = network.irc;
if (args.length === 0) { if (args.length === 0) {

View File

@ -1,10 +1,8 @@
var _ = require("lodash"); var _ = require("lodash");
module.exports = function(network, chan, cmd, args) { exports.commands = ["quit", "disconnect"];
if (cmd !== "quit" && cmd !== "disconnect") {
return;
}
exports.input = function(network, chan, cmd, args) {
var client = this; var client = this;
var irc = network.irc; var irc = network.irc;
var quitMessage = args[0] ? args.join(" ") : ""; var quitMessage = args[0] ? args.join(" ") : "";

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["raw", "send", "quote"];
if (cmd !== "raw" && cmd !== "send" && cmd !== "quote") {
return;
}
exports.input = function(network, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc; var irc = network.irc;
irc.write(args.join(" ")); irc.write(args.join(" "));

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["topic"];
if (cmd !== "topic") {
return;
}
exports.input = function(network, chan, cmd, args) {
var msg = "TOPIC"; var msg = "TOPIC";
msg += " " + chan.name; msg += " " + chan.name;
msg += args[0] ? (" :" + args.join(" ")) : ""; msg += args[0] ? (" :" + args.join(" ")) : "";

View File

@ -1,8 +1,6 @@
module.exports = function(network, chan, cmd, args) { exports.commands = ["query", "whois"];
if (cmd !== "whois" && cmd !== "query") {
return;
}
exports.input = function(network, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc; var irc = network.irc;
irc.whois(args[0]); irc.whois(args[0]);