3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-02-03 08:04:07 +01:00

matchHost: extend negation via "!" to regular hostmasks as well as exttargets

This commit is contained in:
James Lu 2017-05-04 19:04:03 -07:00
parent d51c399351
commit 5d629f7331

View File

@ -1162,14 +1162,24 @@ class Irc(utils.DeprecatedAttributesObject):
# Try to convert target into a UID. If this fails, it's probably a hostname. # Try to convert target into a UID. If this fails, it's probably a hostname.
target = self.nickToUid(target) or target target = self.nickToUid(target) or target
# Allow queries like !$exttarget to invert the given match.
invert = glob.startswith('!')
if invert:
glob = glob.lstrip('!')
def match_host_core():
"""
Core processor for matchHost(), minus the inversion check.
"""
# Work with variables in the matchHost() scope, from
# http://stackoverflow.com/a/8178808
nonlocal glob
# Prepare a list of hosts to check against. # Prepare a list of hosts to check against.
if target in self.users: if target in self.users:
if glob.startswith(('$', '!$')): if glob.startswith('$'):
# !$exttarget inverts the given match.
invert = glob.startswith('!$')
# Exttargets start with $. Skip regular ban matching and find the matching ban handler. # Exttargets start with $. Skip regular ban matching and find the matching ban handler.
glob = glob.lstrip('$!') glob = glob.lstrip('$')
exttargetname = glob.split(':', 1)[0] exttargetname = glob.split(':', 1)[0]
handler = world.exttarget_handlers.get(exttargetname) handler = world.exttarget_handlers.get(exttargetname)
@ -1178,8 +1188,6 @@ class Irc(utils.DeprecatedAttributesObject):
result = handler(self, glob, target) result = handler(self, glob, target)
log.debug('(%s) Got %s from exttarget %s in matchHost() glob $%s for target %s', log.debug('(%s) Got %s from exttarget %s in matchHost() glob $%s for target %s',
self.name, result, exttargetname, glob, target) self.name, result, exttargetname, glob, target)
if invert: # Anti-exttarget was specified.
result = not result
return result return result
else: else:
log.debug('(%s) Unknown exttarget %s in matchHost() glob $%s', self.name, log.debug('(%s) Unknown exttarget %s in matchHost() glob $%s', self.name,
@ -1222,6 +1230,11 @@ class Irc(utils.DeprecatedAttributesObject):
return False return False
result = match_host_core()
if invert:
result = not result
return result
class IrcUser(): class IrcUser():
"""PyLink IRC user class.""" """PyLink IRC user class."""
def __init__(self, nick, ts, uid, server, ident='null', host='null', def __init__(self, nick, ts, uid, server, ident='null', host='null',