2022-06-18 14:48:46 +08:00
|
|
|
|
from pydantic import ValidationError
|
2022-04-10 22:19:50 +08:00
|
|
|
|
from configs.config import Config
|
|
|
|
|
|
from services.log import logger
|
|
|
|
|
|
from utils.manager import (plugins2block_manager, plugins2cd_manager,
|
|
|
|
|
|
plugins2count_manager, plugins2settings_manager,
|
|
|
|
|
|
plugins_manager)
|
|
|
|
|
|
from utils.utils import get_matchers
|
|
|
|
|
|
|
|
|
|
|
|
from ..auth import Depends, User, token_to_user
|
|
|
|
|
|
from ..config import *
|
|
|
|
|
|
|
|
|
|
|
|
plugin_name_list = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/webui/plugins")
|
|
|
|
|
|
def _(type_: Optional[str], user: User = Depends(token_to_user)) -> Result:
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取插件列表
|
|
|
|
|
|
:param type_: 类型 normal, superuser, hidden, admin
|
|
|
|
|
|
"""
|
|
|
|
|
|
global plugin_name_list
|
|
|
|
|
|
if not plugin_name_list:
|
2022-11-23 23:11:36 +08:00
|
|
|
|
plugin_name_list = [x.plugin_name for x in get_matchers(True)]
|
2022-04-10 22:19:50 +08:00
|
|
|
|
plugin_list = []
|
|
|
|
|
|
plugin_data = plugins_manager.get_data()
|
|
|
|
|
|
for model in plugin_data:
|
|
|
|
|
|
if model in plugin_name_list:
|
|
|
|
|
|
try:
|
|
|
|
|
|
data = plugin_data.get(model)
|
2022-11-23 23:11:36 +08:00
|
|
|
|
# data.model = model
|
|
|
|
|
|
plugin_name = data.plugin_name
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if (
|
|
|
|
|
|
(type_ == "hidden" and "[hidden]" not in plugin_name.lower())
|
|
|
|
|
|
or (type_ == "admin" and "[admin]" not in plugin_name.lower())
|
|
|
|
|
|
or (
|
|
|
|
|
|
type_ == "superuser"
|
|
|
|
|
|
and "[superuser]" not in plugin_name.lower()
|
|
|
|
|
|
)
|
|
|
|
|
|
):
|
|
|
|
|
|
continue
|
|
|
|
|
|
if type_ == "normal" and (
|
|
|
|
|
|
"[hidden]" in plugin_name.lower()
|
|
|
|
|
|
or "[admin]" in plugin_name.lower()
|
|
|
|
|
|
or "[superuser]" in plugin_name.lower()
|
|
|
|
|
|
):
|
|
|
|
|
|
continue
|
|
|
|
|
|
data = {"model": model}
|
|
|
|
|
|
if x := plugin_data.get(model):
|
2022-11-23 23:11:36 +08:00
|
|
|
|
if not x.status and x.block_type in [
|
2022-04-10 22:19:50 +08:00
|
|
|
|
"group",
|
|
|
|
|
|
"private",
|
|
|
|
|
|
"all",
|
|
|
|
|
|
]:
|
2022-11-23 23:11:36 +08:00
|
|
|
|
x.block_type = (
|
2022-04-10 22:19:50 +08:00
|
|
|
|
"群聊"
|
2022-11-23 23:11:36 +08:00
|
|
|
|
if x.block_type == "group"
|
2022-04-10 22:19:50 +08:00
|
|
|
|
else "私聊"
|
2022-11-23 23:11:36 +08:00
|
|
|
|
if x.block_type == "private"
|
2022-04-10 22:19:50 +08:00
|
|
|
|
else "全部"
|
|
|
|
|
|
)
|
2022-11-23 23:11:36 +08:00
|
|
|
|
data["plugin_manager"] = PluginManager(**x.dict())
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if x := plugins2settings_manager.get(model):
|
2022-11-21 20:43:41 +08:00
|
|
|
|
if x.cmd and isinstance(x.cmd, list):
|
|
|
|
|
|
x.cmd = ",".join(x.cmd)
|
2022-11-23 23:11:36 +08:00
|
|
|
|
data["plugin_settings"] = PluginSettings(**x.dict())
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if x := plugins2cd_manager.get(model):
|
2022-11-23 23:11:36 +08:00
|
|
|
|
data["cd_limit"] = CdLimit(**x.dict())
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if x := plugins2block_manager.get(model):
|
2022-11-23 23:11:36 +08:00
|
|
|
|
data["block_limit"] = BlockLimit(**x.dict())
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if x := plugins2count_manager.get(model):
|
2022-11-23 23:11:36 +08:00
|
|
|
|
data["count_limit"] = CountLimit(**x.dict())
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if x := Config.get(model):
|
|
|
|
|
|
id_ = 0
|
|
|
|
|
|
tmp = []
|
|
|
|
|
|
for key in x.keys():
|
|
|
|
|
|
tmp.append(
|
|
|
|
|
|
PluginConfig(
|
|
|
|
|
|
**{
|
|
|
|
|
|
"key": key,
|
|
|
|
|
|
"help_": x[key].get("help"),
|
|
|
|
|
|
"id": id_,
|
|
|
|
|
|
**x[key],
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
id_ += 1
|
|
|
|
|
|
data["plugin_config"] = tmp
|
|
|
|
|
|
plugin_list.append(Plugin(**data))
|
2022-06-18 14:48:46 +08:00
|
|
|
|
except (AttributeError, ValidationError) as e:
|
|
|
|
|
|
logger.error(
|
|
|
|
|
|
f"WEB_UI GET /webui/plugins model:{model} 发生错误 {type(e)}:{e}"
|
|
|
|
|
|
)
|
2022-04-10 22:19:50 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(
|
|
|
|
|
|
f"WEB_UI GET /webui/plugins model:{model} 发生错误 {type(e)}:{e}"
|
|
|
|
|
|
)
|
|
|
|
|
|
return Result(
|
|
|
|
|
|
code=500,
|
|
|
|
|
|
data=f"WEB_UI GET /webui/plugins model:{model} 发生错误 {type(e)}:{e}",
|
|
|
|
|
|
)
|
|
|
|
|
|
return Result(code=200, data=plugin_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/webui/plugins")
|
|
|
|
|
|
def _(plugin: Plugin, user: User = Depends(token_to_user)) -> Result:
|
|
|
|
|
|
"""
|
|
|
|
|
|
修改插件信息
|
|
|
|
|
|
:param plugin: 插件内容
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
if plugin.plugin_config:
|
|
|
|
|
|
for c in plugin.plugin_config:
|
2022-07-31 17:45:14 +08:00
|
|
|
|
if not c.value:
|
|
|
|
|
|
Config.set_config(plugin.model, c.key, None)
|
|
|
|
|
|
continue
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if str(c.value).lower() in ["true", "false"] and (
|
|
|
|
|
|
c.default_value is None or isinstance(c.default_value, bool)
|
|
|
|
|
|
):
|
2022-06-03 00:59:00 +08:00
|
|
|
|
c.value = str(c.value).lower() == "true"
|
2022-04-10 22:19:50 +08:00
|
|
|
|
elif isinstance(
|
|
|
|
|
|
Config.get_config(plugin.model, c.key, c.value), int
|
|
|
|
|
|
) or isinstance(c.default_value, int):
|
|
|
|
|
|
c.value = int(c.value)
|
|
|
|
|
|
elif isinstance(
|
|
|
|
|
|
Config.get_config(plugin.model, c.key, c.value), float
|
|
|
|
|
|
) or isinstance(c.default_value, float):
|
|
|
|
|
|
c.value = float(c.value)
|
|
|
|
|
|
elif isinstance(c.value, str) and (
|
2022-07-31 17:45:14 +08:00
|
|
|
|
isinstance(Config.get_config(plugin.model, c.key, c.value), (list, tuple))
|
|
|
|
|
|
or isinstance(c.default_value, (list, tuple))
|
2022-04-10 22:19:50 +08:00
|
|
|
|
):
|
2022-06-05 19:51:23 +08:00
|
|
|
|
default_value = Config.get_config(plugin.model, c.key, c.value)
|
2022-04-10 22:19:50 +08:00
|
|
|
|
c.value = c.value.split(",")
|
2022-06-05 19:51:23 +08:00
|
|
|
|
if default_value and isinstance(default_value[0], int):
|
|
|
|
|
|
c.value = [int(x) for x in c.value]
|
|
|
|
|
|
elif default_value and isinstance(default_value[0], float):
|
|
|
|
|
|
c.value = [float(x) for x in c.value]
|
|
|
|
|
|
elif default_value and isinstance(default_value[0], bool):
|
|
|
|
|
|
temp = []
|
|
|
|
|
|
for x in c.value:
|
|
|
|
|
|
temp.append(x.lower() == "true")
|
|
|
|
|
|
c.value = temp
|
2022-04-10 22:19:50 +08:00
|
|
|
|
Config.set_config(plugin.model, c.key, c.value)
|
|
|
|
|
|
Config.save(None, True)
|
|
|
|
|
|
else:
|
|
|
|
|
|
if plugin.plugin_settings:
|
|
|
|
|
|
for key, value in plugin.plugin_settings:
|
2022-06-18 14:31:44 +08:00
|
|
|
|
if key == "cmd":
|
|
|
|
|
|
value = value.split(",")
|
2022-11-23 23:11:36 +08:00
|
|
|
|
setattr(plugins2settings_manager[plugin.model], key, value)
|
2022-04-10 22:19:50 +08:00
|
|
|
|
if plugin.plugin_manager:
|
|
|
|
|
|
for key, value in plugin.plugin_manager:
|
2022-11-23 23:11:36 +08:00
|
|
|
|
setattr(plugins_manager[plugin.model], key, value)
|
2022-04-10 22:19:50 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(
|
|
|
|
|
|
f"WEB_UI POST /webui/plugins model:{plugin.model} 发生错误 {type(e)}:{e}"
|
|
|
|
|
|
)
|
|
|
|
|
|
return Result(
|
|
|
|
|
|
code=500,
|
|
|
|
|
|
data=f"WEB_UI POST /webui/plugins model:{plugin.model} 发生错误 {type(e)}:{e}",
|
|
|
|
|
|
)
|
2022-11-30 23:40:55 +08:00
|
|
|
|
for key in plugins2settings_manager.keys():
|
|
|
|
|
|
if isinstance(plugins2settings_manager[key].cmd, str):
|
|
|
|
|
|
plugins2settings_manager[key].cmd = plugins2settings_manager[key].cmd.split(',')
|
2022-11-23 23:11:36 +08:00
|
|
|
|
plugins2settings_manager.save()
|
|
|
|
|
|
plugins_manager.save()
|
2022-06-05 19:51:23 +08:00
|
|
|
|
return Result(code=200, data="修改成功!")
|