2015-04-25 07:37:07 +02:00
|
|
|
import string
|
2015-06-17 05:46:01 +02:00
|
|
|
import re
|
2015-06-24 04:08:43 +02:00
|
|
|
from collections import defaultdict
|
2015-04-25 07:37:07 +02:00
|
|
|
|
2015-07-05 22:44:48 +02:00
|
|
|
from log import log
|
|
|
|
|
2015-06-24 04:08:43 +02:00
|
|
|
global bot_commands, command_hooks
|
2015-05-31 21:20:09 +02:00
|
|
|
# This should be a mapping of command names to functions
|
|
|
|
bot_commands = {}
|
2015-06-24 04:08:43 +02:00
|
|
|
command_hooks = defaultdict(list)
|
2015-07-10 03:29:00 +02:00
|
|
|
networkobjects = {}
|
2015-07-12 22:09:35 +02:00
|
|
|
schedulers = {}
|
2015-07-13 08:28:34 +02:00
|
|
|
plugins = []
|
2015-05-31 21:20:09 +02:00
|
|
|
|
2015-06-19 19:43:42 +02:00
|
|
|
class TS6UIDGenerator():
|
|
|
|
"""TS6 UID Generator module, adapted from InspIRCd source
|
|
|
|
https://github.com/inspircd/inspircd/blob/f449c6b296ab/src/server.cpp#L85-L156
|
|
|
|
"""
|
|
|
|
|
2015-06-23 01:51:42 +02:00
|
|
|
def __init__(self, sid):
|
2015-06-19 19:43:42 +02:00
|
|
|
# TS6 UIDs are 6 characters in length (9 including the SID).
|
2015-06-22 03:11:17 +02:00
|
|
|
# They wrap from ABCDEFGHIJKLMNOPQRSTUVWXYZ -> 0123456789 -> wrap around:
|
|
|
|
# (e.g. AAAAAA, AAAAAB ..., AAAAA8, AAAAA9, AAAABA)
|
2015-06-19 19:43:42 +02:00
|
|
|
self.allowedchars = string.ascii_uppercase + string.digits
|
2015-06-22 03:11:17 +02:00
|
|
|
self.uidchars = [self.allowedchars[0]]*6
|
2015-06-23 01:51:42 +02:00
|
|
|
self.sid = sid
|
2015-06-19 19:43:42 +02:00
|
|
|
|
|
|
|
def increment(self, pos=5):
|
|
|
|
# If we're at the last character in the list of allowed ones, reset
|
|
|
|
# and increment the next level above.
|
|
|
|
if self.uidchars[pos] == self.allowedchars[-1]:
|
|
|
|
self.uidchars[pos] = self.allowedchars[0]
|
|
|
|
self.increment(pos-1)
|
|
|
|
else:
|
|
|
|
# Find what position in the allowed characters list we're currently
|
|
|
|
# on, and add one.
|
|
|
|
idx = self.allowedchars.find(self.uidchars[pos])
|
|
|
|
self.uidchars[pos] = self.allowedchars[idx+1]
|
|
|
|
|
2015-06-23 01:51:42 +02:00
|
|
|
def next_uid(self):
|
|
|
|
uid = self.sid + ''.join(self.uidchars)
|
2015-06-19 19:43:42 +02:00
|
|
|
self.increment()
|
2015-06-22 03:11:17 +02:00
|
|
|
return uid
|
2015-05-31 08:00:39 +02:00
|
|
|
|
2015-07-12 04:56:26 +02:00
|
|
|
class TS6SIDGenerator():
|
|
|
|
"""<query>
|
|
|
|
|
|
|
|
TS6 SID Generator. <query> is a 3 character string with any combination of
|
|
|
|
uppercase letters, digits, and #'s. <query> must contain at least one #,
|
|
|
|
which are used by the generator as a wildcard. On every next_sid() call,
|
|
|
|
the first available wildcard character (from the right) will be
|
|
|
|
incremented to generate the next SID.
|
|
|
|
|
|
|
|
When there are no more available SIDs left (SIDs are not reused, only
|
|
|
|
incremented), RuntimeError is raised.
|
|
|
|
|
|
|
|
Example queries:
|
|
|
|
"1#A" would give: 10A, 11A, 12A ... 19A, 1AA, 1BA ... 1ZA (36 total results)
|
|
|
|
"#BQ" would give: 0BQ, 1BQ, 2BQ ... 9BQ (10 total results)
|
|
|
|
"6##" would give: 600, 601, 602, ... 60Y, 60Z, 610, 611, ... 6ZZ (1296 total results)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, query):
|
|
|
|
self.query = list(query)
|
|
|
|
self.iters = self.query.copy()
|
|
|
|
self.output = self.query.copy()
|
|
|
|
self.allowedchars = {}
|
|
|
|
qlen = len(query)
|
|
|
|
assert qlen == 3, 'Incorrect length for a SID (must be 3, got %s)' % qlen
|
|
|
|
assert '#' in query, "Must be at least one wildcard (#) in query"
|
|
|
|
for idx, char in enumerate(query):
|
|
|
|
assert char in (string.digits+string.ascii_uppercase+"#"), \
|
|
|
|
"Invalid character %r found." % char
|
|
|
|
if char == '#':
|
|
|
|
if idx == 0: # The first char be only digits
|
|
|
|
self.allowedchars[idx] = string.digits
|
|
|
|
else:
|
|
|
|
self.allowedchars[idx] = string.digits+string.ascii_uppercase
|
|
|
|
self.iters[idx] = iter(self.allowedchars[idx])
|
|
|
|
self.output[idx] = self.allowedchars[idx][0]
|
|
|
|
next(self.iters[idx])
|
|
|
|
|
|
|
|
|
|
|
|
def increment(self, pos=2):
|
|
|
|
if pos < 0:
|
|
|
|
# Oh no, we've wrapped back to the start!
|
|
|
|
raise RuntimeError('No more available SIDs!')
|
|
|
|
it = self.iters[pos]
|
|
|
|
try:
|
|
|
|
self.output[pos] = next(it)
|
|
|
|
except TypeError: # This position is not an iterator, but a string.
|
|
|
|
self.increment(pos-1)
|
|
|
|
except StopIteration:
|
|
|
|
self.output[pos] = self.allowedchars[pos][0]
|
|
|
|
self.iters[pos] = iter(self.allowedchars[pos])
|
|
|
|
next(self.iters[pos])
|
|
|
|
self.increment(pos-1)
|
|
|
|
else:
|
|
|
|
print('NEXT')
|
|
|
|
|
|
|
|
def next_sid(self):
|
|
|
|
sid = ''.join(self.output)
|
|
|
|
self.increment()
|
|
|
|
return sid
|
|
|
|
|
2015-05-31 21:20:09 +02:00
|
|
|
def msg(irc, target, text, notice=False):
|
2015-07-10 01:23:20 +02:00
|
|
|
if notice:
|
|
|
|
irc.proto.noticeClient(irc, irc.pseudoclient.uid, target, text)
|
|
|
|
else:
|
|
|
|
irc.proto.messageClient(irc, irc.pseudoclient.uid, target, text)
|
2015-05-31 21:20:09 +02:00
|
|
|
|
|
|
|
def add_cmd(func, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = func.__name__
|
|
|
|
name = name.lower()
|
|
|
|
bot_commands[name] = func
|
2015-06-08 04:31:56 +02:00
|
|
|
|
2015-06-24 04:08:43 +02:00
|
|
|
def add_hook(func, command):
|
|
|
|
"""Add a hook <func> for command <command>."""
|
2015-07-05 04:00:29 +02:00
|
|
|
command = command.upper()
|
2015-06-24 04:08:43 +02:00
|
|
|
command_hooks[command].append(func)
|
|
|
|
|
2015-06-08 04:36:21 +02:00
|
|
|
def nickToUid(irc, nick):
|
2015-06-08 04:31:56 +02:00
|
|
|
for k, v in irc.users.items():
|
|
|
|
if v.nick == nick:
|
|
|
|
return k
|
2015-06-17 05:46:01 +02:00
|
|
|
|
2015-06-22 00:00:33 +02:00
|
|
|
def clientToServer(irc, numeric):
|
|
|
|
"""<irc object> <numeric>
|
|
|
|
|
|
|
|
Finds the server SID of user <numeric> and returns it."""
|
|
|
|
for server in irc.servers:
|
|
|
|
if numeric in irc.servers[server].users:
|
|
|
|
return server
|
|
|
|
|
2015-06-17 05:46:01 +02:00
|
|
|
# A+ regex
|
|
|
|
_nickregex = r'^[A-Za-z\|\\_\[\]\{\}\^\`][A-Z0-9a-z\-\|\\_\[\]\{\}\^\`]*$'
|
|
|
|
def isNick(s, nicklen=None):
|
|
|
|
if nicklen and len(s) > nicklen:
|
|
|
|
return False
|
|
|
|
return bool(re.match(_nickregex, s))
|
|
|
|
|
|
|
|
def isChannel(s):
|
2015-06-22 00:00:33 +02:00
|
|
|
return s.startswith('#')
|
|
|
|
|
2015-07-04 02:05:44 +02:00
|
|
|
def _isASCII(s):
|
|
|
|
chars = string.ascii_letters + string.digits + string.punctuation
|
|
|
|
return all(char in chars for char in s)
|
2015-06-22 00:00:33 +02:00
|
|
|
|
|
|
|
def isServerName(s):
|
2015-07-07 23:31:47 +02:00
|
|
|
return _isASCII(s) and '.' in s and not s.startswith('.')
|
2015-06-21 05:36:35 +02:00
|
|
|
|
2015-07-05 21:48:39 +02:00
|
|
|
def parseModes(irc, target, args):
|
2015-07-05 08:49:28 +02:00
|
|
|
"""Parses a mode string into a list of (mode, argument) tuples.
|
|
|
|
['+mitl-o', '3', 'person'] => [('+m', None), ('+i', None), ('+t', None), ('+l', '3'), ('-o', 'person')]
|
2015-06-21 05:36:35 +02:00
|
|
|
"""
|
2015-07-05 08:49:28 +02:00
|
|
|
# http://www.irc.org/tech_docs/005.html
|
2015-07-06 04:19:49 +02:00
|
|
|
# A = Mode that adds or removes a nick or address to a list. Always has a parameter.
|
|
|
|
# B = Mode that changes a setting and always has a parameter.
|
2015-07-05 08:49:28 +02:00
|
|
|
# C = Mode that changes a setting and only has a parameter when set.
|
|
|
|
# D = Mode that changes a setting and never has a parameter.
|
2015-07-05 21:48:39 +02:00
|
|
|
usermodes = not isChannel(target)
|
2015-07-09 07:42:04 +02:00
|
|
|
prefix = ''
|
2015-07-05 08:49:28 +02:00
|
|
|
modestring = args[0]
|
|
|
|
if not modestring:
|
2015-06-21 05:36:35 +02:00
|
|
|
return ValueError('No modes supplied in parseModes query: %r' % modes)
|
2015-07-05 08:49:28 +02:00
|
|
|
args = args[1:]
|
|
|
|
if usermodes:
|
2015-07-06 01:46:57 +02:00
|
|
|
log.debug('(%s) Using irc.umodes for this query: %s', irc.name, irc.umodes)
|
2015-07-06 04:19:49 +02:00
|
|
|
supported_modes = irc.umodes
|
2015-07-05 08:49:28 +02:00
|
|
|
else:
|
2015-07-06 01:46:57 +02:00
|
|
|
log.debug('(%s) Using irc.cmodes for this query: %s', irc.name, irc.cmodes)
|
2015-07-05 08:49:28 +02:00
|
|
|
supported_modes = irc.cmodes
|
2015-06-21 05:36:35 +02:00
|
|
|
res = []
|
2015-07-05 08:49:28 +02:00
|
|
|
for mode in modestring:
|
2015-06-21 05:36:35 +02:00
|
|
|
if mode in '+-':
|
|
|
|
prefix = mode
|
|
|
|
else:
|
2015-07-09 07:42:04 +02:00
|
|
|
if not prefix:
|
|
|
|
raise ValueError('Invalid query %r: mode char given without preceding prefix.' % modestring)
|
2015-07-05 08:49:28 +02:00
|
|
|
arg = None
|
2015-07-06 04:19:49 +02:00
|
|
|
log.debug('Current mode: %s%s; args left: %s', prefix, mode, args)
|
2015-07-09 07:42:04 +02:00
|
|
|
try:
|
|
|
|
if mode in (supported_modes['*A'] + supported_modes['*B']):
|
|
|
|
# Must have parameter.
|
|
|
|
log.debug('Mode %s: This mode must have parameter.', mode)
|
|
|
|
arg = args.pop(0)
|
|
|
|
elif mode in irc.prefixmodes and not usermodes:
|
|
|
|
# We're setting a prefix mode on someone (e.g. +o user1)
|
|
|
|
log.debug('Mode %s: This mode is a prefix mode.', mode)
|
|
|
|
arg = args.pop(0)
|
|
|
|
elif prefix == '+' and mode in supported_modes['*C']:
|
|
|
|
# Only has parameter when setting.
|
|
|
|
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.
|
2015-07-05 08:49:28 +02:00
|
|
|
res.append((prefix + mode, arg))
|
2015-06-21 05:36:35 +02:00
|
|
|
return res
|
|
|
|
|
2015-07-05 21:48:39 +02:00
|
|
|
def applyModes(irc, target, changedmodes):
|
2015-07-06 08:00:14 +02:00
|
|
|
"""<target> <changedmodes>
|
2015-07-09 01:58:59 +02:00
|
|
|
|
2015-07-06 08:00:14 +02:00
|
|
|
Takes a list of parsed IRC modes (<changedmodes>, in the format of parseModes()), and applies them on <target>.
|
|
|
|
<target> can be either a channel or a user; this is handled automatically."""
|
2015-07-05 21:48:39 +02:00
|
|
|
usermodes = not isChannel(target)
|
2015-07-06 01:46:57 +02:00
|
|
|
log.debug('(%s) Using usermodes for this query? %s', irc.name, usermodes)
|
2015-07-05 21:48:39 +02:00
|
|
|
if usermodes:
|
2015-07-09 08:11:44 +02:00
|
|
|
old_modelist = irc.users[target].modes
|
2015-07-06 08:15:17 +02:00
|
|
|
supported_modes = irc.umodes
|
2015-07-05 21:48:39 +02:00
|
|
|
else:
|
2015-07-09 08:11:44 +02:00
|
|
|
old_modelist = irc.channels[target].modes
|
2015-07-06 08:15:17 +02:00
|
|
|
supported_modes = irc.cmodes
|
2015-07-09 08:11:44 +02:00
|
|
|
modelist = old_modelist.copy()
|
2015-07-05 22:44:48 +02:00
|
|
|
log.debug('(%s) Applying modes %r on %s (initial modelist: %s)', irc.name, changedmodes, target, modelist)
|
2015-06-21 05:36:35 +02:00
|
|
|
for mode in changedmodes:
|
2015-07-06 08:00:14 +02:00
|
|
|
# Chop off the +/- part that parseModes gives; it's meaningless for a mode list.
|
|
|
|
real_mode = (mode[0][1], mode[1])
|
2015-07-05 21:48:39 +02:00
|
|
|
if not usermodes:
|
|
|
|
pmode = ''
|
|
|
|
for m in ('owner', 'admin', 'op', 'halfop', 'voice'):
|
2015-07-06 08:00:14 +02:00
|
|
|
if m in irc.cmodes and real_mode[0] == irc.cmodes[m]:
|
2015-07-05 21:48:39 +02:00
|
|
|
pmode = m+'s'
|
|
|
|
if pmode:
|
|
|
|
pmodelist = irc.channels[target].prefixmodes[pmode]
|
2015-07-05 22:44:48 +02:00
|
|
|
log.debug('(%s) Initial prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes)
|
2015-07-05 21:48:39 +02:00
|
|
|
if mode[0][0] == '+':
|
|
|
|
pmodelist.add(mode[1])
|
|
|
|
else:
|
|
|
|
pmodelist.discard(mode[1])
|
2015-07-05 22:44:48 +02:00
|
|
|
log.debug('(%s) Final prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes)
|
2015-07-06 08:00:14 +02:00
|
|
|
if real_mode[0] in irc.prefixmodes:
|
2015-07-05 21:53:53 +02:00
|
|
|
# Ignore other prefix modes such as InspIRCd's +Yy
|
2015-07-06 08:15:17 +02:00
|
|
|
log.debug('(%s) Not adding mode %s to IrcChannel.modes because '
|
|
|
|
'it\'s a prefix mode we don\'t care about.', irc.name, str(mode))
|
2015-07-05 21:48:39 +02:00
|
|
|
continue
|
2015-07-05 08:49:28 +02:00
|
|
|
if mode[0][0] == '+':
|
2015-06-21 05:36:35 +02:00
|
|
|
# We're adding a mode
|
2015-07-09 08:13:23 +02:00
|
|
|
existing = [m for m in modelist if m[0] == real_mode[0] and m[1] != real_mode[1]]
|
|
|
|
if existing and real_mode[1] and real_mode[0] not in irc.cmodes['*A']:
|
2015-07-06 08:15:17 +02:00
|
|
|
# The mode we're setting takes a parameter, but is not a list mode (like +beI).
|
|
|
|
# Therefore, only one version of it can exist at a time, and we must remove
|
|
|
|
# any old modepairs using the same letter. Otherwise, we'll get duplicates when,
|
|
|
|
# for example, someone sets mode "+l 30" on a channel already set "+l 25".
|
|
|
|
log.debug('(%s) Old modes for mode %r exist on %s, removing them: %s',
|
2015-07-09 08:13:23 +02:00
|
|
|
irc.name, real_mode, target, str(existing))
|
2015-07-06 08:15:17 +02:00
|
|
|
[modelist.discard(oldmode) for oldmode in existing]
|
2015-07-06 08:00:14 +02:00
|
|
|
modelist.add(real_mode)
|
2015-07-09 08:13:23 +02:00
|
|
|
log.debug('(%s) Adding mode %r on %s', irc.name, real_mode, target)
|
2015-06-21 05:36:35 +02:00
|
|
|
else:
|
2015-07-09 08:13:23 +02:00
|
|
|
log.debug('(%s) Removing mode %r on %s', irc.name, real_mode, target)
|
2015-07-06 04:19:49 +02:00
|
|
|
# We're removing a mode
|
2015-07-06 08:00:14 +02:00
|
|
|
if real_mode[1] is None:
|
2015-07-06 04:19:49 +02:00
|
|
|
# We're removing a mode that only takes arguments when setting.
|
2015-07-06 08:15:17 +02:00
|
|
|
# Remove all mode entries that use the same letter as the one
|
|
|
|
# we're unsetting.
|
2015-07-06 04:19:49 +02:00
|
|
|
for oldmode in modelist.copy():
|
2015-07-06 08:00:14 +02:00
|
|
|
if oldmode[0] == real_mode[0]:
|
2015-07-06 04:19:49 +02:00
|
|
|
modelist.discard(oldmode)
|
|
|
|
else:
|
|
|
|
# Swap the - for a + and then remove it from the list.
|
2015-07-06 08:00:14 +02:00
|
|
|
modelist.discard(real_mode)
|
2015-07-06 01:46:57 +02:00
|
|
|
log.debug('(%s) Final modelist: %s', irc.name, modelist)
|
2015-07-09 08:11:44 +02:00
|
|
|
if usermodes:
|
|
|
|
irc.users[target].modes = modelist
|
|
|
|
else:
|
|
|
|
irc.channels[target].modes = modelist
|
2015-06-21 05:54:01 +02:00
|
|
|
|
|
|
|
def joinModes(modes):
|
2015-07-09 01:58:59 +02:00
|
|
|
"""<mode list>
|
|
|
|
|
|
|
|
Takes a list of (mode, arg) tuples in parseModes() format, and
|
|
|
|
joins them into a string. See testJoinModes in tests/test_utils.py
|
|
|
|
for some examples."""
|
|
|
|
prefix = '+' # Assume we're adding modes unless told otherwise
|
2015-07-05 08:49:28 +02:00
|
|
|
modelist = ''
|
|
|
|
args = []
|
|
|
|
for modepair in modes:
|
|
|
|
mode, arg = modepair
|
2015-07-09 01:58:59 +02:00
|
|
|
assert len(mode) in (1, 2), "Incorrect length of a mode (received %r)" % mode
|
|
|
|
try:
|
|
|
|
# If the mode has a prefix, use that.
|
|
|
|
curr_prefix, mode = mode
|
|
|
|
except ValueError:
|
|
|
|
# If not, the current prefix stays the same; move on to the next
|
|
|
|
# modepair.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# If the prefix of this mode isn't the same as the last one, add
|
|
|
|
# the prefix to the modestring. This prevents '+nt-lk' from turning
|
|
|
|
# into '+n+t-l-k' or '+ntlk'.
|
|
|
|
if prefix != curr_prefix:
|
|
|
|
modelist += curr_prefix
|
|
|
|
prefix = curr_prefix
|
2015-07-06 21:28:10 +02:00
|
|
|
modelist += mode
|
2015-07-05 08:49:28 +02:00
|
|
|
if arg is not None:
|
|
|
|
args.append(arg)
|
2015-07-09 01:58:59 +02:00
|
|
|
if not modelist.startswith(('+', '-')):
|
|
|
|
# Our starting mode didn't have a prefix with it. Assume '+'.
|
|
|
|
modelist = '+' + modelist
|
2015-07-06 21:28:10 +02:00
|
|
|
if args:
|
2015-07-09 01:58:59 +02:00
|
|
|
# Add the args if there are any.
|
|
|
|
modelist += ' %s' % ' '.join(args)
|
|
|
|
return modelist
|
2015-06-24 04:29:53 +02:00
|
|
|
|
|
|
|
def isInternalClient(irc, numeric):
|
|
|
|
"""<irc object> <client numeric>
|
|
|
|
|
|
|
|
Checks whether <client numeric> is a PyLink PseudoClient,
|
|
|
|
returning the SID of the PseudoClient's server if True.
|
|
|
|
"""
|
|
|
|
for sid in irc.servers:
|
|
|
|
if irc.servers[sid].internal and numeric in irc.servers[sid].users:
|
|
|
|
return sid
|
|
|
|
|
|
|
|
def isInternalServer(irc, sid):
|
|
|
|
"""<irc object> <sid>
|
|
|
|
|
|
|
|
Returns whether <sid> is an internal PyLink PseudoServer.
|
|
|
|
"""
|
|
|
|
return (sid in irc.servers and irc.servers[sid].internal)
|
2015-07-11 01:42:53 +02:00
|
|
|
|
|
|
|
def isOper(irc, uid):
|
|
|
|
"""<irc object> <UID>
|
|
|
|
|
|
|
|
Returns whether <UID> is an opered client.
|
|
|
|
"""
|
|
|
|
return (uid in irc.users and ("o", None) in irc.users[uid].modes)
|