hackbot/plugins/timer.py

152 lines
5 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# file: timer.py
# date: 25.07.2020
# desc: Starts a time for max. 24 hours and sends a message after it has
# expied.
import common
import logging
logging = logging.getLogger()
from manager import Plugin
from threading import Timer, Lock
lock = Lock()
class Plugin(Plugin):
'''
Timer starts a timer and sends a message to muc after it has expired.
'''
__module = 'timer'
__command = 'timer'
__description = 'Starts a timer.'
@staticmethod
def get_module():
return Plugin.__module
@staticmethod
def get_command():
return Plugin.__command
@staticmethod
def get_description():
return Plugin.__description
def __init__(self, callback=None):
self.callback = callback
def help(self):
return ('!timer sets a timer that sends a completion message when it '
'has expired. The function expects a time value and '
'optionally a unit. Possible units are seconds, seconde, '
'sekunden, sec, sek, s, minute, minutes, minuten, min, '
'hour, hours, stunde, stunden or h. If no unit is given it '
'uses seconds as default. The timer runs for a maximum of 24 '
'hours.'
'\nSyntax: !timer <value> <unit>')
def run(self, stanza):
'''
'''
self.units = ('seconde', 'seconds', 'sekunde', 'sekunden',
'sec', 'sek', 's',
'minute', 'minutes', 'minuten', 'min', 'm',
'hour', 'hours', 'h', 'stunde', 'stunden')
call_msg = 'Call "!help {}"'.format(self.get_command())
no_args_msg = ' '.join(('Timer without time!', call_msg))
no_valu_msg = ' '.join(('Not a valid value: "{}"!', call_msg))
no_unit_msg = ' '.join(('Not a valid unit: "{}"!', call_msg))
to_long_msg = 'Sorry ... but i dont want stay here for this time'
value = None
unit = None
muc_nick = common.get_nick_from_stanza(stanza)
arguments = common.get_arguments_from_body(stanza)
logging.debug('Arguments: {}'.format(arguments))
if arguments is False:
logging.warning('No arguments for timer. Abort.')
self.callback(': '.join((muc_nick, no_args_msg)))
return
else:
try:
value = self.get_timer_value(arguments[0])
unit = self.get_timer_unit(arguments[1])
except IndexError:
pass
except Exception as e:
logging.warning('Error while creating timer')
logging.warning('Exception: {}'.format(e))
if value in (None, False):
msg = ': '.join((muc_nick, no_valu_msg.format(arguments[0])))
self.callback(msg)
return
if unit is False:
msg = ': '.join((muc_nick, no_unit_msg.format(arguments[1])))
self.callback(msg)
return
# timer starten
elif unit is None:
self.start_timer(value, self.callback, muc_nick)
else:
value = value * unit
if value > 5184000:
logging.warning('Timer value to hight: {}'.format(value))
self.callback(': '.join((muc_nick, to_long_msg)))
self.start_timer(value, self.callback, muc_nick)
def get_timer_value(self, item):
'''
Try to convert a string into integer.
param 1: string
retuns: integer or false
'''
try:
value = int(item.strip())
return value
except Exception as e:
logging.warning('Invalid value for timer: {}'.format(item))
logging.warning('Exception: {}'.format(e))
return False
def get_timer_unit(self, item):
'''
param 1: string
returns: integer
'''
if item.strip() in self.units[0:7]:
logging.debug('Timer unit: seconds')
factor = 1
elif item.strip() in self.units[7:12]:
logging.debug('Timer unit: minutes')
factor = 60
elif item.strip() in self.units[13:17]:
logging.debug('Timer unit: hours')
factor = 60 * 60
else:
logging.warning('Invalid unit for timer: {}'.format(item.strip()))
factor = False
return factor
def start_timer(self, value, callback, muc_nick):
'''
Starts the timer. Arguments are the duration in seconds and the
chatters nick who called timer.
param 1: integer
param 2: string
'''
timer_start_msg = 'Timer started'
timer_ends_msg = '{}: Timer finished!'.format(muc_nick)
t = Timer(value, callback, [timer_ends_msg])
t.start()
logging.debug('Timer started for {} seconds'.format(value))
self.callback(': '.join((muc_nick, timer_start_msg)))