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,9 +39,12 @@ 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;
if (mbt > 0) {
if (!this.justJoined[roomId]) this.justJoined[roomId] = {};
// When a new member logs in, store the time they joined. This will be useful // When a new member logs in, store the time they joined. This will be useful
// when we need to check if a message was sent within 20 minutes of joining // when we need to check if a message was sent within 20 minutes of joining
@ -49,23 +57,34 @@ export class WordList implements IProtection {
delete this.justJoined[roomId][event['sender']] delete this.justJoined[roomId][event['sender']]
} }
return; // stop processing (membership event spam is another problem) return;
}
} }
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;
// Check conditions first
if (mbt > 0) {
const joinTime = this.justJoined[roomId][event['sender']] const joinTime = this.justJoined[roomId][event['sender']]
if (joinTime) { // Disregard if the user isn't recently joined 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 return
} }
} else {
// The user isn't in the recently joined users list, no need to keep
// looking
return
}
}
// Perform the test // Perform the test
if (message && this.badWords.test(message)) { if (message && this.badWords.test(message)) {
await logMessage(LogLevel.WARN, "WordList", `Banning ${event['sender']} for word list violation in ${roomId}.`); await logMessage(LogLevel.WARN, "WordList", `Banning ${event['sender']} for word list violation in ${roomId}.`);
@ -85,4 +104,3 @@ export class WordList implements IProtection {
} }
} }
} }
}