summaryrefslogtreecommitdiff
path: root/plugins/figlet.py
blob: 4d4c7577af54831e4d883a50cbc985b88979b2ba (plain)
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
"""
This plugin uses figlet to transform every message into a big ascii-art
message.


Usage
-----

Say something in a Chat tab.

.. note:: Can create fun things when used with :ref:`The rainbow plugin <rainbow-plugin>`.

"""

import subprocess
from poezio.plugin import BasePlugin


def is_figlet() -> bool:
    """Ensure figlet exists"""
    process = subprocess.Popen(
        ['which', 'figlet'],
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
    )
    return process.wait() == 0


class Plugin(BasePlugin):
    def init(self):
        if not is_figlet():
            self.api.information(
                'Couldn\'t find the figlet program. '
                'Please install it and reload the plugin.',
                'Error',
            )
            return None

        self.api.add_event_handler('muc_say', self.figletize)
        self.api.add_event_handler('conversation_say', self.figletize)
        self.api.add_event_handler('private_say', self.figletize)
        return None

    def figletize(self, msg, tab):
        process = subprocess.Popen(
            ['figlet', '--', msg['body']], stdout=subprocess.PIPE)
        result = process.communicate()[0].decode('utf-8')
        msg['body'] = result