2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-15 16:05:19 +00:00
|
|
|
const _ = require("lodash");
|
|
|
|
const colors = require("colors/safe");
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const Client = require("./client");
|
|
|
|
const Helper = require("./helper");
|
2017-07-10 19:47:03 +00:00
|
|
|
const WebPush = require("./plugins/webpush");
|
2014-08-13 23:43:11 +00:00
|
|
|
|
|
|
|
module.exports = ClientManager;
|
|
|
|
|
|
|
|
function ClientManager() {
|
2014-08-14 16:35:37 +00:00
|
|
|
this.clients = [];
|
2016-12-17 09:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientManager.prototype.init = function(identHandler, sockets) {
|
|
|
|
this.sockets = sockets;
|
|
|
|
this.identHandler = identHandler;
|
2017-07-10 19:47:03 +00:00
|
|
|
this.webPush = new WebPush();
|
2016-12-17 09:51:33 +00:00
|
|
|
|
2017-08-23 05:11:28 +00:00
|
|
|
if (!Helper.config.public && !Helper.config.ldap.enable) {
|
2017-10-15 16:05:19 +00:00
|
|
|
// TODO: Remove deprecated warning in v3.0.0
|
2016-12-17 09:51:33 +00:00
|
|
|
if ("autoload" in Helper.config) {
|
|
|
|
log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`);
|
|
|
|
}
|
2016-04-26 20:41:08 +00:00
|
|
|
|
2016-12-17 09:51:33 +00:00
|
|
|
this.autoloadUsers();
|
2016-04-26 20:41:08 +00:00
|
|
|
}
|
2016-12-17 09:51:33 +00:00
|
|
|
};
|
2014-08-13 23:43:11 +00:00
|
|
|
|
2017-06-21 07:58:29 +00:00
|
|
|
ClientManager.prototype.findClient = function(name) {
|
|
|
|
return this.clients.find((u) => u.name === name);
|
2014-08-14 16:35:37 +00:00
|
|
|
};
|
|
|
|
|
2016-12-07 05:50:11 +00:00
|
|
|
ClientManager.prototype.autoloadUsers = function() {
|
2017-08-23 05:11:28 +00:00
|
|
|
const users = this.getUsers();
|
2017-11-12 20:49:04 +00:00
|
|
|
const noUsersWarning = `There are currently no users. Create one with ${colors.bold("thelounge add <name>")}.`;
|
2017-08-23 05:11:28 +00:00
|
|
|
|
2017-08-26 04:55:49 +00:00
|
|
|
if (users.length === 0) {
|
2017-08-23 05:11:28 +00:00
|
|
|
log.info(noUsersWarning);
|
|
|
|
}
|
|
|
|
|
|
|
|
users.forEach((name) => this.loadUser(name));
|
2016-12-07 05:50:11 +00:00
|
|
|
|
2017-12-07 03:54:54 +00:00
|
|
|
fs.watch(Helper.getUsersPath(), _.debounce(() => {
|
2017-04-08 12:34:31 +00:00
|
|
|
const loaded = this.clients.map((c) => c.name);
|
2016-12-07 05:50:11 +00:00
|
|
|
const updatedUsers = this.getUsers();
|
|
|
|
|
2017-08-26 04:55:49 +00:00
|
|
|
if (updatedUsers.length === 0) {
|
2017-08-23 05:11:28 +00:00
|
|
|
log.info(noUsersWarning);
|
|
|
|
}
|
|
|
|
|
2017-09-30 22:19:53 +00:00
|
|
|
// Reload all users. Existing users will only have their passwords reloaded.
|
|
|
|
updatedUsers.forEach((name) => this.loadUser(name));
|
2016-12-07 05:50:11 +00:00
|
|
|
|
|
|
|
// Existing users removed since last time users were loaded
|
2017-04-08 12:34:31 +00:00
|
|
|
_.difference(loaded, updatedUsers).forEach((name) => {
|
2016-12-07 05:50:11 +00:00
|
|
|
const client = _.find(this.clients, {name: name});
|
|
|
|
if (client) {
|
2017-08-30 17:26:45 +00:00
|
|
|
client.quit(true);
|
2016-12-07 05:50:11 +00:00
|
|
|
this.clients = _.without(this.clients, client);
|
2017-10-15 16:05:19 +00:00
|
|
|
log.info(`User ${colors.bold(name)} disconnected and removed.`);
|
2016-12-07 05:50:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000, {maxWait: 10000}));
|
2014-08-14 01:51:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.loadUser = function(name) {
|
2017-09-30 22:19:53 +00:00
|
|
|
const userConfig = readUserConfig(name);
|
2017-10-15 16:05:19 +00:00
|
|
|
|
2017-09-30 22:19:53 +00:00
|
|
|
if (!userConfig) {
|
2014-08-14 01:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-15 16:05:19 +00:00
|
|
|
|
|
|
|
let client = this.findClient(name);
|
|
|
|
|
|
|
|
if (client) {
|
2017-09-30 22:19:53 +00:00
|
|
|
if (userConfig.password !== client.config.password) {
|
|
|
|
/**
|
|
|
|
* If we happen to reload an existing client, make super duper sure we
|
|
|
|
* have their latest password. We're not replacing the entire config
|
|
|
|
* object, because that could have undesired consequences.
|
|
|
|
*
|
|
|
|
* @see https://github.com/thelounge/lounge/issues/598
|
|
|
|
*/
|
|
|
|
client.config.password = userConfig.password;
|
|
|
|
log.info(`Password for user ${colors.bold(name)} was reset.`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
client = new Client(this, name, userConfig);
|
|
|
|
this.clients.push(client);
|
2014-09-24 22:23:54 +00:00
|
|
|
}
|
2017-10-15 16:05:19 +00:00
|
|
|
return client;
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.getUsers = function() {
|
2017-10-15 16:05:19 +00:00
|
|
|
return fs
|
2017-12-07 03:54:54 +00:00
|
|
|
.readdirSync(Helper.getUsersPath())
|
2017-10-15 16:05:19 +00:00
|
|
|
.filter((file) => file.endsWith(".json"))
|
|
|
|
.map((file) => file.slice(0, -5));
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|
|
|
|
|
2017-01-29 19:33:57 +00:00
|
|
|
ClientManager.prototype.addUser = function(name, password, enableLog) {
|
2017-10-15 16:05:19 +00:00
|
|
|
if (path.basename(name) !== name) {
|
|
|
|
throw new Error(`${name} is an invalid username.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const userPath = Helper.getUserConfigPath(name);
|
|
|
|
|
|
|
|
if (fs.existsSync(userPath)) {
|
|
|
|
log.error(`User ${colors.green(name)} already exists.`);
|
2014-08-14 17:25:22 +00:00
|
|
|
return false;
|
2014-08-13 23:43:11 +00:00
|
|
|
}
|
2016-04-02 21:19:57 +00:00
|
|
|
|
2017-10-15 16:05:19 +00:00
|
|
|
const user = {
|
|
|
|
password: password || "",
|
|
|
|
log: enableLog || false,
|
|
|
|
awayMessage: "",
|
|
|
|
networks: [],
|
|
|
|
sessions: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(userPath, JSON.stringify(user, null, "\t"));
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
2017-10-15 16:05:19 +00:00
|
|
|
log.error(`Failed to create user ${colors.green(name)} (${e})`);
|
2014-08-13 23:43:11 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2017-10-15 16:05:19 +00:00
|
|
|
|
2014-08-14 17:25:22 +00:00
|
|
|
return true;
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|
|
|
|
|
2016-11-19 20:54:16 +00:00
|
|
|
ClientManager.prototype.updateUser = function(name, opts, callback) {
|
2017-10-15 16:05:19 +00:00
|
|
|
const user = readUserConfig(name);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
log.error(`Tried to update invalid user ${colors.green(name)}. This is most likely a bug.`);
|
2016-02-17 00:14:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-19 20:54:16 +00:00
|
|
|
const currentUser = JSON.stringify(user, null, "\t");
|
|
|
|
_.assign(user, opts);
|
|
|
|
const newUser = JSON.stringify(user, null, "\t");
|
|
|
|
|
|
|
|
// Do not touch the disk if object has not changed
|
|
|
|
if (currentUser === newUser) {
|
|
|
|
return callback ? callback() : true;
|
2016-02-17 00:14:43 +00:00
|
|
|
}
|
2016-11-19 20:54:16 +00:00
|
|
|
|
2017-11-11 18:44:09 +00:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(Helper.getUserConfigPath(name), newUser);
|
|
|
|
} catch (e) {
|
|
|
|
log.error(`Failed to update user ${colors.green(name)} (${e})`);
|
|
|
|
throw e;
|
|
|
|
}
|
2016-02-17 00:14:43 +00:00
|
|
|
};
|
|
|
|
|
2017-10-15 16:05:19 +00:00
|
|
|
ClientManager.prototype.removeUser = function(name) {
|
|
|
|
const userPath = Helper.getUserConfigPath(name);
|
|
|
|
|
|
|
|
if (!fs.existsSync(userPath)) {
|
|
|
|
log.error(`Tried to remove non-existing user ${colors.green(name)}.`);
|
2016-02-17 00:14:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-10-15 16:05:19 +00:00
|
|
|
|
|
|
|
fs.unlinkSync(userPath);
|
|
|
|
|
|
|
|
return true;
|
2016-02-17 00:14:43 +00:00
|
|
|
};
|
|
|
|
|
2017-10-15 16:05:19 +00:00
|
|
|
function readUserConfig(name) {
|
|
|
|
const userPath = Helper.getUserConfigPath(name);
|
|
|
|
|
|
|
|
if (!fs.existsSync(userPath)) {
|
|
|
|
log.error(`Tried to read non-existing user ${colors.green(name)}`);
|
2014-08-14 17:25:22 +00:00
|
|
|
return false;
|
2014-08-13 23:43:11 +00:00
|
|
|
}
|
2017-10-15 16:05:19 +00:00
|
|
|
|
|
|
|
const data = fs.readFileSync(userPath, "utf-8");
|
|
|
|
return JSON.parse(data);
|
|
|
|
}
|