CyTube/acp.js

146 lines
4.8 KiB
JavaScript
Raw Normal View History

2013-06-21 22:24:26 +00:00
var Server = require("./server");
var Auth = require("./auth");
var Database = require("./database");
2013-06-22 01:28:21 +00:00
var ActionLog = require("./actionlog");
2013-06-21 22:24:26 +00:00
module.exports = {
init: function(user) {
2013-06-22 01:28:21 +00:00
ActionLog.record(user.ip, user.name, "acp-init");
2013-06-21 22:24:26 +00:00
user.socket.on("acp-announce", function(data) {
2013-06-23 18:02:20 +00:00
ActionLog.record(user.ip, user.name, "acp-announce", [data]);
2013-06-21 22:24:26 +00:00
Server.announcement = data;
Server.io.sockets.emit("announcement", data);
});
user.socket.on("acp-announce-clear", function() {
2013-06-22 01:28:21 +00:00
ActionLog.record(user.ip, user.name, "acp-announce-clear");
2013-06-21 22:24:26 +00:00
Server.announcement = null;
});
user.socket.on("acp-global-ban", function(data) {
2013-06-23 18:02:20 +00:00
ActionLog.record(user.ip, user.name, "acp-global-ban", [data.ip]);
2013-06-21 22:24:26 +00:00
Database.globalBanIP(data.ip, data.note);
user.socket.emit("acp-global-banlist", Database.refreshGlobalBans());
});
user.socket.on("acp-global-unban", function(ip) {
2013-06-27 02:49:47 +00:00
ActionLog.record(user.ip, user.name, "acp-global-unban", [ip]);
2013-06-21 22:24:26 +00:00
Database.globalUnbanIP(ip);
user.socket.emit("acp-global-banlist", Database.refreshGlobalBans());
});
user.socket.emit("acp-global-banlist", Database.refreshGlobalBans());
user.socket.on("acp-lookup-user", function(name) {
var db = Database.getConnection();
if(!db) {
return;
}
var query = Database.createQuery(
"SELECT id,uname,global_rank,profile_image,profile_text,email FROM registrations WHERE uname LIKE ?",
["%"+name+"%"]
);
var res = db.querySync(query);
if(!res)
return;
var rows = res.fetchAllSync();
user.socket.emit("acp-userdata", rows);
});
user.socket.on("acp-reset-password", function(data) {
if(Auth.getGlobalRank(data.name) >= user.global_rank)
return;
try {
var hash = Database.generatePasswordReset(user.ip, data.name, data.email);
2013-06-23 18:02:20 +00:00
ActionLog.record(user.ip, user.name, "acp-reset-password", [data.name]);
2013-06-21 22:24:26 +00:00
}
catch(e) {
user.socket.emit("acp-reset-password", {
success: false,
error: e
});
return;
}
if(hash) {
user.socket.emit("acp-reset-password", {
success: true,
hash: hash
});
}
else {
user.socket.emit("acp-reset-password", {
success: false,
error: "Reset failed"
});
}
});
user.socket.on("acp-set-rank", function(data) {
if(data.rank < 1 || data.rank >= user.global_rank)
return;
if(Auth.getGlobalRank(data.name) >= user.global_rank)
return;
var db = Database.getConnection();
if(!db)
return;
2013-06-23 18:02:20 +00:00
ActionLog.record(user.ip, user.name, "acp-set-rank", [data]);
2013-06-21 22:24:26 +00:00
var query = Database.createQuery(
"UPDATE registrations SET global_rank=? WHERE uname=?",
2013-06-25 03:42:11 +00:00
[data.rank, data.name]
2013-06-21 22:24:26 +00:00
);
var res = db.querySync(query);
if(!res)
return;
user.socket.emit("acp-set-rank", data);
});
user.socket.on("acp-list-loaded", function() {
var chans = [];
for(var c in Server.channels) {
var chan = Server.channels[c];
if(!chan)
continue;
chans.push({
name: c,
title: chan.opts.pagetitle,
usercount: chan.users.length,
mediatitle: chan.media ? chan.media.title : "-",
is_public: chan.opts.show_public,
registered: chan.registered
});
}
user.socket.emit("acp-list-loaded", chans);
});
user.socket.on("acp-channel-unload", function(data) {
if(data.name in Server.channels) {
var c = Server.channels[data.name];
if(!c)
return;
2013-06-22 01:28:21 +00:00
ActionLog.record(user.ip, user.name, "acp-channel-unload");
2013-06-21 22:24:26 +00:00
c.initialized = data.save;
c.users.forEach(function(u) {
c.kick(u, "Channel shutting down");
});
Server.unload(c);
}
});
2013-06-22 01:28:21 +00:00
2013-06-22 22:54:08 +00:00
user.socket.on("acp-actionlog-clear", function(data) {
ActionLog.clear(data);
2013-06-23 18:02:20 +00:00
ActionLog.record(user.ip, user.name, "acp-actionlog-clear", data);
2013-06-22 01:28:21 +00:00
});
2013-06-21 22:24:26 +00:00
}
}