2014-07-06 15:22:43 +00:00
|
|
|
var _ = require("lodash");
|
2014-07-19 01:31:00 +00:00
|
|
|
var config = require("../config.json");
|
2014-07-06 15:22:43 +00:00
|
|
|
var net = require("net");
|
2014-09-10 15:56:19 +00:00
|
|
|
var Msg = require("./models/msg");
|
2014-07-06 15:22:43 +00:00
|
|
|
var Network = require("./models/network");
|
|
|
|
var slate = require("slate-irc");
|
|
|
|
var tls = require("tls");
|
|
|
|
|
|
|
|
module.exports = Client;
|
|
|
|
|
|
|
|
var id = 0;
|
|
|
|
var events = [
|
2014-09-03 21:43:27 +00:00
|
|
|
"ctcp",
|
2014-08-16 19:49:28 +00:00
|
|
|
"error",
|
2014-08-17 21:40:08 +00:00
|
|
|
"image",
|
2014-07-06 15:22:43 +00:00
|
|
|
"join",
|
|
|
|
"kick",
|
|
|
|
"mode",
|
|
|
|
"motd",
|
|
|
|
"message",
|
|
|
|
"names",
|
|
|
|
"nick",
|
|
|
|
"notice",
|
|
|
|
"part",
|
|
|
|
"quit",
|
|
|
|
"topic",
|
|
|
|
"welcome",
|
|
|
|
"whois"
|
|
|
|
];
|
2014-09-09 19:31:23 +00:00
|
|
|
var inputs = [
|
|
|
|
"action",
|
|
|
|
"connect",
|
|
|
|
"invite",
|
|
|
|
"join",
|
|
|
|
"kick",
|
|
|
|
"mode",
|
|
|
|
"msg",
|
|
|
|
"nick",
|
|
|
|
"notice",
|
|
|
|
"part",
|
|
|
|
"quit",
|
|
|
|
"raw",
|
|
|
|
"topic",
|
|
|
|
"whois"
|
|
|
|
];
|
2014-07-06 15:22:43 +00:00
|
|
|
|
2014-07-19 01:31:00 +00:00
|
|
|
function Client(sockets, config) {
|
2014-07-08 20:50:41 +00:00
|
|
|
_.merge(this, {
|
2014-07-19 01:31:00 +00:00
|
|
|
config: config,
|
2014-07-08 20:50:41 +00:00
|
|
|
id: id++,
|
2014-07-19 01:31:00 +00:00
|
|
|
name: "",
|
|
|
|
networks: [],
|
2014-07-08 20:50:41 +00:00
|
|
|
sockets: sockets
|
|
|
|
});
|
2014-07-19 01:31:00 +00:00
|
|
|
if (config) {
|
|
|
|
var client = this;
|
|
|
|
_.each(config.networks || [], function(n) {
|
|
|
|
client.connect(n);
|
|
|
|
});
|
|
|
|
}
|
2014-07-06 15:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Client.prototype.emit = function(event, data) {
|
|
|
|
if (this.sockets !== null) {
|
|
|
|
this.sockets.in(this.id).emit(event, data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.find = function(id) {
|
|
|
|
var network = null;
|
|
|
|
var chan = null;
|
|
|
|
for (var i in this.networks) {
|
|
|
|
var n = this.networks[i];
|
|
|
|
chan = _.find(n.channels, {id: id});
|
|
|
|
if (chan) {
|
|
|
|
network = n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (network && chan) {
|
|
|
|
return {
|
|
|
|
network: network,
|
|
|
|
chan: chan
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.connect = function(args) {
|
|
|
|
var client = this;
|
2014-09-09 22:11:51 +00:00
|
|
|
var server = _.defaults(_.pick(args, ['host', 'port', 'rejectUnauthorized', 'name']), {
|
2014-09-08 21:24:20 +00:00
|
|
|
host: "irc.freenode.org",
|
2014-09-10 15:56:19 +00:00
|
|
|
port: 6697,
|
|
|
|
rejectUnauthorized: false
|
2014-09-08 21:24:20 +00:00
|
|
|
});
|
2014-07-18 14:40:08 +00:00
|
|
|
|
2014-07-19 01:31:00 +00:00
|
|
|
var stream = args.tls ? tls.connect(server) : net.connect(server);
|
2014-07-06 15:22:43 +00:00
|
|
|
stream.on("error", function(e) {
|
2014-09-10 15:56:19 +00:00
|
|
|
console.log("Client#connect():\n" + e);
|
|
|
|
stream.end();
|
|
|
|
var msg = new Msg({
|
|
|
|
type: Msg.Type.ERROR,
|
|
|
|
text: "Connection error."
|
|
|
|
});
|
|
|
|
client.emit("msg", {
|
|
|
|
msg: msg
|
|
|
|
});
|
2014-07-06 15:22:43 +00:00
|
|
|
});
|
2014-07-18 14:40:08 +00:00
|
|
|
|
2014-07-19 01:31:00 +00:00
|
|
|
var nick = args.nick || "shout-user";
|
|
|
|
var realname = args.realname || "Shout User";
|
2014-07-18 14:40:08 +00:00
|
|
|
|
2014-07-06 15:22:43 +00:00
|
|
|
var irc = slate(stream);
|
2014-09-08 21:24:20 +00:00
|
|
|
|
|
|
|
if (args.password) {
|
|
|
|
irc.pass(args.password);
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:50:41 +00:00
|
|
|
irc.me = nick;
|
|
|
|
irc.nick(nick);
|
|
|
|
irc.user(nick, realname);
|
2014-07-18 14:40:08 +00:00
|
|
|
|
2014-07-06 15:22:43 +00:00
|
|
|
var network = new Network({
|
2014-07-19 01:31:00 +00:00
|
|
|
host: server.host,
|
2014-09-08 19:35:50 +00:00
|
|
|
name: server.name,
|
2014-07-06 15:22:43 +00:00
|
|
|
irc: irc
|
|
|
|
});
|
2014-07-18 14:40:08 +00:00
|
|
|
|
2014-07-06 15:22:43 +00:00
|
|
|
client.networks.push(network);
|
|
|
|
client.emit("network", {
|
|
|
|
network: network
|
|
|
|
});
|
2014-07-18 14:40:08 +00:00
|
|
|
|
2014-07-06 15:22:43 +00:00
|
|
|
events.forEach(function(plugin) {
|
2014-07-19 01:31:00 +00:00
|
|
|
var path = "./plugins/irc-events/" + plugin;
|
|
|
|
require(path).apply(client, [
|
2014-07-08 20:50:41 +00:00
|
|
|
irc,
|
|
|
|
network
|
|
|
|
]);
|
2014-07-06 15:22:43 +00:00
|
|
|
});
|
2014-07-18 14:40:08 +00:00
|
|
|
|
2014-09-09 19:31:23 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-07-08 20:50:41 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-09 19:31:23 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:50:41 +00:00
|
|
|
Client.prototype.quit = function() {
|
|
|
|
this.networks.forEach(function(network) {
|
|
|
|
var irc = network.irc;
|
|
|
|
if (network.connected) {
|
2014-07-19 01:31:00 +00:00
|
|
|
irc.quit();
|
2014-07-08 20:50:41 +00:00
|
|
|
} else {
|
|
|
|
irc.stream.end();
|
|
|
|
}
|
2014-07-06 15:22:43 +00:00
|
|
|
});
|
|
|
|
};
|