sync/lib/channel.js

3366 lines
86 KiB
JavaScript
Raw Normal View History

2014-01-27 22:44:22 +00:00
var util = require("./utilities");
var db = require("./database");
var Playlist = require("./playlist");
var Poll = require("./poll").Poll;
var Filter = require("./filter").Filter;
var Logger = require("./logger");
var AsyncQueue = require("./asyncqueue");
var MakeEmitter = require("./emitter");
var InfoGetter = require("./get-info");
var ChatCommand = require("./chatcommand");
var XSS = require("./xss");
2014-02-02 18:41:41 +00:00
var Media = require("./media").Media;
2014-02-06 00:05:52 +00:00
var Config = require("./config");
2013-02-16 17:19:59 +00:00
var fs = require("fs");
var path = require("path");
2013-10-11 21:31:40 +00:00
var url = require("url");
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
var DEFAULT_FILTERS = [
new Filter("monospace", "`(.+?)`", "g", "<code>$1</code>"),
new Filter("bold", "\\*(.+?)\\*", "g", "<strong>$1</strong>"),
new Filter("italic", "_(.+?)_", "g", "<em>$1</em>"),
new Filter("strike", "~~(.+?)~~", "g", "<s>$1</s>"),
new Filter("inline spoiler", "\\[sp\\](.*?)\\[\\/sp\\]", "ig", "<span class=\"spoiler\">$1</span>")
];
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
function Channel(name) {
MakeEmitter(this);
var self = this; // Alias `this` to prevent scoping issues
Logger.syslog.log("Loading channel " + name);
// Defaults
self.ready = false;
2013-08-17 17:30:52 +00:00
self.name = name;
2014-01-27 22:44:22 +00:00
self.uniqueName = name.toLowerCase(); // To prevent casing issues
self.registered = false; // set to true if the channel exists in the database
2013-08-17 17:30:52 +00:00
self.users = [];
2014-01-27 22:44:22 +00:00
self.mutedUsers = new util.Set();
2013-08-17 17:30:52 +00:00
self.playlist = new Playlist(self);
2014-01-27 22:44:22 +00:00
self.plmeta = { count: 0, time: "00:00:00" };
self.plqueue = new AsyncQueue(); // For synchronizing playlist actions
2013-08-17 17:30:52 +00:00
self.drinks = 0;
self.leader = null;
self.chatbuffer = [];
2014-01-27 22:44:22 +00:00
self.playlistLock = true;
self.poll = null;
self.voteskip = null;
2013-08-17 17:30:52 +00:00
self.permissions = {
2014-02-18 01:06:49 +00:00
seeplaylist: -1, // See the playlist
2014-01-27 22:44:22 +00:00
playlistadd: 1.5, // Add video to the playlist
playlistnext: 1.5,
playlistmove: 1.5, // Move a video on the playlist
playlistdelete: 2, // Delete a video from the playlist
playlistjump: 1.5, // Start a different video on the playlist
playlistaddlist: 1.5, // Add a list of videos to the playlist
oplaylistadd: -1, // Same as above, but for open (unlocked) playlist
2013-05-22 19:38:16 +00:00
oplaylistnext: 1.5,
oplaylistmove: 1.5,
oplaylistdelete: 2,
oplaylistjump: 1.5,
2013-06-01 20:56:23 +00:00
oplaylistaddlist: 1.5,
2014-01-27 22:44:22 +00:00
playlistaddcustom: 3, // Add custom embed to the playlist
playlistaddlive: 1.5, // Add a livestream to the playlist
exceedmaxlength: 2, // Add a video longer than the maximum length set
addnontemp: 2, // Add a permanent video to the playlist
settemp: 2, // Toggle temporary status of a playlist item
playlistshuffle: 2, // Shuffle the playlist
playlistclear: 2, // Clear the playlist
pollctl: 1.5, // Open/close polls
pollvote: -1, // Vote in polls
viewhiddenpoll: 1.5, // View results of hidden polls
voteskip: -1, // Vote to skip the current video
mute: 1.5, // Mute other users
kick: 1.5, // Kick other users
ban: 2, // Ban other users
motdedit: 3, // Edit the MOTD
filteredit: 3, // Control chat filters
filterimport: 3, // Import chat filter list
2014-02-13 05:33:42 +00:00
emoteedit: 3, // Control emotes
emoteimport: 3, // Import emote list
2014-01-27 22:44:22 +00:00
playlistlock: 2, // Lock/unlock the playlist
leaderctl: 2, // Give/take leader
drink: 1.5, // Use the /d command
chat: 0 // Send chat messages
2013-05-22 19:38:16 +00:00
};
2013-08-17 17:30:52 +00:00
self.opts = {
2014-01-27 22:44:22 +00:00
allow_voteskip: true, // Allow users to voteskip
voteskip_ratio: 0.5, // Ratio of skip votes:non-afk users needed to skip the video
afk_timeout: 600, // Number of seconds before a user is automatically marked afk
pagetitle: self.name, // Title of the browser tab
maxlength: 0, // Maximum length (in seconds) of a video queued
externalcss: "", // Link to external stylesheet
externaljs: "", // Link to external script
chat_antiflood: false, // Throttle chat messages
2013-11-19 21:14:40 +00:00
chat_antiflood_params: {
2014-01-27 22:44:22 +00:00
burst: 4, // Number of messages to allow with no throttling
sustained: 1, // Throttle rate (messages/second)
cooldown: 4 // Number of seconds with no messages before burst is reset
2013-11-19 21:14:40 +00:00
},
2014-01-27 22:44:22 +00:00
show_public: false, // List the channel on the index page
enable_link_regex: true, // Use the built-in link filter
password: false // Channel password (false -> no password required for entry)
};
2013-08-17 17:30:52 +00:00
self.motd = {
2014-01-27 22:44:22 +00:00
motd: "", // Raw MOTD text
html: "" // Filtered MOTD text (XSS removed; \n replaced by <br>)
2013-04-01 21:02:09 +00:00
};
2014-02-17 03:40:26 +00:00
self.filters = [];
DEFAULT_FILTERS.forEach(function (f) {
var filt = new Filter(f.name, f.source, f.flags, f.replace);
self.updateFilter(filt);
});
2014-02-10 05:53:46 +00:00
self.emotes = [];
self.logger = new Logger.Logger(path.join(__dirname, "../chanlogs",
2014-01-27 22:44:22 +00:00
self.uniqueName + ".log"));
self.css = ""; // Up to 20KB of inline CSS
self.js = ""; // Up to 20KB of inline Javascript
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
self.error = false; // Set to true if something bad happens => don't save state
2013-05-15 15:34:27 +00:00
2014-01-27 22:44:22 +00:00
self.on("ready", function () {
self.ready = true;
});
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
// Load from database
2013-12-14 00:52:13 +00:00
db.channels.load(self, function (err) {
2014-01-27 22:44:22 +00:00
if (err && err !== "Channel is not registered") {
return;
2014-01-27 22:44:22 +00:00
} else {
// Load state from JSON blob
self.tryLoadState();
}
});
};
Channel.prototype.isMuted = function (name) {
return this.mutedUsers.contains(name.toLowerCase()) ||
this.mutedUsers.contains("[shadow]" + name.toLowerCase());
};
Channel.prototype.isShadowMuted = function (name) {
return this.mutedUsers.contains("[shadow]" + name.toLowerCase());
};
Channel.prototype.mutedUsers = function () {
var self = this;
return self.users.filter(function (u) {
return self.mutedUsers.contains(u.name);
});
};
2014-01-27 22:44:22 +00:00
Channel.prototype.shadowMutedUsers = function () {
var self = this;
return self.users.filter(function (u) {
return self.mutedUsers.contains("[shadow]" + u.name);
2013-08-17 17:30:52 +00:00
});
2014-01-27 22:44:22 +00:00
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
Channel.prototype.channelModerators = function () {
return this.users.filter(function (u) {
return u.rank >= 2;
});
};
2013-05-22 19:38:16 +00:00
2014-01-27 22:44:22 +00:00
Channel.prototype.channelAdmins = function () {
return this.users.filter(function (u) {
return u.rank >= 3;
});
};
2013-11-25 05:20:41 +00:00
2014-01-27 22:44:22 +00:00
Channel.prototype.tryLoadState = function () {
2013-08-13 14:46:18 +00:00
var self = this;
2013-11-25 05:20:41 +00:00
if (self.name === "") {
return;
}
2014-01-27 22:44:22 +00:00
// Don't load state if the channel isn't registered
2013-11-25 05:20:41 +00:00
if (!self.registered) {
self.setUnregisteredPermissions();
2014-01-27 22:44:22 +00:00
self.emit("ready");
return;
2013-11-25 05:20:41 +00:00
}
2014-01-27 22:44:22 +00:00
var file = path.join(__dirname, "../chandump", self.uniqueName);
fs.stat(file, function (err, stats) {
2013-11-25 05:20:41 +00:00
if (!err) {
2013-08-13 14:46:18 +00:00
var mb = stats.size / 1048576;
2014-01-27 22:44:22 +00:00
mb = Math.floor(mb * 100) / 100;
2013-11-25 05:20:41 +00:00
if (mb > 1) {
2014-01-27 22:44:22 +00:00
Logger.errlog.log("Large chandump detected: " + self.uniqueName +
" (" + mb + " MiB)");
2014-02-16 08:13:53 +00:00
self.setMotd("Your channel file has exceeded the maximum size of 1MB " +
2014-01-27 22:44:22 +00:00
"and cannot be loaded. Please ask an administrator for " +
"assistance in restoring it.");
self.error = true;
self.emit("ready");
2013-08-13 14:46:18 +00:00
return;
2013-06-19 21:54:27 +00:00
}
}
2014-01-27 22:44:22 +00:00
self.loadState();
2013-11-25 05:20:41 +00:00
});
};
2014-01-27 22:44:22 +00:00
/**
* Load the channel state from disk.
*
* SHOULD ONLY BE CALLED FROM tryLoadState
*/
Channel.prototype.loadState = function () {
2013-11-25 05:20:41 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
if (self.error) {
2013-11-25 05:20:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
fs.readFile(path.join(__dirname, "../chandump", self.uniqueName),
function (err, data) {
2013-11-25 05:20:41 +00:00
if (err) {
2014-01-27 22:44:22 +00:00
// File didn't exist => start fresh
if (err.code === "ENOENT") {
self.emit("ready");
self.saveState();
2013-11-25 05:20:41 +00:00
} else {
2014-01-27 22:44:22 +00:00
Logger.errlog.log("Failed to open channel dump " + self.uniqueName);
2013-11-25 05:20:41 +00:00
Logger.errlog.log(err);
2014-02-16 08:13:53 +00:00
self.setMotd("Channel state load failed. Contact an administrator.");
2014-01-27 22:44:22 +00:00
self.error = true;
self.emit("ready");
}
2013-11-25 05:20:41 +00:00
return;
}
try {
2014-02-08 18:45:07 +00:00
self.logger.log("[init] Loading channel state from disk");
2013-11-25 05:20:41 +00:00
data = JSON.parse(data);
2014-01-27 22:44:22 +00:00
// Load the playlist
2013-11-25 05:20:41 +00:00
if ("playlist" in data) {
2014-01-27 22:44:22 +00:00
self.playlist.load(data.playlist, function () {
self.sendPlaylist(self.users);
self.updatePlaylistMeta();
self.sendPlaylistMeta(self.users);
2013-11-25 05:20:41 +00:00
self.playlist.startPlayback(data.playlist.time);
2013-08-13 14:46:18 +00:00
});
2013-11-25 05:20:41 +00:00
}
2014-01-27 22:44:22 +00:00
// Playlist lock
self.setLock(data.playlistLock || false);
2013-11-25 05:20:41 +00:00
2014-01-27 22:44:22 +00:00
// Configurables
if ("opts" in data) {
for (var key in data.opts) {
self.opts[key] = data.opts[key];
}
2013-11-25 05:20:41 +00:00
}
2014-01-27 22:44:22 +00:00
// Permissions
if ("permissions" in data) {
for (var key in data.permissions) {
self.permissions[key] = data.permissions[key];
}
2013-11-25 05:20:41 +00:00
}
2014-01-27 22:44:22 +00:00
// Chat filters
if ("filters" in data) {
for (var i = 0; i < data.filters.length; i++) {
var f = data.filters[i];
var filt = new Filter(f.name, f.source, f.flags, f.replace);
filt.active = f.active;
filt.filterlinks = f.filterlinks;
self.updateFilter(filt, false);
2013-08-13 14:46:18 +00:00
}
2013-04-01 21:02:09 +00:00
}
2013-11-25 05:20:41 +00:00
2014-02-10 05:53:46 +00:00
// Emotes
if ("emotes" in data) {
data.emotes.forEach(function (e) {
2014-02-16 07:33:38 +00:00
self.updateEmote(e);
2014-02-10 05:53:46 +00:00
});
}
2014-01-27 22:44:22 +00:00
// MOTD
if ("motd" in data) {
self.motd = {
motd: data.motd.motd,
html: data.motd.html
};
2013-05-04 17:02:38 +00:00
}
2013-11-25 05:20:41 +00:00
2014-01-27 22:44:22 +00:00
// Chat history
if ("chatbuffer" in data) {
data.chatbuffer.forEach(function (msg) {
self.chatbuffer.push(msg);
});
}
2013-11-25 05:20:41 +00:00
2014-01-27 22:44:22 +00:00
// Inline CSS/JS
2013-11-25 05:20:41 +00:00
self.css = data.css || "";
self.js = data.js || "";
2014-01-27 22:44:22 +00:00
self.emit("ready");
2014-01-27 22:44:22 +00:00
} catch (e) {
self.error = true;
Logger.errlog.log("Channel dump load failed (" + self.uniqueName + "): " + e);
2014-02-16 08:13:53 +00:00
self.setMotd("Channel state load failed. Contact an administrator.");
2014-01-27 22:44:22 +00:00
self.emit("ready");
}
});
2014-01-27 22:44:22 +00:00
};
2014-01-27 22:44:22 +00:00
Channel.prototype.saveState = function () {
var self = this;
if (self.error) {
return;
2013-10-12 23:59:50 +00:00
}
2014-01-27 22:44:22 +00:00
if (!self.registered || self.uniqueName === "") {
return;
}
2014-02-08 18:45:07 +00:00
self.logger.log("[init] Saving channel state to disk");
2014-01-27 22:44:22 +00:00
var filters = self.filters.map(function (f) {
return f.pack();
});
2014-01-27 22:44:22 +00:00
var data = {
playlist: self.playlist.dump(),
opts: self.opts,
permissions: self.permissions,
filters: filters,
2014-02-10 05:53:46 +00:00
emotes: self.emotes,
2014-01-27 22:44:22 +00:00
motd: self.motd,
playlistLock: self.playlistLock,
chatbuffer: self.chatbuffer,
css: self.css,
js: self.js
};
2013-03-17 17:14:34 +00:00
2014-01-27 22:44:22 +00:00
var text = JSON.stringify(data);
fs.writeFileSync(path.join(__dirname, "../chandump", self.uniqueName), text);
};
2013-08-17 17:30:52 +00:00
2014-01-27 22:44:22 +00:00
/**
* Checks whether a user has the given permission node
*/
Channel.prototype.hasPermission = function (user, key) {
// Special case: you can have separate permissions for when playlist is unlocked
if (key.indexOf("playlist") === 0 && !this.playlistLock) {
var key2 = "o" + key;
var v = this.permissions[key2];
if (typeof v === "number" && user.rank >= v) {
return true;
}
2013-03-17 17:14:34 +00:00
}
2013-08-17 17:30:52 +00:00
2014-01-27 22:44:22 +00:00
var v = this.permissions[key];
if (typeof v !== "number") {
return false;
2013-05-13 19:41:29 +00:00
}
2014-01-27 22:44:22 +00:00
return user.rank >= v;
};
2014-01-27 22:44:22 +00:00
/**
* Defer a callback to complete when the channel is ready to accept users.
* Called immediately if the ready flag is already set
*/
Channel.prototype.whenReady = function (fn) {
if (this.ready) {
setImmediate(fn);
} else {
this.on("ready", fn);
}
};
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Looks up a user's rank in the channel. Computed as max(global_rank, channel rank)
*/
2013-08-17 17:30:52 +00:00
Channel.prototype.getRank = function (name, callback) {
var self = this;
2013-12-14 00:52:13 +00:00
db.users.getGlobalRank(name, function (err, global) {
2014-01-27 22:44:22 +00:00
if (self.dead) {
return;
2014-01-27 22:44:22 +00:00
}
2014-01-27 22:44:22 +00:00
if (err) {
2013-08-17 17:30:52 +00:00
callback(err, null);
return;
}
2014-01-27 22:44:22 +00:00
if (!self.registered) {
2013-08-17 17:30:52 +00:00
callback(null, global);
return;
}
2014-01-27 22:44:22 +00:00
db.channels.getRank(self.name, name, function (err, rank) {
if (self.dead) {
return;
}
if (err) {
2013-08-17 17:30:52 +00:00
callback(err, null);
return;
}
2014-01-27 22:44:22 +00:00
callback(null, Math.max(rank, global));
2013-08-17 17:30:52 +00:00
});
});
};
2014-01-27 22:44:22 +00:00
/**
* Looks up the highest rank of any alias of an IP address
*/
2013-08-17 17:30:52 +00:00
Channel.prototype.getIPRank = function (ip, callback) {
var self = this;
2013-12-14 02:39:21 +00:00
db.getAliases(ip, function (err, names) {
2014-01-27 22:44:22 +00:00
if (self.dead) {
return;
2014-01-27 22:44:22 +00:00
}
2013-12-14 00:52:13 +00:00
db.users.getGlobalRanks(names, function (err, res) {
2014-01-27 22:44:22 +00:00
if (self.dead) {
2013-08-17 17:30:52 +00:00
return;
}
2013-06-15 20:07:38 +00:00
2014-01-27 22:44:22 +00:00
if (err) {
callback(err, null);
return;
2013-08-17 17:30:52 +00:00
}
2014-03-03 04:00:24 +00:00
var globalRank = res.reduce(function (a, b) {
2014-01-27 22:44:22 +00:00
return Math.max(a, b);
}, 0);
2013-10-07 15:04:08 +00:00
if (!self.registered) {
2014-03-03 04:00:24 +00:00
callback(null, globalRank);
2013-10-07 15:04:08 +00:00
return;
}
2013-12-14 00:52:13 +00:00
db.channels.getRanks(self.name, names,
function (err, res) {
2014-01-27 22:44:22 +00:00
if (self.dead) {
2013-10-07 15:04:08 +00:00
return;
2014-01-27 22:44:22 +00:00
}
2013-10-07 15:04:08 +00:00
2014-01-27 22:44:22 +00:00
if (err) {
2013-08-18 22:58:16 +00:00
callback(err, null);
return;
}
2014-01-27 22:44:22 +00:00
var rank = res.reduce(function (a, b) {
return Math.max(a, b);
2014-03-03 04:00:24 +00:00
}, globalRank);
2013-08-18 22:58:16 +00:00
callback(null, rank);
});
2013-08-17 17:30:52 +00:00
});
2013-08-18 22:58:16 +00:00
});
2014-01-27 22:44:22 +00:00
};
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Called when a user attempts to join a channel.
* Handles password check
*/
Channel.prototype.preJoin = function (user, password) {
2013-08-17 17:30:52 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
self.whenReady(function () {
user.whenLoggedIn(function () {
self.getRank(user.name, function (err, rank) {
if (err) {
user.rank = user.global_rank;
} else {
user.rank = Math.max(rank, user.global_rank);
}
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
user.socket.emit("rank", user.rank);
user.emit("channelRank", user.rank);
2014-02-28 06:09:20 +00:00
if (self.permissions.seeplaylist > -1) {
self.sendPlaylist([user]);
}
2014-01-27 22:44:22 +00:00
});
2013-08-18 23:35:49 +00:00
});
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
if (self.opts.password !== false && user.rank < 2) {
if (password !== self.opts.password) {
var checkPassword = function (pw) {
if (self.dead) {
return;
}
if (pw !== self.opts.password) {
user.socket.emit("needPassword", true);
return;
}
user.socket.listeners("channelPassword").splice(
user.socket.listeners("channelPassword").indexOf(checkPassword)
);
user.socket.emit("cancelNeedPassword");
self.join(user);
};
user.socket.on("channelPassword", checkPassword);
user.socket.emit("needPassword", typeof password !== "undefined");
user.once("channelRank", function (r) {
if (!user.inChannel() && !self.dead && r >= 2) {
user.socket.emit("cancelNeedPassword");
self.join(user);
}
});
return;
}
2013-08-17 17:30:52 +00:00
}
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
self.join(user);
});
};
/**
* Called when a user joins a channel
*/
Channel.prototype.join = function (user) {
var self = this;
var afterLogin = function () {
if (self.dead) {
2013-08-17 17:30:52 +00:00
return;
}
2014-01-27 22:44:22 +00:00
var lname = user.name.toLowerCase();
for (var i = 0; i < self.users.length; i++) {
if (self.users[i].name.toLowerCase() === lname && self.users[i] !== user) {
self.users[i].kick("Duplicate login");
2013-08-17 17:30:52 +00:00
}
}
2014-01-27 22:44:22 +00:00
self.sendUserJoin(self.users, user);
self.sendUserlist([user]);
};
var afterIPBan = function () {
user.autoAFK();
user.socket.join(self.uniqueName);
user.channel = self;
2014-03-04 22:22:16 +00:00
user.autoAFK();
2014-02-02 18:41:41 +00:00
if (!self.registered) {
user.socket.emit("channelNotRegistered");
}
2014-01-27 22:44:22 +00:00
self.users.push(user);
self.sendVoteskipUpdate(self.users);
self.sendUsercount(self.users);
2014-02-28 15:21:28 +00:00
user.whenChannelRank(function () {
2014-02-02 18:41:41 +00:00
if (!self.registered) {
afterLogin();
return;
}
2014-01-27 22:44:22 +00:00
db.channels.isNameBanned(self.name, user.name, function (err, banned) {
if (!err && banned) {
user.kick("You're banned!");
} else {
afterLogin();
}
});
2013-08-17 17:30:52 +00:00
});
2014-02-28 06:09:20 +00:00
if (self.hasPermission(user, "seeplaylist")) {
self.sendPlaylist([user]);
}
2014-01-27 22:44:22 +00:00
self.sendMediaUpdate([user]);
self.sendPlaylistLock([user]);
self.sendUserlist([user]);
2014-02-10 05:53:46 +00:00
self.sendEmoteList([user]);
2014-01-27 22:44:22 +00:00
self.sendRecentChat([user]);
self.sendCSSJS([user]);
self.sendPoll([user]);
self.sendOpts([user]);
self.sendPermissions([user]);
self.sendMotd([user]);
self.sendDrinkCount([user]);
2014-02-08 18:45:07 +00:00
self.logger.log("[login] " + user.ip + " joined");
2014-01-27 22:44:22 +00:00
Logger.syslog.log(user.ip + " joined channel " + self.name);
};
2014-02-02 18:41:41 +00:00
if (!self.registered) {
afterIPBan();
return;
}
2014-01-27 22:44:22 +00:00
db.channels.isIPBanned(self.name, user.ip, function (err, banned) {
if (!err && banned) {
user.kick("You're banned!");
2013-08-17 17:30:52 +00:00
return;
2014-01-27 22:44:22 +00:00
} else {
afterIPBan();
2013-05-21 16:17:01 +00:00
}
2013-08-17 17:30:52 +00:00
});
2014-01-27 22:44:22 +00:00
};
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
/**
* Called when a user leaves the channel.
* Cleans up and sends appropriate updates to other users
*/
Channel.prototype.part = function (user) {
2013-08-17 17:37:35 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
user.channel = null;
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
// Clear poll vote
if (self.poll) {
self.poll.unvote(user.ip);
self.sendPollUpdate(self.users);
}
2013-08-17 17:37:35 +00:00
2014-01-27 22:44:22 +00:00
// Clear voteskip vote
if (self.voteskip) {
self.voteskip.unvote(user.ip);
self.sendVoteskipUpdate(self.users);
}
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
// Return video lead to server if necessary
if (self.leader === user) {
self.changeLeader("");
}
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
// Remove from users array
var idx = self.users.indexOf(user);
if (idx >= 0 && idx < self.users.length) {
self.users.splice(idx, 1);
2013-05-21 16:17:01 +00:00
}
2014-01-27 22:44:22 +00:00
// A change in usercount might cause a voteskip result to change
self.checkVoteskipPass();
self.sendUsercount(self.users);
if (user.loggedIn) {
self.sendUserLeave(self.users, user);
2013-05-21 16:17:01 +00:00
}
2014-01-27 22:44:22 +00:00
2014-02-08 18:45:07 +00:00
self.logger.log("[login] " + user.ip + " (" + user.name + ") left");
2014-01-27 22:44:22 +00:00
if (self.users.length === 0) {
self.emit("empty");
2013-08-18 23:35:49 +00:00
return;
}
2014-01-27 22:44:22 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Send the MOTD to the given users
*/
Channel.prototype.sendMOTD = function (users) {
var motd = this.motd;
users.forEach(function (u) {
u.socket.emit("setMotd", motd);
});
};
2014-01-27 22:44:22 +00:00
/**
* Sends a message to channel moderators
*/
Channel.prototype.sendModMessage = function (msg, minrank) {
if (isNaN(minrank)) {
minrank = 2;
}
2013-08-17 17:30:52 +00:00
2014-01-27 22:44:22 +00:00
var notice = {
username: "[server]",
msg: msg,
meta: {
addClass: "server-whisper" ,
addClassToNameAndTimestamp: true
},
time: Date.now()
};
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
this.users.forEach(function(u) {
2014-02-28 06:09:20 +00:00
if (u.rank >= minrank) {
2014-01-27 22:44:22 +00:00
u.socket.emit("chatMsg", notice);
}
});
};
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
/**
* Stores a video in the channel's library
*/
Channel.prototype.cacheMedia = function (media) {
// Don't cache Google Drive videos because of their time limit
if (media.type === "gd") {
return false;
}
2013-06-18 14:46:28 +00:00
2014-01-27 22:44:22 +00:00
if (this.registered) {
db.channels.addToLibrary(this.name, media);
}
};
2013-08-17 17:30:52 +00:00
2014-01-27 22:44:22 +00:00
/**
* Attempts to ban a user by name
*/
Channel.prototype.handleNameBan = function (actor, name, reason) {
var self = this;
if (!self.hasPermission(actor, "ban")) {
return false;
}
2014-01-27 22:44:22 +00:00
if (!self.registered) {
actor.socket.emit("errorMsg", {
msg: "Banning is only supported in registered channels"
2013-08-17 17:30:52 +00:00
});
2014-01-27 22:44:22 +00:00
return;
}
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
name = name.toLowerCase();
if (name == actor.name.toLowerCase()) {
actor.socket.emit("costanza", {
msg: "Trying to ban yourself?"
});
return;
2013-10-12 23:59:50 +00:00
}
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
// Look up the name's rank so people can't ban others with higher rank than themselves
self.getRank(name, function (err, rank) {
if (self.dead) {
return;
}
2013-03-24 03:45:10 +00:00
2014-01-27 22:44:22 +00:00
if (err) {
actor.socket.emit("errorMsg", {
msg: "Internal error: " + err
});
return;
}
2013-08-07 03:21:54 +00:00
2014-01-27 22:44:22 +00:00
if (rank >= actor.rank) {
actor.socket.emit("errorMsg", {
msg: "You don't have permission to ban " + name
});
return;
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
if (typeof reason !== "string") {
reason = "";
}
2013-03-24 03:45:10 +00:00
2014-01-27 22:44:22 +00:00
reason = reason.substring(0, 255);
2013-05-21 16:17:01 +00:00
2014-01-27 22:44:22 +00:00
// If in the channel already, kick the banned user
for (var i = 0; i < self.users.length; i++) {
if (self.users[i].name.toLowerCase() == name) {
self.users[i].kick("You're banned!");
break;
}
}
2014-02-08 18:45:07 +00:00
self.logger.log("[mod] " + actor.name + " namebanned " + name);
2014-01-27 22:44:22 +00:00
self.sendModMessage(actor.name + " banned " + name, self.permissions.ban);
2014-01-27 22:44:22 +00:00
db.channels.isNameBanned(self.name, name, function (err, banned) {
if (!err && banned) {
actor.socket.emit("errorMsg", {
msg: name + " is already banned"
});
return;
}
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
if (self.dead) {
return;
}
2014-01-27 22:44:22 +00:00
// channel, ip, name, reason, actor
db.channels.ban(self.name, "*", name, reason, actor.name);
// TODO send banlist?
});
2013-02-16 05:02:42 +00:00
});
2014-01-27 22:44:22 +00:00
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Removes a ban by ID
*/
Channel.prototype.handleUnban = function (actor, data) {
var self = this;
2014-01-27 22:44:22 +00:00
if (!this.hasPermission(actor, "ban")) {
return;
}
2014-01-27 22:44:22 +00:00
if (typeof data.id !== "number") {
data.id = parseInt(data.id);
if (isNaN(data.id)) {
return;
}
}
2014-01-27 22:44:22 +00:00
data.actor = actor.name;
if (!self.registered) {
return;
}
db.channels.unbanId(self.name, data.id, function (err, res) {
if (err) {
actor.socket.emit("errorMsg", {
msg: err
});
return;
}
self.sendUnban(self.users, data);
});
};
2014-01-27 22:44:22 +00:00
/**
* Sends an unban packet
*/
Channel.prototype.sendUnban = function (users, data) {
2013-11-25 05:20:41 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
users.forEach(function (u) {
if (self.hasPermission(u, "ban")) {
u.socket.emit("banlistRemove", data);
}
2013-11-25 05:20:41 +00:00
});
2014-02-08 18:45:07 +00:00
self.logger.log("[mod] " + data.actor + " unbanned " + data.name);
2014-01-27 22:44:22 +00:00
self.sendModMessage(data.actor + " unbanned " + data.name, self.permissions.ban);
2013-11-25 05:20:41 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Bans all IP addresses associated with a username
*/
Channel.prototype.handleBanAllIP = function (actor, name, reason, range) {
var self = this;
2014-01-27 22:44:22 +00:00
if (!self.hasPermission(actor, "ban")) {
2013-11-25 22:20:15 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (typeof name !== "string") {
2013-11-25 05:20:41 +00:00
return;
}
2013-11-25 22:20:15 +00:00
2014-01-27 22:44:22 +00:00
if (!self.registered) {
actor.socket.emit("errorMsg", {
msg: "Banning is not supported for unregistered rooms"
});
2013-03-24 03:01:37 +00:00
return;
2013-03-24 01:08:35 +00:00
}
2014-01-27 22:44:22 +00:00
name = name.toLowerCase();
if (name === actor.name.toLowerCase()) {
actor.socket.emit("costanza", {
msg: "Trying to ban yourself?"
});
2013-05-21 16:17:01 +00:00
return;
}
2013-03-24 01:08:35 +00:00
2014-01-27 22:44:22 +00:00
db.getIPs(name, function (err, ips) {
if (self.dead) {
return;
}
2013-11-25 05:20:41 +00:00
2014-01-27 22:44:22 +00:00
if (err) {
actor.socket.emit("errorMsg", {
msg: "Internal error: " + err
});
return;
2013-03-17 14:39:22 +00:00
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
ips.forEach(function (ip) {
2014-02-16 23:55:05 +00:00
self.banIP(actor, ip, name, range);
});
2014-01-27 22:44:22 +00:00
});
};
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Bans an individual IP
*/
Channel.prototype.banIP = function (actor, ip, name, reason, range) {
2014-02-19 03:56:54 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
if (range) {
ip = ip.replace(/(\d+)\.(\d+)\.(\d+)\.(\d+)/, "$1.$2.$3");
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
if (typeof reason !== "string") {
reason = "";
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
reason = reason.substring(0, 255);
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
self.getIPRank(ip, function (err, rank) {
if (self.dead) {
return;
}
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
if (err) {
actor.socket.emit("errorMsg", {
msg: "Internal error: " + err
});
return;
}
2013-04-14 17:38:00 +00:00
2014-01-27 22:44:22 +00:00
if (rank >= actor.rank) {
actor.socket.emit("errorMsg", {
msg: "You don't have permission to ban IP: " + util.maskIP(ip)
});
return;
}
2013-05-21 16:17:01 +00:00
2014-02-08 18:45:07 +00:00
self.logger.log("[mod] " + actor.name + " banned " + ip + " (" + name + ")");
2014-03-01 23:37:59 +00:00
self.sendModMessage(actor.name + " banned " + util.maskIP(ip) +
" (" + name + ")", self.permissions.ban);
2014-01-27 22:44:22 +00:00
// If in the channel already, kick the banned user
for (var i = 0; i < self.users.length; i++) {
if (self.users[i].ip === ip) {
self.users[i].kick("You're banned!");
break;
}
}
2013-06-18 03:57:29 +00:00
2014-01-27 22:44:22 +00:00
if (!self.registered) {
return;
}
2013-06-18 03:57:29 +00:00
2014-01-27 22:44:22 +00:00
db.channels.isIPBanned(self.name, ip, function (err, banned) {
if (!err && banned) {
var disp = actor.global_rank >= 255 ? ip : util.maskIP(ip);
actor.socket.emit("errorMsg", {
msg: disp + " is alraedy banned"
});
2014-01-27 22:44:22 +00:00
return;
2013-03-25 19:39:03 +00:00
}
2014-01-27 22:44:22 +00:00
if (self.dead) {
return;
2013-05-21 16:17:01 +00:00
}
2014-01-27 22:44:22 +00:00
// channel, ip, name, reason, ban actor
db.channels.ban(self.name, ip, name, reason, actor.name, function (err) {
if (err) {
actor.socket.emit("errorMsg", {
msg: "Ban failed: " + err
});
}
});
});
});
};
/**
* Sends the banlist
*/
Channel.prototype.sendBanlist = function (users) {
var self = this;
2014-02-02 18:41:41 +00:00
if (!self.registered) {
return;
}
2014-01-27 22:44:22 +00:00
var bans = [];
var unmaskedbans = [];
db.channels.listBans(self.name, function (err, banlist) {
if (err) {
return;
2013-05-21 16:17:01 +00:00
}
2014-01-27 22:44:22 +00:00
for (var i = 0; i < banlist.length; i++) {
bans.push({
id: banlist[i].id,
ip: banlist[i].ip === "*" ? "*" : util.maskIP(banlist[i].ip),
name: banlist[i].name,
reason: banlist[i].reason,
bannedby: banlist[i].bannedby
});
unmaskedbans.push({
id: banlist[i].id,
ip: banlist[i].ip,
name: banlist[i].name,
reason: banlist[i].reason,
bannedby: banlist[i].bannedby
});
2013-04-03 17:47:41 +00:00
}
2013-06-18 14:57:53 +00:00
2014-01-27 22:44:22 +00:00
users.forEach(function (u) {
if (!self.hasPermission(u, "ban")) {
2013-08-17 20:47:11 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (u.rank >= 255) {
u.socket.emit("banlist", unmaskedbans);
} else {
u.socket.emit("banlist", bans);
}
2013-08-17 20:47:11 +00:00
});
2014-01-27 22:44:22 +00:00
});
};
/**
* Sends the channel ranks list
*/
Channel.prototype.sendChannelRanks = function (users) {
var self = this;
2014-02-02 18:41:41 +00:00
if (!self.registered) {
return;
}
2014-01-27 22:44:22 +00:00
db.channels.allRanks(self.name, function (err, ranks) {
if (err) {
return;
2013-04-03 17:47:41 +00:00
}
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
users.forEach(function (u) {
if (u.rank >= 3) {
u.socket.emit("channelRanks", ranks);
}
});
});
};
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Sends the chat filter list
*/
Channel.prototype.sendChatFilters = function (users) {
var self = this;
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
var pkt = self.filters.map(function (f) {
return f.pack();
});
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
users.forEach(function (u) {
if (!self.hasPermission(u, "filteredit")) {
return;
}
2014-01-27 22:44:22 +00:00
u.socket.emit("chatFilters", pkt);
});
};
2014-02-10 05:53:46 +00:00
/**
* Sends the emote list
*/
Channel.prototype.sendEmoteList = function (users) {
var self = this;
users.forEach(function (u) {
u.socket.emit("emoteList", self.emotes);
});
};
2014-01-27 22:44:22 +00:00
/**
* Sends the channel permissions
*/
Channel.prototype.sendPermissions = function (users) {
var perms = this.permissions;
users.forEach(function (u) {
u.socket.emit("setPermissions", perms);
});
};
/**
* Sends the playlist
*/
Channel.prototype.sendPlaylist = function (users) {
var self = this;
var pl = self.playlist.items.toArray();
var current = null;
if (self.playlist.current) {
current = self.playlist.current.uid;
}
2014-01-27 22:44:22 +00:00
users.forEach(function (u) {
2014-02-18 01:06:49 +00:00
if (self.hasPermission(u, "seeplaylist")) {
u.socket.emit("playlist", pl);
u.socket.emit("setPlaylistMeta", self.plmeta);
if (current !== null) {
u.socket.emit("setCurrent", current);
}
2013-11-09 18:33:18 +00:00
}
2014-01-27 22:44:22 +00:00
});
};
2013-11-09 18:33:18 +00:00
2014-01-27 22:44:22 +00:00
/**
* Updates the playlist count/time
*/
Channel.prototype.updatePlaylistMeta = function () {
var total = 0;
2013-07-02 15:38:13 +00:00
var iter = this.playlist.items.first;
2014-01-27 22:44:22 +00:00
while (iter !== null) {
if (iter.media !== null) {
total += iter.media.seconds;
2014-01-27 22:44:22 +00:00
}
2013-06-29 22:09:20 +00:00
iter = iter.next;
}
2014-01-27 22:44:22 +00:00
var timestr = util.formatTime(total);
this.plmeta = {
2013-07-02 15:38:13 +00:00
count: this.playlist.items.length,
time: timestr
};
2014-01-27 22:44:22 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Send the playlist count/time
*/
Channel.prototype.sendPlaylistMeta = function (users) {
2013-08-17 20:47:11 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
users.forEach(function (u) {
2014-02-18 01:06:49 +00:00
if (self.hasPermission(u, "seeplaylist")) {
u.socket.emit("setPlaylistMeta", self.plmeta);
}
2014-01-27 22:44:22 +00:00
});
};
2014-01-27 22:44:22 +00:00
/**
* Sends the playlist lock
*/
Channel.prototype.sendPlaylistLock = function (users) {
var lock = this.playlistLock;
users.forEach(function (u) {
u.socket.emit("setPlaylistLocked", lock);
});
};
2013-06-18 03:57:29 +00:00
2014-01-27 22:44:22 +00:00
/**
* Sends a changeMedia packet
*/
Channel.prototype.sendMediaUpdate = function (users) {
var update = this.playlist.getFullUpdate();
if (update) {
users.forEach(function (u) {
u.socket.emit("changeMedia", update);
2013-08-17 20:47:11 +00:00
});
2014-01-27 22:44:22 +00:00
}
};
2013-06-18 03:57:29 +00:00
2014-01-27 22:44:22 +00:00
/**
* Sends the drink count
*/
Channel.prototype.sendDrinkCount = function (users) {
var drinks = this.drinks;
users.forEach(function (u) {
u.socket.emit("drinkCount", drinks);
});
};
2013-08-17 20:47:11 +00:00
2014-01-27 22:44:22 +00:00
/**
* Send the userlist
*/
Channel.prototype.sendUserlist = function (toUsers) {
var self = this;
var base = [];
var mod = [];
var sadmin = [];
2013-08-17 20:47:11 +00:00
2014-01-27 22:44:22 +00:00
for (var i = 0; i < self.users.length; i++) {
var u = self.users[i];
if (u.name === "") {
continue;
2013-10-16 22:36:05 +00:00
}
2013-11-09 18:33:18 +00:00
2014-01-27 22:44:22 +00:00
var data = self.packUserData(self.users[i]);
base.push(data.base);
mod.push(data.mod);
sadmin.push(data.sadmin);
}
toUsers.forEach(function (u) {
if (u.global_rank >= 255) {
u.socket.emit("userlist", sadmin);
} else if (u.rank >= 2) {
u.socket.emit("userlist", mod);
2013-11-09 18:33:18 +00:00
} else {
2014-01-27 22:44:22 +00:00
u.socket.emit("userlist", base);
2013-11-09 18:33:18 +00:00
}
2013-08-17 20:47:11 +00:00
2014-01-27 22:44:22 +00:00
if (self.leader != null) {
u.socket.emit("setLeader", self.leader.name);
2013-08-17 20:47:11 +00:00
}
2013-04-03 17:47:41 +00:00
});
2014-01-27 22:44:22 +00:00
};
2013-03-16 20:39:58 +00:00
2014-01-27 22:44:22 +00:00
/**
* Send the user count
*/
Channel.prototype.sendUsercount = function (users) {
2013-09-12 01:22:00 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
users.forEach(function (u) {
u.socket.emit("usercount", self.users.length);
2013-09-12 01:22:00 +00:00
});
2014-01-27 22:44:22 +00:00
};
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Send the chat buffer
*/
Channel.prototype.sendRecentChat = function (users) {
2013-09-12 01:22:00 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
users.forEach(function (u) {
for (var i = 0; i < self.chatbuffer.length; i++) {
u.socket.emit("chatMsg", self.chatbuffer[i]);
}
});
};
2013-09-27 15:28:48 +00:00
2014-01-27 22:44:22 +00:00
/**
* Sends a user profile
*/
Channel.prototype.sendUserProfile = function (users, user) {
var packet = {
name: user.name,
profile: user.profile
};
users.forEach(function (u) {
u.socket.emit("setUserProfile", packet);
2013-09-12 01:22:00 +00:00
});
2014-01-27 22:44:22 +00:00
};
/**
* Packs userdata for addUser or userlist
*/
Channel.prototype.packUserData = function (user) {
var base = {
name: user.name,
rank: user.rank,
profile: user.profile,
meta: {
afk: user.meta.afk,
muted: user.meta.muted && !user.meta.smuted
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
};
var mod = {
name: user.name,
rank: user.rank,
profile: user.profile,
meta: {
afk: user.meta.afk,
muted: user.meta.muted,
smuted: user.meta.smuted,
aliases: user.meta.aliases,
ip: util.maskIP(user.ip)
}
};
var sadmin = {
name: user.name,
rank: user.rank,
profile: user.profile,
meta: {
afk: user.meta.afk,
muted: user.meta.muted,
smuted: user.meta.smuted,
aliases: user.meta.aliases,
ip: user.ip
}
};
return {
base: base,
mod: mod,
sadmin: sadmin
};
};
/**
* Sends a user.meta update, optionally filtering by minimum rank
*/
Channel.prototype.sendUserMeta = function (users, user, minrank) {
var self = this;
var userdata = self.packUserData(user);
self.users.filter(function (u) {
return typeof minrank !== "number" || u.rank > minrank
}).forEach(function (u) {
if (u.rank >= 255) {
u.socket.emit("setUserMeta", {
name: user.name,
meta: userdata.sadmin.meta
2013-05-21 16:17:01 +00:00
});
2014-01-27 22:44:22 +00:00
} else if (u.rank >= 2) {
u.socket.emit("setUserMeta", {
name: user.name,
meta: userdata.mod.meta
});
} else {
u.socket.emit("setUserMeta", {
name: user.name,
meta: userdata.base.meta
2013-05-21 16:17:01 +00:00
});
}
2014-01-27 22:44:22 +00:00
});
};
/**
* Send a user join notification
*/
Channel.prototype.sendUserJoin = function (users, user) {
var self = this;
db.getAliases(user.ip, function (err, aliases) {
if (self.dead) {
return;
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
if (err || aliases.length === 0) {
aliases = [user.name];
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
user.meta.aliases = aliases;
if (self.isShadowMuted(user.name)) {
user.meta.muted = true;
user.meta.shadowmuted = true;
} else if (self.isMuted(user.name)) {
user.meta.muted = true;
user.meta.shadowmuted = false;
}
var data = self.packUserData(user);
users.forEach(function (u) {
if (u.global_rank >= 255) {
u.socket.emit("addUser", data.sadmin);
} else if (u.rank >= 2) {
u.socket.emit("addUser", data.mod);
} else {
u.socket.emit("addUser", data.base);
}
});
self.sendModMessage(user.name + " joined (aliases: " + aliases.join(",") + ")", 2);
});
};
/**
* Sends a notification that a user left
*/
Channel.prototype.sendUserLeave = function (users, user) {
var data = {
name: user.name
};
users.forEach(function (u) {
u.socket.emit("userLeave", data);
});
};
/**
* Sends the current poll
*/
Channel.prototype.sendPoll = function (users) {
var self = this;
if (!self.poll) {
return;
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
var obscured = self.poll.packUpdate(false);
var unobscured = self.poll.packUpdate(true);
users.forEach(function (u) {
2014-02-15 06:12:11 +00:00
if (self.hasPermission(u, "viewhiddenpoll")) {
2014-01-27 22:44:22 +00:00
u.socket.emit("newPoll", unobscured);
} else {
u.socket.emit("newPoll", obscured);
}
});
};
/**
* Sends a poll notification
*/
Channel.prototype.sendPollUpdate = function (users) {
var self = this;
var unhidden = self.poll.packUpdate(true);
var hidden = self.poll.packUpdate(false);
users.forEach(function (u) {
if (self.hasPermission(u, "viewhiddenpoll")) {
u.socket.emit("updatePoll", unhidden);
} else {
u.socket.emit("updatePoll", hidden);
}
});
};
/**
* Sends a "poll closed" notification
*/
Channel.prototype.sendPollClose = function (users) {
users.forEach(function (u) {
u.socket.emit("closePoll");
});
};
/**
* Broadcasts the channel options
*/
Channel.prototype.sendOpts = function (users) {
var opts = this.opts;
users.forEach(function (u) {
u.socket.emit("channelOpts", opts);
});
};
/**
* Calculates the number of eligible users to voteskip
*/
2013-09-12 03:16:56 +00:00
Channel.prototype.calcVoteskipMax = function () {
var self = this;
return self.users.map(function (u) {
2014-01-27 22:44:22 +00:00
if (!self.hasPermission(u, "voteskip")) {
2013-09-12 03:16:56 +00:00
return 0;
2014-01-27 22:44:22 +00:00
}
2013-09-12 03:16:56 +00:00
return u.meta.afk ? 0 : 1;
}).reduce(function (a, b) {
return a + b;
2013-09-12 21:26:45 +00:00
}, 0);
2013-09-12 03:16:56 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Creates a voteskip update packet
*/
Channel.prototype.getVoteskipPacket = function () {
var have = this.voteskip ? this.voteskip.counts[0] : 0;
var max = this.calcVoteskipMax();
var need = this.voteskip ? Math.ceil(max * this.opts.voteskip_ratio) : 0;
2013-12-27 16:08:03 +00:00
return {
2014-01-27 22:44:22 +00:00
count: have,
2013-12-27 16:08:03 +00:00
need: need
};
2014-01-27 22:44:22 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Sends a voteskip update packet
*/
Channel.prototype.sendVoteskipUpdate = function (users) {
var update = this.getVoteskipPacket();
users.forEach(function (u) {
2013-12-27 16:08:03 +00:00
if (u.rank >= 1.5) {
2014-01-27 22:44:22 +00:00
u.socket.emit("voteskip", update);
2013-12-27 16:08:03 +00:00
}
});
};
2014-01-27 22:44:22 +00:00
/**
* Sends the inline CSS and JS
*/
Channel.prototype.sendCSSJS = function (users) {
var data = {
css: this.css,
js: this.js
};
users.forEach(function (u) {
u.socket.emit("channelCSSJS", data);
});
};
/**
* Sends the MOTD
*/
Channel.prototype.sendMotd = function (users) {
var motd = this.motd;
users.forEach(function (u) {
u.socket.emit("setMotd", motd);
});
};
2013-04-04 19:56:43 +00:00
2014-01-27 22:44:22 +00:00
/**
* Sends the drink count
*/
Channel.prototype.sendDrinks = function (users) {
var drinks = this.drinks;
users.forEach(function (u) {
u.socket.emit("drinkCount", drinks);
});
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Resets video-related variables
*/
Channel.prototype.resetVideo = function () {
2013-07-06 03:34:04 +00:00
this.voteskip = false;
2014-01-27 22:44:22 +00:00
this.sendVoteskipUpdate(this.users);
this.drinks = 0;
2014-01-27 22:44:22 +00:00
this.sendDrinks(this.users);
};
/**
* Handles a queue message from a client
*/
Channel.prototype.handleQueue = function (user, data) {
// Verify the user has permission to add
if (!this.hasPermission(user, "playlistadd")) {
return;
2013-07-01 22:45:55 +00:00
}
2013-05-02 15:34:12 +00:00
2014-01-27 22:44:22 +00:00
// Verify data types
if (typeof data.id !== "string" && data.id !== false) {
return;
2013-05-04 22:54:28 +00:00
}
2014-01-27 22:44:22 +00:00
var id = data.id || false;
if (typeof data.type !== "string") {
return;
2013-05-04 22:54:28 +00:00
}
2014-01-27 22:44:22 +00:00
var type = data.type;
var link = util.formatLink(id, type);
2013-05-04 22:54:28 +00:00
2014-01-27 22:44:22 +00:00
// Verify user has the permission to add at the position given
if (data.pos === "next" && !this.hasPermission(user, "playlistnext")) {
2013-04-03 17:47:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
var pos = data.pos || "end";
// Verify user has permission to add a YouTube playlist, if relevant
if (data.type === "yp" && !this.hasPermission(user, "playlistaddlist")) {
user.socket.emit("queueFail", {
msg: "You don't have permission to add playlists",
link: link
});
2013-04-03 17:47:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
// Verify the user has permission to add livestreams, if relevant
if (util.isLive(type) && !this.hasPermission(user, "playlistaddlive")) {
user.socket.emit("queueFail", {
msg: "You don't have permission to add livestreams",
link: link
});
return;
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
// Verify the user has permission to add a Custom Embed, if relevant
if (data.type === "cu" && !this.hasPermission(user, "playlistaddcustom")) {
user.socket.emit("queueFail", {
msg: "You don't have permission to add custom embeds",
link: null
});
2013-04-03 17:47:41 +00:00
return;
}
2013-04-27 17:13:01 +00:00
2014-01-27 22:44:22 +00:00
/**
* Always reset any user-provided title if it's not a custom embed.
* Additionally reset if it is a custom embed but a title is not provided
*/
if (typeof data.title !== "string" || data.type !== "cu") {
data.title = false;
}
var title = data.title || false;
var queueby = user != null ? user.name : "";
var temp = data.temp || !this.hasPermission(user, "addnontemp");
2014-02-10 02:10:11 +00:00
// Allow override of duration for live content
var duration = undefined;
if (util.isLive(data.type) && typeof data.duration === "number") {
duration = !isNaN(data.duration) ? data.duration : undefined;
}
2014-01-27 22:44:22 +00:00
// Throttle video adds
2013-08-29 00:25:53 +00:00
var limit = {
burst: 3,
sustained: 1
};
2014-01-27 22:44:22 +00:00
if (user.rank >= 2 || this.leader === user) {
2013-08-29 00:25:53 +00:00
limit = {
burst: 10,
sustained: 2
};
}
if (user.queueLimiter.throttle(limit)) {
2013-10-06 06:43:25 +00:00
user.socket.emit("queueFail", {
msg: "You are adding videos too quickly",
link: null
});
2013-08-26 15:36:47 +00:00
return;
2013-04-24 17:45:17 +00:00
}
2014-01-27 22:44:22 +00:00
// Actually add the video
this.addMedia({
id: id,
title: title,
pos: pos,
queueby: queueby,
temp: temp,
type: type,
2014-02-10 02:10:11 +00:00
duration: duration,
2014-01-27 22:44:22 +00:00
maxlength: this.hasPermission(user, "exceedmaxlength") ? 0 : this.opts.maxlength
}, function (err, media) {
if (err) {
user.socket.emit("queueFail", {
msg: err,
link: link
});
return;
}
2013-07-09 16:38:48 +00:00
2014-01-27 22:44:22 +00:00
if (media.restricted) {
user.socket.emit("queueWarn", {
msg: "This video is blocked in the following countries: " +
media.restricted,
link: link
});
return;
2013-09-30 14:51:38 +00:00
}
2014-01-27 22:44:22 +00:00
});
};
2013-07-02 15:38:13 +00:00
2014-01-27 22:44:22 +00:00
/**
* Add a video to the playlist
*/
Channel.prototype.addMedia = function (data, callback) {
2013-08-17 20:47:11 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
if (data.type === "cu" && typeof data.title === "string") {
2013-09-30 00:53:27 +00:00
var t = data.title;
2014-01-27 22:44:22 +00:00
if (t.length > 100) {
2013-09-30 00:53:27 +00:00
t = t.substring(0, 97) + "...";
2014-01-27 22:44:22 +00:00
}
2013-09-30 00:53:27 +00:00
data.title = t;
}
2014-01-27 22:44:22 +00:00
if (data.pos === "end") {
data.pos = "append";
}
var afterLookup = function (lock, shouldCache, media) {
if (data.maxlength && media.seconds > data.maxlength) {
callback("Maximum length exceeded: " + data.maxlength + " seconds", null);
lock.release();
return;
2013-09-30 00:53:27 +00:00
}
2014-01-27 22:44:22 +00:00
media.pos = data.pos;
media.queueby = data.queueby;
media.temp = data.temp;
if (data.title && media.type === "cu") {
media.title = data.title;
}
var res = self.playlist.addMedia(media);
2013-09-30 00:53:27 +00:00
if (res.error) {
2014-01-27 22:44:22 +00:00
callback(res.error, null);
lock.release();
2013-09-30 00:53:27 +00:00
return;
2013-08-28 15:32:35 +00:00
}
2013-09-30 00:53:27 +00:00
2014-02-08 18:45:07 +00:00
self.logger.log("[playlist] " + data.queueby + " queued " + media.title + " (" +
media.type + ":" + media.id + ")");
2013-11-11 04:26:30 +00:00
2013-09-30 00:53:27 +00:00
var item = res.item;
2014-01-27 22:44:22 +00:00
var packet = {
item: item.pack(),
after: item.prev ? item.prev.uid : "prepend"
2014-01-27 22:44:22 +00:00
};
self.users.forEach(function (u) {
u.socket.emit("queue", packet);
});
2014-01-27 22:44:22 +00:00
self.updatePlaylistMeta();
self.sendPlaylistMeta(self.users);
if (shouldCache) {
self.cacheMedia(media);
}
lock.release();
callback(null, media);
2013-09-30 00:53:27 +00:00
};
2014-01-27 22:44:22 +00:00
// Cached video data
if (data.type !== "cu" && typeof data.title === "string") {
self.plqueue.queue(function (lock) {
2013-10-04 03:07:44 +00:00
var m = new Media(data.id, data.title, data.seconds, data.type);
2014-01-27 22:44:22 +00:00
afterLookup(lock, false, m);
2013-10-04 03:07:44 +00:00
});
return;
}
2014-01-27 22:44:22 +00:00
// YouTube playlists
if (data.type === "yp") {
2014-01-27 22:44:22 +00:00
self.plqueue.queue(function (lock) {
InfoGetter.getMedia(data.id, data.type, function (e, vids) {
if (e) {
2014-01-27 22:44:22 +00:00
callback(e, null);
lock.release();
return;
}
2014-01-27 22:44:22 +00:00
// If queueing next, reverse queue order so the videos end up
// in the correct order
if (data.pos === "next") {
vids.reverse();
2014-01-27 22:44:22 +00:00
// Special case to ensure correct playlist order
if (self.playlist.length === 0) {
vids.unshift(vids.pop());
2014-01-27 22:44:22 +00:00
}
}
2014-01-27 22:44:22 +00:00
// We only want to release the lock after the entire playlist
// is processed. Set up a dummy so the same code will work.
var dummy = {
release: function () { }
};
for (var i = 0; i < vids.length; i++) {
2014-01-27 22:44:22 +00:00
afterLookup(dummy, true, vids[i]);
}
2014-01-27 22:44:22 +00:00
lock.release();
});
});
return;
}
2014-01-27 22:44:22 +00:00
// Cases where there is no cached data in the database
if (!self.registered || util.isLive(data.type)) {
self.plqueue.queue(function (lock) {
InfoGetter.getMedia(data.id, data.type, function (e, media) {
2013-09-30 00:53:27 +00:00
if (e) {
2014-01-27 22:44:22 +00:00
callback(e, null);
lock.release();
2013-09-30 00:53:27 +00:00
return;
}
2014-02-10 02:10:11 +00:00
if (data.duration) {
media.seconds = data.duration;
}
2014-01-27 22:44:22 +00:00
afterLookup(lock, false, media);
2013-09-30 00:53:27 +00:00
});
});
2014-01-27 22:44:22 +00:00
return;
}
2014-01-27 22:44:22 +00:00
// Finally, the "normal" case
self.plqueue.queue(function (lock) {
if (self.dead) {
return;
}
var lookupNewMedia = function () {
InfoGetter.getMedia(data.id, data.type, function (e, media) {
if (self.dead) {
return;
2013-08-17 20:47:11 +00:00
}
2014-01-27 22:44:22 +00:00
if (e) {
callback(e, null);
lock.release();
return;
}
2014-01-27 22:44:22 +00:00
afterLookup(lock, true, media);
});
2014-01-27 22:44:22 +00:00
};
db.channels.getLibraryItem(self.name, data.id, function (err, item) {
if (self.dead) {
return;
}
if (err && err !== "Item not in library") {
callback(err, null);
2014-01-27 22:44:22 +00:00
lock.release();
return;
}
if (item !== null) {
afterLookup(lock, true, item);
} else {
lookupNewMedia();
}
2013-09-30 00:53:27 +00:00
});
2014-01-27 22:44:22 +00:00
});
};
2013-06-01 19:42:08 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user queueing a user playlist
*/
Channel.prototype.handleQueuePlaylist = function (user, data) {
2013-08-17 20:47:11 +00:00
var self = this;
if (!self.hasPermission(user, "playlistaddlist")) {
return;
}
2014-01-27 22:44:22 +00:00
if (typeof data.name !== "string") {
return;
}
2014-01-27 22:44:22 +00:00
var name = data.name;
2014-03-02 07:35:20 +00:00
if (data.pos === "next" && !self.hasPermission(user, "playlistnext")) {
return;
}
2014-01-27 22:44:22 +00:00
var pos = data.pos || "end";
2014-01-27 22:44:22 +00:00
var temp = data.temp || !self.hasPermission(user, "addnontemp");
db.getUserPlaylist(user.name, name, function (err, pl) {
if (self.dead) {
return;
2014-01-27 22:44:22 +00:00
}
if (err) {
2013-08-17 20:47:11 +00:00
user.socket.emit("errorMsg", {
msg: "Playlist load failed: " + err
});
return;
}
2013-10-12 20:54:39 +00:00
try {
2014-01-27 22:44:22 +00:00
// Ensure correct order when queueing next
if (pos === "next") {
2013-10-12 20:54:39 +00:00
pl.reverse();
2014-01-27 22:44:22 +00:00
if (pl.length > 0 && self.playlist.items.length === 0) {
2013-10-12 20:54:39 +00:00
pl.unshift(pl.pop());
2014-01-27 22:44:22 +00:00
}
2013-10-12 20:54:39 +00:00
}
2014-02-28 05:47:06 +00:00
pl.forEach(function (pli) {
pli.pos = pos;
pli.temp = temp;
pli.queueby = user.name;
self.addMedia(pli, function (err, media) {
2014-01-27 22:44:22 +00:00
if (err) {
user.socket.emit("queueFail", {
msg: err,
2014-02-28 05:47:06 +00:00
link: util.formatLink(pli.id, pli.type)
2014-01-27 22:44:22 +00:00
});
}
});
2014-02-28 05:47:06 +00:00
});
2013-10-12 20:54:39 +00:00
} catch (e) {
Logger.errlog.log("Loading user playlist failed!");
2014-01-27 22:44:22 +00:00
Logger.errlog.log("PL: " + user.name + "-" + name);
2013-10-12 20:54:39 +00:00
Logger.errlog.log(e.stack);
2014-01-27 22:44:22 +00:00
user.socket.emit("queueFail", {
msg: "Internal error occurred when loading playlist. The administrator has been notified.",
link: null
});
}
});
};
/**
* Handles a user message to delete a playlist item
*/
Channel.prototype.handleDelete = function (user, data) {
var self = this;
if (!self.hasPermission(user, "playlistdelete")) {
return;
}
if (typeof data !== "number") {
return;
}
var plitem = self.playlist.items.find(data);
self.deleteMedia(data, function (err) {
if (!err && plitem && plitem.media) {
2014-02-08 18:45:07 +00:00
self.logger.log("[playlist] " + user.name + " deleted " + plitem.media.title);
2014-01-27 22:44:22 +00:00
}
});
};
/**
* Deletes a playlist item
*/
Channel.prototype.deleteMedia = function (uid, callback) {
var self = this;
self.plqueue.queue(function (lock) {
if (self.dead) {
return;
}
if (self.playlist.remove(uid)) {
self.sendAll("delete", {
uid: uid
});
self.updatePlaylistMeta();
self.sendPlaylistMeta(self.users);
callback(null);
} else {
callback("Delete failed");
}
2014-01-27 22:44:22 +00:00
lock.release();
2013-08-17 20:47:11 +00:00
});
2014-01-27 22:44:22 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Sets the temporary status of a playlist item
*/
Channel.prototype.setTemp = function (uid, temp) {
2013-07-02 15:38:13 +00:00
var item = this.playlist.items.find(uid);
2014-03-04 17:57:05 +00:00
if (item === false) {
return;
2014-01-27 22:44:22 +00:00
}
2013-06-29 22:09:20 +00:00
item.temp = temp;
2013-05-04 22:54:28 +00:00
this.sendAll("setTemp", {
2013-06-29 22:09:20 +00:00
uid: uid,
2013-05-04 22:54:28 +00:00
temp: temp
});
2014-01-27 22:44:22 +00:00
// TODO might change the way this works
if (!temp) {
2013-07-01 22:45:55 +00:00
this.cacheMedia(item.media);
2013-05-04 22:54:28 +00:00
}
2014-01-27 22:44:22 +00:00
};
2013-05-04 22:54:28 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to set a playlist item as temporary/not
*/
Channel.prototype.handleSetTemp = function (user, data) {
if (!this.hasPermission(user, "settemp")) {
2013-05-04 22:54:28 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (typeof data.uid !== "number" || typeof data.temp !== "boolean") {
2013-05-04 22:54:28 +00:00
return;
}
2013-06-29 22:09:20 +00:00
this.setTemp(data.uid, data.temp);
2014-01-27 22:44:22 +00:00
// TODO log?
};
2013-06-11 15:29:21 +00:00
2014-01-27 22:44:22 +00:00
/**
* Moves a playlist item in the playlist
*/
Channel.prototype.move = function (from, after, callback) {
callback = typeof callback === "function" ? callback : function () { };
var self = this;
2014-01-27 22:44:22 +00:00
if (from === after) {
callback("Cannot move playlist item after itself!", null);
return;
}
self.plqueue.queue(function (lock) {
if (self.dead) {
return;
2014-01-27 22:44:22 +00:00
}
2014-01-27 22:44:22 +00:00
if (self.playlist.move(from, after)) {
self.sendAll("moveVideo", {
from: from,
after: after
});
2014-01-27 22:44:22 +00:00
callback(null, true);
} else {
callback(true, null);
}
2014-01-27 22:44:22 +00:00
lock.release();
});
2014-01-27 22:44:22 +00:00
};
/**
* Handles a user message to move a playlist item
*/
Channel.prototype.handleMove = function (user, data) {
var self = this;
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
if (!self.hasPermission(user, "playlistmove")) {
2013-04-03 17:47:41 +00:00
return;
2014-01-27 22:44:22 +00:00
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
if (typeof data.from !== "number" || (typeof data.after !== "number" && typeof data.after !== "string")) {
2013-08-07 03:21:54 +00:00
return;
2013-10-12 23:59:50 +00:00
}
2014-01-27 22:44:22 +00:00
self.move(data.from, data.after, function (err) {
if (!err) {
var fromit = self.playlist.items.find(data.from);
var afterit = self.playlist.items.find(data.after);
var aftertitle = (afterit && afterit.media) ? afterit.media.title : "";
if (fromit) {
2014-02-08 18:45:07 +00:00
self.logger.log("[playlist] " + user.name + " moved " + fromit.media.title +
2014-01-27 22:44:22 +00:00
(aftertitle ? " after " + aftertitle : ""));
}
}
});
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to remove a video from the library
*/
Channel.prototype.handleUncache = function (user, data) {
2013-08-18 23:03:49 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
if (!self.registered) {
return;
}
2014-01-27 22:44:22 +00:00
if (user.rank < 2) {
return;
}
2014-01-27 22:44:22 +00:00
if (typeof data.id !== "string") {
2013-10-07 15:04:08 +00:00
return;
2014-01-27 22:44:22 +00:00
}
db.channels.deleteFromLibrary(self.name, data.id, function (err, res) {
if (self.dead) {
return;
2014-01-27 22:44:22 +00:00
}
2014-01-27 22:44:22 +00:00
if (err) {
2013-08-17 20:47:11 +00:00
return;
2014-01-27 22:44:22 +00:00
}
2013-08-17 20:47:11 +00:00
2014-02-08 18:45:07 +00:00
self.logger.log("[library] " + user.name + " deleted " + data.id + " from library");
2013-08-17 20:47:11 +00:00
});
2014-01-27 22:44:22 +00:00
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to skip to the next video in the playlist
*/
Channel.prototype.handlePlayNext = function (user) {
if (!this.hasPermission(user, "playlistjump")) {
return;
2013-07-01 22:45:55 +00:00
}
2013-04-03 17:47:41 +00:00
2014-02-08 18:45:07 +00:00
var title = "";
if (this.playlist.current && this.playlist.current.title) {
title = " " + this.playlist.current.title;
}
this.logger.log("[playlist] " + user.name + " skipped" + title);
2014-01-27 22:44:22 +00:00
this.playlist.next();
};
2013-08-07 03:21:54 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to jump to a video in the playlist
*/
Channel.prototype.handleJumpTo = function (user, data) {
if (!this.hasPermission(user, "playlistjump")) {
return;
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
if (typeof data !== "string" && typeof data !== "number") {
2013-04-03 17:47:41 +00:00
return;
}
2014-02-08 18:45:07 +00:00
var to = this.playlist.items.find(data);
var title = "";
2014-02-28 14:43:04 +00:00
if (to !== false) {
2014-02-08 18:45:07 +00:00
title = " to " + to.media.title;
2014-02-28 14:43:04 +00:00
this.logger.log("[playlist] " + user.name + " skipped" + title);
this.playlist.jump(data);
2014-02-08 18:45:07 +00:00
}
2014-01-27 22:44:22 +00:00
};
2013-08-07 03:21:54 +00:00
2014-01-27 22:44:22 +00:00
/**
* Clears the playlist
*/
Channel.prototype.clear = function () {
2013-06-29 22:09:20 +00:00
this.playlist.clear();
this.plqueue.reset();
2014-01-27 22:44:22 +00:00
this.updatePlaylistMeta();
this.sendPlaylist(this.users);
};
2013-04-22 20:37:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to clear the playlist
*/
Channel.prototype.handleClear = function (user) {
if (!this.hasPermission(user, "playlistclear")) {
2013-04-22 20:37:42 +00:00
return;
}
2013-08-07 03:21:54 +00:00
2014-02-08 18:45:07 +00:00
this.logger.log("[playlist] " + user.name + " cleared the playlist");
2014-01-27 22:44:22 +00:00
this.clear();
};
2013-04-22 20:37:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Shuffles the playlist
*/
Channel.prototype.shuffle = function () {
2013-07-03 21:29:49 +00:00
var pl = this.playlist.items.toArray(false);
this.playlist.clear();
this.plqueue.reset();
2014-01-27 22:44:22 +00:00
while (pl.length > 0) {
var i = Math.floor(Math.random() * pl.length);
2013-07-03 21:29:49 +00:00
var item = this.playlist.makeItem(pl[i].media);
item.temp = pl[i].temp;
item.queueby = pl[i].queueby;
this.playlist.items.append(item);
2013-06-29 22:09:20 +00:00
pl.splice(i, 1);
}
2014-01-27 22:44:22 +00:00
2013-07-03 21:29:49 +00:00
this.playlist.current = this.playlist.items.first;
2014-01-27 22:44:22 +00:00
this.sendPlaylist(this.users);
2013-07-03 21:29:49 +00:00
this.playlist.startPlayback();
2014-01-27 22:44:22 +00:00
};
2013-04-22 20:37:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to shuffle the playlist
*/
Channel.prototype.handleShuffle = function (user) {
if (!this.hasPermission(user, "playlistshuffle")) {
2013-04-03 17:47:41 +00:00
return;
}
2014-02-08 18:45:07 +00:00
this.logger.log("[playlist] " + user.name + " shuffle the playlist");
2014-01-27 22:44:22 +00:00
this.shuffle();
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a video update from a leader
*/
Channel.prototype.handleUpdate = function (user, data) {
if (this.leader !== user) {
2013-04-04 19:56:43 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (typeof data.id !== "string" || typeof data.currentTime !== "number") {
2013-04-03 17:47:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (this.playlist.current === null) {
return;
}
2014-01-27 22:44:22 +00:00
var media = this.playlist.current.media;
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
if (util.isLive(media.type) && media.type !== "jw") {
return;
2013-05-22 19:38:16 +00:00
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
if (media.id !== data.id || isNaN(data.currentTime)) {
return;
2013-10-12 23:59:50 +00:00
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
media.currentTime = data.currentTime;
media.paused = Boolean(data.paused);
this.sendAll("mediaUpdate", media.timeupdate());
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to open a poll
*/
Channel.prototype.handleOpenPoll = function (user, data) {
if (!this.hasPermission(user, "pollctl")) {
return;
}
2014-01-27 22:44:22 +00:00
if (typeof data.title !== "string" || !(data.opts instanceof Array)) {
return;
}
2014-01-27 22:44:22 +00:00
var title = data.title.substring(0, 255);
var opts = [];
2013-12-01 23:20:42 +00:00
for (var i = 0; i < data.opts.length; i++) {
2014-01-27 22:44:22 +00:00
opts[i] = (""+data.opts[i]).substring(0, 255);
2013-12-01 23:20:42 +00:00
}
2013-09-12 01:22:00 +00:00
var obscured = (data.obscured === true);
2014-01-27 22:44:22 +00:00
var poll = new Poll(user.name, title, opts, obscured);
2014-02-09 05:58:27 +00:00
var self = this;
if (typeof data.timeout === "number" && !isNaN(data.timeout) && data.timeout > 0) {
poll.timer = setTimeout(function () {
if (self.poll === poll) {
self.handleClosePoll({ name: "[poll timer]", rank: 255 });
}
}, data.timeout * 1000);
}
this.poll = poll;
2014-01-27 22:44:22 +00:00
this.sendPoll(this.users, true);
2014-02-08 18:45:07 +00:00
this.logger.log("[poll] " + user.name + " Opened Poll: '" + poll.title + "'");
2014-01-27 22:44:22 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to close the active poll
*/
Channel.prototype.handleClosePoll = function (user) {
if (!this.hasPermission(user, "pollctl")) {
2013-04-03 17:47:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (this.poll) {
2013-09-12 01:22:00 +00:00
if (this.poll.obscured) {
this.poll.obscured = false;
2014-01-27 22:44:22 +00:00
this.sendPollUpdate(this.users);
2013-09-12 01:22:00 +00:00
}
2014-01-27 22:44:22 +00:00
2014-02-09 05:58:27 +00:00
if (this.poll.timer) {
clearTimeout(this.poll.timer);
}
2014-02-08 18:45:07 +00:00
this.logger.log("[poll] " + user.name + " closed the active poll");
2013-04-03 17:47:41 +00:00
this.poll = false;
2014-01-27 22:44:22 +00:00
this.sendAll("closePoll");
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
};
2013-05-22 19:38:16 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to vote in a poll
*/
Channel.prototype.handlePollVote = function (user, data) {
if (!this.hasPermission(user, "pollvote")) {
2013-05-22 19:38:16 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (typeof data.option !== "number") {
2013-04-03 17:47:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (this.poll) {
2013-04-03 17:47:41 +00:00
this.poll.vote(user.ip, data.option);
2014-01-27 22:44:22 +00:00
this.sendPollUpdate(this.users);
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to voteskip the current video
*/
Channel.prototype.handleVoteskip = function (user) {
if (!this.opts.allow_voteskip) {
2013-04-04 16:05:01 +00:00
return;
}
2013-09-12 03:16:56 +00:00
2014-01-27 22:44:22 +00:00
if (!this.hasPermission(user, "voteskip")) {
2013-09-12 03:16:56 +00:00
return;
2014-01-27 22:44:22 +00:00
}
user.setAFK(false);
user.autoAFK();
2014-01-27 22:44:22 +00:00
if (!this.voteskip) {
2013-04-03 17:47:41 +00:00
this.voteskip = new Poll("voteskip", "voteskip", ["yes"]);
}
this.voteskip.vote(user.ip, 0);
2014-02-08 18:45:07 +00:00
var title = "";
if (this.playlist.current && this.playlist.current.title) {
title = " " + this.playlist.current.title;
}
this.logger.log("[playlist] " + (user.name ? user.name : "anonymous") +
" voteskipped" + title);
2013-08-01 13:39:10 +00:00
this.checkVoteskipPass();
2014-01-27 22:44:22 +00:00
};
2013-08-01 13:39:10 +00:00
2014-01-27 22:44:22 +00:00
/**
* Checks if the voteskip requirement is met
*/
2013-08-01 13:39:10 +00:00
Channel.prototype.checkVoteskipPass = function () {
2014-01-27 22:44:22 +00:00
if (!this.opts.allow_voteskip) {
return false;
}
if (!this.voteskip) {
2013-08-01 13:39:10 +00:00
return false;
2014-01-27 22:44:22 +00:00
}
2013-08-01 13:39:10 +00:00
2014-01-27 22:44:22 +00:00
if (this.playlist.length === 0) {
2013-08-01 13:39:10 +00:00
return false;
2014-01-27 22:44:22 +00:00
}
2013-08-01 13:39:10 +00:00
2014-01-27 22:44:22 +00:00
var max = this.calcVoteskipMax();
var need = Math.ceil(max * this.opts.voteskip_ratio);
if (this.voteskip.counts[0] >= need) {
2014-02-08 18:45:07 +00:00
var title = "";
if (this.playlist.current && this.playlist.current.title) {
title = " " + this.playlist.current.title;
}
this.logger.log("[playlist] Voteskip passed, skipping" + title);
2014-01-27 22:44:22 +00:00
this.playlist.next();
}
2013-08-01 13:39:10 +00:00
2014-01-27 22:44:22 +00:00
this.sendVoteskipUpdate(this.users);
2013-08-01 13:39:10 +00:00
return true;
2014-01-27 22:44:22 +00:00
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Sets the locked state of the playlist
*/
Channel.prototype.setLock = function (locked) {
this.playlistLock = locked;
this.sendPlaylistLock(this.users);
};
/**
* Handles a user message to change the locked state of the playlist
*/
Channel.prototype.handleSetLock = function (user, data) {
if (!this.hasPermission(user, "playlistlock")) {
return;
}
data.locked = Boolean(data.locked);
2014-02-08 18:45:07 +00:00
this.logger.log("[playlist] " + user.name + " set playlist lock to " + data.locked);
2014-01-27 22:44:22 +00:00
this.setLock(data.locked);
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to toggle the locked state of the playlist
*/
Channel.prototype.handleToggleLock = function (user) {
this.handleSetLock(user, { locked: !this.playlistLock });
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Imports a list of chat filters, replacing the current list
*/
Channel.prototype.importFilters = function (filters) {
this.filters = filters;
this.sendChatFilters(this.users);
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to import a list of chat filters
*/
Channel.prototype.handleImportFilters = function (user, data) {
if (!this.hasPermission(user, "filterimport")) {
2013-04-03 17:47:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (!(data instanceof Array)) {
2013-06-11 15:29:21 +00:00
return;
}
2014-01-27 22:44:22 +00:00
this.filters = data.map(this.validateChatFilter.bind(this))
.filter(function (f) { return f !== false; });
2013-06-11 15:29:21 +00:00
2014-01-27 22:44:22 +00:00
this.sendChatFilters(this.users);
};
/**
* Validates data for a chat filter
*/
Channel.prototype.validateChatFilter = function (f) {
if (typeof f.source !== "string" || typeof f.flags !== "string" ||
typeof f.replace !== "string") {
return false;
2013-10-12 23:59:50 +00:00
}
2013-06-18 15:51:42 +00:00
if (typeof f.name !== "string") {
2014-01-27 22:44:22 +00:00
f.name = f.source;
}
2014-01-27 22:44:22 +00:00
f.replace = f.replace.substring(0, 1000);
f.replace = XSS.sanitizeHTML(f.replace);
f.flags = f.flags.substring(0, 4);
2013-06-18 15:51:42 +00:00
2014-01-27 22:44:22 +00:00
try {
new RegExp(f.source, f.flags);
} catch (e) {
return false;
}
2014-01-27 22:44:22 +00:00
var filter = new Filter(f.name, f.source, f.flags, f.replace);
filter.active = Boolean(f.active);
filter.filterlinks = Boolean(f.filterlinks);
return filter;
};
/**
* Updates a chat filter, or adds a new one if the filter does not exist
*/
Channel.prototype.updateFilter = function (filter) {
var self = this;
if (!filter.name) {
2013-06-18 15:51:42 +00:00
filter.name = filter.source;
2014-01-27 22:44:22 +00:00
}
2013-06-18 15:51:42 +00:00
var found = false;
2014-01-27 22:44:22 +00:00
for (var i = 0; i < self.filters.length; i++) {
if (self.filters[i].name === filter.name) {
2013-06-18 15:51:42 +00:00
found = true;
2014-01-27 22:44:22 +00:00
self.filters[i] = filter;
break;
}
}
2014-01-27 22:44:22 +00:00
if (!found) {
self.filters.push(filter);
2013-06-18 15:51:42 +00:00
}
2014-01-27 22:44:22 +00:00
self.users.forEach(function (u) {
if (self.hasPermission(u, "filteredit")) {
u.socket.emit("updateChatFilter", filter);
}
});
};
/**
* Handles a user message to update a filter
*/
Channel.prototype.handleUpdateFilter = function (user, f) {
if (!this.hasPermission(user, "filteredit")) {
2013-10-12 23:59:50 +00:00
user.kick("Attempted updateFilter with insufficient permission");
return;
}
2014-01-27 22:44:22 +00:00
filter = this.validateChatFilter(f);
if (!filter) {
2013-04-03 17:47:41 +00:00
return;
}
2014-02-08 18:45:07 +00:00
this.logger.log("[mod] " + user.name + " updated filter: " + f.name + " -> " +
"s/" + f.source + "/" + f.replace + "/" + f.flags + " active: " +
f.active);
2014-01-27 22:44:22 +00:00
this.updateFilter(filter);
};
2014-01-27 22:44:22 +00:00
/**
* Removes a chat filter
*/
Channel.prototype.removeFilter = function (filter) {
var self = this;
for (var i = 0; i < self.filters.length; i++) {
if (self.filters[i].name === filter.name) {
self.filters.splice(i, 1);
self.users.forEach(function (u) {
if (self.hasPermission(u, "filteredit")) {
u.socket.emit("deleteChatFilter", filter);
}
});
break;
}
}
2014-01-27 22:44:22 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to delete a chat filter
*/
Channel.prototype.handleRemoveFilter = function (user, f) {
if (!this.hasPermission(user, "filteredit")) {
user.kick("Attempted removeFilter with insufficient permission");
return;
2013-06-18 15:51:42 +00:00
}
2014-01-27 22:44:22 +00:00
if (typeof f.name !== "string") {
2013-04-03 17:47:41 +00:00
return;
}
2014-02-08 18:45:07 +00:00
this.logger.log("[mod] " + user.name + " removed filter: " + f.name);
2014-01-27 22:44:22 +00:00
this.removeFilter(f);
};
/**
* Changes the order of chat filters
*/
Channel.prototype.moveFilter = function (from, to) {
if (from < 0 || to < 0 || from >= this.filters.length || to >= this.filters.length) {
2013-06-18 15:51:42 +00:00
return;
2013-04-03 17:47:41 +00:00
}
2014-01-27 22:44:22 +00:00
var f = this.filters[from];
to = to > from ? to + 1 : to;
from = to > from ? from : from + 1;
2013-06-18 15:51:42 +00:00
this.filters.splice(to, 0, f);
this.filters.splice(from, 1);
2014-01-27 22:44:22 +00:00
// TODO broadcast
};
2013-06-18 15:51:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to change the chat filter order
*/
Channel.prototype.handleMoveFilter = function (user, data) {
if (!this.hasPermission(user, "filteredit")) {
2013-10-12 23:59:50 +00:00
user.kick("Attempted moveFilter with insufficient permission");
2013-06-18 15:51:42 +00:00
return;
2013-10-12 23:59:50 +00:00
}
2013-06-18 15:51:42 +00:00
2014-01-27 22:44:22 +00:00
if (typeof data.to !== "number" || typeof data.from !== "number") {
2013-06-18 15:51:42 +00:00
return;
2013-10-12 23:59:50 +00:00
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
this.moveFilter(data.from, data.to);
};
2014-02-10 05:53:46 +00:00
/**
* Imports a list of emotes, replacing the current list
*/
Channel.prototype.importEmotes = function (emotes) {
this.emotes = emotes;
this.sendEmoteList(this.users);
};
/**
* Handles a user message to import a list of emotes
*/
Channel.prototype.handleImportEmotes = function (user, data) {
if (!this.hasPermission(user, "emoteimport")) {
return;
}
if (!(data instanceof Array)) {
return;
}
this.emotes = data.map(this.validateEmote.bind(this))
.filter(function (f) { return f !== false; });
this.sendEmoteList(this.users);
};
/**
* Validates data for an emote
*/
Channel.prototype.validateEmote = function (f) {
2014-02-13 05:33:42 +00:00
if (typeof f.name !== "string" || typeof f.image !== "string") {
2014-02-10 05:53:46 +00:00
return false;
}
f.image = f.image.substring(0, 1000);
f.image = XSS.sanitizeText(f.image);
2014-02-13 05:33:42 +00:00
var s = f.name.replace(/\\\.\?\+\*\$\^\(\)\[\]\{\}/g, "\\$1");
s = s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
2014-02-16 07:33:38 +00:00
s = "(^|\\s)" + s + "($|\\s)";
2014-02-13 05:33:42 +00:00
f.source = s;
2014-02-10 05:53:46 +00:00
try {
new RegExp(f.regex, "gi");
} catch (e) {
return false;
}
return f;
};
/**
* Updates an emote, or adds a new one if the emote does not exist
*/
Channel.prototype.updateEmote = function (emote) {
var self = this;
2014-02-13 05:33:42 +00:00
emote = this.validateEmote(emote);
if (!emote) {
return;
2014-02-10 05:53:46 +00:00
}
var found = false;
for (var i = 0; i < self.emotes.length; i++) {
if (self.emotes[i].name === emote.name) {
found = true;
self.emotes[i] = emote;
break;
}
}
if (!found) {
self.emotes.push(emote);
}
self.users.forEach(function (u) {
u.socket.emit("updateEmote", emote);
});
};
/**
* Handles a user message to update an emote
*/
Channel.prototype.handleUpdateEmote = function (user, f) {
if (!this.hasPermission(user, "emoteedit")) {
user.kick("Attempted updateEmote with insufficient permission");
return;
}
var emote = this.validateEmote(f);
if (!emote) {
return;
}
this.logger.log("[mod] " + user.name + " updated emote: " + f.name + " -> " +
f.image);
this.updateEmote(emote);
};
/**
* Removes an emote
*/
Channel.prototype.removeEmote = function (emote) {
var self = this;
for (var i = 0; i < self.emotes.length; i++) {
if (self.emotes[i].name === emote.name) {
self.emotes.splice(i, 1);
self.users.forEach(function (u) {
2014-02-13 05:33:42 +00:00
u.socket.emit("removeEmote", emote);
2014-02-10 05:53:46 +00:00
});
break;
}
}
};
/**
* Handles a user message to delete an emote
*/
Channel.prototype.handleRemoveEmote = function (user, f) {
if (!this.hasPermission(user, "emoteedit")) {
user.kick("Attempted removeEmote with insufficient permission");
return;
}
if (typeof f.name !== "string") {
return;
}
this.logger.log("[mod] " + user.name + " removed emote: " + f.name);
2014-02-13 05:33:42 +00:00
this.removeEmote(f);
2014-02-10 05:53:46 +00:00
};
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to change the channel permissions
*/
Channel.prototype.handleSetPermissions = function (user, perms) {
if (user.rank < 3) {
user.kick("Attempted setPermissions as a non-admin");
2013-05-22 19:38:16 +00:00
return;
}
2014-01-27 22:44:22 +00:00
for (var key in perms) {
if (key in this.permissions) {
this.permissions[key] = perms[key];
}
2013-05-22 19:38:16 +00:00
}
2014-01-27 22:44:22 +00:00
2014-02-18 01:06:49 +00:00
if ("seeplaylist" in perms) {
this.sendPlaylist(this.users);
}
2014-02-08 18:45:07 +00:00
this.logger.log("[mod] " + user.name + " updated permissions");
2013-05-22 19:38:16 +00:00
this.sendAll("setPermissions", this.permissions);
2014-01-27 22:44:22 +00:00
};
2013-05-22 19:38:16 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to change the channel settings
*/
Channel.prototype.handleUpdateOptions = function (user, data) {
if (user.rank < 2) {
user.kick("Attempted setOptions as a non-moderator");
2013-04-03 17:47:41 +00:00
return;
}
if ("allow_voteskip" in data) {
2014-02-28 06:09:20 +00:00
this.opts.allow_voteskip = Boolean(data.allow_voteskip);
}
if ("voteskip_ratio" in data) {
var ratio = parseFloat(data.voteskip_ratio);
if (isNaN(ratio) || ratio < 0) {
ratio = 0;
}
this.opts.voteskip_ratio = ratio;
}
2013-08-07 14:32:43 +00:00
if ("afk_timeout" in data) {
var tm = parseInt(data.afk_timeout);
if (isNaN(tm) || tm < 0) {
tm = 0;
}
2014-01-27 22:44:22 +00:00
var same = tm === this.opts.afk_timeout;
this.opts.afk_timeout = tm;
if (!same) {
this.users.forEach(function (u) {
u.autoAFK();
});
}
2013-08-07 14:32:43 +00:00
}
if ("pagetitle" in data && user.rank >= 3) {
2014-02-06 00:05:52 +00:00
var title = (""+data.pagetitle).substring(0, 100);
if (!title.trim().match(Config.get("reserved-names.pagetitles"))) {
this.opts.pagetitle = (""+data.pagetitle).substring(0, 100);
} else {
user.socket.emit("errorMsg", {
msg: "That pagetitle is reserved",
alert: true
});
}
2013-11-25 22:20:15 +00:00
}
if ("maxlength" in data) {
var ml = parseInt(data.maxlength);
if (isNaN(ml) || ml < 0) {
ml = 0;
2013-04-03 17:47:41 +00:00
}
this.opts.maxlength = ml;
}
if ("externalcss" in data && user.rank >= 3) {
2014-01-27 22:44:22 +00:00
this.opts.externalcss = (""+data.externalcss).substring(0, 255);
}
if ("externaljs" in data && user.rank >= 3) {
2014-01-27 22:44:22 +00:00
this.opts.externaljs = (""+data.externaljs).substring(0, 255);
}
if ("chat_antiflood" in data) {
this.opts.chat_antiflood = Boolean(data.chat_antiflood);
}
if ("chat_antiflood_params" in data) {
if (typeof data.chat_antiflood_params !== "object") {
data.chat_antiflood_params = {
burst: 4,
sustained: 1
};
}
2014-01-27 22:44:22 +00:00
var b = parseInt(data.chat_antiflood_params.burst);
2014-01-27 22:44:22 +00:00
if (isNaN(b) || b < 0) {
b = 1;
2014-01-27 22:44:22 +00:00
}
var s = parseInt(data.chat_antiflood_params.sustained);
if (isNaN(s) || s <= 0) {
s = 1;
2014-01-27 22:44:22 +00:00
}
var c = b / s;
2014-01-27 22:44:22 +00:00
this.opts.chat_antiflood_params = {
burst: b,
sustained: s,
cooldown: c
};
}
if ("show_public" in data && user.rank >= 3) {
this.opts.show_public = Boolean(data.show_public);
}
if ("enable_link_regex" in data) {
this.opts.enable_link_regex = Boolean(data.enable_link_regex);
2013-04-03 17:47:41 +00:00
}
2013-11-30 04:53:08 +00:00
if ("password" in data && user.rank >= 3) {
2014-01-27 22:44:22 +00:00
var pw = data.password + "";
pw = pw === "" ? false : pw.substring(0, 100);
2013-11-30 04:53:08 +00:00
this.opts.password = pw;
2013-04-03 17:47:41 +00:00
}
2014-02-08 18:45:07 +00:00
this.logger.log("[mod] " + user.name + " updated channel options");
2014-01-27 22:44:22 +00:00
this.sendOpts(this.users);
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to set the inline channel CSS
*/
Channel.prototype.handleSetCSS = function (user, data) {
if (user.rank < 3) {
user.kick("Attempted setChannelCSS as non-admin");
2013-05-15 15:34:27 +00:00
return;
}
2013-10-12 23:59:50 +00:00
if (typeof data.css !== "string") {
return;
}
2014-01-27 22:44:22 +00:00
var css = data.css.substring(0, 20000);
2013-05-15 15:34:27 +00:00
this.css = css;
2014-01-27 22:44:22 +00:00
this.sendCSSJS(this.users);
2014-02-08 18:45:07 +00:00
this.logger.log("[mod] " + user.name + " updated the channel CSS");
2014-01-27 22:44:22 +00:00
};
2013-05-15 15:34:27 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to set the inline channel CSS
*/
Channel.prototype.handleSetJS = function (user, data) {
if (user.rank < 3) {
user.kick("Attempted setChannelJS as non-admin");
2013-10-12 23:59:50 +00:00
return;
}
2014-01-27 22:44:22 +00:00
2013-10-12 23:59:50 +00:00
if (typeof data.js !== "string") {
2013-05-15 15:34:27 +00:00
return;
}
2014-01-27 22:44:22 +00:00
var js = data.js.substring(0, 20000);
2013-05-15 15:34:27 +00:00
this.js = js;
2014-01-27 22:44:22 +00:00
this.sendCSSJS(this.users);
2014-02-08 18:45:07 +00:00
this.logger.log("[mod] " + user.name + " updated the channel JS");
2014-01-27 22:44:22 +00:00
};
2013-05-15 15:34:27 +00:00
2014-01-27 22:44:22 +00:00
/**
* Sets the MOTD
*/
Channel.prototype.setMotd = function (motd) {
motd = XSS.sanitizeHTML(motd);
2013-04-03 17:47:41 +00:00
var html = motd.replace(/\n/g, "<br>");
this.motd = {
motd: motd,
html: html
};
2014-01-27 22:44:22 +00:00
this.sendMotd(this.users);
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to update the MOTD
*/
Channel.prototype.handleSetMotd = function (user, data) {
if (!this.hasPermission(user, "motdedit")) {
2013-10-12 23:59:50 +00:00
user.kick("Attempted setMotd with insufficient permission");
return;
}
if (typeof data.motd !== "string") {
2013-04-03 17:47:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
var motd = data.motd.substring(0, 20000);
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
this.setMotd(motd);
2014-02-08 18:45:07 +00:00
this.logger.log("[mod] " + user.name + " updated the MOTD");
2014-01-27 22:44:22 +00:00
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user chat message
*/
Channel.prototype.handleChat = function (user, data) {
if (!this.hasPermission(user, "chat")) {
return;
}
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
if (typeof data.meta !== "object") {
2013-11-17 19:12:56 +00:00
data.meta = {};
}
2014-01-27 22:44:22 +00:00
if (!user.name) {
2013-04-03 17:47:41 +00:00
return;
}
2014-01-27 22:44:22 +00:00
if (typeof data.msg !== "string") {
2013-06-25 14:18:33 +00:00
return;
}
2014-01-27 22:44:22 +00:00
var msg = data.msg.substring(0, 240);
2013-06-25 14:18:33 +00:00
2014-01-27 22:44:22 +00:00
var muted = this.isMuted(user.name);
var smuted = this.isShadowMuted(user.name);
2013-04-03 17:47:41 +00:00
2013-11-17 21:32:19 +00:00
var meta = {};
if (user.rank >= 2) {
if ("modflair" in data.meta && data.meta.modflair === user.rank) {
meta.modflair = data.meta.modflair;
}
}
2013-11-19 21:14:40 +00:00
if (user.rank < 2 && this.opts.chat_antiflood &&
2013-11-19 21:14:40 +00:00
user.chatLimiter.throttle(this.opts.chat_antiflood_params)) {
2014-01-27 22:44:22 +00:00
user.socket.emit("chatCooldown", 1000 / this.opts.chat_antiflood_params.sustained);
2013-04-27 17:13:01 +00:00
}
2013-04-03 17:47:41 +00:00
2014-03-04 22:20:37 +00:00
if (muted) {
user.socket.emit("noflood", {
action: "chat",
msg: "You have been muted on this channel."
});
return;
} else if (smuted) {
2014-01-27 22:44:22 +00:00
msg = XSS.sanitizeText(msg);
2013-11-09 02:45:59 +00:00
msg = this.filterMessage(msg);
var msgobj = {
username: user.name,
msg: msg,
2014-01-27 22:44:22 +00:00
meta: meta,
2013-11-09 02:45:59 +00:00
time: Date.now()
};
2014-01-27 22:44:22 +00:00
this.shadowMutedUsers().forEach(function (u) {
u.socket.emit("chatMsg", msgobj);
});
2013-11-09 02:45:59 +00:00
return;
}
2013-11-17 19:12:56 +00:00
if (msg.indexOf("/") === 0) {
2014-01-27 22:44:22 +00:00
if (!ChatCommand.handle(this, user, msg, meta)) {
this.sendMessage(user, msg, meta);
}
2013-11-17 19:12:56 +00:00
} else {
if (msg.indexOf(">") === 0) {
2014-01-27 22:44:22 +00:00
meta.addClass = "greentext";
2013-11-17 19:12:56 +00:00
}
2014-01-27 22:44:22 +00:00
this.sendMessage(user, msg, meta);
2013-11-17 19:12:56 +00:00
}
2014-01-27 22:44:22 +00:00
};
2013-04-28 18:04:15 +00:00
2014-02-15 07:40:14 +00:00
Channel.prototype.handlePm = function (user, data) {
if (typeof data.meta !== "object") {
data.meta = {};
}
if (!user.name) {
return;
}
if (typeof data.msg !== "string" || typeof data.to !== "string") {
return;
}
var reallyTo = data.to;
2014-02-16 04:23:18 +00:00
data.to = data.to.toLowerCase();
2014-02-15 07:40:14 +00:00
2014-02-15 18:29:05 +00:00
if (data.to === user.name) {
user.socket.emit("errorMsg", {
msg: "You can't PM yourself!"
});
return;
}
if (!util.isValidUserName(data.to)) {
user.socket.emit("errorMsg", {
msg: data.to + " isn't a valid username."
});
return;
}
2014-02-15 07:40:14 +00:00
var msg = data.msg.substring(0, 240);
var to = null;
for (var i = 0; i < this.users.length; i++) {
if (this.users[i].name.toLowerCase() === data.to) {
to = this.users[i];
break;
}
}
if (!to) {
2014-02-15 18:29:05 +00:00
user.socket.emit("errorMsg", {
msg: data.to + " is not on this channel."
});
2014-02-15 07:40:14 +00:00
return;
}
var meta = {};
if (user.rank >= 2) {
if ("modflair" in data.meta && data.meta.modflair === user.rank) {
meta.modflair = data.meta.modflair;
}
}
if (msg.indexOf(">") === 0) {
meta.addClass = "greentext";
}
msg = XSS.sanitizeText(msg);
msg = this.filterMessage(msg);
var msgobj = {
username: user.name,
to: reallyTo,
2014-02-15 07:40:14 +00:00
msg: msg,
meta: meta,
time: Date.now()
};
to.socket.emit("pm", msgobj);
user.socket.emit("pm", msgobj);
};
2014-01-27 22:44:22 +00:00
/**
* Filters a chat message
*/
Channel.prototype.filterMessage = function (msg) {
2013-09-27 15:28:48 +00:00
const link = /(\w+:\/\/(?:[^:\/\[\]\s]+|\[[0-9a-f:]+\])(?::\d+)?(?:\/[^\/\s]*)*)/ig;
2014-01-27 22:44:22 +00:00
var parts = msg.split(link);
for (var j = 0; j < parts.length; j++) {
// Case 1: The substring is a URL
if (this.opts.enable_link_regex && parts[j].match(link)) {
var original = parts[j];
// Apply chat filters that are active and filter links
for (var i = 0; i < this.filters.length; i++) {
if (!this.filters[i].filterlinks || !this.filters[i].active) {
2013-06-18 15:51:42 +00:00
continue;
2014-01-27 22:44:22 +00:00
}
parts[j] = this.filters[i].filter(parts[j]);
2013-06-18 15:51:42 +00:00
}
2013-09-27 15:28:48 +00:00
2014-01-27 22:44:22 +00:00
// Unchanged, apply link filter
if (parts[j] === original) {
parts[j] = url.format(url.parse(parts[j]));
parts[j] = parts[j].replace(link, "<a href=\"$1\" target=\"_blank\">$1</a>");
2013-09-27 15:28:48 +00:00
}
2014-01-27 22:44:22 +00:00
2013-03-29 18:15:46 +00:00
continue;
2014-01-27 22:44:22 +00:00
} else {
// Substring is not a URL
for (var i = 0; i < this.filters.length; i++) {
if (!this.filters[i].active) {
continue;
}
parts[j] = this.filters[i].filter(parts[j]);
}
2013-05-13 23:54:52 +00:00
}
2013-03-29 18:15:46 +00:00
}
2013-04-01 21:02:09 +00:00
2014-01-27 22:44:22 +00:00
// Recombine the message
return parts.join("");
};
/**
* Sends a chat message
*/
Channel.prototype.sendMessage = function (user, msg, meta) {
msg = XSS.sanitizeText(msg);
2013-11-17 19:12:56 +00:00
msg = this.filterMessage(msg);
var msgobj = {
username: user.name,
msg: msg,
meta: meta,
time: Date.now()
2014-01-27 22:44:22 +00:00
};
2013-11-17 19:12:56 +00:00
2014-01-27 22:44:22 +00:00
this.sendAll("chatMsg", msgobj);
this.chatbuffer.push(msgobj);
if (this.chatbuffer.length > 15) {
this.chatbuffer.shift();
2013-11-17 19:12:56 +00:00
}
2014-01-27 22:44:22 +00:00
this.logger.log("<" + user.name + (meta.addClass ? "." + meta.addClass : "") + "> " +
XSS.decodeText(msg));
};
2013-04-03 17:47:41 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to change another user's rank
*/
Channel.prototype.handleSetRank = function (user, data) {
2013-08-17 17:30:52 +00:00
var self = this;
2014-01-27 22:44:22 +00:00
if (user.rank < 2) {
user.kick("Attempted setChannelRank as a non-moderator");
return;
2013-10-12 23:59:50 +00:00
}
2014-01-27 22:44:22 +00:00
if (typeof data.user !== "string" || typeof data.rank !== "number") {
return;
2013-10-12 23:59:50 +00:00
}
2014-01-27 22:44:22 +00:00
var name = data.user.substring(0, 20);
var rank = data.rank;
2014-03-01 23:37:59 +00:00
if (isNaN(rank) || rank < 1 || (rank >= user.rank && !(user.rank === 4 &&
rank === 4))) {
2014-02-18 00:51:19 +00:00
user.socket.emit("channelRankFail", {
msg: "Updating user rank failed: You can't promote someone to equal or " +
"higher rank than yourself, or demote them below rank 1."
});
return;
2014-01-27 22:44:22 +00:00
}
var receiver;
2014-01-27 22:44:22 +00:00
var lowerName = name.toLowerCase();
for (var i = 0; i < self.users.length; i++) {
if (self.users[i].name.toLowerCase() === lowerName) {
2013-08-17 17:30:52 +00:00
receiver = self.users[i];
break;
}
}
2014-01-27 22:44:22 +00:00
var updateDB = function () {
self.getRank(name, function (err, oldrank) {
if (self.dead) {
return;
2014-01-27 22:44:22 +00:00
}
2014-01-27 22:44:22 +00:00
if (err) {
user.socket.emit("channelRankFail", {
msg: "Updating user rank failed: " + err
});
2013-08-17 17:30:52 +00:00
return;
2014-01-27 22:44:22 +00:00
}
2014-03-01 23:37:59 +00:00
if (oldrank >= user.rank && !(oldrank === 4 && user.rank === 4)) {
2014-01-27 22:44:22 +00:00
user.socket.emit("channelRankFail", {
msg: "Updating user rank failed: " + name + " has equal or higher " +
"rank than you"
});
2013-08-17 17:30:52 +00:00
return;
2014-01-27 22:44:22 +00:00
}
db.channels.setRank(self.name, name, rank, function (err, res) {
if (self.dead) {
return;
}
2014-01-27 22:44:22 +00:00
if (err) {
user.socket.emit("channelRankFail", {
msg: "Updating user rank failed: " + err
});
return;
2014-01-27 22:44:22 +00:00
}
2014-02-08 18:45:07 +00:00
self.logger.log("[mod] " + user.name + " set " + name + "'s rank to " + rank);
2014-02-18 00:51:19 +00:00
if (receiver) {
receiver.rank = rank;
receiver.socket.emit("rank", rank);
}
2014-01-27 22:44:22 +00:00
self.sendAll("setUserRank", {
name: name,
rank: rank
});
2013-08-17 17:30:52 +00:00
});
});
2014-01-27 22:44:22 +00:00
};
if (receiver) {
2014-03-01 23:37:59 +00:00
var receiverrank = Math.max(receiver.rank, receiver.global_rank);
if (receiverrank > user.rank && !(receiverrank === 4 && user.rank === 4)) {
2014-02-18 00:51:19 +00:00
user.socket.emit("channelRankFail", {
msg: "Updating user rank failed: " + receiver.name + " has higher rank "+
"than you."
});
2014-01-27 22:44:22 +00:00
return;
}
if (receiver.loggedIn) {
updateDB();
2014-02-18 00:51:19 +00:00
} else {
self.logger.log("[mod] " + user.name + " set " + name + "'s rank to " + rank);
receiver.rank = rank;
receiver.socket.emit("rank", rank);
self.sendAll("setUserRank", {
name: name,
rank: rank
});
2014-01-27 22:44:22 +00:00
}
} else if (self.registered) {
updateDB();
2013-02-16 05:02:42 +00:00
}
2014-01-27 22:44:22 +00:00
};
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Assigns a leader for video playback
*/
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;
2014-01-27 22:44:22 +00:00
if (old.rank === 1.5) {
2013-05-22 19:38:16 +00:00
old.rank = old.oldrank;
2014-01-27 22:44:22 +00:00
old.socket.emit("rank", old.rank);
this.sendAll("setUserRank", {
name: old.name,
rank: old.rank
});
2013-05-22 19:38:16 +00:00
}
2013-02-16 05:02:42 +00:00
}
2014-01-27 22:44:22 +00:00
if (!name) {
2013-11-09 04:12:17 +00:00
this.sendAll("setLeader", "");
2014-02-08 18:45:07 +00:00
this.logger.log("[playlist] Resuming autolead");
2013-06-30 00:59:33 +00:00
this.playlist.lead(true);
2013-02-16 05:02:42 +00:00
return;
}
2014-01-27 22:44:22 +00:00
for (var i = 0; i < this.users.length; i++) {
if (this.users[i].name === name) {
2013-11-09 04:12:17 +00:00
this.sendAll("setLeader", name);
2014-02-08 18:45:07 +00:00
this.logger.log("[playlist] Assigned leader: " + name);
2013-06-30 00:59:33 +00:00
this.playlist.lead(false);
2013-02-16 05:02:42 +00:00
this.leader = this.users[i];
2014-01-27 22:44:22 +00:00
if (this.users[i].rank < 1.5) {
2013-05-22 19:38:16 +00:00
this.users[i].oldrank = this.users[i].rank;
this.users[i].rank = 1.5;
2014-01-27 22:44:22 +00:00
this.users[i].socket.emit("rank", 1.5);
this.sendAll("setUserRank", {
name: name,
rank: this.users[i].rank
});
2013-05-22 19:38:16 +00:00
}
2014-01-27 22:44:22 +00:00
break;
2013-02-16 05:02:42 +00:00
}
}
2014-01-27 22:44:22 +00:00
};
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
/**
* Handles a user message to assign a new leader
*/
Channel.prototype.handleChangeLeader = function (user, data) {
if (!this.hasPermission(user, "leaderctl")) {
2013-10-12 23:59:50 +00:00
user.kick("Attempted assignLeader with insufficient permission");
2013-04-03 17:47:41 +00:00
return;
2013-04-02 19:07:22 +00:00
}
2013-02-16 05:02:42 +00:00
2014-01-27 22:44:22 +00:00
if (typeof data.name !== "string") {
2013-02-16 05:02:42 +00:00
return;
}
2013-04-03 17:47:41 +00:00
this.changeLeader(data.name);
2014-02-08 18:45:07 +00:00
this.logger.log("[mod] " + user.name + " assigned leader to " + data.name);
2014-01-27 22:44:22 +00:00
};
/**
* Searches channel library
*/
Channel.prototype.search = function (query, callback) {
var self = this;
if (!self.registered) {
callback([]);
return;
}
if (typeof query !== "string") {
query = "";
}
query = query.substring(0, 100);
db.channels.searchLibrary(self.name, query, function (err, res) {
if (err) {
res = [];
}
res.sort(function(a, b) {
var x = a.title.toLowerCase();
var y = b.title.toLowerCase();
return (x == y) ? 0 : (x < y ? -1 : 1);
});
res.forEach(function (r) {
r.duration = util.formatTime(r.seconds);
});
callback(res);
});
};
/**
* Sends the result of readLog() to a user if the user has sufficient permission
*/
Channel.prototype.handleReadLog = function (user) {
var self = this;
if (user.rank < 3) {
user.kick("Attempted readChanLog with insufficient permission");
return;
}
if (!self.registered) {
user.socket.emit("readChanLog", {
success: false,
data: "Channel log is only available to registered channels."
});
return;
}
var filterIp = user.global_rank < 255;
self.readLog(filterIp, function (err, data) {
if (err) {
user.socket.emit("readChanLog", {
success: false,
data: "Reading channel log failed."
});
} else {
user.socket.emit("readChanLog", {
success: true,
data: data
});
}
});
};
/**
* Reads the last 100KiB of the channel's log file, masking IP addresses if desired
*/
Channel.prototype.readLog = function (filterIp, callback) {
var maxLen = 102400; // Limit to last 100KiB
var file = this.logger.filename;
fs.stat(file, function (err, data) {
if (err) {
callback(err, null);
return;
}
var start = Math.max(data.size - maxLen, 0);
var end = data.size - 1;
var rs = fs.createReadStream(file, {
start: start,
end: end
});
var buffer = "";
rs.on("data", function (data) {
buffer += data;
});
rs.on("end", function () {
if (filterIp) {
buffer = buffer.replace(
/\d+\.\d+\.(\d+\.\d+)/g,
"x.x.$1"
).replace(
/\d+\.\d+\.(\d+)/g,
"x.x.$.*"
);
}
callback(null, buffer);
});
});
};
/**
* Broadcasts a message to the entire channel
*/
Channel.prototype.sendAll = function (msg, data) {
this.users.forEach(function (u) {
u.socket.emit(msg, data);
});
};
2013-02-16 05:02:42 +00:00
/**
* Loads a special set of permissions for unregistered channels
*/
Channel.prototype.setUnregisteredPermissions = function () {
2014-02-28 19:53:23 +00:00
var perms = {
seeplaylist: -1,
playlistadd: -1, // Add video to the playlist
playlistnext: 0,
playlistmove: 0, // Move a video on the playlist
playlistdelete: 0, // Delete a video from the playlist
playlistjump: 0, // Start a different video on the playlist
playlistaddlist: 0, // Add a list of videos to the playlist
oplaylistadd: -1, // Same as above, but for open (unlocked) playlist
oplaylistnext: 0,
oplaylistmove: 0,
oplaylistdelete: 0,
oplaylistjump: 0,
oplaylistaddlist: 0,
playlistaddcustom: 0, // Add custom embed to the playlist
playlistaddlive: 0, // Add a livestream to the playlist
exceedmaxlength: 0, // Add a video longer than the maximum length set
addnontemp: 0, // Add a permanent video to the playlist
settemp: 0, // Toggle temporary status of a playlist item
playlistshuffle: 0, // Shuffle the playlist
playlistclear: 0, // Clear the playlist
pollctl: 0, // Open/close polls
pollvote: -1, // Vote in polls
viewhiddenpoll: 1.5, // View results of hidden polls
voteskip: -1, // Vote to skip the current video
playlistlock: 2, // Lock/unlock the playlist
leaderctl: 0, // Give/take leader
drink: 0, // Use the /d command
chat: 0 // Send chat messages
};
2014-02-28 19:53:23 +00:00
for (var key in perms) {
this.permissions[key] = perms[key];
}
this.sendAll("setPermissions", this.permissions);
2014-02-28 19:53:23 +00:00
this.setLock(false);
}
2013-07-16 03:01:12 +00:00
module.exports = Channel;