2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, 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.
|
|
|
|
###
|
|
|
|
|
2003-03-25 07:53:51 +01:00
|
|
|
"""
|
|
|
|
Logs each channel to its own individual logfile.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import 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
|
2003-03-12 07:26:59 +01:00
|
|
|
import conf
|
|
|
|
import world
|
|
|
|
import irclib
|
|
|
|
import ircmsgs
|
2003-04-11 22:42:21 +02:00
|
|
|
import ircutils
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
###
|
|
|
|
# Logger: Handles logging of IRC channels to files.
|
|
|
|
###
|
|
|
|
class ChannelLogger(irclib.IrcCallback):
|
2003-04-11 22:42:21 +02:00
|
|
|
logs = ircutils.IrcDict()
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self):
|
2003-10-15 07:56:32 +02:00
|
|
|
self.laststate = None
|
2003-08-27 03:42:00 +02:00
|
|
|
self.lastMsg = None
|
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()
|
|
|
|
world.flushers.remove(self.flush)
|
|
|
|
|
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()
|
2003-04-11 22:42:21 +02:00
|
|
|
self.logs = ircutils.IrcDict()
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def flush(self):
|
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
|
|
|
|
|
|
|
def getLog(self, channel):
|
|
|
|
if channel in self.logs:
|
|
|
|
return self.logs[channel]
|
|
|
|
else:
|
|
|
|
try:
|
2004-01-18 08:58:26 +01:00
|
|
|
logDir = conf.supybot.directories.log()
|
|
|
|
log = file(os.path.join(logDir, '%s.log' % channel), '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:')
|
2003-03-12 07:26:59 +01:00
|
|
|
return StringIO()
|
|
|
|
|
|
|
|
def timestamp(self, log):
|
2004-01-18 08:58:26 +01:00
|
|
|
log.write(time.strftime(conf.supybot.log.timestampFormat()))
|
2003-03-12 07:26:59 +01:00
|
|
|
log.write(' ')
|
|
|
|
|
|
|
|
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):
|
|
|
|
log = self.getLog(channel)
|
|
|
|
self.timestamp(log)
|
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):
|
2003-06-10 09:31:11 +02:00
|
|
|
log.write('* %s %s\n' % (nick, ircmsgs.unAction(msg)))
|
2003-04-12 14:20:00 +02:00
|
|
|
else:
|
2003-06-10 09:31:11 +02:00
|
|
|
log.write('<%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):
|
|
|
|
log = self.getLog(channel)
|
|
|
|
self.timestamp(log)
|
|
|
|
log.write('-%s- %s\n' % (msg.nick, text))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def doJoin(self, irc, msg):
|
|
|
|
for channel in msg.args[0].split(','):
|
|
|
|
log = self.getLog(channel)
|
|
|
|
self.timestamp(log)
|
2004-01-09 00:03:48 +01:00
|
|
|
log.write('*** %s has joined %s\n' %
|
2003-03-12 07:26:59 +01:00
|
|
|
(msg.nick or msg.prefix, channel))
|
|
|
|
|
|
|
|
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 = ''
|
2003-03-12 07:26:59 +01:00
|
|
|
log = self.getLog(channel)
|
|
|
|
self.timestamp(log)
|
2003-09-03 20:45:17 +02:00
|
|
|
if kickmsg:
|
2004-01-09 00:03:48 +01:00
|
|
|
log.write('*** %s was kicked by %s (%s)\n' %
|
2003-09-03 20:45:17 +02:00
|
|
|
(target, msg.nick, kickmsg))
|
|
|
|
else:
|
2003-09-04 23:37:38 +02:00
|
|
|
log.write('*** %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(','):
|
|
|
|
log = self.getLog(channel)
|
|
|
|
self.timestamp(log)
|
|
|
|
log.write('*** %s has left %s\n' % (msg.nick, channel))
|
|
|
|
|
|
|
|
def doMode(self, irc, msg):
|
|
|
|
channel = msg.args[0]
|
2003-04-12 14:20:00 +02:00
|
|
|
if ircutils.isChannel(channel):
|
|
|
|
log = self.getLog(channel)
|
|
|
|
self.timestamp(log)
|
2004-01-09 00:03:48 +01:00
|
|
|
log.write('*** %s sets mode: %s %s\n' %
|
2003-04-12 14:20:00 +02:00
|
|
|
(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]
|
|
|
|
log = self.getLog(channel)
|
|
|
|
self.timestamp(log)
|
|
|
|
log.write('*** %s changes topic to "%s"\n' % (msg.nick, msg.args[1]))
|
|
|
|
|
|
|
|
def doQuit(self, irc, msg):
|
|
|
|
for (channel, chan) in self.laststate.channels.iteritems():
|
|
|
|
if msg.nick in chan.users:
|
|
|
|
log = self.getLog(channel)
|
|
|
|
log.write('*** %s has quit IRC\n' % msg.nick)
|
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
# Gotta catch my own messages *somehow* :)
|
|
|
|
# Let's try this little trick...
|
2003-10-15 07:56:32 +02:00
|
|
|
if msg.command != 'PART':
|
|
|
|
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:
|