CyTube/lib/acp.js

319 lines
8.6 KiB
JavaScript
Raw Normal View History

2013-08-18 17:21:34 +00:00
var Logger = require("./logger");
2013-10-11 21:31:40 +00:00
var Server = require("./server");
2014-01-30 03:50:45 +00:00
var db = require("./database");
2014-02-05 04:39:13 +00:00
var util = require("./utilities");
var Config = require("./config");
2014-02-07 05:31:47 +00:00
var Server = require("./server");
2013-10-11 21:31:40 +00:00
2014-02-07 16:45:28 +00:00
function eventUsername(user) {
return user.getName() + "@" + user.realip;
2014-02-07 16:45:28 +00:00
}
2014-01-30 03:50:45 +00:00
function handleAnnounce(user, data) {
var sv = Server.getServer();
2013-07-16 03:01:12 +00:00
2014-02-01 18:41:06 +00:00
sv.announce({
2014-01-30 03:50:45 +00:00
title: data.title,
text: data.content,
2014-05-21 02:30:14 +00:00
from: user.getName()
2014-02-01 18:41:06 +00:00
});
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] " + eventUsername(user) + " opened announcement `" +
data.title + "`");
2014-01-30 03:50:45 +00:00
}
2013-10-11 21:31:40 +00:00
2014-01-30 03:50:45 +00:00
function handleAnnounceClear(user) {
2014-02-01 18:41:06 +00:00
Server.getServer().announce(null);
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] " + eventUsername(user) + " cleared announcement");
2014-01-30 03:50:45 +00:00
}
2013-08-07 14:30:16 +00:00
2014-01-30 03:50:45 +00:00
function handleGlobalBan(user, data) {
db.globalBanIP(data.ip, data.note, function (err, res) {
if (err) {
user.socket.emit("errMessage", {
msg: err
2013-07-16 03:01:12 +00:00
});
2014-01-30 03:50:45 +00:00
return;
}
2013-07-16 03:01:12 +00:00
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] " + eventUsername(user) + " global banned " + data.ip);
2014-01-30 03:50:45 +00:00
db.listGlobalBans(function (err, bans) {
if (err) {
user.socket.emit("errMessage", {
msg: err
});
2013-10-11 21:31:40 +00:00
return;
2014-01-30 03:50:45 +00:00
}
2013-07-16 03:01:12 +00:00
2014-01-30 03:50:45 +00:00
var flat = [];
for (var ip in bans) {
flat.push({
ip: ip,
note: bans[ip].reason
});
2014-01-30 03:50:45 +00:00
}
user.socket.emit("acp-gbanlist", flat);
2013-10-11 21:31:40 +00:00
});
2014-01-30 03:50:45 +00:00
});
}
function handleGlobalBanDelete(user, data) {
db.globalUnbanIP(data.ip, function (err, res) {
if (err) {
user.socket.emit("errMessage", {
msg: err
});
return;
}
2013-10-11 21:31:40 +00:00
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] " + eventUsername(user) + " un-global banned " +
data.ip);
2014-01-30 03:50:45 +00:00
db.listGlobalBans(function (err, bans) {
if (err) {
user.socket.emit("errMessage", {
msg: err
2013-10-11 21:31:40 +00:00
});
2014-01-30 03:50:45 +00:00
return;
2013-10-11 21:31:40 +00:00
}
2013-07-31 03:21:32 +00:00
2014-01-30 03:50:45 +00:00
var flat = [];
for (var ip in bans) {
flat.push({
ip: ip,
note: bans[ip].reason
});
2013-10-11 21:31:40 +00:00
}
2014-01-30 03:50:45 +00:00
user.socket.emit("acp-gbanlist", flat);
2013-10-11 21:31:40 +00:00
});
2014-01-30 03:50:45 +00:00
});
}
function handleListUsers(user, data) {
var name = data.name;
if (typeof name !== "string") {
name = "";
}
var fields = ["id", "name", "global_rank", "email", "ip", "time"];
2013-10-11 21:31:40 +00:00
2014-01-30 03:50:45 +00:00
db.users.search(name, fields, function (err, users) {
if (err) {
user.socket.emit("errMessage", {
msg: err
2013-10-11 21:31:40 +00:00
});
2014-01-30 03:50:45 +00:00
return;
}
user.socket.emit("acp-list-users", users);
});
}
2013-10-11 21:31:40 +00:00
2014-01-30 03:50:45 +00:00
function handleSetRank(user, data) {
2014-02-05 04:39:13 +00:00
var name = data.name;
var rank = data.rank;
if (typeof name !== "string" || typeof rank !== "number") {
return;
}
if (rank >= user.global_rank) {
user.socket.emit("errMessage", {
msg: "You are not permitted to promote others to equal or higher rank than " +
"yourself."
});
return;
}
2013-10-11 21:31:40 +00:00
2014-02-05 04:39:13 +00:00
db.users.getGlobalRank(name, function (err, oldrank) {
if (err) {
user.socket.emit("errMessage", {
msg: err
});
return;
}
if (oldrank >= user.global_rank) {
user.socket.emit("errMessage", {
msg: "You are not permitted to change the rank of users who rank " +
"higher than you."
});
return;
}
db.users.setGlobalRank(name, rank, function (err) {
if (err) {
user.socket.emit("errMessage", {
msg: err
});
} else {
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] " + eventUsername(user) + " set " + name +
"'s global_rank to " + rank);
2014-02-05 04:39:13 +00:00
user.socket.emit("acp-set-rank", data);
}
});
});
2014-01-30 03:50:45 +00:00
}
function handleResetPassword(user, data) {
2014-02-05 04:39:13 +00:00
var name = data.name;
var email = data.email;
if (typeof name !== "string" || typeof email !== "string") {
return;
}
2013-10-11 21:31:40 +00:00
2014-02-05 04:39:13 +00:00
db.users.getGlobalRank(name, function (err, rank) {
if (rank >= user.global_rank) {
user.socket.emit("errMessage", {
msg: "You don't have permission to reset the password for " + name
});
return;
}
var hash = util.sha1(util.randomSalt(64));
var expire = Date.now() + 86400000;
db.addPasswordReset({
ip: "",
name: name,
email: email,
hash: hash,
expire: expire
}, function (err) {
if (err) {
user.socket.emit("errMessage", {
msg: err
});
return;
}
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] " + eventUsername(user) + " initialized a " +
"password recovery for " + name);
2014-02-05 04:39:13 +00:00
user.socket.emit("errMessage", {
msg: "Reset link: " + Config.get("http.domain") +
"/account/passwordrecover/" + hash
});
});
});
2014-01-30 03:50:45 +00:00
}
2014-02-05 23:44:37 +00:00
function handleListChannels(user, data) {
var field = data.field;
var value = data.value;
if (typeof field !== "string" || typeof value !== "string") {
return;
}
var dbfunc;
if (field === "owner") {
dbfunc = db.channels.searchOwner;
} else {
dbfunc = db.channels.search;
}
dbfunc(value, function (err, rows) {
if (err) {
user.socket.emit("errMessage", {
msg: err
});
return;
}
user.socket.emit("acp-list-channels", rows);
});
}
function handleDeleteChannel(user, data) {
var name = data.name;
if (typeof data.name !== "string") {
return;
}
var sv = Server.getServer();
if (sv.isChannelLoaded(name)) {
sv.getChannel(name).users.forEach(function (u) {
u.kick("Channel shutting down");
});
}
db.channels.drop(name, function (err) {
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] " + eventUsername(user) + " deleted channel " + name);
2014-02-05 23:44:37 +00:00
if (err) {
user.socket.emit("errMessage", {
msg: err
});
} else {
user.socket.emit("acp-delete-channel", {
name: name
});
}
});
}
2014-02-07 05:31:47 +00:00
function handleListActiveChannels(user) {
2014-05-24 05:40:35 +00:00
user.socket.emit("acp-list-activechannels", Server.getServer().packChannelList(false, true));
2014-02-07 05:31:47 +00:00
}
function handleForceUnload(user, data) {
var name = data.name;
if (typeof name !== "string") {
return;
}
var sv = Server.getServer();
if (!sv.isChannelLoaded(name)) {
return;
}
var chan = sv.getChannel(name);
var users = Array.prototype.slice.call(chan.users);
chan.emit("empty");
users.forEach(function (u) {
u.kick("Channel shutting down");
});
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] " + eventUsername(user) + " forced unload of " + name);
2014-02-07 05:31:47 +00:00
}
2014-02-13 05:52:38 +00:00
function handleListStats(user) {
db.listStats(function (err, rows) {
user.socket.emit("acp-list-stats", rows);
});
}
2014-01-30 03:50:45 +00:00
function init(user) {
var s = user.socket;
s.on("acp-announce", handleAnnounce.bind(this, user));
s.on("acp-announce-clear", handleAnnounceClear.bind(this, user));
s.on("acp-gban", handleGlobalBan.bind(this, user));
s.on("acp-gban-delete", handleGlobalBanDelete.bind(this, user));
s.on("acp-list-users", handleListUsers.bind(this, user));
s.on("acp-set-rank", handleSetRank.bind(this, user));
2014-01-30 04:50:14 +00:00
s.on("acp-reset-password", handleResetPassword.bind(this, user));
2014-02-05 23:44:37 +00:00
s.on("acp-list-channels", handleListChannels.bind(this, user));
s.on("acp-delete-channel", handleDeleteChannel.bind(this, user));
2014-02-07 05:31:47 +00:00
s.on("acp-list-activechannels", handleListActiveChannels.bind(this, user));
s.on("acp-force-unload", handleForceUnload.bind(this, user));
2014-02-13 05:52:38 +00:00
s.on("acp-list-stats", handleListStats.bind(this, user));
2014-01-30 03:50:45 +00:00
db.listGlobalBans(function (err, bans) {
if (err) {
user.socket.emit("errMessage", {
msg: err
2013-07-16 18:57:34 +00:00
});
2014-01-30 03:50:45 +00:00
return;
}
var flat = [];
for (var ip in bans) {
flat.push({
ip: ip,
note: bans[ip].reason
});
}
user.socket.emit("acp-gbanlist", flat);
});
2014-02-07 16:45:28 +00:00
Logger.eventlog.log("[acp] Initialized ACP for " + eventUsername(user));
2013-06-21 22:24:26 +00:00
}
2014-01-30 03:50:45 +00:00
module.exports.init = init;