2014-09-13 21:29:45 +00:00
|
|
|
var _ = require("lodash");
|
|
|
|
var config = require("../config");
|
2014-09-16 20:06:13 +00:00
|
|
|
var Chan = require("./models/chan");
|
2014-09-15 21:13:03 +00:00
|
|
|
var crypto = require("crypto");
|
2014-09-16 19:47:01 +00:00
|
|
|
var log = require("./log");
|
2014-09-13 21:29:45 +00:00
|
|
|
var net = require("net");
|
|
|
|
var Msg = require("./models/msg");
|
|
|
|
var Network = require("./models/network");
|
|
|
|
var slate = require("slate-irc");
|
|
|
|
var tls = require("tls");
|
|
|
|
|
|
|
|
module.exports = Client;
|
|
|
|
|
|
|
|
var id = 0;
|
|
|
|
var events = [
|
|
|
|
"ctcp",
|
|
|
|
"error",
|
|
|
|
"image",
|
|
|
|
"join",
|
|
|
|
"kick",
|
|
|
|
"mode",
|
|
|
|
"motd",
|
|
|
|
"message",
|
|
|
|
"names",
|
|
|
|
"nick",
|
|
|
|
"notice",
|
|
|
|
"part",
|
|
|
|
"quit",
|
|
|
|
"topic",
|
|
|
|
"welcome",
|
|
|
|
"whois"
|
|
|
|
];
|
|
|
|
var inputs = [
|
|
|
|
"action",
|
|
|
|
"connect",
|
|
|
|
"invite",
|
|
|
|
"join",
|
|
|
|
"kick",
|
|
|
|
"mode",
|
|
|
|
"msg",
|
|
|
|
"nick",
|
|
|
|
"notice",
|
|
|
|
"part",
|
|
|
|
"quit",
|
|
|
|
"raw",
|
|
|
|
"topic",
|
|
|
|
"whois"
|
|
|
|
];
|
|
|
|
|
2014-09-16 19:47:01 +00:00
|
|
|
function Client(sockets, name, config) {
|
2014-09-13 21:29:45 +00:00
|
|
|
_.merge(this, {
|
2014-09-21 16:46:43 +00:00
|
|
|
activeChannel: -1,
|
2014-09-13 21:29:45 +00:00
|
|
|
config: config,
|
|
|
|
id: id++,
|
2014-09-16 19:47:01 +00:00
|
|
|
name: name,
|
2014-09-13 21:29:45 +00:00
|
|
|
networks: [],
|
|
|
|
sockets: sockets
|
|
|
|
});
|
2014-09-15 21:13:03 +00:00
|
|
|
var client = this;
|
|
|
|
crypto.randomBytes(48, function(err, buf) {
|
|
|
|
client.token = buf.toString("hex");
|
|
|
|
});
|
2014-09-13 21:29:45 +00:00
|
|
|
if (config) {
|
|
|
|
var delay = 0;
|
|
|
|
(config.networks || []).forEach(function(n) {
|
|
|
|
setTimeout(function() {
|
|
|
|
client.connect(n);
|
|
|
|
}, delay);
|
|
|
|
delay += 1000;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Client.prototype.emit = function(event, data) {
|
|
|
|
if (this.sockets !== null) {
|
|
|
|
this.sockets.in(this.id).emit(event, data);
|
|
|
|
}
|
2014-09-16 20:39:19 +00:00
|
|
|
if ((this.config || {}).log === true) {
|
2014-09-16 19:47:01 +00:00
|
|
|
if (event == "msg") {
|
|
|
|
var target = this.find(data.chan);
|
|
|
|
if (target) {
|
2014-09-16 20:06:13 +00:00
|
|
|
var chan = target.chan.name;
|
|
|
|
if (target.chan.type == Chan.Type.LOBBY) {
|
|
|
|
chan = target.network.host;
|
|
|
|
}
|
|
|
|
log.write(
|
|
|
|
this.name,
|
|
|
|
target.network.host,
|
|
|
|
chan,
|
|
|
|
data.msg
|
|
|
|
);
|
2014-09-16 19:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-13 17:18:42 +00:00
|
|
|
};
|
2014-09-13 21:29:45 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
var server = {
|
|
|
|
host: args.host || "irc.freenode.org",
|
|
|
|
port: args.port || (args.tls ? 6697 : 6667),
|
|
|
|
name: args.name || "",
|
|
|
|
rejectUnauthorized: false
|
|
|
|
};
|
|
|
|
|
|
|
|
var stream = args.tls ? tls.connect(server) : net.connect(server);
|
|
|
|
stream.on("error", function(e) {
|
|
|
|
console.log("Client#connect():\n" + e);
|
|
|
|
stream.end();
|
|
|
|
var msg = new Msg({
|
|
|
|
type: Msg.Type.ERROR,
|
|
|
|
text: "Connection error."
|
|
|
|
});
|
|
|
|
client.emit("msg", {
|
|
|
|
msg: msg
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var nick = args.nick || "shout-user";
|
2014-09-24 11:03:58 +00:00
|
|
|
var username = args.username || nick;
|
2014-09-13 21:29:45 +00:00
|
|
|
var realname = args.realname || "Shout User";
|
|
|
|
|
|
|
|
var irc = slate(stream);
|
|
|
|
|
|
|
|
if (args.password) {
|
|
|
|
irc.pass(args.password);
|
|
|
|
}
|
|
|
|
|
|
|
|
irc.me = nick;
|
|
|
|
irc.nick(nick);
|
2014-09-24 11:03:58 +00:00
|
|
|
irc.user(username, realname);
|
2014-09-13 21:29:45 +00:00
|
|
|
|
|
|
|
var network = new Network({
|
|
|
|
host: server.host,
|
|
|
|
name: server.name,
|
|
|
|
irc: irc
|
|
|
|
});
|
|
|
|
|
|
|
|
client.networks.push(network);
|
|
|
|
client.emit("network", {
|
|
|
|
network: network
|
|
|
|
});
|
|
|
|
|
|
|
|
events.forEach(function(plugin) {
|
|
|
|
var path = "./plugins/irc-events/" + plugin;
|
|
|
|
require(path).apply(client, [
|
|
|
|
irc,
|
|
|
|
network
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
2014-09-27 15:56:23 +00:00
|
|
|
var text = data.text.trim();
|
2014-09-13 21:29:45 +00:00
|
|
|
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.more = function(data) {
|
|
|
|
var client = this;
|
|
|
|
var target = client.find(data.target);
|
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var chan = target.chan;
|
|
|
|
var count = chan.messages.length - (data.count || 0);
|
|
|
|
var messages = chan.messages.slice(Math.max(0, count - 100), count);
|
|
|
|
client.emit("more", {
|
|
|
|
chan: chan.id,
|
|
|
|
messages: messages
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-21 16:46:43 +00:00
|
|
|
Client.prototype.open = function(data) {
|
|
|
|
var target = this.find(data);
|
|
|
|
if (target) {
|
|
|
|
target.chan.unread = 0;
|
|
|
|
this.activeChannel = target.chan.id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-24 19:42:36 +00:00
|
|
|
Client.prototype.sort = function(data) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var type = data.type;
|
|
|
|
var order = data.order || [];
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case "networks":
|
|
|
|
var sorted = [];
|
|
|
|
_.each(order, function(i) {
|
|
|
|
var find = _.find(self.networks, {id: i});
|
|
|
|
if (find) {
|
|
|
|
sorted.push(find);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.networks = sorted;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "channels":
|
|
|
|
var target = data.target;
|
|
|
|
var network = _.find(self.networks, {id: target});
|
|
|
|
if (!network) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var sorted = [];
|
|
|
|
_.each(order, function(i) {
|
|
|
|
var find = _.find(network.channels, {id: i});
|
|
|
|
if (find) {
|
|
|
|
sorted.push(find);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
network.channels = sorted;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
Client.prototype.quit = function() {
|
2014-09-24 22:23:54 +00:00
|
|
|
this.sockets.in(this.id).sockets.forEach(function(socket) {
|
|
|
|
socket.disconnect(true);
|
|
|
|
});
|
2014-09-13 21:29:45 +00:00
|
|
|
this.networks.forEach(function(network) {
|
|
|
|
var irc = network.irc;
|
|
|
|
if (network.connected) {
|
|
|
|
irc.quit();
|
|
|
|
} else {
|
|
|
|
irc.stream.end();
|
|
|
|
}
|
|
|
|
});
|
2014-09-13 17:18:42 +00:00
|
|
|
};
|