sync/channel.js

1109 lines
36 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.
*/
2013-02-16 17:19:59 +00:00
var fs = require("fs");
var mysql = require("mysql-libmysqlclient");
var Config = require("./config.js");
var Rank = require("./rank.js");
2013-02-16 05:02:42 +00:00
// I should use the <noun><verb>er naming scheme more often
var InfoGetter = require("./get-info.js");
var Media = require("./media.js").Media;
var ChatCommand = require("./chatcommand.js");
var Server = require("./server.js");
2013-03-23 18:29:47 +00:00
var io = Server.io;
2013-03-27 19:28:51 +00:00
var Logger = require("./logger.js");
2013-04-02 19:07:22 +00:00
var Poll = require("./poll.js").Poll;
2013-02-16 05:02:42 +00:00
var Channel = function(name) {
2013-03-27 19:28:51 +00:00
Logger.syslog.log("Opening channel " + name);
2013-02-16 05:02:42 +00:00
this.name = name;
this.registered = false;
this.users = [];
this.queue = [];
this.library = {};
this.currentPosition = -1;
this.currentMedia = null;
this.leader = null;
this.recentChat = [];
2013-03-16 20:39:58 +00:00
this.qlocked = true;
2013-03-16 21:49:58 +00:00
this.poll = false;
2013-04-02 19:07:22 +00:00
this.voteskip = false;
this.opts = {
qopen_allow_qnext: false,
qopen_allow_move: false,
qopen_allow_playnext: false,
qopen_allow_delete: false,
2013-04-02 19:07:22 +00:00
allow_voteskip: true,
pagetitle: "Sync",
2013-03-27 19:28:51 +00:00
customcss: ""
};
2013-03-29 18:15:46 +00:00
this.filters = [
[/`([^`]+)`/g , "<code>$1</code>" , true],
[/\*([^\*]+)\*/g , "<strong>$1</strong>", true],
[/(^| )_([^_]+)_/g , "$1<em>$2</em>" , true],
[/\\\\([-a-zA-Z0-9]+)/g, "[](/$1)" , true]
2013-03-29 18:15:46 +00:00
];
2013-04-01 21:02:09 +00:00
this.motd = {
motd: "",
html: ""
};
2013-02-16 05:02:42 +00:00
2013-03-27 19:28:51 +00:00
this.ipbans = {};
this.logger = new Logger.Logger("chanlogs/" + this.name + ".log");
fs.readFile("chandump/" + this.name, function(err, data) {
if(err) {
return;
}
try {
this.logger.log("*** Loading channel dump from disk");
data = JSON.parse(data);
for(var i = 0; i < data.queue.length; i++) {
var e = data.queue[i];
this.queue.push(new Media(e.id, e.title, e.seconds, e.type));
}
this.sendAll("playlist", {
pl: this.queue
});
this.currentPosition = data.currentPosition - 1;
2013-04-02 15:55:14 +00:00
if(this.currentPosition < -1)
this.currentPosition = -1;
if(this.queue.length > 0)
this.playNext();
this.opts = data.opts;
if(data.filters) {
this.filters = new Array(data.filters.length);
for(var i = 0; i < data.filters.length; i++) {
this.filters[i] = [new RegExp(data.filters[i][0]),
data.filters[i][1],
data.filters[i][2]];
}
}
2013-04-01 21:02:09 +00:00
if(data.motd) {
this.motd = data.motd;
}
}
catch(e) {
Logger.errlog.log("Channel dump load failed: ");
Logger.errlog.log(e);
}
}.bind(this));
2013-03-24 01:08:35 +00:00
// Autolead stuff
// Accumulator
this.i = 0;
// Time of last update
this.time = new Date().getTime();
2013-02-16 05:02:42 +00:00
this.loadMysql();
};
// Check if this channel is registered
// If it is, fetch the library
Channel.prototype.loadMysql = function() {
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.loadMysql: MySQL Connection Failed");
2013-02-16 17:23:51 +00:00
return false;
2013-02-16 05:02:42 +00:00
}
// Check if channel exists
var query = "SELECT * FROM channels WHERE name='{}'"
2013-02-16 05:02:42 +00:00
.replace(/\{\}/, this.name);
var results = db.querySync(query);
2013-03-18 17:17:56 +00:00
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.loadMysql: Channel query failed");
2013-03-18 17:17:56 +00:00
return;
}
2013-02-16 05:02:42 +00:00
var rows = results.fetchAllSync();
if(rows.length == 0) {
2013-03-27 19:28:51 +00:00
Logger.syslog.log("Channel " + this.name + " is unregistered.");
2013-02-16 05:02:42 +00:00
return;
}
this.registered = true;
// Load library
var query = "SELECT * FROM chan_{}_library"
2013-02-16 05:02:42 +00:00
.replace(/\{\}/, this.name);
var results = db.querySync(query);
2013-03-18 17:17:56 +00:00
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.loadMysql: failed to load library for " + this.name);
2013-03-18 17:17:56 +00:00
return;
}
2013-02-16 05:02:42 +00:00
var rows = results.fetchAllSync();
for(var i = 0; i < rows.length; i++) {
this.library[rows[i].id] = new Media(rows[i].id, rows[i].title, rows[i].seconds, rows[i].type);
}
2013-03-24 03:01:37 +00:00
// Load bans
var query = "SELECT * FROM chan_{}_bans"
2013-03-24 03:01:37 +00:00
.replace(/\{\}/, this.name);
var results = db.querySync(query);
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.loadMysql: failed to load banlist for " + this.name);
2013-03-24 03:01:37 +00:00
return;
}
var rows = results.fetchAllSync();
for(var i = 0; i < rows.length; i++) {
this.ipbans[rows[i].ip] = [rows[i].name, rows[i].banner];
}
2013-03-27 19:28:51 +00:00
Logger.syslog.log("Loaded channel " + this.name + " from database");
2013-02-16 05:02:42 +00:00
db.closeSync();
}
// Creates a new channel record in the MySQL Database
// Currently unused, but might be useful if I add a registration page
Channel.prototype.createTables = function() {
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.createTables: DB connection failed");
2013-02-16 17:23:51 +00:00
return false;
2013-02-16 05:02:42 +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(/\{\}/, this.name);
var results = db.querySync(query);
// Create rank table
var query = "CREATE TABLE `chan_{}_ranks` (\
`name` VARCHAR( 32 ) NOT NULL ,\
`rank` INT NOT NULL ,\
UNIQUE (\
`name`\
)\
) ENGINE = MYISAM"
.replace(/\{\}/, this.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(/\{\}/, this.name);
results = db.querySync(query) || results;
2013-02-16 05:02:42 +00:00
// Insert into global channel table
var query = "INSERT INTO channels (`id`, `name`) VALUES (NULL, '{}')"
2013-02-16 05:02:42 +00:00
.replace(/\{\}/, this.name);
2013-03-17 17:14:34 +00:00
results = db.querySync(query) || results;
2013-02-16 05:02:42 +00:00
db.closeSync();
return results;
}
2013-03-17 17:14:34 +00:00
Channel.prototype.tryRegister = function(user) {
if(this.registered) {
user.socket.emit("registerChannel", {
2013-03-17 17:14:34 +00:00
success: false,
error: "This channel is already registered"
});
}
else if(!user.loggedIn) {
user.socket.emit("registerChannel", {
2013-03-17 17:14:34 +00:00
success: false,
error: "You must log in to register a channel"
});
}
else if(!Rank.hasPermission(user, "registerChannel")) {
user.socket.emit("registerChannel", {
2013-03-17 17:14:34 +00:00
success: false,
error: "You don't have permission to register this channel"
});
}
else {
if(this.createTables()) {
this.registered = true;
this.saveRank(user);
user.socket.emit("registerChannel", {
2013-03-17 17:14:34 +00:00
success: true,
});
2013-03-27 19:28:51 +00:00
this.logger.log("*** " + user.name + " registered the channel");
Logger.syslog.log("Channel " + this.name + " was registered by " + user.name);
2013-03-17 17:14:34 +00:00
}
else {
user.socket.emit("registerChannel", {
2013-03-17 17:14:34 +00:00
success: false,
error: "Unable to register channel, see an admin"
});
}
}
}
// Retrieves a user"s rank from the database
2013-02-16 05:02:42 +00:00
Channel.prototype.getRank = function(name) {
if(!this.registered)
2013-02-16 05:02:42 +00:00
return Rank.Guest;
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.getRank: DB connection failed");
2013-02-16 17:23:51 +00:00
return Rank.Guest;
2013-02-16 05:02:42 +00:00
}
var query = "SELECT * FROM chan_{1}_ranks WHERE name='{2}'"
2013-02-16 05:02:42 +00:00
.replace(/\{1\}/, this.name)
.replace(/\{2\}/, name);
var results = db.querySync(query);
if(!results)
return Rank.Guest;
2013-02-16 05:02:42 +00:00
var rows = results.fetchAllSync();
if(rows.length == 0) {
return Rank.Guest;
}
db.closeSync();
return rows[0].rank;
}
// Saves a user"s rank to the database
2013-02-16 05:02:42 +00:00
Channel.prototype.saveRank = function(user) {
if(!this.registered)
return false;
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.saveRank: DB connection failed");
2013-02-16 17:23:51 +00:00
return false;
2013-02-16 05:02:42 +00:00
}
var query = "UPDATE chan_{1}_ranks SET rank='{2}' WHERE name='{3}'"
2013-02-16 05:02:42 +00:00
.replace(/\{1\}/, this.name)
.replace(/\{2\}/, user.rank)
.replace(/\{3\}/, user.name);
var results = db.querySync(query);
// Gonna have to insert a new one, bugger
2013-03-17 16:49:25 +00:00
if(!results.fetchAllSync) {
var query = "INSERT INTO chan_{1}_ranks (`name`, `rank`) VALUES ('{2}', '{3}')"
2013-02-16 05:02:42 +00:00
.replace(/\{1\}/, this.name)
.replace(/\{2\}/, user.name)
.replace(/\{3\}/, user.rank);
results = db.querySync(query);
}
db.closeSync();
return results;
}
// Caches media metadata to the channel library.
// If the channel is registered, stores it in the database as well
Channel.prototype.addToLibrary = function(media) {
this.library[media.id] = media;
if(!this.registered)
return;
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.addToLibrary: DB connection failed");
2013-02-16 17:23:51 +00:00
return false;
2013-02-16 05:02:42 +00:00
}
var query = "INSERT INTO chan_{1}_library VALUES ('{2}', '{3}', {4}, '{5}', '{6}')"
2013-02-16 05:02:42 +00:00
.replace(/\{1\}/, this.name)
.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-03-24 03:01:37 +00:00
Channel.prototype.banIP = function(banner, bannee) {
// It is assumed that the banner has permission at this point
this.ipbans[bannee.ip] = [bannee.name, banner.name];
bannee.socket.disconnect();
2013-03-24 03:45:10 +00:00
this.broadcastIpbans();
2013-03-24 03:01:37 +00:00
if(!this.registered)
return false;
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.banIP: DB connection failed");
2013-03-24 03:01:37 +00:00
return false;
}
var query = "INSERT INTO chan_{1}_bans (`ip`, `name`, `banner`) VALUES ('{2}', '{3}', '{4}')"
2013-03-24 03:01:37 +00:00
.replace(/\{1\}/, this.name)
.replace(/\{2\}/, bannee.ip)
.replace(/\{3\}/, bannee.name)
.replace(/\{4\}/, banner.name);
results = db.querySync(query);
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.banIP: Insert failed");
}
else {
this.logger.log(bannee.ip + " (" + bannee.name + ") was banned by " + banner.name);
2013-03-24 03:01:37 +00:00
}
db.closeSync();
return results;
}
2013-03-24 03:45:10 +00:00
Channel.prototype.unbanIP = function(ip) {
this.ipbans[ip] = null;
this.broadcastIpbans();
if(!this.registered)
return false;
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.unbanIP: DB connection failed");
2013-03-24 03:45:10 +00:00
return false;
}
var query = "DELETE FROM chan_{1}_bans WHERE `ip` = '{2}'"
2013-03-24 03:45:10 +00:00
.replace(/\{1\}/, this.name)
.replace(/\{2\}/, ip);
results = db.querySync(query);
if(!results) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("Channel.unbanIP: Failed to delete entry");
2013-03-24 03:45:10 +00:00
return;
}
db.closeSync();
return results;
}
2013-02-16 05:02:42 +00:00
// Searches the local library for media titles containing query
Channel.prototype.searchLibrary = function(query) {
query = query.toLowerCase();
var results = [];
for(var id in this.library) {
if(this.library[id].title.toLowerCase().indexOf(query) != -1) {
results.push(this.library[id]);
}
}
results.sort(function(a, b) {
var x = a.title.toLowerCase();
var y = b.title.toLowerCase();
return (x == y) ? 0 : (x < y ? -1 : 1);
});
return results;
}
// Called when a new user enters the channel
Channel.prototype.userJoin = function(user) {
2013-03-24 03:01:37 +00:00
if(user.ip in this.ipbans && this.ipbans[user.ip] != null) {
2013-03-27 19:28:51 +00:00
this.logger.log("--- Kicking /" + user.ip + " - banned");
2013-03-24 03:01:37 +00:00
user.socket.disconnect();
return;
2013-03-24 01:08:35 +00:00
}
user.socket.join(this.name);
2013-03-17 14:39:22 +00:00
// Prevent duplicate login
if(user.name != "") {
for(var i = 0; i < this.users.length; i++) {
if(this.users[i].name == user.name) {
user.name = "";
user.loggedIn = false;
user.socket.emit("login", {
2013-03-17 14:39:22 +00:00
success: false,
error: "The username " + user.name + " is already in use on this channel"
});
}
}
}
// If the channel is empty and isn"t registered, the first person
2013-02-16 05:02:42 +00:00
// gets ownership of the channel (temporarily)
if(this.users.length == 0 && !this.registered) {
2013-03-17 17:14:34 +00:00
user.rank = (user.rank < Rank.Owner) ? Rank.Owner + 7 : user.rank;
user.socket.emit("channelNotRegistered");
2013-02-16 05:02:42 +00:00
}
this.users.push(user);
if(user.name != "") {
this.broadcastNewUser(user);
}
2013-03-07 01:38:16 +00:00
this.updateUsercount();
2013-02-16 05:02:42 +00:00
// Set the new guy up
this.sendPlaylist(user);
user.socket.emit("queueLock", {locked: this.qlocked});
2013-02-16 05:02:42 +00:00
this.sendUserlist(user);
this.sendRecentChat(user);
2013-03-16 21:49:58 +00:00
if(this.poll) {
user.socket.emit("newPoll", this.poll.packUpdate());
2013-03-16 21:49:58 +00:00
}
user.socket.emit("channelOpts", this.opts);
2013-04-01 21:02:09 +00:00
user.socket.emit("updateMotd", this.motd);
2013-02-16 05:02:42 +00:00
if(user.playerReady)
this.sendMediaUpdate(user);
2013-03-27 19:28:51 +00:00
this.logger.log("+++ /" + user.ip + " joined");
Logger.syslog.log("/" + user.ip + " joined channel " + this.name);
2013-02-16 05:02:42 +00:00
}
// Called when a user leaves the channel
Channel.prototype.userLeave = function(user) {
try {
user.socket.leave(this.name);
}
catch(e) {}
2013-03-16 21:49:58 +00:00
if(this.poll) {
this.poll.unvote(user.ip);
this.broadcastPollUpdate();
}
if(this.leader == user) {
this.changeLeader("");
}
2013-03-24 02:05:13 +00:00
var idx = this.users.indexOf(user);
if(idx >= 0 && idx < this.users.length)
this.users.splice(idx, 1);
2013-03-07 01:38:16 +00:00
this.updateUsercount();
2013-02-16 05:02:42 +00:00
if(user.name != "") {
this.sendAll("userLeave", {
2013-02-16 05:02:42 +00:00
name: user.name
});
}
2013-03-27 19:28:51 +00:00
this.logger.log("--- /" + user.ip + " (" + user.name + ") left");
2013-02-16 05:02:42 +00:00
}
// Queues a new media
Channel.prototype.enqueue = function(data) {
var idx = data.pos == "next" ? this.currentPosition + 1 : this.queue.length;
// Try to look up cached metadata first
if(data.id in this.library) {
this.queue.splice(idx, 0, this.library[data.id]);
this.sendAll("queue", {
2013-02-16 05:02:42 +00:00
media: this.library[data.id].pack(),
pos: idx
});
2013-03-27 19:28:51 +00:00
this.logger.log("*** Queued from cache: id=" + data.id);
2013-02-16 05:02:42 +00:00
}
// Query metadata from YouTube
else if(data.type == "yt") {
var callback = (function(chan, id) { return function(res, data) {
if(res != 200) {
return;
}
try {
// Whoever decided on this variable name should be fired
var seconds = data.entry.media$group.yt$duration.seconds;
// This one"s slightly better
var title = data.entry.title.$t;
var vid = new Media(id, title, seconds, "yt");
chan.queue.splice(idx, 0, vid);
chan.sendAll("queue", {
media: vid.pack(),
pos: idx
});
chan.addToLibrary(vid);
2013-03-27 19:28:51 +00:00
chan.logger.log("*** Queued new YT video: " + id);
}
catch(e) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("YTQueue Fail: id=" + id);
console.log(e);
}
2013-02-16 05:02:42 +00:00
}})(this, data.id);
InfoGetter.getYTInfo(data.id, callback);
}
2013-03-25 19:39:03 +00:00
// YouTube Playlist
else if(data.type == "yp") {
var callback = (function(chan, plid) { return function(res, data) {
2013-03-25 19:39:03 +00:00
if(res != 200) {
return;
}
try {
for(var i = 0; i < data.feed.entry.length; i++) {
var item = data.feed.entry[i];
var title = item.title.$t;
var url = item.link[1].href;
var parts = url.split("/");
// INCOMING H4X BECAUSE YTAPI IS INCONSISTENT
var last = parts[parts.length - 1];
var match = last.match(/watch\?v=([^&]+)/);
var id;
if(match) {
id = match[1];
}
else {
id = parts[parts.length - 2];
}
2013-03-25 19:39:03 +00:00
var seconds = item.media$group.yt$duration.seconds;
var vid = new Media(id, title, seconds, "yt");
chan.queue.splice(idx, 0, vid);
chan.sendAll("queue", {
media: vid.pack(),
pos: idx
});
chan.addToLibrary(vid);
idx++;
}
chan.logger.log("*** Queued YT Playlist: id=" + plid);
2013-03-25 19:39:03 +00:00
}
catch(e) {
2013-03-27 19:28:51 +00:00
Logger.errlog.log("YTPlaylist Failed: id=", id);
2013-03-25 19:39:03 +00:00
console.log(e);
}
}})(this, data.id);
InfoGetter.getYTPlaylist(data.id, callback);
}
2013-02-16 05:02:42 +00:00
// Set up twitch metadata
else if(data.type == "tw") {
var media = new Media(data.id, "Twitch ~ " + data.id, 0, "tw");
this.queue.splice(idx, 0, media);
this.sendAll("queue", {
2013-02-16 05:02:42 +00:00
media: media.pack(),
pos: idx
});
2013-03-27 19:28:51 +00:00
this.logger.log("*** Queued Twitch channel: " + data.id);
2013-02-16 05:02:42 +00:00
}
2013-03-16 22:17:36 +00:00
else if(data.type == "li") {
var media = new Media(data.id, "Livestream ~ " + data.id, 0, "li");
this.queue.splice(idx, 0, media);
this.sendAll("queue", {
2013-03-16 22:17:36 +00:00
media: media.pack(),
pos: idx
});
2013-03-27 19:28:51 +00:00
this.logger.log("*** Queued Livestream channel: " + data.id);
2013-03-16 22:17:36 +00:00
}
2013-02-16 05:02:42 +00:00
// Query metadata from Soundcloud
else if(data.type == "sc") {
var callback = (function(chan, id) { return function(res, data) {
if(res != 200) {
return;
}
2013-03-27 19:28:51 +00:00
try {
var seconds = data.duration / 1000;
var title = data.title;
var vid = new Media(id, title, seconds, "sc");
chan.queue.splice(idx, 0, vid);
chan.sendAll("queue", {
media: vid.pack(),
pos: idx
});
chan.addToLibrary(vid);
chan.logger.log("*** Queued Soundcloud id=" + id);
}
catch(e) {
Logger.errlog.log("SCQueue fail: id=" + id);
Logger.errlog.log(e);
}
2013-02-16 05:02:42 +00:00
}})(this, data.id);
InfoGetter.getSCInfo(data.id, callback);
}
// Query metadata from Vimeo
else if(data.type == "vi") {
var callback = (function(chan, id) { return function(res, data) {
if(res != 200) {
return;
}
2013-03-27 19:28:51 +00:00
try {
data = data[0];
var seconds = data.duration;
var title = data.title;
var vid = new Media(id, title, seconds, "vi");
chan.queue.splice(idx, 0, vid);
chan.sendAll("queue", {
media: vid.pack(),
pos: idx
});
chan.addToLibrary(vid);
chan.logger.log("*** Queued Vimeo id=" + id);
}
catch(e) {
Logger.errlog.log("VIQueue fail: id=" + id);
Logger.errlog.log(e);
}
2013-02-16 05:02:42 +00:00
}})(this, data.id);
InfoGetter.getVIInfo(data.id, callback);
}
2013-03-23 18:17:39 +00:00
// Dailymotion
else if(data.type == "dm") {
var callback = (function(chan, id) { return function(res, data) {
if(res != 200) {
return;
}
2013-03-27 19:28:51 +00:00
try {
var seconds = data.duration;
var title = data.title;
var vid = new Media(id, title, seconds, "dm");
chan.queue.splice(idx, 0, vid);
chan.sendAll("queue", {
media: vid.pack(),
pos: idx
});
chan.addToLibrary(vid);
chan.logger.log("*** Queued Dailymotion id=" + id);
}
catch(e) {
Logger.errlog.log("DMQueue fail: id=" + id);
Logger.errlog.log(e);
}
2013-03-23 18:17:39 +00:00
}})(this, data.id);
InfoGetter.getDMInfo(data.id, callback);
}
2013-02-16 05:02:42 +00:00
}
// Removes a media from the play queue
Channel.prototype.unqueue = function(data) {
// Stop trying to break my server
if(data.pos < 0 || data.pos >= this.queue.length)
return;
this.queue.splice(data.pos, 1);
this.sendAll("unqueue", {
2013-02-16 05:02:42 +00:00
pos: data.pos
});
if(data.pos < this.currentPosition) {
this.currentPosition--;
}
if(data.pos == this.currentPosition) {
this.currentPosition--;
this.playNext();
}
}
// Play the next media in the queue
Channel.prototype.playNext = function() {
2013-03-21 23:55:32 +00:00
if(this.queue.length == 0)
2013-02-16 05:02:42 +00:00
return;
2013-03-29 20:05:08 +00:00
var old = this.currentPosition;
2013-04-02 19:07:22 +00:00
this.voteskip = false;
2013-03-21 23:55:32 +00:00
if(this.currentPosition + 1 >= this.queue.length) {
this.currentPosition = -1;
}
2013-02-16 05:02:42 +00:00
this.currentPosition++;
this.currentMedia = this.queue[this.currentPosition];
this.currentMedia.currentTime = 0;
this.sendAll("mediaUpdate", this.currentMedia.packupdate());
this.sendAll("updatePlaylistIdx", {
2013-03-29 20:05:08 +00:00
old: old,
2013-02-16 05:02:42 +00:00
idx: this.currentPosition
});
// Enable autolead for non-twitch
2013-03-17 00:58:38 +00:00
if(this.leader == null && this.currentMedia.type != "tw" && this.currentMedia.type != "li") {
this.time = new Date().getTime();
2013-02-16 05:02:42 +00:00
channelVideoUpdate(this, this.currentMedia.id);
}
}
Channel.prototype.jumpTo = function(pos) {
if(this.queue.length == 0)
return;
if(pos >= this.queue.length || pos < 0)
return;
2013-04-02 19:07:22 +00:00
this.voteskip = false;
2013-03-29 20:05:08 +00:00
var old = this.currentPosition;
this.currentPosition = pos;
this.currentMedia = this.queue[this.currentPosition];
this.currentMedia.currentTime = 0;
this.sendAll("mediaUpdate", this.currentMedia.packupdate());
this.sendAll("updatePlaylistIdx", {
2013-03-29 20:05:08 +00:00
idx: this.currentPosition,
old: old
});
// Enable autolead for non-twitch
if(this.leader == null && this.currentMedia.type != "tw" && this.currentMedia.type != "li") {
this.time = new Date().getTime();
channelVideoUpdate(this, this.currentMedia.id);
}
}
2013-03-16 20:39:58 +00:00
Channel.prototype.setLock = function(locked) {
this.qlocked = locked;
this.sendAll("queueLock", {locked: locked});
2013-03-16 20:39:58 +00:00
for(var i = 0; i < this.users.length; i++) {
this.sendPlaylist(this.users[i]);
}
}
2013-02-16 05:02:42 +00:00
// Synchronize to a sync packet from the leader
Channel.prototype.update = function(data) {
2013-02-16 22:33:38 +00:00
if(this.currentMedia == null) {
this.currentMedia = new Media(data.id, data.title, data.seconds, data.type);
this.currentMedia.currentTime = data.currentTime;
2013-02-16 22:33:38 +00:00
}
2013-02-16 05:02:42 +00:00
else
this.currentMedia.currentTime = data.seconds;
this.sendAll("mediaUpdate", this.currentMedia.packupdate());
2013-02-16 05:02:42 +00:00
}
// Move something around in the queue
Channel.prototype.moveMedia = function(data) {
if(data.src < 0 || data.src >= this.queue.length)
return;
if(data.dest < 0 || data.dest > this.queue.length)
return;
var media = this.queue[data.src];
2013-04-02 15:55:14 +00:00
// Took me a while to figure out why the queue was going insane
// Gotta account for when you've copied it to the destination
// but not removed the source yet
var dest = data.dest > data.src ? data.dest + 1 : data.dest;
var src = data.dest > data.src ? data.src : data.src + 1;
this.queue.splice(dest, 0, media);
this.queue.splice(src, 1);
this.sendAll("moveVideo", {
2013-02-16 05:02:42 +00:00
src: data.src,
dest: data.dest
});
2013-03-29 20:05:08 +00:00
if(data.src < this.currentPosition && data.dest >= this.currentPosition) {
2013-02-16 05:02:42 +00:00
this.currentPosition--;
}
2013-03-29 20:05:08 +00:00
else if(data.src > this.currentPosition && data.dest < this.currentPosition) {
2013-02-16 05:02:42 +00:00
this.currentPosition++
}
2013-03-29 20:05:08 +00:00
else if(data.src == this.currentPosition) {
this.currentPosition = data.dest;
2013-03-29 20:05:08 +00:00
}
2013-02-16 05:02:42 +00:00
}
Channel.prototype.updateFilter = function(filter) {
var found = false;
for(var i = 0; i < this.filters.length; i++) {
if(this.filters[i][0].source == filter[0].source) {
found = true;
this.filters[i][1] = filter[1];
this.filters[i][2] = filter[2];
}
}
if(!found) {
this.filters.push(filter);
}
this.broadcastChatFilters();
}
Channel.prototype.removeFilter = function(regex) {
for(var i = 0; i < this.filters.length; i++) {
if(this.filters[i][0].source == regex) {
this.filters.splice(i, 1);
break;
}
}
this.broadcastChatFilters();
}
2013-02-16 05:02:42 +00:00
// Chat message from a user
Channel.prototype.chatMessage = function(user, msg) {
2013-03-05 18:51:58 +00:00
if(msg.indexOf("/") == 0)
2013-03-05 19:57:00 +00:00
ChatCommand.handle(this, user, msg);
2013-03-05 18:51:58 +00:00
2013-02-16 05:02:42 +00:00
else if(msg.indexOf(">") == 0)
2013-03-05 18:51:58 +00:00
this.sendMessage(user.name, msg, "greentext");
else
2013-03-05 19:57:00 +00:00
this.sendMessage(user.name, msg, "");
2013-03-05 18:51:58 +00:00
}
2013-04-01 21:02:09 +00:00
Channel.prototype.filterMessage = function(msg) {
2013-03-29 18:15:46 +00:00
msg = msg.replace(/(((https?)|(ftp))(:\/\/[0-9a-zA-Z\.]+(:[0-9]+)?[^\s$]+))/g, "<a href=\"$1\" target=\"_blank\">$1</a>");
// Apply other filters
for(var i = 0; i < this.filters.length; i++) {
if(!this.filters[i][2])
continue;
var regex = this.filters[i][0];
var replace = this.filters[i][1];
msg = msg.replace(regex, replace);
}
2013-04-01 21:02:09 +00:00
return msg;
}
Channel.prototype.sendMessage = function(username, msg, msgclass) {
// I don"t want HTML from strangers
msg = msg.replace(/</g, "&lt;").replace(/>/g, "&gt;");
msg = this.filterMessage(msg);
this.sendAll("chatMsg", {
2013-03-05 18:51:58 +00:00
username: username,
2013-02-16 05:02:42 +00:00
msg: msg,
msgclass: msgclass
});
this.recentChat.push({
2013-03-05 18:51:58 +00:00
username: username,
2013-02-16 05:02:42 +00:00
msg: msg,
msgclass: msgclass
});
if(this.recentChat.length > 15)
this.recentChat.shift();
2013-03-27 19:28:51 +00:00
this.logger.log("<" + username + "." + msgclass + "> " + msg);
2013-03-05 18:51:58 +00:00
};
2013-02-16 05:02:42 +00:00
// Promotion! Actor is the client who initiated the promotion, name is the
// name of the person being promoted
Channel.prototype.promoteUser = function(actor, name) {
var receiver;
for(var i = 0; i < this.users.length; i++) {
if(this.users[i].name == name) {
receiver = this.users[i];
break;
}
}
if(receiver) {
// You can only promote someone if you are 2 ranks or higher above
// them. This way you can"t promote them to your rank and end
// up in a situation where you can"t demote them
2013-02-16 05:02:42 +00:00
if(actor.rank > receiver.rank + 1) {
receiver.rank++;
if(receiver.loggedIn) {
this.saveRank(receiver);
}
2013-03-27 19:28:51 +00:00
this.logger.log("*** " + actor.name + " promoted " + receiver.name + " from " + (receiver.rank - 1) + " to " + receiver.rank);
2013-02-16 05:02:42 +00:00
this.broadcastRankUpdate(receiver);
}
}
}
// You"re fired
2013-02-16 05:02:42 +00:00
Channel.prototype.demoteUser = function(actor, name) {
var receiver;
for(var i = 0; i < this.users.length; i++) {
if(this.users[i].name == name) {
receiver = this.users[i];
break;
}
}
if(receiver) {
// Wouldn"t it be funny if you could demote people who rank higher
// than you? No, it wouldn"t.
2013-02-16 05:02:42 +00:00
if(actor.rank > receiver.rank) {
receiver.rank--;
if(receiver.loggedIn) {
this.saveRank(receiver);
}
2013-03-27 19:28:51 +00:00
this.logger.log("*** " + actor.name + " demoted " + receiver.name + " from " + (receiver.rank + 1) + " to " + receiver.rank);
2013-02-16 05:02:42 +00:00
this.broadcastRankUpdate(receiver);
}
}
}
// Manual leader. This shouldn"t be necessary since the server autoleads,
2013-02-16 05:02:42 +00:00
// but you never know
Channel.prototype.changeLeader = function(name) {
if(this.leader != null) {
2013-02-17 05:00:33 +00:00
var old = this.leader;
2013-02-16 05:02:42 +00:00
this.leader = null;
2013-02-16 22:33:38 +00:00
this.broadcastRankUpdate(old);
2013-02-16 05:02:42 +00:00
}
if(name == "") {
2013-03-27 19:28:51 +00:00
this.logger.log("*** Resuming autolead");
if(this.currentMedia != null) {
this.time = new Date().getTime();
this.i = 0;
channelVideoUpdate(this, this.currentMedia.id);
}
2013-02-16 05:02:42 +00:00
return;
}
for(var i = 0; i < this.users.length; i++) {
if(this.users[i].name == name) {
2013-03-27 19:28:51 +00:00
this.logger.log("*** Assigned leader: " + name);
2013-02-16 05:02:42 +00:00
this.leader = this.users[i];
this.broadcastRankUpdate(this.leader);
}
}
}
// Send the userlist to a client
// Do you know you"re all my very best friends?
2013-02-16 05:02:42 +00:00
Channel.prototype.sendUserlist = function(user) {
var users = [];
for(var i = 0; i < this.users.length; i++) {
// Skip people who haven"t logged in
2013-02-16 05:02:42 +00:00
if(this.users[i].name != "") {
users.push({
name: this.users[i].name,
rank: this.users[i].rank,
leader: this.users[i] == this.leader
});
}
}
user.socket.emit("userlist", users)
2013-02-16 05:02:42 +00:00
}
2013-03-07 01:38:16 +00:00
Channel.prototype.updateUsercount = function() {
this.sendAll("usercount", {
2013-03-07 01:38:16 +00:00
count: this.users.length
});
}
2013-02-16 05:02:42 +00:00
// Send the play queue
Channel.prototype.sendPlaylist = function(user) {
user.socket.emit("playlist", {
2013-02-16 05:02:42 +00:00
pl: this.queue
});
user.socket.emit("updatePlaylistIdx", {
2013-02-16 05:02:42 +00:00
idx: this.currentPosition
});
}
// Send the last 15 messages for context
Channel.prototype.sendRecentChat = function(user) {
for(var i = 0; i < this.recentChat.length; i++) {
user.socket.emit("chatMsg", this.recentChat[i]);
2013-02-16 05:02:42 +00:00
}
}
// Send a sync packet
Channel.prototype.sendMediaUpdate = function(user) {
2013-03-17 00:58:38 +00:00
if(this.currentMedia != null) {
user.socket.emit("mediaUpdate", this.currentMedia.packupdate());
2013-03-17 00:58:38 +00:00
}
2013-02-16 05:02:42 +00:00
}
// Sent when someone logs in, to add them to the user list
Channel.prototype.broadcastNewUser = function(user) {
this.sendAll("addUser", {
2013-02-16 05:02:42 +00:00
name: user.name,
rank: user.rank,
leader: this.leader == user
});
2013-04-01 21:02:09 +00:00
this.handleRankChange(user);
2013-02-16 05:02:42 +00:00
}
2013-04-01 21:02:09 +00:00
Channel.prototype.handleRankChange = function(user) {
user.socket.emit("rank", {
rank: user.rank
});
if(Rank.hasPermission(user, "ipban")) {
var ents = [];
for(var ip in this.ipbans) {
if(this.ipbans[ip] != null) {
ents.push({
ip: ip,
name: this.ipbans[ip][0],
banner: this.ipbans[ip][1]
});
}
}
user.socket.emit("banlist", {entries: ents});
}
if(Rank.hasPermission(user, "chatFilter")) {
var filts = new Array(this.filters.length);
for(var i = 0; i < this.filters.length; i++) {
filts[i] = [this.filters[i][0].source, this.filters[i][1], this.filters[i][2]];
}
user.socket.emit("chatFilters", {filters: filts});
}
2013-02-16 05:02:42 +00:00
}
2013-04-01 21:02:09 +00:00
// Someone"s rank changed, or their leadership status changed
Channel.prototype.broadcastRankUpdate = function(user) {
this.sendAll("updateUser", {
name: user.name,
rank: user.rank,
leader: this.leader == user
});
this.handleRankChange(user);
}
2013-03-16 21:49:58 +00:00
Channel.prototype.broadcastPoll = function() {
this.sendAll("newPoll", this.poll.packUpdate());
2013-03-16 21:49:58 +00:00
}
Channel.prototype.broadcastPollUpdate = function() {
this.sendAll("updatePoll", this.poll.packUpdate());
2013-03-16 21:49:58 +00:00
}
Channel.prototype.broadcastPollClose = function() {
this.sendAll("closePoll");
2013-03-16 21:49:58 +00:00
}
Channel.prototype.broadcastOpts = function() {
this.sendAll("channelOpts", this.opts);
}
2013-03-24 03:45:10 +00:00
Channel.prototype.broadcastIpbans = function() {
var ents = [];
for(var ip in this.ipbans) {
if(this.ipbans[ip] != null) {
ents.push({
ip: ip,
name: this.ipbans[ip][0],
banner: this.ipbans[ip][1]
});
}
}
for(var i = 0; i < this.users.length; i++) {
if(Rank.hasPermission(this.users[i], "ipban")) {
this.users[i].socket.emit("banlist", {entries: ents});
}
}
}
Channel.prototype.broadcastChatFilters = function() {
var filts = new Array(this.filters.length);
for(var i = 0; i < this.filters.length; i++) {
filts[i] = [this.filters[i][0].source, this.filters[i][1], this.filters[i][2]];
}
for(var i = 0; i < this.users.length; i++) {
if(Rank.hasPermission(this.users[i], "ipban")) {
this.users[i].socket.emit("chatFilters", {filters: filts});
}
}
2013-03-24 03:45:10 +00:00
}
2013-04-01 21:02:09 +00:00
Channel.prototype.broadcastMotd = function() {
this.sendAll("updateMotd", this.motd);
}
2013-04-02 19:07:22 +00:00
Channel.prototype.handleVoteskip = function(user) {
if(!this.voteskip) {
this.voteskip = new Poll("voteskip", "voteskip", ["yes"]);
}
this.voteskip.vote(user.ip, 0);
if(this.voteskip.counts[0] > this.users.length / 2) {
this.playNext();
}
}
2013-02-16 05:02:42 +00:00
// Send to ALL the clients!
Channel.prototype.sendAll = function(message, data) {
io.sockets.in(this.name).emit(message, data);
2013-02-16 05:02:42 +00:00
}
// Autolead yay
function channelVideoUpdate(chan, id) {
// Someone changed the video or there"s a manual leader, so your
2013-02-16 05:02:42 +00:00
// argument is invalid
2013-03-07 05:21:08 +00:00
if(chan.currentMedia == null || id != chan.currentMedia.id || chan.leader != null)
2013-02-16 05:02:42 +00:00
return;
// Add dt since last update
chan.currentMedia.currentTime += (new Date().getTime() - chan.time)/1000.0;
chan.time = new Date().getTime();
2013-02-16 05:02:42 +00:00
// Video over, move on to next
if(chan.currentMedia.currentTime > chan.currentMedia.seconds) {
chan.playNext();
}
// Every ~5 seconds send a sync packet to everyone
else if(chan.i % 5 == 0)
chan.sendAll("mediaUpdate", chan.currentMedia.packupdate());
chan.i++;
2013-02-16 05:02:42 +00:00
// Do it all over again in about a second
setTimeout(function() { channelVideoUpdate(chan, id); }, 1000);
}
exports.Channel = Channel;