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

utils.parseModes: be a bit more fault-tolerant

- Log "mode requires an argument but none was found" errors to warning, and skip the mode instead of failing completely.
- Assign a fallback value for 'prefix', checking to make sure it exists and raising ValueError (not UnboundLocalError) when it doesn't.
This commit is contained in:
James Lu 2015-07-08 22:42:04 -07:00
parent 20772e5429
commit 35f8a0e25f

View File

@ -94,6 +94,7 @@ def parseModes(irc, target, args):
# C = Mode that changes a setting and only has a parameter when set. # C = Mode that changes a setting and only has a parameter when set.
# D = Mode that changes a setting and never has a parameter. # D = Mode that changes a setting and never has a parameter.
usermodes = not isChannel(target) usermodes = not isChannel(target)
prefix = ''
modestring = args[0] modestring = args[0]
if not modestring: if not modestring:
return ValueError('No modes supplied in parseModes query: %r' % modes) return ValueError('No modes supplied in parseModes query: %r' % modes)
@ -109,20 +110,28 @@ def parseModes(irc, target, args):
if mode in '+-': if mode in '+-':
prefix = mode prefix = mode
else: else:
if not prefix:
raise ValueError('Invalid query %r: mode char given without preceding prefix.' % modestring)
arg = None arg = None
log.debug('Current mode: %s%s; args left: %s', prefix, mode, args) log.debug('Current mode: %s%s; args left: %s', prefix, mode, args)
if mode in (supported_modes['*A'] + supported_modes['*B']): try:
# Must have parameter. if mode in (supported_modes['*A'] + supported_modes['*B']):
log.debug('Mode %s: This mode must have parameter.', mode) # Must have parameter.
arg = args.pop(0) log.debug('Mode %s: This mode must have parameter.', mode)
elif mode in irc.prefixmodes and not usermodes: arg = args.pop(0)
# We're setting a prefix mode on someone (e.g. +o user1) elif mode in irc.prefixmodes and not usermodes:
log.debug('Mode %s: This mode is a prefix mode.', mode) # We're setting a prefix mode on someone (e.g. +o user1)
arg = args.pop(0) log.debug('Mode %s: This mode is a prefix mode.', mode)
elif prefix == '+' and mode in supported_modes['*C']: arg = args.pop(0)
# Only has parameter when setting. elif prefix == '+' and mode in supported_modes['*C']:
log.debug('Mode %s: Only has parameter when setting.', mode) # Only has parameter when setting.
arg = args.pop(0) log.debug('Mode %s: Only has parameter when setting.', mode)
arg = args.pop(0)
except IndexError:
log.warning('(%s/%s) Error while parsing mode %r: mode requires an '
'argument but none was found. (modestring: %r)',
irc.name, target, mode, modestring)
continue # Skip this mode; don't error out completely.
res.append((prefix + mode, arg)) res.append((prefix + mode, arg))
return res return res