From 28ec8d316dbe264aaaf93c04d948ed9e91d44950 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Thu, 22 Feb 2024 23:53:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=E5=92=8C=E4=BF=AE=E5=A4=8DPython=E6=8F=92=E4=BB=B6=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/Cargo.toml | 2 +- ica-rs/plugins/bmcl.py | 7 +++- ica-rs/plugins/save_eval.py | 78 +++++++++++++++++++++++++++++++++++++ ica-rs/src/py/mod.rs | 7 +++- news.md | 4 ++ 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 ica-rs/plugins/save_eval.py diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index e5552a4..33fd717 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ica-rs" -version = "0.4.8" +version = "0.4.9" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/ica-rs/plugins/bmcl.py b/ica-rs/plugins/bmcl.py index 5e98511..e061c85 100644 --- a/ica-rs/plugins/bmcl.py +++ b/ica-rs/plugins/bmcl.py @@ -136,7 +136,12 @@ def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None: else: # 搜索是否有这个名字的节点 names = [r["name"].lower() for r in ranks] - finds = [re.search(name.lower(), n) for n in names] + try: + finds = [re.search(name.lower(), n) for n in names] + except re.error as e: + reply = msg.reply_with(f"正则表达式错误: {e}, 请检查输入") + client.send_message(reply) + return if not any(finds): reply = msg.reply_with(f"未找到名为{name}的节点") client.send_message(reply) diff --git a/ica-rs/plugins/save_eval.py b/ica-rs/plugins/save_eval.py new file mode 100644 index 0000000..e3a20cc --- /dev/null +++ b/ica-rs/plugins/save_eval.py @@ -0,0 +1,78 @@ +import time +import random +import traceback + +from typing import TYPE_CHECKING, TypeVar + +if TYPE_CHECKING: + from ica_typing import NewMessage, IcaClient +else: + NewMessage = TypeVar("NewMessage") + IcaClient = TypeVar("IcaClient") + +def safe_eval(code: str) -> str: + try: + # code = code.replace('help', '坏东西!\n') + # code = code.replace('bytes', '坏东西!\n') + # code = code.replace('encode', '坏东西!\n') + # code = code.replace('decode', '坏东西!\n') + # code = code.replace('compile', '屑的!\n') + # code = code.replace('globals', '拿不到!\n') + code = code.replace("os", "坏东西!\n") + code = code.replace("sys", "坏东西!\n") + # code = code.replace('input', '坏东西!\n') + # code = code.replace('__', '啊哈!\n') + # code = code.replace('import', '很坏!\n') + code = code.replace(" kill", "别跑!\n") + code = code.replace(" rm ", "别跑!\n") + code = code.replace("exit", "好坏!\n") + code = code.replace("eval", "啊哈!\n") + code = code.replace("exec", "抓住!\n") + start_time = time.time() + try: + import os + import math + import decimal + + global_val = { + "time": time, + "math": math, + "decimal": decimal, + "random": random, + "__import__": "", + "globals": "", + "compile": "", + "help": "", + "exit": "", + "input": "", + "return": "别惦记你那个 return 了", + "getattr": "", + "setattr": "", + } + os.system = "不许" + result = str(eval(code, global_val, {})) + limit = 500 + if len(result) > limit: + result = result[:limit] + except: + result = traceback.format_exc() + end_time = time.time() + + if result == "6" or result == 6: + result = "他确实等于 6" + + result = f"{code}\neval result:\n{result}\n耗时: {end_time - start_time} s" + return result + except: + error = traceback.format_exc() + result = f"error:\n{error}" + return result + + +def on_message(message: NewMessage, client: IcaClient) -> None: + if not (message.is_from_self or message.is_reply): + if message.content.startswith("/="): + code = message.content[2:] + result = safe_eval(code) + reply = message.reply_with(result) + client.send_message(reply) diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 5c0e976..09659d6 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -214,7 +214,12 @@ pub async fn new_message_py(message: &NewMessage, client: &Client) { let async_py_func = py_module.getattr(py, "on_message"); match async_py_func { Ok(async_py_func) => { - async_py_func.as_ref(py).call1(args).unwrap(); + match async_py_func.as_ref(py).call1(args) { + Err(e) => { + warn!("get a PyErr when call on_message from {:?}: {:?}", path, e); + }, + _ => () + } } Err(e) => { warn!("failed to get on_message function: {:?}", e); diff --git a/news.md b/news.md index d92e54f..e254031 100644 --- a/news.md +++ b/news.md @@ -1,5 +1,9 @@ # 更新日志 +## 0.4.9 + +修复了 Python 插件运行错误会导致整个程序崩溃的问题 + ## 0.4.8 添加了 `filter_list` 用于过滤特定人的消息