sync/lib/chatcommand.js

388 lines
12 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-11-17 19:12:56 +00:00
var handlers = {
2013-11-17 21:32:19 +00:00
/* commands that send chat messages */
2013-11-17 19:12:56 +00:00
"me": function (chan, user, msg, meta) {
meta.addClass = "action";
meta.action = true;
chan.sendMessage(user, msg, meta);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 19:12:56 +00:00
},
"sp": function (chan, user, msg, meta) {
meta.addClass = "spoiler";
chan.sendMessage(user, msg, meta);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 19:12:56 +00:00
},
"say": function (chan, user, msg, meta) {
if (user.rank >= 1.5) {
meta.addClass = "shout";
2013-11-19 22:14:33 +00:00
meta.addClassToNameAndTimestamp = true;
2013-11-17 19:12:56 +00:00
meta.forceShowName = true;
chan.sendMessage(user, msg, meta);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 19:12:56 +00:00
}
},
"a": function (chan, user, msg, meta) {
2013-11-19 19:19:51 +00:00
if (user.global_rank < 255) {
2014-01-12 23:06:25 +00:00
return false;
2013-11-19 19:19:51 +00:00
}
2014-01-26 06:01:36 +00:00
2013-11-17 19:12:56 +00:00
var superadminflair = {
2014-01-26 06:01:36 +00:00
labelclass: "label-danger",
icon: "glyphicon-globe"
2013-11-17 19:12:56 +00:00
};
var args = msg.split(" ");
var cargs = [];
for (var i = 0; i < args.length; i++) {
var a = args[i];
if (a.indexOf("!icon-") === 0) {
2014-01-26 06:01:36 +00:00
superadminflair.icon = "glyph" + a.substring(1);
2013-11-17 19:12:56 +00:00
} else if (a.indexOf("!label-") === 0) {
superadminflair.labelclass = a.substring(1);
} else {
cargs.push(a);
}
}
meta.superadminflair = superadminflair;
meta.forceShowName = true;
chan.sendMessage(user, cargs.join(" "), meta);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 19:12:56 +00:00
},
2013-11-17 21:32:19 +00:00
"poll": function (chan, user, msg, meta) {
handlePoll(chan, user, msg, false);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
"hpoll": function (chan, user, msg, meta) {
handlePoll(chan, user, msg, true);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
/* commands that do not send chat messages */
"afk": function (chan, user, msg, meta) {
user.setAFK(!user.meta.afk);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
2013-11-17 19:12:56 +00:00
"mute": function (chan, user, msg, meta) {
handleMute(chan, user, msg.split(" "));
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 19:12:56 +00:00
},
"smute": function (chan, user, msg, meta) {
handleShadowMute(chan, user, msg.split(" "));
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
"unmute": function (chan, user, msg, meta) {
handleUnmute(chan, user, msg.split(" "));
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
"kick": function (chan, user, msg, meta) {
handleKick(chan, user, msg.split(" "));
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
"ban": function (chan, user, msg, meta) {
handleBan(chan, user, msg.split(" "));
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
"ipban": function (chan, user, msg, meta) {
handleIPBan(chan, user, msg.split(" "));
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
"clear": function (chan, user, msg, meta) {
handleClear(chan, user);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
"clean": function (chan, user, msg, meta) {
handleClean(chan, user, msg);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
},
"cleantitle": function (chan, user, msg, meta) {
handleCleanTitle(chan, user, msg);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 19:12:56 +00:00
}
};
var handlerList = [];
for (var key in handlers) {
handlerList.push({
2013-11-18 17:11:00 +00:00
// match /command followed by a space or end of string
2013-11-17 19:12:56 +00:00
re: new RegExp("^\\/" + key + "(?:\\s|$)"),
fn: handlers[key]
});
}
function handle(chan, user, msg, meta) {
2013-11-18 17:11:00 +00:00
// Special case because the drink command can vary
2013-11-17 21:32:19 +00:00
var m = msg.match(/^\/d(-?[0-9]*)(?:\s|$)(.*)/);
if (m) {
handleDrink(chan, user, m[1], m[2], meta);
2014-01-12 23:06:25 +00:00
return true;
2013-11-17 21:32:19 +00:00
}
2013-11-17 19:12:56 +00:00
for (var i = 0; i < handlerList.length; i++) {
var h = handlerList[i];
if (msg.match(h.re)) {
2013-11-17 21:32:19 +00:00
var rest;
if (msg.indexOf(" ") >= 0) {
rest = msg.substring(msg.indexOf(" ") + 1);
} else {
rest = "";
}
2014-01-12 23:06:25 +00:00
return h.fn(chan, user, rest, meta);
2013-11-17 19:12:56 +00:00
}
}
}
2013-11-17 21:32:19 +00:00
function handleDrink(chan, user, count, msg, meta) {
if (!chan.hasPermission(user, "drink")) {
return;
}
if (count === "") {
count = 1;
}
count = parseInt(count);
if (isNaN(count)) {
return;
}
meta.drink = true;
meta.forceShowName = true;
meta.addClass = "drink";
chan.drinks += count;
2014-01-12 23:06:25 +00:00
chan.sendDrinks(chan.users);
2013-11-17 21:32:19 +00:00
if (count < 0 && msg.trim() === "") {
return;
}
msg = msg + " drink!";
if (count !== 1) {
msg += " (x" + count + ")";
}
chan.sendMessage(user, msg, meta);
}
2013-11-09 02:45:59 +00:00
function handleShadowMute(chan, user, args) {
2013-11-17 21:32:19 +00:00
if (chan.hasPermission(user, "mute") && args.length > 0) {
2013-11-09 02:45:59 +00:00
args[0] = args[0].toLowerCase();
var person = false;
2013-11-18 17:11:00 +00:00
for (var i = 0; i < chan.users.length; i++) {
if (chan.users[i].name.toLowerCase() === args[0]) {
2013-11-09 02:45:59 +00:00
person = chan.users[i];
break;
}
}
2013-11-17 21:32:19 +00:00
if (person) {
if (person.rank >= user.rank) {
2013-11-09 02:45:59 +00:00
user.socket.emit("errorMsg", {
msg: "You don't have permission to mute that person."
});
return;
}
2013-11-18 17:11:00 +00:00
2013-11-09 18:33:18 +00:00
/* Reset a previous regular mute */
if (chan.mutedUsers.contains(person.name.toLowerCase())) {
chan.mutedUsers.remove(person.name.toLowerCase());
}
person.meta.smuted = person.meta.muted = true;
chan.sendUserMeta(chan.users, person, 2);
2013-11-09 02:45:59 +00:00
chan.mutedUsers.add("[shadow]" + person.name.toLowerCase());
2014-02-08 18:45:07 +00:00
chan.logger.log("[mod] " + user.name + " shadow muted " + args[0]);
chan.sendModMessage(user.name + " shadow muted " + args[0], 2);
2013-11-09 02:45:59 +00:00
}
}
}
2013-06-25 14:18:33 +00:00
function handleMute(chan, user, args) {
2013-11-18 17:11:00 +00:00
if (chan.hasPermission(user, "mute") && args.length > 0) {
2013-06-25 14:18:33 +00:00
args[0] = args[0].toLowerCase();
var person = false;
2013-11-18 17:11:00 +00:00
for (var i = 0; i < chan.users.length; i++) {
if (chan.users[i].name.toLowerCase() == args[0]) {
2013-06-25 14:18:33 +00:00
person = chan.users[i];
break;
}
}
2013-11-18 17:11:00 +00:00
if (person) {
if (person.rank >= user.rank) {
user.socket.emit("errorMsg", {
msg: "You don't have permission to mute that person."
});
return;
}
person.meta.muted = true;
chan.sendUserMeta(chan.users, person);
2013-10-16 22:36:05 +00:00
chan.mutedUsers.add(person.name.toLowerCase());
2014-02-08 18:45:07 +00:00
chan.logger.log("[mod] " + user.name + " muted " + args[0]);
chan.sendModMessage(user.name + " muted " + args[0], 2);
2013-06-25 14:18:33 +00:00
}
}
}
function handleUnmute(chan, user, args) {
2013-11-18 17:11:00 +00:00
if (chan.hasPermission(user, "mute") && args.length > 0) {
2013-06-25 14:18:33 +00:00
args[0] = args[0].toLowerCase();
var person = false;
2013-11-18 17:11:00 +00:00
for (var i = 0; i < chan.users.length; i++) {
if (chan.users[i].name.toLowerCase() == args[0]) {
2013-06-25 14:18:33 +00:00
person = chan.users[i];
break;
}
}
2013-11-18 17:11:00 +00:00
if (person) {
if (person.rank >= user.rank) {
user.socket.emit("errorMsg", {
msg: "You don't have permission to unmute that person."
});
return;
}
person.meta.muted = false;
var wasSmuted = person.meta.smuted;
person.meta.smuted = false;
chan.sendUserMeta(chan.users, person, wasSmuted ? 2 : false);
2013-10-16 22:36:05 +00:00
chan.mutedUsers.remove(person.name.toLowerCase());
2013-11-09 02:45:59 +00:00
chan.mutedUsers.remove("[shadow]" + person.name.toLowerCase());
2014-02-08 18:45:07 +00:00
chan.logger.log("[mod] " + user.name + " unmuted " + args[0]);
chan.sendModMessage(user.name + " unmuted " + args[0], 2);
2013-06-25 14:18:33 +00:00
}
}
}
2013-03-05 20:23:02 +00:00
function handleKick(chan, user, args) {
2013-11-18 17:11:00 +00:00
if (chan.hasPermission(user, "kick") && args.length > 0) {
2013-05-19 21:23:35 +00:00
args[0] = args[0].toLowerCase();
2013-11-18 17:11:00 +00:00
if (args[0] == user.name.toLowerCase()) {
2013-08-19 00:31:34 +00:00
user.socket.emit("costanza", {
msg: "Kicking yourself?"
});
return;
}
2013-03-05 20:23:02 +00:00
var kickee;
2013-11-18 17:11:00 +00:00
for (var i = 0; i < chan.users.length; i++) {
if (chan.users[i].name.toLowerCase() == args[0]) {
if (chan.users[i].rank >= user.rank) {
2013-08-19 00:31:34 +00:00
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;
}
}
2013-11-18 17:11:00 +00:00
if (kickee) {
2014-02-08 18:45:07 +00:00
chan.logger.log("[mod] " + user.name + " kicked " + args[0]);
2013-04-14 17:38:00 +00:00
args[0] = "";
var reason = args.join(" ");
2014-01-09 02:12:02 +00:00
kickee.kick(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) {
2014-01-20 23:35:55 +00:00
var name = args.shift();
var range = args.shift();
var reason;
if (range !== "range") {
reason = range + " " + args.join(" ");
range = false;
} else {
reason = args.join(" ");
range = true;
}
chan.handleBanAllIP(user, name, reason, range);
2013-06-18 03:57:29 +00:00
// Ban the name too for good measure
2014-01-20 23:35:55 +00:00
chan.handleNameBan(user, name, reason);
2013-04-29 23:59:51 +00:00
}
2013-05-22 19:38:16 +00:00
function handleBan(chan, user, args) {
2014-01-20 23:35:55 +00:00
var name = args.shift();
var reason = args.join(" ");
chan.handleNameBan(user, name, reason);
2013-05-22 19:38:16 +00:00
}
2013-03-24 03:45:10 +00:00
function handleUnban(chan, user, args) {
2013-11-18 17:11:00 +00:00
if (chan.hasPermission(user, "ban") && args.length > 0) {
2014-02-08 18:45:07 +00:00
chan.logger.log("[mod] " + user.name + " unbanned " + args[0]);
2013-11-18 17:11:00 +00:00
if (args[0].match(/(\d+)\.(\d+)\.(\d+)\.(\d+)/)) {
2013-05-22 19:38:16 +00:00
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-11-18 17:11:00 +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;
2014-01-12 23:06:25 +00:00
chan.sendPoll(chan.users);
2014-02-08 18:45:07 +00:00
chan.logger.log("[poll] " + user.name + " Opened Poll: '" + poll.title + "'");
2013-03-16 21:49:58 +00:00
}
}
2013-05-23 04:03:37 +00:00
function handleClear(chan, user) {
2013-11-18 17:11:00 +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;