2013-04-13 20:33:06 +00:00
|
|
|
"""
|
|
|
|
This plugin allows you to quote messages easily.
|
|
|
|
|
|
|
|
Usage
|
2014-03-27 22:45:47 +00:00
|
|
|
-----
|
2013-04-13 20:33:06 +00:00
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/quote
|
2014-10-20 23:05:11 +00:00
|
|
|
**Usage:** ``/quote <message>``
|
2013-04-13 20:33:06 +00:00
|
|
|
|
2014-10-20 23:05:11 +00:00
|
|
|
The message must exist. You can use autocompletion to get the message
|
|
|
|
you want to quote easily.
|
2013-04-13 20:33:06 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
2014-10-20 23:05:11 +00:00
|
|
|
/quote "Pouet"
|
2013-04-13 20:33:06 +00:00
|
|
|
|
2014-10-20 23:05:11 +00:00
|
|
|
If the message "Pouet" exists, it will be put in the input. If not you
|
|
|
|
will get a warning.
|
2014-10-12 22:39:55 +00:00
|
|
|
|
|
|
|
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.
|
2013-04-13 20:33:06 +00:00
|
|
|
"""
|
2014-10-20 23:05:11 +00:00
|
|
|
|
2016-08-21 13:27:53 +00:00
|
|
|
from poezio.core.structs import Completion
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
|
|
|
from poezio.xhtml import clean_text
|
|
|
|
from poezio import common
|
|
|
|
from poezio import tabs
|
2011-11-23 08:09:48 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
2011-11-23 08:09:48 +00:00
|
|
|
class Plugin(BasePlugin):
|
|
|
|
def init(self):
|
2019-07-19 16:55:16 +00:00
|
|
|
for _class in (tabs.MucTab, tabs.DynamicConversationTab, tabs.StaticConversationTab, tabs.PrivateTab):
|
2018-08-15 11:13:17 +00:00
|
|
|
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)
|
2011-11-23 08:09:48 +00:00
|
|
|
|
|
|
|
def command_quote(self, args):
|
|
|
|
args = common.shell_split(args)
|
2014-10-20 23:05:11 +00:00
|
|
|
if len(args) == 1:
|
|
|
|
message = args[-1]
|
2011-11-23 08:09:48 +00:00
|
|
|
else:
|
2013-03-08 21:53:35 +00:00
|
|
|
return self.api.run_command('/help quote')
|
2014-10-20 23:05:11 +00:00
|
|
|
message = self.find_message(message)
|
2011-11-23 08:09:48 +00:00
|
|
|
if message:
|
2018-08-15 11:13:17 +00:00
|
|
|
before = self.config.get('before_quote', '') % {
|
|
|
|
'nick': message.nickname or '',
|
|
|
|
'time': message.str_time
|
|
|
|
}
|
|
|
|
after = self.config.get('after_quote', '') % {
|
|
|
|
'nick': message.nickname or '',
|
|
|
|
'time': message.str_time
|
|
|
|
}
|
|
|
|
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]', ' ')
|
|
|
|
})
|
2011-11-23 08:09:48 +00:00
|
|
|
else:
|
2014-10-20 23:05:11 +00:00
|
|
|
self.api.information('No message found', 'Warning')
|
2011-11-23 08:09:48 +00:00
|
|
|
|
2014-10-20 23:05:11 +00:00
|
|
|
def find_message(self, txt):
|
2013-03-08 21:53:35 +00:00
|
|
|
messages = self.api.get_conversation_messages()
|
2011-11-23 08:09:48 +00:00
|
|
|
if not messages:
|
|
|
|
return None
|
|
|
|
for message in messages[::-1]:
|
2014-10-20 23:05:11 +00:00
|
|
|
if clean_text(message.txt) == txt:
|
2011-11-23 08:09:48 +00:00
|
|
|
return message
|
|
|
|
return None
|
|
|
|
|
|
|
|
def completion_quote(self, the_input):
|
2014-10-20 23:05:11 +00:00
|
|
|
def message_match(msg):
|
|
|
|
return input_message.lower() in clean_text(msg.txt).lower()
|
2018-08-15 11:13:17 +00:00
|
|
|
|
2013-03-08 21:53:35 +00:00
|
|
|
messages = self.api.get_conversation_messages()
|
2011-11-23 08:09:48 +00:00
|
|
|
if not messages:
|
|
|
|
return
|
|
|
|
text = the_input.get_text()
|
|
|
|
args = common.shell_split(text)
|
2014-10-20 23:05:11 +00:00
|
|
|
if not text.endswith(' '):
|
|
|
|
input_message = args[-1]
|
|
|
|
messages = list(filter(message_match, messages))
|
|
|
|
elif len(args) > 1:
|
|
|
|
return False
|
2018-08-15 11:13:17 +00:00
|
|
|
return Completion(the_input.auto_completion,
|
|
|
|
[clean_text(msg.txt) for msg in messages[::-1]], '')
|