From a6d347934b3d9b36239508d3d0b5fdbaa8989e04 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 26 Sep 2022 03:16:26 +0600 Subject: [PATCH] add CAS Support --- .../inmo/plagubot/plugins/captcha/Plugin.kt | 42 ++++++++++++++++--- .../plugins/captcha/cas/CASChecker.kt | 7 ++++ .../plugins/captcha/cas/KtorCASChecker.kt | 24 +++++++++++ .../plugins/captcha/settings/ChatSettings.kt | 3 +- 4 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/dev/inmo/plagubot/plugins/captcha/cas/CASChecker.kt create mode 100644 src/main/kotlin/dev/inmo/plagubot/plugins/captcha/cas/KtorCASChecker.kt diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt index e1ea6fa..7b0117a 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt @@ -4,6 +4,8 @@ import com.benasher44.uuid.uuid4 import dev.inmo.micro_utils.coroutines.* import dev.inmo.micro_utils.repos.create import dev.inmo.plagubot.Plugin +import dev.inmo.plagubot.plugins.captcha.cas.CASChecker +import dev.inmo.plagubot.plugins.captcha.cas.KtorCASChecker import dev.inmo.plagubot.plugins.captcha.db.CaptchaChatsSettingsRepo import dev.inmo.plagubot.plugins.captcha.provider.* import dev.inmo.plagubot.plugins.captcha.settings.ChatSettings @@ -22,6 +24,9 @@ import dev.inmo.tgbotapi.libraries.cache.admins.* import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.chat.* import dev.inmo.tgbotapi.types.commands.BotCommandScope +import dev.inmo.tgbotapi.utils.link +import dev.inmo.tgbotapi.utils.mention +import io.ktor.client.HttpClient import kotlinx.coroutines.* import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject @@ -29,6 +34,7 @@ import org.jetbrains.exposed.sql.Database import org.koin.core.Koin import org.koin.core.module.Module import org.koin.core.qualifier.named +import org.koin.dsl.binds private const val enableAutoDeleteCommands = "captcha_auto_delete_commands_on" private const val disableAutoDeleteCommands = "captcha_auto_delete_commands_off" @@ -121,11 +127,20 @@ class CaptchaBotPlugin : Plugin { BotCommand(disableKickOnUnsuccess, "Not solved captcha users will NOT be kicked from the chat") ) } + single { + KtorCASChecker( + HttpClient(), + get() + ) + } binds arrayOf( + CASChecker::class + ) } override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { val repo: CaptchaChatsSettingsRepo by koin.inject() val adminsAPI = koin.getOrNull() + val casChecker = koin.get() suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first() @@ -133,17 +148,17 @@ class CaptchaBotPlugin : Plugin { initialFilter = { it.chat is GroupChat } - ) { - val settings = it.chat.settings() + ) { msg -> + val settings = msg.chat.settings() if (!settings.enabled) return@onNewChatMembers safelyWithoutExceptions { if (settings.autoRemoveEvents) { - deleteMessage(it) + deleteMessage(msg) } } - val chat = it.chat.groupChatOrThrow() - val newUsers = it.chatEvent.members + val chat = msg.chat.groupChatOrThrow() + var newUsers = msg.chatEvent.members newUsers.forEach { user -> restrictChatMember( chat, @@ -151,10 +166,25 @@ class CaptchaBotPlugin : Plugin { permissions = RestrictionsChatPermissions ) } + newUsers = if (settings.casEnabled) { + newUsers.filterNot { user -> + casChecker.isBanned(user.id).also { it -> + if (it) { + reply( + msg + ) { + +"User " + mention(user) + " is banned in " + link("CAS System", "https://cas.chat/query?u=${user.id.chatId}") + } + } + } + } + } else { + newUsers + } val defaultChatPermissions = LeftRestrictionsChatPermissions with (settings.captchaProvider) { - doAction(it.date, chat, newUsers, defaultChatPermissions, adminsAPI, settings.kickOnUnsuccess) + doAction(msg.date, chat, newUsers, defaultChatPermissions, adminsAPI, settings.kickOnUnsuccess) } } diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/cas/CASChecker.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/cas/CASChecker.kt new file mode 100644 index 0000000..31a3ba5 --- /dev/null +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/cas/CASChecker.kt @@ -0,0 +1,7 @@ +package dev.inmo.plagubot.plugins.captcha.cas + +import dev.inmo.tgbotapi.types.UserId + +interface CASChecker { + suspend fun isBanned(userId: UserId): Boolean +} diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/cas/KtorCASChecker.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/cas/KtorCASChecker.kt new file mode 100644 index 0000000..43a2d4c --- /dev/null +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/cas/KtorCASChecker.kt @@ -0,0 +1,24 @@ +package dev.inmo.plagubot.plugins.captcha.cas + +import dev.inmo.tgbotapi.types.UserId +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.get +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json + +class KtorCASChecker( + private val httpClient: HttpClient, + private val json: Json +) : CASChecker { + @Serializable + private data class CheckResponse( + val ok: Boolean + ) + override suspend fun isBanned(userId: UserId): Boolean = httpClient.get( + "https://api.cas.chat/check?user_id=${userId.chatId}" + ).body().let { + json.decodeFromString(CheckResponse.serializer(), it) + }.ok + +} diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt index 94b5d62..b7dc035 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt @@ -12,5 +12,6 @@ data class ChatSettings( val autoRemoveCommands: Boolean = false, val autoRemoveEvents: Boolean = true, val kickOnUnsuccess: Boolean = true, - val enabled: Boolean = true + val enabled: Boolean = true, + val casEnabled: Boolean = false )