2023-08-24 00:27:56 +02:00
|
|
|
import asyncio
|
2020-10-09 10:40:37 +02:00
|
|
|
import importlib
|
2020-10-09 23:37:35 +02:00
|
|
|
import json
|
2020-10-09 10:40:37 +02:00
|
|
|
import os
|
2022-04-26 21:33:43 +02:00
|
|
|
import signal
|
2022-08-15 14:26:09 +02:00
|
|
|
import sys
|
2023-01-30 18:21:34 +01:00
|
|
|
import time
|
2022-08-15 14:26:09 +02:00
|
|
|
import traceback
|
2020-10-09 10:40:37 +02:00
|
|
|
from pathlib import Path
|
2022-08-15 13:19:48 +02:00
|
|
|
from typing import Any, Iterable, Optional
|
2020-10-09 10:40:37 +02:00
|
|
|
|
2022-08-15 13:35:42 +02:00
|
|
|
import irc.client
|
2023-08-24 00:27:56 +02:00
|
|
|
import irc.client_aio
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
2022-08-15 13:19:48 +02:00
|
|
|
from irc.client import Connection, Event, NickMask
|
2020-10-08 18:46:45 +02:00
|
|
|
|
|
|
|
from edmond.log import Logger
|
2022-08-15 13:19:48 +02:00
|
|
|
from edmond.plugin import Plugin
|
2020-10-08 18:46:45 +02:00
|
|
|
|
|
|
|
|
2023-08-24 00:27:56 +02:00
|
|
|
class Bot(irc.client_aio.AioSimpleIRCClient, Logger):
|
2022-11-25 18:33:00 +01:00
|
|
|
"""Main class for the IRC bot: handles connection and manages plugins."""
|
2020-10-08 18:46:45 +02:00
|
|
|
|
2020-11-01 19:23:42 +01:00
|
|
|
CHANNELS_RUNTIME_KEY = "_channels"
|
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def __init__(self, config: dict, logger):
|
2020-10-08 18:46:45 +02:00
|
|
|
super().__init__()
|
2022-08-15 13:19:48 +02:00
|
|
|
self.config: dict = config
|
2020-10-08 18:46:45 +02:00
|
|
|
self.logger = logger
|
2022-08-15 13:19:48 +02:00
|
|
|
self.plugins: list[Plugin] = []
|
|
|
|
self.values: dict[str, Any] = {}
|
2022-08-15 13:35:42 +02:00
|
|
|
self.storage: dict[str, Any] = self.get_storage()
|
2023-08-24 00:27:56 +02:00
|
|
|
self.tasks: list[asyncio.Task] = []
|
2022-08-15 13:19:48 +02:00
|
|
|
self.done: bool = False
|
2023-08-24 00:27:56 +02:00
|
|
|
self.scheduler = AsyncIOScheduler()
|
2020-10-08 18:46:45 +02:00
|
|
|
|
|
|
|
@property
|
2022-08-15 13:19:48 +02:00
|
|
|
def nick(self) -> str:
|
2020-11-01 19:29:51 +01:00
|
|
|
"""Nickname validated by the server, or the configured nick."""
|
2020-10-12 15:45:31 +02:00
|
|
|
if self.connection.is_connected():
|
|
|
|
return self.connection.get_nickname()
|
2020-10-08 18:46:45 +02:00
|
|
|
return self.config["nick"]
|
|
|
|
|
2020-10-09 10:40:37 +02:00
|
|
|
@property
|
2022-08-15 13:19:48 +02:00
|
|
|
def names(self) -> Iterable[str]:
|
2020-11-01 19:29:51 +01:00
|
|
|
"""Collection of names the bot should identify with."""
|
2020-10-12 15:45:31 +02:00
|
|
|
return (self.nick, *self.config["alternative_nicks"])
|
2020-10-09 10:40:37 +02:00
|
|
|
|
2020-11-01 19:23:42 +01:00
|
|
|
@property
|
2022-08-15 13:19:48 +02:00
|
|
|
def channels(self) -> list[str]:
|
2020-11-01 19:23:42 +01:00
|
|
|
"""List of joined channels."""
|
|
|
|
if self.CHANNELS_RUNTIME_KEY not in self.values:
|
|
|
|
self.values[self.CHANNELS_RUNTIME_KEY] = []
|
|
|
|
return self.values[self.CHANNELS_RUNTIME_KEY]
|
|
|
|
|
2022-08-15 13:35:42 +02:00
|
|
|
def get_storage(self) -> dict:
|
2020-10-09 23:37:35 +02:00
|
|
|
"""Load data from storage."""
|
|
|
|
try:
|
|
|
|
with open(self.config["storage_file"], "rt") as storage_file:
|
|
|
|
storage = json.load(storage_file)
|
|
|
|
self.log_d("Loaded storage file.")
|
|
|
|
return storage
|
|
|
|
except (OSError, json.decoder.JSONDecodeError) as exc:
|
|
|
|
self.log_e(f"Could not load storage file: {exc}")
|
2022-08-09 23:47:28 +02:00
|
|
|
self.log_w(
|
|
|
|
"If it's not the first time Edm0nd is run, you may lose"
|
|
|
|
" data when closing the program."
|
|
|
|
)
|
2020-10-09 23:37:35 +02:00
|
|
|
return {}
|
|
|
|
|
2022-08-15 13:35:42 +02:00
|
|
|
def save_storage(self) -> None:
|
2020-10-09 23:37:35 +02:00
|
|
|
"""Save storage data to disk."""
|
|
|
|
try:
|
|
|
|
with open(self.config["storage_file"], "wt") as storage_file:
|
|
|
|
json.dump(self.storage, storage_file, indent=2, sort_keys=True)
|
|
|
|
self.log_d("Saved storage file.")
|
2022-07-05 23:35:58 +02:00
|
|
|
except OSError as exc:
|
2020-10-09 23:37:35 +02:00
|
|
|
self.log_e(f"Could not save storage file: {exc}")
|
|
|
|
|
2023-08-24 00:27:56 +02:00
|
|
|
def handle_task(self, coro):
|
|
|
|
"""Schedule a task in the event loop. Keep a reference to cancel it."""
|
|
|
|
task = self.connection.reactor.loop.create_task(coro)
|
|
|
|
self.tasks.append(task)
|
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def on_welcome(self, connection: Connection, event: Event):
|
2020-11-01 19:29:51 +01:00
|
|
|
"""Handle a successful connection to a server."""
|
2020-10-08 18:46:45 +02:00
|
|
|
self.log_i(f"Connected to server {event.source}.")
|
2020-10-09 12:19:58 +02:00
|
|
|
self.run_plugin_callbacks(event)
|
2020-10-08 18:46:45 +02:00
|
|
|
for channel in self.config["channels"]:
|
|
|
|
connection.join(channel)
|
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def on_join(self, connection: Connection, event: Event):
|
2020-11-01 19:29:51 +01:00
|
|
|
"""Handle someone, possibly the bot, joining a channel."""
|
2020-10-12 15:45:31 +02:00
|
|
|
if event.source.nick == self.nick:
|
|
|
|
self.log_i(f"Joined {event.target}.")
|
2020-11-01 19:23:42 +01:00
|
|
|
self.channels.append(event.target)
|
2023-08-24 00:27:56 +02:00
|
|
|
self.scheduler.add_job(self.flubiz, 'cron', hour='1',
|
|
|
|
minute='52')
|
|
|
|
self.scheduler.start()
|
2020-10-09 10:40:37 +02:00
|
|
|
self.run_plugin_callbacks(event)
|
2020-10-08 18:46:45 +02:00
|
|
|
|
2023-08-24 00:27:56 +02:00
|
|
|
async def flubiz(self):
|
|
|
|
self.say('#idi0crates', "acab")
|
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def on_part(self, connection: Connection, event: Event):
|
2020-11-01 19:29:51 +01:00
|
|
|
"""Handle someone, possibly the bot, leaving a channel."""
|
2020-10-12 15:45:31 +02:00
|
|
|
if event.source.nick == self.nick:
|
|
|
|
self.log_i(f"Left {event.target} (args: {event.arguments[0]}).")
|
2020-11-01 19:23:42 +01:00
|
|
|
self.channels.remove(event.target)
|
2020-10-09 10:40:37 +02:00
|
|
|
self.run_plugin_callbacks(event)
|
2020-10-08 18:46:45 +02:00
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def on_pubmsg(self, connection: Connection, event: Event):
|
2020-11-01 19:29:51 +01:00
|
|
|
"""Handle a message received in a channel."""
|
2020-10-08 18:46:45 +02:00
|
|
|
channel = event.target
|
|
|
|
nick = NickMask(event.source).nick
|
|
|
|
message = event.arguments[0]
|
|
|
|
self.log_d(f"Message in {channel} from {nick}: {message}")
|
2020-10-09 10:40:37 +02:00
|
|
|
self.run_plugin_callbacks(event)
|
2020-10-08 18:46:45 +02:00
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def on_privmsg(self, connection: Connection, event: Event):
|
2022-07-07 19:06:59 +02:00
|
|
|
"""Handle a message received privately, usually like a channel msg."""
|
2020-10-08 18:46:45 +02:00
|
|
|
nick = NickMask(event.source).nick
|
|
|
|
target = event.target
|
|
|
|
message = event.arguments[0]
|
|
|
|
self.log_d(f"Private message from {nick} to {target}: {message}")
|
2020-10-09 10:40:37 +02:00
|
|
|
self.run_plugin_callbacks(event)
|
2020-10-08 18:46:45 +02:00
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def on_ping(self, connection: Connection, event: Event):
|
2020-11-01 19:27:12 +01:00
|
|
|
"""Handle a ping; can be used as a random event timer."""
|
|
|
|
self.log_d(f"Received ping from {event.target}.")
|
|
|
|
self.run_plugin_callbacks(event)
|
|
|
|
|
2020-10-08 18:46:45 +02:00
|
|
|
def run(self):
|
2020-10-09 10:40:37 +02:00
|
|
|
"""Connect the bot to server, join channels and start responding."""
|
2020-10-08 18:46:45 +02:00
|
|
|
self.log_i("Starting Edmond.")
|
2020-10-09 10:40:37 +02:00
|
|
|
self.load_plugins()
|
2023-08-24 00:27:56 +02:00
|
|
|
self.log_i("Connecting to server…")
|
|
|
|
|
2022-04-26 21:33:43 +02:00
|
|
|
signal.signal(signal.SIGTERM, self.handle_sigterm)
|
2020-10-08 18:46:45 +02:00
|
|
|
try:
|
2023-08-24 00:27:56 +02:00
|
|
|
self.connect(self.config["host"], self.config["port"], self.nick)
|
2020-10-08 18:46:45 +02:00
|
|
|
self.start()
|
2023-08-24 00:27:56 +02:00
|
|
|
except irc.client.ServerConnectionError as exc:
|
|
|
|
self.log_c(f"Connection failed: {exc}")
|
2020-10-08 18:46:45 +02:00
|
|
|
except KeyboardInterrupt:
|
2021-06-11 11:28:22 +02:00
|
|
|
self.log_i("Caught keyboard interrupt.")
|
2022-08-15 14:26:09 +02:00
|
|
|
except Exception as exc:
|
2022-11-29 12:59:05 +01:00
|
|
|
self.log_c(f"Caught unhandled {type(exc).__name__}: {exc}")
|
2022-08-15 14:26:09 +02:00
|
|
|
_, _, exc_traceback = sys.exc_info()
|
|
|
|
for line in traceback.format_tb(exc_traceback):
|
|
|
|
self.log_d(line.rstrip())
|
2021-06-11 11:28:22 +02:00
|
|
|
finally:
|
2022-04-26 21:33:43 +02:00
|
|
|
self.cleanup()
|
2020-10-09 10:40:37 +02:00
|
|
|
|
|
|
|
def load_plugins(self):
|
|
|
|
"""Load all installed plugins."""
|
2023-08-24 00:27:56 +02:00
|
|
|
self.log_i("Loading plugins…")
|
2020-10-09 10:40:37 +02:00
|
|
|
plugin_files = os.listdir(Path(__file__).parent / "plugins")
|
|
|
|
plugin_names = map(
|
|
|
|
lambda f: os.path.splitext(f)[0],
|
2022-08-15 13:20:25 +02:00
|
|
|
filter(
|
|
|
|
lambda f: f.endswith(".py") and f != "__init__.py",
|
|
|
|
plugin_files,
|
|
|
|
),
|
2020-10-09 10:40:37 +02:00
|
|
|
)
|
|
|
|
for plugin_name in plugin_names:
|
|
|
|
module = importlib.import_module(f"edmond.plugins.{plugin_name}")
|
2020-10-09 22:35:04 +02:00
|
|
|
# Get plugin class name from its module name.
|
2022-08-09 23:47:28 +02:00
|
|
|
class_name = (
|
|
|
|
"".join(map(lambda w: w.capitalize(), plugin_name.split("_")))
|
|
|
|
+ "Plugin"
|
|
|
|
)
|
2020-10-09 10:40:37 +02:00
|
|
|
plugin_class = getattr(module, class_name)
|
|
|
|
self.plugins.append(plugin_class(self))
|
2020-10-09 12:19:58 +02:00
|
|
|
self.values[plugin_name] = {}
|
2020-10-09 10:40:37 +02:00
|
|
|
self.log_d(f"Loaded {class_name}.")
|
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def get_plugin(self, name: str) -> Optional[Plugin]:
|
2020-11-03 17:28:26 +01:00
|
|
|
"""Get a loaded plugin by its name (e.g. 'mood'), or None."""
|
2022-09-11 23:14:25 +02:00
|
|
|
matching_plugins = filter(
|
|
|
|
lambda plugin: plugin.name == name,
|
|
|
|
self.plugins,
|
|
|
|
)
|
2022-08-15 13:19:48 +02:00
|
|
|
return next(matching_plugins, None)
|
2020-11-03 17:28:26 +01:00
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def say(self, target: str, message: str) -> None:
|
2020-10-09 10:40:37 +02:00
|
|
|
"""Send message to target after a slight delay."""
|
2021-12-02 11:35:02 +01:00
|
|
|
message = message.replace("\n", " ").replace("\r", " ")
|
2020-10-09 10:40:37 +02:00
|
|
|
time.sleep(self.config["speak_delay"])
|
|
|
|
self.log_d(f"Sending to {target}: {message}")
|
2021-09-13 09:04:18 +02:00
|
|
|
try:
|
|
|
|
if message.startswith("/me "):
|
|
|
|
self.connection.action(target, message[4:])
|
|
|
|
else:
|
|
|
|
self.connection.privmsg(target, message)
|
|
|
|
except irc.client.MessageTooLong:
|
|
|
|
self.log_e("Could not send, message is too long.")
|
2020-10-09 10:40:37 +02:00
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def run_plugin_callbacks(self, event: Event) -> None:
|
2020-10-09 23:37:35 +02:00
|
|
|
"""Run appropriate callbacks for each plugin."""
|
2020-10-09 10:40:37 +02:00
|
|
|
etype = event.type
|
2022-08-15 13:19:48 +02:00
|
|
|
ready_plugins = filter(lambda p: p.is_ready, self.plugins)
|
|
|
|
plugins = sorted(ready_plugins, key=lambda p: p.priority, reverse=True)
|
2020-11-03 16:22:57 +01:00
|
|
|
for plugin in plugins:
|
2020-10-09 10:40:37 +02:00
|
|
|
callbacks = plugin.callbacks
|
|
|
|
if etype not in callbacks:
|
|
|
|
continue
|
2020-11-02 16:44:35 +01:00
|
|
|
if callbacks[etype](event):
|
|
|
|
break
|
2022-04-26 21:33:43 +02:00
|
|
|
|
|
|
|
def handle_sigterm(self, *args):
|
|
|
|
"""Handle SIGTERM (keyboard interrupt, systemd stop, etc)."""
|
|
|
|
self.cleanup()
|
2022-05-16 12:02:29 +02:00
|
|
|
exit("Exiting after received SIGTERM.")
|
2022-04-26 21:33:43 +02:00
|
|
|
|
2022-08-15 13:19:48 +02:00
|
|
|
def cleanup(self) -> None:
|
2022-04-26 21:33:43 +02:00
|
|
|
"""Save the storage file and close the connection. Run only once."""
|
|
|
|
if self.done:
|
|
|
|
return
|
|
|
|
self.log_i("Stopping Edmond.")
|
2023-08-24 00:27:56 +02:00
|
|
|
self.save_storage() # FIRST THINGS FIRST
|
|
|
|
|
|
|
|
for task in self.tasks:
|
|
|
|
if not task.cancelled():
|
|
|
|
self.log_d(f"Cancelling task {task.get_name()}…")
|
|
|
|
task.cancel()
|
2022-04-26 21:33:43 +02:00
|
|
|
if self.connection.is_connected():
|
|
|
|
self.connection.close()
|
2023-08-24 00:27:56 +02:00
|
|
|
self.reactor.loop.close()
|
2022-04-26 21:33:43 +02:00
|
|
|
self.done = True
|