From 898aa30b1d7047d42cbe0b56ad3366fb0b03106f Mon Sep 17 00:00:00 2001 From: lanvent Date: Thu, 6 Apr 2023 21:53:48 +0800 Subject: [PATCH] godcmd: add temp passwd --- common/log.py | 15 ++++++++++++--- common/package_manager.py | 7 +++++-- plugins/godcmd/godcmd.py | 16 +++++++++++----- plugins/plugin_manager.py | 5 ++++- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/common/log.py b/common/log.py index f10eff9..14c7eff 100644 --- a/common/log.py +++ b/common/log.py @@ -2,9 +2,13 @@ import logging import sys -def _get_logger(): - log = logging.getLogger('log') - log.setLevel(logging.INFO) +def _reset_logger(log): + for handler in log.handlers: + handler.close() + log.removeHandler(handler) + del handler + log.handlers.clear() + log.propagate = False console_handle = logging.StreamHandler(sys.stdout) console_handle.setFormatter(logging.Formatter('[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')) @@ -13,6 +17,11 @@ def _get_logger(): datefmt='%Y-%m-%d %H:%M:%S')) log.addHandler(file_handle) log.addHandler(console_handle) + +def _get_logger(): + log = logging.getLogger('log') + _reset_logger(log) + log.setLevel(logging.INFO) return log diff --git a/common/package_manager.py b/common/package_manager.py index da9b982..9370298 100644 --- a/common/package_manager.py +++ b/common/package_manager.py @@ -1,11 +1,14 @@ import time import pip +from pip._internal import main as pipmain +from common.log import logger,_reset_logger def install(package): - pip.main(['install', package]) + pipmain(['install', package]) def install_requirements(file): - pip.main(['install', '-r', file, "--upgrade"]) + pipmain(['install', '-r', file, "--upgrade"]) + _reset_logger(logger) def check_dulwich(): needwait = False diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index d294fb3..73439df 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -2,6 +2,8 @@ import json import os +import random +import string import traceback from typing import Tuple from bridge.bridge import Bridge @@ -158,7 +160,11 @@ class Godcmd(Plugin): else: with open(config_path,"r") as f: gconf=json.load(f) - + if gconf["password"] == "": + self.temp_password = "".join(random.sample(string.digits, 4)) + logger.info("[Godcmd] 因未设置口令,本次的临时口令为%s。"%self.temp_password) + else: + self.temp_password = None custom_commands = conf().get("clear_memory_commands", []) for custom_command in custom_commands: if custom_command and custom_command.startswith("#"): @@ -167,7 +173,7 @@ class Godcmd(Plugin): COMMANDS["reset"]["alias"].append(custom_command) self.password = gconf["password"] - self.admin_users = gconf["admin_users"] # 预存的管理员账号,这些账号不需要认证 TODO: 用户名每次都会变,目前不可用 + self.admin_users = gconf["admin_users"] # 预存的管理员账号,这些账号不需要认证。itchat的用户名每次都会变,不可用 self.isrunning = True # 机器人是否运行中 self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context @@ -358,9 +364,6 @@ class Godcmd(Plugin): if isadmin: return False,"管理员账号无需认证" - if len(self.password) == 0: - return False,"未设置口令,无法认证" - if len(args) != 1: return False,"请提供口令" @@ -368,6 +371,9 @@ class Godcmd(Plugin): if password == self.password: self.admin_users.append(userid) return True,"认证成功" + elif password == self.temp_password: + self.admin_users.append(userid) + return True,"认证成功,请尽快设置口令" else: return False,"认证失败" diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index a2b2bc8..9856e34 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -238,7 +238,7 @@ class PluginManager: if os.path.exists(os.path.join(dirname,"requirements.txt")): logger.info("detect requirements.txt,installing...") pkgmgr.install_requirements(os.path.join(dirname,"requirements.txt")) - return True, "安装插件成功,请扫描插件或重启程序" + return True, "安装插件成功,请使用#scanp命令扫描插件或重启程序" except Exception as e: logger.error("Failed to install plugin, {}".format(e)) return False, "安装插件失败,"+str(e) @@ -254,6 +254,9 @@ class PluginManager: import shutil shutil.rmtree(dirname) rawname = self.plugins[name].name + for event in self.listening_plugins: + if name in self.listening_plugins[event]: + self.listening_plugins[event].remove(name) del self.plugins[name] del self.pconf["plugins"][rawname] self.loaded[dirname] = None