dr.botzo/modules/Countdown.py

88 lines
3.8 KiB
Python
Raw Normal View History

2010-07-28 23:47:29 -05:00
# Countdown - track and display time until events
# Copyright (C) 2010 Brian S. Stephan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2010-07-27 20:35:01 -05:00
from ConfigParser import NoOptionError
from datetime import datetime
2010-07-27 20:35:01 -05:00
from dateutil.parser import *
from dateutil.relativedelta import *
from dateutil.tz import *
from irclib import irclib
from Module import Module
2010-07-29 00:04:01 -05:00
# Class that adds a countdown item to the bot
2010-07-27 20:35:01 -05:00
class Countdown(Module):
def register_handlers(self, server):
server.add_global_handler('pubmsg', self.on_pubmsg)
server.add_global_handler('privmsg', self.on_privmsg)
def unregister_handlers(self):
self.server.remove_global_handler('pubmsg', self.on_pubmsg)
self.server.remove_global_handler('privmsg', self.on_privmsg)
2010-07-27 20:35:01 -05:00
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
whats = what.split(' ')
if whats[0] == 'countdown' and len(whats) >= 2:
if whats[1] == 'add' and len(whats) >= 4:
item = whats[2]
target = parse(' '.join(whats[3:]), default=datetime.now().replace(tzinfo=tzlocal()))
if not self.config.has_section('countdown'):
self.config.add_section('countdown')
self.config.set('countdown', item, target.astimezone(tzutc()).isoformat())
replystr = 'added countdown item ' + whats[2]
return self.reply(connection, replypath, replystr)
2010-07-27 20:35:01 -05:00
elif whats[1] == 'remove':
try:
if self.config.remove_option('countdown', whats[2]):
replystr = 'removed countdown item ' + whats[2]
return self.reply(connection, replypath, replystr)
2010-07-27 20:35:01 -05:00
except NoSectionError: pass
elif whats[1] == 'list':
try:
cdlist = self.config.options('countdown')
cdlist.remove('debug')
cdlist.sort()
liststr = ', '.join(cdlist)
return self.reply(connection, replypath, liststr)
2010-07-27 20:35:01 -05:00
except NoSectionError: pass
else:
try:
timestr = self.config.get('countdown', whats[1])
time = parse(timestr)
rdelta = relativedelta(time, datetime.now().replace(tzinfo=tzlocal()))
relstr = whats[1] + ' will occur in '
if rdelta.years != 0:
relstr += str(rdelta.years) + ' years '
if rdelta.months != 0:
relstr += str(rdelta.months) + ' months '
if rdelta.days != 0:
relstr += str(rdelta.days) + ' days '
if rdelta.hours != 0:
relstr += str(rdelta.hours) + ' hours '
if rdelta.minutes != 0:
relstr += str(rdelta.minutes) + ' minutes '
if rdelta.seconds != 0:
relstr += str(rdelta.seconds) + ' seconds'
return self.reply(connection, replypath, relstr)
2010-07-27 20:35:01 -05:00
except NoOptionError: pass
2010-07-28 23:48:47 -05:00
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;