dr.botzo/modules/Countdown.py
Brian S. Stephan 0bd681c324 convert most modules to use ! commands.
okay, it's time. we got around for a while with all sorts of silly
config options and exceptions and common strings triggering bot
commands. but now it's time to man up and expect modules to be
more strict and less loosey-goosey.

convert the popular modules (i.e. the ones that still work) to
trigger on !pi rather than pi, etc. usually, this is achieved via
regex searches, although there are some weird bugs (ones i'm hoping
are caused by other recursion/alias bugs and not this commit).

more code around this will be gutted soon, but this, at least,
means you can't say 'tweet that shit, yo' and accidentally trigger
the bot.
2011-01-07 20:37:24 -06:00

111 lines
4.3 KiB
Python

"""
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/>.
"""
from ConfigParser import NoOptionError, NoSectionError
from datetime import datetime
import re
from dateutil.parser import *
from dateutil.relativedelta import *
from dateutil.tz import *
from extlib import irclib
from Module import Module
class Countdown(Module):
"""Class that adds a countdown item to the bot."""
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""Add/retrieve countdown items."""
match = re.search('^!countdown\s+add\s+(\S+)\s+(.*)$', what)
if match:
item = match.group(1)
target = parse(match.group(2), default=datetime.now().replace(tzinfo=tzlocal()))
if not self.config.has_section(self.__class__.__name__):
self.config.add_section(self.__class__.__name__)
self.config.set(self.__class__.__name__, item, target.astimezone(tzutc()).isoformat())
replystr = 'added countdown item ' + item
return self.reply(connection, replypath, replystr)
match = re.search('^!countdown\s+remove\s+(\S+)$', what)
if match:
try:
item = match.group(1)
if self.config.remove_option(self.__class__.__name__, item):
replystr = 'removed countdown item ' + item
return self.reply(connection, replypath, replystr)
except NoSectionError: pass
match = re.search('^!countdown\s+list$', what)
if match:
try:
cdlist = self.config.options(self.__class__.__name__)
self.remove_metaoptions(cdlist)
cdlist.sort()
liststr = ', '.join(cdlist)
return self.reply(connection, replypath, liststr)
except NoSectionError: pass
match = re.search('^!countdown\s+(\S+)$', what)
if match:
try:
item = match.group(1)
timestr = self.config.get(self.__class__.__name__, item)
time = parse(timestr)
rdelta = relativedelta(time, datetime.now().replace(tzinfo=tzlocal()))
relstr = item + ' will occur in '
if rdelta.years != 0:
relstr += str(rdelta.years) + ' years '
if rdelta.years > 1:
relstr += 's'
relstr += ', '
if rdelta.months != 0:
relstr += str(rdelta.months) + ' month'
if rdelta.months > 1:
relstr += 's'
relstr += ', '
if rdelta.days != 0:
relstr += str(rdelta.days) + ' day'
if rdelta.days > 1:
relstr += 's'
relstr += ', '
if rdelta.hours != 0:
relstr += str(rdelta.hours) + ' hour'
if rdelta.hours > 1:
relstr += 's'
relstr += ', '
if rdelta.minutes != 0:
relstr += str(rdelta.minutes) + ' minute'
if rdelta.minutes > 1:
relstr += 's'
relstr += ', '
if rdelta.seconds != 0:
relstr += str(rdelta.seconds) + ' second'
if rdelta.seconds > 1:
relstr += 's'
relstr += ', '
return self.reply(connection, replypath, relstr[0:-2])
except NoOptionError: pass
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;