From 2c63509084e0ca883f709937f10107814871c497 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Fri, 20 Mar 2020 18:04:37 +0100 Subject: [PATCH] Added ability to white or blacklist attachment filetype in automod Intended to allow certain channels (i.e. bug reporting ones) to only allow .log files with uploads enabled, making it impossible to upload mp4s to troll or similar --- backend/src/plugins/Automod/Automod.ts | 57 ++++++++++++++++++++++++++ backend/src/plugins/Automod/types.ts | 15 +++++++ 2 files changed, 72 insertions(+) diff --git a/backend/src/plugins/Automod/Automod.ts b/backend/src/plugins/Automod/Automod.ts index 609cdb83..d6b91400 100644 --- a/backend/src/plugins/Automod/Automod.ts +++ b/backend/src/plugins/Automod/Automod.ts @@ -60,10 +60,12 @@ import { TMatchWordsTrigger, TMemberJoinTrigger, TRule, + TMatchAttachmentTypeTrigger, } from "./types"; import { pluginInfo } from "./info"; import { ERRORS, RecoverablePluginError } from "../../RecoverablePluginError"; import Timeout = NodeJS.Timeout; +import { StrictValidationError } from "src/validatorUtils"; const unactioned = (action: TextRecentAction | OtherRecentAction) => !action.actioned; @@ -116,6 +118,19 @@ const defaultMatchLinksTrigger: Partial = { match_custom_status: false, }; +const defaultMatchAttachmentTypeTrigger: Partial = { + filetype_blacklist: [], + blacklist_enabled: false, + filetype_whitelist: [], + whitelist_enabled: false, + match_messages: true, + match_embeds: true, + match_visible_names: false, + match_usernames: false, + match_nicknames: false, + match_custom_status: false, +}; + const defaultTextSpamTrigger: Partial> = { per_channel: true, }; @@ -130,6 +145,7 @@ const defaultTriggers = { match_regex: defaultMatchRegexTrigger, match_invites: defaultMatchInvitesTrigger, match_links: defaultMatchLinksTrigger, + match_attachment_type: defaultMatchAttachmentTypeTrigger, message_spam: defaultTextSpamTrigger, mention_spam: defaultTextSpamTrigger, link_spam: defaultTextSpamTrigger, @@ -249,6 +265,21 @@ export class AutomodPlugin extends ZeppelinPlugin`, + ]); + } else if (!white && !black) { + throw new StrictValidationError([ + `Must have either blacklist or whitelist enabled at rule <${rule.name}/match_attachment_type>`, + ]); + } + } } } @@ -457,6 +488,23 @@ export class AutomodPlugin extends ZeppelinPlugin { + return this.evaluateMatchAttachmentTypeTrigger(trigger.match_attachment_type, msg); + }); + if (match) return { ...match, trigger: "match_attachment_type" } as TextTriggerMatchResult; + } + if (trigger.message_spam) { const match = this.matchTextSpamTrigger(RecentActionType.Message, trigger.message_spam, msg); if (match) return { ...match, rule, trigger: "message_spam" }; @@ -1319,6 +1374,8 @@ export class AutomodPlugin extends ZeppelinPlugin; +export const MatchAttachmentTypeTrigger = t.type({ + filetype_blacklist: t.array(t.string), + blacklist_enabled: t.boolean, + filetype_whitelist: t.array(t.string), + whitelist_enabled: t.boolean, + match_messages: t.boolean, + match_embeds: t.boolean, + match_visible_names: t.boolean, + match_usernames: t.boolean, + match_nicknames: t.boolean, + match_custom_status: t.boolean, +}); +export type TMatchAttachmentTypeTrigger = t.TypeOf; + export const BaseSpamTrigger = t.type({ amount: t.number, within: t.string, @@ -280,6 +294,7 @@ export const Rule = t.type({ match_regex: tNullable(MatchRegexTrigger), match_invites: tNullable(MatchInvitesTrigger), match_links: tNullable(MatchLinksTrigger), + match_attachment_type: tNullable(MatchAttachmentTypeTrigger), message_spam: tNullable(MessageSpamTrigger), mention_spam: tNullable(MentionSpamTrigger), link_spam: tNullable(LinkSpamTrigger),