2004-01-01 20:45:15 +01:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2003-2004, Jeremiah Fincher
|
2004-01-01 20:45:15 +01:00
|
|
|
# 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Gives the user the ability to schedule commands to run at a particular time,
|
|
|
|
or repeatedly run at a particular interval.
|
|
|
|
"""
|
|
|
|
|
2004-09-01 08:06:54 +02:00
|
|
|
import supybot
|
|
|
|
|
2004-03-19 18:13:33 +01:00
|
|
|
__revision__ = "$Id$"
|
2004-09-01 08:06:54 +02:00
|
|
|
__author__ = supybot.authors.jemfinch
|
2004-03-19 18:13:33 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2004-01-01 20:45:15 +01:00
|
|
|
|
2004-01-08 01:44:35 +01:00
|
|
|
import sets
|
2004-01-01 20:45:15 +01:00
|
|
|
import time
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
2004-10-23 06:55:52 +02:00
|
|
|
from supybot.commands import *
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.schedule as schedule
|
|
|
|
import supybot.callbacks as callbacks
|
2004-01-01 20:45:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Scheduler(callbacks.Privmsg):
|
2004-01-08 01:44:35 +01:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
|
|
|
self.events = {}
|
|
|
|
|
2004-03-29 18:52:42 +02:00
|
|
|
def _makeCommandFunction(self, irc, msg, command, remove=True):
|
2004-01-01 20:45:15 +01:00
|
|
|
"""Makes a function suitable for scheduling from command."""
|
|
|
|
tokens = callbacks.tokenize(command)
|
|
|
|
def f():
|
2004-03-29 18:45:09 +02:00
|
|
|
if remove:
|
2004-05-27 18:47:05 +02:00
|
|
|
del self.events[str(f.eventId)]
|
2004-01-01 20:45:15 +01:00
|
|
|
self.Proxy(irc.irc, msg, tokens)
|
|
|
|
return f
|
|
|
|
|
2004-10-23 06:55:52 +02:00
|
|
|
def add(self, irc, msg, args, seconds, command):
|
2004-01-01 20:45:15 +01:00
|
|
|
"""<seconds> <command>
|
|
|
|
|
|
|
|
Schedules the command string <command> to run <seconds> seconds in the
|
2004-05-27 18:47:05 +02:00
|
|
|
future. For example, 'scheduler add [seconds 30m] "echo [cpu]"' will
|
2004-01-01 20:45:15 +01:00
|
|
|
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
|
2004-09-22 23:38:20 +02:00
|
|
|
echo). Do pay attention to the quotes in that example.
|
2004-01-01 20:45:15 +01:00
|
|
|
"""
|
|
|
|
f = self._makeCommandFunction(irc, msg, command)
|
|
|
|
id = schedule.addEvent(f, time.time() + seconds)
|
2004-03-29 18:26:09 +02:00
|
|
|
f.eventId = id
|
2004-01-08 01:44:35 +01:00
|
|
|
self.events[str(id)] = command
|
2004-01-09 00:03:48 +01:00
|
|
|
irc.replySuccess('Event #%s added.' % id)
|
2004-10-23 06:55:52 +02:00
|
|
|
add = wrap(add, ['positiveInt', 'text'])
|
2004-01-01 20:45:15 +01:00
|
|
|
|
2004-10-23 06:55:52 +02:00
|
|
|
def remove(self, irc, msg, args, id):
|
2004-01-01 20:45:15 +01:00
|
|
|
"""<id>
|
|
|
|
|
|
|
|
Removes the event scheduled with id <id> from the schedule.
|
|
|
|
"""
|
2004-01-08 01:44:35 +01:00
|
|
|
if id in self.events:
|
|
|
|
del self.events[id]
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
schedule.removeEvent(id)
|
2004-01-09 00:03:48 +01:00
|
|
|
irc.replySuccess()
|
2004-01-08 01:44:35 +01:00
|
|
|
except KeyError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('Invalid event id.')
|
2004-01-08 01:44:35 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('Invalid event id.')
|
2004-10-23 06:55:52 +02:00
|
|
|
remove = wrap(remove, ['lowered'])
|
2004-01-01 20:45:15 +01:00
|
|
|
|
2004-10-23 06:55:52 +02:00
|
|
|
def repeat(self, irc, msg, args, name, seconds, command):
|
2004-01-01 20:45:15 +01:00
|
|
|
"""<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.
|
|
|
|
"""
|
2004-12-16 10:50:42 +01:00
|
|
|
name = name.lower()
|
2004-07-21 21:36:35 +02:00
|
|
|
self.events[name] = command
|
2004-03-29 18:45:09 +02:00
|
|
|
f = self._makeCommandFunction(irc, msg, command, remove=False)
|
2004-01-01 20:45:15 +01:00
|
|
|
id = schedule.addPeriodicEvent(f, seconds, name)
|
2004-01-08 01:44:35 +01:00
|
|
|
assert id == name
|
2004-01-04 16:43:49 +01:00
|
|
|
# We don't reply because the command runs immediately.
|
2004-03-29 18:26:09 +02:00
|
|
|
# But should we? What if the command doesn't have visible output?
|
2004-01-08 04:12:14 +01:00
|
|
|
# irc.replySuccess()
|
2004-10-23 06:55:52 +02:00
|
|
|
repeat = wrap(repeat, ['nonInt', 'positiveInt', 'text'])
|
2004-01-01 20:45:15 +01:00
|
|
|
|
2004-01-08 01:44:35 +01:00
|
|
|
def list(self, irc, msg, args):
|
2004-08-19 13:58:17 +02:00
|
|
|
"""takes no arguments
|
2004-01-08 01:44:35 +01:00
|
|
|
|
|
|
|
Lists the currently scheduled events.
|
|
|
|
"""
|
|
|
|
L = self.events.items()
|
|
|
|
if L:
|
|
|
|
L.sort()
|
|
|
|
for (i, (name, command)) in enumerate(L):
|
|
|
|
L[i] = '%s: %s' % (name, utils.dqrepr(command))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify(L))
|
2004-01-08 01:44:35 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('There are currently no scheduled commands.')
|
2004-10-23 06:55:52 +02:00
|
|
|
list = wrap(list)
|
2004-01-08 01:44:35 +01:00
|
|
|
|
2004-01-01 20:45:15 +01:00
|
|
|
|
|
|
|
Class = Scheduler
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|