sync/database.js

550 lines
13 KiB
JavaScript
Raw Normal View History

var mysql = require("mysql-libmysqlclient");
2013-05-26 01:01:32 +00:00
var Logger = require("./logger");
var Media = require("./media").Media;
var db = false;
var SERVER = "";
var USER = "";
var DATABASE = "";
var PASSWORD = "";
var CONFIG = {};
function setup(cfg) {
SERVER = cfg.MYSQL_SERVER;
USER = cfg.MYSQL_USER;
DATABASE = cfg.MYSQL_DB;
PASSWORD = cfg.MYSQL_PASSWORD;
CONFIG = cfg;
}
function getConnection() {
if(db && db.connectedSync()) {
return db;
}
db = mysql.createConnectionSync();
db.connectSync(SERVER, USER, PASSWORD, DATABASE);
if(!db.connectedSync()) {
2013-05-26 01:01:32 +00:00
//Logger.errlog.log("DB connection failed");
return false;
}
2013-05-26 01:01:32 +00:00
if(CONFIG.DEBUG) {
db._querySync = db.querySync;
db.querySync = function(q) {
Logger.syslog.log("DEBUG: " + q);
return this._querySync(q);
}
}
2013-04-02 19:39:56 +00:00
return db;
}
2013-05-26 01:01:32 +00:00
function createQuery(template, args) {
var last = -1;
while(template.indexOf("?", last) >= 0) {
var idx = template.indexOf("?", last);
var arg = args.shift();
if(typeof arg == "string") {
arg = arg.replace(/([\'])/g, "\\$1");
if(idx == 0 || template[idx-1] != "`") {
arg = "'" + arg + "'";
}
}
if(arg === null || arg === undefined) {
arg = "NULL";
}
var first = template.substring(0, idx);
template = first + template.substring(idx).replace("?", arg);
last = idx + (arg+"").length;
}
2013-05-26 01:01:32 +00:00
return template;
}
2013-04-02 19:39:56 +00:00
2013-05-26 01:01:32 +00:00
function init() {
var db = getConnection();
2013-04-02 19:39:56 +00:00
if(!db) {
2013-03-06 22:02:40 +00:00
return false;
}
2013-05-26 01:01:32 +00:00
// Create channel table
var query = ["CREATE TABLE IF NOT EXISTS `channels` (",
"`id` INT NOT NULL AUTO_INCREMENT,",
"`name` VARCHAR(255) NOT NULL,",
"PRIMARY KEY(`id`))",
"ENGINE = MyISAM;"].join("");
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
if(!results) {
2013-05-26 01:01:32 +00:00
Logger.errlog.log("! Failed to create channels table");
}
// Create registration table
query = ["CREATE TABLE IF NOT EXISTS `registrations` (",
"`id` INT NOT NULL AUTO_INCREMENT,",
"`uname` VARCHAR(20) NOT NULL,",
"`pw` VARCHAR(64) NOT NULL,",
"`global_rank` INT NOT NULL,",
"`session_hash` VARCHAR(64) NOT NULL,",
"`expire` BIGINT NOT NULL,",
"`profile_image` VARCHAR(255) NOT NULL,",
"`profile_text` TEXT NOT NULL,",
"PRIMARY KEY (`id`))",
"ENGINE = MyISAM;"].join("");
results = db.querySync(query);
if(!results) {
Logger.errlog.log("! Failed to create registrations table");
2013-03-06 22:02:40 +00:00
}
2013-05-26 01:01:32 +00:00
// Create global bans table
query = ["CREATE TABLE IF NOT EXISTS `global_bans` (",
"`ip` VARCHAR(15) NOT NULL,",
"`note` VARCHAR(255) NOT NULL,",
"PRIMARY KEY (`ip`))",
"ENGINE = MyISAM;"].join("");
results = db.querySync(query);
2013-04-28 06:30:18 +00:00
if(!results) {
2013-05-26 01:01:32 +00:00
Logger.errlog.log("! Failed to create global ban table");
2013-04-28 06:30:18 +00:00
}
2013-05-26 01:01:32 +00:00
refreshGlobalBans();
2013-03-06 22:02:40 +00:00
}
2013-05-26 01:01:32 +00:00
/* REGION Global Bans */
function checkGlobalBan(ip) {
const re = /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
var s16 = ip.replace(re, "$1.$2");
var s24 = ip.replace(re, "$1.$2.$3");
return (ip in global_bans ||
s16 in global_bans ||
s24 in global_bans);
2013-04-28 06:30:18 +00:00
}
2013-05-26 01:01:32 +00:00
function refreshGlobalBans() {
var db = getConnection();
if(!db) {
2013-05-26 01:01:32 +00:00
return;
}
2013-05-26 01:01:32 +00:00
var query = "SELECT * FROM `global_bans` WHERE 1";
var results = db.querySync(query);
if(!results) {
2013-05-26 01:01:32 +00:00
Logger.errlog.log("! Failed to load global bans");
}
2013-05-26 01:01:32 +00:00
else {
var rows = results.fetchAllSync();
global_bans = {};
for(var i = 0; i < rows.length; i++) {
global_bans[rows[i].ip] = rows[i].note;
}
}
}
2013-05-26 01:01:32 +00:00
function globalBanIP(ip, reason) {
var db = getConnection();
if(!db) {
2013-05-26 01:01:32 +00:00
return;
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
"INSERT INTO `global_bans` VALUES (?, ?)",
[ip, reason]
);
return db.querySync(query);
}
2013-05-26 01:01:32 +00:00
function globalUnbanIP(ip) {
var db = getConnection();
if(!db) {
2013-05-26 01:01:32 +00:00
return;
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
"DELETE FROM `global_bans` WHERE ip=?",
[ip]
);
return db.querySync(query);
}
2013-05-26 01:01:32 +00:00
/* REGION Channel Registration/Loading */
function registerChannel(name) {
var db = getConnection();
if(!db) { return false;
2013-04-02 19:39:56 +00:00
}
2013-05-26 01:01:32 +00:00
// Library table
var query = ["CREATE TABLE `?` (",
"`id` VARCHAR(255) NOT NULL,",
"`title` VARCHAR(255) NOT NULL,",
"`seconds` INT NOT NULL,",
"`type` VARCHAR(2) NOT NULL,",
"PRIMARY KEY (`id`))",
"ENGINE = MyISAM;"].join("");
query = createQuery(query, ["chan_" + name + "_library"]);
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
if(!results) {
2013-05-26 01:01:32 +00:00
Logger.errlog.log("! Failed to create table: chan_"+name+"_library");
return false;
2013-03-06 22:02:40 +00:00
}
2013-05-26 01:01:32 +00:00
// Rank table
query = ["CREATE TABLE `?` (",
"`name` VARCHAR(32) NOT NULL,",
"`rank` INT NOT NULL,",
"UNIQUE (`name`))",
"ENGINE = MyISAM;"].join("");
query = createQuery(query, ["chan_" + name + "_ranks"]);
results = db.querySync(query);
if(!results) {
Logger.errlog.log("! Failed to create table: chan_"+name+"_ranks");
return false;
2013-04-02 19:39:56 +00:00
}
2013-05-26 01:01:32 +00:00
// Ban table
query = ["CREATE TABLE `?` (",
"`ip` VARCHAR(15) NOT NULL,",
"`name` VARCHAR(32) NOT NULL,",
"`banner` VARCHAR(32) NOT NULL,",
"PRIMARY KEY (`ip`))",
"ENGINE = MyISAM;"].join("");
query = createQuery(query, ["chan_" + name + "_bans"]);
results = db.querySync(query);
if(!results) {
Logger.errlog.log("! Failed to create table: chan_"+name+"_bans");
return false;
2013-04-03 17:47:41 +00:00
}
2013-03-06 22:02:40 +00:00
2013-05-26 01:01:32 +00:00
// Insert into channel table
query = createQuery(
"INSERT INTO `channels` VALUES (NULL, ?)",
[name]
);
results = db.querySync(query);
2013-04-02 19:39:56 +00:00
if(!results) {
2013-05-26 01:01:32 +00:00
Logger.errlog.log("! Failed to insert into channel table: " + name);
return false;
2013-04-02 19:39:56 +00:00
}
2013-05-26 01:01:32 +00:00
return true;
}
function loadChannel(chan) {
var db = getConnection();
if(!db) {
return;
2013-03-06 22:02:40 +00:00
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
"SELECT * FROM `channels` WHERE name=?",
[chan.name]
);
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
if(!results) {
2013-05-26 01:01:32 +00:00
Logger.errlog.log("! Failed to query channel table");
2013-04-02 19:39:56 +00:00
}
2013-05-26 01:01:32 +00:00
else {
var rows = results.fetchAllSync();
if(rows.length == 0) {
// Unregistered
Logger.syslog.log("Channel " + chan.name + " is unregistered");
return;
2013-05-21 16:17:01 +00:00
}
2013-05-26 01:01:32 +00:00
// Database is case insensitive
else if(rows[0].name != chan.name) {
chan.name = rows[0].name;
}
chan.registered = true;
}
// Load channel library
query = createQuery(
"SELECT * FROM `?`",
["chan_" + chan.name + "_library"]
);
results = db.querySync(query);
if(!results) {
Logger.errlog.log("! Failed to load channel library: " + chan.name);
}
else {
var rows = results.fetchAllSync();
for(var i = 0; i < rows.length; i++) {
var r = rows[i];
var m = new Media(r.id, r.title, r.seconds, r.type);
chan.library[r.id] = m;
}
}
// Load channel bans
query = createQuery(
"SELECT * FROM `?`",
["chan_" + chan.name + "_bans"]
);
results = db.querySync(query);
if(!results) {
Logger.errlog.log("! Failed to load channel bans: " + chan.name);
}
else {
var rows = results.fetchAllSync();
for(var i = 0; i < rows.length; i++) {
var r = rows[i];
if(r.ip == "*") {
chan.nameban[r.name] = r.banner;
}
else {
chan.ipbans[r.ip] = [r.name, r.banner];
}
2013-05-21 16:17:01 +00:00
}
2013-04-02 19:39:56 +00:00
}
chan.logger.log("*** Loaded channel from database");
Logger.syslog.log("Loaded channel " + chan.name + " from database");
}
2013-05-26 01:01:32 +00:00
function deleteChannel(name) {
if(!/[a-zA-Z0-9-_]+/.test(name)) {
2013-03-06 22:02:40 +00:00
return false;
}
2013-05-26 01:01:32 +00:00
var db = getConnection();
2013-05-13 19:41:29 +00:00
if(!db) {
return false;
}
2013-05-26 01:01:32 +00:00
var query = "DROP TABLE `chan_?_bans`, `chan_?_ranks`, `chan_?_library`"
.replace(/\?/g, name);
2013-05-13 19:41:29 +00:00
var results = db.querySync(query);
if(!results) {
2013-05-26 01:01:32 +00:00
Logger.errlog.log("! Failed to delete channel tables for " + name);
2013-05-13 19:41:29 +00:00
return false;
}
2013-05-26 01:01:32 +00:00
query = createQuery(
"DELETE FROM `channels` WHERE name=?",
[name]
);
2013-05-13 19:41:29 +00:00
results = db.querySync(query);
2013-05-26 01:01:32 +00:00
if(!results) {
Logger.errlog.log("! Failed to delete row from channel table: " + name);
return false;
}
return true;
2013-05-13 19:41:29 +00:00
}
2013-05-26 01:01:32 +00:00
/* REGION Channel data */
function getChannelRank(chan, name) {
var db = getConnection();
2013-04-02 19:39:56 +00:00
if(!db) {
2013-05-26 01:01:32 +00:00
return 0;
}
var query;
if(typeof name == "object") {
var n = "(?";
for(var i = 1; i < name.length; i++) {
n += ",?";
}
n += ")"
name.unshift("chan_" + chan + "_ranks");
query = createQuery(
"SELECT * FROM `?` WHERE name IN " + n,
name
);
}
else {
query = createQuery(
"SELECT * FROM `?` WHERE name=?",
["chan_"+chan+"_ranks", name]
);
2013-04-02 19:39:56 +00:00
}
2013-05-26 01:01:32 +00:00
2013-04-02 19:39:56 +00:00
var results = db.querySync(query);
2013-04-04 20:41:41 +00:00
if(!results) {
2013-05-26 01:01:32 +00:00
Logger.errlog.log("! Failed to lookup chan_"+chan+"_ranks");
return 0;
2013-04-04 20:41:41 +00:00
}
2013-05-26 01:01:32 +00:00
2013-04-02 19:39:56 +00:00
var rows = results.fetchAllSync();
2013-05-26 01:01:32 +00:00
if(typeof name == "object") {
var ranks = [];
for(var i = 0; i < rows.length; i++) {
ranks.push(rows[i].rank);
}
while(ranks.length < rows.length) {
ranks.push(0);
}
return ranks;
}
2013-04-02 19:39:56 +00:00
if(rows.length == 0) {
2013-05-26 01:01:32 +00:00
return 0;
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
return rows[0].rank;
}
2013-05-26 01:01:32 +00:00
function setChannelRank(chan, name, rank) {
var db = getConnection();
2013-04-02 19:39:56 +00:00
if(!db) {
2013-03-06 22:02:40 +00:00
return false;
2013-04-02 19:39:56 +00:00
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
["INSERT INTO `?` ",
"(`name`, `rank`) ",
"VALUES ",
"(?, ?) ",
"ON DUPLICATE KEY UPDATE ",
"`rank`=?"].join(""),
["chan_"+chan+"_ranks", name, rank, rank]
);
return db.querySync(query);
2013-04-02 19:39:56 +00:00
}
2013-03-06 22:02:40 +00:00
2013-05-26 01:01:32 +00:00
function listChannelRanks(chan) {
var db = getConnection();
2013-04-02 19:39:56 +00:00
if(!db) {
2013-05-26 01:01:32 +00:00
return [];
2013-03-06 22:02:40 +00:00
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
"SELECT * FROM `?` WHERE 1",
["chan_"+chan+"_ranks"]
);
var results = db.querySync(query);
2013-05-26 01:01:32 +00:00
if(!results) {
Logger.errlog.log("! Failed to list ranks: " + chan);
return [];
}
return results.fetchAllSync();
}
2013-05-26 01:01:32 +00:00
function addToLibrary(chan, media) {
var db = getConnection();
if(!db) {
return false;
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
["INSERT INTO `?` ",
"(`id`, `title`, `seconds`, `type`) ",
"VALUES ",
"(?, ?, ?, ?)"].join(""),
["chan_"+chan+"_library", media.id, media.title, media.seconds, media.type]
);
return db.querySync(query);
2013-04-02 19:39:56 +00:00
}
2013-04-03 03:56:44 +00:00
2013-05-26 01:01:32 +00:00
function removeFromLibrary(chan, id) {
var db = getConnection();
2013-04-03 03:56:44 +00:00
if(!db) {
return false;
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
"DELETE FROM `?` WHERE id=?",
["chan_"+chan+"_library", id]
);
return db.querySync(query);
2013-04-03 03:56:44 +00:00
}
2013-05-26 01:01:32 +00:00
function channelBan(chan, ip, name, banby) {
var db = getConnection();
2013-04-03 03:56:44 +00:00
if(!db) {
return false;
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
["INSERT INTO `?` ",
"(`ip`, `name`, `banner`) ",
"VALUES ",
"(?, ?, ?)"].join(""),
["chan_"+chan+"_bans", ip, name, banby]
);
return db.querySync(query);
2013-04-03 03:56:44 +00:00
}
2013-05-26 01:01:32 +00:00
function channelUnbanIP(chan, ip) {
var db = getConnection();
2013-05-21 16:17:01 +00:00
if(!db) {
return false;
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
"DELETE FROM `?` WHERE `ip`=?",
["chan_"+chan+"_bans", ip]
);
return db.querySync(query);
2013-05-21 16:17:01 +00:00
}
2013-05-26 01:01:32 +00:00
function channelUnbanName(chan, name) {
var db = getConnection();
if(!db) {
return false;
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
"DELETE FROM `?` WHERE `ip`='*' AND `name`=?",
["chan_"+chan+"_bans", name]
);
2013-05-26 01:01:32 +00:00
return db.querySync(query);
}
2013-05-26 01:01:32 +00:00
/* REGION Users */
function setProfile(name, data) {
var db = getConnection();
if(!db) {
return false;
}
2013-05-26 01:01:32 +00:00
var query = createQuery(
["UPDATE `registrations` SET ",
"`profile_image`=?,",
"`profile_text`=? ",
"WHERE uname=?"].join(""),
[data.image, data.text, name]
);
2013-05-26 01:01:32 +00:00
return db.querySync(query);
}
2013-05-26 01:01:32 +00:00
exports.setup = setup;
exports.getConnection = getConnection;
exports.createQuery = createQuery;
exports.init = init;
exports.checkGlobalBan = checkGlobalBan;
exports.refreshGlobalBans = refreshGlobalBans;
exports.globalBanIP = globalBanIP;
exports.globalUnbanIP = globalUnbanIP;
exports.registerChannel = registerChannel;
exports.loadChannel = loadChannel;
exports.deleteChannel = deleteChannel;
exports.getChannelRank = getChannelRank;
exports.setChannelRank = setChannelRank;
exports.listChannelRanks = listChannelRanks;
exports.addToLibrary = addToLibrary;
exports.removeFromLibrary = removeFromLibrary;
exports.channelBan = channelBan;
exports.channelUnbanIP = channelUnbanIP;
exports.channelUnbanName = channelUnbanName;
exports.setProfile = setProfile;