Added configuration for wordlist protection

This commit is contained in:
Emi Simpson 2020-06-21 15:18:34 -04:00
parent bccb18225b
commit c2fbf0934a
No known key found for this signature in database
GPG Key ID: 68FAB2E2E6DFC98B
3 changed files with 82 additions and 34 deletions

View File

@ -107,6 +107,24 @@ commands:
additionalPrefixes: additionalPrefixes:
- "mjolnir_bot" - "mjolnir_bot"
# Configuration specific to certain toggleable protections
protections:
# Configuration for the wordlist plugin, which can ban users based if they say certain
# blocked words shortly after joining.
wordlist:
# A list of words which should be monitored by the bot. These will match if any part
# of the word is present in the message in any case. e.g. "poop" also matches
# "poOPYHEad". Additionally, regular expressions can be used.
words:
- "nigger"
- "faggot"
- "tranny"
- "retard"
# How long after a user joins the server should the bot monitor their messages. After
# this time, users can say words from the wordlist without being banned automatically.
# Set to zero to disable (users will always be banned if they say a bad word)
minutesBeforeTrusting: 20
# Options for monitoring the health of the bot # Options for monitoring the health of the bot
health: health:
# healthz options. These options are best for use in container environments # healthz options. These options are best for use in container environments

View File

@ -43,6 +43,12 @@ interface IConfig {
allowNoPrefix: boolean; allowNoPrefix: boolean;
additionalPrefixes: string[]; additionalPrefixes: string[];
}; };
protections: {
wordlist: {
words: string[];
minutesBeforeTrusting: number;
};
};
health: { health: {
healthz: { healthz: {
enabled: boolean; enabled: boolean;
@ -89,6 +95,12 @@ const defaultConfig: IConfig = {
allowNoPrefix: false, allowNoPrefix: false,
additionalPrefixes: [], additionalPrefixes: [],
}, },
protections: {
wordlist: {
words: ["nigger", "faggot", "tranny", "retard"],
minutesBeforeTrusting: 20
}
},
health: { health: {
healthz: { healthz: {
enabled: false, enabled: false,

View File

@ -24,9 +24,14 @@ import { isTrueJoinEvent } from "../utils";
export class WordList implements IProtection { export class WordList implements IProtection {
private justJoined: { [roomId: string]: { [username: string]: Date} } = {}; private justJoined: { [roomId: string]: { [username: string]: Date} } = {};
private badWords: RegExp = new RegExp(/.*(poopyhead).*/i) private badWords: RegExp;
constructor() { constructor() {
// Create a mega-regex from all the tiny baby regexs
this.badWords = new RegExp(
"(" + config.protections.wordlist.words.join(")|(")+ ")",
"i"
)
} }
public get name(): string { public get name(): string {
@ -34,53 +39,66 @@ export class WordList implements IProtection {
} }
public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise<any> { public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise<any> {
if (!this.justJoined[roomId]) this.justJoined[roomId] = {};
const content = event['content'] || {}; const content = event['content'] || {};
const mbt = config.protections.wordlist.minutesBeforeTrusting;
// When a new member logs in, store the time they joined. This will be useful if (mbt > 0) {
// when we need to check if a message was sent within 20 minutes of joining if (!this.justJoined[roomId]) this.justJoined[roomId] = {};
if (event['type'] === 'm.room.member') {
if (isTrueJoinEvent(event)) { // When a new member logs in, store the time they joined. This will be useful
const now = new Date(); // when we need to check if a message was sent within 20 minutes of joining
this.justJoined[roomId][event['state_key']] = now; if (event['type'] === 'm.room.member') {
LogService.info("WordList", `${event['state_key']} joined ${roomId} at ${now.toDateString()}`); if (isTrueJoinEvent(event)) {
} else if (content['membership'] == 'leave' || content['membership'] == 'ban') { const now = new Date();
delete this.justJoined[roomId][event['sender']] this.justJoined[roomId][event['state_key']] = now;
LogService.info("WordList", `${event['state_key']} joined ${roomId} at ${now.toDateString()}`);
} else if (content['membership'] == 'leave' || content['membership'] == 'ban') {
delete this.justJoined[roomId][event['sender']]
}
return;
} }
return; // stop processing (membership event spam is another problem)
} }
if (event['type'] === 'm.room.message') { if (event['type'] === 'm.room.message') {
const message = content['formatted_body'] || content['body'] || null; const message = content['formatted_body'] || content['body'] || null;
const joinTime = this.justJoined[roomId][event['sender']] // Check conditions first
if (joinTime) { // Disregard if the user isn't recently joined if (mbt > 0) {
const joinTime = this.justJoined[roomId][event['sender']]
if (joinTime) { // Disregard if the user isn't recently joined
// Check if they did join recently, was it within 20 minutes // Check if they did join recently, was it within the timeframe
const now = new Date(); const now = new Date();
if (now.valueOf() - joinTime.valueOf() > 20 * 60 * 1000) { if (now.valueOf() - joinTime.valueOf() > mbt * 60 * 1000) {
delete this.justJoined[roomId][event['sender']] // Remove the user delete this.justJoined[roomId][event['sender']] // Remove the user
LogService.info("WordList", `${event['sender']} is no longer considered suspect`); LogService.info("WordList", `${event['sender']} is no longer considered suspect`);
return
}
} else {
// The user isn't in the recently joined users list, no need to keep
// looking
return return
} }
}
// Perform the test
if (message && this.badWords.test(message)) {
await logMessage(LogLevel.WARN, "WordList", `Banning ${event['sender']} for word list violation in ${roomId}.`);
if (!config.noop) {
await mjolnir.client.banUser(event['sender'], roomId, "Word list violation");
} else {
await logMessage(LogLevel.WARN, "WordList", `Tried to ban ${event['sender']} in ${roomId} but Mjolnir is running in no-op mode`, roomId);
}
// Redact the event // Perform the test
if (!config.noop) { if (message && this.badWords.test(message)) {
await mjolnir.client.redactEvent(roomId, event['event_id'], "spam"); await logMessage(LogLevel.WARN, "WordList", `Banning ${event['sender']} for word list violation in ${roomId}.`);
} else { if (!config.noop) {
await logMessage(LogLevel.WARN, "WordList", `Tried to redact ${event['event_id']} in ${roomId} but Mjolnir is running in no-op mode`, roomId); await mjolnir.client.banUser(event['sender'], roomId, "Word list violation");
} } else {
await logMessage(LogLevel.WARN, "WordList", `Tried to ban ${event['sender']} in ${roomId} but Mjolnir is running in no-op mode`, roomId);
}
// Redact the event
if (!config.noop) {
await mjolnir.client.redactEvent(roomId, event['event_id'], "spam");
} else {
await logMessage(LogLevel.WARN, "WordList", `Tried to redact ${event['event_id']} in ${roomId} but Mjolnir is running in no-op mode`, roomId);
} }
} }
} }