3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 09:19:23 +01:00

automode: separate matching into a separate function & listen for services login changes

This commit is contained in:
James Lu 2016-07-07 21:31:36 -07:00
parent 552070132d
commit 2be4811673

View File

@ -182,34 +182,52 @@ def save(irc, source, args):
reply(irc, 'Done.')
modebot.add_cmd(save)
def match(irc, channel, uid):
"""
Automode matcher engine.
"""
dbentry = db.get(irc.name+channel)
if dbentry is None:
return
modebot_uid = modebot.uids.get(irc.name)
# Check every mask defined in the channel ACL.
for mask, modes in dbentry.items():
if irc.matchHost(mask, uid):
# User matched a mask. Filter the mode list given to only those that are valid
# prefix mode characters.
outgoing_modes = [('+'+mode, uid) for mode in modes if mode in irc.prefixmodes]
log.debug("(%s) automode: Filtered mode list of %s to %s (protocol:%s)",
irc.name, modes, outgoing_modes, irc.protoname)
# If the Automode bot is missing, send the mode through the PyLink server.
if not modebot_uid:
modebot_uid = irc.sid
irc.proto.mode(modebot_uid, channel, outgoing_modes)
# Create a hook payload to support plugins like relay.
irc.callHooks([modebot_uid, 'AUTOMODE_MODE',
{'target': channel, 'modes': outgoing_modes, 'parse_as': 'MODE'}])
def handle_join(irc, source, command, args):
"""
Automode JOIN listener. This sets modes accordingly if the person joining matches a mask in the
ACL.
"""
channel = irc.toLower(args['channel'])
dbentry = db.get(irc.name+channel)
modebot_uid = modebot.uids.get(irc.name)
if dbentry:
# Database entry exists for the channel given. Iterate over all the masks we have and the
# joining UIDs:
for mask, modes in dbentry.items():
for uid in args['users']:
if irc.matchHost(mask, uid):
# User matched a mask. Filter the mode list given to only those that are valid
# prefix mode characters.
outgoing_modes = [('+'+mode, uid) for mode in modes if mode in irc.prefixmodes]
log.debug("(%s) automode: Filtered mode list of %s to %s (protocol:%s)",
irc.name, modes, outgoing_modes, irc.protoname)
# If the Automode bot is missing, send the mode through the PyLink server.
if not modebot_uid:
modebot_uid = irc.sid
irc.proto.mode(modebot_uid, channel, outgoing_modes)
# Create a hook payload to support plugins like relay.
irc.callHooks([modebot_uid, 'AUTOMODE_MODE',
{'target': channel, 'modes': outgoing_modes, 'parse_as': 'MODE'}])
# Iterate over all the joining UIDs:
for uid in args['users']:
match(irc, channel, uid)
utils.add_hook(handle_join, 'JOIN')
def handle_services_login(irc, source, command, args):
"""
Handles services login change, to trigger Automode matching."""
for channel in irc.users[source].channels:
# Look at all the users' channels for any possible changes.
match(irc, channel, source)
utils.add_hook(handle_services_login, 'CLIENT_SERVICES_LOGIN')