hardlounge/src/clientManager.js

169 lines
3.9 KiB
JavaScript
Raw Normal View History

"use strict";
2014-08-14 17:25:22 +00:00
var _ = require("lodash");
var colors = require("colors/safe");
2014-08-13 23:43:11 +00:00
var fs = require("fs");
2014-08-14 01:51:54 +00:00
var Client = require("./client");
var 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() {
this.clients = [];
}
ClientManager.prototype.init = function(identHandler, sockets) {
this.sockets = sockets;
this.identHandler = identHandler;
2017-07-10 19:47:03 +00:00
this.webPush = new WebPush();
if (!Helper.config.public) {
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
this.autoloadUsers();
2016-04-26 20:41:08 +00:00
}
};
2014-08-13 23:43:11 +00:00
ClientManager.prototype.findClient = function(name) {
return this.clients.find((u) => u.name === name);
};
ClientManager.prototype.autoloadUsers = function() {
this.getUsers().forEach((name) => this.loadUser(name));
fs.watch(Helper.USERS_PATH, _.debounce(() => {
const loaded = this.clients.map((c) => c.name);
const updatedUsers = this.getUsers();
// New users created since last time users were loaded
_.difference(updatedUsers, loaded).forEach((name) => this.loadUser(name));
// Existing users removed since last time users were loaded
_.difference(loaded, updatedUsers).forEach((name) => {
const client = _.find(this.clients, {name: name});
if (client) {
client.quit();
this.clients = _.without(this.clients, client);
log.info(`User ${colors.bold(name)} disconnected and removed`);
}
});
}, 1000, {maxWait: 10000}));
2014-08-14 01:51:54 +00:00
};
ClientManager.prototype.loadUser = function(name) {
2016-10-09 08:54:44 +00:00
let json;
2014-08-14 01:51:54 +00:00
try {
2016-10-09 08:54:44 +00:00
json = this.readUserConfig(name);
2015-09-30 22:39:57 +00:00
} catch (e) {
2016-04-16 11:32:38 +00:00
log.error("Failed to read user config", e);
2014-08-14 01:51:54 +00:00
return;
}
2014-09-24 22:23:54 +00:00
if (!this.findClient(name)) {
this.clients.push(new Client(
this,
2014-09-24 22:23:54 +00:00
name,
json
));
}
2014-08-13 23:43:11 +00:00
};
ClientManager.prototype.getUsers = function() {
var users = [];
try {
var files = fs.readdirSync(Helper.USERS_PATH);
files.forEach((file) => {
2014-10-14 22:24:15 +00:00
if (file.indexOf(".json") !== -1) {
users.push(file.replace(".json", ""));
}
2014-10-14 20:05:16 +00:00
});
2015-09-30 22:39:57 +00:00
} catch (e) {
2016-04-16 11:32:38 +00:00
log.error("Failed to get users", e);
2014-08-13 23:43:11 +00:00
return;
}
return users;
};
ClientManager.prototype.addUser = function(name, password, enableLog) {
2014-08-13 23:43:11 +00:00
var users = this.getUsers();
if (users.indexOf(name) !== -1) {
2014-08-14 17:25:22 +00:00
return false;
2014-08-13 23:43:11 +00:00
}
try {
if (require("path").basename(name) !== name) {
throw new Error(name + " is an invalid username.");
}
2014-08-14 01:51:54 +00:00
var user = {
user: name,
password: password || "",
log: enableLog,
awayMessage: "",
networks: [],
sessions: {},
2014-08-14 01:51:54 +00:00
};
fs.writeFileSync(
Helper.getUserConfigPath(name),
2016-06-19 17:16:50 +00:00
JSON.stringify(user, null, "\t")
2014-08-14 01:51:54 +00:00
);
2015-09-30 22:39:57 +00:00
} catch (e) {
log.error("Failed to add user " + name, e);
2014-08-13 23:43:11 +00:00
throw e;
}
2014-08-14 17:25:22 +00:00
return true;
2014-08-13 23:43:11 +00:00
};
ClientManager.prototype.updateUser = function(name, opts, callback) {
const users = this.getUsers();
if (users.indexOf(name) === -1) {
return false;
}
if (typeof opts === "undefined") {
return false;
}
const user = this.readUserConfig(name);
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;
}
fs.writeFile(Helper.getUserConfigPath(name), newUser, (err) => {
if (err) {
log.error("Failed to update user", err);
}
if (callback) {
callback(err);
}
});
};
ClientManager.prototype.readUserConfig = function(name) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
return false;
}
var data = fs.readFileSync(Helper.getUserConfigPath(name), "utf-8");
2016-06-12 06:43:35 +00:00
return JSON.parse(data);
};
2014-08-13 23:43:11 +00:00
ClientManager.prototype.removeUser = function(name) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
2014-08-14 17:25:22 +00:00
return false;
2014-08-13 23:43:11 +00:00
}
try {
fs.unlinkSync(Helper.getUserConfigPath(name));
2015-09-30 22:39:57 +00:00
} catch (e) {
2014-08-13 23:43:11 +00:00
throw e;
}
2014-08-14 17:25:22 +00:00
return true;
2014-08-13 23:43:11 +00:00
};