Add flood protection to PackageInfo (LP: #561908)

update copyright stuff.
This commit is contained in:
Terence Simpson
2010-04-13 02:12:54 +01:00
parent 62201d9a12
commit f757b715bd
6 changed files with 36 additions and 37 deletions

View File

@ -1,5 +1,5 @@
###
# Copyright (c) 2008, Terence Simpson <tsimpson@ubuntu.com>
# Copyright (c) 2008-2010, Terence Simpson <tsimpson@ubuntu.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -35,10 +35,10 @@ Display information on packages using apt-cache and search for files in packages
import supybot
import supybot.world as world
__version__ = "0.9.0"
__version__ = "0.9.1"
__author__ = supybot.Author("Terence Simpson", "tsimpson", "tsimpson@ubuntu.com")
__contributors__ = {supybot.Author("Dennis Kaarsemaker","Seveas","dennis@kaarsemaker.net"): ["Origional code"]}
__url__ = 'http://ubottu.com'
__url__ = 'https://launchpad.net/ubuntu-bots'
import config
reload(config)

View File

@ -1,5 +1,5 @@
###
# Copyright (c) 2008, Terence Simpson
# Copyright (c) 2008-2010, Terence Simpson <tsimpson@ubuntu.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without

View File

@ -1,5 +1,6 @@
###
# Copyright (c) 2006-2007 Dennis Kaarsemaker
# Copyright (c) 2008-2010 Terence Simpson <tsimpson@ubuntu.com>
#
# 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
@ -25,15 +26,11 @@ class Apt:
def __init__(self, plugin):
self.aptdir = plugin.registryValue('aptdir')
self.distros = []
# self.urls = {}
self.plugin = plugin
self.log = plugin.log
os.environ["LANG"] = "C"
if self.aptdir:
self.distros = [x[:-5] for x in os.listdir(self.aptdir) if x.endswith('.list')]
# urls = [x for x in os.listdir(self.aptdir) if x.endswith(".url")]
# for urlf in urls:
# self.readIrl(urlf)
self.distros.sort()
self.aptcommand = """apt-cache\\
-o"Dir::State::Lists=%s/%%s"\\
@ -54,11 +51,9 @@ class Apt:
pkg = _pkg
data = commands.getoutput(self.aptcommand % (distro, distro, distro, 'search -n', pkg))
#self.log.info("command output: %r" % data)
if not data:
if filelookup:
data = commands.getoutput(self.aptfilecommand % (distro, distro, pkg)).split()
#self.log.info("command output: %r" % ' '.join(data))
if data:
if data[0] == 'sh:': # apt-file isn't installed
self.log.error("apt-file is not installed")
@ -143,24 +138,6 @@ class Apt:
maxp['Priority'], maxp['Version'], distro, int(maxp['Size'])/1024, maxp['Installed-Size'], archs))
return 'Package %s does not exist in %s' % (pkg, checkdists)
@staticmethod
def readUrl(urlfile):
distro = os.path.splitext(urlfile)[0]
url = None
try:
assert distro in self.distros, '%s is not a valid distrobution (no .list file)' % distro
f = open(os.path.join(self.aptdir, urlfile))
lines = [i.strip() for i in f.readlines() if i.strip()]
assert len(lines) == 1, 'Expected 1 line in "%s", read %d' % (urlfile, len(lines))
self.urls[distro] = lines[0]
except Exception, e:
self.plugin.log.warning("%s (%s)" % (e.__class__, e))
def getUrl(self, maxp):
if not maxp["Distrobution"] in self.urls:
return ""
return " - see %s" % (self.urls[maxp["Distrobution"]] % maxp)
# Simple test
if __name__ == "__main__":
import sys
@ -179,7 +156,7 @@ if __name__ == "__main__":
def __init__(self):
self.log = self.FakeLog()
def registryValue(self, *args, **kwargs):
return "/home/jussi/bot/aptdir"
return "/home/bot/aptdir"
command = argv[1].split(None, 1)[0]
try:

View File

@ -1,5 +1,5 @@
###
# Copyright (c) 2008, Terence Simpson
# Copyright (c) 2008-2010, Terence Simpson <tsimpson@ubuntu.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -49,6 +49,25 @@ def get_user(msg):
return False
return user
## Taken from Encyclopedia ##
# Repeat filtering message queue
msgcache = {}
def queue(irc, to, msg):
now = time.time()
for m in msgcache.keys():
if msgcache[m] < now - 30:
msgcache.pop(m)
for m in msgcache:
if m[0] == irc and m[1] == to:
oldmsg = m[2]
if msg == oldmsg or oldmsg.endswith(msg):
break
if msg.endswith(oldmsg):
msg = msg[:-len(oldmsg)] + 'please see above'
else:
msgcache[(irc, to, msg)] = now
irc.queueMsg(ircmsgs.privmsg(to, msg))
class PackageInfo(callbacks.Plugin):
"""Lookup package information via apt-cache/apt-file"""
threaded = True
@ -98,7 +117,7 @@ class PackageInfo(callbacks.Plugin):
target = rest.split()[1]
if target.lower() == "me":
target = msg.nick
irc.reply("%s: %s" % (target, reply))
queue(irc, msg.args[0], "%s: %s" % (target, reply))
return
except Exception, e:
self.log.info("Info: Exception in pipe: %r" % e)
@ -108,13 +127,13 @@ class PackageInfo(callbacks.Plugin):
target = rest.split()[1]
if target.lower() == "me":
target = msg.nick
irc.queueMsg(ircmsgs.privmsg(target, "<%s> wants you to know: %s" % (msg.nick, reply)))
queue(irc, target, "<%s> wants you to know: %s" % (msg.nick, reply))
return
except Exception, e:
self.log.info("Info: Exception in redirect: %r" % e)
pass
irc.reply(reply)
queue(irc, args[0], reply)
info = wrap(real_info, ['anything', optional('text')])
@ -135,7 +154,7 @@ class PackageInfo(callbacks.Plugin):
target = rest.split()[1]
if target.lower() == "me":
target = msg.nick
irc.reply("%s: %s" % (target, reply))
queue(irc, msg.args[0], "%s: %s" % (target, reply))
return
except Exception, e:
self.log.info("Find: Exception in pipe: %r" % e)
@ -145,13 +164,13 @@ class PackageInfo(callbacks.Plugin):
target = rest.split()[1]
if target.lower() == "me":
target = msg.nick
irc.queueMsg(ircmsgs.privmsg(target, "<%s> wants you to know: %s" % (msg.nick, reply)))
queue(irc, target, "<%s> wants you to know: %s" % (msg.nick, reply))
return
except Exception, e:
self.log.info("Find: Exception in redirect: %r" % e)
pass
irc.reply(reply)
queue(irc, msg.args[0], reply)
find = wrap(real_find, ['anything', optional('text')])

View File

@ -1,5 +1,5 @@
###
# Copyright (c) 2008, Terence Simpson
# Copyright (c) 2008-2010, Terence Simpson <tsimpson@ubuntu.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without

View File

@ -7,6 +7,9 @@ DEFAULT_OPTS="-qq"
while [ "x$1" != "x" ]; do
case "$1" in
-v|--verbose)
DEFAULT_OPTS="-q"
;;
-V|--very-verbose)
DEFAULT_OPTS=""
;;
-d|--dir)