Compare commits

..

No commits in common. "417773016462a786952ee654545a86a8ab75dbe0" and "895b0dec47936a5ce17deb6d2de46f0fbcab250d" have entirely different histories.

10 changed files with 124 additions and 209 deletions

View file

@ -9,7 +9,7 @@ ADD edmond /app/edmond
WORKDIR /app
RUN pip install requests irc
RUN pip install -r plugins-requirements.txt
RUN pip install -r edmond/plugins/requirements.txt
RUN groupadd --gid ${GID} edmond
RUN useradd --uid ${UID} --gid edmond \

View file

@ -8,6 +8,7 @@ python-lsp-server = "*"
rope = "*"
pyflakes = "*"
mccabe = "*"
pycodestyle = "*"
black = "*"
python-lsp-black = "*"
mypy = "*"

83
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "0c289dec5d2be30e6e49a067f62cefe5f00942be835168cbe8d735a271412b46"
"sha256": "7efa513c27dbd60153dad1bbd870d6ee19d085a5daaf7b4a356f98caab790fd3"
},
"pipfile-spec": 6,
"requires": {},
@ -14,13 +14,6 @@
]
},
"default": {
"autocommand": {
"hashes": [
"sha256:85d03044c2a1fc1c7844ac41545045927aecde0cbaf8ea28b88e0cd8588ce5d3",
"sha256:fed420e9d02745821a782971b583c6970259ee0b229be2a0a401e1467a4f170f"
],
"version": "==2.2.1"
},
"certifi": {
"hashes": [
"sha256:84c85a9078b11105f04f3036a9482ae10e4621616db313fe045dd24743a0820d",
@ -42,13 +35,6 @@
],
"version": "==3.3"
},
"inflect": {
"hashes": [
"sha256:0bc1516ec2725e2d8221707a612245093cb6f1cea209cfd8cbd4fc5e96fa6365",
"sha256:e3b85d65a296843268f35f4136283ad7c012a129375db1529d49b4b01ecb400b"
],
"version": "==6.0.0"
},
"irc": {
"hashes": [
"sha256:5114aee1247ff634abed80f3349d75ccab35616a4b76be2a25add7ef189db0e5",
@ -101,10 +87,10 @@
},
"jaraco.text": {
"hashes": [
"sha256:4b2dc3678994de601e3e5b6f554eeb8445651b7ab984893c5e9ac44564033077",
"sha256:810bc4aee7329e30bec8c6c6bab1b6ef149359ca4b98928e42178345ed7d7bd8"
"sha256:450957c3f8fb9a553d9d3e60738733ab1c5cc27b36a463342adb937e9a70ab3e",
"sha256:e4418d632425d741b8f9128cdf8fd9c0c878dc1450a7430dbd4bf6296eeac915"
],
"version": "==3.9.0"
"version": "==3.8.1"
},
"more-itertools": {
"hashes": [
@ -113,52 +99,12 @@
],
"version": "==8.14.0"
},
"pydantic": {
"hashes": [
"sha256:1061c6ee6204f4f5a27133126854948e3b3d51fcc16ead2e5d04378c199b2f44",
"sha256:19b5686387ea0d1ea52ecc4cffb71abb21702c5e5b2ac626fd4dbaa0834aa49d",
"sha256:2bd446bdb7755c3a94e56d7bdfd3ee92396070efa8ef3a34fab9579fe6aa1d84",
"sha256:328558c9f2eed77bd8fffad3cef39dbbe3edc7044517f4625a769d45d4cf7555",
"sha256:32e0b4fb13ad4db4058a7c3c80e2569adbd810c25e6ca3bbd8b2a9cc2cc871d7",
"sha256:3ee0d69b2a5b341fc7927e92cae7ddcfd95e624dfc4870b32a85568bd65e6131",
"sha256:4aafd4e55e8ad5bd1b19572ea2df546ccace7945853832bb99422a79c70ce9b8",
"sha256:4b3946f87e5cef3ba2e7bd3a4eb5a20385fe36521d6cc1ebf3c08a6697c6cfb3",
"sha256:4de71c718c9756d679420c69f216776c2e977459f77e8f679a4a961dc7304a56",
"sha256:5565a49effe38d51882cb7bac18bda013cdb34d80ac336428e8908f0b72499b0",
"sha256:5803ad846cdd1ed0d97eb00292b870c29c1f03732a010e66908ff48a762f20e4",
"sha256:5da164119602212a3fe7e3bc08911a89db4710ae51444b4224c2382fd09ad453",
"sha256:615661bfc37e82ac677543704437ff737418e4ea04bef9cf11c6d27346606044",
"sha256:78a4d6bdfd116a559aeec9a4cfe77dda62acc6233f8b56a716edad2651023e5e",
"sha256:7d0f183b305629765910eaad707800d2f47c6ac5bcfb8c6397abdc30b69eeb15",
"sha256:7ead3cd020d526f75b4188e0a8d71c0dbbe1b4b6b5dc0ea775a93aca16256aeb",
"sha256:84d76ecc908d917f4684b354a39fd885d69dd0491be175f3465fe4b59811c001",
"sha256:8cb0bc509bfb71305d7a59d00163d5f9fc4530f0881ea32c74ff4f74c85f3d3d",
"sha256:91089b2e281713f3893cd01d8e576771cd5bfdfbff5d0ed95969f47ef6d676c3",
"sha256:9c9e04a6cdb7a363d7cb3ccf0efea51e0abb48e180c0d31dca8d247967d85c6e",
"sha256:a8c5360a0297a713b4123608a7909e6869e1b56d0e96eb0d792c27585d40757f",
"sha256:afacf6d2a41ed91fc631bade88b1d319c51ab5418870802cedb590b709c5ae3c",
"sha256:b34ba24f3e2d0b39b43f0ca62008f7ba962cff51efa56e64ee25c4af6eed987b",
"sha256:bd67cb2c2d9602ad159389c29e4ca964b86fa2f35c2faef54c3eb28b4efd36c8",
"sha256:c0f5e142ef8217019e3eef6ae1b6b55f09a7a15972958d44fbd228214cede567",
"sha256:cdb4272678db803ddf94caa4f94f8672e9a46bae4a44f167095e4d06fec12979",
"sha256:d70916235d478404a3fa8c997b003b5f33aeac4686ac1baa767234a0f8ac2326",
"sha256:d8ce3fb0841763a89322ea0432f1f59a2d3feae07a63ea2c958b2315e1ae8adb",
"sha256:e0b214e57623a535936005797567231a12d0da0c29711eb3514bc2b3cd008d0f",
"sha256:e631c70c9280e3129f071635b81207cad85e6c08e253539467e4ead0e5b219aa",
"sha256:e78578f0c7481c850d1c969aca9a65405887003484d24f6110458fb02cca7747",
"sha256:f0ca86b525264daa5f6b192f216a0d1e860b7383e3da1c65a1908f9c02f42801",
"sha256:f1a68f4f65a9ee64b6ccccb5bf7e17db07caebd2730109cb8a95863cfa9c4e55",
"sha256:fafe841be1103f340a24977f61dee76172e4ae5f647ab9e7fd1e1fca51524f08",
"sha256:ff68fc85355532ea77559ede81f35fff79a6a5543477e168ab3a381887caea76"
],
"version": "==1.9.2"
},
"pytz": {
"hashes": [
"sha256:220f481bdafa09c3955dfbdddb7b57780e9a94f5127e35456a48589b9e0c0197",
"sha256:cea221417204f2d1a2aa03ddae3e867921971d0d76f14d87abb4414415bbdcf5"
"sha256:1e760e2fe6a8163bc0b3d9a19c4f84342afa0a2affebfaa84b01b978a02ecaa7",
"sha256:e68985985296d9a66a881eb3193b0906246245294a881e7c8afe623866ac6a5c"
],
"version": "==2022.2.1"
"version": "==2022.1"
},
"requests": {
"hashes": [
@ -175,13 +121,6 @@
],
"version": "==5.0.2"
},
"typing-extensions": {
"hashes": [
"sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02",
"sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6"
],
"version": "==4.3.0"
},
"urllib3": {
"hashes": [
"sha256:c33ccba33c819596124764c23a97d25f32b28433ba0dedeb77d873a38722c9bc",
@ -313,6 +252,14 @@
],
"version": "==1.0.0"
},
"pycodestyle": {
"hashes": [
"sha256:2c9607871d58c76354b697b42f5d57e1ada7d261c261efac224b664affdc5785",
"sha256:d1735fc58b418fd7c5f658d28d943854f8a849b01a5d0a1e6f3f3fdd0166804b"
],
"index": "pypi",
"version": "==2.9.1"
},
"pyflakes": {
"hashes": [
"sha256:4579f67d887f804e67edb544428f264b7b24f435b263c4614f384135cea553d2",

View file

@ -4,48 +4,46 @@ import os
import time
import signal
from pathlib import Path
from typing import Any, Iterable, Optional
import irc.client
from irc.client import Connection, Event, NickMask
import irc.client # type: ignore
from irc.client import NickMask
from edmond.log import Logger
from edmond.plugin import Plugin
class Bot(irc.client.SimpleIRCClient, Logger):
CHANNELS_RUNTIME_KEY = "_channels"
def __init__(self, config: dict, logger):
def __init__(self, config, logger):
super().__init__()
self.config: dict = config
self.config = config
self.logger = logger
self.plugins: list[Plugin] = []
self.values: dict[str, Any] = {}
self.storage: dict[str, Any] = self.get_storage()
self.done: bool = False
self.plugins = []
self.values = {}
self.storage = self.__get_storage()
self.done = False
@property
def nick(self) -> str:
def nick(self):
"""Nickname validated by the server, or the configured nick."""
if self.connection.is_connected():
return self.connection.get_nickname()
return self.config["nick"]
@property
def names(self) -> Iterable[str]:
def names(self):
"""Collection of names the bot should identify with."""
return (self.nick, *self.config["alternative_nicks"])
@property
def channels(self) -> list[str]:
def channels(self):
"""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]
def get_storage(self) -> dict:
def __get_storage(self):
"""Load data from storage."""
try:
with open(self.config["storage_file"], "rt") as storage_file:
@ -60,7 +58,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
)
return {}
def save_storage(self) -> None:
def __save_storage(self):
"""Save storage data to disk."""
try:
with open(self.config["storage_file"], "wt") as storage_file:
@ -69,28 +67,28 @@ class Bot(irc.client.SimpleIRCClient, Logger):
except OSError as exc:
self.log_e(f"Could not save storage file: {exc}")
def on_welcome(self, connection: Connection, event: Event):
def on_welcome(self, connection, event):
"""Handle a successful connection to a server."""
self.log_i(f"Connected to server {event.source}.")
self.run_plugin_callbacks(event)
for channel in self.config["channels"]:
connection.join(channel)
def on_join(self, connection: Connection, event: Event):
def on_join(self, connection, event):
"""Handle someone, possibly the bot, joining a channel."""
if event.source.nick == self.nick:
self.log_i(f"Joined {event.target}.")
self.channels.append(event.target)
self.run_plugin_callbacks(event)
def on_part(self, connection: Connection, event: Event):
def on_part(self, connection, event):
"""Handle someone, possibly the bot, leaving a channel."""
if event.source.nick == self.nick:
self.log_i(f"Left {event.target} (args: {event.arguments[0]}).")
self.channels.remove(event.target)
self.run_plugin_callbacks(event)
def on_pubmsg(self, connection: Connection, event: Event):
def on_pubmsg(self, connection, event):
"""Handle a message received in a channel."""
channel = event.target
nick = NickMask(event.source).nick
@ -98,7 +96,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
self.log_d(f"Message in {channel} from {nick}: {message}")
self.run_plugin_callbacks(event)
def on_privmsg(self, connection: Connection, event: Event):
def on_privmsg(self, connection, event):
"""Handle a message received privately, usually like a channel msg."""
nick = NickMask(event.source).nick
target = event.target
@ -106,7 +104,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
self.log_d(f"Private message from {nick} to {target}: {message}")
self.run_plugin_callbacks(event)
def on_ping(self, connection: Connection, event: Event):
def on_ping(self, connection, event):
"""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)
@ -133,10 +131,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
plugin_files = os.listdir(Path(__file__).parent / "plugins")
plugin_names = map(
lambda f: os.path.splitext(f)[0],
filter(
lambda f: f.endswith(".py") and f != "__init__.py",
plugin_files,
),
filter(lambda f: f.endswith(".py"), plugin_files),
)
for plugin_name in plugin_names:
module = importlib.import_module(f"edmond.plugins.{plugin_name}")
@ -154,12 +149,11 @@ class Bot(irc.client.SimpleIRCClient, Logger):
self.values[plugin_name] = {}
self.log_d(f"Loaded {class_name}.")
def get_plugin(self, name: str) -> Optional[Plugin]:
def get_plugin(self, name):
"""Get a loaded plugin by its name (e.g. 'mood'), or None."""
matching_plugins = filter(lambda plugin: plugin.name == name, self.plugins)
return next(matching_plugins, None)
return next(filter(lambda p: p.name == name, self.plugins), None)
def say(self, target: str, message: str) -> None:
def say(self, target, message):
"""Send message to target after a slight delay."""
message = message.replace("\n", " ").replace("\r", " ")
time.sleep(self.config["speak_delay"])
@ -172,11 +166,11 @@ class Bot(irc.client.SimpleIRCClient, Logger):
except irc.client.MessageTooLong:
self.log_e("Could not send, message is too long.")
def run_plugin_callbacks(self, event: Event) -> None:
def run_plugin_callbacks(self, event):
"""Run appropriate callbacks for each plugin."""
etype = event.type
ready_plugins = filter(lambda p: p.is_ready, self.plugins)
plugins = sorted(ready_plugins, key=lambda p: p.priority, reverse=True)
plugins = filter(lambda p: p.is_ready, self.plugins)
plugins = sorted(plugins, key=lambda p: p.priority, reverse=True)
for plugin in plugins:
callbacks = plugin.callbacks
if etype not in callbacks:
@ -189,7 +183,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
self.cleanup()
exit("Exiting after received SIGTERM.")
def cleanup(self) -> None:
def cleanup(self):
"""Save the storage file and close the connection. Run only once."""
if self.done:
return

View file

@ -1,7 +1,7 @@
import json
def load_config(config_path: str, logger=None) -> dict:
def load_config(config_path, logger=None):
try:
with open(config_path, "rt") as config_file:
return json.load(config_file)

View file

@ -1,26 +1,7 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional
from irc.client import NickMask # type: ignore
@dataclass
class Question:
preamble: str
content: str
@dataclass
class Command:
# Identifier as set in config. Set even when an alias has been used.
ident: str
# Content of the command when it has been parsed, empty str otherwise.
content: str
# Raw command content (minus name and suffix), always set.
raw: str
# Identifier matched, possibly an alias. Set only when matched.
matched: str = ""
from irc.client import NickMask
class Plugin:
@ -59,17 +40,14 @@ class Plugin:
- -10: lowest, e.g. for random reactions usually with a very low rate
"""
REQUIRED_CONFIGS: list[str] = []
REQUIRED_CONFIGS = []
def __init__(self, bot):
from edmond.bot import Bot
self.bot: Bot = bot
# self.name is the plugin name, lowercased, without the Plugin suffix.
self.name: str = self.__class__.__name__.lower()[:-6]
self.priority: int = 0
self.config: dict = self.__get_config()
self.is_ready: bool = self.__check_config()
self.bot = bot
self.name = self.__class__.__name__.lower()[:-6] # Remove "Plugin".
self.priority = 0
self.config = self.__get_config()
self.is_ready = self.__check_config()
@property
def callbacks(self):
@ -80,7 +58,7 @@ class Plugin:
if cb.startswith("on_") and callable(getattr(self, cb))
}
def __get_config(self) -> dict:
def __get_config(self):
"""Return the plugin section from the bot config plus common values."""
plugins_configs = self.bot.config["plugins"]
config = plugins_configs["common"].copy()
@ -91,14 +69,12 @@ class Plugin:
res_path = Path(res_dir) / res_path
try:
with open(res_path, "rt") as res_file:
config[key] = [
line.strip() for line in res_file.readlines()
]
config[key] = [l.strip() for l in res_file.readlines()]
except OSError:
self.bot.log_e(f"Could not load resource at {res_path}.")
return config
def __check_config(self) -> bool:
def __check_config(self):
"""Return True if the plugin config is properly setup."""
missing = False
for key in self.REQUIRED_CONFIGS:
@ -107,9 +83,7 @@ class Plugin:
missing = True
return not missing
def get_runtime_value(
self, key: str, default: Any = None, ns: str = None
) -> Any:
def get_runtime_value(self, key, default=None, ns=None):
"""Get a value from the plugin runtime dict.
This will get the value from the plugin namespace, but it is possible
@ -119,11 +93,11 @@ class Plugin:
ns = self.name
return self.bot.values[ns].get(key, default)
def set_runtime_value(self, key: str, value: Any) -> Any:
def set_runtime_value(self, key, value):
"""Set a value in the plugin runtime dict."""
self.bot.values[self.name][key] = value
def get_storage_value(self, key: str, default=None, ns: str = None) -> Any:
def get_storage_value(self, key, default=None, ns=None):
"""Get a value from the plugin persistent storage.
This will get the value from the plugin namespace, but it is possible
@ -133,16 +107,15 @@ class Plugin:
ns = self.name
return self.bot.storage.get(ns, {}).get(key, default)
def set_storage_value(self, key: str, value: Any, ns: str = None) -> None:
def set_storage_value(self, key, value, ns=None):
"""Set a value in the plugin persistent storage."""
name = ns or self.name
if name not in self.bot.storage:
self.bot.storage[name] = {key: value}
else:
self.bot.storage[name][key] = value
self.bot.save_storage()
def append_storage_list_value(self, key: str, value: Any) -> None:
def append_storage_list_value(self, key, value):
"""Append a value to a list in the plugin persistent storage."""
if self.name not in self.bot.storage:
self.bot.storage[self.name] = {key: [value]}
@ -150,18 +123,16 @@ class Plugin:
self.bot.storage[self.name][key] = [value]
else:
self.bot.storage[self.name][key].append(value)
self.bot.save_storage()
def remove_storage_list_value(self, key: str, value: Any) -> None:
def remove_storage_list_value(self, key, value):
"""Remove a value from a persistent storage list."""
if (
self.name in self.bot.storage
and key in self.bot.storage[self.name]
):
self.bot.storage[self.name][key].remove(value)
self.bot.save_storage()
def should_read_message(self, message: str) -> Optional[str]:
def should_read_message(self, message):
"""Return a message content if it has been addressed to me, else None.
If the message starts with one of the bot's names, the rest of the
@ -188,9 +159,8 @@ class Plugin:
content = first_word_and_rest[1].strip()
self.bot.log_i(f"Reading message from {self.name}: {content}")
return content
return None
def should_answer_question(self, message: str) -> bool:
def should_answer_question(self, message):
"""Store Question in object and return True if I should answer it.
To answer a question, the message must start with one of the bot's
@ -218,18 +188,19 @@ class Plugin:
return True
return False
def __save_question(self, question: str, matched: str, preamble: str):
def __save_question(self, question, matched, preamble):
content = question[len(matched) :].strip()
content = content.rstrip("?").rstrip()
self.question = Question(preamble, content)
self.bot.log_i(f"Answering from plugin {self.name}: {self.question}")
question = Question(preamble, content)
self.question = question
self.bot.log_i(f"Answering from plugin {self.name}: {question}")
def should_handle_command(
self,
message: str,
no_content: bool = False,
exclude_conditions: Optional[dict] = None,
) -> bool:
message,
no_content=False,
exclude_conditions=None,
):
"""Store Command in object and return True if I should handle it.
If no_content is True, the command does not parse command contents and
@ -283,9 +254,7 @@ class Plugin:
return True
return False
def __parse_command(
self, message: str, no_content: bool = False
) -> Optional[Command]:
def __parse_command(self, message, no_content=False):
"""Return a parsed Command if this message is a command, else None.
The command raw field is always set. The ident and content fields are
@ -305,16 +274,13 @@ class Plugin:
ident = words[1]
content = " ".join(words[2:-1])
return Command(ident, content, raw)
return None
def __save_command(self, command: Command):
def __save_command(self, command):
"""Save command in instance for further processing by the plugin."""
self.command = command
self.bot.log_i(f"Processing command from p. {self.name}: {command}")
def respects_handling_conditions(
self, exclude_conditions: Optional[dict] = None
):
def respects_handling_conditions(self, exclude_conditions=None):
"""Check if handling conditions are valid.
Handling conditions can be specified by each plugin to create states in
@ -348,3 +314,21 @@ class Plugin:
event.target = NickMask(event.source).nick
return on_pubmsg(event)
return False
@dataclass
class Question:
preamble: str
content: str
@dataclass
class Command:
# Identifier as set in config. Set even when an alias has been used.
ident: str
# Content of the command when it has been parsed, empty str otherwise.
content: str
# Raw command content (minus name and suffix), always set.
raw: str
# Identifier matched, possibly an alias. Set only when matched.
matched: str = ""

View file

@ -40,44 +40,35 @@ class YoutubePlugin(Plugin):
return False
def handle_commands(self, target):
if self.command.ident != self.config["commands"][0]:
return
try:
search_response = (
self.youtube.search()
.list(
q=self.command.content,
part="id,snippet",
maxResults=1,
if self.command.ident == self.config["commands"][0]:
try:
search_response = (
self.youtube.search()
.list(
q=self.command.content,
part="id,snippet",
maxResults=1,
)
.execute()
)
.execute()
)
except GoogleApiError:
self.signal_failure(target)
return
except GoogleApiError:
self.signal_failure(target)
return
link = ""
icon = ""
title = ""
self.bot.log_i(str(search_response))
for result in search_response.get("items", []):
kind = result["id"]["kind"]
if kind == "youtube#video":
video_id = result["id"]["videoId"]
link = self.VIDEO_URL_FMT.format(video_id)
icon = "📼"
elif kind == "youtube#channel":
channel_id = result["id"]["channelId"]
link = self.CHANNEL_URL_FMT.format(channel_id)
icon = "📺"
elif kind == "youtube#playlist":
playlist_id = result["id"]["playlistId"]
link = self.PLAYLIST_URL_FMT.format(playlist_id)
icon = ""
if link:
title = result["snippet"]["title"]
break
else:
self.signal_failure(target)
return
self.bot.say(target, f"{icon} {link} {title}")
link = ""
for result in search_response.get("items", []):
if result["id"]["kind"] == "youtube#video":
video_id = result["id"]["videoId"]
link = self.VIDEO_URL_FMT.format(video_id)
elif result["id"]["kind"] == "youtube#channel":
channel_id = result["id"]["channelId"]
link = self.CHANNEL_URL_FMT.format(channel_id)
elif result["id"]["kind"] == "youtube#playlist":
playlist_id = result["id"]["playlistId"]
link = self.PLAYLIST_URL_FMT.format(playlist_id)
if link:
break
else:
self.signal_failure(target)
return
self.bot.say(target, link)

View file

@ -1,15 +1,13 @@
import random
from typing import Optional
import requests
def http_get(url: str) -> Optional[str]:
def http_get(url):
response = requests.get(url)
if response.status_code == 200:
return response.text
return None
def proc(proba_percentage: int) -> bool:
def proc(proba_percentage):
return random.random() < (proba_percentage / 100.0)