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", "welcome",
"whois" "whois"
]; ];
var inputs = [
"action",
"connect",
"invite",
"join",
"kick",
"mode",
"msg",
"nick",
"notice",
"part",
"quit",
"raw",
"topic",
"whois"
];
function Client(sockets, config) { function Client(sockets, config) {
_.merge(this, { _.merge(this, {
@ -114,12 +130,59 @@ Client.prototype.connect = function(args) {
]); ]);
}); });
var join = (args.join || "#shout-irc").replace(/\,/g, " ").split(/\s+/g); irc.once("welcome", function() {
irc.on("welcome", function() { var delay = 1000;
irc.join(join); 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() { Client.prototype.quit = function() {
this.networks.forEach(function(network) { this.networks.forEach(function(network) {
var irc = network.irc; var irc = network.irc;

View File

@ -21,11 +21,9 @@ module.exports = function(network, chan, cmd, args) {
} }
var text = args.join(" "); var text = args.join(" ");
irc.send(target, text); irc.send(target, text);
if (target == chan.name && typeof chan !== "undefined") { irc.emit("message", {
irc.emit("message", { from: irc.me,
from: irc.me, to: target,
to: chan.name, message: text
message: text });
});
}
}; };

View File

@ -9,23 +9,6 @@ var io = require("socket.io");
var sockets = null; var sockets = null;
var manager = new ClientManager(); 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) { module.exports = function(port, host, isPublic) {
config.port = port; config.port = port;
config.host = host; config.host = host;
@ -78,7 +61,7 @@ function init(socket, client) {
socket.on( socket.on(
"input", "input",
function(data) { function(data) {
input(client, data); client.input(data);
} }
); );
socket.on( 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) { function showMore(client, data) {
var target = client.find(data.target); var target = client.find(data.target);
if (!target) { if (!target) {

View File

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