3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Automod: add include_words/exclude_words and include_regex/exclude_regex to match_links trigger

This commit is contained in:
Dragory 2020-04-11 16:56:55 +03:00
parent e67816ec7b
commit b30df3f8d4
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
3 changed files with 57 additions and 13 deletions

View file

@ -458,14 +458,39 @@ export class AutomodPlugin extends ZeppelinPlugin<TConfigSchema, ICustomOverride
for (const link of links) {
const normalizedHostname = link.hostname.toLowerCase();
if (trigger.include_domains) {
for (const domain of trigger.include_domains) {
const normalizedDomain = domain.toLowerCase();
if (normalizedDomain === normalizedHostname) {
return domain;
// Exclude > Include
// In order of specificity, regex > word > domain
if (trigger.exclude_regex) {
for (const pattern of trigger.exclude_regex) {
if (pattern.test(link.input)) {
return null;
}
if (trigger.include_subdomains && normalizedHostname.endsWith(`.${domain}`)) {
return domain;
}
}
if (trigger.include_regex) {
for (const pattern of trigger.include_regex) {
if (pattern.test(link.input)) {
return link.input;
}
}
}
if (trigger.exclude_words) {
for (const word of trigger.exclude_words) {
const regex = new RegExp(escapeStringRegexp(word), "i");
if (regex.test(link.input)) {
return null;
}
}
}
if (trigger.include_words) {
for (const word of trigger.include_words) {
const regex = new RegExp(escapeStringRegexp(word), "i");
if (regex.test(link.input)) {
return link.input;
}
}
}
@ -483,6 +508,18 @@ export class AutomodPlugin extends ZeppelinPlugin<TConfigSchema, ICustomOverride
return link.toString();
}
if (trigger.include_domains) {
for (const domain of trigger.include_domains) {
const normalizedDomain = domain.toLowerCase();
if (normalizedDomain === normalizedHostname) {
return domain;
}
if (trigger.include_subdomains && normalizedHostname.endsWith(`.${domain}`)) {
return domain;
}
}
}
}
return null;

View file

@ -170,6 +170,10 @@ export const MatchLinksTrigger = t.type({
include_domains: tNullable(t.array(t.string)),
exclude_domains: tNullable(t.array(t.string)),
include_subdomains: t.boolean,
include_words: tNullable(t.array(t.string)),
exclude_words: tNullable(t.array(t.string)),
include_regex: tNullable(t.array(TSafeRegex)),
exclude_regex: tNullable(t.array(TSafeRegex)),
match_messages: t.boolean,
match_embeds: t.boolean,
match_visible_names: t.boolean,

View file

@ -390,18 +390,21 @@ export async function findRelevantAuditLogEntry(
const urlRegex = /(\S+\.\S+)/g;
const protocolRegex = /^[a-z]+:\/\//;
export function getUrlsInString(str: string, unique = false): url.URL[] {
interface MatchedURL extends url.URL {
input: string;
}
export function getUrlsInString(str: string, unique = false): MatchedURL[] {
let matches = str.match(urlRegex) || [];
if (unique) matches = Array.from(new Set(matches));
return matches.reduce((urls, match) => {
if (!protocolRegex.test(match)) {
match = `https://${match}`;
}
const withProtocol = protocolRegex.test(match) ? match : `https://${match}`;
let matchUrl: url.URL;
let matchUrl: MatchedURL;
try {
matchUrl = new url.URL(match);
matchUrl = new url.URL(withProtocol) as MatchedURL;
matchUrl.input = match;
} catch (e) {
return urls;
}