The quote plugin now uses full messages instead of timestamps.

This commit is contained in:
Eijebong 2014-10-21 01:05:11 +02:00 committed by Florent Le Coz
parent ea2b703bfd
commit 1ff6b40e20

View file

@ -7,18 +7,19 @@ Usage
.. glossary:: .. glossary::
/quote /quote
**Usage:** ``/quote <timestamp>`` **Usage:** ``/quote <message>``
Timestamp is in ``HH:MM:SS`` format, and can be completed with [tab]. The message must exist. You can use autocompletion to get the message
you want to quote easily.
Example: Example:
.. code-block:: none .. code-block:: none
/quote 21:12:23 /quote "Pouet"
If there is a message at 21:12:23, it will be put in the input. If there If the message "Pouet" exists, it will be put in the input. If not you
isnt, you will get a warning. will get a warning.
Options Options
------- -------
@ -42,14 +43,11 @@ Options
be used to insert the nick of the user who sent the message or the be used to insert the nick of the user who sent the message or the
time of the message. time of the message.
""" """
from plugin import BasePlugin from plugin import BasePlugin
from xhtml import clean_text from xhtml import clean_text
import common import common
import tabs import tabs
import re
timestamp_re = re.compile(r'^(\d\d\d\d-\d\d-\d\d )?\d\d:\d\d:\d\d$')
seconds_re = re.compile(r'^:\d\d$')
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -58,20 +56,18 @@ class Plugin(BasePlugin):
def init(self): def init(self):
for _class in (tabs.MucTab, tabs.ConversationTab, tabs.PrivateTab): for _class in (tabs.MucTab, tabs.ConversationTab, tabs.PrivateTab):
self.api.add_tab_command(_class, 'quote', self.command_quote, self.api.add_tab_command(_class, 'quote', self.command_quote,
usage='<timestamp>', usage='<message>',
help='Takes the message received at <timestamp> and insert it in the input, to quote it.', help='Quote the message you typed if it exists.',
short='Quote a message from a timestamp', short='Quote a message.',
completion=self.completion_quote) completion=self.completion_quote)
def command_quote(self, args): def command_quote(self, args):
args = common.shell_split(args) args = common.shell_split(args)
if len(args) in (1, 2): if len(args) == 1:
timestamp = args[-1] message = args[-1]
else: else:
return self.api.run_command('/help quote') return self.api.run_command('/help quote')
if re.match(timestamp_re, timestamp) is None: message = self.find_message(message)
return self.api.information('Timestamp has a wrong format.', 'Warning')
message = self.find_message_with_timestamp(timestamp)
if message: if message:
before = self.config.get('before_quote', '') % {'nick': message.nickname or '', before = self.config.get('before_quote', '') % {'nick': message.nickname or '',
'time': message.str_time} 'time': message.str_time}
@ -81,47 +77,29 @@ class Plugin(BasePlugin):
'quote': clean_text(message.txt), 'quote': clean_text(message.txt),
'after': after.replace('\\n', '\n').replace('[SP]', ' ')}) 'after': after.replace('\\n', '\n').replace('[SP]', ' ')})
else: else:
self.api.information('No message found for timestamp %s.' % timestamp, 'Warning') self.api.information('No message found', 'Warning')
def find_message_with_timestamp(self, timestamp): def find_message(self, txt):
# TODO: handle messages with the same
# timestamp but not the same day
messages = self.api.get_conversation_messages() messages = self.api.get_conversation_messages()
if not messages: if not messages:
return None return None
for message in messages[::-1]: for message in messages[::-1]:
if message.str_time == timestamp: if clean_text(message.txt) == txt:
return message return message
return None return None
def completion_quote(self, the_input): def completion_quote(self, the_input):
def nick_match(msg): def message_match(msg):
if not msg.nickname: return input_message.lower() in clean_text(msg.txt).lower()
return nick == ''
return msg.nickname.lower().startswith(nick.lower())
def time_match(msg):
return msg.str_time.endswith(time)
messages = self.api.get_conversation_messages() messages = self.api.get_conversation_messages()
if not messages: if not messages:
return return
text = the_input.get_text() text = the_input.get_text()
args = common.shell_split(text) args = common.shell_split(text)
n = len(args) if not text.endswith(' '):
if text.endswith(' '): input_message = args[-1]
n += 1 messages = list(filter(message_match, messages))
time = args[-1] elif len(args) > 1:
if re.match(seconds_re, time) is not None: return False
messages = list(filter(time_match, messages)) return the_input.auto_completion([clean_text(msg.txt) for msg in messages[::-1]], '')
for i in range(3):
the_input.key_backspace(False)
elif n == 2:
try:
if args[1][0] not in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0'):
return False
except:
pass
nick = ''
if n == 3:
nick = args[1]
messages = list(filter(nick_match, messages))
return the_input.auto_completion([msg.str_time for msg in messages[::-1]], '')