File: cyber.py

package info (click to toggle)
poezio 0.16-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,124 kB
  • sloc: python: 25,093; xml: 995; ansic: 329; makefile: 200; sh: 74; javascript: 2
file content (42 lines) | stat: -rw-r--r-- 956 bytes parent folder | download | duplicates (2)
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
"""
This plugin adds a "cyber" prefix to a random word in your chatroom messages.

Usage
-----

Say something in a MUC tab.

Configuration options
---------------------

.. glossary::

    frequency
        **Default:** ``10``

        The percentage of the time the plugin will activate (randomly). 100 for every message, <= 0 for never.
"""

from poezio.plugin import BasePlugin
from random import choice, randint
import re

DEFAULT_CONFIG = {'cyber': {'frequency': 10}}


class Plugin(BasePlugin):

    default_config = DEFAULT_CONFIG

    def init(self):
        self.api.add_event_handler('muc_say', self.cyberize)

    def cyberize(self, msg, tab):
        if randint(1, 100) > self.config.get('frequency'):
            return
        words = [
            word for word in re.split(r'\W+', msg['body']) if len(word) > 3
        ]
        if words:
            word = choice(words)
            msg['body'] = msg['body'].replace(word, 'cyber' + word)