2003-03-12 07:26:59 +01:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-03-12 07:26:59 +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.
|
|
|
|
###
|
|
|
|
|
2003-03-25 07:53:51 +01:00
|
|
|
"""
|
|
|
|
Logs each channel to its own individual logfile.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-09-01 08:06:54 +02:00
|
|
|
import supybot
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
2004-09-01 08:06:54 +02:00
|
|
|
__author__ = supybot.authors.jemfinch
|
2003-11-25 09:23:47 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import time
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
2003-10-05 13:56:46 +02:00
|
|
|
import os
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.world as world
|
|
|
|
import supybot.irclib as irclib
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.callbacks as callbacks
|
2004-03-24 23:50:10 +01:00
|
|
|
|
|
|
|
conf.registerPlugin('ChannelLogger')
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.ChannelLogger,
|
|
|
|
'flushImmediately', registry.Boolean(False, """Determines whether channel
|
|
|
|
logfiles will be flushed anytime they're written to, rather than being
|
|
|
|
buffered by the operating system."""))
|
2004-09-01 20:24:59 +02:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.ChannelLogger,
|
2004-09-01 20:15:57 +02:00
|
|
|
'stripFormatting', registry.Boolean(True, """Determines whether
|
|
|
|
formatting characters (such as bolding, color, etc.) are removed when
|
|
|
|
writing the logs to disk."""))
|
2004-03-25 00:10:55 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.ChannelLogger, 'timestamp',
|
|
|
|
registry.Boolean(True, """Determines whether the logs for this channel are
|
|
|
|
timestamped with the timestamp in supybot.log.timestampFormat."""))
|
2004-03-24 23:50:10 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.ChannelLogger, 'noLogPrefix',
|
|
|
|
registry.String('[nolog]', """Determines what string a message should be
|
|
|
|
prefixed with in order not to be logged. If you don't want any such
|
|
|
|
prefix, just set it to the empty string."""))
|
2004-03-27 21:18:47 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.ChannelLogger, 'rotateLogs',
|
|
|
|
registry.Boolean(False, """Determines whether the bot will automatically
|
2004-08-21 09:29:11 +02:00
|
|
|
rotate the logs for this channel. The bot will rotate logs when the
|
|
|
|
timestamp for the log changes. The timestamp is set according to
|
|
|
|
the 'filenameTimestamp' configuration variable."""))
|
2004-03-27 21:18:47 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.ChannelLogger,
|
|
|
|
'filenameTimestamp', registry.String('%d-%a-%Y', """Determines how to
|
|
|
|
represent the timestamp used for the filename in rotated logs. When this
|
|
|
|
timestamp changes, the old logfiles will be closed and a new one started.
|
|
|
|
The format characters for the timestamp are in the time.strftime docs at
|
2004-04-13 07:24:05 +02:00
|
|
|
python.org. In order for your logs to be rotated, you'll also have to
|
|
|
|
enable supybot.plugins.ChannelLogger.rotateLogs."""))
|
2004-03-27 21:18:47 +01:00
|
|
|
|
2004-09-30 08:03:21 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.ChannelLogger, 'directories',
|
|
|
|
registry.Boolean(True, """Determines whether the bot will partition its
|
|
|
|
channel logs into separate directories based on different criteria."""))
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.ChannelLogger.directories,
|
|
|
|
'network', registry.Boolean(True, """Determines whether the bot will use
|
|
|
|
a network directory if using directories."""))
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.ChannelLogger.directories,
|
|
|
|
'channel', registry.Boolean(True, """Determines whether the bot will use
|
|
|
|
a channel directory if using directories."""))
|
|
|
|
t = conf.registerGlobalValue(conf.supybot.plugins.ChannelLogger.directories,
|
|
|
|
'timestamp', registry.Boolean(False, """Determines whether the bot will use
|
|
|
|
a timestamp (determined by
|
|
|
|
supybot.plugins.ChannelLogger.directories.timestamp.format) if using
|
|
|
|
directories."""))
|
|
|
|
conf.registerGlobalValue(t, 'format', registry.String('%B', """Determines what
|
|
|
|
timestamp format will be used in the directory stucture for channel logs if
|
|
|
|
supybot.plugins.ChannelLogger.directories.timestamp is True."""))
|
|
|
|
|
2004-03-27 21:18:47 +01:00
|
|
|
class FakeLog(object):
|
|
|
|
def flush(self):
|
|
|
|
return
|
|
|
|
def close(self):
|
|
|
|
return
|
|
|
|
def write(self, s):
|
|
|
|
return
|
2004-03-24 23:50:10 +01:00
|
|
|
|
|
|
|
class ChannelLogger(callbacks.Privmsg):
|
2004-09-01 08:23:05 +02:00
|
|
|
noIgnore = True
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self):
|
2004-09-30 08:03:21 +02:00
|
|
|
self.__parent = super(ChannelLogger, self)
|
|
|
|
self.__parent.__init__()
|
2003-08-27 03:42:00 +02:00
|
|
|
self.lastMsg = None
|
2004-03-24 23:50:10 +01:00
|
|
|
self.laststate = None
|
|
|
|
self.logs = ircutils.IrcDict()
|
2003-03-12 07:26:59 +01:00
|
|
|
world.flushers.append(self.flush)
|
|
|
|
|
2003-09-04 23:36:01 +02:00
|
|
|
def die(self):
|
|
|
|
for log in self.logs.itervalues():
|
|
|
|
log.close()
|
2004-03-27 21:18:47 +01:00
|
|
|
world.flushers = [x for x in world.flushers
|
2004-09-10 09:28:07 +02:00
|
|
|
if hasattr(x, 'im_class') and
|
|
|
|
x.im_class is not self.__class__]
|
2003-09-04 23:36:01 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def __call__(self, irc, msg):
|
2003-09-23 18:01:04 +02:00
|
|
|
try:
|
|
|
|
super(self.__class__, self).__call__(irc, msg)
|
|
|
|
if self.lastMsg:
|
|
|
|
self.laststate.addMsg(irc, self.lastMsg)
|
2003-10-15 07:56:32 +02:00
|
|
|
else:
|
|
|
|
self.laststate = irc.state.copy()
|
2003-09-23 18:01:04 +02:00
|
|
|
finally:
|
2003-09-23 18:17:13 +02:00
|
|
|
# We must make sure this always gets updated.
|
2003-09-23 18:01:04 +02:00
|
|
|
self.lastMsg = msg
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
for log in self.logs.itervalues():
|
|
|
|
log.close()
|
2004-03-27 21:18:47 +01:00
|
|
|
self.logs.clear()
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def flush(self):
|
2004-03-27 21:18:47 +01:00
|
|
|
self.checkLogNames()
|
2003-09-04 23:36:01 +02:00
|
|
|
try:
|
|
|
|
for log in self.logs.itervalues():
|
|
|
|
log.flush()
|
|
|
|
except ValueError, e:
|
|
|
|
if e.args[0] != 'I/O operation on a closed file':
|
2003-11-26 19:21:12 +01:00
|
|
|
self.log.exception('Odd exception:')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-03-27 21:18:47 +01:00
|
|
|
def logNameTimestamp(self, channel):
|
|
|
|
format = self.registryValue('filenameTimestamp', channel)
|
|
|
|
return time.strftime(format)
|
|
|
|
|
|
|
|
def getLogName(self, channel):
|
|
|
|
if self.registryValue('rotateLogs', channel):
|
|
|
|
return '%s.%s.log' % (channel, self.logNameTimestamp(channel))
|
|
|
|
else:
|
|
|
|
return '%s.log' % channel
|
|
|
|
|
2004-09-30 08:03:21 +02:00
|
|
|
def getLogDir(self, irc, channel):
|
|
|
|
logDir = conf.supybot.directories.log()
|
|
|
|
logDir = os.path.join(logDir, self.name())
|
|
|
|
if self.registryValue('directories'):
|
|
|
|
if self.registryValue('directories.network'):
|
|
|
|
logDir = os.path.join(logDir, irc.network)
|
|
|
|
if self.registryValue('directories.channel'):
|
|
|
|
logDir = os.path.join(logDir, channel)
|
|
|
|
if self.registryValue('directories.timestamp'):
|
|
|
|
format = self.registryValue('directories.timestamp.format')
|
2004-10-01 20:48:16 +02:00
|
|
|
timeDir =time.strftime(format)
|
2004-09-30 08:03:21 +02:00
|
|
|
logDir = os.path.join(logDir, timeDir)
|
|
|
|
if not os.path.exists(logDir):
|
|
|
|
os.makedirs(logDir)
|
|
|
|
return logDir
|
|
|
|
|
2004-03-27 21:18:47 +01:00
|
|
|
def checkLogNames(self):
|
|
|
|
for (channel, log) in self.logs.items():
|
|
|
|
if self.registryValue('rotateLogs', channel):
|
|
|
|
name = self.getLogName(channel)
|
|
|
|
if name != log.name:
|
|
|
|
log.close()
|
|
|
|
del self.logs[channel]
|
|
|
|
|
2004-09-30 08:03:21 +02:00
|
|
|
def getLog(self, irc, channel):
|
2004-03-27 21:18:47 +01:00
|
|
|
self.checkLogNames()
|
2003-03-12 07:26:59 +01:00
|
|
|
if channel in self.logs:
|
|
|
|
return self.logs[channel]
|
|
|
|
else:
|
|
|
|
try:
|
2004-03-27 21:18:47 +01:00
|
|
|
name = self.getLogName(channel)
|
2004-09-30 08:03:21 +02:00
|
|
|
logDir = self.getLogDir(irc, channel)
|
2004-03-27 21:18:47 +01:00
|
|
|
log = file(os.path.join(logDir, name), 'a')
|
2003-03-12 07:26:59 +01:00
|
|
|
self.logs[channel] = log
|
|
|
|
return log
|
|
|
|
except IOError:
|
2003-11-26 19:21:12 +01:00
|
|
|
self.log.exception('Error opening log:')
|
2004-03-27 21:18:47 +01:00
|
|
|
return FakeLog()
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def timestamp(self, log):
|
2004-03-25 00:05:28 +01:00
|
|
|
format = conf.supybot.log.timestampFormat()
|
|
|
|
if format:
|
|
|
|
log.write(time.strftime(format))
|
|
|
|
log.write(' ')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-04-17 17:53:24 +02:00
|
|
|
def normalizeChannel(self, irc, channel):
|
|
|
|
return ircutils.toLower(channel)
|
|
|
|
|
|
|
|
def doLog(self, irc, channel, s):
|
|
|
|
channel = self.normalizeChannel(irc, channel)
|
2004-09-30 08:03:21 +02:00
|
|
|
log = self.getLog(irc, channel)
|
2004-03-25 00:10:55 +01:00
|
|
|
if self.registryValue('timestamp', channel):
|
|
|
|
self.timestamp(log)
|
2004-09-01 20:15:57 +02:00
|
|
|
if self.registryValue('stripFormatting', channel):
|
|
|
|
s = ircutils.stripFormatting(s)
|
2004-03-24 23:50:10 +01:00
|
|
|
log.write(s)
|
|
|
|
if self.registryValue('flushImmediately'):
|
|
|
|
log.flush()
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def doPrivmsg(self, irc, msg):
|
|
|
|
(recipients, text) = msg.args
|
|
|
|
for channel in recipients.split(','):
|
2003-04-12 14:20:00 +02:00
|
|
|
if ircutils.isChannel(channel):
|
2004-08-20 08:22:33 +02:00
|
|
|
noLogPrefix = self.registryValue('noLogPrefix', channel)
|
|
|
|
if noLogPrefix and text.startswith(noLogPrefix):
|
|
|
|
text = '-= THIS MESSAGE NOT LOGGED =-'
|
2003-06-10 09:31:11 +02:00
|
|
|
nick = msg.nick or irc.nick
|
2003-04-12 14:20:00 +02:00
|
|
|
if ircmsgs.isAction(msg):
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel,
|
2004-03-24 23:50:10 +01:00
|
|
|
'* %s %s\n' % (nick, ircmsgs.unAction(msg)))
|
2003-04-12 14:20:00 +02:00
|
|
|
else:
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel, '<%s> %s\n' % (nick, text))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def doNotice(self, irc, msg):
|
|
|
|
(recipients, text) = msg.args
|
|
|
|
for channel in recipients.split(','):
|
2003-04-12 14:20:00 +02:00
|
|
|
if ircutils.isChannel(channel):
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel, '-%s- %s\n' % (msg.nick, text))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-10-19 19:09:11 +02:00
|
|
|
def doNick(self, irc, msg):
|
|
|
|
oldNick = msg.nick
|
|
|
|
newNick = msg.args[0]
|
|
|
|
for (channel, c) in irc.state.channels.iteritems():
|
|
|
|
if newNick in c.users:
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s is now known as %s\n' % (oldNick, newNick))
|
2003-03-12 07:26:59 +01:00
|
|
|
def doJoin(self, irc, msg):
|
|
|
|
for channel in msg.args[0].split(','):
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel,
|
2004-03-24 23:50:10 +01:00
|
|
|
'*** %s has joined %s\n' %
|
|
|
|
(msg.nick or msg.prefix, channel))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def doKick(self, irc, msg):
|
2003-09-03 20:45:17 +02:00
|
|
|
if len(msg.args) == 3:
|
|
|
|
(channel, target, kickmsg) = msg.args
|
|
|
|
else:
|
|
|
|
(channel, target) = msg.args
|
|
|
|
kickmsg = ''
|
|
|
|
if kickmsg:
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel,
|
2004-03-24 23:50:10 +01:00
|
|
|
'*** %s was kicked by %s (%s)\n' %
|
|
|
|
(target, msg.nick, kickmsg))
|
2003-09-03 20:45:17 +02:00
|
|
|
else:
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel,
|
2004-03-24 23:50:10 +01:00
|
|
|
'*** %s was kicked by %s\n' % (target, msg.nick))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def doPart(self, irc, msg):
|
|
|
|
for channel in msg.args[0].split(','):
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s has left %s\n' % (msg.nick, channel))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def doMode(self, irc, msg):
|
|
|
|
channel = msg.args[0]
|
2004-02-17 20:04:24 +01:00
|
|
|
if ircutils.isChannel(channel) and msg.args[1:]:
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel,
|
2004-03-24 23:50:10 +01:00
|
|
|
'*** %s sets mode: %s %s\n' %
|
|
|
|
(msg.nick or msg.prefix, msg.args[1],
|
|
|
|
' '.join(msg.args[2:])))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def doTopic(self, irc, msg):
|
2003-09-06 04:06:57 +02:00
|
|
|
if len(msg.args) == 1:
|
|
|
|
return # It's an empty TOPIC just to get the current topic.
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = msg.args[0]
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel,
|
2004-03-24 23:50:10 +01:00
|
|
|
'*** %s changes topic to "%s"\n' % (msg.nick, msg.args[1]))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def doQuit(self, irc, msg):
|
|
|
|
for (channel, chan) in self.laststate.channels.iteritems():
|
|
|
|
if msg.nick in chan.users:
|
2004-04-17 17:53:24 +02:00
|
|
|
self.doLog(irc, channel, '*** %s has quit IRC\n' % msg.nick)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
# Gotta catch my own messages *somehow* :)
|
|
|
|
# Let's try this little trick...
|
2004-10-20 10:47:25 +02:00
|
|
|
if msg.command in ('PRIVMSG', 'NOTICE'):
|
|
|
|
# Other messages should be sent back to us.
|
2003-10-15 07:56:32 +02:00
|
|
|
m = ircmsgs.IrcMsg(msg=msg, prefix=irc.prefix)
|
|
|
|
self(irc, m)
|
2003-03-12 07:26:59 +01:00
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
|
|
Class = ChannelLogger
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|