2005-02-02 13:37:16 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2009-2010, James McCoy
|
2005-02-02 13:37:16 +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.
|
|
|
|
###
|
|
|
|
|
|
|
|
import os
|
2014-02-04 00:40:54 +01:00
|
|
|
import sys
|
2005-02-02 13:37:16 +01:00
|
|
|
import time
|
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.world as world
|
2013-04-26 17:52:18 +02:00
|
|
|
import supybot.ircdb as ircdb
|
2019-09-06 20:15:18 +02:00
|
|
|
import supybot.utils as utils
|
2005-02-02 13:37:16 +01:00
|
|
|
import supybot.irclib as irclib
|
2015-08-11 16:50:23 +02:00
|
|
|
import supybot.utils.minisix as minisix
|
2005-02-02 13:37:16 +01:00
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-17 11:22:07 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('ChannelLogger')
|
2005-02-02 13:37:16 +01:00
|
|
|
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
|
|
|
from io import open
|
|
|
|
|
2005-02-02 13:37:16 +01:00
|
|
|
class FakeLog(object):
|
|
|
|
def flush(self):
|
|
|
|
return
|
|
|
|
def close(self):
|
|
|
|
return
|
|
|
|
def write(self, s):
|
|
|
|
return
|
|
|
|
|
2005-02-09 08:04:04 +01:00
|
|
|
class ChannelLogger(callbacks.Plugin):
|
2014-11-30 08:18:44 +01:00
|
|
|
"""This plugin allows the bot to log channel conversations to disk."""
|
2005-02-02 13:37:16 +01:00
|
|
|
noIgnore = True
|
2020-08-23 16:30:46 +02:00
|
|
|
echoMessage = True
|
|
|
|
|
2005-02-02 13:37:16 +01:00
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(ChannelLogger, self)
|
|
|
|
self.__parent.__init__(irc)
|
|
|
|
self.logs = {}
|
2006-10-13 07:59:39 +02:00
|
|
|
self.flusher = self.flush
|
|
|
|
world.flushers.append(self.flusher)
|
2005-02-02 13:37:16 +01:00
|
|
|
|
2020-08-23 16:34:59 +02:00
|
|
|
# 10 minutes is long enough to be confident the server won't send
|
|
|
|
# messages after they timeouted.
|
|
|
|
self._emitted_relayed_msgs = utils.structures.ExpiringDict(10*60)
|
|
|
|
|
2005-02-02 13:37:16 +01:00
|
|
|
def die(self):
|
|
|
|
for log in self._logs():
|
|
|
|
log.close()
|
2006-10-13 07:59:39 +02:00
|
|
|
world.flushers = [x for x in world.flushers if x is not self.flusher]
|
2005-02-02 13:37:16 +01:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
for log in self._logs():
|
|
|
|
log.close()
|
|
|
|
self.logs.clear()
|
|
|
|
|
|
|
|
def _logs(self):
|
2015-08-08 22:20:14 +02:00
|
|
|
for logs in self.logs.values():
|
|
|
|
for log in logs.values():
|
2005-02-02 13:37:16 +01:00
|
|
|
yield log
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
self.checkLogNames()
|
|
|
|
for log in self._logs():
|
|
|
|
try:
|
|
|
|
log.flush()
|
2014-01-20 15:49:15 +01:00
|
|
|
except ValueError as e:
|
2005-02-02 13:37:16 +01:00
|
|
|
if e.args[0] != 'I/O operation on a closed file':
|
|
|
|
self.log.exception('Odd exception:')
|
|
|
|
|
2019-09-08 19:05:13 +02:00
|
|
|
def logNameTimestamp(self, network, channel):
|
|
|
|
format = self.registryValue('filenameTimestamp', channel, network)
|
2005-02-02 13:37:16 +01:00
|
|
|
return time.strftime(format)
|
|
|
|
|
2019-09-08 19:05:13 +02:00
|
|
|
def getLogName(self, network, channel):
|
|
|
|
if self.registryValue('rotateLogs', channel, network):
|
2019-09-14 14:35:43 +02:00
|
|
|
name = '%s.%s.log' % (channel, self.logNameTimestamp(network, channel))
|
2005-02-02 13:37:16 +01:00
|
|
|
else:
|
2019-09-06 20:15:18 +02:00
|
|
|
name = '%s.log' % channel
|
|
|
|
return utils.file.sanitizeName(name)
|
2005-02-02 13:37:16 +01:00
|
|
|
|
|
|
|
def getLogDir(self, irc, channel):
|
2017-05-09 12:50:21 +02:00
|
|
|
channel = self.normalizeChannel(irc, channel)
|
2005-02-02 13:37:16 +01:00
|
|
|
logDir = conf.supybot.directories.log.dirize(self.name())
|
|
|
|
if self.registryValue('directories'):
|
|
|
|
if self.registryValue('directories.network'):
|
|
|
|
logDir = os.path.join(logDir, irc.network)
|
|
|
|
if self.registryValue('directories.channel'):
|
2019-09-14 14:35:43 +02:00
|
|
|
logDir = os.path.join(logDir, utils.file.sanitizeName(channel))
|
2005-02-02 13:37:16 +01:00
|
|
|
if self.registryValue('directories.timestamp'):
|
|
|
|
format = self.registryValue('directories.timestamp.format')
|
|
|
|
timeDir =time.strftime(format)
|
|
|
|
logDir = os.path.join(logDir, timeDir)
|
|
|
|
if not os.path.exists(logDir):
|
|
|
|
os.makedirs(logDir)
|
|
|
|
return logDir
|
|
|
|
|
|
|
|
def checkLogNames(self):
|
|
|
|
for (irc, logs) in self.logs.items():
|
2015-08-31 10:40:30 +02:00
|
|
|
for (channel, log) in list(logs.items()):
|
2019-08-24 17:50:05 +02:00
|
|
|
if self.registryValue('rotateLogs', channel, irc.network):
|
2019-09-08 19:05:13 +02:00
|
|
|
name = self.getLogName(irc.network, channel)
|
2014-01-16 00:15:11 +01:00
|
|
|
if name != os.path.basename(log.name):
|
2005-02-02 13:37:16 +01:00
|
|
|
log.close()
|
|
|
|
del logs[channel]
|
|
|
|
|
|
|
|
def getLog(self, irc, channel):
|
|
|
|
self.checkLogNames()
|
|
|
|
try:
|
|
|
|
logs = self.logs[irc]
|
|
|
|
except KeyError:
|
|
|
|
logs = ircutils.IrcDict()
|
|
|
|
self.logs[irc] = logs
|
|
|
|
if channel in logs:
|
|
|
|
return logs[channel]
|
|
|
|
else:
|
|
|
|
try:
|
2019-09-08 19:05:13 +02:00
|
|
|
name = self.getLogName(irc.network, channel)
|
2005-02-02 13:37:16 +01:00
|
|
|
logDir = self.getLogDir(irc, channel)
|
2014-01-16 07:15:09 +01:00
|
|
|
log = open(os.path.join(logDir, name), encoding='utf-8', mode='a')
|
2005-02-02 13:37:16 +01:00
|
|
|
logs[channel] = log
|
|
|
|
return log
|
|
|
|
except IOError:
|
|
|
|
self.log.exception('Error opening log:')
|
|
|
|
return FakeLog()
|
|
|
|
|
|
|
|
def timestamp(self, log):
|
|
|
|
format = conf.supybot.log.timestampFormat()
|
|
|
|
if format:
|
2014-02-17 17:01:31 +01:00
|
|
|
string = time.strftime(format) + ' '
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2014-02-17 22:30:15 +01:00
|
|
|
string = string.decode('utf8', 'ignore')
|
2014-02-17 17:01:31 +01:00
|
|
|
log.write(string)
|
2005-02-02 13:37:16 +01:00
|
|
|
|
|
|
|
def normalizeChannel(self, irc, channel):
|
|
|
|
return ircutils.toLower(channel)
|
|
|
|
|
|
|
|
def doLog(self, irc, channel, s, *args):
|
2019-08-24 17:50:05 +02:00
|
|
|
if not self.registryValue('enable', channel, irc.network):
|
2009-01-19 22:13:18 +01:00
|
|
|
return
|
2005-02-02 13:37:16 +01:00
|
|
|
s = format(s, *args)
|
|
|
|
channel = self.normalizeChannel(irc, channel)
|
|
|
|
log = self.getLog(irc, channel)
|
2019-08-24 17:50:05 +02:00
|
|
|
if self.registryValue('timestamp', channel, irc.network):
|
2005-02-02 13:37:16 +01:00
|
|
|
self.timestamp(log)
|
2019-08-24 17:50:05 +02:00
|
|
|
if self.registryValue('stripFormatting', channel, irc.network):
|
2005-02-02 13:37:16 +01:00
|
|
|
s = ircutils.stripFormatting(s)
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2014-02-17 22:30:15 +01:00
|
|
|
s = s.decode('utf8', 'ignore')
|
2005-02-02 13:37:16 +01:00
|
|
|
log.write(s)
|
|
|
|
if self.registryValue('flushImmediately'):
|
|
|
|
log.flush()
|
|
|
|
|
|
|
|
def doPrivmsg(self, irc, msg):
|
|
|
|
(recipients, text) = msg.args
|
|
|
|
for channel in recipients.split(','):
|
|
|
|
if irc.isChannel(channel):
|
2019-08-24 17:50:05 +02:00
|
|
|
noLogPrefix = self.registryValue('noLogPrefix',
|
|
|
|
channel, irc.network)
|
2013-04-26 17:52:18 +02:00
|
|
|
cap = ircdb.makeChannelCapability(channel, 'logChannelMessages')
|
|
|
|
try:
|
|
|
|
logChannelMessages = ircdb.checkCapability(msg.prefix, cap,
|
|
|
|
ignoreOwner=True)
|
|
|
|
except KeyError:
|
|
|
|
logChannelMessages = True
|
2013-08-19 17:22:56 +02:00
|
|
|
nick = msg.nick or irc.nick
|
2020-05-17 21:05:59 +02:00
|
|
|
rewriteRelayed = self.registryValue('rewriteRelayed',
|
|
|
|
channel, irc.network)
|
2020-08-23 16:34:59 +02:00
|
|
|
if msg.tagged('ChannelLogger__relayed'):
|
|
|
|
wasRelayed = True
|
|
|
|
elif 'label' in msg.server_tags:
|
|
|
|
label = msg.server_tags['label']
|
|
|
|
if label in self._emitted_relayed_msgs:
|
|
|
|
del self._emitted_relayed_msgs[label]
|
|
|
|
wasRelayed = True
|
|
|
|
else:
|
|
|
|
wasRelayed = False
|
|
|
|
else:
|
|
|
|
wasRelayed = False
|
|
|
|
|
|
|
|
if rewriteRelayed and wasRelayed:
|
2013-08-19 17:22:56 +02:00
|
|
|
(nick, text) = text.split(' ', 1)
|
|
|
|
nick = nick[1:-1]
|
|
|
|
msg.args = (recipients, text)
|
2013-04-26 17:52:18 +02:00
|
|
|
if (noLogPrefix and text.startswith(noLogPrefix)) or \
|
|
|
|
not logChannelMessages:
|
2005-02-02 13:37:16 +01:00
|
|
|
text = '-= THIS MESSAGE NOT LOGGED =-'
|
|
|
|
if ircmsgs.isAction(msg):
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'* %s %s\n', nick, ircmsgs.unAction(msg))
|
|
|
|
else:
|
|
|
|
self.doLog(irc, channel, '<%s> %s\n', nick, text)
|
|
|
|
|
|
|
|
def doNotice(self, irc, msg):
|
|
|
|
(recipients, text) = msg.args
|
|
|
|
for channel in recipients.split(','):
|
|
|
|
if irc.isChannel(channel):
|
|
|
|
self.doLog(irc, channel, '-%s- %s\n', msg.nick, text)
|
|
|
|
|
|
|
|
def doNick(self, irc, msg):
|
|
|
|
oldNick = msg.nick
|
|
|
|
newNick = msg.args[0]
|
2015-12-04 13:30:07 +01:00
|
|
|
for channel in msg.tagged('channels'):
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s is now known as %s\n', oldNick, newNick)
|
2015-08-22 18:24:32 +02:00
|
|
|
|
|
|
|
def doInvite(self, irc, msg):
|
|
|
|
(target, channel) = msg.args
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s <%s> invited %s to %s\n',
|
|
|
|
msg.nick, msg.prefix, target, channel)
|
|
|
|
|
2005-02-02 13:37:16 +01:00
|
|
|
def doJoin(self, irc, msg):
|
2012-09-25 07:11:09 +02:00
|
|
|
for channel in msg.args[0].split(','):
|
2019-08-24 17:50:05 +02:00
|
|
|
if(self.registryValue('showJoinParts', channel, irc.network)):
|
2012-09-22 22:01:29 +02:00
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s <%s> has joined %s\n',
|
|
|
|
msg.nick, msg.prefix, channel)
|
2005-02-02 13:37:16 +01:00
|
|
|
|
|
|
|
def doKick(self, irc, msg):
|
|
|
|
if len(msg.args) == 3:
|
|
|
|
(channel, target, kickmsg) = msg.args
|
|
|
|
else:
|
|
|
|
(channel, target) = msg.args
|
|
|
|
kickmsg = ''
|
|
|
|
if kickmsg:
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s was kicked by %s (%s)\n',
|
|
|
|
target, msg.nick, kickmsg)
|
|
|
|
else:
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s was kicked by %s\n', target, msg.nick)
|
|
|
|
|
|
|
|
def doPart(self, irc, msg):
|
2012-09-25 07:11:09 +02:00
|
|
|
if len(msg.args) > 1:
|
|
|
|
reason = " (%s)" % msg.args[1]
|
|
|
|
else:
|
|
|
|
reason = ""
|
|
|
|
for channel in msg.args[0].split(','):
|
2019-08-24 17:50:05 +02:00
|
|
|
if(self.registryValue('showJoinParts', channel, irc.network)):
|
2012-09-22 22:01:29 +02:00
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s <%s> has left %s%s\n',
|
|
|
|
msg.nick, msg.prefix, channel, reason)
|
2005-02-02 13:37:16 +01:00
|
|
|
|
|
|
|
def doMode(self, irc, msg):
|
|
|
|
channel = msg.args[0]
|
|
|
|
if irc.isChannel(channel) and msg.args[1:]:
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s sets mode: %s %s\n',
|
|
|
|
msg.nick or msg.prefix, msg.args[1],
|
|
|
|
' '.join(msg.args[2:]))
|
|
|
|
|
|
|
|
def doTopic(self, irc, msg):
|
|
|
|
if len(msg.args) == 1:
|
|
|
|
return # It's an empty TOPIC just to get the current topic.
|
|
|
|
channel = msg.args[0]
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s changes topic to "%s"\n', msg.nick, msg.args[1])
|
|
|
|
|
|
|
|
def doQuit(self, irc, msg):
|
2010-06-16 05:53:15 +02:00
|
|
|
if len(msg.args) == 1:
|
|
|
|
reason = " (%s)" % msg.args[0]
|
|
|
|
else:
|
|
|
|
reason = ""
|
2015-12-04 13:30:07 +01:00
|
|
|
for channel in msg.tagged('channels'):
|
2019-08-24 17:50:05 +02:00
|
|
|
if(self.registryValue('showJoinParts', channel, irc.network)):
|
2015-12-04 13:30:07 +01:00
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s <%s> has quit IRC%s\n',
|
|
|
|
msg.nick, msg.prefix, reason)
|
2005-02-02 13:37:16 +01:00
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
2020-08-29 22:53:25 +02:00
|
|
|
# Mark/remember outgoing relayed messages, so we can rewrite them if
|
|
|
|
# rewriteRelayed is True.
|
2005-02-02 13:37:16 +01:00
|
|
|
if msg.command in ('PRIVMSG', 'NOTICE'):
|
2020-08-23 16:34:59 +02:00
|
|
|
rewriteRelayed = self.registryValue(
|
|
|
|
'rewriteRelayed', msg.channel, irc.network)
|
|
|
|
if rewriteRelayed and 'echo-message' in self.state.capabilities_ack:
|
|
|
|
assert 'labeled-response' in self.state.capabilities_ack, \
|
|
|
|
'echo-message was negotiated without labeled-response.'
|
|
|
|
# If we negotiated the echo-message cap, we have to remember
|
|
|
|
# this message was relayed when the server sends it back to us.
|
|
|
|
if 'label' not in msg.server_tags:
|
|
|
|
msg.server_tags['label'] = ircutils.makeLabel()
|
|
|
|
if msg.tagged('relayedMsg'):
|
|
|
|
# Remember this was a relayed message, in case
|
|
|
|
# rewriteRelayed is True.
|
|
|
|
self._emitted_relayed_msgs[msg.server_tags['label']] = True
|
|
|
|
else:
|
|
|
|
# Else, we can simply rely on internal tags, because echos are
|
|
|
|
# simulated.
|
|
|
|
if msg.tagged('relayedMsg'):
|
2020-08-29 22:49:06 +02:00
|
|
|
msg.tag('ChannelLogger__relayed')
|
2005-02-02 13:37:16 +01:00
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
|
|
Class = ChannelLogger
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|