PagerMaid-Pyro/pagermaid/modules/plugin.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

328 lines
14 KiB
Python
Raw Normal View History

2022-05-23 12:40:30 +00:00
""" PagerMaid module to manage plugins. """
2022-06-20 13:55:14 +00:00
import contextlib
from os import remove, path, sep
2022-05-23 12:40:30 +00:00
from os.path import exists
2023-01-31 16:24:56 +00:00
from re import search, I
2022-05-23 12:40:30 +00:00
from shutil import copyfile, move
from pagermaid.common.plugin import plugin_remote_manager, plugin_manager
2023-01-31 16:24:56 +00:00
from pagermaid.common.reload import reload_all
from pagermaid.enums import Message
2022-05-23 12:40:30 +00:00
from pagermaid.listener import listener
from pagermaid.static import working_dir
from pagermaid.utils import lang
from pagermaid.utils.bot_utils import log, upload_attachment
2022-05-23 12:40:30 +00:00
def remove_plugin(name):
plugin_directory = f"{working_dir}{sep}plugins{sep}"
2022-06-20 13:55:14 +00:00
with contextlib.suppress(FileNotFoundError):
2022-05-23 12:40:30 +00:00
remove(f"{plugin_directory}{name}.py")
2022-06-20 13:55:14 +00:00
with contextlib.suppress(FileNotFoundError):
2022-05-23 12:40:30 +00:00
remove(f"{plugin_directory}{name}.py.disabled")
def move_plugin(file_path):
name = path.basename(file_path)[:-3]
plugin_directory = f"{working_dir}{sep}plugins{sep}"
remove_plugin(name)
move(file_path, plugin_directory)
2023-03-12 03:56:01 +00:00
@listener(
is_plugin=False,
outgoing=True,
command="apt",
need_admin=True,
diagnostics=False,
description=lang("apt_des"),
parameters=lang("apt_parameters"),
)
2022-06-20 13:55:14 +00:00
async def plugin(message: Message):
2022-05-23 12:40:30 +00:00
if len(message.parameter) == 0:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
return
reply = message.reply_to_message
plugin_directory = f"{working_dir}{sep}plugins{sep}"
if message.parameter[0] == "install":
if len(message.parameter) == 1:
file_path = await plugin_manager.download_from_message(message)
if file_path is None:
2023-03-12 03:56:01 +00:00
await message.edit(lang("apt_no_py"))
2022-05-23 12:40:30 +00:00
return
plugin_name = path.basename(file_path)[:-3]
plugin_manager.remove_plugin(plugin_name)
2022-05-23 12:40:30 +00:00
move_plugin(file_path)
2023-03-12 03:56:01 +00:00
await message.edit(
f"<b>{lang('apt_name')}</b>\n\n"
f"{lang('apt_plugin')} "
f"{plugin_name} {lang('apt_installed')}"
2023-03-12 03:56:01 +00:00
)
await log(f"{lang('apt_install_success')} {plugin_name}.")
2022-11-14 14:11:27 +00:00
await reload_all()
2022-05-23 12:40:30 +00:00
elif len(message.parameter) >= 2:
2023-01-31 16:24:56 +00:00
await plugin_manager.load_remote_plugins()
2022-05-23 12:40:30 +00:00
process_list = message.parameter
2023-03-12 03:56:01 +00:00
message = await message.edit(lang("apt_processing"))
2022-05-23 12:40:30 +00:00
del process_list[0]
success_list = []
failed_list = []
no_need_list = []
for i in process_list:
2023-01-31 16:24:56 +00:00
if plugin_manager.get_remote_plugin(i):
local_temp = plugin_manager.get_local_plugin(i)
if local_temp and not plugin_manager.plugin_need_update(i):
no_need_list.append(i)
else:
try:
if await plugin_manager.install_remote_plugin(i):
success_list.append(i)
else:
failed_list.append(i)
except Exception:
failed_list.append(i)
2022-05-23 12:40:30 +00:00
else:
failed_list.append(i)
text = f"<b>{lang('apt_name')}</b>\n\n"
if len(success_list) > 0:
2023-03-12 03:56:01 +00:00
text += lang("apt_install_success") + " : %s\n" % ", ".join(
success_list
)
2022-05-23 12:40:30 +00:00
if len(failed_list) > 0:
2023-03-12 03:56:01 +00:00
text += lang("apt_not_found") + " %s\n" % ", ".join(failed_list)
2022-05-23 12:40:30 +00:00
if len(no_need_list) > 0:
2023-03-12 03:56:01 +00:00
text += lang("apt_no_update") + " %s\n" % ", ".join(no_need_list)
2022-05-23 12:40:30 +00:00
await log(text)
restart = len(success_list) > 0
await message.edit(text)
if restart:
2022-11-14 14:11:27 +00:00
await reload_all()
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
elif message.parameter[0] == "remove":
if len(message.parameter) == 2:
2023-01-31 16:24:56 +00:00
if plugin_manager.remove_plugin(message.parameter[1]):
2023-03-12 03:56:01 +00:00
await message.edit(
f"{lang('apt_remove_success')} {message.parameter[1]}"
)
2022-05-23 12:40:30 +00:00
await log(f"{lang('apt_remove')} {message.parameter[1]}.")
2022-11-14 14:11:27 +00:00
await reload_all()
2022-05-23 12:40:30 +00:00
elif "/" in message.parameter[1]:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("apt_not_exist"))
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
elif message.parameter[0] == "status":
if len(message.parameter) == 1:
(
active_plugins,
disabled_plugins,
inactive_plugins,
) = plugin_manager.get_plugins_status()
active_plugins_string = ", ".join(active_plugins)
inactive_plugins_string = ", ".join([i.name for i in inactive_plugins])
disabled_plugins_string = ", ".join([i.name for i in disabled_plugins])
2022-05-23 12:40:30 +00:00
if len(active_plugins) == 0:
active_plugins_string = f"`{lang('apt_no_running_plugins')}`"
if len(inactive_plugins) == 0:
inactive_plugins_string = f"`{lang('apt_no_load_failed_plugins')}`"
if len(disabled_plugins) == 0:
disabled_plugins_string = f"`{lang('apt_no_disabled_plugins')}`"
2023-03-12 03:56:01 +00:00
output = (
f"**{lang('apt_plugin_list')}**\n"
f"{lang('apt_plugin_running')}: {active_plugins_string}\n"
f"{lang('apt_plugin_disabled')}: {disabled_plugins_string}\n"
f"{lang('apt_plugin_failed')}: {inactive_plugins_string}"
)
2022-05-23 12:40:30 +00:00
await message.edit(output)
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
elif message.parameter[0] == "enable":
if len(message.parameter) == 2:
2023-01-31 16:24:56 +00:00
if plugin_manager.enable_plugin(message.parameter[1]):
2023-03-12 03:56:01 +00:00
await message.edit(
f"{lang('apt_plugin')} {message.parameter[1]} "
f"{lang('apt_enable')}"
)
2022-05-23 12:40:30 +00:00
await log(f"{lang('apt_enable')} {message.parameter[1]}.")
2022-11-14 14:11:27 +00:00
await reload_all()
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("apt_not_exist"))
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
elif message.parameter[0] == "disable":
if len(message.parameter) == 2:
2023-01-31 16:24:56 +00:00
if plugin_manager.disable_plugin(message.parameter[1]):
2023-03-12 03:56:01 +00:00
await message.edit(
f"{lang('apt_plugin')} {message.parameter[1]} "
f"{lang('apt_disable')}"
)
2022-05-23 12:40:30 +00:00
await log(f"{lang('apt_disable')} {message.parameter[1]}.")
2022-11-14 14:11:27 +00:00
await reload_all()
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("apt_not_exist"))
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
elif message.parameter[0] == "upload":
if len(message.parameter) == 2:
file_name = f"{message.parameter[1]}.py"
reply_id = None
if reply:
reply_id = reply.id
if exists(f"{plugin_directory}{file_name}"):
copyfile(f"{plugin_directory}{file_name}", file_name)
elif exists(f"{plugin_directory}{file_name}.disabled"):
copyfile(f"{plugin_directory}{file_name}.disabled", file_name)
if exists(file_name):
try:
await message.edit(lang("apt_uploading"))
await upload_attachment(
file_name,
message.chat.id,
reply_id,
2024-02-04 07:33:01 +00:00
message_thread_id=message.message_thread_id,
thumb=f"pagermaid{sep}assets{sep}logo.jpg",
caption=f"<b>{lang('apt_name')}</b>\n\n"
f"PagerMaid-Pyro {message.parameter[1]} plugin.",
)
await message.safe_delete()
finally:
remove(file_name)
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("apt_not_exist"))
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
elif message.parameter[0] == "update":
if not exists(f"{plugin_directory}version.json"):
2023-03-12 03:56:01 +00:00
await message.edit(lang("apt_why_not_install_a_plugin"))
2022-05-23 12:40:30 +00:00
return
2023-01-31 16:24:56 +00:00
await plugin_manager.load_remote_plugins()
2023-03-12 03:56:01 +00:00
updated_plugins = [
i.name for i in await plugin_manager.update_all_remote_plugin() if i
]
2023-01-31 16:24:56 +00:00
if len(updated_plugins) == 0:
2023-03-12 03:56:01 +00:00
await message.edit(
f"<b>{lang('apt_name')}</b>\n\n"
+ lang("apt_loading_from_online_but_nothing_need_to_update")
)
2022-05-23 12:40:30 +00:00
else:
2023-01-31 16:24:56 +00:00
message = await message.edit(lang("apt_loading_from_online_and_updating"))
await message.edit(
2023-03-12 03:56:01 +00:00
f"<b>{lang('apt_name')}</b>\n\n"
+ lang("apt_reading_list")
+ "\n"
+ "".join(updated_plugins)
2023-01-31 16:24:56 +00:00
)
await reload_all()
2022-05-23 12:40:30 +00:00
elif message.parameter[0] == "search":
if len(message.parameter) == 1:
await message.edit(lang("apt_search_no_name"))
elif len(message.parameter) == 2:
await plugin_manager.load_remote_plugins()
2022-05-23 12:40:30 +00:00
search_result = []
plugin_name = message.parameter[1]
for i in plugin_manager.remote_plugins:
if search(plugin_name, i.name, I):
search_result.append(f"`{i.name}` / `{i.version}`\n {i.des_short}")
2022-05-23 12:40:30 +00:00
if len(search_result) == 0:
await message.edit(lang("apt_search_not_found"))
else:
2023-03-12 03:56:01 +00:00
await message.edit(
f"{lang('apt_search_result_hint')}:\n\n"
+ "\n\n".join(search_result)
)
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
elif message.parameter[0] == "show":
if len(message.parameter) == 1:
await message.edit(lang("apt_search_no_name"))
elif len(message.parameter) == 2:
await plugin_manager.load_remote_plugins()
2022-05-23 12:40:30 +00:00
search_result = ""
plugin_name = message.parameter[1]
for i in plugin_manager.remote_plugins:
if plugin_name == i.name:
if i.supported:
2022-05-23 12:40:30 +00:00
search_support = lang("apt_search_supporting")
else:
search_support = lang("apt_search_not_supporting")
2023-03-12 03:56:01 +00:00
search_result = (
f"{lang('apt_plugin_name')}:`{i.name}`\n"
f"{lang('apt_plugin_ver')}:`Ver {i.version}`\n"
f"{lang('apt_plugin_section')}:`{i.section}`\n"
f"{lang('apt_plugin_maintainer')}:`{i.maintainer}`\n"
f"{lang('apt_plugin_size')}:`{i.size}`\n"
2023-03-12 03:56:01 +00:00
f"{lang('apt_plugin_support')}:{search_support}\n"
f"{lang('apt_plugin_des_short')}:{i.des_short}"
2023-03-12 03:56:01 +00:00
)
2022-05-23 12:40:30 +00:00
break
2023-03-12 03:56:01 +00:00
if search_result == "":
2022-05-23 12:40:30 +00:00
await message.edit(lang("apt_search_not_found"))
else:
await message.edit(search_result)
elif message.parameter[0] == "export":
if not exists(f"{plugin_directory}version.json"):
await message.edit(lang("apt_why_not_install_a_plugin"))
return
2022-06-08 03:18:21 +00:00
message = await message.edit(lang("stats_loading"))
2022-05-23 12:40:30 +00:00
list_plugin = []
for key, value in plugin_manager.version_map.items():
if not value:
2022-05-23 12:40:30 +00:00
continue
list_plugin.append(key)
2022-05-23 12:40:30 +00:00
if len(list_plugin) == 0:
await message.edit(lang("apt_why_not_install_a_plugin"))
else:
await message.edit(",apt install " + " ".join(list_plugin))
else:
await message.edit(lang("arg_error"))
@listener(
is_plugin=False,
outgoing=True,
command="apt_source",
need_admin=True,
description=lang("apt_source_des"),
parameters=lang("apt_source_parameters"),
)
async def apt_source(message: Message):
if len(message.parameter) == 0:
remotes = plugin_remote_manager.get_remotes()
if len(remotes) == 0:
await message.edit(lang("apt_source_not_found"))
return
await message.edit(
f"{lang('apt_source_header')}\n\n" + "\n".join([i.text for i in remotes]),
disable_web_page_preview=True,
)
elif len(message.parameter) == 2:
url = message.parameter[1]
if not url.endswith("/"):
url += "/"
if message.parameter[0] == "add":
try:
status = await plugin_manager.fetch_remote_url(url)
except Exception:
status = False
if status:
if plugin_remote_manager.add_remote(url):
await message.edit(lang("apt_source_add_success"))
await plugin_manager.load_remote_plugins(enable_cache=False)
else:
await message.edit(lang("apt_source_add_failed"))
else:
await message.edit(lang("apt_source_add_invalid"))
elif message.parameter[0] == "del":
if plugin_remote_manager.remove_remote(url):
await message.edit(lang("apt_source_del_success"))
await plugin_manager.load_remote_plugins(enable_cache=False)
else:
await message.edit(lang("apt_source_del_failed"))
else:
await message.edit(lang("arg_error"))
else:
await message.edit(lang("arg_error"))