Allow commands on connect

This commit is contained in:
Mattias Erming 2014-09-09 12:31:23 -07:00
parent dea1fe1b66
commit c66fab06a4
4 changed files with 76 additions and 54 deletions

View File

@ -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;

View File

@ -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
});
};

View File

@ -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) {

View File

@ -7,6 +7,10 @@
"password": "serverpw"
"nick": "example",
"realname": "Example User",
"commands": [
"/msg NickServ identify password",
"/msg ChanServ op #chan"
],
"join": "#foo, #bar"
}]
}