Limnoria/plugins/Tail.py

194 lines
7.2 KiB
Python
Raw Normal View History

2004-08-02 14:39:36 +02:00
###
# Copyright (c) 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.
###
"""
2004-08-03 06:39:11 +02:00
Messages a list of targets when new lines are added to any of a list of files,
much like "tail -f" does on UNIX.
2004-08-02 14:39:36 +02:00
"""
import supybot
2004-08-02 14:39:36 +02:00
__revision__ = "$Id$"
__author__ = supybot.authors.jemfinch
2004-08-02 14:39:36 +02:00
import supybot.plugins as plugins
2004-08-03 06:39:11 +02:00
import os
import getopt
2004-08-02 14:39:36 +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-08-02 14:39:36 +02:00
import supybot.ircutils as ircutils
import supybot.privmsgs as privmsgs
import supybot.registry as registry
2004-08-03 06:39:11 +02:00
import supybot.schedule as schedule
2004-08-02 14:39:36 +02:00
import supybot.callbacks as callbacks
import supybot.plugins.LogToIrc as LogToIrc
def configure(advanced):
# This will be called by setup.py to configure this module. Advanced is
# a bool that specifies whether the user identified himself as an advanced
# user or not. You should effect your configuration by manipulating the
# registry as appropriate.
from supybot.questions import expect, anything, something, yn
conf.registerPlugin('Tail', True)
conf.registerPlugin('Tail')
conf.registerGlobalValue(conf.supybot.plugins.Tail, 'targets',
LogToIrc.Targets([], """Determines what targets will be messaged with lines
from the files being tailed."""))
conf.registerGlobalValue(conf.supybot.plugins.Tail, 'bold',
registry.Boolean(False, """Determines whether the bot will bold the filename
in tail lines announced to the channel."""))
conf.registerGlobalValue(conf.supybot.plugins.Tail, 'files',
registry.SpaceSeparatedSetOfStrings([], """Determines what files the bot
will tail to its targets."""))
conf.registerGlobalValue(conf.supybot.plugins.Tail, 'notice',
registry.Boolean(False, """Determines whether the bot will send its tail
messages to the targets via NOTICEs rather than PRIVMSGs."""))
2004-08-03 06:39:11 +02:00
conf.registerGlobalValue(conf.supybot.plugins.Tail, 'period',
registry.PositiveInteger(60, """Determines how often the bot will check
the files that are being tailed. The number is in seconds. This plugin
must be reloaded for changes to this period to take effect."""))
2004-08-02 14:39:36 +02:00
class Tail(privmsgs.CapabilityCheckingPrivmsg):
capability = 'owner'
def __init__(self):
privmsgs.CapabilityCheckingPrivmsg.__init__(self)
2004-08-03 06:39:11 +02:00
self.files = {}
period = self.registryValue('period')
schedule.addPeriodicEvent(self._checkFiles, period, name=self.name())
2004-08-02 14:39:36 +02:00
for filename in self.registryValue('files'):
self._add(filename)
2004-08-03 06:39:11 +02:00
def die(self):
schedule.removeEvent(self.name())
2004-08-04 14:39:24 +02:00
for fd in self.files.values():
fd.close()
2004-08-03 06:39:11 +02:00
2004-08-02 14:39:36 +02:00
def __call__(self, irc, msg):
irc = callbacks.SimpleProxy(irc, msg)
self.lastIrc = irc
self.lastMsg = msg
def _checkFiles(self):
2004-08-13 14:31:38 +02:00
self.log.debug('Checking files.')
for filename in self.registryValue('files'):
self._checkFile(filename)
def _checkFile(self, filename):
2004-08-03 06:39:11 +02:00
fd = self.files[filename]
pos = fd.tell()
line = fd.readline()
while line:
line = line.strip()
if line:
self._send(self.lastIrc, filename, line)
2004-08-03 06:39:11 +02:00
pos = fd.tell()
2004-08-02 14:39:36 +02:00
line = fd.readline()
2004-08-03 06:39:11 +02:00
fd.seek(pos)
2004-08-02 14:39:36 +02:00
def _add(self, filename):
try:
fd = file(filename)
except EnvironmentError, e:
self.log.warning('Couldn\'t open %s: %s', filename, e)
2004-08-03 06:39:11 +02:00
raise
2004-08-02 14:39:36 +02:00
fd.seek(0, 2) # 0 bytes, offset from the end of the file.
2004-08-03 06:39:11 +02:00
self.files[filename] = fd
2004-08-02 14:39:36 +02:00
self.registryValue('files').add(filename)
def _remove(self, filename):
2004-08-03 06:39:11 +02:00
fd = self.files.pop(filename)
fd.close()
self.registryValue('files').remove(filename)
2004-08-02 14:39:36 +02:00
def _send(self, irc, filename, text):
if self.registryValue('bold'):
filename = ircutils.bold(filename)
notice = self.registryValue('notice')
payload = '%s: %s' % (filename, text)
for target in self.registryValue('targets'):
2004-08-03 06:39:11 +02:00
irc.reply(payload, to=target, notice=notice, private=True)
2004-10-23 06:55:52 +02:00
def add(self, irc, msg, args, filename):
2004-08-02 14:39:36 +02:00
"""<filename>
Basically does the equivalent of tail -f to the targets.
"""
try:
self._add(filename)
except EnvironmentError, e:
irc.error(utils.exnTostring(e))
return
irc.replySuccess()
2004-10-23 06:55:52 +02:00
add = wrap(add, ['filename'])
2004-08-02 14:39:36 +02:00
2004-10-23 06:55:52 +02:00
def remove(self, irc, msg, args, filename):
2004-08-02 14:39:36 +02:00
"""<filename>
Stops announcing the lines appended to <filename>.
"""
try:
self._remove(filename)
2004-08-02 14:39:36 +02:00
irc.replySuccess()
except KeyError:
irc.error('I\'m not currently announcing %s.' % filename)
2004-10-23 06:55:52 +02:00
remove = wrap(remove, ['filename'])
2004-10-23 06:55:52 +02:00
def target(self, irc, msg, args, optlist, targets):
"""[--remove] [<target> ...]
If given no arguments, returns the current list of targets for this
plugin. If given any number of targets, will add these targets to
the current list of targets. If given --remove and any number of
targets, will remove those targets from the current list of targets.
"""
remove = False
for (option, arg) in optlist:
2004-10-23 06:55:52 +02:00
if option == 'remove':
remove = True
2004-10-23 06:55:52 +02:00
if not targets:
L = self.registryValue('targets')
if L:
utils.sortBy(ircutils.toLower, L)
irc.reply(utils.commaAndify(L))
else:
2004-08-19 01:15:27 +02:00
irc.reply('I\'m not currently targeting anywhere.')
elif remove:
pass #XXX
2004-10-23 06:55:52 +02:00
target = wrap(target, [getopts({'remove': ''}), any('something')])
2004-08-02 14:39:36 +02:00
Class = Tail
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: