2014-03-27 22:45:47 +00:00
|
|
|
"""
|
|
|
|
Send a message after a certain delay.
|
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
|
|
|
This plugin adds a command to the chat tabs.
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/send_delayed
|
|
|
|
**Usage:** ``/send_delayed <delay> <message>``
|
|
|
|
|
|
|
|
Send a message after a given delay to the current tab.
|
|
|
|
The delay can be either in seconds or in a classic XdXhXm format
|
|
|
|
(e.g. ``7h3m`` or ``1d``), some examples are given with the
|
|
|
|
autocompletion.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
2016-08-21 13:27:53 +00:00
|
|
|
from poezio.core.structs import Completion
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio.decorators import command_args_parser
|
|
|
|
from poezio import tabs
|
|
|
|
from poezio import common
|
|
|
|
from poezio import timed_events
|
2011-11-15 20:35:39 +00:00
|
|
|
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
class Plugin(BasePlugin):
|
2011-11-15 20:35:39 +00:00
|
|
|
def init(self):
|
2013-03-01 18:25:31 +00:00
|
|
|
for _class in (tabs.PrivateTab, tabs.ConversationTab, tabs.MucTab):
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.add_tab_command(
|
|
|
|
_class,
|
|
|
|
'send_delayed',
|
|
|
|
self.command_delayed,
|
|
|
|
usage='<delay> <message>',
|
|
|
|
help='Send <message> with a delay of <delay> seconds.',
|
|
|
|
short='Send a message later',
|
|
|
|
completion=self.completion_delay)
|
2011-11-15 20:35:39 +00:00
|
|
|
|
2015-11-11 19:56:10 +00:00
|
|
|
@command_args_parser.quoted(2)
|
|
|
|
def command_delayed(self, args):
|
|
|
|
if args is None:
|
2016-10-15 23:03:23 +00:00
|
|
|
self.core.command.help('send_delayed')
|
2011-11-15 20:35:39 +00:00
|
|
|
return
|
2015-11-11 19:56:10 +00:00
|
|
|
delay_str, txt = args
|
|
|
|
delay = common.parse_str_to_secs(delay_str)
|
2016-10-15 23:03:23 +00:00
|
|
|
if not delay:
|
|
|
|
self.api.information('Failed to parse %s.' % delay_str, 'Error')
|
2011-11-15 20:35:39 +00:00
|
|
|
return
|
|
|
|
|
2013-03-08 21:53:35 +00:00
|
|
|
tab = self.api.current_tab()
|
2015-11-11 19:56:10 +00:00
|
|
|
timed_event = timed_events.DelayedEvent(delay, self.say, (tab, txt))
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_timed_event(timed_event)
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.information(
|
|
|
|
'Delayed message will be sent in %ds (%s).' % (delay, delay_str),
|
|
|
|
'Info')
|
2011-11-15 20:35:39 +00:00
|
|
|
|
|
|
|
def completion_delay(self, the_input):
|
|
|
|
txt = the_input.get_text()
|
|
|
|
args = common.shell_split(txt)
|
|
|
|
n = len(args)
|
|
|
|
if txt.endswith(' '):
|
|
|
|
n += 1
|
|
|
|
if n == 2:
|
2018-08-15 11:13:17 +00:00
|
|
|
return Completion(the_input.auto_completion,
|
|
|
|
["60", "5m", "15m", "30m", "1h", "10h", "1d"],
|
|
|
|
'')
|
2011-11-15 20:35:39 +00:00
|
|
|
|
|
|
|
def say(self, args=None):
|
|
|
|
if not args:
|
|
|
|
return
|
|
|
|
|
|
|
|
tab = args[0]
|
|
|
|
# anything could happen to the tab during the interval
|
|
|
|
try:
|
|
|
|
tab.command_say(args[1])
|
|
|
|
except:
|
|
|
|
pass
|