Skip to content

Commit

Permalink
fix: fix support for old userlixo plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
usernein committed Nov 2, 2023
1 parent a71719b commit b44f9d2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies = [
"psutil>=5.9.6",
"kink>=0.7.0",
"pyrogram @ git+https://github.com/AmanoTeam/Pyrogram.git@v2.0.106+amn8",
"pip>=23.3.1",
]

[tool.hatch.build]
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ typing-extensions==4.8.0
tzlocal==5.2
virtualenv==20.24.6
# The following packages are considered to be unsafe in a requirements file:
pip==23.3.1
setuptools==68.2.2
2 changes: 2 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ tgcrypto==1.2.5
tortoise-orm==0.20.0
typing-extensions==4.8.0
tzlocal==5.2
# The following packages are considered to be unsafe in a requirements file:
pip==23.3.1
11 changes: 5 additions & 6 deletions userlixo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async def unload_inactive_plugins():
print(f"The plugin {plugin_type}/{name} thrown an error: {e}")
continue
functions = [*filter(callable, module.__dict__.values())]
functions = [*filter(lambda f: hasattr(f, "modules"), functions)]
functions = [*filter(lambda f: hasattr(f, "handlers"), functions)]

for f in functions:
for handler in f.handlers:
Expand Down Expand Up @@ -196,8 +196,7 @@ def message_ikb(self):
# exist. I want to use the fallback also when the key exists but it's invalid
user = Client(
os.getenv("PYROGRAM_SESSION") or "user",
# plugins={"root": "userlixo/modules/user"},
plugins=None,
plugins={"root": "userlixo/plugins/user"},
workdir=".",
api_id=api_id,
api_hash=api_hash,
Expand All @@ -206,11 +205,11 @@ def message_ikb(self):

bot = Client(
"bot",
plugins={"root": "userlixo/plugins/bot"},
api_id=api_id,
api_hash=api_hash,
bot_token=os.getenv("BOT_TOKEN"),
workdir=".",
plugins=None,
**pyrogram_config,
)

Expand All @@ -232,9 +231,9 @@ def message_ikb(self):
cmds = {x: 1 for x in cmds_list}

plugins = {"user": {}, "bot": {}}
for file in Path("userlixo/modules").glob("**/plugins/*.py"):
for file in Path("userlixo/plugins").glob("**/*.py"):
basename = Path(file).name
plugin_type = ("user", "bot")["modules/bot/" in str(file)]
plugin_type = ("user", "bot")["/bot/" in str(file)]
if basename.startswith("__"):
continue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ async def handle_callback_query(self, _client, query: CallbackQuery):

cache_filename = query.matches[0]["filename"]
basename = Path(cache_filename).name
new_filename = "userlixo/modules/" + plugin_type + "/plugins/" + basename
new_filename = "userlixo/plugins/" + plugin_type + "/" + basename

plugin = read_plugin_info(cache_filename)
new_notation = re.sub(
"info_plugin_callback_handler.py$", "", os.path.relpath(new_filename)
).replace("/", ".")
new_notation = re.sub(".py$", "", os.path.relpath(new_filename)).replace("/", ".")

requirements = plugin.get("requirements")
if requirements:
Expand Down Expand Up @@ -85,7 +83,7 @@ async def handle_callback_query(self, _client, query: CallbackQuery):
raise e

functions = [*filter(callable, module.__dict__.values())]
functions = [*filter(lambda f: hasattr(f, "modules"), functions)]
functions = [*filter(lambda f: hasattr(f, "handlers"), functions)]

# if not len(functions):
# os.remove(new_filename)
Expand Down Expand Up @@ -132,7 +130,7 @@ async def handle_callback_query(self, _client, query: CallbackQuery):
unload = True

if unload:
functions = [*filter(lambda f: hasattr(f, "modules"), functions)]
functions = [*filter(lambda f: hasattr(f, "handlers"), functions)]

for f in functions:
for handler in f.handlers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PluginCallbackQueryController:
confirm_add_plugin_handler: ConfirmAddPluginCallbackQueryHandler

@on_callback_query(
filters.regex(r"^info_plugin (?P<basename>.+) (?P<plugin_type>user|bot) (?P<pg>\d+)")
filters.regex(r"^info_plugin (?P<basename>.+) (?P<plugin_type>user|bot) (?P<page>\d+)")
)
async def info_plugin(self, _c, callback_query):
await self.info_plugin_handler.handle_callback_query(_c, callback_query)
Expand All @@ -39,8 +39,8 @@ async def info_plugin(self, _c, callback_query):
+ r" (?P<plugin_type>user|bot) (?P<page>\d+)"
)
)
async def activate_plugin(self, _c, callback_query):
pass
async def toggle_plugin(self, _c, callback_query):
await self.toggle_plugin_handler.handle_callback_query(_c, callback_query)

@on_callback_query(
filters.regex(r"^remove_plugin (?P<basename>.+) (?P<plugin_type>user|bot) (?P<page>\d+)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def handle_callback_query(self, client, query: CallbackQuery):

basename = query.matches[0]["basename"]
plugin_type = query.matches[0]["plugin_type"]
page = query.matches[0]["page"]
page = int(query.matches[0]["page"])

if basename not in plugins[plugin_type]:
return await query.answer(lang.plugin_not_found(name=basename))
Expand All @@ -49,11 +49,12 @@ async def handle_callback_query(self, client, query: CallbackQuery):
return await query.edit(lang.plugin_could_not_load(e=e))

functions = [*filter(callable, module.__dict__.values())]
functions = [*filter(lambda f: hasattr(f, "handler"), functions)]
functions = [*filter(lambda f: hasattr(f, "handlers"), functions)]

c = (user, bot)[plugin_type == "bot"]
for f in functions:
c.remove_handler(*f.handler)
for handler in f.handlers:
c.remove_handler(*handler)
del plugins[plugin_type][basename]
Path(plugin["filename"]).unlink()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ async def handle_callback_query(self, client, query: CallbackQuery):
return await query.edit(lang.plugin_could_not_load(e=e))

functions = [*filter(callable, module.__dict__.values())]
functions = [*filter(lambda f: hasattr(f, "handler"), functions)]
functions = [*filter(lambda f: hasattr(f, "handlers"), functions)]

c = (user, bot)[plugin_type == "bot"]
for f in functions:
(c.remove_handler if deactivate else c.add_handler)(*f.handler)
for handler in f.handlers:
(c.remove_handler if deactivate else c.add_handler)(*handler)

text = lang.plugin_has_been_deactivated if deactivate else lang.plugin_has_been_activated
await query.answer(text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ async def handle_message(self, client: Client, message: Message):
return await act(lang.plugin_could_not_load(e=e))

functions = [*filter(callable, module.__dict__.values())]
functions = [*filter(lambda f: hasattr(f, "handler"), functions)]
functions = [*filter(lambda f: hasattr(f, "handlers"), functions)]

client = (user, bot)[plugin_type == "bot"]
for f in functions:
client.remove_handler(*f.handler)
for handler in f.handlers:
client.remove_handler(*handler)
del plugins[plugin_type][basename]
Path(plugin["filename"]).unlink()

Expand Down

0 comments on commit b44f9d2

Please sign in to comment.