2010-05-24 13:38:06 +00:00
|
|
|
# -*- Encoding: utf-8 -*-
|
2006-10-19 14:39:11 +00:00
|
|
|
###
|
2010-05-24 13:38:06 +00:00
|
|
|
# Copyright (c) 2005-2007 Dennis Kaarsemaker
|
|
|
|
# Copyright (c) 2008-2010 Terence Simpson
|
|
|
|
# Copyright (c) 2010 Elián Hanisch
|
2006-10-19 14:39:11 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of version 2 of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
###
|
2010-05-24 13:38:06 +00:00
|
|
|
###
|
2006-10-19 14:39:11 +00:00
|
|
|
# Based on the standard supybot logging plugin, which has the following
|
|
|
|
# copyright:
|
|
|
|
#
|
|
|
|
# Copyright (c) 2002-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.
|
|
|
|
###
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.conf as conf
|
2007-02-15 22:16:44 +00:00
|
|
|
import supybot.ircdb as ircdb
|
2010-03-30 11:47:16 +00:00
|
|
|
import supybot.schedule as schedule
|
2012-07-16 00:15:06 +00:00
|
|
|
import supybot.utils as utils
|
2012-08-03 14:46:17 +00:00
|
|
|
from supybot.utils.str import format as Format
|
2010-03-15 16:12:51 +00:00
|
|
|
from fnmatch import fnmatch
|
2012-07-16 01:28:38 +00:00
|
|
|
from collections import defaultdict
|
2009-01-19 22:10:51 +00:00
|
|
|
import sqlite
|
|
|
|
import pytz
|
|
|
|
import cPickle
|
|
|
|
import datetime
|
2012-07-05 05:31:53 +00:00
|
|
|
import csv
|
2009-01-19 22:10:51 +00:00
|
|
|
import time
|
|
|
|
import random
|
2010-02-04 18:58:05 +00:00
|
|
|
import hashlib
|
2009-01-19 22:10:51 +00:00
|
|
|
import threading
|
2006-06-26 17:57:20 +00:00
|
|
|
|
2010-04-04 00:11:22 +00:00
|
|
|
isUserHostmask = ircutils.isUserHostmask
|
|
|
|
|
2008-05-10 15:13:26 +00:00
|
|
|
tz = 'UTC'
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
def now():
|
|
|
|
return cPickle.dumps(datetime.datetime.now(pytz.timezone(tz)))
|
|
|
|
|
2012-06-25 22:35:42 +00:00
|
|
|
def nowSeconds():
|
|
|
|
# apparently time.time() isn't the same thing.
|
|
|
|
# return time.time()
|
|
|
|
return int(time.mktime(time.gmtime()))
|
|
|
|
|
2009-01-19 22:10:51 +00:00
|
|
|
def fromTime(x):
|
|
|
|
return cPickle.dumps(datetime.datetime(*time.gmtime(x)[:6], **{'tzinfo': pytz.timezone("UTC")}))
|
2008-08-07 13:31:26 +00:00
|
|
|
|
2012-07-04 22:23:37 +00:00
|
|
|
|
2012-07-04 22:36:19 +00:00
|
|
|
class FuzzyDict(dict):
|
|
|
|
def __getitem__(self, k):
|
|
|
|
try:
|
|
|
|
return dict.__getitem__(self, k)
|
|
|
|
except KeyError:
|
|
|
|
# ok, lets find the closest match
|
|
|
|
n = len(k)
|
|
|
|
keys = [ s for s in self if s[:n] == k ]
|
|
|
|
if len(keys) != 1:
|
|
|
|
# ambiguous
|
|
|
|
raise
|
|
|
|
return dict.__getitem__(self, keys[0])
|
|
|
|
|
|
|
|
timeUnits = FuzzyDict({
|
|
|
|
'minutes': 60, 'm': 60,
|
|
|
|
'hours' : 3600, 'M': 2592000,
|
|
|
|
'days' : 86400,
|
|
|
|
'weeks' : 604800,
|
|
|
|
'months' : 2592000,
|
|
|
|
'years' : 31536000,
|
|
|
|
})
|
2012-07-04 22:23:37 +00:00
|
|
|
|
|
|
|
def readTimeDelta(s):
|
|
|
|
"""convert a string like "2 days" or "1h2d3w" into seconds"""
|
|
|
|
# split number and words
|
2012-07-28 00:08:25 +00:00
|
|
|
if not s:
|
|
|
|
raise ValueError(s)
|
|
|
|
|
2012-07-04 22:49:38 +00:00
|
|
|
digit = string = number = None
|
|
|
|
seconds = 0
|
2012-07-04 22:23:37 +00:00
|
|
|
for c in s:
|
2012-07-28 02:12:35 +00:00
|
|
|
if c == ' ':
|
|
|
|
continue
|
|
|
|
|
2012-08-03 14:46:17 +00:00
|
|
|
if c in '+-0123456789':
|
2012-07-04 22:49:38 +00:00
|
|
|
if string is None:
|
|
|
|
# start
|
|
|
|
digit, string = True, ''
|
2012-07-04 22:23:37 +00:00
|
|
|
elif digit is False:
|
|
|
|
digit = True
|
2012-07-04 22:49:38 +00:00
|
|
|
# completed an unit, add to seconds
|
|
|
|
string = string.strip()
|
|
|
|
if string:
|
2012-07-27 03:50:25 +00:00
|
|
|
try:
|
|
|
|
unit = timeUnits[string]
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError(string)
|
|
|
|
seconds += number * unit
|
2012-07-04 22:49:38 +00:00
|
|
|
string = ''
|
|
|
|
string += c
|
2012-07-04 22:23:37 +00:00
|
|
|
else:
|
|
|
|
if digit is None:
|
|
|
|
# need a number first
|
2012-07-28 02:12:35 +00:00
|
|
|
raise ValueError(s)
|
2012-07-04 22:23:37 +00:00
|
|
|
if digit is True:
|
|
|
|
digit = False
|
2012-07-04 22:49:38 +00:00
|
|
|
# completed a number
|
|
|
|
number, string = int(string), ''
|
|
|
|
string += c
|
2012-07-04 22:23:37 +00:00
|
|
|
|
2012-07-04 22:49:38 +00:00
|
|
|
# check last string
|
2012-07-28 00:08:25 +00:00
|
|
|
if string is None:
|
|
|
|
raise ValueError(s)
|
|
|
|
|
2012-08-03 14:46:17 +00:00
|
|
|
try:
|
2012-07-04 22:49:38 +00:00
|
|
|
seconds += int(string)
|
2012-08-03 14:46:17 +00:00
|
|
|
except ValueError:
|
2012-07-04 22:49:38 +00:00
|
|
|
string = string.strip()
|
|
|
|
if string:
|
2012-07-27 03:50:25 +00:00
|
|
|
try:
|
|
|
|
unit = timeUnits[string]
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError(string)
|
|
|
|
seconds += number * unit
|
2012-07-04 22:49:38 +00:00
|
|
|
|
2012-07-04 22:23:37 +00:00
|
|
|
return seconds
|
|
|
|
|
2012-07-16 00:15:06 +00:00
|
|
|
# utils.gen.timeElapsed is too noisy, what do I care of the seconds and minutes
|
|
|
|
# if the period is like a month long, or the zero values?
|
|
|
|
def timeElapsed(elapsed, short=False, resolution=2):
|
|
|
|
"""Given <elapsed> seconds, returns a string with an English description of
|
|
|
|
the amount of time passed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
before = False
|
|
|
|
def Format(s, i):
|
|
|
|
if i:
|
|
|
|
if short:
|
|
|
|
ret.append('%s%s' % (i, s[0]))
|
|
|
|
else:
|
|
|
|
ret.append(utils.str.format('%n', (i, s)))
|
|
|
|
elapsed = int(elapsed)
|
|
|
|
|
|
|
|
# Handle negative times
|
|
|
|
if elapsed < 0:
|
|
|
|
before = True
|
|
|
|
elapsed = -elapsed
|
|
|
|
|
|
|
|
for s, i in (('year', 31536000), ('month', 2592000), ('week', 604800),
|
|
|
|
('day', 86400), ('hour', 3600), ('minute', 60)):
|
|
|
|
count, elapsed = elapsed // i, elapsed % i
|
|
|
|
Format(s, count)
|
|
|
|
if len(ret) == resolution:
|
|
|
|
break
|
|
|
|
#Format('second', elapsed) # seconds are pointless for now
|
|
|
|
if not ret:
|
|
|
|
raise ValueError, 'Time difference not great enough to be noted.'
|
|
|
|
result = ''
|
|
|
|
#ret = ret[:resolution]
|
|
|
|
if short:
|
|
|
|
result = ' '.join(ret)
|
|
|
|
else:
|
|
|
|
result = utils.str.format('%L', ret)
|
|
|
|
if before:
|
|
|
|
result += ' ago'
|
|
|
|
return result
|
|
|
|
|
2012-07-27 22:21:21 +00:00
|
|
|
def splitID(s):
|
|
|
|
"""get a list of integers from a comma separated list of numbers"""
|
|
|
|
for id in s.split(','):
|
|
|
|
if id.isdigit():
|
|
|
|
id = int(id)
|
|
|
|
if id > 0:
|
|
|
|
yield id
|
|
|
|
|
2008-07-20 15:59:27 +00:00
|
|
|
def capab(user, capability):
|
2008-08-07 13:31:26 +00:00
|
|
|
capability = capability.lower()
|
|
|
|
capabilities = list(user.capabilities)
|
|
|
|
# Capability hierarchy #
|
|
|
|
if capability == "bantracker":
|
2008-08-08 03:58:47 +00:00
|
|
|
if capab(user, "admin"):
|
2008-07-20 15:59:27 +00:00
|
|
|
return True
|
2008-08-07 13:31:26 +00:00
|
|
|
if capability == "admin":
|
2008-08-08 03:58:47 +00:00
|
|
|
if capab(user, "owner"):
|
2008-08-07 13:31:26 +00:00
|
|
|
return True
|
|
|
|
# End #
|
|
|
|
if capability in capabilities:
|
|
|
|
return True
|
|
|
|
else:
|
2008-07-20 15:59:27 +00:00
|
|
|
return False
|
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def hostmaskPatternEqual(pattern, hostmask):
|
2010-04-13 00:24:45 +00:00
|
|
|
if pattern.count('!') != 1 or pattern.count('@') != 1:
|
2008-08-04 10:11:15 +00:00
|
|
|
return False
|
2010-04-13 00:24:45 +00:00
|
|
|
if pattern.count('$') == 1:
|
2010-04-20 11:27:35 +00:00
|
|
|
pattern = pattern.split('$',1)[0]
|
|
|
|
if pattern.startswith('%'):
|
2009-01-19 22:10:51 +00:00
|
|
|
pattern = pattern[1:]
|
2008-08-04 10:11:15 +00:00
|
|
|
return ircutils.hostmaskPatternEqual(pattern, hostmask)
|
|
|
|
|
2010-03-15 23:51:32 +00:00
|
|
|
def nickMatch(nick, pattern):
|
|
|
|
"""Checks if a given nick matches a pattern or in a list of patterns."""
|
|
|
|
if isinstance(pattern, str):
|
|
|
|
pattern = [pattern]
|
|
|
|
nick = nick.lower()
|
|
|
|
for s in pattern:
|
2010-03-16 02:28:09 +00:00
|
|
|
if fnmatch(nick, s.lower()):
|
2010-03-15 23:51:32 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def dequeue(parent, irc):
|
|
|
|
global queue
|
|
|
|
queue.dequeue(parent, irc)
|
|
|
|
|
|
|
|
class MsgQueue(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.msgcache = []
|
|
|
|
def queue(self, msg):
|
|
|
|
if msg not in self.msgcache:
|
|
|
|
self.msgcache.append(msg)
|
|
|
|
def clear(self):
|
|
|
|
self.msgcache = []
|
|
|
|
def dequeue(self, parent, irc):
|
2009-10-12 18:26:35 +00:00
|
|
|
parent.thread_timer.cancel()
|
|
|
|
parent.thread_timer = threading.Timer(10.0, dequeue, args=(parent, irc))
|
2009-01-19 22:10:51 +00:00
|
|
|
if len(self.msgcache):
|
|
|
|
msg = self.msgcache.pop(0)
|
2009-10-12 18:26:35 +00:00
|
|
|
irc.queueMsg(msg)
|
2008-08-04 10:11:15 +00:00
|
|
|
parent.thread_timer.start()
|
|
|
|
|
|
|
|
queue = MsgQueue()
|
|
|
|
|
2012-06-25 22:30:11 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
class Ban(object):
|
|
|
|
"""Hold my bans"""
|
|
|
|
def __init__(self, args=None, **kwargs):
|
2010-03-14 13:07:59 +00:00
|
|
|
self.id = None
|
2008-08-04 10:11:15 +00:00
|
|
|
if args:
|
2010-03-14 13:07:59 +00:00
|
|
|
# in most ircd: args = (nick, channel, mask, who, when)
|
2012-06-27 04:14:51 +00:00
|
|
|
self.channel = args[1]
|
2008-08-04 10:11:15 +00:00
|
|
|
self.mask = args[2]
|
|
|
|
self.who = args[3]
|
2009-01-19 22:10:51 +00:00
|
|
|
self.when = float(args[4])
|
2008-08-04 10:11:15 +00:00
|
|
|
else:
|
2012-06-27 04:14:51 +00:00
|
|
|
self.channel = kwargs['channel']
|
2008-08-04 10:11:15 +00:00
|
|
|
self.mask = kwargs['mask']
|
|
|
|
self.who = kwargs['who']
|
2009-01-19 22:10:51 +00:00
|
|
|
self.when = float(kwargs['when'])
|
2010-03-14 13:07:59 +00:00
|
|
|
if 'id' in kwargs:
|
|
|
|
self.id = kwargs['id']
|
2009-01-19 22:10:51 +00:00
|
|
|
self.ascwhen = time.asctime(time.gmtime(self.when))
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
def __tuple__(self):
|
|
|
|
return (self.mask, self.who, self.ascwhen)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.__tuple__().__iter__()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s by %s on %s" % tuple(self)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<%s object "%s" at 0x%x>' % (self.__class__.__name__, self, id(self))
|
|
|
|
|
2011-06-19 01:39:16 +00:00
|
|
|
def __eq__(self, ban):
|
|
|
|
return self.mask == ban.mask
|
|
|
|
|
|
|
|
def __ne__(self, ban):
|
|
|
|
return not self.__eq__(ban)
|
|
|
|
|
2009-11-04 03:50:49 +00:00
|
|
|
def op(self):
|
|
|
|
return self.mask.split('!')[0]
|
|
|
|
|
|
|
|
def time(self):
|
|
|
|
return datetime.datetime.fromtimestamp(self.when)
|
|
|
|
|
2012-06-25 22:30:11 +00:00
|
|
|
@property
|
|
|
|
def type(self):
|
2012-07-05 22:24:49 +00:00
|
|
|
return guessBanType(self.mask)
|
2012-06-25 22:30:11 +00:00
|
|
|
|
2012-07-05 05:31:53 +00:00
|
|
|
def serialize(self):
|
|
|
|
id = self.id
|
|
|
|
if id is None:
|
|
|
|
id = ''
|
|
|
|
return (id, self.channel, self.mask, self.who, self.when)
|
|
|
|
|
|
|
|
def deserialize(self, L):
|
|
|
|
id = L[0]
|
|
|
|
if id == '':
|
|
|
|
id = None
|
|
|
|
else:
|
|
|
|
id = int(id)
|
|
|
|
self.id = id
|
|
|
|
self.channel, self.mask, self.who = L[1:4]
|
|
|
|
self.when = float(L[4])
|
|
|
|
self.ascwhen = time.asctime(time.gmtime(self.when))
|
|
|
|
|
2010-04-01 20:52:22 +00:00
|
|
|
|
2012-07-05 22:24:49 +00:00
|
|
|
def guessBanType(mask):
|
|
|
|
if mask[0] == '%':
|
|
|
|
return 'quiet'
|
2012-07-05 22:36:13 +00:00
|
|
|
elif ircutils.isUserHostmask(mask) \
|
|
|
|
or mask[0] == '$' \
|
|
|
|
or mask.endswith('(realname)'):
|
2012-07-05 22:24:49 +00:00
|
|
|
if not ('*' in mask or '?' in mask or '$' in mask):
|
|
|
|
# XXX hack over hack, we are supposing these are marks as normal
|
|
|
|
# bans aren't usually set to exact match, while marks are.
|
|
|
|
return 'mark'
|
|
|
|
return 'ban'
|
|
|
|
return 'removal'
|
|
|
|
|
|
|
|
|
2012-06-25 22:30:11 +00:00
|
|
|
class ReviewStore(dict):
|
2010-04-02 20:28:43 +00:00
|
|
|
def __init__(self, filename):
|
|
|
|
self.filename = conf.supybot.directories.data.dirize(filename)
|
2012-06-25 22:30:11 +00:00
|
|
|
self.lastReview = 0
|
|
|
|
|
|
|
|
def __getitem__(self, k):
|
|
|
|
try:
|
|
|
|
return dict.__getitem__(self, k)
|
|
|
|
except KeyError:
|
|
|
|
self[k] = L = []
|
|
|
|
return L
|
2010-04-02 20:28:43 +00:00
|
|
|
|
|
|
|
def open(self):
|
|
|
|
import csv
|
|
|
|
try:
|
|
|
|
reader = csv.reader(open(self.filename, 'rb'))
|
|
|
|
except IOError:
|
|
|
|
return
|
2012-06-25 22:30:11 +00:00
|
|
|
self.lastReview = int(reader.next()[1])
|
2010-04-02 20:28:43 +00:00
|
|
|
for row in reader:
|
|
|
|
host, value = self.deserialize(*row)
|
|
|
|
try:
|
2010-05-14 03:09:50 +00:00
|
|
|
L = self[host]
|
|
|
|
if value not in L:
|
|
|
|
L.append(value)
|
2010-04-02 20:28:43 +00:00
|
|
|
except KeyError:
|
|
|
|
self[host] = [value]
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
import csv
|
|
|
|
try:
|
|
|
|
writer = csv.writer(open(self.filename, 'wb'))
|
|
|
|
except IOError:
|
|
|
|
return
|
2012-06-25 22:30:11 +00:00
|
|
|
writer.writerow(('time', str(int(self.lastReview))))
|
2010-04-02 20:28:43 +00:00
|
|
|
for host, values in self.iteritems():
|
|
|
|
for v in values:
|
|
|
|
writer.writerow(self.serialize(host, v))
|
|
|
|
|
|
|
|
def deserialize(self, host, nick, command, channel, text):
|
|
|
|
if command == 'PRIVMSG':
|
|
|
|
msg = ircmsgs.privmsg(channel, text)
|
|
|
|
elif command == 'NOTICE':
|
|
|
|
msg = ircmsgs.notice(channel, text)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
return (host, (nick, msg))
|
|
|
|
|
|
|
|
def serialize(self, host, value):
|
|
|
|
nick, msg = value
|
|
|
|
command, channel, text = msg.command, msg.args[0], msg.args[1]
|
|
|
|
return (host, nick, command, channel, text)
|
|
|
|
|
|
|
|
|
2012-06-25 22:40:09 +00:00
|
|
|
class BanRemoval(object):
|
|
|
|
"""This object saves information about a ban that should be removed when expires"""
|
|
|
|
def __init__(self, ban, expires):
|
|
|
|
"""
|
|
|
|
ban: ban object
|
|
|
|
expires: time in seconds for it to expire
|
|
|
|
"""
|
|
|
|
self.ban = ban
|
|
|
|
self.expires = expires
|
2012-07-05 00:35:07 +00:00
|
|
|
self.notified = False
|
2012-06-25 22:40:09 +00:00
|
|
|
|
2012-07-05 00:35:07 +00:00
|
|
|
def expired(self, offset=0):
|
2012-06-25 22:40:09 +00:00
|
|
|
"""Check if the ban did expire."""
|
2012-07-05 00:35:07 +00:00
|
|
|
if (nowSeconds() + offset) > (self.ban.when + self.expires):
|
2012-06-25 22:40:09 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2012-07-05 05:31:53 +00:00
|
|
|
def serialize(self):
|
|
|
|
notified = self.notified and 1 or 0
|
|
|
|
L = [ self.expires, notified ]
|
|
|
|
L.extend(self.ban.serialize())
|
|
|
|
return tuple(L)
|
|
|
|
|
|
|
|
def deserialize(self, L):
|
|
|
|
self.expires = int(L[0])
|
|
|
|
self.notified = bool(int(L[1]))
|
|
|
|
self.ban = Ban(args=(None, None, None, None, 0))
|
|
|
|
self.ban.deserialize(L[2:])
|
|
|
|
|
2012-07-16 00:06:23 +00:00
|
|
|
def enumerateReversed(L):
|
|
|
|
"""enumerate in reverse order"""
|
|
|
|
for i in reversed(xrange(len(L))):
|
|
|
|
yield i, L[i]
|
2012-06-25 22:40:09 +00:00
|
|
|
|
|
|
|
class BanStore(object):
|
|
|
|
def __init__(self, filename):
|
2012-07-05 05:31:53 +00:00
|
|
|
self.filename = conf.supybot.directories.data.dirize(filename)
|
2012-06-25 22:40:09 +00:00
|
|
|
self.shelf = []
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.shelf)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.shelf)
|
|
|
|
|
2012-07-05 05:31:53 +00:00
|
|
|
def open(self):
|
|
|
|
try:
|
|
|
|
reader = csv.reader(open(self.filename, 'rb'))
|
|
|
|
except IOError:
|
|
|
|
return
|
|
|
|
|
|
|
|
for row in reader:
|
|
|
|
ban = BanRemoval(None, None)
|
|
|
|
ban.deserialize(row)
|
|
|
|
self.add(ban)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
try:
|
|
|
|
writer = csv.writer(open(self.filename, 'wb'))
|
|
|
|
except IOError:
|
|
|
|
return
|
|
|
|
|
|
|
|
for ban in self:
|
|
|
|
writer.writerow(ban.serialize())
|
|
|
|
|
2012-06-25 22:40:09 +00:00
|
|
|
def add(self, obj):
|
|
|
|
self.shelf.append(obj)
|
|
|
|
|
|
|
|
def sort(self):
|
|
|
|
"""Sort bans by expire date"""
|
|
|
|
def key(x):
|
|
|
|
return x.ban.when + x.expires
|
|
|
|
|
|
|
|
self.shelf.sort(key=key, reverse=True)
|
|
|
|
|
2012-07-05 00:35:07 +00:00
|
|
|
def popExpired(self, time=0):
|
2012-06-25 22:40:09 +00:00
|
|
|
"""Pops a list of expired bans"""
|
|
|
|
L = []
|
|
|
|
for i, ban in enumerateReversed(self.shelf):
|
2012-07-05 00:35:07 +00:00
|
|
|
if ban.expired(offset=time):
|
2012-06-25 22:40:09 +00:00
|
|
|
L.append(ban)
|
|
|
|
del self.shelf[i]
|
|
|
|
return L
|
|
|
|
|
2012-07-05 00:35:07 +00:00
|
|
|
def getExpired(self, time=0):
|
|
|
|
def generator():
|
|
|
|
for ban in self.shelf:
|
|
|
|
if ban.expired(offset=time):
|
|
|
|
yield ban
|
|
|
|
return generator()
|
|
|
|
|
2010-04-03 03:26:20 +00:00
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
class Bantracker(callbacks.Plugin):
|
2008-08-04 10:11:15 +00:00
|
|
|
"""Plugin to manage bans.
|
2009-01-19 22:10:51 +00:00
|
|
|
See '@list Bantracker' and '@help <command>' for commands"""
|
2006-06-26 17:57:20 +00:00
|
|
|
noIgnore = True
|
2009-01-19 22:10:51 +00:00
|
|
|
threaded = True
|
2012-08-18 14:27:50 +00:00
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(Bantracker, self)
|
|
|
|
self.__parent.__init__(irc)
|
2009-01-19 22:10:51 +00:00
|
|
|
self.default_irc = irc
|
2006-06-26 17:57:20 +00:00
|
|
|
self.lastMsgs = {}
|
|
|
|
self.lastStates = {}
|
2009-01-19 22:10:51 +00:00
|
|
|
self.replies = {}
|
2010-04-02 17:01:07 +00:00
|
|
|
self.logs = ircutils.IrcDict()
|
2008-08-04 10:11:15 +00:00
|
|
|
self.nicks = {}
|
2009-01-19 22:10:51 +00:00
|
|
|
self.hosts = {}
|
2010-04-02 17:01:07 +00:00
|
|
|
self.bans = ircutils.IrcDict()
|
2012-08-18 14:27:50 +00:00
|
|
|
self.opped = defaultdict(lambda: False)
|
|
|
|
self.pendingBanremoval = {}
|
2008-08-04 10:11:15 +00:00
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
self.thread_timer = threading.Timer(10.0, dequeue, args=(self,irc))
|
2008-08-04 10:11:15 +00:00
|
|
|
self.thread_timer.start()
|
2006-06-26 17:57:20 +00:00
|
|
|
|
2007-01-22 21:14:41 +00:00
|
|
|
db = self.registryValue('database')
|
|
|
|
if db:
|
|
|
|
self.db = sqlite.connect(db)
|
|
|
|
else:
|
|
|
|
self.db = None
|
2008-08-04 10:11:15 +00:00
|
|
|
self.get_bans(irc)
|
|
|
|
self.get_nicks(irc)
|
2012-06-25 22:40:09 +00:00
|
|
|
|
|
|
|
# init review stuff
|
2012-06-25 22:30:11 +00:00
|
|
|
self.pendingReviews = ReviewStore('bt.reviews.db')
|
2010-04-02 21:32:19 +00:00
|
|
|
self.pendingReviews.open()
|
2010-10-24 06:13:43 +00:00
|
|
|
self._banreviewfix()
|
2012-06-25 22:40:09 +00:00
|
|
|
|
|
|
|
# init autoremove stuff
|
2012-07-05 05:31:53 +00:00
|
|
|
self.managedBans = BanStore('bt.autoremove.db')
|
|
|
|
self.managedBans.open()
|
2012-06-25 22:40:09 +00:00
|
|
|
|
|
|
|
# add our scheduled events for check bans for reviews or removal
|
|
|
|
schedule.addPeriodicEvent(lambda: self.reviewBans(irc), 60*60,
|
2012-08-23 10:21:58 +00:00
|
|
|
'Bantracker_review')
|
2012-07-27 16:45:37 +00:00
|
|
|
schedule.addPeriodicEvent(lambda: self.autoRemoveBans(irc), 600,
|
2012-08-23 10:21:58 +00:00
|
|
|
'Bantracker_autoremove')
|
2012-06-25 22:40:09 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def get_nicks(self, irc):
|
2009-01-19 22:10:51 +00:00
|
|
|
self.hosts.clear()
|
2008-08-04 10:11:15 +00:00
|
|
|
for (channel, c) in irc.state.channels.iteritems():
|
2010-05-22 08:11:37 +00:00
|
|
|
if not self.registryValue('enabled', channel):
|
|
|
|
continue
|
2008-08-04 10:11:15 +00:00
|
|
|
for nick in list(c.users):
|
2009-01-19 22:10:51 +00:00
|
|
|
nick = nick.lower()
|
2008-08-04 10:11:15 +00:00
|
|
|
if not nick in self.nicks:
|
2009-01-19 22:10:51 +00:00
|
|
|
host = self.nick_to_host(irc, nick, False).lower()
|
|
|
|
self.nicks[nick] = host
|
|
|
|
host = host.split('@', 1)[1]
|
|
|
|
if '*' not in host:
|
|
|
|
if host not in self.hosts:
|
|
|
|
self.hosts[host] = []
|
|
|
|
self.hosts[host].append(nick)
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
def get_bans(self, irc):
|
|
|
|
global queue
|
|
|
|
for channel in irc.state.channels.keys():
|
2010-05-22 08:11:37 +00:00
|
|
|
if not self.registryValue('enabled', channel):
|
|
|
|
continue
|
2008-08-04 10:11:15 +00:00
|
|
|
if channel not in self.bans:
|
|
|
|
self.bans[channel] = []
|
|
|
|
queue.queue(ircmsgs.mode(channel, 'b'))
|
|
|
|
|
2009-01-19 22:10:51 +00:00
|
|
|
def sendWhois(self, irc, nick, do_reply=False, *args):
|
|
|
|
nick = nick.lower()
|
2008-08-04 10:11:15 +00:00
|
|
|
irc.queueMsg(ircmsgs.whois(nick, nick))
|
2009-01-19 22:10:51 +00:00
|
|
|
if do_reply:
|
|
|
|
self.replies[nick] = [args[0], args[1:]]
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
def do311(self, irc, msg):
|
|
|
|
"""/whois"""
|
|
|
|
nick = msg.args[1].lower()
|
|
|
|
mask = "%s!%s@%s" % (nick, msg.args[2].lower(), msg.args[3].lower())
|
|
|
|
self.nicks[nick] = mask
|
2009-01-19 22:10:51 +00:00
|
|
|
if nick in self.replies:
|
2010-03-14 14:16:39 +00:00
|
|
|
f = getattr(self, "%s_real" % self.replies[nick][0])
|
2009-01-19 22:10:51 +00:00
|
|
|
args = self.replies[nick][1]
|
|
|
|
del self.replies[nick]
|
|
|
|
kwargs={'from_reply': True, 'reply': "%s!%s@%s" % (msg.args[1], msg.args[2], msg.args[3])}
|
|
|
|
f(*args, **kwargs)
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
def do314(self, irc, msg):
|
|
|
|
"""/whowas"""
|
|
|
|
nick = msg.args[1].lower()
|
|
|
|
mask = "%s!%s@%s" % (nick, msg.args[2].lower(), msg.args[3].lower())
|
|
|
|
if not nick in self.nicks:
|
|
|
|
self.nicks[nick] = mask
|
2009-01-19 22:10:51 +00:00
|
|
|
if nick in self.replies:
|
2010-03-14 14:16:39 +00:00
|
|
|
f = getattr(self, "%s_real" % self.replies[nick][0])
|
2009-01-19 22:10:51 +00:00
|
|
|
args = self.replies[nick][1]
|
|
|
|
del self.replies[nick]
|
|
|
|
kwargs={'from_reply': True, 'reply': "%s!%s@%s" % (msg.args[1], msg.args[2], msg.args[3])}
|
|
|
|
f(*args, **kwargs)
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
def do401(self, irc, msg):
|
|
|
|
"""/whois faild"""
|
|
|
|
irc.queueMsg(ircmsgs.IrcMsg(prefix="", command='WHOWAS', args=(msg.args[1],), msg=msg))
|
|
|
|
|
|
|
|
def do406(self, irc, msg):
|
|
|
|
"""/whowas faild"""
|
2009-01-19 22:10:51 +00:00
|
|
|
nick = msg.args[1].lower()
|
|
|
|
if nick in self.replies:
|
2010-03-14 14:16:39 +00:00
|
|
|
f = getattr(self, "%s_real" % self.replies[nick][0])
|
2009-01-19 22:10:51 +00:00
|
|
|
args = self.replies[nick][1]
|
|
|
|
del self.replies[nick]
|
|
|
|
kwargs = {'from_reply': True, 'reply': None}
|
|
|
|
f(*args, **kwargs)
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
def do367(self, irc, msg):
|
|
|
|
"""Got ban"""
|
2011-06-19 01:39:16 +00:00
|
|
|
channel = msg.args[1]
|
|
|
|
try:
|
|
|
|
bans = self.bans[channel]
|
|
|
|
except KeyError:
|
|
|
|
bans = self.bans[channel] = []
|
|
|
|
ban = Ban(msg.args)
|
|
|
|
if ban.mask.startswith("$r:"):
|
|
|
|
ban.mask = ban.mask[3:] + " (realname)"
|
|
|
|
if ban not in bans:
|
|
|
|
bans.append(ban)
|
|
|
|
|
|
|
|
def do368(self, irc, msg):
|
|
|
|
"""End of channel ban list."""
|
|
|
|
channel = msg.args[1]
|
|
|
|
try:
|
|
|
|
bans = self.bans[channel]
|
|
|
|
bans.sort(key=lambda x: x.when) # needed for self.reviewBans
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2008-08-04 10:11:15 +00:00
|
|
|
|
2009-01-19 22:10:51 +00:00
|
|
|
def nick_to_host(self, irc=None, target='', with_nick=True, reply_now=True):
|
2008-08-04 10:11:15 +00:00
|
|
|
target = target.lower()
|
|
|
|
if ircutils.isUserHostmask(target):
|
|
|
|
return target
|
|
|
|
elif target in self.nicks:
|
|
|
|
return self.nicks[target]
|
2009-01-19 22:10:51 +00:00
|
|
|
elif irc:
|
2008-08-04 10:11:15 +00:00
|
|
|
try:
|
|
|
|
return irc.state.nickToHostmask(target)
|
|
|
|
except:
|
2009-01-19 22:10:51 +00:00
|
|
|
if reply_now:
|
|
|
|
if with_nick:
|
|
|
|
return "%s!*@*" % target
|
|
|
|
return "*@*"
|
|
|
|
return
|
2007-01-22 21:14:41 +00:00
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
if target in self.nicks:
|
|
|
|
return self.nicks[target]
|
|
|
|
else:
|
|
|
|
return "%s!*@*" % target
|
|
|
|
|
2007-01-23 23:42:45 +00:00
|
|
|
def die(self):
|
2008-08-04 10:11:15 +00:00
|
|
|
global queue
|
|
|
|
if self.db:
|
2008-10-28 08:09:48 +00:00
|
|
|
try:
|
|
|
|
self.db.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
self.thread_timer.cancel()
|
|
|
|
except:
|
|
|
|
pass
|
2008-08-04 10:11:15 +00:00
|
|
|
queue.clear()
|
2012-06-25 22:40:09 +00:00
|
|
|
schedule.removeEvent(self.name() + '_review')
|
|
|
|
schedule.removeEvent(self.name() + '_autoremove')
|
2010-04-02 20:28:43 +00:00
|
|
|
self.pendingReviews.close()
|
2012-07-05 05:31:53 +00:00
|
|
|
self.managedBans.close()
|
2007-01-23 23:42:45 +00:00
|
|
|
|
|
|
|
def reset(self):
|
2008-08-04 10:11:15 +00:00
|
|
|
global queue
|
|
|
|
if self.db:
|
2008-10-28 08:09:48 +00:00
|
|
|
try:
|
|
|
|
self.db.close()
|
|
|
|
except:
|
|
|
|
pass
|
2008-08-04 10:11:15 +00:00
|
|
|
queue.clear()
|
2009-01-19 22:10:51 +00:00
|
|
|
# self.logs.clear()
|
2008-08-04 10:11:15 +00:00
|
|
|
self.lastMsgs.clear()
|
|
|
|
self.lastStates.clear()
|
2009-10-12 18:26:35 +00:00
|
|
|
# self.nicks.clear()
|
2007-01-23 23:42:45 +00:00
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
def __call__(self, irc, msg):
|
|
|
|
try:
|
|
|
|
super(self.__class__, self).__call__(irc, msg)
|
|
|
|
if irc in self.lastMsgs:
|
|
|
|
if irc not in self.lastStates:
|
|
|
|
self.lastStates[irc] = irc.state.copy()
|
|
|
|
self.lastStates[irc].addMsg(irc, self.lastMsgs[irc])
|
|
|
|
finally:
|
|
|
|
self.lastMsgs[irc] = msg
|
|
|
|
|
2011-08-26 06:48:13 +00:00
|
|
|
def db_run(self, query, parms, expect_result = False, expect_id = False, retry = True):
|
2010-05-14 16:17:40 +00:00
|
|
|
if not self.db or self.db.closed:
|
|
|
|
db = self.registryValue('database')
|
|
|
|
if db:
|
|
|
|
try:
|
|
|
|
self.db = sqlite.connect(db)
|
|
|
|
except:
|
|
|
|
self.log.error("Bantracker: failed to connect to database")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
self.log.error("Bantracker: no database")
|
|
|
|
return
|
2011-08-26 06:48:13 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
maxCount = 5 #TODO: Make this configurable?
|
|
|
|
err = None
|
|
|
|
|
|
|
|
while count < maxCount:
|
|
|
|
try:
|
|
|
|
cur = self.db.cursor()
|
|
|
|
cur.execute(query, parms)
|
|
|
|
break
|
|
|
|
except Exception, err:
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
if count == maxCount:
|
|
|
|
self.log.error("Bantracker: Error while trying to access the Bantracker database (%s(%s)).", type(err).__name__, str(err))
|
|
|
|
try:
|
|
|
|
self.db.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
self.db = None # force reconnection to database
|
|
|
|
|
|
|
|
if not retry: # We probably failed twice, so bigger issues than database locking
|
|
|
|
return None
|
|
|
|
return self.db_run(query, parms, expect_result, expect_id, False) # Try again
|
|
|
|
|
2007-01-22 21:14:41 +00:00
|
|
|
data = None
|
2009-10-12 18:26:35 +00:00
|
|
|
if expect_result and cur: data = cur.fetchall()
|
2007-01-23 23:42:45 +00:00
|
|
|
if expect_id: data = self.db.insert_id()
|
2007-01-22 21:14:41 +00:00
|
|
|
self.db.commit()
|
|
|
|
return data
|
|
|
|
|
2010-04-04 02:48:54 +00:00
|
|
|
def requestComment(self, irc, channel, ban):
|
2010-04-13 02:02:07 +00:00
|
|
|
if not ban or not self.registryValue('request', channel):
|
2010-04-03 22:46:32 +00:00
|
|
|
return
|
2010-03-15 16:12:51 +00:00
|
|
|
# check the type of the action taken
|
2010-03-14 13:07:59 +00:00
|
|
|
mask = ban.mask
|
2012-06-25 22:30:11 +00:00
|
|
|
type = ban.type
|
2010-04-04 02:48:54 +00:00
|
|
|
if type == 'quiet':
|
|
|
|
mask = mask[1:]
|
2010-03-17 10:14:07 +00:00
|
|
|
# check if type is enabled
|
2010-04-04 02:48:54 +00:00
|
|
|
if type not in self.registryValue('request.type', channel):
|
2010-03-17 10:14:07 +00:00
|
|
|
return
|
2010-03-17 00:46:18 +00:00
|
|
|
prefix = conf.supybot.reply.whenAddressedBy.chars()[0] # prefix char for commands
|
2010-03-15 23:51:32 +00:00
|
|
|
# check to who send the request
|
2010-04-02 17:03:01 +00:00
|
|
|
try:
|
2010-04-04 02:48:54 +00:00
|
|
|
nick = ircutils.nickFromHostmask(ban.who)
|
2010-04-02 17:03:01 +00:00
|
|
|
except:
|
2010-04-04 02:48:54 +00:00
|
|
|
nick = ban.who
|
2010-04-13 02:02:07 +00:00
|
|
|
if nickMatch(nick, self.registryValue('request.ignore', channel)):
|
|
|
|
return
|
2010-04-04 02:48:54 +00:00
|
|
|
if nickMatch(nick, self.registryValue('request.forward', channel)):
|
2012-06-25 22:30:11 +00:00
|
|
|
# somebody else should comment this (like with bans set by bots)
|
2010-04-03 04:57:08 +00:00
|
|
|
s = "Please somebody comment on the %s of %s in %s done by %s, use:"\
|
2010-04-04 02:48:54 +00:00
|
|
|
" %scomment %s <comment>" %(type, mask, channel, nick, prefix, ban.id)
|
2011-05-29 22:22:34 +00:00
|
|
|
self._sendForward(irc, s, 'request', channel)
|
2010-04-04 02:48:54 +00:00
|
|
|
else:
|
2012-06-25 22:30:11 +00:00
|
|
|
# send to operator
|
2010-04-04 02:48:54 +00:00
|
|
|
s = "Please comment on the %s of %s in %s, use: %scomment %s <comment>" \
|
|
|
|
%(type, mask, channel, prefix, ban.id)
|
|
|
|
irc.reply(s, to=nick, private=True)
|
2010-03-14 13:07:59 +00:00
|
|
|
|
2010-04-03 04:44:57 +00:00
|
|
|
def reviewBans(self, irc=None):
|
2011-05-29 22:22:34 +00:00
|
|
|
reviewTime = int(self.registryValue('review.when') * 86400)
|
2010-04-04 00:55:59 +00:00
|
|
|
if not reviewTime:
|
|
|
|
# time is zero, do nothing
|
|
|
|
return
|
2012-06-25 22:35:42 +00:00
|
|
|
|
|
|
|
now = nowSeconds()
|
2012-06-25 22:30:11 +00:00
|
|
|
lastReview = self.pendingReviews.lastReview
|
|
|
|
self.pendingReviews.lastReview = now # update last time reviewed
|
|
|
|
if not lastReview:
|
2010-04-03 03:26:20 +00:00
|
|
|
# initialize last time reviewed timestamp
|
2012-06-25 22:30:11 +00:00
|
|
|
lastReview = now - reviewTime
|
2010-04-04 02:48:54 +00:00
|
|
|
|
2010-04-03 03:26:20 +00:00
|
|
|
for channel, bans in self.bans.iteritems():
|
2010-04-04 02:48:54 +00:00
|
|
|
if not self.registryValue('enabled', channel) \
|
2011-05-29 22:22:34 +00:00
|
|
|
or not self.registryValue('review', channel):
|
2010-04-03 03:26:20 +00:00
|
|
|
continue
|
2010-04-04 02:48:54 +00:00
|
|
|
|
2010-04-03 03:26:20 +00:00
|
|
|
for ban in bans:
|
2011-05-31 06:15:58 +00:00
|
|
|
# XXX this shouldn't be hardcoded, but I'm starting to hate this plugin,
|
|
|
|
# the less I touch it the better.
|
|
|
|
if ban.mask.endswith('$#ubuntu-read-topic'):
|
|
|
|
continue
|
2012-06-25 22:30:11 +00:00
|
|
|
|
|
|
|
type = ban.type
|
|
|
|
if type in ('removal', 'mark'):
|
|
|
|
# skip kicks and marks
|
2011-05-31 22:43:45 +00:00
|
|
|
continue
|
2012-06-25 22:30:11 +00:00
|
|
|
|
2010-04-04 00:55:59 +00:00
|
|
|
banAge = now - ban.when
|
2012-06-25 22:30:11 +00:00
|
|
|
reviewWindow = lastReview - ban.when
|
2012-06-25 22:35:42 +00:00
|
|
|
#self.log.debug('review ban: %s ban %s by %s (%s/%s/%s %s)',
|
|
|
|
# channel, ban.mask, ban.who, reviewWindow, reviewTime,
|
|
|
|
# banAge, reviewTime - reviewWindow)
|
2010-04-04 00:55:59 +00:00
|
|
|
if reviewWindow <= reviewTime < banAge:
|
2010-04-03 03:26:20 +00:00
|
|
|
# ban is old enough, and inside the "review window"
|
|
|
|
try:
|
|
|
|
# ban.who should be a user hostmask
|
2010-04-04 02:48:54 +00:00
|
|
|
nick = ircutils.nickFromHostmask(ban.who)
|
2010-04-03 03:26:20 +00:00
|
|
|
host = ircutils.hostFromHostmask(ban.who)
|
|
|
|
except:
|
2010-04-03 16:20:15 +00:00
|
|
|
if ircutils.isNick(ban.who, strictRfc=True):
|
|
|
|
# ok, op's nick, use it
|
2010-04-04 02:48:54 +00:00
|
|
|
nick = ban.who
|
2010-04-03 16:20:15 +00:00
|
|
|
host = None
|
|
|
|
else:
|
|
|
|
# probably a ban restored by IRC server in a netsplit
|
|
|
|
# XXX see if something can be done about this
|
|
|
|
continue
|
2011-05-29 22:22:34 +00:00
|
|
|
if nickMatch(nick, self.registryValue('review.ignore', channel)):
|
2010-04-03 03:26:20 +00:00
|
|
|
# in the ignore list
|
|
|
|
continue
|
|
|
|
if not ban.id:
|
|
|
|
ban.id = self.get_banId(ban.mask, channel)
|
2011-05-31 04:45:05 +00:00
|
|
|
mask = ban.mask
|
|
|
|
if type == 'quiet':
|
|
|
|
mask = mask[1:]
|
2011-05-29 22:22:34 +00:00
|
|
|
if nickMatch(nick, self.registryValue('review.forward', channel)):
|
2011-05-31 06:39:28 +00:00
|
|
|
s = "Review: %s '%s' set by %s on %s in %s, link: %s/bans.cgi?log=%s" \
|
|
|
|
% (type,
|
|
|
|
mask,
|
|
|
|
nick,
|
|
|
|
ban.ascwhen,
|
|
|
|
channel,
|
|
|
|
self.registryValue('bansite'),
|
|
|
|
ban.id)
|
2011-05-29 22:22:34 +00:00
|
|
|
self._sendForward(irc, s, 'review', channel)
|
2010-04-03 03:26:20 +00:00
|
|
|
else:
|
2011-05-31 06:39:28 +00:00
|
|
|
s = "Review: %s '%s' set on %s in %s, link: %s/bans.cgi?log=%s" \
|
|
|
|
% (type,
|
|
|
|
mask,
|
|
|
|
ban.ascwhen,
|
|
|
|
channel,
|
|
|
|
self.registryValue('bansite'),
|
|
|
|
ban.id)
|
2010-04-04 02:48:54 +00:00
|
|
|
msg = ircmsgs.privmsg(nick, s)
|
2012-06-25 22:30:11 +00:00
|
|
|
if (nick, msg) not in self.pendingReviews[host]:
|
2010-04-04 02:48:54 +00:00
|
|
|
self.pendingReviews[host].append((nick, msg))
|
2010-04-04 00:55:59 +00:00
|
|
|
elif banAge < reviewTime:
|
2010-04-03 03:26:20 +00:00
|
|
|
# since we made sure bans are sorted by time, the bans left are more recent
|
|
|
|
break
|
2010-04-04 02:48:54 +00:00
|
|
|
|
2011-05-29 22:22:34 +00:00
|
|
|
def _sendForward(self, irc, s, setting, channel=None):
|
2010-04-04 02:48:54 +00:00
|
|
|
if not irc:
|
|
|
|
return
|
2011-05-29 22:22:34 +00:00
|
|
|
for chan in self.registryValue('%s.forward.channels' % setting, channel=channel):
|
2010-04-04 02:48:54 +00:00
|
|
|
msg = ircmsgs.notice(chan, s)
|
|
|
|
irc.queueMsg(msg)
|
2010-03-30 11:47:16 +00:00
|
|
|
|
2010-10-24 06:13:43 +00:00
|
|
|
def _banreviewfix(self):
|
|
|
|
# FIXME workaround until proper fix is done.
|
|
|
|
bag = set()
|
|
|
|
nodups = set()
|
|
|
|
for host, reviews in self.pendingReviews.iteritems():
|
|
|
|
for nick, msg in reviews:
|
|
|
|
if nick == 'Automated-Addition':
|
|
|
|
continue
|
|
|
|
chan, m = msg.args[0], msg.args[1]
|
|
|
|
s = m.rpartition(' ')[0] #remove the url
|
|
|
|
if (nick, chan, s) not in bag:
|
|
|
|
bag.add((nick, chan, s))
|
|
|
|
nodups.add((host, nick, msg))
|
|
|
|
|
|
|
|
self.pendingReviews.clear()
|
|
|
|
|
|
|
|
for host, nick, msg in nodups:
|
2012-06-25 22:30:11 +00:00
|
|
|
self.pendingReviews[host].append((nick, msg))
|
2010-10-24 06:13:43 +00:00
|
|
|
|
2010-04-02 17:03:01 +00:00
|
|
|
def _sendReviews(self, irc, msg):
|
|
|
|
host = ircutils.hostFromHostmask(msg.prefix)
|
|
|
|
if host in self.pendingReviews:
|
2010-10-24 06:13:43 +00:00
|
|
|
self._banreviewfix()
|
2010-04-04 02:48:54 +00:00
|
|
|
for nick, m in self.pendingReviews[host]:
|
|
|
|
if msg.nick != nick and not irc.isChannel(nick): # I'm a bit extra careful here
|
2010-04-03 16:20:15 +00:00
|
|
|
# correct nick in msg
|
2010-04-04 02:48:54 +00:00
|
|
|
m = ircmsgs.privmsg(msg.nick, m.args[1])
|
|
|
|
irc.queueMsg(m)
|
2010-04-02 17:03:01 +00:00
|
|
|
del self.pendingReviews[host]
|
2010-04-03 16:20:15 +00:00
|
|
|
# check if we have any reviews by nick to send
|
|
|
|
if None in self.pendingReviews:
|
|
|
|
L = self.pendingReviews[None]
|
|
|
|
for i, v in enumerate(L):
|
2010-04-04 02:48:54 +00:00
|
|
|
nick, m = v
|
|
|
|
if ircutils.strEqual(msg.nick, nick):
|
|
|
|
irc.queueMsg(m)
|
2010-04-03 16:20:15 +00:00
|
|
|
del L[i]
|
|
|
|
if not L:
|
|
|
|
del self.pendingReviews[None]
|
2010-03-14 13:07:59 +00:00
|
|
|
|
2012-08-18 14:27:50 +00:00
|
|
|
def getOp(self, irc, channel):
|
|
|
|
msg = ircmsgs.privmsg('Chanserv', "op %s %s" % (channel, irc.nick))
|
|
|
|
irc.queueMsg(msg)
|
2012-08-23 10:21:58 +00:00
|
|
|
schedule.addEvent(lambda: self._getOpFail(irc, channel), 60,
|
|
|
|
'Bantracker_getop_%s' % channel)
|
|
|
|
|
|
|
|
def _getOpFail(self, irc, channel):
|
|
|
|
for c in self.registryValue('autoremove.notify.channels', channel):
|
|
|
|
notice = ircmsgs.notice(c, "Failed to get op in %s" % channel)
|
|
|
|
irc.queueMsg(notice)
|
|
|
|
|
|
|
|
def _getOpOK(self, channel):
|
|
|
|
try:
|
|
|
|
schedule.removeEvent('Bantracker_getop_%s' % channel)
|
2012-08-23 10:50:39 +00:00
|
|
|
return True
|
2012-08-23 10:21:58 +00:00
|
|
|
except KeyError:
|
2012-08-23 10:50:39 +00:00
|
|
|
return False
|
2012-08-18 14:27:50 +00:00
|
|
|
|
|
|
|
def removeBans(self, irc, channel, modes, deop=False):
|
|
|
|
# send unban messages, with 4 modes max each.
|
|
|
|
maxModes = 4
|
|
|
|
if deop:
|
|
|
|
modes.append(('-o', irc.nick))
|
|
|
|
for i in range(len(modes) / maxModes + 1):
|
|
|
|
L = modes[i * maxModes : (i + 1) * maxModes]
|
|
|
|
if L:
|
|
|
|
msg = ircmsgs.mode(channel, ircutils.joinModes(L))
|
|
|
|
irc.queueMsg(msg)
|
|
|
|
|
2012-07-05 00:35:07 +00:00
|
|
|
def autoRemoveBans(self, irc):
|
2012-06-27 04:14:51 +00:00
|
|
|
modedict = { 'quiet': '-q', 'ban': '-b' }
|
2012-07-16 01:28:38 +00:00
|
|
|
unbandict = defaultdict(list)
|
2012-06-25 22:40:09 +00:00
|
|
|
for ban in self.managedBans.popExpired():
|
2012-06-27 04:14:51 +00:00
|
|
|
channel, mask, type = ban.ban.channel, ban.ban.mask, ban.ban.type
|
2012-07-05 01:12:55 +00:00
|
|
|
if not self.registryValue('autoremove', channel):
|
|
|
|
continue
|
|
|
|
|
2012-07-05 23:40:57 +00:00
|
|
|
if type == 'quiet':
|
|
|
|
mask = mask[1:]
|
2012-07-05 00:35:07 +00:00
|
|
|
self.log.info("%s [%s] %s in %s expired", type,
|
2012-07-05 01:12:55 +00:00
|
|
|
ban.ban.id,
|
|
|
|
mask,
|
|
|
|
channel)
|
2012-07-16 01:28:38 +00:00
|
|
|
unbandict[channel].append((modedict[type], mask))
|
|
|
|
for channel, modes in unbandict.iteritems():
|
2012-08-18 14:27:50 +00:00
|
|
|
if not self.opped[channel]:
|
|
|
|
self.pendingBanremoval[channel] = modes
|
|
|
|
self.getOp(irc, channel)
|
|
|
|
else:
|
|
|
|
self.removeBans(irc, channel, modes)
|
2012-06-25 22:40:09 +00:00
|
|
|
|
2012-07-05 00:35:07 +00:00
|
|
|
# notify about bans soon to expire
|
|
|
|
for ban in self.managedBans.getExpired(600):
|
|
|
|
if ban.notified:
|
|
|
|
continue
|
2012-07-05 01:12:55 +00:00
|
|
|
|
|
|
|
channel = ban.ban.channel
|
|
|
|
if not self.registryValue('autoremove', channel) \
|
|
|
|
or not self.registryValue('autoremove.notify', channel):
|
|
|
|
continue
|
|
|
|
|
2012-07-05 23:40:57 +00:00
|
|
|
type, mask = ban.ban.type, ban.ban.mask
|
|
|
|
if type == 'quiet':
|
|
|
|
mask = mask[1:]
|
2012-07-05 01:12:55 +00:00
|
|
|
for c in self.registryValue('autoremove.notify.channels', channel):
|
2012-07-05 23:40:57 +00:00
|
|
|
notice = ircmsgs.notice(c, "%s %s%s%s %s in %s will expire in a few minutes." \
|
|
|
|
% (type,
|
|
|
|
ircutils.mircColor('[', 'light green'),
|
|
|
|
ircutils.bold(ban.ban.id),
|
|
|
|
ircutils.mircColor(']', 'light green'),
|
|
|
|
ircutils.mircColor(mask, 'teal'),
|
|
|
|
ircutils.mircColor(channel, 'teal')))
|
2012-07-05 01:12:55 +00:00
|
|
|
irc.queueMsg(notice)
|
2012-07-05 00:35:07 +00:00
|
|
|
ban.notified = True
|
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
def doLog(self, irc, channel, s):
|
|
|
|
if not self.registryValue('enabled', channel):
|
|
|
|
return
|
|
|
|
channel = ircutils.toLower(channel)
|
|
|
|
if channel not in self.logs.keys():
|
2010-04-09 15:10:08 +00:00
|
|
|
self.logs[channel] = []
|
2006-06-26 17:57:20 +00:00
|
|
|
format = conf.supybot.log.timestampFormat()
|
|
|
|
if format:
|
2008-08-04 10:11:15 +00:00
|
|
|
s = time.strftime(format, time.gmtime()) + " " + ircutils.stripFormatting(s)
|
2010-04-09 15:10:08 +00:00
|
|
|
self.logs[channel] = self.logs[channel][-199:] + [s.strip()]
|
2006-11-17 00:11:05 +00:00
|
|
|
|
2010-04-03 22:46:32 +00:00
|
|
|
def doKickban(self, irc, channel, *args, **kwargs):
|
|
|
|
ban = self._doKickban(irc, channel, *args, **kwargs)
|
|
|
|
self.requestComment(irc, channel, ban)
|
|
|
|
return ban
|
|
|
|
|
2011-06-18 19:27:51 +00:00
|
|
|
def _doKickban(self, irc, channel, operator, target, kickmsg = None, use_time = None,
|
|
|
|
extra_comment = None, add_to_cache = True):
|
2006-06-26 17:57:20 +00:00
|
|
|
if not self.registryValue('enabled', channel):
|
|
|
|
return
|
|
|
|
n = now()
|
2009-01-19 22:10:51 +00:00
|
|
|
if use_time:
|
|
|
|
n = fromTime(use_time)
|
2010-04-02 17:03:01 +00:00
|
|
|
try:
|
|
|
|
nick = ircutils.nickFromHostmask(operator)
|
|
|
|
except:
|
|
|
|
nick = operator
|
2007-01-23 23:42:45 +00:00
|
|
|
id = self.db_run("INSERT INTO bans (channel, mask, operator, time, log) values(%s, %s, %s, %s, %s)",
|
2009-10-12 18:26:35 +00:00
|
|
|
(channel, target, nick, n, '\n'.join(self.logs[channel])), expect_id=True)
|
2006-06-26 17:57:20 +00:00
|
|
|
if kickmsg and id and not (kickmsg == nick):
|
2007-01-23 23:42:45 +00:00
|
|
|
self.db_run("INSERT INTO comments (ban_id, who, comment, time) values(%s,%s,%s,%s)", (id, nick, kickmsg, n))
|
2009-10-12 18:26:35 +00:00
|
|
|
if extra_comment:
|
|
|
|
self.db_run("INSERT INTO comments (ban_id, who, comment, time) values(%s,%s,%s,%s)", (id, nick, extra_comment, n))
|
2012-06-27 04:14:51 +00:00
|
|
|
ban = Ban(mask=target, who=operator, when=time.mktime(time.gmtime()), id=id, channel=channel)
|
2011-06-18 19:27:51 +00:00
|
|
|
if add_to_cache:
|
|
|
|
if channel not in self.bans:
|
|
|
|
self.bans[channel] = []
|
|
|
|
self.bans[channel].append(ban)
|
2010-04-03 22:46:32 +00:00
|
|
|
return ban
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
def doUnban(self, irc, channel, nick, mask):
|
|
|
|
if not self.registryValue('enabled', channel):
|
|
|
|
return
|
2007-01-23 23:42:45 +00:00
|
|
|
data = self.db_run("SELECT MAX(id) FROM bans where channel=%s and mask=%s", (channel, mask), expect_result=True)
|
2011-06-19 01:32:04 +00:00
|
|
|
if data and len(data) and not (data[0][0] == None):
|
2007-01-23 23:42:45 +00:00
|
|
|
self.db_run("UPDATE bans SET removal=%s , removal_op=%s WHERE id=%s", (now(), nick, int(data[0][0])))
|
2008-08-04 10:11:15 +00:00
|
|
|
if not channel in self.bans:
|
|
|
|
self.bans[channel] = []
|
2012-07-16 00:06:23 +00:00
|
|
|
for idx, ban in enumerateReversed(self.bans[channel]):
|
2008-08-04 10:11:15 +00:00
|
|
|
if ban.mask == mask:
|
2011-06-19 01:32:04 +00:00
|
|
|
del self.bans[channel][idx]
|
|
|
|
# we don't break here because bans might be duplicated.
|
2012-07-16 00:06:23 +00:00
|
|
|
for idx, br in enumerateReversed(self.managedBans.shelf):
|
|
|
|
if (channel == br.ban.channel) and (mask == br.ban.mask):
|
|
|
|
del self.managedBans.shelf[idx]
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
def doPrivmsg(self, irc, msg):
|
|
|
|
(recipients, text) = msg.args
|
|
|
|
for channel in recipients.split(','):
|
|
|
|
if irc.isChannel(channel):
|
|
|
|
nick = msg.nick or irc.nick
|
|
|
|
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))
|
2010-03-30 17:56:35 +00:00
|
|
|
self._sendReviews(irc, msg)
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
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]
|
|
|
|
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))
|
2009-10-12 18:26:35 +00:00
|
|
|
if oldNick.lower() in self.nicks:
|
|
|
|
del self.nicks[oldNick.lower()]
|
2008-08-04 10:11:15 +00:00
|
|
|
nick = newNick.lower()
|
|
|
|
hostmask = nick + "!".join(msg.prefix.lower().split('!')[1:])
|
|
|
|
self.nicks[nick] = hostmask
|
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
def doJoin(self, irc, msg):
|
2008-08-04 10:11:15 +00:00
|
|
|
global queue
|
2006-06-26 17:57:20 +00:00
|
|
|
for channel in msg.args[0].split(','):
|
2009-10-12 18:26:35 +00:00
|
|
|
if msg.nick:
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s (%s) has joined %s\n' % (msg.nick, msg.prefix.split('!', 1)[1], channel))
|
|
|
|
else:
|
|
|
|
self.doLog(irc, channel,
|
|
|
|
'*** %s has joined %s\n' % (msg.prefix, channel))
|
2011-06-19 02:14:35 +00:00
|
|
|
if msg.nick == irc.nick:
|
|
|
|
if channel in self.bans:
|
|
|
|
del self.bans[channel]
|
2008-08-04 10:11:15 +00:00
|
|
|
queue.queue(ircmsgs.mode(channel, 'b'))
|
|
|
|
nick = msg.nick.lower() or msg.prefix.lower().split('!', 1)[0]
|
|
|
|
self.nicks[nick] = msg.prefix.lower()
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
def doKick(self, irc, msg):
|
|
|
|
if len(msg.args) == 3:
|
|
|
|
(channel, target, kickmsg) = msg.args
|
|
|
|
else:
|
|
|
|
(channel, target) = msg.args
|
|
|
|
kickmsg = ''
|
2009-10-12 18:26:35 +00:00
|
|
|
host = self.nick_to_host(irc, target, True)
|
|
|
|
if host == "%s!*@*" % host:
|
|
|
|
host = None
|
2006-06-26 17:57:20 +00:00
|
|
|
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))
|
2010-04-01 20:52:22 +00:00
|
|
|
self.doKickban(irc, channel, msg.prefix, target, kickmsg, extra_comment=host)
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
def doPart(self, irc, msg):
|
|
|
|
for channel in msg.args[0].split(','):
|
2010-01-30 15:19:53 +00:00
|
|
|
self.doLog(irc, channel, '*** %s (%s) has left %s (%s)\n' % (msg.nick, msg.prefix, channel, len(msg.args) > 1 and msg.args[1] or ''))
|
|
|
|
if len(msg.args) > 1 and msg.args[1].startswith('requested by'):
|
2006-06-26 17:57:20 +00:00
|
|
|
args = msg.args[1].split()
|
2010-04-13 00:24:45 +00:00
|
|
|
self.doKickban(irc, channel, args[2], msg.nick, ' '.join(args[3:]).strip(), extra_comment=msg.prefix)
|
2006-06-26 17:57:20 +00: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:])))
|
2009-12-11 17:53:15 +00:00
|
|
|
modes = ircutils.separateModes(msg.args[1:])
|
|
|
|
for param in modes:
|
2010-01-30 15:19:53 +00:00
|
|
|
mode = param[0]
|
2012-08-18 14:27:50 +00:00
|
|
|
# op stuff
|
|
|
|
if mode[1] == "o":
|
|
|
|
if ircutils.nickEqual(irc.nick, param[1]):
|
|
|
|
opped = self.opped[channel] = mode[0] == '+'
|
|
|
|
if opped == True:
|
2012-08-23 10:50:39 +00:00
|
|
|
opped_ok = self._getOpOK(channel)
|
2012-08-18 14:27:50 +00:00
|
|
|
# check if we have bans to remove
|
|
|
|
if channel in self.pendingBanremoval:
|
|
|
|
modes = self.pendingBanremoval.pop(channel)
|
2012-08-23 10:50:39 +00:00
|
|
|
self.removeBans(irc, channel, modes, deop=opped_ok)
|
2010-01-30 15:19:53 +00:00
|
|
|
continue
|
2012-08-18 14:27:50 +00:00
|
|
|
|
|
|
|
# channel mask stuff
|
|
|
|
realname = mask = ''
|
|
|
|
comment = None
|
|
|
|
if mode[1] not in "bq":
|
|
|
|
continue
|
|
|
|
|
2010-01-30 15:19:53 +00:00
|
|
|
mask = param[1]
|
|
|
|
if mask.startswith("$r:"):
|
|
|
|
mask = mask[3:]
|
|
|
|
realname = ' (realname)'
|
|
|
|
|
2012-08-18 14:27:50 +00:00
|
|
|
if mode[1] == 'q':
|
2010-01-30 15:19:53 +00:00
|
|
|
mask = '%' + mask
|
|
|
|
|
2012-08-18 14:27:50 +00:00
|
|
|
if mode[0] == '+':
|
2010-01-30 15:19:53 +00:00
|
|
|
comment = self.getHostFromBan(irc, msg, mask)
|
2012-06-25 22:40:09 +00:00
|
|
|
ban = self.doKickban(irc, channel, msg.prefix, mask + realname,
|
|
|
|
extra_comment=comment)
|
2012-08-18 14:27:50 +00:00
|
|
|
elif mode[0] == '-':
|
2010-01-30 15:19:53 +00:00
|
|
|
self.doUnban(irc,channel, msg.nick, mask + realname)
|
2006-06-26 17:57:20 +00:00
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
def getHostFromBan(self, irc, msg, mask):
|
|
|
|
if irc not in self.lastStates:
|
|
|
|
self.lastStates[irc] = irc.state.copy()
|
|
|
|
if mask[0] == '%':
|
|
|
|
mask = mask[1:]
|
2010-03-14 13:11:35 +00:00
|
|
|
try:
|
|
|
|
(nick, ident, host) = ircutils.splitHostmask(mask)
|
|
|
|
except AssertionError:
|
|
|
|
# not a hostmask
|
|
|
|
return None
|
2009-10-12 18:26:35 +00:00
|
|
|
channel = None
|
|
|
|
chan = None
|
|
|
|
if mask[0] not in ('*', '?'): # Nick ban
|
|
|
|
if nick in self.nicks:
|
|
|
|
return self.nicks[nick]
|
|
|
|
else: # Host/ident ban
|
|
|
|
for (inick, ihost) in self.nicks.iteritems():
|
|
|
|
if ircutils.hostmaskPatternEqual(mask, ihost):
|
|
|
|
return ihost
|
|
|
|
return None
|
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
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):
|
2009-10-12 18:26:35 +00:00
|
|
|
if irc not in self.lastStates:
|
|
|
|
self.lastStates[irc] = irc.state.copy()
|
2006-06-26 17:57:20 +00:00
|
|
|
for (channel, chan) in self.lastStates[irc].channels.iteritems():
|
|
|
|
if msg.nick in chan.users:
|
2009-10-12 18:26:35 +00:00
|
|
|
self.doLog(irc, channel, '*** %s (%s) has quit IRC (%s)\n' % (msg.nick, msg.prefix, msg.args[0]))
|
|
|
|
# if msg.nick in self.user:
|
|
|
|
# del self.user[msg.nick]
|
2006-06-26 17:57:20 +00:00
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
# Gotta catch my own messages *somehow* :)
|
|
|
|
# Let's try this little trick...
|
|
|
|
if msg.command in ('PRIVMSG', 'NOTICE'):
|
|
|
|
# Other messages should be sent back to us.
|
|
|
|
m = ircmsgs.IrcMsg(msg=msg, prefix=irc.prefix)
|
|
|
|
self(irc, m)
|
|
|
|
return msg
|
2006-12-27 14:33:59 +00:00
|
|
|
|
2009-10-15 22:06:18 +00:00
|
|
|
# def callPrecedence(self, irc):
|
|
|
|
# before = []
|
|
|
|
# for cb in irc.callbacks:
|
|
|
|
# if cb.name() == 'IRCLogin':
|
|
|
|
# return (['IRCLogin'], [])
|
|
|
|
# return ([], [])
|
2008-11-02 01:29:06 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def check_auth(self, irc, msg, args, cap='bantracker'):
|
2008-11-02 01:29:06 +00:00
|
|
|
hasIRCLogin = False
|
|
|
|
for cb in self.callPrecedence(irc)[0]:
|
|
|
|
if cb.name() == "IRCLogin":
|
|
|
|
hasIRCLogin = True
|
|
|
|
if hasIRCLogin and not msg.tagged('identified'):
|
2008-07-22 01:40:39 +00:00
|
|
|
irc.error(conf.supybot.replies.incorrectAuthentication())
|
|
|
|
return False
|
|
|
|
try:
|
2010-04-13 04:11:15 +00:00
|
|
|
user = ircdb.users.getUser(msg.prefix)
|
2008-07-22 01:40:39 +00:00
|
|
|
except:
|
|
|
|
irc.error(conf.supybot.replies.incorrectAuthentication())
|
|
|
|
return False
|
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
if not capab(user, cap):
|
|
|
|
irc.error(conf.supybot.replies.noCapability() % cap)
|
2008-07-22 01:40:39 +00:00
|
|
|
return False
|
|
|
|
return user
|
|
|
|
|
2007-02-15 22:16:44 +00:00
|
|
|
def btlogin(self, irc, msg, args):
|
2009-01-19 22:10:51 +00:00
|
|
|
"""Takes no arguments
|
2008-05-05 16:44:14 +00:00
|
|
|
|
|
|
|
Sends you a message with a link to login to the bantracker.
|
|
|
|
"""
|
2008-07-22 01:40:39 +00:00
|
|
|
user = self.check_auth(irc, msg, args)
|
|
|
|
if not user:
|
|
|
|
return
|
|
|
|
user.addAuth(msg.prefix)
|
2009-10-12 18:26:35 +00:00
|
|
|
try:
|
|
|
|
ircdb.users.setUser(user, flush=False)
|
|
|
|
except:
|
|
|
|
pass
|
2008-07-20 15:59:27 +00:00
|
|
|
|
|
|
|
if not capab(user, 'bantracker'):
|
|
|
|
irc.error(conf.supybot.replies.noCapability() % 'bantracker')
|
|
|
|
return
|
|
|
|
if not self.registryValue('bansite'):
|
|
|
|
irc.error("No bansite set, please set supybot.plugins.Bantracker.bansite")
|
|
|
|
return
|
2010-02-04 18:58:05 +00:00
|
|
|
sessid = hashlib.md5('%s%s%d' % (msg.prefix, time.time(), random.randint(1,100000))).hexdigest()
|
2008-08-04 10:11:15 +00:00
|
|
|
self.db_run("INSERT INTO sessions (session_id, user, time) VALUES (%s, %s, %d);",
|
2009-10-12 18:26:35 +00:00
|
|
|
( sessid, msg.nick, int(time.mktime(time.gmtime())) ) )
|
2008-08-04 10:11:15 +00:00
|
|
|
irc.reply('Log in at %s/bans.cgi?sess=%s' % (self.registryValue('bansite'), sessid), private=True)
|
2007-02-15 22:16:44 +00:00
|
|
|
|
|
|
|
btlogin = wrap(btlogin)
|
2006-06-26 17:57:20 +00:00
|
|
|
|
2008-05-11 01:36:57 +00:00
|
|
|
def mark(self, irc, msg, args, channel, target, kickmsg):
|
2008-08-04 10:11:15 +00:00
|
|
|
"""[<channel>] <nick|hostmask> [<comment>]
|
2008-05-11 01:36:57 +00:00
|
|
|
|
2008-05-11 04:37:14 +00:00
|
|
|
Creates an entry in the Bantracker as if <nick|hostmask> was kicked from <channel> with the comment <comment>,
|
2008-05-11 01:36:57 +00:00
|
|
|
if <comment> is given it will be uses as the comment on the Bantracker, <channel> is only needed when send in /msg
|
|
|
|
"""
|
2008-07-22 01:40:39 +00:00
|
|
|
user = self.check_auth(irc, msg, args)
|
|
|
|
if not user:
|
2008-05-11 01:36:57 +00:00
|
|
|
return
|
2008-07-20 15:59:27 +00:00
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
if target == '*' or target[0] == '*':
|
|
|
|
irc.error("Can not create a mark for '%s'" % target)
|
|
|
|
return
|
|
|
|
|
2008-05-11 01:36:57 +00:00
|
|
|
if not channel:
|
|
|
|
irc.error('<channel> must be given if not in a channel')
|
|
|
|
return
|
2009-01-19 22:10:51 +00:00
|
|
|
channel = channel.lower()
|
2008-07-20 15:59:27 +00:00
|
|
|
channels = []
|
2008-08-04 10:11:15 +00:00
|
|
|
for chan in irc.state.channels.keys():
|
2009-01-19 22:10:51 +00:00
|
|
|
channels.append(chan.lower())
|
2008-07-20 15:59:27 +00:00
|
|
|
|
|
|
|
if not channel in channels:
|
|
|
|
irc.error('Not in that channel')
|
|
|
|
return
|
|
|
|
|
2008-05-11 01:36:57 +00:00
|
|
|
if not kickmsg:
|
|
|
|
kickmsg = '**MARK**'
|
|
|
|
else:
|
|
|
|
kickmsg = "**MARK** - %s" % kickmsg
|
2008-08-04 10:11:15 +00:00
|
|
|
hostmask = self.nick_to_host(irc, target)
|
2008-05-11 04:37:14 +00:00
|
|
|
|
2008-07-20 15:59:27 +00:00
|
|
|
self.doLog(irc, channel.lower(), '*** %s requested a mark for %s\n' % (msg.nick, target))
|
2010-04-03 23:06:29 +00:00
|
|
|
self._doKickban(irc, channel.lower(), msg.prefix, hostmask, kickmsg)
|
2008-05-11 01:36:57 +00:00
|
|
|
irc.replySuccess()
|
|
|
|
|
|
|
|
mark = wrap(mark, [optional('channel'), 'something', additional('text')])
|
|
|
|
|
2008-07-22 01:40:39 +00:00
|
|
|
def sort_bans(self, channel=None):
|
|
|
|
data = self.db_run("SELECT mask, removal, channel, id FROM bans", (), expect_result=True)
|
|
|
|
if channel:
|
|
|
|
data = [i for i in data if i[2] == channel]
|
2008-08-04 10:11:15 +00:00
|
|
|
bans = [(i[0], i[3]) for i in data if i[1] == None and '%' not in i[0]]
|
|
|
|
mutes = [(i[0], i[3]) for i in data if i[1] == None and '%' in i[0]]
|
2008-07-22 01:40:39 +00:00
|
|
|
return mutes + bans
|
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def get_banId(self, mask, channel):
|
2010-04-01 15:39:46 +00:00
|
|
|
data = self.db_run("SELECT MAX(id) FROM bans WHERE mask=%s AND channel=%s", (mask, channel), True)
|
|
|
|
if data:
|
|
|
|
data = data[0]
|
|
|
|
if not data or not data[0]:
|
2008-08-04 10:11:15 +00:00
|
|
|
return
|
|
|
|
return int(data[0])
|
|
|
|
|
|
|
|
def getBans(self, hostmask, channel):
|
|
|
|
match = []
|
|
|
|
if channel:
|
|
|
|
if channel in self.bans and self.bans[channel]:
|
|
|
|
for b in self.bans[channel]:
|
|
|
|
if hostmaskPatternEqual(b.mask, hostmask):
|
|
|
|
match.append((b.mask, self.get_banId(b.mask,channel)))
|
|
|
|
data = self.sort_bans(channel)
|
|
|
|
for e in data:
|
|
|
|
if hostmaskPatternEqual(e[0], hostmask):
|
|
|
|
if (e[0], e[1]) not in match:
|
|
|
|
match.append((e[0], e[1]))
|
|
|
|
else:
|
|
|
|
for c in self.bans:
|
|
|
|
for b in self.bans[c]:
|
|
|
|
if hostmaskPatternEqual(b.mask, hostmask):
|
|
|
|
match.append((b.mask, self.get_banId(b.mask,c)))
|
|
|
|
data = self.sort_bans()
|
|
|
|
for e in data:
|
|
|
|
if hostmaskPatternEqual(e[0], hostmask):
|
|
|
|
if (e[0], e[1]) not in match:
|
|
|
|
match.append((e[0], e[1]))
|
|
|
|
return match
|
|
|
|
|
2009-10-15 22:06:18 +00:00
|
|
|
def bansearch_real(self, irc, msg, args, target, channel, from_reply=False, reply=None):
|
2008-07-22 01:40:39 +00:00
|
|
|
"""<nick|hostmask> [<channel>]
|
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
Search bans database for a ban on <nick|hostmask>,
|
|
|
|
if <channel> is not given search all channel bans.
|
2008-07-22 01:40:39 +00:00
|
|
|
"""
|
|
|
|
def format_entry(entry):
|
|
|
|
ret = list(entry[:-1])
|
|
|
|
t = cPickle.loads(entry[-1]).astimezone(pytz.timezone('UTC')).strftime("%b %d %Y %H:%M:%S")
|
|
|
|
ret.append(t)
|
|
|
|
return tuple(ret)
|
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
user = self.check_auth(irc, msg, args)
|
|
|
|
if not user:
|
2008-07-22 01:40:39 +00:00
|
|
|
return
|
|
|
|
|
2009-01-19 22:10:51 +00:00
|
|
|
if from_reply:
|
|
|
|
if not reply:
|
|
|
|
if capab(user, 'admin'):
|
|
|
|
if len(queue.msgcache) > 0:
|
|
|
|
irc.reply("Warning: still syncing (%i)" % len(queue.msgcache))
|
|
|
|
irc.reply("No matches found for %s in %s" % (hostmask, True and channel or "any channel"))
|
|
|
|
hostmask = reply
|
|
|
|
else:
|
|
|
|
hostmask = self.nick_to_host(irc, target, reply_now=False)
|
|
|
|
if not hostmask:
|
|
|
|
self.sendWhois(irc, target, True, 'bansearch', irc, msg, args, target, channel)
|
|
|
|
return
|
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
if capab(user, 'owner'):
|
2008-08-04 10:11:15 +00:00
|
|
|
if len(queue.msgcache) > 0:
|
|
|
|
irc.reply("Warning: still syncing (%i)" % len(queue.msgcache))
|
2008-07-22 01:40:39 +00:00
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
if channel:
|
|
|
|
if not ircutils.isChannel(channel):
|
|
|
|
channel = None
|
|
|
|
|
|
|
|
if '*' in target or '?' in target:
|
|
|
|
irc.error("Can only search for a complete hostmask")
|
|
|
|
return
|
|
|
|
hostmask = target
|
|
|
|
if '!' not in target or '@' not in target:
|
|
|
|
hostmask = self.nick_to_host(irc, target)
|
|
|
|
if '!' not in hostmask:
|
|
|
|
if "n=" in hostmask:
|
|
|
|
hostmask = hostmask.replace("n=", "!n=", 1)
|
|
|
|
elif "i=" in hostmask:
|
|
|
|
hostmask = hostmask.replace("i=", "!i=", 1)
|
|
|
|
match = self.getBans(hostmask, channel)
|
|
|
|
|
2008-07-22 01:40:39 +00:00
|
|
|
if not match:
|
|
|
|
irc.reply("No matches found for %s in %s" % (hostmask, True and channel or "any channel"))
|
|
|
|
return
|
|
|
|
ret = []
|
2009-10-12 18:26:35 +00:00
|
|
|
replies = []
|
2008-07-22 01:40:39 +00:00
|
|
|
for m in match:
|
2008-08-04 10:11:15 +00:00
|
|
|
if m[1]:
|
|
|
|
ret.append((format_entry(self.db_run("SELECT mask, operator, channel, time FROM bans WHERE id=%d", m[1], expect_result=True)[0]), m[1]))
|
|
|
|
if not ret:
|
|
|
|
done = []
|
|
|
|
for c in self.bans:
|
|
|
|
for b in self.bans[c]:
|
|
|
|
for m in match:
|
|
|
|
if m[0] == b.mask:
|
|
|
|
if not c in done:
|
|
|
|
irc.reply("Match %s in %s" % (b, c))
|
|
|
|
done.append(c)
|
|
|
|
return
|
2008-07-22 01:40:39 +00:00
|
|
|
for i in ret:
|
2009-10-12 18:26:35 +00:00
|
|
|
if '*' in i[0][0] or '?' in i[0][0]:
|
|
|
|
banstr = "Match: %s by %s in %s on %s (ID: %s)" % (i[0] + (i[1],))
|
|
|
|
else:
|
|
|
|
banstr = "Mark: by %s in %s on %s (ID: %s)" % (i[0][1:] + (i[1],))
|
|
|
|
if (banstr, False) not in replies:
|
|
|
|
replies.append((banstr, False))
|
2008-07-22 01:40:39 +00:00
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
if replies:
|
|
|
|
for r in replies:
|
|
|
|
irc.reply(r[0], private=r[1])
|
|
|
|
return
|
|
|
|
irc.error("Something not so good happened, please tell stdin about it")
|
|
|
|
|
|
|
|
bansearch = wrap(bansearch_real, ['something', optional('something', default=None)])
|
2008-07-22 01:40:39 +00:00
|
|
|
|
|
|
|
def banlog(self, irc, msg, args, target, channel):
|
|
|
|
"""<nick|hostmask> [<channel>]
|
|
|
|
|
|
|
|
Prints the last 5 messages from the nick/host logged before a ban/mute,
|
|
|
|
the nick/host has to have an active ban/mute against it.
|
|
|
|
If channel is not given search all channel bans.
|
|
|
|
"""
|
2009-10-15 20:34:18 +00:00
|
|
|
user = self.check_auth(irc, msg, args)
|
2009-01-19 22:10:51 +00:00
|
|
|
if not user:
|
2008-07-22 01:40:39 +00:00
|
|
|
return
|
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
if capab(user, 'owner') and len(queue.msgcache) > 0:
|
|
|
|
irc.reply("Warning: still syncing (%i)" % len(queue.msgcache))
|
2008-07-22 01:40:39 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
hostmask = self.nick_to_host(irc, target)
|
|
|
|
target = target.split('!', 1)[0]
|
|
|
|
match = self.getBans(hostmask, channel)
|
2008-07-22 01:40:39 +00:00
|
|
|
|
|
|
|
if not match:
|
|
|
|
irc.reply("No matches found for %s (%s) in %s" % (target, hostmask, True and channel or "any channel"))
|
|
|
|
return
|
2008-08-04 10:11:15 +00:00
|
|
|
|
2008-07-22 01:40:39 +00:00
|
|
|
ret = []
|
|
|
|
for m in match:
|
2009-10-12 18:26:35 +00:00
|
|
|
if m[1]:
|
|
|
|
ret.append((self.db_run("SELECT log, channel FROM bans WHERE id=%d", m[1], expect_result=True), m[1]))
|
2008-07-22 01:40:39 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
sent = []
|
2009-10-12 18:26:35 +00:00
|
|
|
if not ret:
|
|
|
|
irc.reply("No matches in tracker")
|
2008-08-04 10:11:15 +00:00
|
|
|
for logs in ret:
|
|
|
|
log = logs[0]
|
|
|
|
id = logs[1]
|
|
|
|
lines = ["%s: %s" % (log[0][1], i) for i in log[0][0].split('\n') if "<%s>" % target.lower() in i.lower() and i[21:21+len(target)].lower() == target.lower()]
|
|
|
|
show_sep = False
|
2008-07-22 01:40:39 +00:00
|
|
|
if not lines:
|
2008-08-04 10:11:15 +00:00
|
|
|
show_sep = False
|
|
|
|
irc.error("No log for ID %s available" % id)
|
|
|
|
else:
|
|
|
|
for l in lines[:5]:
|
|
|
|
if l not in sent:
|
|
|
|
show_sep = True
|
|
|
|
irc.reply(l)
|
|
|
|
sent.append(l)
|
|
|
|
if show_sep:
|
|
|
|
irc.reply('--')
|
2008-07-22 01:40:39 +00:00
|
|
|
|
|
|
|
banlog = wrap(banlog, ['something', optional('anything', default=None)])
|
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def updatebt(self, irc, msg, args, channel):
|
|
|
|
"""[<channel>]
|
2009-01-19 22:10:51 +00:00
|
|
|
|
|
|
|
Update bans in the tracker from the channel ban list,
|
2008-08-04 10:11:15 +00:00
|
|
|
if channel is not given then run in all channels
|
|
|
|
"""
|
|
|
|
|
|
|
|
def getBans(chan):
|
|
|
|
data = self.db_run("SELECT mask, removal FROM bans WHERE channel=%s", chan, expect_result=True)
|
2010-04-04 00:11:22 +00:00
|
|
|
L = []
|
|
|
|
for mask, removal in data:
|
|
|
|
if removal is not None:
|
|
|
|
continue
|
|
|
|
elif not isUserHostmask(mask) and mask[0] != '$':
|
|
|
|
continue
|
|
|
|
L.append(mask)
|
|
|
|
return L
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
def remBans(chan):
|
|
|
|
bans = getBans(chan)
|
|
|
|
old_bans = bans[:]
|
|
|
|
new_bans = [i.mask for i in self.bans[chan]]
|
|
|
|
remove_bans = []
|
|
|
|
for ban in old_bans:
|
|
|
|
if ban not in new_bans:
|
|
|
|
remove_bans.append(ban)
|
|
|
|
bans.remove(ban)
|
|
|
|
|
|
|
|
for ban in remove_bans:
|
2010-05-24 13:38:06 +00:00
|
|
|
self.log.info("Bantracker: Removing ban %s from %s" % (ban.replace('%', '%%'), chan))
|
2008-08-04 10:11:15 +00:00
|
|
|
self.doUnban(irc, channel, "Automated-Removal", ban)
|
2009-10-12 18:26:35 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
return len(remove_bans)
|
|
|
|
|
2009-01-19 22:10:51 +00:00
|
|
|
def addBans(chan):
|
|
|
|
bans = self.bans[chan]
|
|
|
|
old_bans = getBans(chan)
|
|
|
|
add_bans = []
|
|
|
|
for ban in bans:
|
|
|
|
if ban.mask not in old_bans and ban not in add_bans:
|
|
|
|
add_bans.append(ban)
|
|
|
|
|
|
|
|
for ban in add_bans:
|
|
|
|
nick = ban.who
|
|
|
|
if nick.endswith('.freenode.net'):
|
|
|
|
nick = "Automated-Addition"
|
2010-05-24 13:38:06 +00:00
|
|
|
self.log.info("Bantracker: Adding ban %s to %s (%s)" % (str(ban).replace('%', '%%'), chan, nick))
|
2009-01-19 22:10:51 +00:00
|
|
|
self.doLog(irc, channel.lower(), '*** Ban sync from channel: %s\n' % str(ban).replace('%', '%%'))
|
2011-06-18 19:27:51 +00:00
|
|
|
self._doKickban(irc, chan, nick, ban.mask, use_time = ban.when, add_to_cache = False)
|
2009-01-19 22:10:51 +00:00
|
|
|
return len(add_bans)
|
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
if not self.check_auth(irc, msg, args, 'owner'):
|
|
|
|
return
|
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
add_res = 0
|
|
|
|
rem_res = 0
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
if len(queue.msgcache) > 0:
|
|
|
|
irc.reply("Error: still syncing (%i)" % len(queue.msgcache))
|
|
|
|
return
|
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
try:
|
|
|
|
if channel:
|
|
|
|
rem_res += remBans(channel)
|
|
|
|
add_res += addBans(channel)
|
|
|
|
else:
|
|
|
|
for channel in irc.state.channels.keys():
|
|
|
|
if channel not in self.bans:
|
|
|
|
self.bans[channel] = []
|
|
|
|
rem_res += remBans(channel)
|
|
|
|
add_res += addBans(channel)
|
|
|
|
except KeyError, e:
|
|
|
|
irc.error("%s, Please wait longer" % e)
|
|
|
|
return
|
2008-08-04 10:11:15 +00:00
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
irc.reply("Cleared %i obsolete bans, Added %i new bans" % (rem_res, add_res))
|
2008-08-04 10:11:15 +00:00
|
|
|
|
|
|
|
updatebt = wrap(updatebt, [optional('anything', default=None)])
|
|
|
|
|
2012-07-27 03:50:25 +00:00
|
|
|
def _getBan(self, id):
|
|
|
|
"""gets mask, channel and removal date of ban"""
|
|
|
|
L = self.db_run("SELECT mask, channel, removal FROM bans WHERE id = %s",
|
|
|
|
id, expect_result=True)
|
|
|
|
if not L:
|
|
|
|
raise ValueError
|
|
|
|
return L[0]
|
|
|
|
|
|
|
|
def _setBanDuration(self, id, duration):
|
2012-08-03 14:46:17 +00:00
|
|
|
"""Set ban for remove after <duration> time, if <duration> is negative
|
|
|
|
or zero, never remove the ban.
|
|
|
|
"""
|
2012-07-27 03:50:25 +00:00
|
|
|
# check if ban has already a duration time
|
|
|
|
for idx, br in enumerate(self.managedBans):
|
|
|
|
if id == br.ban.id:
|
|
|
|
ban = br.ban
|
|
|
|
del self.managedBans.shelf[idx]
|
|
|
|
break
|
|
|
|
else:
|
2012-08-03 14:46:17 +00:00
|
|
|
if duration < 1:
|
|
|
|
# nothing to do.
|
|
|
|
raise Exception("ban isn't marked for removal")
|
|
|
|
|
2012-07-27 03:50:25 +00:00
|
|
|
# ban obj ins't in self.managedBans
|
|
|
|
try:
|
|
|
|
mask, channel, removal = self._getBan(id)
|
|
|
|
except ValueError:
|
|
|
|
raise Exception("unknow id")
|
|
|
|
|
|
|
|
type = guessBanType(mask)
|
|
|
|
if type not in ('ban', 'quiet'):
|
|
|
|
raise Exception("not a ban or quiet")
|
|
|
|
|
|
|
|
if removal:
|
|
|
|
raise Exception("ban was removed")
|
|
|
|
|
|
|
|
for ban in self.bans[channel]:
|
|
|
|
if mask == ban.mask:
|
|
|
|
if ban.id is None:
|
|
|
|
ban.id = id
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# ban not in sync it seems, shouldn't happen normally.
|
|
|
|
raise Exception("bans not in sync")
|
|
|
|
|
2012-08-03 14:46:17 +00:00
|
|
|
# add ban duration if is positive and non-zero
|
|
|
|
if duration > 0:
|
|
|
|
self.managedBans.add(BanRemoval(ban, duration))
|
2012-07-27 03:50:25 +00:00
|
|
|
|
|
|
|
def comment(self, irc, msg, args, ids, kickmsg):
|
|
|
|
"""<id>[,<id> ...] [<comment>][, <duration>]
|
2009-01-19 22:10:51 +00:00
|
|
|
|
2012-07-30 02:36:18 +00:00
|
|
|
Reads or adds the <comment> for the ban with <id>, use @bansearch to
|
|
|
|
find the id of a ban. Using <duration> will set the duration of the ban.
|
2009-01-19 22:10:51 +00:00
|
|
|
"""
|
2012-07-27 03:50:25 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def addComment(id, nick, msg):
|
|
|
|
n = now()
|
|
|
|
self.db_run("INSERT INTO comments (ban_id, who, comment, time) values(%s,%s,%s,%s)", (id, nick, msg, n))
|
2012-07-28 00:44:29 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def readComment(id):
|
|
|
|
return self.db_run("SELECT who, comment, time FROM comments WHERE ban_id=%i", (id,), True)
|
|
|
|
|
|
|
|
nick = msg.nick
|
2012-07-27 03:50:25 +00:00
|
|
|
duration, banset = None, []
|
|
|
|
if kickmsg and ',' in kickmsg:
|
2012-07-28 02:12:35 +00:00
|
|
|
s = kickmsg[kickmsg.rfind(',') + 1:]
|
2012-07-27 03:50:25 +00:00
|
|
|
try:
|
|
|
|
duration = readTimeDelta(s)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2012-07-27 22:21:21 +00:00
|
|
|
for id in splitID(ids):
|
2012-07-27 03:50:25 +00:00
|
|
|
try:
|
2012-07-30 02:36:18 +00:00
|
|
|
self._getBan(id)
|
2012-07-27 03:50:25 +00:00
|
|
|
except ValueError:
|
|
|
|
irc.reply("I don't know any ban with id %s." % id)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if kickmsg:
|
|
|
|
addComment(id, nick, kickmsg)
|
|
|
|
if duration is not None:
|
|
|
|
# set duration time
|
|
|
|
try:
|
|
|
|
self._setBanDuration(id, duration)
|
|
|
|
banset.append(str(id))
|
|
|
|
except Exception as exc:
|
2012-07-30 02:36:18 +00:00
|
|
|
irc.reply("Failed to set duration time on %s (%s)" % (id, exc))
|
2012-07-27 03:50:25 +00:00
|
|
|
else:
|
|
|
|
data = readComment(id)
|
|
|
|
if data:
|
|
|
|
for c in data:
|
|
|
|
date = cPickle.loads(c[2]).astimezone(pytz.timezone('UTC')).strftime("%b %d %Y %H:%M")
|
|
|
|
irc.reply("%s %s: %s" % (date, c[0], c[1].strip()))
|
|
|
|
else:
|
|
|
|
irc.reply("No comments recorded for ban %s" % id)
|
|
|
|
|
2012-07-30 02:36:18 +00:00
|
|
|
# success reply. If duration time used, say which ones were set.
|
2008-08-04 10:11:15 +00:00
|
|
|
if kickmsg:
|
2012-07-28 00:44:29 +00:00
|
|
|
if banset:
|
2012-08-03 14:46:17 +00:00
|
|
|
if duration < 1:
|
|
|
|
irc.reply(Format("Comment added. %L won't expire.", banset))
|
|
|
|
return
|
|
|
|
|
2012-07-30 02:36:18 +00:00
|
|
|
try:
|
|
|
|
time = 'after ' + timeElapsed(duration)
|
|
|
|
except ValueError:
|
|
|
|
time = 'soon'
|
2012-08-03 14:46:17 +00:00
|
|
|
irc.reply(Format("Comment added. %L will be removed %s.",
|
|
|
|
banset, time))
|
2008-08-04 10:11:15 +00:00
|
|
|
else:
|
2012-07-30 02:36:18 +00:00
|
|
|
# only a comment
|
|
|
|
irc.reply("Comment added.")
|
2012-07-27 03:50:25 +00:00
|
|
|
|
|
|
|
comment = wrap(comment, ['something', optional('text')])
|
2008-08-04 10:11:15 +00:00
|
|
|
|
2012-07-27 22:21:21 +00:00
|
|
|
def duration(self, irc, msg, args, ids, duration):
|
|
|
|
"""[<id>[,<id> ...]] [<duration>]
|
2012-07-13 16:45:37 +00:00
|
|
|
|
2012-07-30 02:36:18 +00:00
|
|
|
Sets the duration of a ban. If <duration> isn't given show when a ban expires. f no <id> is given shows the ids of bans set to expire.
|
2012-07-13 16:45:37 +00:00
|
|
|
"""
|
2012-07-27 22:21:21 +00:00
|
|
|
if ids is None:
|
2012-07-27 19:03:15 +00:00
|
|
|
count = len(self.managedBans)
|
|
|
|
L = [ str(item.ban.id) for item in self.managedBans ]
|
2012-08-03 14:46:17 +00:00
|
|
|
irc.reply(Format("%n set to expire: %L", (count, 'ban'), L))
|
2012-07-27 19:03:15 +00:00
|
|
|
return
|
|
|
|
|
2012-07-27 22:21:21 +00:00
|
|
|
if duration is not None:
|
2012-07-16 00:15:06 +00:00
|
|
|
try:
|
2012-07-27 22:21:21 +00:00
|
|
|
duration = readTimeDelta(duration)
|
2012-07-16 00:15:06 +00:00
|
|
|
except ValueError:
|
2012-07-27 22:21:21 +00:00
|
|
|
irc.error("bad time format.")
|
|
|
|
return
|
|
|
|
|
|
|
|
banset = []
|
|
|
|
for id in splitID(ids):
|
|
|
|
if duration is not None:
|
|
|
|
# set ban duration
|
|
|
|
try:
|
|
|
|
self._setBanDuration(id, duration)
|
|
|
|
banset.append(str(id))
|
|
|
|
except Exception as exc:
|
2012-07-28 00:44:29 +00:00
|
|
|
irc.reply("Failed to set duration time on %s (%s)" \
|
2012-07-27 22:21:21 +00:00
|
|
|
% (id, exc))
|
|
|
|
else:
|
|
|
|
# get ban information
|
|
|
|
try:
|
|
|
|
mask, channel, removal = self._getBan(id)
|
|
|
|
except ValueError:
|
|
|
|
irc.reply("I don't know any ban with id %s." % id)
|
|
|
|
continue
|
|
|
|
|
|
|
|
type = guessBanType(mask)
|
|
|
|
if type == 'quiet':
|
|
|
|
mask = mask[1:]
|
|
|
|
for br in self.managedBans:
|
|
|
|
if br.ban.id == id:
|
|
|
|
break
|
2012-07-16 00:15:06 +00:00
|
|
|
else:
|
2012-07-27 22:21:21 +00:00
|
|
|
br = None
|
2012-07-16 00:06:23 +00:00
|
|
|
|
2012-07-27 22:21:21 +00:00
|
|
|
expires = None
|
|
|
|
if br:
|
|
|
|
expires = (br.ban.when + br.expires) - nowSeconds()
|
2012-07-30 02:36:18 +00:00
|
|
|
if expires > 0:
|
|
|
|
try:
|
|
|
|
expires = "expires in %s" % timeElapsed(expires)
|
|
|
|
except ValueError:
|
|
|
|
expires = "expires soon"
|
|
|
|
else:
|
|
|
|
expires = "expired and will be removed soon"
|
2012-07-27 22:21:21 +00:00
|
|
|
else:
|
|
|
|
if type in ('quiet', 'ban'):
|
|
|
|
if not removal:
|
|
|
|
expires = "never expires"
|
|
|
|
else:
|
|
|
|
expires = "not active"
|
|
|
|
|
|
|
|
if expires:
|
|
|
|
irc.reply("[%s] %s - %s - %s - %s" % (id, type, mask, channel, expires))
|
|
|
|
else:
|
|
|
|
irc.reply("[%s] %s - %s - %s" % (id, type, mask, channel))
|
2012-07-13 16:45:37 +00:00
|
|
|
|
2012-07-27 22:21:21 +00:00
|
|
|
# reply with the bans ids that were correctly set.
|
|
|
|
if banset:
|
2012-08-03 14:46:17 +00:00
|
|
|
if duration < 1:
|
|
|
|
irc.reply(Format("%L won't expire.", banset))
|
|
|
|
return
|
|
|
|
|
2012-07-30 02:36:18 +00:00
|
|
|
try:
|
|
|
|
time = 'after ' + timeElapsed(duration)
|
|
|
|
except ValueError:
|
|
|
|
time = 'soon'
|
2012-08-03 14:46:17 +00:00
|
|
|
irc.reply(Format("%L will be removed %s.", banset, time))
|
2012-07-13 16:45:37 +00:00
|
|
|
|
2012-07-27 22:21:21 +00:00
|
|
|
duration = wrap(duration, [optional('something'), optional('text')])
|
2012-07-13 16:45:37 +00:00
|
|
|
|
2008-08-04 10:11:15 +00:00
|
|
|
def banlink(self, irc, msg, args, id, highlight):
|
|
|
|
"""<id> [<highlight>]
|
2009-01-19 22:10:51 +00:00
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
Returns a link to the log of the ban/kick with id <id>.
|
|
|
|
If <highlight> is given, lines containing that term will be highlighted
|
2008-08-04 10:11:15 +00:00
|
|
|
"""
|
|
|
|
if not self.check_auth(irc, msg, args):
|
|
|
|
return
|
|
|
|
if not highlight:
|
|
|
|
irc.reply("%s/bans.cgi?log=%s" % (self.registryValue('bansite'), id), private=True)
|
|
|
|
else:
|
|
|
|
irc.reply("%s/bans.cgi?log=%s&mark=%s" % (self.registryValue('bansite'), id, highlight), private=True)
|
|
|
|
banlink = wrap(banlink, ['id', optional('somethingWithoutSpaces')])
|
|
|
|
|
2011-05-29 22:21:27 +00:00
|
|
|
def banreview(self, irc, msg, args, optlist):
|
|
|
|
"""[--verbose | --flush <nick@host> | --view <nick@host>]
|
2010-04-04 04:31:15 +00:00
|
|
|
Lists pending ban reviews."""
|
|
|
|
if not self.check_auth(irc, msg, args):
|
|
|
|
return
|
2011-05-29 22:21:27 +00:00
|
|
|
verbose = False
|
|
|
|
flush = view = None
|
|
|
|
for k, v in optlist:
|
|
|
|
if k == 'verbose':
|
|
|
|
verbose = True
|
|
|
|
elif k == 'flush':
|
|
|
|
flush = v
|
|
|
|
elif k == 'view':
|
|
|
|
view = v
|
|
|
|
|
|
|
|
key = view or flush
|
|
|
|
if key:
|
|
|
|
if '@' in key:
|
|
|
|
nick, host = key.split('@', 1)
|
|
|
|
else:
|
|
|
|
nick, host = key, None
|
2012-06-25 22:30:11 +00:00
|
|
|
if host in self.pendingReviews:
|
2011-05-29 22:21:27 +00:00
|
|
|
reviews = self.pendingReviews[host]
|
2012-06-25 22:30:11 +00:00
|
|
|
else:
|
2011-05-29 22:21:27 +00:00
|
|
|
irc.reply('No reviews for %s, use --verbose for check the correct nick@host key.' % key)
|
|
|
|
return
|
|
|
|
|
|
|
|
L = []
|
|
|
|
for _nick, msg in reviews:
|
|
|
|
if nick == _nick:
|
|
|
|
irc.reply(msg.args[1])
|
|
|
|
elif flush:
|
|
|
|
L.append((_nick, msg))
|
|
|
|
if flush:
|
|
|
|
if L:
|
|
|
|
self.pendingReviews[host] = L
|
|
|
|
else:
|
|
|
|
del self.pendingReviews[host]
|
|
|
|
return
|
|
|
|
|
2010-04-03 16:20:15 +00:00
|
|
|
count = {}
|
2011-05-29 22:21:27 +00:00
|
|
|
for host, reviews in self.pendingReviews.iteritems():
|
2010-04-03 16:20:15 +00:00
|
|
|
for nick, msg in reviews:
|
2011-05-29 22:21:27 +00:00
|
|
|
if verbose and host: # host can be None for those "nick only" reviews.
|
|
|
|
key = '%s@%s' % (nick, host)
|
|
|
|
else:
|
|
|
|
key = nick
|
2010-04-03 16:20:15 +00:00
|
|
|
try:
|
2011-05-29 22:21:27 +00:00
|
|
|
count[key] += 1
|
2010-04-03 16:20:15 +00:00
|
|
|
except KeyError:
|
2011-05-29 22:21:27 +00:00
|
|
|
count[key] = 1
|
2010-04-03 16:20:15 +00:00
|
|
|
total = sum(count.itervalues())
|
|
|
|
s = ' '.join([ '%s:%s' %pair for pair in count.iteritems() ])
|
2010-04-02 17:03:44 +00:00
|
|
|
s = 'Pending ban reviews (%s): %s' %(total, s)
|
|
|
|
irc.reply(s)
|
|
|
|
|
2011-05-29 22:21:27 +00:00
|
|
|
banreview = wrap(banreview, [getopts({'verbose':'',
|
|
|
|
'flush': 'something',
|
|
|
|
'view': 'something'})])
|
2010-04-02 17:03:44 +00:00
|
|
|
|
2006-06-26 17:57:20 +00:00
|
|
|
Class = Bantracker
|