-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_system.py
More file actions
422 lines (363 loc) · 16.1 KB
/
Copy pathplugin_system.py
File metadata and controls
422 lines (363 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
from __future__ import annotations
import importlib
import sys
import threading
from dataclasses import dataclass, field
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
from typing import Any, Callable, TYPE_CHECKING
if TYPE_CHECKING:
from bot import IRCBot
@dataclass(frozen=True)
class MessageContext:
source_nick: str
source_ident: str
source_host: str
source_mask: str
target: str
message: str
reply_target: str
command_prefix: str
is_private_message: bool
CommandHandler = Callable[["IRCBot", MessageContext, str], None]
MessageHandler = Callable[["IRCBot", MessageContext], None]
TickHandler = Callable[["IRCBot"], None]
HelpVisibilityHandler = Callable[["IRCBot", MessageContext], bool]
@dataclass(frozen=True)
class CommandSpec:
canonical: str
handler: CommandHandler
aliases: tuple[str, ...] = ()
primary_names: dict[str, str] = field(default_factory=dict)
help_args: dict[str, str] = field(default_factory=dict)
help_texts: dict[str, str] = field(default_factory=dict)
help_visible: HelpVisibilityHandler | None = None
help_sort: int = 100
def all_aliases(self) -> tuple[str, ...]:
values = [self.canonical, *self.aliases]
seen: set[str] = set()
normalized: list[str] = []
for value in values:
alias = value.strip().lower()
if not alias or alias in seen:
continue
seen.add(alias)
normalized.append(alias)
return tuple(normalized)
def primary_name(self, language: str) -> str:
return self.primary_names.get(language, self.primary_names.get("en", self.canonical))
def help_arg(self, language: str) -> str:
return self.help_args.get(language, self.help_args.get("en", "")).strip()
def help_text(self, language: str) -> str:
return self.help_texts.get(language, self.help_texts.get("en", "")).strip()
@dataclass(frozen=True)
class MessageHandlerSpec:
handler: MessageHandler
@dataclass(frozen=True)
class TickHandlerSpec:
handler: TickHandler
@dataclass(frozen=True)
class PluginSpec:
name: str
aliases: tuple[str, ...] = ()
commands: tuple[CommandSpec, ...] = ()
message_handlers: tuple[MessageHandlerSpec, ...] = ()
tick_handlers: tuple[TickHandlerSpec, ...] = ()
translations: dict[str, dict[str, str]] = field(default_factory=dict)
enabled_by_default: bool = True
hooks: dict[str, Callable[..., Any]] | None = None
on_config_loaded: Callable[..., Any] | None = None
class PluginManager:
def __init__(self, bot: "IRCBot", plugins_dir: Path) -> None:
self.bot = bot
self.plugins_dir = plugins_dir.resolve()
self._commands_by_canonical: dict[str, CommandSpec] = {}
self._commands_by_alias: dict[str, CommandSpec] = {}
self._message_handlers: list[MessageHandler] = []
self._tick_handlers: list[TickHandler] = []
self._loaded_plugins: list[str] = []
self._translations: dict[str, dict[str, str]] = {}
self._plugins_by_name: dict[str, PluginSpec] = {}
self._state_lock = threading.RLock()
self.load_plugins()
@property
def loaded_plugins(self) -> tuple[str, ...]:
with self._state_lock:
return tuple(self._loaded_plugins)
def command_aliases(self) -> dict[str, list[str]]:
with self._state_lock:
return {name: list(spec.all_aliases()) for name, spec in self._commands_by_canonical.items()}
def primary_command_name(self, canonical: str, language: str) -> str:
with self._state_lock:
spec = self._commands_by_canonical.get(canonical)
if spec is None:
return canonical
return spec.primary_name(language)
def translation(self, key: str, language: str) -> str | None:
with self._state_lock:
return self._translations.get(language, {}).get(key)
def resolve_command(self, token: str) -> CommandSpec | None:
with self._state_lock:
return self._commands_by_alias.get(token.strip().lower())
def build_help_entries(self, prefix: str, language: str, context: MessageContext | None = None) -> tuple[str, ...]:
rendered: list[str] = []
with self._state_lock:
ordered = sorted(self._commands_by_canonical.values(), key=lambda spec: (spec.help_sort, spec.canonical))
for spec in ordered:
if context is not None and spec.help_visible is not None and not spec.help_visible(self.bot, context):
continue
command_name = spec.primary_name(language)
help_arg = spec.help_arg(language)
help_text = spec.help_text(language)
line = f"{prefix}{command_name} {help_arg}".rstrip()
if help_text:
line = f"{line} - {help_text}"
rendered.append(line)
return tuple(rendered)
def get_hooks(self, name: str) -> tuple[Callable[..., Any], ...]:
with self._state_lock:
hooks = []
for plugin in self._plugins_by_name.values():
plugin_hooks = plugin.hooks
if plugin_hooks:
hook = plugin_hooks.get(name)
if hook is not None:
hooks.append(hook)
return tuple(hooks)
def get_hook(self, name: str) -> Callable[..., Any] | None:
with self._state_lock:
for plugin in self._plugins_by_name.values():
plugin_hooks = plugin.hooks
if plugin_hooks:
hook = plugin_hooks.get(name)
if hook is not None:
return hook
return None
def call_config_hooks(self, bot) -> None:
raw_config = getattr(bot, "raw_config", {})
with self._state_lock:
for plugin in self._plugins_by_name.values():
hook = plugin.on_config_loaded
if hook is not None:
try:
hook(bot, raw_config)
except Exception:
pass
def call_hooks(self, name: str, *args, **kwargs) -> None:
with self._state_lock:
for plugin in self._plugins_by_name.values():
plugin_hooks = plugin.hooks
if plugin_hooks:
hook = plugin_hooks.get(name)
if hook is not None:
try:
hook(*args, **kwargs)
except Exception:
pass
def handle_privmsg(self, context: MessageContext) -> None:
if not self.bot.public_triggers_enabled():
return
with self._state_lock:
message_handlers = tuple(self._message_handlers)
for handler in message_handlers:
handler(self.bot, context)
if not context.message.startswith(context.command_prefix):
return
cmdline = context.message[len(context.command_prefix) :].strip()
if not cmdline:
return
parts = cmdline.split(maxsplit=1)
token = parts[0].lower()
arg = parts[1] if len(parts) > 1 else ""
with self._state_lock:
command = self._commands_by_alias.get(token)
if command is None:
return
command.handler(self.bot, context, arg)
def handle_tick(self) -> None:
with self._state_lock:
tick_handlers = tuple(self._tick_handlers)
for handler in tick_handlers:
handler(self.bot)
def _reset_plugin_runtime_state(self) -> None:
# Clear plugin-specific in-memory state so reload starts clean.
explicit_attrs = {
"_url_service",
}
with self._state_lock:
loaded_plugins_snapshot = tuple(self._loaded_plugins)
plugin_prefixes = {f"_{name.strip().lower().replace('-', '_')}_" for name in loaded_plugins_snapshot if name.strip()}
prefixed_attrs = tuple(
name
for name in vars(self.bot)
if any(name.startswith(prefix) for prefix in plugin_prefixes)
)
for attr_name in (*explicit_attrs, *prefixed_attrs):
if hasattr(self.bot, attr_name):
delattr(self.bot, attr_name)
def reload_plugins(self) -> None:
"""Entlädt alle Plugin-Module aus sys.modules und lädt sie erneut."""
self._reset_plugin_runtime_state()
self._unload_plugin_modules()
self.load_plugins()
def _is_inside_plugins_dir(self, module_file: str | None) -> bool:
if not module_file:
return False
try:
module_path = Path(module_file).resolve()
except OSError:
return False
try:
module_path.relative_to(self.plugins_dir)
return True
except ValueError:
return False
def _unload_plugin_modules(self) -> None:
# Ensure file system changes are seen before next import.
importlib.invalidate_caches()
module_names_to_unload: set[str] = set()
for module_name, module in sys.modules.items():
if module_name.startswith("ircbot_plugin_"):
module_names_to_unload.add(module_name)
continue
if module_name == "plugins" or module_name.startswith("plugins."):
module_file = getattr(module, "__file__", None)
if self._is_inside_plugins_dir(module_file):
module_names_to_unload.add(module_name)
for module_name in module_names_to_unload:
sys.modules.pop(module_name, None)
def _load_plugin_spec(self, plugin_path: Path) -> PluginSpec:
if plugin_path.parent.name == "plugins":
module_name = f"ircbot_plugin_{plugin_path.stem.lower()}"
else:
module_name = f"ircbot_plugin_{plugin_path.parent.name.lower()}"
spec = spec_from_file_location(module_name, plugin_path)
if spec is None or spec.loader is None:
raise RuntimeError(f"Plugin {plugin_path.parent.name} konnte nicht geladen werden.")
module = module_from_spec(spec)
sys.modules[module_name] = module
try:
spec.loader.exec_module(module)
except Exception:
sys.modules.pop(module_name, None)
raise
plugin = getattr(module, "PLUGIN", None)
if not isinstance(plugin, PluginSpec):
raise TypeError(f"Plugin {plugin_path.parent.name} exportiert kein PluginSpec in PLUGIN.")
return plugin
@staticmethod
def _plugin_is_enabled(
plugin_path: Path,
plugin: PluginSpec,
enabled_plugins: set[str],
disabled_plugins: set[str],
) -> bool:
plugin_name = plugin.name.strip().lower()
if plugin_path.parent.name == "plugins":
plugin_aliases = {plugin_path.stem.lower()}
else:
plugin_aliases = {
alias.strip().lower()
for alias in (plugin_path.parent.name, *plugin.aliases)
if alias.strip()
}
plugin_aliases.add(plugin_name)
if enabled_plugins:
return bool(plugin_aliases & enabled_plugins)
return plugin.enabled_by_default and not (plugin_aliases & disabled_plugins)
def load_plugins(self) -> None:
if not self.plugins_dir.exists():
with self._state_lock:
self._commands_by_canonical.clear()
self._commands_by_alias.clear()
self._message_handlers.clear()
self._tick_handlers.clear()
self._loaded_plugins.clear()
self._translations.clear()
self._plugins_by_name.clear()
return
enabled_plugins = {name.strip().lower() for name in (self.bot.config.enabled_plugins or []) if name.strip()}
disabled_plugins = {name.strip().lower() for name in (self.bot.config.disabled_plugins or []) if name.strip()}
next_commands_by_canonical: dict[str, CommandSpec] = {}
next_commands_by_alias: dict[str, CommandSpec] = {}
next_message_handlers: list[MessageHandler] = []
next_tick_handlers: list[TickHandler] = []
next_loaded_plugins: list[str] = []
next_translations: dict[str, dict[str, str]] = {}
next_plugins_by_name: dict[str, PluginSpec] = {}
def _register_loaded_plugin(plugin_path: Path, plugin: PluginSpec) -> None:
plugin_name = plugin.name.strip().lower()
self._register_plugin(
plugin,
commands_by_canonical=next_commands_by_canonical,
commands_by_alias=next_commands_by_alias,
message_handlers=next_message_handlers,
tick_handlers=next_tick_handlers,
translation_catalogs=next_translations,
)
next_loaded_plugins.append(plugin_name)
next_plugins_by_name[plugin_name] = plugin
for plugin_path in sorted(self.plugins_dir.glob("*/plugin.py")):
try:
plugin = self._load_plugin_spec(plugin_path)
except Exception:
continue
if not self._plugin_is_enabled(plugin_path, plugin, enabled_plugins, disabled_plugins):
continue
_register_loaded_plugin(plugin_path, plugin)
for plugin_path in sorted(self.plugins_dir.glob("*.py")):
if plugin_path.name.startswith("_"):
continue
try:
plugin = self._load_plugin_spec(plugin_path)
except Exception:
continue
if not self._plugin_is_enabled(plugin_path, plugin, enabled_plugins, disabled_plugins):
continue
_register_loaded_plugin(plugin_path, plugin)
with self._state_lock:
self._commands_by_canonical = next_commands_by_canonical
self._commands_by_alias = next_commands_by_alias
self._message_handlers = next_message_handlers
self._tick_handlers = next_tick_handlers
self._loaded_plugins = next_loaded_plugins
self._translations = next_translations
self._plugins_by_name = next_plugins_by_name
def _register_plugin(
self,
plugin: PluginSpec,
commands_by_canonical: dict[str, CommandSpec],
commands_by_alias: dict[str, CommandSpec],
message_handlers: list[MessageHandler],
tick_handlers: list[TickHandler],
translation_catalogs: dict[str, dict[str, str]],
) -> None:
self._register_translations(plugin, translation_catalogs)
self._register_commands(plugin, commands_by_canonical, commands_by_alias)
for message_handler in plugin.message_handlers:
message_handlers.append(message_handler.handler)
for tick_handler in plugin.tick_handlers:
tick_handlers.append(tick_handler.handler)
def _register_translations(self, plugin: PluginSpec, translation_catalogs: dict[str, dict[str, str]]) -> None:
for language, plugin_catalog in plugin.translations.items():
catalog = translation_catalogs.setdefault(language, {})
for key, value in plugin_catalog.items():
existing = catalog.get(key)
if existing is not None and existing != value:
raise ValueError(f"Übersetzungsschlüssel {key} kollidiert in Sprache {language}.")
catalog[key] = value
def _register_commands(
self,
plugin: PluginSpec,
commands_by_canonical: dict[str, CommandSpec],
commands_by_alias: dict[str, CommandSpec],
) -> None:
for command in plugin.commands:
if command.canonical in commands_by_canonical:
raise ValueError(f"Befehl {command.canonical} wurde mehrfach registriert.")
commands_by_canonical[command.canonical] = command
for alias in command.all_aliases():
existing = commands_by_alias.get(alias)
if existing is not None:
raise ValueError(f"Alias {alias} kollidiert mit {existing.canonical}.")
commands_by_alias[alias] = command