summaryrefslogtreecommitdiff
path: root/plugins/display_corrections.py
blob: cad5a32797cd7f5d383c082ad9b3d442280a8a1f (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
# A plugin that adds the /display_corrections command, to view the previous
# versions of a corrected message.

from plugin import BasePlugin
from common import shell_split
import tabs

class Plugin(BasePlugin):
    def init(self):
        for tab_type in (tabs.MucTab, tabs.PrivateTab, tabs.ConversationTab):
            self.api.add_tab_command(tab_type, 'display_corrections',
                    handler=self.command_display_corrections,
                    usage='<number>',
                    help='Display all the corrections of the number-th last corrected message.',
                    short='Display the corrections of a message')

    def find_corrected(self, nb):
        messages = self.api.get_conversation_messages()
        if not messages:
            return None
        for message in messages[::-1]:
            if message.old_message:
                if nb == 1:
                    return message
                else:
                    nb -= 1
        return None

    def command_display_corrections(self, args):
        args = shell_split(args)
        if len(args) == 1:
            try:
                nb = int(args[0])
            except:
                return self.api.run_command('/help display_corrections')
        else:
            nb = 1
        message = self.find_corrected(nb)
        if message:
            display = []
            while message:
                display.append('%s %s%s%s %s' % (message.str_time, '* ' if message.me else '', message.nickname, '' if message.me else '>', message.txt))
                message = message.old_message
            self.api.information('Older versions:\n' + '\n'.join(display[::-1]), 'Info')
        else:
            self.api.information('No corrected message found.', 'Warning')

    def cleanup(self):
        del self.config