CyTube/database.js

267 lines
9.1 KiB
JavaScript
Raw Normal View History

2013-03-24 02:28:20 +00:00
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var mysql = require("mysql-libmysqlclient");
var Config = require("./config.js");
2013-03-27 19:28:51 +00:00
var Logger = require("./logger.js");
2013-04-02 19:39:56 +00:00
var Rank = require("./rank.js");
2013-04-03 17:47:41 +00:00
var Media = require("./media.js").Media;
2013-04-04 23:22:11 +00:00
var Server = require("./server.js");
2013-03-06 22:02:40 +00:00
var initialized = false;
2013-04-02 19:39:56 +00:00
exports.getConnection = function() {
2013-03-06 22:02:40 +00:00
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-04-02 19:39:56 +00:00
Logger.errlog.log("database.getConnection: DB connection failed");
return false;
}
2013-04-02 19:39:56 +00:00
return db;
}
exports.init = function() {
if(initialized)
return;
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.init: DB conection failed");
return;
}
2013-03-06 22:02:40 +00:00
var query = "CREATE TABLE IF NOT EXISTS `channels` \
2013-03-31 23:38:20 +00:00
(`id` INT NOT NULL AUTO_INCREMENT, \
2013-03-06 22:02:40 +00:00
`name` VARCHAR(255) NOT NULL, \
PRIMARY KEY (`id`)) \
ENGINE = MyISAM;";
var results = db.querySync(query);
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("database.init: channel table init failed!");
2013-03-06 22:02:40 +00:00
return false;
}
var query = "CREATE TABLE IF NOT EXISTS `registrations` \
2013-03-31 23:35:35 +00:00
(`id` INT NOT NULL AUTO_INCREMENT, \
2013-03-06 22:02:40 +00:00
`uname` VARCHAR(20) NOT NULL, \
`pw` VARCHAR(64) NOT NULL, \
`global_rank` INT NOT NULL, \
PRIMARY KEY (`id`)) \
ENGINE = MyISAM;";
var results = db.querySync(query);
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("database.init: registration table init failed!");
2013-03-06 22:02:40 +00:00
return false;
}
initialized = true;
2013-03-24 03:01:37 +00:00
db.closeSync();
2013-03-06 22:02:40 +00:00
return true;
}
2013-04-02 19:39:56 +00:00
exports.loadChannel = function(chan) {
var db = exports.getConnection();
if(!db) {
chan.logger.log("!!! Could not connect to database");
return;
}
// Check if channel exists
var query = "SELECT * FROM channels WHERE name='{}'"
.replace("{}", chan.name);
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
if(!results) {
2013-04-02 19:39:56 +00:00
Logger.errlog.log("Channel.loadMysql: Channel query failed");
return;
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
var rows = results.fetchAllSync();
if(rows.length == 0) {
Logger.syslog.log("Channel " + chan.name + " is unregistered.");
return;
}
2013-04-03 17:47:41 +00:00
else if(rows[0].name != chan.name) {
chan.name = rows[0].name;
2013-04-03 17:47:41 +00:00
}
2013-04-02 19:39:56 +00:00
chan.registered = true;
2013-03-06 22:02:40 +00:00
2013-04-02 19:39:56 +00:00
// Load library
var query = "SELECT * FROM chan_{}_library"
.replace("{}", chan.name);
var results = db.querySync(query);
if(!results) {
Logger.errlog.log("Channel.loadMysql: failed to load library for " + chan.name);
return;
}
var rows = results.fetchAllSync();
for(var i = 0; i < rows.length; i++) {
chan.library[rows[i].id] = new Media(rows[i].id, rows[i].title, rows[i].seconds, rows[i].type);
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
// Load bans
var query = "SELECT * FROM chan_{}_bans"
.replace("{}", chan.name);
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
if(!results) {
2013-04-02 19:39:56 +00:00
Logger.errlog.log("Channel.loadMysql: failed to load banlist for " + chan.name);
return;
}
var rows = results.fetchAllSync();
for(var i = 0; i < rows.length; i++) {
chan.ipbans[rows[i].ip] = [rows[i].name, rows[i].banner];
}
chan.logger.log("*** Loaded channel from database");
Logger.syslog.log("Loaded channel " + chan.name + " from database");
db.closeSync();
}
exports.registerChannel = function(chan) {
var db = exports.getConnection();
if(!db) {
chan.logger.log("!!! Could not connect to database");
2013-03-06 22:02:40 +00:00
return false;
}
2013-04-02 19:39:56 +00:00
// Create library table
var query= "CREATE TABLE `chan_{}_library` \
(`id` VARCHAR(255) NOT NULL, \
`title` VARCHAR(255) NOT NULL, \
`seconds` INT NOT NULL, \
`playtime` VARCHAR(8) NOT NULL, \
`type` VARCHAR(2) NOT NULL, \
PRIMARY KEY (`id`)) \
ENGINE = MyISAM;"
.replace("{}", chan.name);
var results = db.querySync(query);
2013-03-06 22:02:40 +00:00
2013-04-02 19:39:56 +00:00
// Create rank table
var query = "CREATE TABLE `chan_{}_ranks` (\
`name` VARCHAR( 32 ) NOT NULL ,\
`rank` INT NOT NULL ,\
UNIQUE (\
`name`\
)\
) ENGINE = MYISAM"
.replace("{}", chan.name);
results = db.querySync(query) || results;
// Create ban table
var query = "CREATE TABLE `chan_{}_bans` (\
`ip` VARCHAR( 15 ) NOT NULL ,\
`name` VARCHAR( 32 ) NOT NULL ,\
`banner` VARCHAR( 32 ) NOT NULL ,\
PRIMARY KEY (\
`ip`\
)\
) ENGINE = MYISAM"
.replace("{}", chan.name);
results = db.querySync(query) || results;
// Insert into global channel table
var query = "INSERT INTO channels (`id`, `name`) VALUES (NULL, '{}')"
.replace("{}", chan.name);
results = db.querySync(query) || results;
db.closeSync();
return results;
}
exports.lookupChannelRank = function(channame, username) {
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.lookupChannelRank: DB connection failed");
return Rank.Guest;
}
var query = "SELECT * FROM chan_{1}_ranks WHERE name='{2}'"
.replace("{1}", channame)
.replace("{2}", username);
var results = db.querySync(query);
2013-04-04 20:41:41 +00:00
if(!results) {
2013-04-02 19:39:56 +00:00
return Rank.Guest;
2013-04-04 20:41:41 +00:00
}
2013-04-02 19:39:56 +00:00
var rows = results.fetchAllSync();
if(rows.length == 0) {
return Rank.Guest;
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
db.closeSync();
return rows[0].rank;
}
exports.saveChannelRank = function(channame, user) {
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.saveChannelRank: DB connection failed");
2013-03-06 22:02:40 +00:00
return false;
2013-04-02 19:39:56 +00:00
}
var query = "UPDATE chan_{1}_ranks SET rank='{2}' WHERE name='{3}'"
.replace("{1}", channame)
.replace("{2}", user.rank)
.replace("{3}", user.name);
2013-03-06 22:02:40 +00:00
var results = db.querySync(query);
2013-04-02 19:39:56 +00:00
// Gonna have to insert a new one, bugger
if(!results.fetchAllSync) {
var query = "INSERT INTO chan_{1}_ranks (`name`, `rank`) VALUES ('{2}', '{3}')"
.replace("{1}", channame)
.replace("{2}", user.name)
.replace("{3}", user.rank);
results = db.querySync(query);
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
db.closeSync();
return results;
}
2013-03-06 22:02:40 +00:00
2013-04-02 19:39:56 +00:00
exports.cacheMedia = function(channame, media) {
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("database.cacheMedia: DB connection failed");
return false;
2013-03-06 22:02:40 +00:00
}
2013-04-02 19:39:56 +00:00
var query = "INSERT INTO chan_{1}_library VALUES ('{2}', '{3}', {4}, '{5}', '{6}')"
.replace("{1}", channame)
.replace("{2}", media.id)
.replace("{3}", media.title)
.replace("{4}", media.seconds)
.replace("{5}", media.duration)
.replace("{6}", media.type);
var results = db.querySync(query);
db.closeSync();
return results;
}
2013-04-03 03:56:44 +00:00
2013-04-03 17:47:41 +00:00
exports.addChannelBan = function(channame, actor, receiver) {
2013-04-03 03:56:44 +00:00
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("exports.addChannelBan: DB connection failed");
return false;
}
var query = "INSERT INTO chan_{1}_bans (`ip`, `name`, `banner`) VALUES ('{2}', '{3}', '{4}')"
.replace("{1}", channame)
2013-04-11 00:51:30 +00:00
.replace("{2}", receiver.ip)
.replace("{3}", receiver.name)
2013-04-03 03:56:44 +00:00
.replace("{4}", actor.name);
results = db.querySync(query);
db.closeSync();
return results;
}
2013-04-03 17:47:41 +00:00
exports.removeChannelBan = function(channame, ip) {
2013-04-03 03:56:44 +00:00
var db = exports.getConnection();
if(!db) {
Logger.errlog.log("exports.removeChannelBan: DB connection failed");
return false;
}
var query = "DELETE FROM chan_{1}_bans WHERE `ip` = '{2}'"
.replace("{1}", channame)
.replace("{2}", ip);
results = db.querySync(query);
db.closeSync();
return results;
}