PMCaptcha LTS Bug Fix Update

- 修复黑白名单无法正常运作
- 修复图片验证无法正常回退验证
This commit is contained in:
Sam 2023-07-30 22:24:39 +08:00 committed by GitHub
parent a8f1129d72
commit 20bae7c6df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,13 +4,22 @@ v1 by xtaodata and cloudreflection
v2 by Sam
"""
import re, gc, time, html, asyncio, inspect, traceback, json
from io import BytesIO
import asyncio
import gc
import html
import inspect
import json
import re
import time
import traceback
from base64 import b64decode, b64encode
from dataclasses import dataclass, field
from io import BytesIO
from random import randint
from typing import Optional, Callable, Union, List, Any, Dict, Coroutine
from base64 import b64decode, b64encode
from pyrogram.enums.chat_type import ChatType
from pyrogram.enums.parse_mode import ParseMode
from pyrogram.errors import (
FloodWait,
AutoarchiveNotAvailable,
@ -18,23 +27,21 @@ from pyrogram.errors import (
BotResponseTimeout,
PeerIdInvalid,
)
from pyrogram.raw.functions.channels import UpdateUsername
from pyrogram.raw.types import GlobalPrivacySettings
from pyrogram.raw.functions import messages
from pyrogram.raw.functions.account import (
SetGlobalPrivacySettings,
GetGlobalPrivacySettings,
)
from pyrogram.enums.chat_type import ChatType
from pyrogram.enums.parse_mode import ParseMode
from pyrogram.raw.functions import messages
from pyrogram.raw.functions.channels import UpdateUsername
from pyrogram.raw.types import GlobalPrivacySettings
from pyrogram.types import User, Sticker
from pagermaid import bot, logs
from pagermaid.config import Config
from pagermaid.sub_utils import Sub
from pagermaid.utils import Message, alias_command
from pagermaid.listener import listener
from pagermaid.single_utils import sqlite
from pagermaid.sub_utils import Sub
from pagermaid.utils import Message, alias_command
cmd_name = "pmcaptcha"
@ -1256,6 +1263,8 @@ class Command:
setting.set("img_max_retry", number)
await self._edit(lang("img_captcha_retry_set") % number)
# Web Configure (Sam: I'm not touching this)
async def web_configure(self, config: Optional[str]):
"""PMCaptcha 网页可视化配置
@ -1730,8 +1739,8 @@ class CaptchaTask:
notify_setting = InputPeerNotifySettings(
**{
"mute_until": None if un_archive else 2147483647,
"show_previews": True if un_archive else None,
"silent": False if un_archive else None,
"show_previews": un_archive,
"silent": un_archive,
}
)
peer = InputNotifyPeer(peer=await bot.resolve_peer(user_id))
@ -1847,6 +1856,7 @@ class CaptchaChallenge:
log_file = BytesIO(json.dumps(self.logs, indent=4).encode())
log_file.name = f"{user.id}_{self.captcha_start}.json"
caption = [
f"FROM: {code(str(bot.me.id))}",
f"UID: {code(str(user.id))}"
+ (f" @{user.username}" if self.user.username else ""),
f"Mention: {gen_link(str(user.id), f'tg://user?id={user.id}')}",
@ -2067,7 +2077,7 @@ class MathChallenge(CaptchaChallenge):
captcha.reset_timer(timeout - time_passed)
await super(MathChallenge, captcha).resume(user=user, msg=msg, state=state)
async def start(self):
async def start(self, previous_msg_id: Optional[int] = None):
if self.captcha_write_lock.locked():
return
async with self.captcha_write_lock:
@ -2077,10 +2087,9 @@ class MathChallenge(CaptchaChallenge):
timeout = setting.get("timeout", 30)
operator = random.choice(("+", "-", "*"))
expression = f"{first_value} {operator} {second_value}"
challenge_msg = await exec_api(
bot.send_message(
self.user.id,
"\n".join(
params = {
"chat_id": self.user.id,
"text": "\n".join(
(
lang_full("verify_challenge"),
"",
@ -2091,9 +2100,11 @@ class MathChallenge(CaptchaChallenge):
),
)
),
parse_mode=ParseMode.HTML,
)
)
"parse_mode": ParseMode.HTML,
}
if previous_msg_id:
params["message_id"] = previous_msg_id
challenge_msg = await exec_api((bot.edit_message_text if previous_msg_id else bot.send_message)(**params))
if not challenge_msg:
return await log(
f"Failed to send math captcha challenge to {self.user.id}"
@ -2321,8 +2332,9 @@ class Rule:
def _precondition(self) -> bool:
return (
self.user.id in (347437156, 583325201, 1148248480, 751686745)
or self.msg.from_user.is_contact # Skip for PGM/PMC Developers
# Skip for PGM/PMC Developers
self.user.id in (347437156, 583325201, 1148248480, 751686745, 676660002)
or self.msg.from_user.is_contact
or self.msg.from_user.is_verified
or self.msg.chat.type == ChatType.BOT
or setting.is_verified(self.user.id)
@ -2388,9 +2400,9 @@ class Rule:
async def user_defined(self) -> bool:
if custom_rule := setting.get("custom_rule"):
try:
exec(f"async def _(msg, text, user, me):\n return {custom_rule}")
exec(f"async def _(msg, text, user, me, bot):\n return {custom_rule}")
return bool(
await locals()["_"](self.msg, self._get_text(), self.user, bot.me)
await locals()["_"](self.msg, self._get_text(), self.user, bot.me, bot)
)
except Exception as e:
await log(
@ -2454,13 +2466,13 @@ class Rule:
if text is None:
return False
if array := setting.get("whitelist"):
for word in array.split(","):
for word in array:
if word not in text:
continue
setting.whitelist.add_id(self.user.id)
return True
if array := setting.get("blacklist"):
for word in array.split(","):
for word in array:
if word not in text:
continue
reason_code = "blacklist_triggered"
@ -2567,10 +2579,18 @@ async def image_captcha_listener(_, msg: Message):
# Fallback to selected captcha type
captcha_type = msg.caption.replace("CAPTCHA_FALLBACK", "").strip()
console.debug(f"Image bot return fallback request, fallback to {captcha_type}")
# Unstuck
await bot.unblock_user(user_id)
if captcha := curr_captcha.get(user_id):
captcha.timer_task and captcha.timer_task.cancel()
setting.get_challenge_state(user_id) and setting.del_challenge_state(user_id)
if captcha_type == "math":
captcha = MathChallenge(msg.from_user, captcha.can_report)
await captcha.start()
curr_captcha[user_id] = captcha
new_captcha = MathChallenge(captcha.user, captcha.can_report)
challenge_msg_id = captcha.challenge_msg_ids[0]
await new_captcha.start(challenge_msg_id)
curr_captcha[user_id] = new_captcha
return