2005-02-01 07:08:46 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2003-2004, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.schedule as schedule
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-19 19:50:41 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('Scheduler')
|
2005-02-01 07:08:46 +01:00
|
|
|
|
2005-02-09 08:04:04 +01:00
|
|
|
class Scheduler(callbacks.Plugin):
|
2005-02-01 07:08:46 +01:00
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(Scheduler, self)
|
|
|
|
self.__parent.__init__(irc)
|
|
|
|
self.events = {}
|
2010-09-04 07:25:57 +02:00
|
|
|
self._restoreEvents(irc)
|
|
|
|
world.flushers.append(self._flush)
|
|
|
|
|
|
|
|
def _restoreEvents(self, irc):
|
|
|
|
try:
|
|
|
|
pkl = open(filename, 'rb')
|
|
|
|
try:
|
|
|
|
eventdict = pickle.load(pkl)
|
|
|
|
except Exception, e:
|
|
|
|
self.log.debug('Unable to load pickled data: %s', e)
|
|
|
|
return
|
|
|
|
finally:
|
|
|
|
pkl.close()
|
|
|
|
except IOError, e:
|
|
|
|
self.log.debug('Unable to open pickle file: %s', e)
|
|
|
|
return
|
|
|
|
for name, event in eventdict.iteritems():
|
|
|
|
ircobj = callbacks.ReplyIrcProxy(irc, event['msg'])
|
|
|
|
try:
|
|
|
|
if event['type'] == 'single': # non-repeating event
|
|
|
|
n = None
|
|
|
|
if schedule.schedule.counter > int(name):
|
|
|
|
# counter not reset, we're probably reloading the plugin
|
|
|
|
# though we'll never know for sure, because other
|
|
|
|
# plugins can schedule stuff, too.
|
|
|
|
n = int(name)
|
|
|
|
self._add(ircobj, event['msg'],
|
|
|
|
event['time'], event['command'], n)
|
|
|
|
elif event['type'] == 'repeat': # repeating event
|
|
|
|
self._repeat(ircobj, event['msg'], name,
|
|
|
|
event['time'], event['command'], False)
|
|
|
|
except AssertionError, e:
|
|
|
|
if str(e) == 'An event with the same name has already been scheduled.':
|
|
|
|
# we must be reloading the plugin, event is still scheduled
|
|
|
|
self.log.info('Event %s already exists, adding to dict.' % (name,))
|
|
|
|
self.events[name] = event
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def _flush(self):
|
|
|
|
try:
|
|
|
|
pklfd, tempfn = tempfile.mkstemp(suffix='scheduler', dir=datadir)
|
|
|
|
pkl = os.fdopen(pklfd, 'wb')
|
|
|
|
try:
|
|
|
|
pickle.dump(self.events, pkl)
|
|
|
|
except Exception, e:
|
|
|
|
self.log.warning('Unable to store pickled data: %s', e)
|
|
|
|
pkl.close()
|
|
|
|
shutil.move(tempfn, filename)
|
|
|
|
except (IOError, shutil.Error), e:
|
|
|
|
self.log.warning('File error: %s', e)
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
self._flush()
|
|
|
|
world.flushers.remove(self._flush)
|
|
|
|
self.__parent.die()
|
2005-02-01 07:08:46 +01:00
|
|
|
|
|
|
|
def _makeCommandFunction(self, irc, msg, command, remove=True):
|
|
|
|
"""Makes a function suitable for scheduling from command."""
|
|
|
|
tokens = callbacks.tokenize(command)
|
|
|
|
def f():
|
|
|
|
if remove:
|
|
|
|
del self.events[str(f.eventId)]
|
|
|
|
self.Proxy(irc.irc, msg, tokens)
|
|
|
|
return f
|
|
|
|
|
2010-10-19 19:50:41 +02:00
|
|
|
@internationalizeDocstring
|
2005-02-01 07:08:46 +01:00
|
|
|
def add(self, irc, msg, args, seconds, command):
|
|
|
|
"""<seconds> <command>
|
|
|
|
|
|
|
|
Schedules the command string <command> to run <seconds> seconds in the
|
|
|
|
future. For example, 'scheduler add [seconds 30m] "echo [cpu]"' will
|
|
|
|
schedule the command "cpu" to be sent to the channel the schedule add
|
|
|
|
command was given in (with no prefixed nick, a consequence of using
|
|
|
|
echo). Do pay attention to the quotes in that example.
|
|
|
|
"""
|
|
|
|
f = self._makeCommandFunction(irc, msg, command)
|
|
|
|
id = schedule.addEvent(f, time.time() + seconds)
|
|
|
|
f.eventId = id
|
|
|
|
self.events[str(id)] = command
|
2010-10-19 19:50:41 +02:00
|
|
|
irc.replySuccess(format(_('Event #%i added.'), id))
|
2005-02-01 07:08:46 +01:00
|
|
|
add = wrap(add, ['positiveInt', 'text'])
|
|
|
|
|
2010-10-19 19:50:41 +02:00
|
|
|
@internationalizeDocstring
|
2005-02-01 07:08:46 +01:00
|
|
|
def remove(self, irc, msg, args, id):
|
|
|
|
"""<id>
|
|
|
|
|
|
|
|
Removes the event scheduled with id <id> from the schedule.
|
|
|
|
"""
|
|
|
|
if id in self.events:
|
|
|
|
del self.events[id]
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
schedule.removeEvent(id)
|
|
|
|
irc.replySuccess()
|
|
|
|
except KeyError:
|
2010-10-19 19:50:41 +02:00
|
|
|
irc.error(_('Invalid event id.'))
|
2005-02-01 07:08:46 +01:00
|
|
|
else:
|
2010-10-19 19:50:41 +02:00
|
|
|
irc.error(_('Invalid event id.'))
|
2005-02-01 07:08:46 +01:00
|
|
|
remove = wrap(remove, ['lowered'])
|
|
|
|
|
2010-09-04 07:25:57 +02:00
|
|
|
def _repeat(self, irc, msg, name, seconds, command, now=True):
|
|
|
|
f = self._makeCommandFunction(irc, msg, command, remove=False)
|
|
|
|
id = schedule.addPeriodicEvent(f, seconds, name, now)
|
|
|
|
assert id == name
|
|
|
|
self.events[name] = {'command':command,
|
|
|
|
'msg':msg,
|
|
|
|
'time':seconds,
|
|
|
|
'type':'repeat'}
|
|
|
|
|
2010-10-19 19:50:41 +02:00
|
|
|
@internationalizeDocstring
|
2005-02-01 07:08:46 +01:00
|
|
|
def repeat(self, irc, msg, args, name, seconds, command):
|
|
|
|
"""<name> <seconds> <command>
|
|
|
|
|
|
|
|
Schedules the command <command> to run every <seconds> seconds,
|
|
|
|
starting now (i.e., the command runs now, and every <seconds> seconds
|
|
|
|
thereafter). <name> is a name by which the command can be
|
|
|
|
unscheduled.
|
|
|
|
"""
|
|
|
|
name = name.lower()
|
|
|
|
if name in self.events:
|
2010-10-19 19:50:41 +02:00
|
|
|
irc.error(_('There is already an event with that name, please '
|
|
|
|
'choose another name.'), Raise=True)
|
2005-02-01 07:08:46 +01:00
|
|
|
self.events[name] = command
|
|
|
|
f = self._makeCommandFunction(irc, msg, command, remove=False)
|
|
|
|
id = schedule.addPeriodicEvent(f, seconds, name)
|
|
|
|
assert id == name
|
|
|
|
# We don't reply because the command runs immediately.
|
|
|
|
# But should we? What if the command doesn't have visible output?
|
|
|
|
# irc.replySuccess()
|
|
|
|
repeat = wrap(repeat, ['nonInt', 'positiveInt', 'text'])
|
|
|
|
|
2010-10-19 19:50:41 +02:00
|
|
|
@internationalizeDocstring
|
2005-02-01 07:08:46 +01:00
|
|
|
def list(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Lists the currently scheduled events.
|
|
|
|
"""
|
|
|
|
L = self.events.items()
|
|
|
|
if L:
|
|
|
|
L.sort()
|
|
|
|
for (i, (name, command)) in enumerate(L):
|
|
|
|
L[i] = format('%s: %q', name, command)
|
|
|
|
irc.reply(format('%L', L))
|
|
|
|
else:
|
2010-10-19 19:50:41 +02:00
|
|
|
irc.reply(_('There are currently no scheduled commands.'))
|
2005-02-01 07:08:46 +01:00
|
|
|
list = wrap(list)
|
|
|
|
|
|
|
|
|
|
|
|
Class = Scheduler
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|