false-positives #83
@@ -2,12 +2,13 @@ import { Composer } from "grammy";
|
||||
import type { Context } from "#root/bot/context.js";
|
||||
import { logHandle } from "#root/bot/helpers/logging.js";
|
||||
import metaLinkCheck from "#root/lib/metaLinkCheck.js";
|
||||
import twitterLinkCheck from "#root/lib/twitterLinkCheck.js";
|
||||
import twitterLinkCheck, { twitterRegex } from "#root/lib/twitterLinkCheck.js";
|
||||
import { urql } from "#root/main.js";
|
||||
import increment from "#root/lib/graphql/mutations/incrementMutation.js";
|
||||
import addGroup from "#root/lib/graphql/mutations/addGroupMutation.js";
|
||||
import incrementGroup from "#root/lib/graphql/mutations/incrementGroupMutation.js";
|
||||
import tiktokLinkCheck from "#root/lib/tiktokLinkCheck.js";
|
||||
import fetchLink from "#root/lib/fetchLink.js";
|
||||
|
||||
const composer = new Composer<Context>();
|
||||
|
||||
@@ -20,8 +21,10 @@ const feature = composer.chatType(["group", "supergroup"]);
|
||||
feature.on("message::url", logHandle("embed-check"), async (ctx: Context) => {
|
||||
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
|
||||
|
||||
// Increments the trigger count
|
||||
await urql.mutation(increment, { trigger: true, mutationKey });
|
||||
|
||||
// Checks there is a chat and msg property in the context.
|
||||
if (ctx.chat && ctx.msg) {
|
||||
const groupName = ctx.chat?.title || "";
|
||||
const groupID = ctx.chat?.id.toString() || "";
|
||||
@@ -41,24 +44,25 @@ feature.on("message::url", logHandle("embed-check"), async (ctx: Context) => {
|
||||
|
||||
// If the all embeds array isn't empty filter through them to check for every type of disallowed link.
|
||||
if (allEmbeds.length) {
|
||||
const metaLinks = allEmbeds.filter(({ url }) => metaLinkCheck(url));
|
||||
const twitterLinks = allEmbeds.filter(({ url }) => twitterLinkCheck(url));
|
||||
const tiktokLinks = allEmbeds.filter(({ url }) => tiktokLinkCheck(url));
|
||||
detectedLinks +=
|
||||
allEmbeds.filter(({ url }) => metaLinkCheck(url)).length || 0;
|
||||
|
||||
// Handles Meta & Facebook Links
|
||||
if (metaLinks.length) {
|
||||
detectedLinks += metaLinks.length;
|
||||
}
|
||||
detectedLinks += (
|
||||
await Promise.all(
|
||||
allEmbeds.map(async ({ url }) => {
|
||||
// Checks for a false positive.
|
||||
if (twitterLinkCheck(url)) {
|
||||
return await fetchLink(url, twitterRegex);
|
||||
}
|
||||
return false;
|
||||
})
|
||||
)
|
||||
).reduce((acc, curr) => {
|
||||
return (acc += curr ? 1 : 0);
|
||||
}, 0);
|
||||
|
||||
// Handles Twitter/X links.
|
||||
if (twitterLinks.length) {
|
||||
detectedLinks += twitterLinks.length;
|
||||
}
|
||||
|
||||
// Handles TikTok links.
|
||||
if (tiktokLinks.length) {
|
||||
detectedLinks += tiktokLinks.length;
|
||||
}
|
||||
detectedLinks +=
|
||||
allEmbeds.filter(({ url }) => tiktokLinkCheck(url)).length || 0;
|
||||
}
|
||||
|
||||
if (detectedLinks) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { urql } from "#root/main.js";
|
||||
import increment from "#root/lib/graphql/mutations/incrementMutation.js";
|
||||
import addGroup from "#root/lib/graphql/mutations/addGroupMutation.js";
|
||||
import incrementGroup from "#root/lib/graphql/mutations/incrementGroupMutation.js";
|
||||
import fetchLink from "#root/lib/fetchLink.js";
|
||||
|
||||
const composer = new Composer<Context>();
|
||||
|
||||
@@ -21,8 +22,10 @@ feature.hears(
|
||||
async (ctx: Context) => {
|
||||
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
|
||||
|
||||
// Increments the trigger count
|
||||
await urql.mutation(increment, { trigger: true, mutationKey });
|
||||
|
||||
// Checks there is a chat and msg property in the context.
|
||||
if (ctx.chat && ctx.msg) {
|
||||
const username = ctx.msg.from?.username;
|
||||
|
||||
@@ -30,7 +33,11 @@ feature.hears(
|
||||
.mutation(increment, { link: 1, mutationKey })
|
||||
.toPromise()
|
||||
.then(async () => {
|
||||
if (ctx.msg && ctx.chat) {
|
||||
if (ctx.msg && ctx.chat && ctx.msg.text) {
|
||||
// Checks for a false positive
|
||||
const truePositive = await fetchLink(ctx.msg.text, twitterRegex);
|
||||
if (!truePositive) return;
|
||||
|
||||
// Replies to the user informing them of the action.
|
||||
await ctx.reply(
|
||||
`@${username} Twitter and X links along with reformatting services for Twitter posts are not allowed here\\. Please consider sharing the media directly or from other social media sources or websites\\. No administration action was taken against you other than the message being deleted\\.`,
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* This function will fetch a url to follow all redirects and compare the base url
|
||||
* with the RegEx to validate false and true positives.
|
||||
*
|
||||
* @param message The message that originally triggered the provided RegEx
|
||||
* @param banRegEx RegEx to check against
|
||||
* @returns @type boolean
|
||||
*/
|
||||
|
||||
const linkChecker = async (message: string, banRegEx: RegExp) => {
|
||||
const detectedLinks = message.split(" ").map(subStr => {
|
||||
if (banRegEx.test(subStr)) {
|
||||
return subStr;
|
||||
}
|
||||
});
|
||||
|
||||
if (!detectedLinks || detectedLinks.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const resolvedURLs = detectedLinks.map(async string => {
|
||||
if (string === undefined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const url =
|
||||
string.startsWith("http://") || string.startsWith("https://")
|
||||
? string
|
||||
: `https://${string}`;
|
||||
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
"user-agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0"
|
||||
}
|
||||
});
|
||||
|
||||
const resolvedURL = new URL(res.url);
|
||||
const baseURL = resolvedURL.hostname.split(".").slice(-2).join(".");
|
||||
|
||||
if (/^x\.com/i.test(baseURL)) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
return (await Promise.all(resolvedURLs)).includes(true);
|
||||
};
|
||||
|
||||
export default linkChecker;
|
||||
Reference in New Issue
Block a user