aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/apis/systems/lib9front.py
blob: 9980c9bc4ad121e09d310cf96f933d2975c6d9a4 (plain) (blame)
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
"""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.
    """
    fragment = random.choice(blogcom.split("|\n"))
    # 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 fragment:
        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)
        .replace("website", "message")
        .replace("blog post", "message")
        .replace("blog", "message")
        .replace("post", "message")
        .replace("article", "message")
        .replace("web site", "message")
        .replace("site", "message")
    )


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)