quote.py 3.69 KB
Newer Older
mathieui's avatar
mathieui committed
1 2 3 4
"""
This plugin allows you to quote messages easily.

Usage
5
-----
mathieui's avatar
mathieui committed
6 7 8 9

.. glossary::

    /quote
10
        **Usage:** ``/quote <message>``
mathieui's avatar
mathieui committed
11

12 13
        The message must exist. You can use autocompletion to get the message
        you want to quote easily.
mathieui's avatar
mathieui committed
14 15 16 17 18

        Example:

        .. code-block:: none

19
            /quote "Pouet"
mathieui's avatar
mathieui committed
20

21 22
        If the message "Pouet" exists, it will be put in the input. If not you
        will get a warning.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

Options
-------

.. glossary::
    :sorted:

    before_quote

        **Default value:** ``[empty]``

        Text to insert before the quote. ``%(nick)s`` and ``%(time)s`` can
        be used to insert the nick of the user who sent the message or the
        time of the message.

    after_quote

        **Default value:** ``[empty]``

        Text to insert after the quote. ``%(nick)s`` and ``%(time)s`` can
        be used to insert the nick of the user who sent the message or the
        time of the message.
mathieui's avatar
mathieui committed
45
"""
46

47
from poezio.core.structs import Completion
mathieui's avatar
mathieui committed
48
from poezio.ui.types import Message
49 50
from poezio.plugin import BasePlugin
from poezio.xhtml import clean_text
mathieui's avatar
mathieui committed
51
from poezio.theming import get_theme
52 53
from poezio import common
from poezio import tabs
louiz’'s avatar
louiz’ committed
54 55 56 57

import logging
log = logging.getLogger(__name__)

mathieui's avatar
mathieui committed
58

louiz’'s avatar
louiz’ committed
59 60
class Plugin(BasePlugin):
    def init(self):
61
        for _class in (tabs.MucTab, tabs.DynamicConversationTab, tabs.StaticConversationTab, tabs.PrivateTab):
mathieui's avatar
mathieui committed
62 63 64 65 66 67 68 69
            self.api.add_tab_command(
                _class,
                'quote',
                self.command_quote,
                usage='<message>',
                help='Quote the message you typed if it exists.',
                short='Quote a message.',
                completion=self.completion_quote)
louiz’'s avatar
louiz’ committed
70 71 72

    def command_quote(self, args):
        args = common.shell_split(args)
73 74
        if len(args) == 1:
            message = args[-1]
louiz’'s avatar
louiz’ committed
75
        else:
76
            return self.api.run_command('/help quote')
77
        message = self.find_message(message)
louiz’'s avatar
louiz’ committed
78
        if message:
mathieui's avatar
mathieui committed
79
            str_time = message.time.strftime(get_theme().SHORT_TIME_FORMAT)
mathieui's avatar
mathieui committed
80 81
            before = self.config.get('before_quote', '') % {
                'nick': message.nickname or '',
mathieui's avatar
mathieui committed
82
                'time': str_time,
mathieui's avatar
mathieui committed
83 84 85
            }
            after = self.config.get('after_quote', '') % {
                'nick': message.nickname or '',
mathieui's avatar
mathieui committed
86
                'time': str_time,
mathieui's avatar
mathieui committed
87 88 89 90 91 92 93
            }
            self.core.insert_input_text(
                '%(before)s%(quote)s%(after)s' % {
                    'before': before.replace('\\n', '\n').replace('[SP]', ' '),
                    'quote': clean_text(message.txt),
                    'after': after.replace('\\n', '\n').replace('[SP]', ' ')
                })
louiz’'s avatar
louiz’ committed
94
        else:
95
            self.api.information('No message found', 'Warning')
louiz’'s avatar
louiz’ committed
96

97
    def find_message(self, txt):
98
        messages = self.api.get_conversation_messages()
louiz’'s avatar
louiz’ committed
99 100 101
        if not messages:
            return None
        for message in messages[::-1]:
mathieui's avatar
mathieui committed
102
            if isinstance(message, Message) and clean_text(message.txt) == txt:
louiz’'s avatar
louiz’ committed
103 104 105 106
                return message
        return None

    def completion_quote(self, the_input):
107 108
        def message_match(msg):
            return input_message.lower() in clean_text(msg.txt).lower()
mathieui's avatar
mathieui committed
109

110
        messages = self.api.get_conversation_messages()
louiz’'s avatar
louiz’ committed
111 112 113 114
        if not messages:
            return
        text = the_input.get_text()
        args = common.shell_split(text)
115 116 117 118 119
        if not text.endswith(' '):
            input_message = args[-1]
            messages = list(filter(message_match, messages))
        elif len(args) > 1:
            return False
mathieui's avatar
mathieui committed
120 121 122 123 124
        return Completion(
            the_input.auto_completion,
            [clean_text(msg.txt) for msg in messages[::-1] if isinstance(msg, Message)],
            ''
        )