From 6c3865e40cbcbce4b57edbef16fa5335449e1826 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Tue, 28 May 2024 20:00:07 +0200 Subject: Add support for parsing system information This commit adds support for parsing miscellaneous system file formats, allowing us to further incorporate parsing for files such as ``/proc/PID/status`` for future command purposes. --- arthur/apis/systems/__init__.py | 1 + arthur/apis/systems/lib9front.py | 99 ++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 5 ++ 3 files changed, 105 insertions(+) create mode 100644 arthur/apis/systems/__init__.py create mode 100644 arthur/apis/systems/lib9front.py diff --git a/arthur/apis/systems/__init__.py b/arthur/apis/systems/__init__.py new file mode 100644 index 0000000..302f80c --- /dev/null +++ b/arthur/apis/systems/__init__.py @@ -0,0 +1 @@ +"""Functionality for working with various operating systems.""" diff --git a/arthur/apis/systems/lib9front.py b/arthur/apis/systems/lib9front.py new file mode 100644 index 0000000..e499147 --- /dev/null +++ b/arthur/apis/systems/lib9front.py @@ -0,0 +1,99 @@ +"""Library functions for working with 9front systems and file formats.""" + +import random + + +def generate_blog_comment(blogcom: str) -> str: + """ + Generate a blog comment out of the ``blogcom`` file contents that are passed in. + + The blogcom file can be retrieved at ``/lib/blogcom`` in 9front. + + This function is implemented according to the highest standards in + performance and security, and to be able to generate properly random texts, + it utilizes a 623-dimensionally equidistributed uniform pseudorandom number + generator as described by Makoto Matsumoto and Takuji Nishimura. In other + words, for our purposes, this function matches or even exceeds Python + Discord's security requirements. + """ + # Complete output buffer + out = [] + # Options of the current branch, of which one will be selected at random + options = [] + # Character buffer of the current choice + choice_buf = [] + # Whether we are in a {block|of|options} at the moment + in_block = False + + for char in blogcom: + if char == "{": + in_block = True + elif in_block and char == "|": + options.append("".join(choice_buf)) + choice_buf.clear() + elif in_block and char == "}": + options.append("".join(choice_buf)) + choice_buf.clear() + out.append(random.choice(options)) + options.clear() + in_block = False + elif in_block: + choice_buf.append(char) + else: + out.append(char) + + return "".join(out) + + +def generate_buzzwords(bullshit: str) -> str: + """ + Generates buzzwords to describe a random product of the ``bullshit`` file contents that are passed in. + + The bullshit file can be retrieved at ``/lib/bullshit`` in 9front. + + This function underlies the same security guarantees as ``generate_blog_comment``. + """ + # line markers + # nothing -> word + # ^ -> start + # * -> protocol + # % -> suffix + # | -> adjectives + # $ -> end + words = [] + starters = [] + protocols = [] + suffixes = [] + adjectives = [] + endings = [] + + # Parsing + for line in bullshit.splitlines(): + if " " not in line: + words.append(line) + else: + word, qualifier = line.split() + if qualifier == "^": + starters.append(word) + elif qualifier == "*": + protocols.append(word) + elif qualifier == "%": + suffixes.append(word) + elif qualifier == "|": + adjectives.append(word) + elif qualifier == "$": + endings.append(word) + + # Generating + response = [] + for _ in range(random.randint(1, 2)): + response.append(random.choice(starters)) + for _ in range(random.randint(1, 2)): + response.append(random.choice(adjectives)) + for _ in range(random.randint(1, 2)): + response.append(random.choice(words) + random.choice(suffixes) * random.randint(0, 1)) + if random.random() > 0.5: + response.append("over " + random.choice(protocols)) + if random.random() > 0.3: + response.append(random.choice(endings)) + return " ".join(response) diff --git a/pyproject.toml b/pyproject.toml index b5653b8..b286606 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,6 +67,11 @@ ignore = [ "COM812", "D206", "E111", "E114", "E117", "E501", "ISC001", "Q000", "Q001", "Q002", "Q003", "W191", ] +[tool.ruff.lint.per-file-ignores] +# McCabe is not supported on Linux systems +"arthur/apis/systems/*.py" = ["C901", "PLR0912", "PERF401", "PLR2004"] + + [tool.ruff.lint.isort] order-by-type = false case-sensitive = true -- cgit v1.2.3 From 3ba33437f491485065cc3921d07a0b3745376c24 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Fri, 31 May 2024 18:40:38 +0200 Subject: Correct parsing library information file --- arthur/apis/systems/lib9front.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arthur/apis/systems/lib9front.py b/arthur/apis/systems/lib9front.py index e499147..a527a6e 100644 --- a/arthur/apis/systems/lib9front.py +++ b/arthur/apis/systems/lib9front.py @@ -16,6 +16,7 @@ def generate_blog_comment(blogcom: str) -> str: words, for our purposes, this function matches or even exceeds Python Discord's security requirements. """ + fragment = random.choice(blogcom.split("|\n")) # Complete output buffer out = [] # Options of the current branch, of which one will be selected at random @@ -25,7 +26,7 @@ def generate_blog_comment(blogcom: str) -> str: # Whether we are in a {block|of|options} at the moment in_block = False - for char in blogcom: + for char in fragment: if char == "{": in_block = True elif in_block and char == "|": -- cgit v1.2.3 From 004f126cd3427c0ff35b801e3a417da616da9a91 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Fri, 31 May 2024 18:09:01 +0100 Subject: Ignore DTZ003 lint rule --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index b286606..86f769c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,6 +62,7 @@ ignore = [ "SIM102", "SIM108", "PD", "PLR0913", "PLR0917", "PLR6301", + "DTZ003", # Rules suggested to be ignored when using ruff format "COM812", "D206", "E111", "E114", "E117", "E501", "ISC001", "Q000", "Q001", "Q002", "Q003", "W191", -- cgit v1.2.3 From 3e942baff266b344f1e9d644d6a6aa023a6313c6 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Fri, 31 May 2024 18:09:25 +0100 Subject: Add new System Information extension --- arthur/exts/systems/__init__.py | 1 + arthur/exts/systems/system_information.py | 80 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 arthur/exts/systems/__init__.py create mode 100644 arthur/exts/systems/system_information.py diff --git a/arthur/exts/systems/__init__.py b/arthur/exts/systems/__init__.py new file mode 100644 index 0000000..fa87b0c --- /dev/null +++ b/arthur/exts/systems/__init__.py @@ -0,0 +1 @@ +"""Extensions for fetching system information from our production 9front deployment.""" diff --git a/arthur/exts/systems/system_information.py b/arthur/exts/systems/system_information.py new file mode 100644 index 0000000..83e3daa --- /dev/null +++ b/arthur/exts/systems/system_information.py @@ -0,0 +1,80 @@ +"""Return system information on our production 9front infrastructure.""" + +import random +from datetime import datetime + +import aiohttp +from discord import Message +from discord.ext.commands import Cog +from loguru import logger + +from arthur.apis.systems import lib9front +from arthur.bot import KingArthur +from arthur.config import CONFIG + +BLOGCOM = "https://git.9front.org/plan9front/plan9front/HEAD/lib/blogcom/raw" +THRESHOLD = 0.01 +MIN_MINUTES = 30 +BLOG_ABOUT_IT_THRESHOLD = 1000 + + +class SystemInformation(Cog): + """Utilities for fetching system information from our 9front infrastructure.""" + + def __init__(self, bot: KingArthur) -> None: + self.bot = bot + self.cached_data = None + self.last_sent = None + + async def fetch_blogcom(self) -> str: + """Fetch the blogcom file from the upstream location, or return the cached copy.""" + if not self.cached_data: + async with aiohttp.ClientSession() as session, session.get(BLOGCOM) as resp: + self.cached_data = await resp.text() + + return self.cached_data + + @Cog.listener() + async def on_message(self, msg: Message) -> None: + """Handler for incoming messages, potentially returning system information.""" + if not msg.guild: + logger.trace("Ignoring DM") + return + + if msg.author.id == self.bot.user.id: + logger.trace("Ignoring own message") + return + + if msg.channel.id != CONFIG.devops_channel_id: + logger.trace("Ignoring message outside of DevOps channel") + return + + if self.last_sent: + if (datetime.utcnow() - self.last_sent).minutes < MIN_MINUTES: + logger.trace("Ignoring message as within cooldown") + return + + msg_thresh = THRESHOLD + + if CONFIG.devops_role not in (r.id for r in msg.author.roles): + logger.trace("Upping threshold due to non-DevOps member") + msg_thresh *= 10 + + if len(msg.content) > BLOG_ABOUT_IT_THRESHOLD: + msg_thresh += 0.10 + + if random.random() < msg_thresh: + logger.trace("Criteria hit, generating comment.") + + blogcom = await self.fetch_blogcom() + + comment = lib9front.generate_blog_comment(blogcom).strip() + + await msg.reply(f"{comment} :smile:") + + self.last_sent = datetime.utcnow() + + +async def setup(bot: KingArthur) -> None: + """Add cog to bot.""" + await bot.add_cog(SystemInformation(bot)) -- cgit v1.2.3