add CAS Support

This commit is contained in:
InsanusMokrassar 2022-09-26 03:16:26 +06:00
parent de5391b121
commit a6d347934b
4 changed files with 69 additions and 7 deletions

View File

@ -4,6 +4,8 @@ import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.coroutines.* import dev.inmo.micro_utils.coroutines.*
import dev.inmo.micro_utils.repos.create import dev.inmo.micro_utils.repos.create
import dev.inmo.plagubot.Plugin 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.db.CaptchaChatsSettingsRepo
import dev.inmo.plagubot.plugins.captcha.provider.* import dev.inmo.plagubot.plugins.captcha.provider.*
import dev.inmo.plagubot.plugins.captcha.settings.ChatSettings 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.BotCommand
import dev.inmo.tgbotapi.types.chat.* import dev.inmo.tgbotapi.types.chat.*
import dev.inmo.tgbotapi.types.commands.BotCommandScope 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.coroutines.*
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
@ -29,6 +34,7 @@ import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin import org.koin.core.Koin
import org.koin.core.module.Module import org.koin.core.module.Module
import org.koin.core.qualifier.named import org.koin.core.qualifier.named
import org.koin.dsl.binds
private const val enableAutoDeleteCommands = "captcha_auto_delete_commands_on" private const val enableAutoDeleteCommands = "captcha_auto_delete_commands_on"
private const val disableAutoDeleteCommands = "captcha_auto_delete_commands_off" 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") 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) { override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val repo: CaptchaChatsSettingsRepo by koin.inject() val repo: CaptchaChatsSettingsRepo by koin.inject()
val adminsAPI = koin.getOrNull<AdminsCacheAPI>() val adminsAPI = koin.getOrNull<AdminsCacheAPI>()
val casChecker = koin.get<CASChecker>()
suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first() suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first()
@ -133,17 +148,17 @@ class CaptchaBotPlugin : Plugin {
initialFilter = { initialFilter = {
it.chat is GroupChat it.chat is GroupChat
} }
) { ) { msg ->
val settings = it.chat.settings() val settings = msg.chat.settings()
if (!settings.enabled) return@onNewChatMembers if (!settings.enabled) return@onNewChatMembers
safelyWithoutExceptions { safelyWithoutExceptions {
if (settings.autoRemoveEvents) { if (settings.autoRemoveEvents) {
deleteMessage(it) deleteMessage(msg)
} }
} }
val chat = it.chat.groupChatOrThrow() val chat = msg.chat.groupChatOrThrow()
val newUsers = it.chatEvent.members var newUsers = msg.chatEvent.members
newUsers.forEach { user -> newUsers.forEach { user ->
restrictChatMember( restrictChatMember(
chat, chat,
@ -151,10 +166,25 @@ class CaptchaBotPlugin : Plugin {
permissions = RestrictionsChatPermissions 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 val defaultChatPermissions = LeftRestrictionsChatPermissions
with (settings.captchaProvider) { with (settings.captchaProvider) {
doAction(it.date, chat, newUsers, defaultChatPermissions, adminsAPI, settings.kickOnUnsuccess) doAction(msg.date, chat, newUsers, defaultChatPermissions, adminsAPI, settings.kickOnUnsuccess)
} }
} }

View File

@ -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
}

View File

@ -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<String>().let {
json.decodeFromString(CheckResponse.serializer(), it)
}.ok
}

View File

@ -12,5 +12,6 @@ data class ChatSettings(
val autoRemoveCommands: Boolean = false, val autoRemoveCommands: Boolean = false,
val autoRemoveEvents: Boolean = true, val autoRemoveEvents: Boolean = true,
val kickOnUnsuccess: Boolean = true, val kickOnUnsuccess: Boolean = true,
val enabled: Boolean = true val enabled: Boolean = true,
val casEnabled: Boolean = false
) )