sync/lib/chatcommand.js

287 lines
9.2 KiB
JavaScript
Raw Normal View History

2013-03-24 02:28:20 +00:00
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
2013-03-24 02:28:20 +00:00
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2013-03-24 02:28:20 +00:00
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2013-03-24 02:28:20 +00:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2013-03-27 19:28:51 +00:00
var Logger = require("./logger.js");
2013-10-13 00:04:42 +00:00
var Poll = require("./poll").Poll;
2013-03-05 20:23:02 +00:00
2013-04-28 18:04:15 +00:00
function handle(chan, user, msg, data) {
2013-03-05 18:51:58 +00:00
if(msg.indexOf("/me ") == 0)
2013-04-28 18:04:15 +00:00
chan.sendMessage(user.name, msg.substring(4), "action", data);
2013-03-05 18:51:58 +00:00
else if(msg.indexOf("/sp ") == 0)
2013-04-28 18:04:15 +00:00
chan.sendMessage(user.name, msg.substring(4), "spoiler", data);
2013-03-20 18:35:06 +00:00
else if(msg.indexOf("/say ") == 0) {
2013-10-11 21:31:40 +00:00
if(user.rank >= 1.5) {
2013-04-28 18:04:15 +00:00
chan.sendMessage(user.name, msg.substring(5), "shout", data);
2013-03-20 18:35:06 +00:00
}
}
else if(msg.indexOf("/afk") == 0) {
user.setAFK(!user.meta.afk);
}
else if(msg.indexOf("/m ") == 0) {
2013-10-11 21:31:40 +00:00
if(user.rank >= 2) {
2013-04-28 18:04:15 +00:00
chan.chainMessage(user, msg.substring(3), {modflair: user.rank})
}
}
else if(msg.indexOf("/a ") == 0) {
2013-10-11 21:31:40 +00:00
if(user.rank >= 255) {
var flair = {
superadminflair: {
labelclass: "label-important",
icon: "icon-globe"
}
};
var args = msg.substring(3).split(" ");
var cargs = [];
for(var i = 0; i < args.length; i++) {
var a = args[i];
if(a.indexOf("!icon-") == 0)
flair.superadminflair.icon = a.substring(1);
else if(a.indexOf("!label-") == 0)
flair.superadminflair.labelclass = a.substring(1);
else {
cargs.push(a);
}
}
chan.chainMessage(user, cargs.join(" "), flair);
}
}
2013-06-25 14:18:33 +00:00
else if(msg.indexOf("/mute ") == 0) {
handleMute(chan, user, msg.substring(6).split(" "));
}
else if(msg.indexOf("/unmute ") == 0) {
handleUnmute(chan, user, msg.substring(8).split(" "));
}
2013-03-05 20:23:02 +00:00
else if(msg.indexOf("/kick ") == 0) {
handleKick(chan, user, msg.substring(6).split(" "));
2013-03-05 20:23:02 +00:00
}
2013-03-24 01:08:35 +00:00
else if(msg.indexOf("/ban ") == 0) {
handleBan(chan, user, msg.substring(5).split(" "));
2013-03-24 01:08:35 +00:00
}
2013-05-22 19:38:16 +00:00
else if(msg.indexOf("/ipban ") == 0) {
2013-06-18 03:57:29 +00:00
handleIPBan(chan, user, msg.substring(7).split(" "));
2013-05-22 19:38:16 +00:00
}
2013-03-24 03:45:10 +00:00
else if(msg.indexOf("/unban ") == 0) {
handleUnban(chan, user, msg.substring(7).split(" "));
2013-03-24 03:45:10 +00:00
}
2013-03-16 21:49:58 +00:00
else if(msg.indexOf("/poll ") == 0) {
2013-09-12 01:22:00 +00:00
handlePoll(chan, user, msg.substring(6), false);
}
else if(msg.indexOf("/hpoll ") == 0) {
handlePoll(chan, user, msg.substring(6), true);
2013-03-16 21:49:58 +00:00
}
2013-04-04 19:56:43 +00:00
else if(msg.indexOf("/d") == 0 && msg.length > 2 &&
msg[2].match(/[-0-9 ]/)) {
2013-06-25 03:40:42 +00:00
if(msg[2] == "-") {
if(msg.length == 3)
return;
if(!msg[3].match(/[0-9]/))
return;
}
2013-04-28 18:04:15 +00:00
handleDrink(chan, user, msg.substring(2), data);
2013-04-04 19:56:43 +00:00
}
2013-05-23 04:03:37 +00:00
else if(msg.indexOf("/clear") == 0) {
handleClear(chan, user);
}
else if(msg.indexOf("/clean ") == 0) {
handleClean(chan, user, msg.substring(7));
}
else if(msg.indexOf("/cleantitle ") == 0) {
handleCleanTitle(chan, user, msg.substring(12));
}
2013-03-05 20:23:02 +00:00
}
2013-06-25 14:18:33 +00:00
function handleMute(chan, user, args) {
if(chan.hasPermission(user, "mute") && args.length > 0) {
args[0] = args[0].toLowerCase();
var person = false;
for(var i = 0; i < chan.users.length; i++) {
if(chan.users[i].name.toLowerCase() == args[0]) {
person = chan.users[i];
break;
}
}
if(person) {
if(person.rank >= user.rank) {
user.socket.emit("errorMsg", {
msg: "You don't have permission to mute that person."
});
return;
}
2013-06-25 14:18:33 +00:00
person.meta.icon = "icon-volume-off";
2013-10-16 22:36:05 +00:00
chan.mutedUsers.add(person.name.toLowerCase());
2013-06-25 14:18:33 +00:00
chan.broadcastUserUpdate(person);
chan.logger.log("*** " + user.name + " muted " + args[0]);
}
}
}
function handleUnmute(chan, user, args) {
if(chan.hasPermission(user, "mute") && args.length > 0) {
args[0] = args[0].toLowerCase();
var person = false;
for(var i = 0; i < chan.users.length; i++) {
if(chan.users[i].name.toLowerCase() == args[0]) {
person = chan.users[i];
break;
}
}
if(person) {
if(person.rank >= user.rank) {
user.socket.emit("errorMsg", {
msg: "You don't have permission to unmute that person."
});
return;
}
2013-06-25 14:18:33 +00:00
person.meta.icon = false;
2013-10-16 22:36:05 +00:00
chan.mutedUsers.remove(person.name.toLowerCase());
2013-06-25 14:18:33 +00:00
chan.broadcastUserUpdate(person);
chan.logger.log("*** " + user.name + " unmuted " + args[0]);
}
}
}
2013-03-05 20:23:02 +00:00
function handleKick(chan, user, args) {
2013-05-22 19:38:16 +00:00
if(chan.hasPermission(user, "kick") && args.length > 0) {
2013-05-19 21:23:35 +00:00
args[0] = args[0].toLowerCase();
2013-08-19 00:31:34 +00:00
if(args[0] == user.name.toLowerCase()) {
user.socket.emit("costanza", {
msg: "Kicking yourself?"
});
return;
}
2013-03-05 20:23:02 +00:00
var kickee;
for(var i = 0; i < chan.users.length; i++) {
2013-08-19 00:31:34 +00:00
if(chan.users[i].name.toLowerCase() == args[0]) {
if(chan.users[i].rank >= user.rank) {
user.socket.emit("errorMsg", {
msg: "You don't have permission to kick " + args[0]
});
return;
}
2013-03-05 20:23:02 +00:00
kickee = chan.users[i];
break;
}
}
if(kickee) {
2013-03-27 19:28:51 +00:00
chan.logger.log("*** " + user.name + " kicked " + args[0]);
2013-04-14 17:38:00 +00:00
args[0] = "";
var reason = args.join(" ");
chan.kick(kickee, reason);
2013-03-05 20:23:02 +00:00
}
}
2013-03-05 18:51:58 +00:00
}
2013-05-22 19:38:16 +00:00
function handleIPBan(chan, user, args) {
2013-06-18 03:57:29 +00:00
chan.tryIPBan(user, args[0], args[1]);
// Ban the name too for good measure
chan.tryNameBan(user, args[0]);
2013-04-29 23:59:51 +00:00
}
2013-05-22 19:38:16 +00:00
function handleBan(chan, user, args) {
2013-06-18 03:57:29 +00:00
chan.tryNameBan(user, args[0]);
2013-05-22 19:38:16 +00:00
}
2013-03-24 03:45:10 +00:00
function handleUnban(chan, user, args) {
2013-05-22 19:38:16 +00:00
if(chan.hasPermission(user, "ban") && args.length > 0) {
2013-03-27 19:28:51 +00:00
chan.logger.log("*** " + user.name + " unbanned " + args[0]);
2013-05-22 19:38:16 +00:00
if(args[0].match(/(\d+)\.(\d+)\.(\d+)\.(\d+)/)) {
chan.unbanIP(user, args[0]);
}
else {
chan.unbanName(user, args[0]);
}
2013-03-24 03:45:10 +00:00
}
}
2013-09-12 01:22:00 +00:00
function handlePoll(chan, user, msg, hidden) {
2013-05-24 01:42:12 +00:00
if(chan.hasPermission(user, "pollctl")) {
var args = msg.split(",");
2013-03-16 21:49:58 +00:00
var title = args[0];
args.splice(0, 1);
2013-09-12 01:22:00 +00:00
var poll = new Poll(user.name, title, args, hidden === true);
2013-03-16 21:49:58 +00:00
chan.poll = poll;
chan.broadcastPoll();
2013-03-27 19:28:51 +00:00
chan.logger.log("*** " + user.name + " Opened Poll: '" + poll.title + "'");
2013-03-16 21:49:58 +00:00
}
}
2013-04-28 18:04:15 +00:00
function handleDrink(chan, user, msg, data) {
2013-05-22 19:38:16 +00:00
if(!chan.hasPermission(user, "drink")) {
2013-04-04 19:56:43 +00:00
return;
}
var count = msg.split(" ")[0];
msg = msg.substring(count.length + 1);
if(count == "")
count = 1;
else
count = parseInt(count);
chan.drinks += count;
chan.broadcastDrinks();
2013-04-14 17:38:00 +00:00
if(count < 0 && msg.trim() == "") {
2013-04-04 19:56:43 +00:00
return;
}
2013-04-04 19:56:43 +00:00
msg = msg + " drink!";
2013-04-14 17:38:00 +00:00
if(count != 1)
2013-04-04 19:56:43 +00:00
msg += " (x" + count + ")";
2013-04-28 18:04:15 +00:00
chan.sendMessage(user.name, msg, "drink", data);
2013-04-04 19:56:43 +00:00
}
2013-05-23 04:03:37 +00:00
function handleClear(chan, user) {
2013-10-11 21:31:40 +00:00
if(user.rank < 2) {
2013-05-23 04:03:37 +00:00
return;
}
chan.chatbuffer = [];
chan.sendAll("clearchat");
}
/*
/clean and /cleantitle contributed by http://github.com/unbibium.
Modifications by Calvin Montgomery
*/
function generateTargetRegex(target) {
const flagsre = /^(-[img]+\s+)/i
var m = target.match(flagsre);
var flags = "";
if (m) {
flags = m[0].slice(1,-1);
target = target.replace(flagsre, "");
}
return new RegExp(target, flags);
}
function handleClean(chan, user, target) {
if (!chan.hasPermission(user, "playlistdelete"))
return;
target = generateTargetRegex(target);
chan.playlist.clean(function (item) {
return target.test(item.queueby);
});
}
function handleCleanTitle(chan, user, target) {
if (!chan.hasPermission(user, "playlistdelete"))
return;
target = generateTargetRegex(target);
chan.playlist.clean(function (item) {
return target.exec(item.media.title) !== null;
});
}
2013-03-05 18:51:58 +00:00
exports.handle = handle;