summaryrefslogtreecommitdiff
path: root/plugins/marquee.py
blob: 102f54ff25578461114b250b66379707c517f744 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Marquee plugin: replicate the html <marquee/> tag with message corrections.

Usage of this plugin is not recommended.

Commands
--------

.. glossary::

    /marquee <text>
        Send the following text with <marquee/> behavior

Configuration
-------------

.. glossary::
    :sorted:

    refresh
        **Default:** ``1``

        Interval between each correction (the closest to 0 is the fastest)

    total_duration
        **Default:** ``30``

        Total duration of the animation.

    padding
        **Default:** ``20``

        Padding to use to move the text.


"""
from plugin import BasePlugin
import tabs
from decorators import command_args_parser

def move(text, step, spacing):
    new_text = text + (" " * spacing)
    return new_text[-(step % len(new_text)):] + new_text[:-(step % len(new_text))]

class Plugin(BasePlugin):
    default_config = {"marquee": {"refresh": 1.0, "total_duration": 30, "padding": 20}}

    def init(self):
        self.add_tab_command(tabs.MucTab, 'marquee', self.command_marquee, 'Replicate the <marquee/> behavior in a message')

    @command_args_parser.raw
    def command_marquee(self, args):
        tab = self.api.current_tab()
        tab.command_say(args)
        msg_id = tab.last_sent_message["id"]
        jid = tab.name

        self.api.add_timed_event(self.api.create_delayed_event(self.config.get("refresh"), self.delayed_event, jid, args, msg_id, 0, 0))

    def delayed_event(self, jid, body, msg_id, step, duration):
        if duration >= self.config.get("total_duration"):
            return
        message = self.core.xmpp.make_message(jid)
        message["type"] = "groupchat"
        message["body"] = move(body, step, self.config.get("padding"))
        message["replace"]["id"] = msg_id
        message.send()
        self.api.information("refresh : %s" % self.config.get("refresh"))
        self.api.add_timed_event(self.api.create_delayed_event(self.config.get("refresh"), self.delayed_event, jid, body, message["id"], step + 1, duration + self.config.get("refresh")))