2024-10-25 17:35:12 +00:00
|
|
|
import { Filter } from "https://deno.land/x/grammy/mod.ts";
|
2024-10-24 08:35:13 +00:00
|
|
|
import { BotConfig } from "../../cfg/config.ts";
|
2024-10-19 10:00:35 +00:00
|
|
|
import { Ctx } from "../ctx.ts";
|
2024-10-24 08:35:13 +00:00
|
|
|
import { Kysely } from 'npm:kysely';
|
2024-10-27 17:49:21 +00:00
|
|
|
import { Database } from "../../repo/scheme.ts";
|
|
|
|
import { checkUserRestrictions } from "../../core/users.ts";
|
|
|
|
import type { CheckUserOnStartOut } from "./user_managment.ts";
|
2024-10-19 10:00:35 +00:00
|
|
|
|
|
|
|
const CAPTCHA_FAILS_LIMIT = 3
|
|
|
|
const TIMEOUT_AFTER_FAIL_MINS = 5
|
|
|
|
|
|
|
|
export interface CaptchaSessionData {
|
|
|
|
message_id: number,
|
|
|
|
tries_failed: number,
|
|
|
|
generated_at: number,
|
|
|
|
// Captcha can be changed from a simple matematical ones
|
|
|
|
// to a more complicated images or even science-related questions
|
|
|
|
solution: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
const randInt = (min: number, max: number): number =>
|
|
|
|
Math.floor(Math.random() * (max - min) + min)
|
|
|
|
|
2024-10-27 17:49:21 +00:00
|
|
|
// Returns captcha task (string) and the solution (number)
|
|
|
|
const genCaptcha = (): [string, number] => {
|
|
|
|
const number1 = randInt(-100, 100)
|
|
|
|
const number2 = randInt(-100, 100)
|
|
|
|
return [
|
|
|
|
`Captcha: ${number1}+${number2}=?`,
|
|
|
|
number1 + number2
|
|
|
|
]
|
2024-10-19 10:00:35 +00:00
|
|
|
}
|
2024-10-23 16:09:10 +00:00
|
|
|
|
2024-10-19 10:00:35 +00:00
|
|
|
// User captcha response sanitizer/parser/validator
|
|
|
|
const isSolutionCorrect = (expectSolution: string, solution: string): boolean => {
|
|
|
|
solution = solution.replace(' ','').replace('\t','') // Sanitizing
|
|
|
|
return parseInt(solution, 10) == parseInt(expectSolution, 10) ? true : false
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-23 16:09:10 +00:00
|
|
|
const captchaPassed = async (ctx: Ctx, db: Kysely<Database>, cfg: BotConfig) => {
|
|
|
|
if (ctx.chatId && ctx.session.captcha_data) {
|
2024-10-25 17:35:12 +00:00
|
|
|
await ctx.api.deleteMessage(
|
2024-10-23 16:09:10 +00:00
|
|
|
ctx.chatId, ctx.session.captcha_data.message_id
|
2024-10-25 17:35:12 +00:00
|
|
|
)
|
2024-10-23 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const linkValidUntil = new Date()
|
|
|
|
linkValidUntil.setHours(linkValidUntil.getHours() + 12)
|
|
|
|
|
|
|
|
const link = await ctx.api.createChatInviteLink(cfg.chat_id, {
|
|
|
|
member_limit: 1,
|
|
|
|
expire_date: Math.floor(linkValidUntil.getTime() / 1000),
|
|
|
|
})
|
|
|
|
|
|
|
|
await db.transaction().execute(async trx => {
|
2024-10-28 18:37:34 +00:00
|
|
|
await trx.updateTable('users').set({
|
|
|
|
is_captcha_passed: true
|
|
|
|
}).where('tg_id', '=', ctx.from!.id).execute()
|
2024-10-23 16:09:10 +00:00
|
|
|
|
|
|
|
await trx.insertInto('invite_links').values({
|
|
|
|
link: link.invite_link,
|
|
|
|
expect_user_tg_id: ctx.from!.id,
|
|
|
|
valid_until: linkValidUntil
|
|
|
|
}).execute()
|
|
|
|
})
|
|
|
|
|
2024-10-28 18:00:03 +00:00
|
|
|
ctx.session.captcha_data = undefined
|
2024-10-27 18:29:26 +00:00
|
|
|
await ctx.reply(ctx.t("captcha-passed",
|
|
|
|
{ invite_link: link.invite_link }))
|
2024-10-23 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-28 18:37:34 +00:00
|
|
|
const captchaFailed = async (ctx: Filter<Ctx, "message:text">, db: Kysely<Database>) => {
|
|
|
|
console.log("Captcha solution failed")
|
|
|
|
ctx.session.captcha_data!.tries_failed++
|
|
|
|
if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) {
|
|
|
|
const timeoutUntil = new Date()
|
|
|
|
timeoutUntil.setHours(timeoutUntil.getHours() + 12)
|
|
|
|
|
|
|
|
await db.updateTable('users').set({
|
|
|
|
timeout_until: timeoutUntil,
|
|
|
|
is_timeout: true,
|
|
|
|
}).where('tg_id', '=', ctx.from.id).execute()
|
|
|
|
|
|
|
|
await ctx.reply(ctx.t("captcha-already-in-chat",
|
|
|
|
{timeout_mins: TIMEOUT_AFTER_FAIL_MINS}))
|
|
|
|
await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id)
|
|
|
|
}
|
|
|
|
await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOnStartOut, cfg: BotConfig) => {
|
2024-10-23 16:09:10 +00:00
|
|
|
if (user.isChatParticipant) {
|
2024-10-27 18:29:26 +00:00
|
|
|
await ctx.reply(ctx.t("captcha-already-in-chat"))
|
2024-10-25 17:35:12 +00:00
|
|
|
} else if (user.activeInviteLink) {
|
|
|
|
await ctx.reply(user.activeInviteLink)
|
|
|
|
} else if (user.isCaptchaSolved) {
|
2024-10-23 16:09:10 +00:00
|
|
|
await captchaPassed(ctx, db, cfg)
|
2024-10-25 17:35:12 +00:00
|
|
|
} else {
|
2024-10-27 17:49:21 +00:00
|
|
|
const [captchaText, captchaSolution] = genCaptcha()
|
|
|
|
const msg = await ctx.reply(captchaText)
|
2024-10-25 17:35:12 +00:00
|
|
|
ctx.session.captcha_data = {
|
|
|
|
message_id: msg.message_id,
|
|
|
|
tries_failed: 0,
|
2024-10-27 17:49:21 +00:00
|
|
|
generated_at: new Date().getTime(),
|
|
|
|
solution: captchaSolution.toString(),
|
2024-10-25 17:35:12 +00:00
|
|
|
}
|
2024-10-19 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns true if captcha is response to a captcha; else -- returns false
|
2024-10-28 18:37:34 +00:00
|
|
|
export const checkCaptchaSolution = async (ctx: Filter<Ctx, "message:text">, db: Kysely<Database>, cfg: BotConfig) => {
|
2024-10-23 16:09:10 +00:00
|
|
|
if (!ctx.msg) return
|
|
|
|
const users = await db.selectFrom('users').selectAll().where('tg_id', '=', ctx.msg.from.id).execute()
|
|
|
|
if (users.length < 1) return // TODO: Maybe create new user here, idk
|
|
|
|
|
2024-10-25 17:35:12 +00:00
|
|
|
const user = users[0]
|
|
|
|
const userRestrictions = await checkUserRestrictions(db, user)
|
2024-10-23 16:09:10 +00:00
|
|
|
if (userRestrictions.isBlocked || userRestrictions.isTimeout) return
|
|
|
|
|
|
|
|
if (isSolutionCorrect(ctx.session.captcha_data!.solution, ctx.message.text)) {
|
|
|
|
await captchaPassed(ctx, db, cfg)
|
2024-10-19 10:00:35 +00:00
|
|
|
} else {
|
2024-10-28 18:37:34 +00:00
|
|
|
await captchaFailed(ctx, db)
|
2024-10-19 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|