This commit is contained in:
InsanusMokrassar 2022-09-21 18:04:14 +06:00
parent 7dead36cd9
commit de6a8241c3
2 changed files with 332 additions and 315 deletions

View File

@ -1,33 +1,30 @@
package dev.inmo.plagubot.plugins.captcha package dev.inmo.plagubot.plugins.captcha
import dev.inmo.plagubot.plugins.captcha.provider.cancelData
import dev.inmo.tgbotapi.extensions.utils.SlotMachineReelImage import dev.inmo.tgbotapi.extensions.utils.SlotMachineReelImage
import dev.inmo.tgbotapi.extensions.utils.types.buttons.* import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
infix fun String.startingOf(target: String) = target.startsWith(this) infix fun String.startingOf(target: String) = target.startsWith(this)
private val buttonsPreset: List<List<InlineKeyboardButton>> = SlotMachineReelImage.values().toList().chunked(2).map {
it.map {
CallbackDataInlineKeyboardButton(it.text, it.text)
}
}
fun slotMachineReplyMarkup( fun slotMachineReplyMarkup(
first: String? = null, adminCancelButton: Boolean = false
second: String? = null,
third: String? = null,
): InlineKeyboardMarkup { ): InlineKeyboardMarkup {
val texts = when {
first == null -> SlotMachineReelImage.values().map {
CallbackDataInlineKeyboardButton("${it.text}**", it.text)
}
second == null -> SlotMachineReelImage.values().map {
CallbackDataInlineKeyboardButton("$first${it.text}*", it.text)
}
third == null -> SlotMachineReelImage.values().map {
CallbackDataInlineKeyboardButton("$first$second${it.text}", it.text)
}
else -> listOf(CallbackDataInlineKeyboardButton("$first$second$third", "$first$second$third"))
}
return inlineKeyboard { return inlineKeyboard {
texts.chunked(2).forEach { add(it) } buttonsPreset.forEach(::add)
// row { if (adminCancelButton) {
// dataButton("Cancel (Admins only)", "cancel") row {
// } dataButton("Cancel (Admins only)", cancelData)
}
}
} }
} }

View File

@ -1,12 +1,15 @@
package dev.inmo.plagubot.plugins.captcha.provider package dev.inmo.plagubot.plugins.captcha.provider
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import com.soywiz.klock.DateTime import com.soywiz.klock.*
import com.soywiz.klock.seconds import dev.inmo.kslog.common.e
import dev.inmo.kslog.common.logger
import dev.inmo.micro_utils.coroutines.* import dev.inmo.micro_utils.coroutines.*
import dev.inmo.plagubot.plugins.captcha.slotMachineReplyMarkup import dev.inmo.plagubot.plugins.captcha.slotMachineReplyMarkup
import dev.inmo.tgbotapi.extensions.api.answers.answer
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
import dev.inmo.tgbotapi.extensions.api.chat.members.* import dev.inmo.tgbotapi.extensions.api.chat.members.*
import dev.inmo.tgbotapi.extensions.api.delete
import dev.inmo.tgbotapi.extensions.api.deleteMessage import dev.inmo.tgbotapi.extensions.api.deleteMessage
import dev.inmo.tgbotapi.extensions.api.edit.edit import dev.inmo.tgbotapi.extensions.api.edit.edit
import dev.inmo.tgbotapi.extensions.api.edit.reply_markup.editMessageReplyMarkup import dev.inmo.tgbotapi.extensions.api.edit.reply_markup.editMessageReplyMarkup
@ -15,7 +18,9 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery
import dev.inmo.tgbotapi.extensions.utils.asSlotMachineReelImage import dev.inmo.tgbotapi.extensions.utils.asSlotMachineReelImage
import dev.inmo.tgbotapi.extensions.utils.calculateSlotMachineResult import dev.inmo.tgbotapi.extensions.utils.calculateSlotMachineResult
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
import dev.inmo.tgbotapi.extensions.utils.shortcuts.sentMessages
import dev.inmo.tgbotapi.extensions.utils.types.buttons.* import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
import dev.inmo.tgbotapi.requests.DeleteMessage import dev.inmo.tgbotapi.requests.DeleteMessage
@ -42,17 +47,100 @@ import kotlin.random.Random
@Serializable @Serializable
sealed class CaptchaProvider { sealed class CaptchaProvider {
abstract suspend fun BehaviourContext.doAction( abstract val checkTimeSpan: TimeSpan
interface CaptchaProviderWorker {
suspend fun BehaviourContext.doCaptcha(): Boolean
suspend fun BehaviourContext.onCloseCaptcha(passed: Boolean)
}
protected abstract suspend fun allocateWorker(
eventDateTime: DateTime,
chat: GroupChat,
user: User,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?,
kickOnUnsuccess: Boolean
): CaptchaProviderWorker
suspend fun BehaviourContext.doAction(
eventDateTime: DateTime, eventDateTime: DateTime,
chat: GroupChat, chat: GroupChat,
newUsers: List<User>, newUsers: List<User>,
leftRestrictionsPermissions: ChatPermissions, leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?, adminsApi: AdminsCacheAPI?,
kickOnUnsuccess: Boolean kickOnUnsuccess: Boolean
) {
val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.map { user ->
launch {
createSubContextAndDoWithUpdatesFilter {
val worker = allocateWorker(
eventDateTime,
chat,
user,
leftRestrictionsPermissions,
adminsApi,
kickOnUnsuccess
) )
val deferred = async {
runCatchingSafely {
with(worker) {
doCaptcha()
}
}.onFailure {
this@CaptchaProvider.logger.e("Unable to do captcha", it)
}.getOrElse { false }
} }
private const val cancelData = "cancel" val subscope = LinkedSupervisorScope()
subscope.launch {
delay((userBanDateTime - eventDateTime).millisecondsLong)
subscope.cancel()
}
subscope.launch {
deferred.await()
subscope.cancel()
}
subscope.coroutineContext.job.join()
val passed = runCatching {
deferred.getCompleted()
}.onFailure {
deferred.cancel()
}.getOrElse { false }
when {
passed -> {
safelyWithoutExceptions {
restrictChatMember(
chat,
user,
permissions = leftRestrictionsPermissions
)
}
}
else -> {
send(chat, " ") {
+"User" + mention(user) + underline("didn't passed") + "captcha"
}
if (kickOnUnsuccess) {
banUser(chat, user, leftRestrictionsPermissions)
}
}
}
with(worker) {
onCloseCaptcha(passed)
}
}
}
}.joinAll()
}
}
internal const val cancelData = "cancel"
private fun EntitiesBuilder.mention(user: User, defaultName: String = "User"): EntitiesBuilder { private fun EntitiesBuilder.mention(user: User, defaultName: String = "User"): EntitiesBuilder {
return mention( return mention(
@ -105,109 +193,94 @@ private suspend fun BehaviourContext.banUser(
@Serializable @Serializable
data class SlotMachineCaptchaProvider( data class SlotMachineCaptchaProvider(
val checkTimeSeconds: Seconds = 300, val checkTimeSeconds: Seconds = 300,
val captchaText: String = "solve this captcha: ", val captchaText: String = "Solve this captcha: "
val kick: Boolean = true
) : CaptchaProvider() { ) : CaptchaProvider() {
@Transient @Transient
private val checkTimeSpan = checkTimeSeconds.seconds override val checkTimeSpan = checkTimeSeconds.seconds
override suspend fun BehaviourContext.doAction( private inner class Worker(
eventDateTime: DateTime, private val chat: GroupChat,
chat: GroupChat, private val user: User,
newUsers: List<User>, private val adminsApi: AdminsCacheAPI?
leftRestrictionsPermissions: ChatPermissions, ) : CaptchaProviderWorker {
adminsApi: AdminsCacheAPI?, private val messagesToDelete = mutableListOf<Message>()
kickOnUnsuccess: Boolean
) { override suspend fun BehaviourContext.doCaptcha(): Boolean {
val userBanDateTime = eventDateTime + checkTimeSpan val baseBuilder: EntitiesBuilderBody = {
val authorized = Channel<User>(newUsers.size) mention(user)
val messagesToDelete = Channel<Message>(Channel.UNLIMITED) regular(", $captchaText")
val subContexts = newUsers.map { user -> }
createSubContextAndDoWithUpdatesFilter(stopOnCompletion = false) {
val sentMessage = send( val sentMessage = send(
chat chat
) { ) {
mention(user) baseBuilder()
regular(", $captchaText") +": ✖✖✖"
}.also { messagesToDelete.send(it) } }.also { messagesToDelete.add(it) }
val sentDice = sendDice( val sentDice = sendDice(
sentMessage.chat, sentMessage.chat,
SlotMachineDiceAnimationType, SlotMachineDiceAnimationType,
replyToMessageId = sentMessage.messageId, replyToMessageId = sentMessage.messageId,
replyMarkup = slotMachineReplyMarkup() replyMarkup = slotMachineReplyMarkup(adminsApi != null)
).also { messagesToDelete.send(it) } ).also { messagesToDelete.add(it) }
val reels = sentDice.content.dice.calculateSlotMachineResult()!! val reels = sentDice.content.dice.calculateSlotMachineResult()!!
val leftToClick = mutableListOf( val leftToClick = mutableListOf(
reels.left.asSlotMachineReelImage.text, reels.left.asSlotMachineReelImage.text,
reels.center.asSlotMachineReelImage.text, reels.center.asSlotMachineReelImage.text,
reels.right.asSlotMachineReelImage.text reels.right.asSlotMachineReelImage.text
) )
val clicked = mutableListOf<String>()
fun buildTemplate() = "${clicked.joinToString("")}${leftToClick.joinToString("") { "✖" }}"
launch { waitMessageDataCallbackQuery().filter {
val clicked = arrayOf<String?>(null, null, null)
while (leftToClick.isNotEmpty()) {
val userClicked =
waitMessageDataCallbackQuery().filter { it.user.id == user.id && it.message.messageId == sentDice.messageId }
.first()
when { when {
userClicked.data == leftToClick.first() -> { !it.message.sameMessage(sentDice) -> false
clicked[3 - leftToClick.size] = leftToClick.removeAt(0) it.data == cancelData && adminsApi ?.isAdmin(chat.id, it.user.id) == true -> return@filter true
if (clicked.contains(null)) { it.data == cancelData && adminsApi ?.isAdmin(chat.id, it.user.id) != true -> {
safelyWithoutExceptions { answerCallbackQuery(userClicked, "Ok, next one") } answer(
editMessageReplyMarkup( it,
sentDice, "This button is only for admins"
slotMachineReplyMarkup(clicked[0], clicked[1], clicked[2])
)
} else {
safelyWithoutExceptions {
answerCallbackQuery(
userClicked,
"Thank you and welcome",
showAlert = true
) )
false
} }
safelyWithoutExceptions { deleteMessage(sentMessage) } it.user.id != user.id -> {
safelyWithoutExceptions { deleteMessage(sentDice) } answer(it, "This button is not for you")
false
}
it.data != leftToClick.first() -> {
answer(it, "Nope")
false
}
else -> {
clicked.add(leftToClick.removeFirst())
answer(it, "Ok, next one")
edit(sentMessage) {
baseBuilder()
+": ${buildTemplate()}"
}
leftToClick.isEmpty()
}
}
}.first()
return true
}
override suspend fun BehaviourContext.onCloseCaptcha(passed: Boolean) {
while (messagesToDelete.isNotEmpty()) {
runCatchingSafely { delete(messagesToDelete.removeFirst()) }
} }
} }
else -> safelyWithoutExceptions { answerCallbackQuery(userClicked, "Nope") }
}
}
authorized.send(user)
safelyWithoutExceptions {
restrictChatMember(
chat,
user,
permissions = leftRestrictionsPermissions
)
}
stop()
} }
this to user override suspend fun allocateWorker(
} eventDateTime: DateTime,
} chat: GroupChat,
user: User,
delay((userBanDateTime - eventDateTime).millisecondsLong) leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?,
authorized.close() kickOnUnsuccess: Boolean
val authorizedUsers = authorized.toList() ): CaptchaProviderWorker = Worker(chat, user, adminsApi)
subContexts.forEach { (context, user) ->
if (user !in authorizedUsers) {
context.stop()
if (kickOnUnsuccess) {
banUser(chat, user, leftRestrictionsPermissions)
}
}
}
messagesToDelete.close()
for (message in messagesToDelete) {
executeUnsafe(DeleteMessage(message.chat.id, message.messageId), retries = 0)
}
}
} }
@Serializable @Serializable
@ -217,20 +290,15 @@ data class SimpleCaptchaProvider(
val buttonText: String = "Press me\uD83D\uDE0A" val buttonText: String = "Press me\uD83D\uDE0A"
) : CaptchaProvider() { ) : CaptchaProvider() {
@Transient @Transient
private val checkTimeSpan = checkTimeSeconds.seconds override val checkTimeSpan = checkTimeSeconds.seconds
override suspend fun BehaviourContext.doAction( private inner class Worker(
eventDateTime: DateTime, private val chat: GroupChat,
chat: GroupChat, private val user: User,
newUsers: List<User>, private val adminsApi: AdminsCacheAPI?
leftRestrictionsPermissions: ChatPermissions, ) : CaptchaProviderWorker {
adminsApi: AdminsCacheAPI?, private var sentMessage: Message? = null
kickOnUnsuccess: Boolean override suspend fun BehaviourContext.doCaptcha(): Boolean {
) {
val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.map { user ->
launchSafelyWithoutExceptions {
createSubContext(this).doInContext(stopOnCompletion = false) {
val callbackData = uuid4().toString() val callbackData = uuid4().toString()
val sentMessage = send( val sentMessage = send(
chat, chat,
@ -248,55 +316,52 @@ data class SimpleCaptchaProvider(
mention(user) mention(user)
regular(", $captchaText") regular(", $captchaText")
} }
this@Worker.sentMessage = sentMessage
suspend fun removeRedundantMessages() { val pushed = waitMessageDataCallbackQuery().filter {
safelyWithoutExceptions { when {
deleteMessage(sentMessage) !it.message.sameMessage(sentMessage) -> false
it.data == callbackData && it.user.id == user.id -> true
it.data == cancelData && (adminsApi ?.isAdmin(chat.id, it.user.id) == true) -> true
it.data == callbackData -> {
answer(it, "This button is not for you")
false
} }
it.data == cancelData -> {
answer(it, "This button is for admins only")
false
} }
else -> false
val job = launchSafely {
waitMessageDataCallbackQuery().filter { query ->
val baseCheck = query.message.messageId == sentMessage.messageId
val userAnswered = query.user.id == user.id && query.data == callbackData
val adminCanceled = (query.data == cancelData && (adminsApi?.isAdmin(
sentMessage.chat.id,
query.user.id
)) == true)
if (baseCheck && adminCanceled) {
sendAdminCanceledMessage(
sentMessage.chat,
user,
query.user
)
} }
baseCheck && (adminCanceled || userAnswered)
}.first() }.first()
removeRedundantMessages() answer(
safelyWithoutExceptions { pushed,
restrictChatMember( when (pushed.data) {
chat, cancelData -> "You have cancelled captcha"
user, else -> "Ok, thanks"
permissions = leftRestrictionsPermissions }
) )
}
stop() return true
} }
delay((userBanDateTime - eventDateTime).millisecondsLong) override suspend fun BehaviourContext.onCloseCaptcha(passed: Boolean) {
sentMessage ?.let {
delete(it)
}
}
if (job.isActive) {
job.cancel()
if (kickOnUnsuccess) {
banUser(chat, user, leftRestrictionsPermissions)
}
}
stop()
}
}
}.joinAll()
} }
override suspend fun allocateWorker(
eventDateTime: DateTime,
chat: GroupChat,
user: User,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?,
kickOnUnsuccess: Boolean
): CaptchaProviderWorker = Worker(chat, user, adminsApi)
} }
private object ExpressionBuilder { private object ExpressionBuilder {
@ -355,20 +420,15 @@ data class ExpressionCaptchaProvider(
val attempts: Int = 3 val attempts: Int = 3
) : CaptchaProvider() { ) : CaptchaProvider() {
@Transient @Transient
private val checkTimeSpan = checkTimeSeconds.seconds override val checkTimeSpan = checkTimeSeconds.seconds
override suspend fun BehaviourContext.doAction( private inner class Worker(
eventDateTime: DateTime, private val chat: GroupChat,
chat: GroupChat, private val user: User,
newUsers: List<User>, private val adminsApi: AdminsCacheAPI?
leftRestrictionsPermissions: ChatPermissions, ) : CaptchaProviderWorker {
adminsApi: AdminsCacheAPI?, private var sentMessage: Message? = null
kickOnUnsuccess: Boolean override suspend fun BehaviourContext.doCaptcha(): Boolean {
) {
val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.map { user ->
launch {
createSubContextAndDoWithUpdatesFilter {
val callbackData = ExpressionBuilder.createExpression( val callbackData = ExpressionBuilder.createExpression(
maxPerNumber, maxPerNumber,
operations operations
@ -398,58 +458,8 @@ data class ExpressionCaptchaProvider(
bold(callbackData.second) bold(callbackData.second)
} }
suspend fun removeRedundantMessages(removeSentMessage: Boolean = true) {
safelyWithoutExceptions {
if (removeSentMessage) {
deleteMessage(sentMessage)
}
}
}
var passed: Boolean? = null
val passedMutex = Mutex()
val callback: suspend (Boolean) -> Unit = {
passedMutex.withLock {
if (passed == null) {
passed = it
runCatchingSafely<Unit> {
when {
it -> {
removeRedundantMessages()
safelyWithoutExceptions {
restrictChatMember(
chat,
user,
permissions = leftRestrictionsPermissions
)
}
}
else -> {
removeRedundantMessages(removeSentMessage = false)
edit(sentMessage) {
+"User " + mention(user) + underline("didn't passed") + "captcha"
}
if (kickOnUnsuccess) {
banUser(chat, user, leftRestrictionsPermissions)
}
}
}
}
}
}
}
val banJob = launch {
delay((userBanDateTime - eventDateTime).millisecondsLong)
if (passed == null) {
callback(false)
stop()
}
}
var leftAttempts = attempts var leftAttempts = attempts
waitMessageDataCallbackQuery().takeWhile { leftAttempts > 0 }.filter { query -> return waitMessageDataCallbackQuery().takeWhile { leftAttempts > 0 }.map { query ->
val baseCheck = query.message.messageId == sentMessage.messageId val baseCheck = query.message.messageId == sentMessage.messageId
val dataCorrect = (query.user.id == user.id && query.data == correctAnswer) val dataCorrect = (query.user.id == user.id && query.data == correctAnswer)
val adminCanceled = (query.data == cancelData && (adminsApi?.isAdmin( val adminCanceled = (query.data == cancelData && (adminsApi?.isAdmin(
@ -457,7 +467,6 @@ data class ExpressionCaptchaProvider(
query.user.id query.user.id
)) == true) )) == true)
baseCheck && if (dataCorrect || adminCanceled) { baseCheck && if (dataCorrect || adminCanceled) {
banJob.cancel()
if (adminCanceled) { if (adminCanceled) {
sendAdminCanceledMessage( sendAdminCanceledMessage(
sentMessage.chat, sentMessage.chat,
@ -473,12 +482,23 @@ data class ExpressionCaptchaProvider(
} }
false false
} }
}.firstOrNull() }.firstOrNull() ?: false
}
callback(leftAttempts > 0) override suspend fun BehaviourContext.onCloseCaptcha(passed: Boolean) {
sentMessage ?.let {
delete(it)
} }
} }
}.joinAll() }
}
override suspend fun allocateWorker(
eventDateTime: DateTime,
chat: GroupChat,
user: User,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?,
kickOnUnsuccess: Boolean
): CaptchaProviderWorker = Worker(chat, user, adminsApi)
} }