');
}
popupReply(message) {
this.connection.popup(message);
}
add(data) {
if (this.pmTarget) {
data = this.pmTransform(data);
this.user.send(data);
if (this.pmTarget.send) this.pmTarget.send(data);
return;
}
this.room.add(data);
}
send(data) {
if (this.pmTarget) {
data = this.pmTransform(data);
this.user.send(data);
if (this.pmTarget.send) this.pmTarget.send(data);
return;
}
this.room.send(data);
}
sendModCommand(data) {
this.room.sendModCommand(data);
}
privateModCommand(data) {
this.room.sendModCommand(data);
this.logEntry(data);
this.room.modlog(data);
}
globalModlog(action, user, text) {
let buf = "(" + this.room.id + ") " + action + ": ";
if (typeof user === 'string') {
buf += "[" + toId(user) + "]";
} else {
let userid = user.getLastId();
buf += "[" + userid + "]";
if (user.autoconfirmed && user.autoconfirmed !== userid) buf += " ac:[" + user.autoconfirmed + "]";
}
buf += text;
Rooms.global.modlog(buf);
}
logEntry(data) {
if (this.pmTarget) return;
this.room.logEntry(data);
}
addModCommand(text, logOnlyText) {
this.add('|c|' + this.user.getIdentity(this.room) + '|/log ' + text);
this.room.modlog(text + (logOnlyText || ""));
}
logModCommand(text) {
this.room.modlog(text);
}
can(permission, target, room) {
if (!this.user.can(permission, target, room)) {
this.errorReply(this.cmdToken + this.namespaces.concat(this.cmd).join(" ") + " - Access denied.");
return false;
}
return true;
}
canBroadcast(suppressMessage) {
if (!this.broadcasting && this.cmdToken === BROADCAST_TOKEN) {
let message = this.canTalk(suppressMessage || this.message);
if (!message) return false;
if (!this.pmTarget && !this.user.can('broadcast', null, this.room)) {
this.errorReply("You need to be voiced to broadcast this command's information.");
this.errorReply("To see it for yourself, use: /" + this.message.substr(1));
return false;
}
// broadcast cooldown
let broadcastMessage = message.toLowerCase().replace(/[^a-z0-9\s!,]/g, '');
if (this.room.lastBroadcast === this.broadcastMessage &&
this.room.lastBroadcastTime >= Date.now() - BROADCAST_COOLDOWN) {
this.errorReply("You can't broadcast this because it was just broadcast.");
return false;
}
this.message = message;
this.broadcastMessage = broadcastMessage;
}
return true;
}
runBroadcast(suppressMessage) {
if (this.broadcasting || this.cmdToken !== BROADCAST_TOKEN) {
// Already being broadcast, or the user doesn't intend to broadcast.
return true;
}
if (!this.broadcastMessage) {
// Permission hasn't been checked yet. Do it now.
if (!this.canBroadcast(suppressMessage)) return false;
}
if (this.pmTarget) {
this.add('|c~|' + (suppressMessage || this.message));
} else {
this.add('|c|' + this.user.getIdentity(this.room.id) + '|' + (suppressMessage || this.message));
}
if (!this.pmTarget) {
this.room.lastBroadcast = this.broadcastMessage;
this.room.lastBroadcastTime = Date.now();
}
this.broadcasting = true;
return true;
}
parse(message, inNamespace, room) {
if (inNamespace && this.cmdToken) {
message = this.cmdToken + this.namespaces.concat(message.slice(1)).join(" ");
}
return CommandParser.parse(message, room || this.room, this.user, this.connection, this.relatedRoom, this.pmTarget, this.levelsDeep + 1);
}
run(targetCmd, inNamespace) {
if (targetCmd === 'constructor') return this.sendReply("Access denied.");
let commandHandler;
if (typeof targetCmd === 'function') {
commandHandler = targetCmd;
} else if (inNamespace) {
commandHandler = commands;
for (let i = 0; i < this.namespaces.length; i++) {
commandHandler = commandHandler[this.namespaces[i]];
}
commandHandler = commandHandler[targetCmd];
} else {
commandHandler = commands[targetCmd];
}
let result;
try {
result = commandHandler.call(this, this.target, this.room, this.user, this.connection, this.cmd, this.message);
} catch (err) {
if (require('./crashlogger')(err, 'A chat command', {
user: this.user.name,
room: this.room.id,
message: this.message,
}) === 'lockdown') {
let ministack = Tools.escapeHTML(err.stack).split("\n").slice(0, 2).join(" ");
if (Rooms.lobby) Rooms.lobby.send('|html|
POKEMON SHOWDOWN HAS CRASHED: ' + ministack + '
');
} else {
this.sendReply('|html|
Pokemon Showdown crashed! Don\'t worry, we\'re working on fixing it.
');
}
}
if (result === undefined) result = false;
return result;
}
canTalk(message, room, targetUser) {
if (room === undefined) room = this.room;
if (targetUser === undefined && this.pmTarget) {
room = undefined;
targetUser = this.pmTarget;
}
let user = this.user;
let connection = this.connection;
if (room && room.id === 'global') {
// should never happen
console.log(`Command tried to write to global: ${user.name}: ${message}`);
return false;
}
if (!user.named) {
connection.popup("You must choose a name before you can talk.");
return false;
}
if (!user.can('bypassall')) {
if (room && user.locked) {
this.errorReply("You are locked from talking in chat.");
return false;
}
if (room && room.isMuted(user)) {
this.errorReply("You are muted and cannot talk in this room.");
return false;
}
if ((!room || !room.battle) && (!targetUser || " +".includes(targetUser.group))) {
// in a chat room, or PMing non-staff
if (user.namelocked) {
this.errorReply("You are namelocked and cannot talk except in battles and to global staff.");
return false;
}
}
if (room && room.modchat) {
let userGroup = user.group;
if (!user.can('makeroom')) {
userGroup = room.getAuth(user);
}
if (room.modchat === 'autoconfirmed') {
if (!user.autoconfirmed && userGroup === ' ') {
this.errorReply("Because moderated chat is set, your account must be at least one week old and you must have won at least one ladder game to speak in this room.");
return false;
}
} else if (Config.groupsranking.indexOf(userGroup) < Config.groupsranking.indexOf(room.modchat)) {
let groupName = Config.groups[room.modchat].name || room.modchat;
this.errorReply("Because moderated chat is set, you must be of rank " + groupName + " or higher to speak in this room.");
return false;
}
}
if (room && !(user.userid in room.users)) {
connection.popup("You can't send a message to this room without being in it.");
return false;
}
}
if (typeof message === 'string') {
if (!message) {
connection.popup("Your message can't be blank.");
return false;
}
let length = message.length;
length += 10 * message.replace(/[^\ufdfd]*/g, '').length;
if (length > MAX_MESSAGE_LENGTH && !user.can('ignorelimits')) {
this.errorReply("Your message is too long: " + message);
return false;
}
// remove zalgo
message = message.replace(/[\u0300-\u036f\u0483-\u0489\u0610-\u0615\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06ED\u0E31\u0E34-\u0E3A\u0E47-\u0E4E]{3,}/g, '');
if (/[\u239b-\u23b9]/.test(message)) {
this.errorReply("Your message contains banned characters.");
return false;
}
if (this.checkFormat(room, message) && !user.can('mute', null, room)) {
this.errorReply("Your message was not sent because it contained " + this.checkFormat(room, message));
return false;
}
if (!this.checkSlowchat(room, user) && !user.can('mute', null, room)) {
this.errorReply("This room has slow-chat enabled. You can only talk once every " + room.slowchat + " seconds.");
return false;
}
if (!this.checkBanwords(room, message) && !user.can('mute', null, room)) {
this.errorReply("Your message contained banned words.");
return false;
}
if (room) {
let normalized = message.trim();
if (room.id === 'lobby' && (normalized === user.lastMessage) &&
((Date.now() - user.lastMessageTime) < MESSAGE_COOLDOWN)) {
this.errorReply("You can't send the same message again so soon.");
return false;
}
user.lastMessage = message;
user.lastMessageTime = Date.now();
}
if (Config.chatfilter) {
return Config.chatfilter.call(this, message, user, room, connection, targetUser);
}
return message;
}
return true;
}
canEmbedURI(uri, isRelative) {
if (uri.startsWith('https://')) return uri;
if (uri.startsWith('//')) return uri;
if (uri.startsWith('data:')) return uri;
if (!uri.startsWith('http://')) {
if (/^[a-z]+\:\/\//.test(uri) || isRelative) {
return this.errorReply("URIs must begin with 'https://' or 'http://' or 'data:'");
}
} else {
uri = uri.slice(7);
}
let slashIndex = uri.indexOf('/');
let domain = (slashIndex >= 0 ? uri.slice(0, slashIndex) : uri);
// heuristic that works for all the domains we care about
let secondLastDotIndex = domain.lastIndexOf('.', domain.length - 5);
if (secondLastDotIndex >= 0) domain = domain.slice(secondLastDotIndex + 1);
let approvedDomains = {
'imgur.com': 1,
'gyazo.com': 1,
'puu.sh': 1,
'rotmgtool.com': 1,
'pokemonshowdown.com': 1,
'nocookie.net': 1,
'blogspot.com': 1,
'imageshack.us': 1,
'deviantart.net': 1,
'd.pr': 1,
'pokefans.net': 1,
};
if (domain in approvedDomains) {
return '//' + uri;
}
if (domain === 'bit.ly') {
return this.errorReply("Please don't use URL shorteners.");
}
// unknown URI, allow HTTP to be safe
return 'http://' + uri;
}
canHTML(html) {
html = ('' + (html || '')).trim();
if (!html) return '';
let images = /]*/ig;
let match;
while ((match = images.exec(html))) {
if (this.room.isPersonal && !this.user.can('announce')) {
this.errorReply("Images are not allowed in personal rooms.");
return false;
}
if (!/width=([0-9]+|"[0-9]+")/i.test(match[0]) || !/height=([0-9]+|"[0-9]+")/i.test(match[0])) {
// Width and height are required because most browsers insert the
// element before width and height are known, and when the
// image is loaded, this changes the height of the chat area, which
// messes up autoscrolling.
this.errorReply('All images must have a width and height attribute');
return false;
}
let srcMatch = /src\s*\=\s*"?([^ "]+)(\s*")?/i.exec(match[0]);
if (srcMatch) {
let uri = this.canEmbedURI(srcMatch[1], true);
if (!uri) return false;
html = html.slice(0, match.index + srcMatch.index) + 'src="' + uri + '"' + html.slice(match.index + srcMatch.index + srcMatch[0].length);
// lastIndex is inaccurate since html was changed
images.lastIndex = match.index + 11;
}
}
if ((this.room.isPersonal || this.room.isPrivate === true) && !this.user.can('lock') && html.replace(/\s*style\s*=\s*\"?[^\"]*\"\s*>/g, '>').match(/