summaryrefslogtreecommitdiff
path: root/plugins/replace_word.py
blob: ce66dff48c91a3e880978606e2ea9b9f53495665 (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
"""
Replace some word with some other word in a message before sending it.

Configuration example
---------------------
.. code-block:: ini

[replace_word]
# How to appear casual in your daily conversations.
yes = yep
no = nope

Usage
-----
Just use the word in a message. It will be replaced automatically.

"""

from poezio.plugin import BasePlugin
import re

class Plugin(BasePlugin):
    def init(self):
        self.api.add_event_handler('conversation_say', self.replace_pattern)
        self.api.add_event_handler('muc_say', self.replace_pattern)
        self.api.add_event_handler('private_say', self.replace_pattern)

    def replace_pattern(self, message, tab):
        """
        Look for a given word in the message and replace it by the corresponding word.
        """
        body = message['body']
        for before in self.config.options("replace_word"):
            after = self.config.get(before, before)
            body = re.sub(r"\b%s\b" % before, after, body)
        message['body'] = body