mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-18 15:00:00 +00:00
Automod: add include_words/exclude_words and include_regex/exclude_regex to match_links trigger
This commit is contained in:
parent
e67816ec7b
commit
b30df3f8d4
3 changed files with 57 additions and 13 deletions
|
@ -458,14 +458,39 @@ export class AutomodPlugin extends ZeppelinPlugin<TConfigSchema, ICustomOverride
|
||||||
for (const link of links) {
|
for (const link of links) {
|
||||||
const normalizedHostname = link.hostname.toLowerCase();
|
const normalizedHostname = link.hostname.toLowerCase();
|
||||||
|
|
||||||
if (trigger.include_domains) {
|
// Exclude > Include
|
||||||
for (const domain of trigger.include_domains) {
|
// In order of specificity, regex > word > domain
|
||||||
const normalizedDomain = domain.toLowerCase();
|
|
||||||
if (normalizedDomain === normalizedHostname) {
|
if (trigger.exclude_regex) {
|
||||||
return domain;
|
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();
|
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;
|
return null;
|
||||||
|
|
|
@ -170,6 +170,10 @@ export const MatchLinksTrigger = t.type({
|
||||||
include_domains: tNullable(t.array(t.string)),
|
include_domains: tNullable(t.array(t.string)),
|
||||||
exclude_domains: tNullable(t.array(t.string)),
|
exclude_domains: tNullable(t.array(t.string)),
|
||||||
include_subdomains: t.boolean,
|
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_messages: t.boolean,
|
||||||
match_embeds: t.boolean,
|
match_embeds: t.boolean,
|
||||||
match_visible_names: t.boolean,
|
match_visible_names: t.boolean,
|
||||||
|
|
|
@ -390,18 +390,21 @@ export async function findRelevantAuditLogEntry(
|
||||||
const urlRegex = /(\S+\.\S+)/g;
|
const urlRegex = /(\S+\.\S+)/g;
|
||||||
const protocolRegex = /^[a-z]+:\/\//;
|
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) || [];
|
let matches = str.match(urlRegex) || [];
|
||||||
if (unique) matches = Array.from(new Set(matches));
|
if (unique) matches = Array.from(new Set(matches));
|
||||||
|
|
||||||
return matches.reduce((urls, match) => {
|
return matches.reduce((urls, match) => {
|
||||||
if (!protocolRegex.test(match)) {
|
const withProtocol = protocolRegex.test(match) ? match : `https://${match}`;
|
||||||
match = `https://${match}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
let matchUrl: url.URL;
|
let matchUrl: MatchedURL;
|
||||||
try {
|
try {
|
||||||
matchUrl = new url.URL(match);
|
matchUrl = new url.URL(withProtocol) as MatchedURL;
|
||||||
|
matchUrl.input = match;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return urls;
|
return urls;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue