2007-09-20 04:06:31 +02:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2009, James McCoy
|
2007-09-20 04:06:31 +02: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 re
|
|
|
|
import time
|
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.ircdb as ircdb
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-16 18:54:18 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('BadWords')
|
2007-09-20 04:06:31 +02:00
|
|
|
|
|
|
|
class BadWords(callbacks.Privmsg):
|
2010-09-08 02:27:51 +02:00
|
|
|
"""Maintains a list of words that the bot is not allowed to say.
|
|
|
|
Can also be used to kick people that say these words, if the bot
|
|
|
|
has op."""
|
2007-09-20 04:06:31 +02:00
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(BadWords, self)
|
|
|
|
self.__parent.__init__(irc)
|
|
|
|
# This is so we can not filter certain outgoing messages (like list,
|
|
|
|
# which would be kinda useless if it were filtered).
|
|
|
|
self.filtering = True
|
|
|
|
self.lastModified = 0
|
|
|
|
self.words = conf.supybot.plugins.BadWords.words
|
|
|
|
|
|
|
|
def callCommand(self, name, irc, msg, *args, **kwargs):
|
|
|
|
if ircdb.checkCapability(msg.prefix, 'admin'):
|
|
|
|
self.__parent.callCommand(name, irc, msg, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
irc.errorNoCapability('admin')
|
|
|
|
|
|
|
|
def sub(self, m):
|
|
|
|
replaceMethod = self.registryValue('replaceMethod')
|
|
|
|
if replaceMethod == 'simple':
|
|
|
|
return self.registryValue('simpleReplacement')
|
|
|
|
elif replaceMethod == 'nastyCharacters':
|
|
|
|
return self.registryValue('nastyChars')[:len(m.group(1))]
|
|
|
|
|
|
|
|
def inFilter(self, irc, msg):
|
|
|
|
self.filtering = True
|
2009-03-12 19:47:12 +01:00
|
|
|
# We need to check for bad words here rather than in doPrivmsg because
|
2010-09-08 02:27:51 +02:00
|
|
|
# messages don't get to doPrivmsg if the user is ignored.
|
2009-03-12 19:47:12 +01:00
|
|
|
if msg.command == 'PRIVMSG':
|
|
|
|
channel = msg.args[0]
|
2012-02-25 14:02:02 +01:00
|
|
|
self.updateRegexp(channel)
|
|
|
|
s = ircutils.stripFormatting(msg.args[1])
|
2009-03-12 19:47:12 +01:00
|
|
|
if ircutils.isChannel(channel) and self.registryValue('kick', channel):
|
2015-01-03 04:08:59 +01:00
|
|
|
if self.words and self.regexp.search(s):
|
|
|
|
c = irc.state.channels[channel]
|
|
|
|
cap = ircdb.makeChannelCapability(channel, 'op')
|
|
|
|
if c.isHalfopPlus(irc.nick):
|
|
|
|
if c.isHalfopPlus(msg.nick) or \
|
|
|
|
ircdb.checkCapability(msg.prefix, cap):
|
|
|
|
self.log.debug("Not kicking %s from %s, because "
|
2015-01-03 20:15:59 +01:00
|
|
|
"they are halfop+ or can't be "
|
|
|
|
"kicked.", msg.nick, channel)
|
2015-01-03 04:08:59 +01:00
|
|
|
else:
|
|
|
|
message = self.registryValue('kick.message', channel)
|
|
|
|
irc.queueMsg(ircmsgs.kick(channel, msg.nick, message))
|
2009-03-12 19:47:12 +01:00
|
|
|
else:
|
|
|
|
self.log.warning('Should kick %s from %s, but not opped.',
|
|
|
|
msg.nick, channel)
|
2007-09-20 04:06:31 +02:00
|
|
|
return msg
|
|
|
|
|
2012-02-25 14:02:02 +01:00
|
|
|
def updateRegexp(self, channel):
|
2007-09-20 04:24:52 +02:00
|
|
|
if self.lastModified < self.words.lastModified:
|
2012-02-25 14:02:02 +01:00
|
|
|
self.makeRegexp(self.words(), channel)
|
2007-09-20 04:24:52 +02:00
|
|
|
self.lastModified = time.time()
|
2009-04-15 01:59:59 +02:00
|
|
|
|
2007-09-20 04:06:31 +02:00
|
|
|
def outFilter(self, irc, msg):
|
2009-09-04 05:57:45 +02:00
|
|
|
if self.filtering and msg.command == 'PRIVMSG' and self.words():
|
2012-02-25 14:02:02 +01:00
|
|
|
channel = msg.args[0]
|
|
|
|
self.updateRegexp(channel)
|
2007-09-20 04:06:31 +02:00
|
|
|
s = msg.args[1]
|
|
|
|
if self.registryValue('stripFormatting'):
|
|
|
|
s = ircutils.stripFormatting(s)
|
2009-04-15 01:59:59 +02:00
|
|
|
t = self.regexp.sub(self.sub, s)
|
|
|
|
if t != s:
|
|
|
|
msg = ircmsgs.privmsg(msg.args[0], t, msg=msg)
|
2007-09-20 04:06:31 +02:00
|
|
|
return msg
|
|
|
|
|
2012-02-25 14:02:02 +01:00
|
|
|
def makeRegexp(self, iterable, channel):
|
2007-09-20 04:06:31 +02:00
|
|
|
s = '(%s)' % '|'.join(map(re.escape, iterable))
|
2012-01-07 19:08:33 +01:00
|
|
|
if self.registryValue('requireWordBoundaries', channel):
|
2007-09-20 04:06:31 +02:00
|
|
|
s = r'\b%s\b' % s
|
|
|
|
self.regexp = re.compile(s, re.I)
|
|
|
|
|
2010-10-16 18:54:18 +02:00
|
|
|
@internationalizeDocstring
|
2007-09-20 04:06:31 +02:00
|
|
|
def list(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the list of words being censored.
|
|
|
|
"""
|
|
|
|
L = list(self.words())
|
|
|
|
if L:
|
|
|
|
self.filtering = False
|
|
|
|
utils.sortBy(str.lower, L)
|
|
|
|
irc.reply(format('%L', L))
|
|
|
|
else:
|
2010-10-16 18:54:18 +02:00
|
|
|
irc.reply(_('I\'m not currently censoring any bad words.'))
|
2007-09-20 04:06:31 +02:00
|
|
|
list = wrap(list, ['admin'])
|
|
|
|
|
2010-10-16 18:54:18 +02:00
|
|
|
@internationalizeDocstring
|
2007-09-20 04:06:31 +02:00
|
|
|
def add(self, irc, msg, args, words):
|
|
|
|
"""<word> [<word> ...]
|
|
|
|
|
2010-09-08 02:27:51 +02:00
|
|
|
Adds all <word>s to the list of words being censored.
|
2007-09-20 04:06:31 +02:00
|
|
|
"""
|
|
|
|
set = self.words()
|
|
|
|
set.update(words)
|
|
|
|
self.words.setValue(set)
|
|
|
|
irc.replySuccess()
|
|
|
|
add = wrap(add, ['admin', many('something')])
|
|
|
|
|
2010-10-16 18:54:18 +02:00
|
|
|
@internationalizeDocstring
|
2007-09-20 04:06:31 +02:00
|
|
|
def remove(self, irc, msg, args, words):
|
|
|
|
"""<word> [<word> ...]
|
|
|
|
|
2010-09-08 02:27:51 +02:00
|
|
|
Removes <word>s from the list of words being censored.
|
2007-09-20 04:06:31 +02:00
|
|
|
"""
|
|
|
|
set = self.words()
|
|
|
|
for word in words:
|
|
|
|
set.discard(word)
|
|
|
|
self.words.setValue(set)
|
|
|
|
irc.replySuccess()
|
|
|
|
remove = wrap(remove, ['admin', many('something')])
|
|
|
|
|
|
|
|
|
|
|
|
Class = BadWords
|
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|