更新版本号和修复Python插件运行错误的问题

This commit is contained in:
shenjack 2024-02-22 23:53:23 +08:00
parent 727e5f84dd
commit 28ec8d316d
Signed by: shenjack
GPG Key ID: 7B1134A979775551
5 changed files with 95 additions and 3 deletions

View File

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

View File

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

View File

@ -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__": "<built-in function __import__>",
"globals": "<built-in function globals>",
"compile": "<built-in function compile>",
"help": "<built-in function help>",
"exit": "<built-in function exit>",
"input": "<built-in function input>",
"return": "别惦记你那个 return 了",
"getattr": "<built-in function getattr>",
"setattr": "<built-in function 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)

View File

@ -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);

View File

@ -1,5 +1,9 @@
# 更新日志
## 0.4.9
修复了 Python 插件运行错误会导致整个程序崩溃的问题
## 0.4.8
添加了 `filter_list` 用于过滤特定人的消息