2013-04-13 20:33:06 +00:00
|
|
|
|
"""
|
|
|
|
|
This plugin allows you to quote messages easily.
|
|
|
|
|
|
|
|
|
|
Installation
|
|
|
|
|
------------
|
|
|
|
|
You only have to load the plugin.
|
|
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
|
|
/load quote
|
|
|
|
|
|
|
|
|
|
Usage
|
|
|
|
|
-------
|
|
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
|
|
/quote
|
|
|
|
|
**Usage:** ``/quote <timestamp>``
|
|
|
|
|
|
|
|
|
|
Timestamp is in ``HH:MM:SS`` format, and can be completed with [tab].
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
|
|
/quote 21:12:23
|
|
|
|
|
|
|
|
|
|
If there is a message at 21:12:23, it will be put in the input. If there
|
|
|
|
|
isn’t, you will get a warning.
|
|
|
|
|
"""
|
2012-05-13 16:39:57 +00:00
|
|
|
|
from plugin import BasePlugin
|
2011-11-23 08:09:48 +00:00
|
|
|
|
from xhtml import clean_text
|
|
|
|
|
import common
|
2012-05-13 16:39:57 +00:00
|
|
|
|
import tabs
|
2011-11-23 08:09:48 +00:00
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
timestamp_re = re.compile(r'^(\d\d\d\d-\d\d-\d\d )?\d\d:\d\d:\d\d$')
|
2011-11-23 18:53:02 +00:00
|
|
|
|
seconds_re = re.compile(r'^:\d\d$')
|
2011-11-23 08:09:48 +00:00
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
|
def init(self):
|
2013-03-01 18:25:31 +00:00
|
|
|
|
for _class in (tabs.MucTab, tabs.ConversationTab, tabs.PrivateTab):
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.add_tab_command(_class, 'quote', self.command_quote,
|
2013-03-01 18:25:31 +00:00
|
|
|
|
usage='<timestamp>',
|
|
|
|
|
help='Takes the message received at <timestamp> and insert it in the input, to quote it.',
|
|
|
|
|
short='Quote a message from a timestamp',
|
|
|
|
|
completion=self.completion_quote)
|
2011-11-23 08:09:48 +00:00
|
|
|
|
|
|
|
|
|
def command_quote(self, args):
|
|
|
|
|
args = common.shell_split(args)
|
|
|
|
|
if len(args) in (1, 2):
|
|
|
|
|
timestamp = args[-1]
|
|
|
|
|
else:
|
2013-03-08 21:53:35 +00:00
|
|
|
|
return self.api.run_command('/help quote')
|
2011-11-23 08:09:48 +00:00
|
|
|
|
if re.match(timestamp_re, timestamp) is None:
|
2013-03-08 21:53:35 +00:00
|
|
|
|
return self.api.information('Timestamp has a wrong format.', 'Warning')
|
2011-11-23 08:09:48 +00:00
|
|
|
|
message = self.find_message_with_timestamp(timestamp)
|
|
|
|
|
if message:
|
|
|
|
|
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]', ' ')})
|
|
|
|
|
else:
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.information('No message found for timestamp %s.' % timestamp, 'Warning')
|
2011-11-23 08:09:48 +00:00
|
|
|
|
|
|
|
|
|
def find_message_with_timestamp(self, timestamp):
|
|
|
|
|
# TODO: handle messages with the same
|
|
|
|
|
# timestamp but not the same day
|
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]:
|
|
|
|
|
if message.str_time == timestamp:
|
|
|
|
|
return message
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def completion_quote(self, the_input):
|
|
|
|
|
def nick_match(msg):
|
|
|
|
|
if not msg.nickname:
|
|
|
|
|
return nick == ''
|
|
|
|
|
return msg.nickname.lower().startswith(nick.lower())
|
2011-11-23 18:53:02 +00:00
|
|
|
|
def time_match(msg):
|
|
|
|
|
return msg.str_time.endswith(time)
|
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)
|
|
|
|
|
n = len(args)
|
|
|
|
|
if text.endswith(' '):
|
|
|
|
|
n += 1
|
2011-11-23 18:53:02 +00:00
|
|
|
|
time = args[-1]
|
|
|
|
|
if re.match(seconds_re, time) is not None:
|
|
|
|
|
messages = list(filter(time_match, messages))
|
|
|
|
|
for i in range(3):
|
|
|
|
|
the_input.key_backspace(False)
|
|
|
|
|
elif n == 2:
|
2011-11-23 18:49:19 +00:00
|
|
|
|
try:
|
|
|
|
|
if args[1][0] not in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0'):
|
|
|
|
|
return False
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2011-11-23 08:09:48 +00:00
|
|
|
|
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]], '')
|