2006-06-26 17:57:20 +00:00
|
|
|
###
|
2007-02-04 22:10:10 +00:00
|
|
|
# Copyright (c) 2005-2007 Dennis Kaarsemaker
|
2006-06-26 17:57:20 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of version 2 of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
import supybot.utils as utils
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
|
|
|
import supybot.schedule as schedule
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import pytz
|
|
|
|
import ical
|
2006-10-19 14:39:11 +00:00
|
|
|
import datetime, shelve, re
|
|
|
|
import cPickle as pickle
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
class Webcal(callbacks.Plugin):
|
|
|
|
"""@schedule <timezone>: display the schedule in your timezone"""
|
|
|
|
threaded = True
|
|
|
|
|
|
|
|
def __init__(self, irc):
|
|
|
|
callbacks.Privmsg.__init__(self, irc)
|
|
|
|
self.irc = irc
|
2007-09-17 23:43:05 +00:00
|
|
|
try:
|
|
|
|
schedule.removeEvent(self.name())
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
schedule.removeEvent(self.name() + 'b')
|
|
|
|
except:
|
|
|
|
pass
|
2008-05-05 21:01:58 +00:00
|
|
|
try:
|
|
|
|
schedule.addPeriodicEvent(self.refresh_cache, 60 * 20, name=self.name())
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
schedule.addPeriodicEvent(self.autotopics, 60, name=self.name() + 'b')
|
|
|
|
except:
|
|
|
|
pass
|
2006-06-26 17:57:20 +00:00
|
|
|
self.cache = {}
|
2007-09-17 23:43:05 +00:00
|
|
|
self.firstevent = {}
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
def die(self):
|
2008-05-05 21:01:58 +00:00
|
|
|
try:
|
|
|
|
schedule.removeEvent(self.name())
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
schedule.removeEvent(self.name() + 'b')
|
|
|
|
except:
|
|
|
|
pass
|
2006-06-26 17:57:20 +00:00
|
|
|
self.cache.clear()
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.cache.clear()
|
|
|
|
|
2007-09-17 23:43:05 +00:00
|
|
|
def update(self, url):
|
|
|
|
data = utils.web.getUrl(url)
|
|
|
|
parser = ical.ICalReader(data)
|
|
|
|
self.cache[url] = parser.events
|
|
|
|
|
|
|
|
def refresh_cache(self):
|
|
|
|
for c in self.irc.state.channels:
|
|
|
|
url = self.registryValue('url', c)
|
|
|
|
if url:
|
|
|
|
self.update(url)
|
|
|
|
|
|
|
|
def autotopics(self):
|
|
|
|
for c in self.irc.state.channels:
|
|
|
|
url = self.registryValue('url', c)
|
|
|
|
if url and self.registryValue('doTopic', c):
|
|
|
|
if url not in self.cache:
|
|
|
|
self.update(url)
|
|
|
|
events = self.filter(self.cache[url], c)
|
2008-01-19 17:57:32 +00:00
|
|
|
#if events[0].is_on() and self.firstevent[c].summary == events[0].summary:
|
|
|
|
# continue
|
2007-09-17 23:43:05 +00:00
|
|
|
newtopic = self.maketopic(c, template=self.registryValue('topic',c))
|
|
|
|
if newtopic.strip() != self.irc.state.getTopic(c).strip():
|
|
|
|
self.irc.queueMsg(ircmsgs.topic(c, newtopic))
|
|
|
|
|
|
|
|
def filter(self, events, channel):
|
|
|
|
now = datetime.datetime.now(pytz.UTC)
|
2006-06-29 21:28:13 +00:00
|
|
|
fword = self.registryValue('filter', channel)
|
2007-09-17 23:43:05 +00:00
|
|
|
return [x for x in events if fword.lower() in x.raw_data.lower() and x.seconds_ago() < 1800]
|
2006-06-26 17:57:20 +00:00
|
|
|
|
2007-09-17 23:43:05 +00:00
|
|
|
def maketopic(self, c, tz=None, template='%s', num_events=6):
|
|
|
|
url = self.registryValue('url',c)
|
2006-06-26 17:57:20 +00:00
|
|
|
if url not in self.cache.keys():
|
2007-09-17 23:43:05 +00:00
|
|
|
self.update(url)
|
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
now = datetime.datetime.now(pytz.UTC)
|
2007-09-17 23:43:05 +00:00
|
|
|
events = self.filter(self.cache[url],c)[:num_events]
|
2006-06-26 17:57:20 +00:00
|
|
|
preamble = ''
|
2007-09-17 23:43:05 +00:00
|
|
|
if not len(events):
|
|
|
|
return template % "No meetings scheduled"
|
|
|
|
# The standard slack of 30 minutes after the meeting will be an
|
|
|
|
# error if there are 2 conscutive meetings, so remove the first
|
|
|
|
# one in that case
|
|
|
|
if len(events) > 1 and events[1].startDate < now:
|
2006-06-29 21:28:13 +00:00
|
|
|
events = events[1:]
|
2007-09-17 23:43:05 +00:00
|
|
|
ev0 = events[0]
|
2008-01-19 17:57:32 +00:00
|
|
|
if ev0.seconds_to_go() < 600:
|
|
|
|
preamble = 'Current meeting: %s ' % ev0.summary.replace('Meeting','').strip()
|
2007-09-17 23:43:05 +00:00
|
|
|
if num_events == 1:
|
|
|
|
return preamble + (template % '')
|
|
|
|
events = events[1:]
|
|
|
|
|
2006-06-29 21:28:13 +00:00
|
|
|
if num_events == 1:
|
2007-09-17 23:43:05 +00:00
|
|
|
ev = events[0]
|
|
|
|
return template % ('Next meeting: %s in %s' % (ev.summary.replace(' Meeting','').strip(), ev.time_to_go()))
|
2006-08-18 23:22:54 +00:00
|
|
|
|
2007-09-17 23:43:05 +00:00
|
|
|
events = [x.schedule(tz) for x in events]
|
|
|
|
return preamble + (template % ' | '.join(events))
|
2006-06-29 21:28:13 +00:00
|
|
|
|
2007-09-17 23:43:05 +00:00
|
|
|
# Now the commands
|
2006-06-26 17:57:20 +00:00
|
|
|
def topic(self, irc, msg, args):
|
2007-09-17 23:43:05 +00:00
|
|
|
c = msg.args[0]
|
|
|
|
url = self.registryValue('url', c)
|
|
|
|
if not url or not self.registryValue('doTopic',channel=c):
|
2006-06-26 17:57:20 +00:00
|
|
|
return
|
2007-09-17 23:43:05 +00:00
|
|
|
self.update(url)
|
|
|
|
|
|
|
|
events = self.filter(self.cache[url], c)
|
|
|
|
if events[0].is_on():
|
|
|
|
irc.error("Won't update topic while a meeting is in progress")
|
2007-03-23 19:44:03 +00:00
|
|
|
return
|
2007-09-17 23:43:05 +00:00
|
|
|
|
|
|
|
newtopic = self.maketopic(c, template=self.registryValue('topic',c))
|
|
|
|
if not (newtopic.strip() == irc.state.getTopic(c).strip()):
|
|
|
|
irc.queueMsg(ircmsgs.topic(c, newtopic))
|
2006-06-26 17:57:20 +00:00
|
|
|
topic = wrap(topic)
|
|
|
|
|
|
|
|
def _tzfilter(self, tz, ud):
|
|
|
|
if tz == ud:
|
|
|
|
return True
|
|
|
|
pos = tz.find('/')
|
|
|
|
while not (pos == -1):
|
|
|
|
if tz[pos+1:] == ud:
|
|
|
|
return True
|
|
|
|
pos = tz.find('/',pos+1)
|
2007-02-04 22:10:10 +00:00
|
|
|
# Repeat, with spaces replaced by underscores
|
|
|
|
ud = ud.replace(' ','_')
|
|
|
|
if tz == ud:
|
|
|
|
return True
|
|
|
|
pos = tz.find('/')
|
|
|
|
while not (pos == -1):
|
|
|
|
if tz[pos+1:] == ud:
|
|
|
|
return True
|
|
|
|
pos = tz.find('/',pos+1)
|
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def schedule(self, irc, msg, args, tz):
|
|
|
|
""" Retrieve the date/time of scheduled meetings in a specific timezone """
|
|
|
|
if not tz:
|
|
|
|
tz = 'utc'
|
|
|
|
if irc.isChannel(msg.args[0]):
|
|
|
|
c = msg.args[0]
|
|
|
|
else:
|
|
|
|
c = self.registryValue('defaultChannel')
|
|
|
|
if not c:
|
|
|
|
return
|
|
|
|
url = self.registryValue('url', c)
|
|
|
|
if not url:
|
|
|
|
return
|
|
|
|
tzs = filter(lambda x: self._tzfilter(x.lower(),tz.lower()), pytz.all_timezones)
|
|
|
|
if not tzs or 'gmt' in tz.lower():
|
2007-09-17 23:43:05 +00:00
|
|
|
irc.error('Unknown timezone: %s - Full list: http://ubotu.ubuntu-nl.org/timezones.html' % tz)
|
|
|
|
return
|
|
|
|
newtopic = self.maketopic(c,tz=tzs[0])
|
|
|
|
events = self.filter(self.cache[url], msg.args[0])
|
2008-01-19 17:57:32 +00:00
|
|
|
if events[0].is_on(): # FIXME channel filter
|
2007-09-17 23:43:05 +00:00
|
|
|
irc.error('Please don\'t use @schedule during a meeting')
|
|
|
|
irc.reply('Schedule for %s: %s' % (tzs[0], newtopic), private=True)
|
2006-06-26 17:57:20 +00:00
|
|
|
else:
|
2007-09-17 23:43:05 +00:00
|
|
|
irc.reply('Schedule for %s: %s' % (tzs[0], newtopic))
|
2006-06-26 17:57:20 +00:00
|
|
|
schedule = wrap(schedule, [additional('text')])
|
|
|
|
|
|
|
|
def now(self, irc, msg, args, tz):
|
|
|
|
""" Display the current time """
|
|
|
|
if not tz:
|
|
|
|
tz = 'utc'
|
2007-09-17 23:43:05 +00:00
|
|
|
if irc.isChannel(msg.args[0]):
|
|
|
|
c = msg.args[0]
|
|
|
|
else:
|
|
|
|
c = self.registryValue('defaultChannel')
|
|
|
|
if not c:
|
|
|
|
return
|
|
|
|
url = self.registryValue('url', c)
|
|
|
|
if not url:
|
|
|
|
return
|
2006-06-26 17:57:20 +00:00
|
|
|
tzs = filter(lambda x: self._tzfilter(x.lower(),tz.lower()), pytz.all_timezones)
|
|
|
|
if not tzs or 'gmt' in tz.lower():
|
2007-09-17 23:43:05 +00:00
|
|
|
irc.error('Unknown timezone: %s - Full list: http://ubotu.ubuntu-nl.org/timezones.html' % tz)
|
|
|
|
return
|
|
|
|
now = datetime.datetime.now(pytz.UTC)
|
|
|
|
newtopic = self.maketopic(c,tz=tzs[0],num_events=1)
|
|
|
|
events = self.filter(self.cache[url], msg.args[0])
|
|
|
|
newtopic = 'Current time in %s: %s - %s' % \
|
|
|
|
(tzs[0], datetime.datetime.now(pytz.UTC).astimezone(pytz.timezone(tzs[0])).strftime("%B %d %Y, %H:%M:%S"), newtopic)
|
|
|
|
|
2008-01-19 17:57:32 +00:00
|
|
|
if events[0].is_on(): # Fixme -- channel filter
|
2007-09-17 23:43:05 +00:00
|
|
|
irc.error('Please don\'t use @schedule during a meeting')
|
|
|
|
irc.reply(newtopic, private=True)
|
2006-06-26 17:57:20 +00:00
|
|
|
else:
|
2007-09-17 23:43:05 +00:00
|
|
|
irc.reply(newtopic)
|
2006-06-26 17:57:20 +00:00
|
|
|
now = wrap(now, [additional('text')])
|
|
|
|
time = now
|
|
|
|
|
2006-06-29 21:28:13 +00:00
|
|
|
# Warn people that you manage the topic
|
2006-06-26 17:57:20 +00:00
|
|
|
def doTopic(self, irc, msg):
|
2007-09-17 23:43:05 +00:00
|
|
|
c = msg.args[0]
|
|
|
|
if not self.registryValue('doTopic', c):
|
2006-06-26 17:57:20 +00:00
|
|
|
return
|
2007-09-17 23:43:05 +00:00
|
|
|
url = self.registryValue('url', c)
|
|
|
|
irc.reply("The topic of %s is managed by me and filled with the contents of %s - please don't change manually" %
|
|
|
|
(msg.args[0],url), private=True)
|
2006-06-26 17:57:20 +00:00
|
|
|
Class = Webcal
|