2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const _ = require("lodash");
|
2018-06-15 20:31:06 +00:00
|
|
|
const log = require("./log");
|
2018-03-02 18:28:54 +00:00
|
|
|
const colors = require("chalk");
|
2018-01-11 11:33:36 +00:00
|
|
|
const Chan = require("./models/chan");
|
|
|
|
const crypto = require("crypto");
|
|
|
|
const Msg = require("./models/msg");
|
|
|
|
const Network = require("./models/network");
|
|
|
|
const Helper = require("./helper");
|
2017-06-21 07:58:29 +00:00
|
|
|
const UAParser = require("ua-parser-js");
|
2018-04-10 13:15:44 +00:00
|
|
|
const uuidv4 = require("uuid/v4");
|
2019-01-16 09:23:12 +00:00
|
|
|
const escapeRegExp = require("lodash/escapeRegExp");
|
2019-07-02 16:02:02 +00:00
|
|
|
const inputs = require("./plugins/inputs");
|
2019-07-05 14:02:57 +00:00
|
|
|
const PublicClient = require("./plugins/packages/publicClient");
|
2014-09-13 21:29:45 +00:00
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
const MessageStorage = require("./plugins/messageStorage/sqlite");
|
|
|
|
const TextFileMessageStorage = require("./plugins/messageStorage/text");
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
module.exports = Client;
|
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const events = [
|
2017-07-10 16:01:20 +00:00
|
|
|
"away",
|
2016-03-08 18:50:48 +00:00
|
|
|
"connection",
|
2016-04-24 15:12:54 +00:00
|
|
|
"unhandled",
|
2014-09-13 21:29:45 +00:00
|
|
|
"ctcp",
|
2017-09-19 15:22:50 +00:00
|
|
|
"chghost",
|
2014-09-13 21:29:45 +00:00
|
|
|
"error",
|
2016-02-12 11:24:13 +00:00
|
|
|
"invite",
|
2014-09-13 21:29:45 +00:00
|
|
|
"join",
|
|
|
|
"kick",
|
|
|
|
"mode",
|
2019-04-14 11:44:44 +00:00
|
|
|
"modelist",
|
2014-09-13 21:29:45 +00:00
|
|
|
"motd",
|
|
|
|
"message",
|
|
|
|
"names",
|
|
|
|
"nick",
|
|
|
|
"part",
|
|
|
|
"quit",
|
|
|
|
"topic",
|
|
|
|
"welcome",
|
2016-03-09 20:04:07 +00:00
|
|
|
"list",
|
2017-11-15 06:35:15 +00:00
|
|
|
"whois",
|
2014-09-13 21:29:45 +00:00
|
|
|
];
|
|
|
|
|
2017-11-22 06:39:32 +00:00
|
|
|
function Client(manager, name, config = {}) {
|
2014-09-13 21:29:45 +00:00
|
|
|
_.merge(this, {
|
2017-04-28 15:58:14 +00:00
|
|
|
awayMessage: config.awayMessage || "",
|
2016-09-25 06:41:10 +00:00
|
|
|
lastActiveChannel: -1,
|
|
|
|
attachedClients: {},
|
2014-09-13 21:29:45 +00:00
|
|
|
config: config,
|
2018-04-10 13:15:44 +00:00
|
|
|
id: uuidv4(),
|
2018-04-27 10:16:23 +00:00
|
|
|
idChan: 1,
|
|
|
|
idMsg: 1,
|
2014-09-16 19:47:01 +00:00
|
|
|
name: name,
|
2014-09-13 21:29:45 +00:00
|
|
|
networks: [],
|
2016-02-17 00:14:43 +00:00
|
|
|
sockets: manager.sockets,
|
2017-11-15 06:35:15 +00:00
|
|
|
manager: manager,
|
2018-04-17 08:06:08 +00:00
|
|
|
messageStorage: [],
|
2019-01-16 09:23:12 +00:00
|
|
|
highlightRegex: null,
|
2014-09-13 21:29:45 +00:00
|
|
|
});
|
2016-05-31 21:28:31 +00:00
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const client = this;
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
if (!Helper.config.public && client.config.log) {
|
|
|
|
if (Helper.config.messageStorage.includes("sqlite")) {
|
|
|
|
client.messageStorage.push(new MessageStorage(client));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Helper.config.messageStorage.includes("text")) {
|
|
|
|
client.messageStorage.push(new TextFileMessageStorage(client));
|
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
for (const messageStorage of client.messageStorage) {
|
|
|
|
messageStorage.enable();
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 09:24:19 +00:00
|
|
|
(client.config.networks || []).forEach((network) => client.connect(network, true));
|
|
|
|
|
|
|
|
// Networks are stored directly in the client object
|
|
|
|
// We don't need to keep it in the config object
|
|
|
|
delete client.config.networks;
|
2016-04-16 11:32:38 +00:00
|
|
|
|
2017-06-21 07:58:29 +00:00
|
|
|
if (typeof client.config.sessions !== "object") {
|
|
|
|
client.config.sessions = {};
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:23:12 +00:00
|
|
|
if (typeof client.config.clientSettings !== "object") {
|
|
|
|
client.config.clientSettings = {};
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:51:22 +00:00
|
|
|
if (typeof client.config.browser !== "object") {
|
|
|
|
client.config.browser = {};
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:23:12 +00:00
|
|
|
client.compileCustomHighlights();
|
|
|
|
|
2017-07-10 19:47:03 +00:00
|
|
|
_.forOwn(client.config.sessions, (session) => {
|
|
|
|
if (session.pushSubscription) {
|
|
|
|
this.registerPushSubscription(session, session.pushSubscription, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-30 13:06:07 +00:00
|
|
|
if (client.name) {
|
2016-12-11 08:29:09 +00:00
|
|
|
log.info(`User ${colors.bold(client.name)} loaded`);
|
2019-10-31 09:24:19 +00:00
|
|
|
|
|
|
|
// Networks are created instantly, but to reduce server load on startup
|
|
|
|
// We randomize the IRC connections and channel log loading
|
|
|
|
let delay = manager.clients.length * 500;
|
|
|
|
client.networks.forEach((network) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
network.channels.forEach((channel) => channel.loadMessages(client, network));
|
|
|
|
|
|
|
|
if (!network.userDisconnected && network.irc) {
|
|
|
|
network.irc.connect();
|
|
|
|
}
|
|
|
|
}, delay);
|
|
|
|
|
|
|
|
delay += 1000 + Math.floor(Math.random() * 1000);
|
|
|
|
});
|
2016-06-19 08:01:50 +00:00
|
|
|
}
|
2014-09-13 21:29:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 10:16:23 +00:00
|
|
|
Client.prototype.createChannel = function(attr) {
|
|
|
|
const chan = new Chan(attr);
|
|
|
|
chan.id = this.idChan++;
|
|
|
|
|
|
|
|
return chan;
|
|
|
|
};
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
Client.prototype.emit = function(event, data) {
|
|
|
|
if (this.sockets !== null) {
|
|
|
|
this.sockets.in(this.id).emit(event, data);
|
|
|
|
}
|
2014-09-13 17:18:42 +00:00
|
|
|
};
|
2014-09-13 21:29:45 +00:00
|
|
|
|
2016-10-09 08:54:44 +00:00
|
|
|
Client.prototype.find = function(channelId) {
|
2018-01-11 11:33:36 +00:00
|
|
|
let network = null;
|
|
|
|
let chan = null;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
for (const i in this.networks) {
|
|
|
|
const n = this.networks[i];
|
2016-10-09 08:54:44 +00:00
|
|
|
chan = _.find(n.channels, {id: channelId});
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
if (chan) {
|
|
|
|
network = n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
if (network && chan) {
|
2018-03-05 00:59:16 +00:00
|
|
|
return {network, chan};
|
2014-09-13 21:29:45 +00:00
|
|
|
}
|
2016-10-09 08:54:44 +00:00
|
|
|
|
|
|
|
return false;
|
2014-09-13 21:29:45 +00:00
|
|
|
};
|
|
|
|
|
2019-10-31 09:24:19 +00:00
|
|
|
Client.prototype.connect = function(args, isStartup = false) {
|
2018-01-11 11:33:36 +00:00
|
|
|
const client = this;
|
|
|
|
let channels = [];
|
2016-06-17 10:46:15 +00:00
|
|
|
|
2018-04-27 10:16:23 +00:00
|
|
|
// Get channel id for lobby before creating other channels for nicer ids
|
|
|
|
const lobbyChannelId = client.idChan++;
|
|
|
|
|
2016-06-19 17:12:42 +00:00
|
|
|
if (args.channels) {
|
2018-01-11 11:33:36 +00:00
|
|
|
let badName = false;
|
2016-06-19 17:12:42 +00:00
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
args.channels.forEach((chan) => {
|
2016-06-19 17:12:42 +00:00
|
|
|
if (!chan.name) {
|
|
|
|
badName = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
channels.push(
|
|
|
|
client.createChannel({
|
|
|
|
name: chan.name,
|
|
|
|
key: chan.key || "",
|
|
|
|
type: chan.type,
|
|
|
|
})
|
|
|
|
);
|
2016-06-17 10:46:15 +00:00
|
|
|
});
|
2016-06-19 17:12:42 +00:00
|
|
|
|
|
|
|
if (badName && client.name) {
|
2019-07-17 09:33:59 +00:00
|
|
|
log.warn(
|
|
|
|
"User '" +
|
|
|
|
client.name +
|
|
|
|
"' on network '" +
|
|
|
|
args.name +
|
|
|
|
"' has an invalid channel which has been ignored"
|
|
|
|
);
|
2016-06-19 17:12:42 +00:00
|
|
|
}
|
2019-07-17 09:33:59 +00:00
|
|
|
// `join` is kept for backwards compatibility when updating from versions <2.0
|
|
|
|
// also used by the "connect" window
|
2016-06-19 17:12:42 +00:00
|
|
|
} else if (args.join) {
|
|
|
|
channels = args.join
|
2016-10-09 08:54:44 +00:00
|
|
|
.replace(/,/g, " ")
|
2016-06-19 17:12:42 +00:00
|
|
|
.split(/\s+/g)
|
2018-04-22 18:04:59 +00:00
|
|
|
.map((chan) => {
|
|
|
|
if (!chan.match(/^[#&!+]/)) {
|
|
|
|
chan = `#${chan}`;
|
|
|
|
}
|
|
|
|
|
2018-04-27 10:16:23 +00:00
|
|
|
return client.createChannel({
|
2017-11-15 06:35:15 +00:00
|
|
|
name: chan,
|
2016-06-19 17:12:42 +00:00
|
|
|
});
|
|
|
|
});
|
2016-06-17 10:46:15 +00:00
|
|
|
}
|
2016-03-07 21:09:42 +00:00
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const network = new Network({
|
2017-11-28 17:25:15 +00:00
|
|
|
uuid: args.uuid,
|
2019-07-17 09:33:59 +00:00
|
|
|
name: String(
|
|
|
|
args.name || (Helper.config.displayNetwork ? "" : Helper.config.defaults.name) || ""
|
|
|
|
),
|
2018-03-15 08:37:05 +00:00
|
|
|
host: String(args.host || ""),
|
|
|
|
port: parseInt(args.port, 10),
|
2016-03-07 21:09:42 +00:00
|
|
|
tls: !!args.tls,
|
2018-08-25 09:11:59 +00:00
|
|
|
userDisconnected: !!args.userDisconnected,
|
2018-03-05 18:11:41 +00:00
|
|
|
rejectUnauthorized: !!args.rejectUnauthorized,
|
2018-03-15 08:37:05 +00:00
|
|
|
password: String(args.password || ""),
|
|
|
|
nick: String(args.nick || ""),
|
|
|
|
username: String(args.username || ""),
|
|
|
|
realname: String(args.realname || ""),
|
|
|
|
commands: args.commands || [],
|
2016-06-17 10:46:15 +00:00
|
|
|
channels: channels,
|
2018-07-11 07:57:02 +00:00
|
|
|
ignoreList: args.ignoreList ? args.ignoreList : [],
|
2016-03-07 21:09:42 +00:00
|
|
|
});
|
|
|
|
|
2018-04-27 10:16:23 +00:00
|
|
|
// Set network lobby channel id
|
|
|
|
network.channels[0].id = lobbyChannelId;
|
|
|
|
|
2016-03-07 21:09:42 +00:00
|
|
|
client.networks.push(network);
|
|
|
|
client.emit("network", {
|
2017-11-29 19:54:09 +00:00
|
|
|
networks: [network.getFilteredClone(this.lastActiveChannel, -1)],
|
2016-03-07 21:09:42 +00:00
|
|
|
});
|
|
|
|
|
2018-03-15 08:37:05 +00:00
|
|
|
if (!network.validate(client)) {
|
2016-02-21 12:02:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-15 08:37:05 +00:00
|
|
|
network.createIrcFramework(client);
|
2018-03-10 11:25:56 +00:00
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
events.forEach((plugin) => {
|
2019-07-17 09:33:59 +00:00
|
|
|
require(`./plugins/irc-events/${plugin}`).apply(client, [network.irc, network]);
|
2017-02-02 20:52:37 +00:00
|
|
|
});
|
|
|
|
|
2018-08-25 09:11:59 +00:00
|
|
|
if (network.userDisconnected) {
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
|
|
|
text:
|
|
|
|
"You have manually disconnected from this network before, use the /connect command to connect again.",
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2019-10-31 09:24:19 +00:00
|
|
|
} else if (!isStartup) {
|
2018-08-25 09:11:59 +00:00
|
|
|
network.irc.connect();
|
|
|
|
}
|
2017-02-02 20:52:37 +00:00
|
|
|
|
2019-10-31 09:24:19 +00:00
|
|
|
if (!isStartup) {
|
|
|
|
client.save();
|
|
|
|
channels.forEach((channel) => channel.loadMessages(client, network));
|
|
|
|
}
|
2014-09-13 21:29:45 +00:00
|
|
|
};
|
|
|
|
|
2017-06-21 07:58:29 +00:00
|
|
|
Client.prototype.generateToken = function(callback) {
|
2018-01-05 13:26:12 +00:00
|
|
|
crypto.randomBytes(64, (err, buf) => {
|
2016-10-09 08:54:44 +00:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2017-06-21 07:58:29 +00:00
|
|
|
callback(buf.toString("hex"));
|
2016-05-31 21:28:31 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-01-05 13:26:12 +00:00
|
|
|
Client.prototype.calculateTokenHash = function(token) {
|
2019-07-17 09:33:59 +00:00
|
|
|
return crypto
|
|
|
|
.createHash("sha512")
|
|
|
|
.update(token)
|
|
|
|
.digest("hex");
|
2018-01-05 13:26:12 +00:00
|
|
|
};
|
|
|
|
|
2017-06-21 07:58:29 +00:00
|
|
|
Client.prototype.updateSession = function(token, ip, request) {
|
|
|
|
const client = this;
|
|
|
|
const agent = UAParser(request.headers["user-agent"] || "");
|
|
|
|
let friendlyAgent = "";
|
|
|
|
|
2017-08-13 18:37:12 +00:00
|
|
|
if (agent.browser.name) {
|
2017-06-21 07:58:29 +00:00
|
|
|
friendlyAgent = `${agent.browser.name} ${agent.browser.major}`;
|
|
|
|
} else {
|
|
|
|
friendlyAgent = "Unknown browser";
|
|
|
|
}
|
|
|
|
|
2017-08-13 18:37:12 +00:00
|
|
|
if (agent.os.name) {
|
2018-04-08 10:28:49 +00:00
|
|
|
friendlyAgent += ` on ${agent.os.name}`;
|
|
|
|
|
|
|
|
if (agent.os.version) {
|
|
|
|
friendlyAgent += ` ${agent.os.version}`;
|
|
|
|
}
|
2017-06-21 07:58:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 08:09:19 +00:00
|
|
|
client.config.sessions[token] = _.assign(client.config.sessions[token], {
|
2017-06-21 07:58:29 +00:00
|
|
|
lastUse: Date.now(),
|
|
|
|
ip: ip,
|
|
|
|
agent: friendlyAgent,
|
2017-09-17 08:09:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
client.manager.updateUser(client.name, {
|
2017-11-15 06:35:15 +00:00
|
|
|
sessions: client.config.sessions,
|
2017-09-17 08:09:19 +00:00
|
|
|
});
|
2017-06-21 07:58:29 +00:00
|
|
|
};
|
|
|
|
|
2016-05-31 21:28:31 +00:00
|
|
|
Client.prototype.setPassword = function(hash, callback) {
|
2018-01-11 11:33:36 +00:00
|
|
|
const client = this;
|
2016-05-31 21:28:31 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
client.manager.updateUser(
|
|
|
|
client.name,
|
|
|
|
{
|
|
|
|
password: hash,
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(false);
|
|
|
|
}
|
2016-05-31 21:28:31 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
client.config.password = hash;
|
|
|
|
return callback(true);
|
|
|
|
}
|
|
|
|
);
|
2016-02-17 00:14:43 +00:00
|
|
|
};
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
Client.prototype.input = function(data) {
|
2018-01-11 11:33:36 +00:00
|
|
|
const client = this;
|
2017-04-08 12:34:31 +00:00
|
|
|
data.text.split("\n").forEach((line) => {
|
2016-06-05 02:48:41 +00:00
|
|
|
data.text = line;
|
|
|
|
client.inputLine(data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.inputLine = function(data) {
|
2018-01-11 11:33:36 +00:00
|
|
|
const client = this;
|
|
|
|
const target = client.find(data.target);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2016-09-25 06:41:10 +00:00
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sending a message to a channel is higher priority than merely opening one
|
|
|
|
// so that reloading the page will open this channel
|
|
|
|
this.lastActiveChannel = target.chan.id;
|
2016-03-06 09:24:56 +00:00
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
let text = data.text;
|
|
|
|
|
2016-03-06 09:24:56 +00:00
|
|
|
// This is either a normal message or a command escaped with a leading '/'
|
|
|
|
if (text.charAt(0) !== "/" || text.charAt(1) === "/") {
|
2017-03-11 18:09:37 +00:00
|
|
|
if (target.chan.type === Chan.Type.LOBBY) {
|
2019-07-17 09:33:59 +00:00
|
|
|
target.chan.pushMessage(
|
|
|
|
this,
|
|
|
|
new Msg({
|
|
|
|
type: Msg.Type.ERROR,
|
|
|
|
text: "Messages can not be sent to lobbies.",
|
|
|
|
})
|
|
|
|
);
|
2017-03-11 18:09:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-06 09:24:56 +00:00
|
|
|
text = "say " + text.replace(/^\//, "");
|
|
|
|
} else {
|
|
|
|
text = text.substr(1);
|
2014-09-13 21:29:45 +00:00
|
|
|
}
|
2016-03-06 09:24:56 +00:00
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const args = text.split(" ");
|
|
|
|
const cmd = args.shift().toLowerCase();
|
2016-03-06 09:24:56 +00:00
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const irc = target.network.irc;
|
|
|
|
let connected = irc && irc.connection && irc.connection.connected;
|
2016-04-03 09:58:59 +00:00
|
|
|
|
2019-07-05 14:02:57 +00:00
|
|
|
if (inputs.userInputs.has(cmd)) {
|
|
|
|
const plugin = inputs.userInputs.get(cmd);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-05 14:02:57 +00:00
|
|
|
if (typeof plugin.input === "function" && (connected || plugin.allowDisconnected)) {
|
2016-04-03 09:58:59 +00:00
|
|
|
connected = true;
|
2019-07-05 14:02:57 +00:00
|
|
|
plugin.input.apply(client, [target.network, target.chan, cmd, args]);
|
|
|
|
}
|
|
|
|
} else if (inputs.pluginCommands.has(cmd)) {
|
|
|
|
const plugin = inputs.pluginCommands.get(cmd);
|
|
|
|
|
|
|
|
if (typeof plugin.input === "function" && (connected || plugin.allowDisconnected)) {
|
|
|
|
connected = true;
|
2019-07-17 09:33:59 +00:00
|
|
|
plugin.input(
|
2019-10-22 18:03:54 +00:00
|
|
|
new PublicClient(client, plugin.packageInfo),
|
2019-07-17 09:33:59 +00:00
|
|
|
{network: target.network, chan: target.chan},
|
|
|
|
cmd,
|
|
|
|
args
|
|
|
|
);
|
2016-04-03 09:58:59 +00:00
|
|
|
}
|
|
|
|
} else if (connected) {
|
|
|
|
irc.raw(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!connected) {
|
2019-07-17 09:33:59 +00:00
|
|
|
target.chan.pushMessage(
|
|
|
|
this,
|
|
|
|
new Msg({
|
|
|
|
type: Msg.Type.ERROR,
|
|
|
|
text: "You are not connected to the IRC network, unable to send your command.",
|
|
|
|
})
|
|
|
|
);
|
2016-03-06 09:24:56 +00:00
|
|
|
}
|
2014-09-13 21:29:45 +00:00
|
|
|
};
|
|
|
|
|
2019-01-16 09:23:12 +00:00
|
|
|
Client.prototype.compileCustomHighlights = function() {
|
|
|
|
const client = this;
|
|
|
|
|
|
|
|
if (typeof client.config.clientSettings.highlights !== "string") {
|
|
|
|
client.highlightRegex = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we don't have empty string in the list of highlights
|
|
|
|
// otherwise, users get notifications for everything
|
|
|
|
const highlightsTokens = client.config.clientSettings.highlights
|
|
|
|
.split(",")
|
|
|
|
.map((highlight) => escapeRegExp(highlight.trim()))
|
|
|
|
.filter((highlight) => highlight.length > 0);
|
|
|
|
|
|
|
|
if (highlightsTokens.length === 0) {
|
|
|
|
client.highlightRegex = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
client.highlightRegex = new RegExp(
|
|
|
|
`(?:^|[ .,+!?|/:<>(){}'"@&~-])(?:${highlightsTokens.join("|")})(?:$|[ .,+!?|/:<>(){}'"-])`,
|
|
|
|
"i"
|
|
|
|
);
|
2019-01-16 09:23:12 +00:00
|
|
|
};
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
Client.prototype.more = function(data) {
|
2017-08-23 09:32:59 +00:00
|
|
|
const client = this;
|
|
|
|
const target = client.find(data.target);
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
if (!target) {
|
2018-01-07 13:04:37 +00:00
|
|
|
return null;
|
2014-09-13 21:29:45 +00:00
|
|
|
}
|
2017-08-23 09:32:59 +00:00
|
|
|
|
|
|
|
const chan = target.chan;
|
2017-09-12 13:05:40 +00:00
|
|
|
let messages = [];
|
|
|
|
let index = 0;
|
2017-08-23 09:32:59 +00:00
|
|
|
|
2017-09-12 13:05:40 +00:00
|
|
|
// If client requests -1, send last 100 messages
|
|
|
|
if (data.lastId < 0) {
|
|
|
|
index = chan.messages.length;
|
|
|
|
} else {
|
|
|
|
index = chan.messages.findIndex((val) => val.id === data.lastId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If requested id is not found, an empty array will be sent
|
|
|
|
if (index > 0) {
|
|
|
|
messages = chan.messages.slice(Math.max(0, index - 100), index);
|
|
|
|
}
|
2017-08-23 09:32:59 +00:00
|
|
|
|
2018-01-07 13:04:37 +00:00
|
|
|
return {
|
2014-09-13 21:29:45 +00:00
|
|
|
chan: chan.id,
|
2017-11-15 06:35:15 +00:00
|
|
|
messages: messages,
|
2019-10-17 10:27:15 +00:00
|
|
|
totalMessages: chan.messages.length,
|
2018-01-07 13:04:37 +00:00
|
|
|
};
|
2014-09-13 21:29:45 +00:00
|
|
|
};
|
|
|
|
|
2016-12-21 10:38:50 +00:00
|
|
|
Client.prototype.open = function(socketId, target) {
|
2018-10-21 08:05:05 +00:00
|
|
|
// Due to how socket.io works internally, normal events may arrive later than
|
|
|
|
// the disconnect event, and because we can't control this timing precisely,
|
|
|
|
// process this event normally even if there is no attached client anymore.
|
|
|
|
const attachedClient = this.attachedClients[socketId] || {};
|
|
|
|
|
2016-12-21 10:38:50 +00:00
|
|
|
// Opening a window like settings
|
|
|
|
if (target === null) {
|
2018-10-21 08:05:05 +00:00
|
|
|
attachedClient.openChannel = -1;
|
2016-12-21 10:38:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = this.find(target);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2016-09-25 06:41:10 +00:00
|
|
|
if (!target) {
|
|
|
|
return;
|
2014-09-21 16:46:43 +00:00
|
|
|
}
|
2016-09-25 06:41:10 +00:00
|
|
|
|
|
|
|
target.chan.unread = 0;
|
2017-09-03 15:57:07 +00:00
|
|
|
target.chan.highlight = 0;
|
2016-09-25 06:41:10 +00:00
|
|
|
|
2018-07-17 08:41:12 +00:00
|
|
|
if (target.chan.messages.length > 0) {
|
|
|
|
target.chan.firstUnread = target.chan.messages[target.chan.messages.length - 1].id;
|
|
|
|
}
|
|
|
|
|
2018-10-21 08:05:05 +00:00
|
|
|
attachedClient.openChannel = target.chan.id;
|
2016-09-25 06:41:10 +00:00
|
|
|
this.lastActiveChannel = target.chan.id;
|
|
|
|
|
|
|
|
this.emit("open", target.chan.id);
|
2014-09-21 16:46:43 +00:00
|
|
|
};
|
|
|
|
|
2014-09-24 19:42:36 +00:00
|
|
|
Client.prototype.sort = function(data) {
|
2017-04-22 10:25:36 +00:00
|
|
|
const order = data.order;
|
2014-09-24 19:42:36 +00:00
|
|
|
|
2017-04-22 10:25:36 +00:00
|
|
|
if (!_.isArray(order)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-24 19:42:36 +00:00
|
|
|
|
2017-04-22 10:25:36 +00:00
|
|
|
switch (data.type) {
|
2019-07-17 09:33:59 +00:00
|
|
|
case "networks":
|
|
|
|
this.networks.sort((a, b) => order.indexOf(a.uuid) - order.indexOf(b.uuid));
|
2017-04-22 10:25:36 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
// Sync order to connected clients
|
|
|
|
this.emit("sync_sort", {
|
|
|
|
order: this.networks.map((obj) => obj.uuid),
|
|
|
|
type: data.type,
|
|
|
|
});
|
2014-09-24 19:42:36 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
break;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
case "channels": {
|
|
|
|
const network = _.find(this.networks, {uuid: data.target});
|
2017-04-22 10:25:36 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (!network) {
|
|
|
|
return;
|
2018-08-30 17:16:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels.sort((a, b) => {
|
|
|
|
// Always sort lobby to the top regardless of what the client has sent
|
|
|
|
// Because there's a lot of code that presumes channels[0] is the lobby
|
|
|
|
if (a.type === Chan.Type.LOBBY) {
|
|
|
|
return -1;
|
|
|
|
} else if (b.type === Chan.Type.LOBBY) {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-22 10:25:36 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
return order.indexOf(a.id) - order.indexOf(b.id);
|
|
|
|
});
|
2017-04-22 10:25:36 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
// Sync order to connected clients
|
|
|
|
this.emit("sync_sort", {
|
|
|
|
order: network.channels.map((obj) => obj.id),
|
|
|
|
type: data.type,
|
|
|
|
target: network.uuid,
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-01-11 11:33:36 +00:00
|
|
|
}
|
2016-06-12 10:02:37 +00:00
|
|
|
|
2017-04-22 10:25:36 +00:00
|
|
|
this.save();
|
2014-09-24 19:42:36 +00:00
|
|
|
};
|
|
|
|
|
2016-02-17 02:29:44 +00:00
|
|
|
Client.prototype.names = function(data) {
|
2018-01-11 11:33:36 +00:00
|
|
|
const client = this;
|
|
|
|
const target = client.find(data.target);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2016-02-17 02:29:44 +00:00
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.emit("names", {
|
2016-03-23 10:15:44 +00:00
|
|
|
id: target.chan.id,
|
2017-11-16 20:32:03 +00:00
|
|
|
users: target.chan.getSortedUsers(target.network.irc),
|
2016-02-17 02:29:44 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-30 17:26:45 +00:00
|
|
|
Client.prototype.quit = function(signOut) {
|
2017-08-29 12:43:52 +00:00
|
|
|
const sockets = this.sockets.sockets;
|
|
|
|
const room = sockets.adapter.rooms[this.id];
|
|
|
|
|
|
|
|
if (room && room.sockets) {
|
|
|
|
for (const user in room.sockets) {
|
|
|
|
const socket = sockets.connected[user];
|
|
|
|
|
|
|
|
if (socket) {
|
2017-08-30 17:26:45 +00:00
|
|
|
if (signOut) {
|
|
|
|
socket.emit("sign-out");
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:43:52 +00:00
|
|
|
socket.disconnect();
|
|
|
|
}
|
2014-09-29 15:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-29 12:43:52 +00:00
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
this.networks.forEach((network) => {
|
2016-04-03 09:03:09 +00:00
|
|
|
if (network.irc) {
|
2017-08-18 19:04:16 +00:00
|
|
|
network.irc.quit(Helper.config.leaveMessage);
|
2016-04-03 09:03:09 +00:00
|
|
|
}
|
2017-08-11 12:02:58 +00:00
|
|
|
|
|
|
|
network.destroy();
|
2014-09-13 21:29:45 +00:00
|
|
|
});
|
2018-03-10 16:49:16 +00:00
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
for (const messageStorage of this.messageStorage) {
|
|
|
|
messageStorage.close();
|
2018-03-10 16:49:16 +00:00
|
|
|
}
|
2014-09-13 17:18:42 +00:00
|
|
|
};
|
2014-10-11 20:44:56 +00:00
|
|
|
|
2017-07-10 19:47:03 +00:00
|
|
|
Client.prototype.clientAttach = function(socketId, token) {
|
2018-01-11 11:33:36 +00:00
|
|
|
const client = this;
|
2016-11-19 20:34:05 +00:00
|
|
|
|
2016-12-18 09:24:50 +00:00
|
|
|
if (client.awayMessage && _.size(client.attachedClients) === 0) {
|
|
|
|
client.networks.forEach(function(network) {
|
|
|
|
// Only remove away on client attachment if
|
|
|
|
// there is no away message on this network
|
2018-03-09 10:33:24 +00:00
|
|
|
if (network.irc && !network.awayMessage) {
|
2016-12-18 09:24:50 +00:00
|
|
|
network.irc.raw("AWAY");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-05 00:59:16 +00:00
|
|
|
const openChannel = client.lastActiveChannel;
|
|
|
|
client.attachedClients[socketId] = {token, openChannel};
|
2016-09-25 06:41:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.clientDetach = function(socketId) {
|
2016-12-18 09:24:50 +00:00
|
|
|
const client = this;
|
|
|
|
|
2016-09-25 06:41:10 +00:00
|
|
|
delete this.attachedClients[socketId];
|
2016-12-18 09:24:50 +00:00
|
|
|
|
|
|
|
if (client.awayMessage && _.size(client.attachedClients) === 0) {
|
|
|
|
client.networks.forEach(function(network) {
|
|
|
|
// Only set away on client deattachment if
|
|
|
|
// there is no away message on this network
|
2018-03-09 10:33:24 +00:00
|
|
|
if (network.irc && !network.awayMessage) {
|
2016-12-18 09:24:50 +00:00
|
|
|
network.irc.raw("AWAY", client.awayMessage);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-09-25 06:41:10 +00:00
|
|
|
};
|
|
|
|
|
2017-07-10 19:47:03 +00:00
|
|
|
Client.prototype.registerPushSubscription = function(session, subscription, noSave) {
|
2019-07-17 09:33:59 +00:00
|
|
|
if (
|
|
|
|
!_.isPlainObject(subscription) ||
|
|
|
|
!_.isPlainObject(subscription.keys) ||
|
|
|
|
typeof subscription.endpoint !== "string" ||
|
|
|
|
!/^https?:\/\//.test(subscription.endpoint) ||
|
|
|
|
typeof subscription.keys.p256dh !== "string" ||
|
|
|
|
typeof subscription.keys.auth !== "string"
|
|
|
|
) {
|
2017-07-10 19:47:03 +00:00
|
|
|
session.pushSubscription = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
keys: {
|
|
|
|
p256dh: subscription.keys.p256dh,
|
2017-11-15 06:35:15 +00:00
|
|
|
auth: subscription.keys.auth,
|
|
|
|
},
|
2017-07-10 19:47:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
session.pushSubscription = data;
|
|
|
|
|
|
|
|
if (!noSave) {
|
|
|
|
this.manager.updateUser(this.name, {
|
2017-11-15 06:35:15 +00:00
|
|
|
sessions: this.config.sessions,
|
2017-07-10 19:47:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.unregisterPushSubscription = function(token) {
|
|
|
|
this.config.sessions[token].pushSubscription = null;
|
|
|
|
this.manager.updateUser(this.name, {
|
2017-11-15 06:35:15 +00:00
|
|
|
sessions: this.config.sessions,
|
2017-07-10 19:47:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
Client.prototype.save = _.debounce(
|
|
|
|
function SaveClient() {
|
|
|
|
if (Helper.config.public) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-09 16:01:22 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
const client = this;
|
|
|
|
const json = {};
|
|
|
|
json.networks = this.networks.map((n) => n.export());
|
|
|
|
client.manager.updateUser(client.name, json);
|
|
|
|
},
|
|
|
|
1000,
|
|
|
|
{maxWait: 10000}
|
|
|
|
);
|