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/lib9front.py | 99 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 arthur/apis/systems/lib9front.py (limited to 'arthur/apis/systems/lib9front.py') 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) -- 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(-) (limited to 'arthur/apis/systems/lib9front.py') 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