Files
burrow-server/lib/chatfilter.js
T
SnipzilandClaude Opus 5.5 e05565787f feat(server): staff tools, player-made items, picture safety, chat, multiplayer games
Staff and moderation
- Invisible staff (!invisible, "xiv"): hidden from players, half see-through
  to other staff, cannot chat while hidden. New permission staff.invisible.
- Summon and goto land on the player's exact spot ("xgt" before the move).
- Custom warnings (!warn PLAYER custom TEXT), shown in the warning popup.
- Mod notes (!note, !notes; lib/modnotes.js), on the player page and the
  dashboard.
- Granular permissions: per-role overrides (setting rolePermissions) and
  per-account grants and denies, applied live to staff who are playing
  (refreshStaff re-sends "xsp").
- Item control (lib/itemcontrol.js): lock items out of the item finder
  (!lock, !unlock, !locked), take an item from a player (!take), delete a
  custom item from the game and every inventory.
- GET items/find: search any item by name or id, marked locked, for the
  dashboards' quick lock.

Player-made items (lib/playeritems.js, "xcm")
- The Item Maker: make, rename, set the worn look, delete. 5 a day, 20 in all;
  names go through the name filter.
- Admin switches: Item Maker on/off, new items wait for approval, and a
  separate switch (on by default) that holds every player-uploaded picture
  for a moderator. Review queue for items.approve (moderator and up).
- Pictures (lib/itemart.js): PNG, JPEG and GIF cleaned of metadata, BMP
  converted to PNG; 512px and 300KB at most once cleaned.
- Worn as the maker chooses: a sticker (the picture) or a texture (the
  item's shape painted with the picture).

Picture safety
- A picture waiting for approval is never served publicly: /itemart/ 404s it,
  and it is only shown through /account|/mod|/admin/api/itemart to its maker
  and to staff who review or make items.
- A picture turned away or taken off is remembered by SHA-256 and cannot be
  uploaded again.
- Three strikes: three pictures turned away stops a player's uploads until a
  moderator lets them again. Staff can stop or allow uploads by hand.
- Accounts under 24 hours old cannot upload pictures (AJ_ITEM_ART_MIN_HOURS);
  uploads capped at 10 a day.
- Optional Google Cloud Vision SafeSearch (lib/image-moderation.js) for item
  pictures and Art Studio paintings when AJ_GOOGLE_VISION_API_KEY is set;
  fails closed when the scanner is unreachable.

Chat
- Emoji: Twemoji pictures served at /emoji/<codepoints>.png and cached in
  data/emoji (lib/emoji.js).
- Smart spam filtering before the word filter (lib/chatspam.js): long runs of
  one character, repeated words and lines, huge words and emoji floods are
  tidied, and repeats within 20 seconds are dropped.
- Typing indicator: "xty" is passed on to everyone in the room who can see
  the player, at most every 2 seconds.

Games (lib/minigames.js)
- Room games can be played together: a MULTIPLAYER table gives a game a
  waiting list (Single Player / Multiplayer). Players in the same room are
  matched, taken into the game together, and each one's moves are relayed to
  the others. Gem Breaker is the first: two players, "pos", "shoot" and
  "playerLost" relayed.
- Gem Breaker's start message now lists each player's id. Without it no
  player counted as local, so even single player could not aim.

Other
- Players stay connected through the client's new 75-minute AFK limit
  (idleTimeoutMs 80 minutes).
- Jam-a-Grams carry custom items and colours; Jammer Wall avatar photos are
  validated (walls.js photoParam).
- Weak passwords refused (common words, or containing the username).
- Code-scanning fixes: every logged value goes through logText
  (lib/logtext.js) against log injection, HSTS, path-only error logs, a
  single file read in lib/names.js.

Tests: items (pictures, approval, strikes, re-upload refusal, the hidden
waiting picture, the dashboard search), staff (invisibility, goto and summon
spots, custom warnings, notes), buddygames (two-player Gem Breaker, the
typing relay), and updates for main's verified badge and spawn rooms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 21:44:10 -04:00

176 lines
7.1 KiB
JavaScript

"use strict";
const {logText} = require("./logtext.js");
const fs = require("fs");
const path = require("path");
// the server's own filter for what players write to each other: typed chat,
// Jam-a-Gram messages, Jammer Wall posts and bios. unlike the chat filter in
// the client (the player's to set, or turn off), this one cannot be turned off,
// and what it catches reaches nobody. it is meant to be narrow: slurs, and
// sexual talk aimed at someone, never swearing as such ("fuck" is fine,
// "i'll fuck you in the ass" is not). the rules are lib/chat-filter.txt,
// read again when it changes; the file says how they are written.
const RULES_FILE = path.join(__dirname, "chat-filter.txt");
// for "a .. b": how many words may come between the two ("a + b": none)
const MAX_GAP = 4;
// letters written as digits or signs, as the client's filter reads them
// (AJClient.filterText), plus "@" and "$".
const STAND_INS = {0: "o", 1: "i", 3: "e", 4: "a", 5: "s", 7: "t", "@": "a", $: "s"};
// A letter drawn out three times or more ("fuuuck") is read as the word
// with every run of letters made one, and compared that way: a word written
// plainly is compared as it is ("ass" is not "as").
const squash = word => word.replace(/(.)\1+/g, "$1");
const stretched = word => /(.)\1\1/.test(word);
// the words of a message as compared: small letters, apostrophes dropped
// ("i'll" is "ill"), stand-ins read as the letters they are, split at
// anything else. a word that is only digits is a number and goes. three or
// more single letters in a row ("f u c k", "f.u.c.k") are read as one word.
const wordsOf = text => {
const words = String(text).toLowerCase().replace(/['\u2019`]/g, "")
.split(/[^a-z0-9@$]+/).filter(word => word && !/^\d+$/.test(word))
.map(word => word.replace(/[0-9@$]/g, sign => STAND_INS[sign] || "")).filter(Boolean);
const joined = [];
for (let i = 0; i < words.length;) {
let end = i;
while (end < words.length && words[end].length === 1) {
end++;
}
if (end - i >= 3) {
joined.push(words.slice(i, end).join(""));
i = end;
}
else {
joined.push(words[i]);
i++;
}
}
return joined;
};
// one word of a rule: {text, start, inside, any}. "word" is that word,
// "word*" any word starting with it, "*word*" any word with it anywhere
// inside, "?" any word at all.
const patternOf = token => {
if (token === "?") {
return {any: true};
}
const inside = token.length > 2 && token.startsWith("*") && token.endsWith("*");
const start = !inside && token.endsWith("*");
const text = token.replace(/^\*|\*$/g, "").toLowerCase().replace(/[0-9@$]/g, sign => STAND_INS[sign] || "").replace(/[^a-z]/g, "");
return text ? {text, start, inside} : null;
};
const fits = (word, pattern) => {
if (pattern.any) {
return true;
}
const forms = stretched(word) ? [word, squash(word)] : [word];
const wanted = form => (form === word ? pattern.text : squash(pattern.text));
return forms.some(form => (pattern.inside ? form.includes(wanted(form))
: pattern.start ? form.startsWith(wanted(form)) : form === wanted(form)));
};
// A rule: clauses joined by " & ", all of which must be found in the
// message; a clause: slots in order, joined by " + " (the next word) or
// " .. " (at most MAX_GAP words later), each slot {choices, gap}, its gap
// being how far after the one before it may start; a slot's choices are
// joined by "|", and a choice is one word, or words that follow each other.
// a choice written "!word" is one the slot must not be: "?|!it" is any word
// but "it". A slot of only those takes up no word: "a + !up" is "a", not
// followed by "up" (or at the end of the message).
const parse = line => line.split(/\s+&\s+/).map(clause => {
const parts = clause.split(/\s+(\.\.|\+)\s+/);
const slots = [];
for (let i = 0; i < parts.length; i += 2) {
const written = parts[i].split("|").map(choice => choice.trim());
const wordsIn = list => list.map(choice => choice.split(/\s+/).map(patternOf)).filter(choice => choice.length > 0 && choice.every(Boolean));
const choices = wordsIn(written.filter(choice => !choice.startsWith("!")));
const not = wordsIn(written.filter(choice => choice.startsWith("!")).map(choice => choice.slice(1)));
slots.push({choices, not, gap: parts[i - 1] === "+" ? 0 : MAX_GAP});
}
return slots;
});
// where in `words` a slot's choice starts at `at`: the index after it, or -1.
const startsWith = (words, at, choice) => at + choice.length <= words.length && choice.every((pattern, i) => fits(words[at + i], pattern));
const slotAt = (words, at, slot) => {
if (slot.not.some(choice => startsWith(words, at, choice))) {
return -1;
}
for (const choice of slot.choices) {
if (startsWith(words, at, choice)) {
return at + choice.length;
}
}
return -1;
};
const clauseFrom = (words, slots, from, to) => {
if (slots.length === 0) {
return true;
}
if (slots[0].choices.length === 0) {
return !slots[0].not.some(choice => startsWith(words, from, choice)) && clauseFrom(words, slots.slice(1), from, from + (slots.length > 1 ? slots[1].gap : 0));
}
for (let at = from; at <= Math.min(to, words.length - 1); at++) {
const end = slotAt(words, at, slots[0]);
if (end !== -1 && clauseFrom(words, slots.slice(1), end, end + (slots.length > 1 ? slots[1].gap : 0))) {
return true;
}
}
return false;
};
let kept = {mtime: -1, rules: []};
const rules = () => {
let mtime = 0;
try {
mtime = fs.statSync(RULES_FILE).mtimeMs;
}
catch (err) {
return [];
}
if (kept.mtime !== mtime) {
const lines = fs.readFileSync(RULES_FILE, "utf8").split(/\r?\n/).map(line => line.trim()).filter(line => line && !line.startsWith("#"));
const parsed = [];
for (const line of lines) {
const clauses = parse(line);
if (clauses.every(clause => clause.length > 0 && clause.every(slot => slot.choices.length > 0 || slot.not.length > 0) && clause[0].choices.length > 0)) {
parsed.push({line, clauses});
}
else {
console.log(`[filter] lib/chat-filter.txt: a rule that could not be read was left out: ${logText(line)}`);
}
}
kept = {mtime, rules: parsed};
console.log(`[filter] ${logText(parsed.length)} rules`);
}
return kept.rules;
};
// the rule a message breaks (its line in the file), or null.
const broken = text => {
const words = wordsOf(text);
if (words.length === 0) {
return null;
}
const rule = rules().find(candidate => candidate.clauses.every(clause => clauseFrom(words, clause, 0, words.length - 1)));
return rule ? rule.line : null;
};
// the game's own warning screen, over the game, for ten seconds
// (FacilitatorXtCommManager, "fw" of facilitatorDisciplineResponse). its
// kind picks the picture and the words: 3 "Personal Information", 5
// "Bullying", 11 "Scamming", 0 a short "Inappropriate Chat", and this one
// "Inappropriate Behavior" with the game's own text ("It looks like the
// conversation you're having isn't appropriate..."). it also lets go of
// the sender's chat bubble that waits for the line.
const WARNING_BEHAVIOR = 4;
const warn = session => session.sendXt("fd", -1, "fw", WARNING_BEHAVIOR);
module.exports = {broken, warn, wordsOf};