Continue work on refactoring, add utilities module

This commit is contained in:
calzoneman 2013-08-14 22:51:59 -05:00
parent 9675edadf5
commit 8a2dfa004d
2 changed files with 143 additions and 30 deletions

View File

@ -1,4 +1,5 @@
var mysql = require("mysql"); var mysql = require("mysql");
var $util = require("./utilities");
var Logger = { var Logger = {
errlog: { errlog: {
@ -62,6 +63,10 @@ Database.prototype.query = function (query, sub, callback) {
}); });
} }
function blackHole() {
}
Database.prototype.init = function () { Database.prototype.init = function () {
var self = this; var self = this;
var query; var query;
@ -230,7 +235,7 @@ Database.prototype.isGlobalIPBanned = function (ip, callback) {
Database.prototype.getGlobalIPBans = function (callback) { Database.prototype.getGlobalIPBans = function (callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
self.query("SELECT * FROM global_bans WHERE 1", function (err, res) { self.query("SELECT * FROM global_bans WHERE 1", function (err, res) {
if(err) { if(err) {
@ -250,7 +255,7 @@ Database.prototype.getGlobalIPBans = function (callback) {
Database.prototype.setGlobalIPBan = function (ip, reason, callback) { Database.prototype.setGlobalIPBan = function (ip, reason, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
var query = "INSERT INTO global_bans VALUES (?, ?)" + var query = "INSERT INTO global_bans VALUES (?, ?)" +
" ON DUPLICATE KEY UPDATE note=?"; " ON DUPLICATE KEY UPDATE note=?";
@ -268,7 +273,7 @@ Database.prototype.setGlobalIPBan = function (ip, reason, callback) {
Database.prototype.clearGlobalIPBan = function (ip, callback) { Database.prototype.clearGlobalIPBan = function (ip, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
var query = "DELETE FROM global_bans WHERE ip=?"; var query = "DELETE FROM global_bans WHERE ip=?";
@ -289,6 +294,10 @@ Database.prototype.channelExists = function (name, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
return; return;
if(!$util.isValidChannelName(name)) {
callback("Invalid channel name", null);
return;
}
var query = "SELECT name FROM channels WHERE name=?"; var query = "SELECT name FROM channels WHERE name=?";
self.query(query, [name], function (err, res) { self.query(query, [name], function (err, res) {
@ -299,9 +308,9 @@ Database.prototype.channelExists = function (name, callback) {
Database.prototype.registerChannel = function (name, owner, callback) { Database.prototype.registerChannel = function (name, owner, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
if(!name.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(name)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
@ -368,9 +377,9 @@ Database.prototype.registerChannel = function (name, owner, callback) {
Database.prototype.loadChannelData = function (chan, callback) { Database.prototype.loadChannelData = function (chan, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
if(!chan.name.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(chan.name)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
@ -417,9 +426,9 @@ Database.prototype.loadChannelData = function (chan, callback) {
Database.prototype.dropChannel = function (name, callback) { Database.prototype.dropChannel = function (name, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
if(!name.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(name)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
@ -449,6 +458,11 @@ Database.prototype.getChannelRank = function (channame, names, callback) {
if(typeof callback !== "function") if(typeof callback !== "function")
return; return;
if(!$util.isValidChannelName(channame)) {
callback("Invalid channel name", null);
return;
}
if(typeof names === "string") if(typeof names === "string")
names = [names]; names = [names];
@ -486,9 +500,9 @@ Database.prototype.getChannelRank = function (channame, names, callback) {
Database.prototype.setChannelRank = function (channame, name, rank, callback) { Database.prototype.setChannelRank = function (channame, name, rank, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
if(!channame.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(channame)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
@ -497,9 +511,7 @@ Database.prototype.setChannelRank = function (channame, name, rank, callback) {
"(name, rank) VALUES (?, ?) " + "(name, rank) VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE rank=?"; "ON DUPLICATE KEY UPDATE rank=?";
self.query(query, [name, rank, rank], function (err, res) { self.query(query, [name, rank, rank], callback);
callback(err, res);
});
}; };
Database.prototype.listChannelRanks = function (channame, callback) { Database.prototype.listChannelRanks = function (channame, callback) {
@ -507,23 +519,21 @@ Database.prototype.listChannelRanks = function (channame, callback) {
if(typeof callback !== "function") if(typeof callback !== "function")
return; return;
if(!channame.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(channame)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
var query = "SELECT * FROM `chan_" + channame + "_ranks` WHERE 1"; var query = "SELECT * FROM `chan_" + channame + "_ranks` WHERE 1";
self.query(query, function (err, res) { self.query(query, callback);
callback(err, res);
});
}; };
Database.prototype.addToLibrary = function (channame, media, callback) { Database.prototype.addToLibrary = function (channame, media, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
if(!channame.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(channame)) {
callback("Invalid channel name"); callback("Invalid channel name");
return; return;
} }
@ -537,33 +547,29 @@ Database.prototype.addToLibrary = function (channame, media, callback) {
media.seconds, media.seconds,
media.type media.type
]; ];
self.query(query, params, function (err, res) { self.query(query, params, callback);
callback(err, res);
});
}; };
Database.prototype.removeFromLibrary = function (channame, id, callback) { Database.prototype.removeFromLibrary = function (channame, id, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
if(!channame.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(channame)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
var query = "DELETE FROM `chan_" + channame + "_library` WHERE id=?"; var query = "DELETE FROM `chan_" + channame + "_library` WHERE id=?";
self.query(query, [id], function (err, res) { self.query(query, [id], callback);
callback(err, res);
});
}; };
Database.prototype.getLibraryItem = function (channame, id, callback) { Database.prototype.getLibraryItem = function (channame, id, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = function () { }; callback = blackHole;
if(!channame.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(channame)) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
@ -581,4 +587,96 @@ Database.prototype.getLibraryItem = function (channame, id, callback) {
}); });
}; };
Database.prototype.addChannelBan = function (channame, ip, name, banBy,
callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
if(!$util.isValidChannelName(channame))) {
callback("Invalid channel name", null);
return;
}
var query = "INSERT INTO `chan_" + channame + "_bans`" +
"(ip, name, banner) VALUES (?, ?, ?)";
self.query(query, [ip, name, banBy], callback);
};
Database.prototype.clearChannelIPBan = function (channame, ip, callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
if(!$util.isValidChannelName(channame)) {
callback("Invalid channel name", null);
return;
}
var query = "DELETE FROM `chan_" + channame + "_bans` WHERE ip=?";
self.query(query, [ip], callback);
};
Database.prototype.clearChannelNameBan = function (channame, name,
callback) {
var self = this;
if(typeof callback !== "function") {
callback = blackHole;
return;
}
var query = "DELETE FROM `chan_" + channame + "_bans` WHERE ip='*'" +
"AND name=?";
self.query(query, [name], callback);
};
/* END REGION */
/* REGION users */
Database.prototype.getUserProfile = function (name, callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
var query = "SELECT profile_image, profile_text FROM registrations " +
"WHERE uname=?";
self.query(query, [name], function (err, res) {
if(err) {
callback(err, null);
return;
}
var def = {
profile_image: "",
profile_text: ""
};
callback(null, res.length > 0 ? res[0] : def);
});
};
Database.prototype.setUserProfile = function (name, data, callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
var query = "UPDATE registrations SET profile_image=?, profile_text=?" +
"WHERE uname=?";
self.query(query, [data.image, data.text, name], callback);
};
Database.prototype.setUserEmail = function (name, email, callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
var query = "UPDATE registrations SET email=? WHERE uname=?";
self.query(query, [email, name], callback);
};
module.exports = Database; module.exports = Database;

15
utilities.js Normal file
View File

@ -0,0 +1,15 @@
module.exports = {
isValidChannelName: function (name) {
return name.match(/^[\w-_]+$/);
},
randomSalt: function (length) {
var chars = "abcdefgihjklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "0123456789!@#$%^&*_+=~";
var salt = [];
for(var i = 0; i < length; i++) {
salt.push(chars[parseInt(Math.random()*chars.length)]);
}
return salt.join('');
}
};