sync/lib/database/channels.js

635 lines
17 KiB
JavaScript
Raw Normal View History

2013-12-13 17:18:50 +00:00
var db = require("../database");
2013-12-14 00:52:13 +00:00
var valid = require("../utilities").isValidChannelName;
2014-01-26 06:01:36 +00:00
var fs = require("fs");
var path = require("path");
var Logger = require("../logger");
var tables = require("./tables");
2014-05-21 02:30:14 +00:00
var Flags = require("../flags");
var util = require("../utilities");
2013-12-13 17:18:50 +00:00
var blackHole = function () { };
function dropTable(name, callback) {
db.query("DROP TABLE `" + name + "`", callback);
2013-12-13 17:18:50 +00:00
}
function initTables(name, owner, callback) {
2013-12-26 15:17:33 +00:00
if (!valid(name)) {
2013-12-14 00:52:13 +00:00
callback("Invalid channel name", null);
return;
}
2013-12-26 15:17:33 +00:00
tables.createChannelTables(name, db.query.bind(db), function (err) {
2014-01-26 06:01:36 +00:00
db.users.getGlobalRank(owner, function (err, rank) {
2013-12-13 17:18:50 +00:00
if (err) {
callback(err, null);
return;
}
2014-02-26 20:37:51 +00:00
rank = Math.max(rank, 5);
2014-01-26 06:01:36 +00:00
module.exports.setRank(name, owner, rank, function (err) {
2013-12-13 17:18:50 +00:00
if (err) {
dropTable("chan_" + name + "_ranks");
dropTable("chan_" + name + "_ranks");
dropTable("chan_" + name + "_library");
2013-12-13 17:18:50 +00:00
callback(err, null);
return;
}
2013-12-14 00:52:13 +00:00
callback(null, true);
2013-12-13 17:18:50 +00:00
});
});
});
}
module.exports = {
init: function () {
},
/**
* Checks if the given channel name is registered
*/
isChannelTaken: function (name, callback) {
if (typeof callback !== "function") {
return;
}
2013-12-26 15:17:33 +00:00
if (!valid(name)) {
2013-12-14 00:52:13 +00:00
callback("Invalid channel name", null);
return;
}
2013-12-26 15:17:33 +00:00
db.query("SELECT name FROM `channels` WHERE name=?",
2013-12-13 17:18:50 +00:00
[name],
function (err, rows) {
if (err) {
callback(err, true);
return;
}
callback(null, rows.length > 0);
});
},
/**
* Looks up a channel
*/
lookup: function (name, callback) {
if (typeof callback !== "function") {
return;
}
if (!valid(name)) {
2013-12-14 00:52:13 +00:00
callback("Invalid channel name", null);
return;
}
2013-12-26 15:17:33 +00:00
db.query("SELECT * FROM `channels` WHERE name=?",
2013-12-13 17:18:50 +00:00
[name],
function (err, rows) {
if (err) {
callback(err, null);
return;
}
if (rows.length === 0) {
callback("No such channel", null);
} else {
callback(null, rows[0]);
}
2013-12-13 17:18:50 +00:00
});
},
/**
* Searches for a channel
*/
search: function (name, callback) {
if (typeof callback !== "function") {
return;
}
2013-12-26 15:17:33 +00:00
db.query("SELECT * FROM `channels` WHERE name LIKE ?",
2013-12-13 17:18:50 +00:00
["%" + name + "%"],
function (err, rows) {
if (err) {
callback(err, null);
return;
}
callback(null, rows);
});
},
2014-02-05 23:44:37 +00:00
/**
* Searches for a channel by owner
*/
searchOwner: function (name, callback) {
if (typeof callback !== "function") {
return;
}
db.query("SELECT * FROM `channels` WHERE owner LIKE ?",
["%" + name + "%"],
function (err, rows) {
if (err) {
callback(err, null);
return;
}
callback(null, rows);
});
},
2013-12-13 17:18:50 +00:00
/**
* Validates and registers a new channel
*/
register: function (name, owner, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (typeof name !== "string" || typeof owner !== "string") {
callback("Name and owner are required for channel registration", null);
return;
}
2013-12-14 00:52:13 +00:00
if (!valid(name)) {
2013-12-13 17:18:50 +00:00
callback("Invalid channel name. Channel names may consist of 1-30 " +
"characters a-z, A-Z, 0-9, -, and _", null);
return;
}
module.exports.isChannelTaken(name, function (err, taken) {
if (err) {
callback(err, null);
return;
}
if (taken) {
callback("Channel name " + name + " is already taken", null);
return;
}
2013-12-26 15:17:33 +00:00
db.query("INSERT INTO `channels` " +
2014-01-26 06:01:36 +00:00
"(`name`, `owner`, `time`) VALUES (?, ?, ?)",
[name, owner, Date.now()],
function (err, res) {
2013-12-13 17:18:50 +00:00
if (err) {
callback(err, null);
return;
}
initTables(name, owner, function (err, res) {
if (err) {
2013-12-26 15:17:33 +00:00
db.query("DELETE FROM `channels` WHERE name=?", [name]);
2013-12-13 17:18:50 +00:00
callback(err, null);
return;
}
callback(null, {
name: name
});
});
});
});
},
/**
* Unregisters a channel
*/
drop: function (name, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(name)) {
2013-12-14 00:52:13 +00:00
callback("Invalid channel name", null);
return;
}
2013-12-13 17:18:50 +00:00
dropTable("chan_" + name + "_ranks", function (err) {
dropTable("chan_" + name + "_bans", function (e2) {
if (err && e2) {
err += "\n" + e2;
} else if (e2) {
err = e2;
}
dropTable("chan_" + name + "_library", function (e3) {
if (err && e3) {
err += "\n" + e3;
} else if (e3) {
err = e3;
}
db.query("DELETE FROM `channels` WHERE name=?", [name],
function (e4) {
if (err && e4) {
err += "\n" + e4;
} else if (e4) {
err = e4;
}
2014-01-26 06:01:36 +00:00
fs.unlink(path.join(__dirname, "..", "..", "chandump", name),
function (err) {
2014-02-05 23:44:37 +00:00
if (err && err.code !== "ENOENT") {
2014-01-26 06:01:36 +00:00
Logger.errlog.log("Deleting chandump failed:");
Logger.errlog.log(err);
}
});
2013-12-13 17:18:50 +00:00
callback(err, !Boolean(err));
2013-12-14 00:52:13 +00:00
});
2013-12-13 17:18:50 +00:00
});
});
});
},
/**
* Looks up channels registered by a given user
*/
listUserChannels: function (owner, callback) {
if (typeof callback !== "function") {
return;
}
db.query("SELECT * FROM `channels` WHERE owner=?", [owner],
function (err, res) {
if (err) {
callback(err, []);
return;
}
callback(err, res);
});
},
2013-12-14 00:52:13 +00:00
/**
* Loads the channel from the database
*/
load: function (chan, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan.name)) {
callback("Invalid channel name", null);
return;
}
db.query("SELECT * FROM `channels` WHERE name=?", chan.name, function (err, res) {
if (err) {
callback(err, null);
return;
}
if (res.length === 0) {
callback("Channel is not registered", null);
return;
}
if (chan.dead) {
callback("Channel is dead", null);
return;
}
// Note that before this line, chan.name might have a different capitalization
// than the database has stored. Update accordingly.
chan.name = res[0].name;
2014-02-16 19:12:49 +00:00
chan.uniqueName = chan.name.toLowerCase();
2014-05-21 02:30:14 +00:00
chan.setFlag(Flags.C_REGISTERED);
2014-02-08 18:45:07 +00:00
chan.logger.log("[init] Loaded channel from database");
2014-01-12 05:55:52 +00:00
callback(null, true);
2013-12-14 00:52:13 +00:00
});
},
2013-12-13 17:18:50 +00:00
/**
* Looks up a user's rank
*/
getRank: function (chan, name, callback) {
if (typeof callback !== "function") {
return;
}
2013-12-14 00:52:13 +00:00
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
2013-12-13 17:18:50 +00:00
db.query("SELECT name,rank FROM `chan_" + chan + "_ranks` WHERE name=?",
[name],
function (err, rows) {
if (err) {
2014-05-21 02:30:14 +00:00
callback(err, -1);
2013-12-13 17:18:50 +00:00
return;
}
if (rows.length === 0) {
callback(null, 1);
return;
}
callback(null, rows[0].rank);
});
},
/**
* Looks up multiple users' ranks at once
*/
getRanks: function (chan, names, callback) {
if (typeof callback !== "function") {
return;
}
2013-12-14 00:52:13 +00:00
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
2013-12-13 17:18:50 +00:00
var replace = "(" + names.map(function () { return "?"; }).join(",") + ")";
db.query("SELECT name,rank FROM `chan_" + chan + "_ranks` WHERE name IN " +
2014-02-28 06:09:20 +00:00
replace, names,
2013-12-13 17:18:50 +00:00
function (err, rows) {
if (err) {
callback(err, []);
return;
}
2014-03-03 04:00:24 +00:00
callback(null, rows.map(function (r) { return r.rank; }));
2013-12-13 17:18:50 +00:00
});
},
2013-12-14 00:52:13 +00:00
/**
* Query all user ranks at once
*/
allRanks: function (chan, callback) {
if (typeof callback !== "function") {
return;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("SELECT name,rank FROM `chan_" + chan + "_ranks` WHERE 1", callback);
},
/**
* Updates a user's rank
*/
setRank: function (chan, name, rank, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
2014-01-09 23:16:09 +00:00
if (rank < 2) {
module.exports.deleteRank(chan, name, callback);
return;
}
2013-12-14 00:52:13 +00:00
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("INSERT INTO `chan_" + chan + "_ranks` (name, rank) VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE rank=?", [name, rank, rank], callback);
},
/**
* Inserts a new user rank entry without clobbering an existing one
*/
newRank: function (chan, name, rank, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("INSERT INTO `chan_" + chan + "_ranks` (name, rank) VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE rank=rank", [name, rank], callback);
},
/**
* Removes a user's rank entry
*/
deleteRank: function (chan, name, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("DELETE FROM `chan_" + chan + "_ranks` WHERE name=?", [name], callback);
},
/**
* Adds a media item to the library
*/
addToLibrary: function (chan, media, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
2014-06-04 04:21:00 +00:00
var meta = JSON.stringify({
bitrate: media.meta.bitrate,
codec: media.meta.codec
});
db.query("INSERT INTO `chan_" + chan + "_library` (id, title, seconds, type, meta) " +
"VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE id=id",
[media.id, media.title, media.seconds, media.type, meta], callback);
2013-12-14 00:52:13 +00:00
},
/**
* Retrieves a media item from the library by id
*/
getLibraryItem: function (chan, id, callback) {
if (typeof callback !== "function") {
return;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("SELECT * FROM `chan_" + chan + "_library` WHERE id=?", [id],
function (err, rows) {
if (err) {
callback(err, null);
return;
}
if (rows.length === 0) {
callback("Item not in library", null);
} else {
callback(null, rows[0]);
}
});
},
/**
* Search the library by title
*/
searchLibrary: function (chan, search, callback) {
if (typeof callback !== "function") {
return;
}
db.query("SELECT * FROM `chan_" + chan + "_library` WHERE title LIKE ?",
["%" + search + "%"], callback);
},
/**
* Deletes a media item from the library
*/
deleteFromLibrary: function (chan, id, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("DELETE FROM `chan_" + chan + "_library` WHERE id=?", [id], callback);
},
/**
* Add a ban to the banlist
*/
ban: function (chan, ip, name, note, bannedby, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("INSERT INTO `chan_" + chan + "_bans` (ip, name, reason, bannedby) " +
2014-01-09 02:12:02 +00:00
"VALUES (?, ?, ?, ?)", [ip, name, note, bannedby], callback);
2013-12-14 00:52:13 +00:00
},
2014-01-12 05:55:52 +00:00
/**
* Check if an IP address or range is banned
*/
isIPBanned: function (chan, ip, callback) {
if (typeof callback !== "function") {
return;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
2014-05-21 02:30:14 +00:00
var range = util.getIPRange(ip);
var wrange = util.getWideIPRange(ip);
2014-03-25 22:22:48 +00:00
2014-05-21 02:30:14 +00:00
db.query("SELECT * FROM `chan_" + chan + "_bans` WHERE ip IN (?, ?, ?)",
[ip, range, wrange],
2014-01-12 05:55:52 +00:00
function (err, rows) {
callback(err, err ? false : rows.length > 0);
});
},
/**
* Check if a username is banned
*/
isNameBanned: function (chan, name, callback) {
if (typeof callback !== "function") {
return;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("SELECT * FROM `chan_" + chan + "_bans` WHERE name=?", [name],
function (err, rows) {
callback(err, err ? false : rows.length > 0);
});
},
/**
* Lists all bans
*/
listBans: function (chan, callback) {
if (typeof callback !== "function") {
return;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("SELECT * FROM `chan_" + chan + "_bans` WHERE 1", callback);
},
2013-12-14 00:52:13 +00:00
/**
* Removes a ban from the banlist
*/
unbanName: function (chan, name, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("DELETE FROM `chan_" + chan + "_bans` WHERE ip='*' AND name=?",
[name], callback);
},
/**
* Removes a ban from the banlist
*/
unbanIP: function (chan, ip, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("DELETE FROM `chan_" + chan + "_bans` WHERE ip=?",
[ip], callback);
2014-01-12 05:55:52 +00:00
},
/**
* Removes a ban from the banlist
*/
unbanId: function (chan, id, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("DELETE FROM `chan_" + chan + "_bans` WHERE id=?",
[id], callback);
2013-12-14 00:52:13 +00:00
}
2013-12-13 17:18:50 +00:00
};