thelounge/src/client.js

415 lines
8.2 KiB
JavaScript
Raw Normal View History

2014-09-13 23:29:45 +02:00
var _ = require("lodash");
2014-09-16 22:06:13 +02:00
var Chan = require("./models/chan");
2014-09-15 23:13:03 +02:00
var crypto = require("crypto");
2014-10-11 19:33:28 +02:00
var identd = require("./identd");
2014-09-16 21:47:01 +02:00
var log = require("./log");
2014-09-13 23:29:45 +02:00
var net = require("net");
var Msg = require("./models/msg");
var Network = require("./models/network");
var slate = require("slate-irc");
var tls = require("tls");
var Helper = require("./helper");
2014-09-13 23:29:45 +02:00
module.exports = Client;
var id = 0;
var events = [
"ctcp",
"error",
2016-02-12 12:24:13 +01:00
"invite",
2014-09-13 23:29:45 +02:00
"join",
"kick",
"mode",
"motd",
"message",
2014-09-27 21:17:05 +02:00
"link",
2014-09-13 23:29:45 +02:00
"names",
"nick",
"notice",
"part",
"quit",
"topic",
"welcome",
"whois"
];
var inputs = [
2016-03-06 10:24:56 +01:00
// These inputs are sorted in order that is most likely to be used
"msg",
"part",
2014-09-13 23:29:45 +02:00
"action",
"connect",
"invite",
"kick",
"mode",
"notice",
"query",
2014-09-13 23:29:45 +02:00
"quit",
"raw",
"topic",
].reduce(function(plugins, name) {
var path = "./plugins/inputs/" + name;
var plugin = require(path);
plugin.commands.forEach(function(command) {
plugins[command] = plugin.input;
});
return plugins;
}, {});
2014-09-13 23:29:45 +02:00
function Client(manager, name, config) {
2014-09-13 23:29:45 +02:00
_.merge(this, {
activeChannel: -1,
2014-09-13 23:29:45 +02:00
config: config,
id: id++,
2014-09-16 21:47:01 +02:00
name: name,
2014-09-13 23:29:45 +02:00
networks: [],
sockets: manager.sockets,
manager: manager
2014-09-13 23:29:45 +02:00
});
2014-09-15 23:13:03 +02:00
var client = this;
crypto.randomBytes(48, function(err, buf) {
client.token = buf.toString("hex");
});
2014-09-13 23:29:45 +02: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-10-14 21:25:04 +02:00
var config = this.config || {};
if (config.log === true) {
2015-10-01 00:39:57 +02:00
if (event === "msg") {
2014-09-16 21:47:01 +02:00
var target = this.find(data.chan);
if (target) {
2014-09-16 22:06:13 +02:00
var chan = target.chan.name;
2015-10-01 00:39:57 +02:00
if (target.chan.type === Chan.Type.LOBBY) {
2014-09-16 22:06:13 +02:00
chan = target.network.host;
}
log.write(
this.name,
target.network.host,
chan,
data.msg
);
2014-09-16 21:47:01 +02:00
}
}
}
2014-09-13 19:18:42 +02:00
};
2014-09-13 23:29:45 +02: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) {
2014-10-11 08:17:41 +02:00
var config = Helper.getConfig();
2014-09-13 23:29:45 +02:00
var client = this;
2016-02-21 13:02:35 +01:00
if (config.lockNetwork) {
// This check is needed to prevent invalid user configurations
if (args.host && args.host.length > 0 && args.host !== config.defaults.host) {
var invalidHostnameMsg = new Msg({
type: Msg.Type.ERROR,
text: "Hostname you specified is not allowed."
});
client.emit("msg", {
msg: invalidHostnameMsg
});
return;
}
args.host = config.defaults.host;
args.port = config.defaults.port;
args.tls = config.defaults.tls;
}
2014-09-13 23:29:45 +02:00
var server = {
2014-10-11 22:44:56 +02:00
name: args.name || "",
2016-02-21 13:02:35 +01:00
host: args.host || "",
port: parseInt(args.port, 10) || (args.tls ? 6697 : 6667),
2014-09-13 23:29:45 +02:00
rejectUnauthorized: false
};
2016-02-21 13:02:35 +01:00
if (server.host.length === 0) {
var emptyHostnameMsg = new Msg({
type: Msg.Type.ERROR,
text: "You must specify a hostname to connect."
});
client.emit("msg", {
msg: emptyHostnameMsg
});
return;
}
2014-10-11 19:33:28 +02:00
if (config.bind) {
2014-10-11 08:17:41 +02:00
server.localAddress = config.bind;
2015-10-01 00:39:57 +02:00
if (args.tls) {
2014-10-11 08:17:41 +02:00
var socket = net.connect(server);
server.socket = socket;
}
}
2014-11-09 17:01:22 +01:00
2014-09-13 23:29:45 +02:00
var stream = args.tls ? tls.connect(server) : net.connect(server);
2014-11-09 17:01:22 +01:00
2014-10-16 23:33:07 +02:00
stream.on("error", function(e) {
2014-09-13 23:29:45 +02: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
});
});
var nick = args.nick || "lounge-user";
2015-10-01 00:39:57 +02:00
var username = args.username || nick.replace(/[^a-zA-Z0-9]/g, "");
var realname = args.realname || "The Lounge User";
2014-09-13 23:29:45 +02:00
var irc = slate(stream);
2014-10-11 19:33:28 +02:00
identd.hook(stream, username);
2014-09-13 23:29:45 +02:00
if (args.password) {
irc.pass(args.password);
}
irc.me = nick;
irc.nick(nick);
irc.user(username, realname);
2014-09-13 23:29:45 +02:00
var network = new Network({
name: server.name,
2014-10-11 22:44:56 +02:00
host: server.host,
port: server.port,
2014-10-12 00:47:24 +02:00
tls: !!args.tls,
2014-10-11 22:44:56 +02:00
password: args.password,
username: username,
realname: realname,
2014-11-09 17:01:22 +01:00
commands: args.commands
2014-09-13 23:29:45 +02:00
});
2014-10-11 22:44:56 +02:00
network.irc = irc;
2014-09-13 23:29:45 +02:00
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.setPassword = function(hash) {
var client = this;
client.manager.updateUser(client.name, {password:hash});
// re-read the hash off disk to ensure we use whatever is saved. this will
// prevent situations where the password failed to save properly and so
// a restart of the server would forget the change and use the old
// password again.
var user = client.manager.readUserConfig(client.name);
if (user.password === hash) {
client.config.password = hash;
return true;
}
return false;
};
2014-09-13 23:29:45 +02:00
Client.prototype.input = function(data) {
var client = this;
2014-09-27 17:56:23 +02:00
var text = data.text.trim();
2014-09-13 23:29:45 +02:00
var target = client.find(data.target);
2016-03-06 10:24:56 +01:00
// 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);
2014-09-13 23:29:45 +02:00
}
2016-03-06 10:24:56 +01:00
2014-09-13 23:29:45 +02:00
var args = text.split(" ");
2016-03-06 10:24:56 +01:00
var cmd = args.shift().toLowerCase();
if (cmd in inputs) {
inputs[cmd].apply(client, [target.network, target.chan, cmd, args]);
} else {
2016-03-06 10:24:56 +01:00
target.network.irc.write(text);
}
2014-09-13 23:29:45 +02:00
};
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
});
};
Client.prototype.open = function(data) {
var target = this.find(data);
if (target) {
target.chan.unread = 0;
target.chan.highlight = false;
this.activeChannel = target.chan.id;
}
};
2014-09-24 21:42:36 +02:00
Client.prototype.sort = function(data) {
var self = this;
var type = data.type;
var order = data.order || [];
2015-10-01 00:39:57 +02:00
var sorted = [];
2014-09-24 21:42:36 +02:00
switch (type) {
case "networks":
_.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;
}
_.each(order, function(i) {
var find = _.find(network.channels, {id: i});
if (find) {
sorted.push(find);
}
});
network.channels = sorted;
break;
}
};
Client.prototype.names = function(data) {
var client = this;
var target = client.find(data.target);
if (!target) {
return;
}
client.emit("names", {
chan: target.chan.id,
users: target.chan.users
});
};
2014-09-13 23:29:45 +02:00
Client.prototype.quit = function() {
2014-09-29 17:49:38 +02:00
var sockets = this.sockets.sockets;
var room = sockets.adapter.rooms[this.id] || [];
for (var user in room) {
var socket = sockets.adapter.nsp.connected[user];
if (socket) {
socket.disconnect();
}
}
2014-09-13 23:29:45 +02:00
this.networks.forEach(function(network) {
var irc = network.irc;
if (network.connected) {
irc.quit();
} else {
irc.stream.end();
}
});
2014-09-13 19:18:42 +02:00
};
2014-10-11 22:44:56 +02:00
2014-10-12 02:20:30 +02:00
var timer;
Client.prototype.save = function(force) {
var client = this;
2014-10-12 07:15:03 +02:00
var config = Helper.getConfig();
2015-10-01 00:39:57 +02:00
if (config.public) {
2014-10-12 07:15:03 +02:00
return;
}
2014-11-09 17:01:22 +01:00
2014-10-12 02:20:30 +02:00
if (!force) {
clearTimeout(timer);
timer = setTimeout(function() {
client.save(true);
}, 1000);
return;
}
2014-11-09 17:01:22 +01:00
2014-10-12 00:47:24 +02:00
var networks = _.map(
this.networks,
function(n) {
return n.export();
}
);
2014-11-09 17:01:22 +01:00
2014-10-12 00:47:24 +02:00
var json = {};
json.networks = networks;
client.manager.updateUser(client.name, json);
2014-10-11 22:44:56 +02:00
};