random_nick.py 991 Bytes
Newer Older
louiz’'s avatar
louiz’ committed
1
"""
2
This plugin makes you have a random nick when joining a chatroom.
louiz’'s avatar
louiz’ committed
3 4 5 6 7 8 9 10 11 12 13

Usage
-----

To have a random nick, just join a room with “RANDOM” as your nick. It will
automatically be changed to something random, for example: ::

    /join coucou@conference.example.com/RANDOM

"""

14
from poezio.plugin import BasePlugin
louiz’'s avatar
louiz’ committed
15 16 17 18 19
from random import choice

class Plugin(BasePlugin):
    def init(self):
        self.api.add_event_handler('joining_muc', self.change_nick_to_random)
20
        self.api.add_event_handler('changing_nick', self.change_nick_to_random)
louiz’'s avatar
louiz’ committed
21 22 23

    def change_nick_to_random(self, presence):
        to = presence["to"]
24 25 26
        if to.resource == 'RANDOM':
            to.resource = gen_nick(3)
            presence["to"] = to
louiz’'s avatar
louiz’ committed
27 28 29 30 31 32

s = ["i", "ou", "ou", "on", "a", "o", "u", "i"]
c = ["b", "c", "d", "f", "g", "h", "j", "k", "m", "l", "n", "p", "r", "s", "t", "v", "z"]

def gen_nick(size):
    res = ''
louiz’'s avatar
louiz’ committed
33
    for _ in range(size):
louiz’'s avatar
louiz’ committed
34 35
        res += '%s%s' % (choice(c), choice(s))
    return res