Compare commits
2 Commits
de5391b121
...
9883c47e1b
Author | SHA1 | Date |
---|---|---|
InsanusMokrassar | 9883c47e1b | |
InsanusMokrassar | a6d347934b |
|
@ -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"
|
||||
|
@ -44,6 +50,8 @@ private const val enableCaptcha = "enable_captcha"
|
|||
private val enableDisableKickOnUnsuccess = Regex("captcha_(enable|disable)_kick")
|
||||
private const val enableKickOnUnsuccess = "captcha_enable_kick"
|
||||
private const val disableKickOnUnsuccess = "captcha_disable_kick"
|
||||
private const val enableCAS = "captcha_enable_cas"
|
||||
private const val disableCAS = "captcha_disable_cas"
|
||||
|
||||
private val changeCaptchaMethodCommandRegex = Regex(
|
||||
"captcha_use_((slot_machine)|(simple)|(expression))"
|
||||
|
@ -121,11 +129,32 @@ class CaptchaBotPlugin : Plugin {
|
|||
BotCommand(disableKickOnUnsuccess, "Not solved captcha users will NOT be kicked from the chat")
|
||||
)
|
||||
}
|
||||
single(named(uuid4().toString())) {
|
||||
BotCommandFullInfo(
|
||||
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
|
||||
BotCommand(enableCAS, "Users banned in CAS will fail captcha automatically")
|
||||
)
|
||||
}
|
||||
single(named(uuid4().toString())) {
|
||||
BotCommandFullInfo(
|
||||
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
|
||||
BotCommand(disableCAS, "Users banned in CAS will NOT fail captcha automatically")
|
||||
)
|
||||
}
|
||||
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<AdminsCacheAPI>()
|
||||
val casChecker = koin.get<CASChecker>()
|
||||
|
||||
suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first()
|
||||
|
||||
|
@ -133,17 +162,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 +180,30 @@ class CaptchaBotPlugin : Plugin {
|
|||
permissions = RestrictionsChatPermissions
|
||||
)
|
||||
}
|
||||
newUsers = if (settings.casEnabled) {
|
||||
newUsers.filterNot { user ->
|
||||
casChecker.isBanned(user.id).also { isBanned ->
|
||||
runCatchingSafely {
|
||||
if (isBanned) {
|
||||
reply(
|
||||
msg
|
||||
) {
|
||||
+"User " + mention(user) + " is banned in " + link("CAS System", "https://cas.chat/query?u=${user.id.chatId}")
|
||||
}
|
||||
if (settings.kickOnUnsuccess) {
|
||||
banChatMember(msg.chat.id, user)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -333,6 +382,50 @@ class CaptchaBotPlugin : Plugin {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
onCommand(enableCAS) { message ->
|
||||
message.doAfterVerification(adminsAPI) {
|
||||
val settings = message.chat.settings()
|
||||
|
||||
repo.update(
|
||||
message.chat.id,
|
||||
settings.copy(casEnabled = true)
|
||||
)
|
||||
|
||||
reply(message, "Ok, CAS banned user will automatically fail captcha").apply {
|
||||
launchSafelyWithoutExceptions {
|
||||
delay(5000L)
|
||||
delete(this@apply)
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.autoRemoveCommands) {
|
||||
deleteMessage(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onCommand(disableCAS) { message ->
|
||||
message.doAfterVerification(adminsAPI) {
|
||||
val settings = message.chat.settings()
|
||||
|
||||
repo.update(
|
||||
message.chat.id,
|
||||
settings.copy(casEnabled = false)
|
||||
)
|
||||
|
||||
reply(message, "Ok, CAS banned user will NOT automatically fail captcha").apply {
|
||||
launchSafelyWithoutExceptions {
|
||||
delay(5000L)
|
||||
delete(this@apply)
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.autoRemoveCommands) {
|
||||
deleteMessage(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
|
||||
}
|
|
@ -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
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue