Skip to content

Commit

Permalink
Merge pull request #154 from xPaw/better-commands
Browse files Browse the repository at this point in the history
Handle commands in a better way
  • Loading branch information
astorije committed Mar 11, 2016
2 parents f577da5 + 842b8d6 commit ddc72ea
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 38 deletions.
28 changes: 19 additions & 9 deletions src/client.js
Expand Up @@ -33,21 +33,21 @@ var events = [
"whois"
];
var inputs = [
// These inputs are sorted in order that is most likely to be used
"msg",
"whois",
"part",
"action",
"connect",
"invite",
"join",
"kick",
"mode",
"msg",
"nick",
"notice",
"part",
"quit",
"raw",
"services",
"topic",
"whois"
];

function Client(manager, name, config) {
Expand Down Expand Up @@ -270,16 +270,22 @@ Client.prototype.input = function(data) {
var client = this;
var text = data.text.trim();
var target = client.find(data.target);
if (text.charAt(0) !== "/") {
text = "/say " + text;

// This is either a normal message or a command escaped with a leading '/'
if (text.charAt(0) !== "/" || text.charAt(1) === "/") {
text = "say " + text.replace(/^\//, "");
} else {
text = text.substr(1);
}

var args = text.split(" ");
var cmd = args.shift().replace("/", "").toLowerCase();
_.each(inputs, function(plugin) {
var cmd = args.shift().toLowerCase();

var result = inputs.some(function(plugin) {
try {
var path = "./plugins/inputs/" + plugin;
var fn = require(path);
fn.apply(client, [
return fn.apply(client, [
target.network,
target.chan,
cmd,
Expand All @@ -289,6 +295,10 @@ Client.prototype.input = function(data) {
console.log(path + ": " + e);
}
});

if (result !== true) {
target.network.irc.write(text);
}
};

Client.prototype.more = function(data) {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/inputs/action.js
Expand Up @@ -26,4 +26,6 @@ module.exports = function(network, chan, cmd, args) {
});
break;
}

return true;
};
3 changes: 3 additions & 0 deletions src/plugins/inputs/connect.js
Expand Up @@ -2,10 +2,13 @@ module.exports = function(network, chan, cmd, args) {
if (cmd !== "connect" && cmd !== "server") {
return;
}

if (args.length !== 0) {
var client = this;
client.connect({
host: args[0]
});
}

return true;
};
2 changes: 2 additions & 0 deletions src/plugins/inputs/invite.js
Expand Up @@ -10,4 +10,6 @@ module.exports = function(network, chan, cmd, args) {
} else if (args.length === 1 && chan.type === "channel") {
irc.invite(args[0], chan.name); // Current channel
}

return true;
};
3 changes: 3 additions & 0 deletions src/plugins/inputs/join.js
Expand Up @@ -2,8 +2,11 @@ module.exports = function(network, chan, cmd, args) {
if (cmd !== "join") {
return;
}

if (args.length !== 0) {
var irc = network.irc;
irc.join(args[0], args[1]);
}

return true;
};
3 changes: 3 additions & 0 deletions src/plugins/inputs/kick.js
Expand Up @@ -2,8 +2,11 @@ module.exports = function(network, chan, cmd, args) {
if (cmd !== "kick") {
return;
}

if (args.length !== 0) {
var irc = network.irc;
irc.kick(chan.name, args[0]);
}

return true;
};
4 changes: 3 additions & 1 deletion src/plugins/inputs/mode.js
Expand Up @@ -16,7 +16,7 @@ module.exports = function(network, chan, cmd, args) {
"devoice": "-v"
}[cmd];
} else if (args.length === 1) {
return;
return true;
} else {
mode = args[0];
user = args[1];
Expand All @@ -27,4 +27,6 @@ module.exports = function(network, chan, cmd, args) {
mode,
user
);

return true;
};
6 changes: 4 additions & 2 deletions src/plugins/inputs/msg.js
Expand Up @@ -5,14 +5,14 @@ module.exports = function(network, chan, cmd, args) {
return;
}
if (args.length === 0 || args[0] === "") {
return;
return true;
}
var irc = network.irc;
var target = "";
if (cmd === "msg") {
target = args.shift();
if (args.length === 0) {
return;
return true;
}
} else {
target = chan.name;
Expand All @@ -27,4 +27,6 @@ module.exports = function(network, chan, cmd, args) {
message: msg
});
}

return true;
};
3 changes: 3 additions & 0 deletions src/plugins/inputs/nick.js
Expand Up @@ -2,8 +2,11 @@ module.exports = function(network, chan, cmd, args) {
if (cmd !== "nick") {
return;
}

if (args.length !== 0) {
var irc = network.irc;
irc.nick(args[0]);
}

return true;
};
2 changes: 2 additions & 0 deletions src/plugins/inputs/notice.js
Expand Up @@ -27,4 +27,6 @@ module.exports = function(network, chan, cmd, args) {
chan: targetChan.id,
msg: msg
});

return true;
};
4 changes: 4 additions & 0 deletions src/plugins/inputs/part.js
Expand Up @@ -4,15 +4,19 @@ module.exports = function(network, chan, cmd, args) {
if (cmd !== "part" && cmd !== "leave" && cmd !== "close") {
return;
}

if (chan.type !== "query") {
var irc = network.irc;
if (args.length === 0) {
args.push(chan.name);
}
irc.part(args);
}

network.channels = _.without(network.channels, chan);
this.emit("part", {
chan: chan.id
});

return true;
};
2 changes: 2 additions & 0 deletions src/plugins/inputs/quit.js
Expand Up @@ -16,4 +16,6 @@ module.exports = function(network, chan, cmd, args) {
});

irc.quit(quitMessage);

return true;
};
3 changes: 3 additions & 0 deletions src/plugins/inputs/raw.js
Expand Up @@ -2,8 +2,11 @@ module.exports = function(network, chan, cmd, args) {
if (cmd !== "raw" && cmd !== "send" && cmd !== "quote") {
return;
}

if (args.length !== 0) {
var irc = network.irc;
irc.write(args.join(" "));
}

return true;
};
26 changes: 0 additions & 26 deletions src/plugins/inputs/services.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/plugins/inputs/topic.js
Expand Up @@ -9,4 +9,6 @@ module.exports = function(network, chan, cmd, args) {

var irc = network.irc;
irc.write(msg);

return true;
};
3 changes: 3 additions & 0 deletions src/plugins/inputs/whois.js
Expand Up @@ -2,8 +2,11 @@ module.exports = function(network, chan, cmd, args) {
if (cmd !== "whois" && cmd !== "query") {
return;
}

if (args.length !== 0) {
var irc = network.irc;
irc.whois(args[0]);
}

return true;
};

0 comments on commit ddc72ea

Please sign in to comment.