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-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-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-05-31 21:20:09 +02:00
|
|
|
def msg(irc, target, text, notice=False):
|
2015-05-31 08:00:39 +02:00
|
|
|
command = 'NOTICE' if notice else 'PRIVMSG'
|
2015-06-17 05:05:41 +02:00
|
|
|
irc.proto._sendFromUser(irc, irc.pseudoclient.uid, '%s %s :%s' % (command, 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-04 02:05:44 +02:00
|
|
|
return _isASCII(s) and '.' in s and not s.startswith('.') \
|
2015-06-22 00:00:33 +02:00
|
|
|
and not s.endswith('.')
|
2015-06-21 05:36:35 +02:00
|
|
|
|
2015-07-05 08:49:28 +02:00
|
|
|
def parseModes(irc, args, usermodes=False):
|
|
|
|
"""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
|
|
|
|
# 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.
|
|
|
|
# C = Mode that changes a setting and only has a parameter when set.
|
|
|
|
# D = Mode that changes a setting and never has a parameter.
|
|
|
|
print(args)
|
|
|
|
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:
|
|
|
|
supported_modes = irc.umodes
|
|
|
|
else:
|
|
|
|
supported_modes = irc.cmodes
|
|
|
|
print('supported modes: %s' % supported_modes)
|
2015-06-21 05:36:35 +02:00
|
|
|
res = []
|
2015-07-05 08:49:28 +02:00
|
|
|
for x in ('A', 'B', 'C', 'D'):
|
|
|
|
print('%s modes: %s' % (x, supported_modes['*'+x]))
|
|
|
|
for mode in modestring:
|
2015-06-21 05:36:35 +02:00
|
|
|
if mode in '+-':
|
|
|
|
prefix = mode
|
|
|
|
else:
|
2015-07-05 08:49:28 +02:00
|
|
|
arg = None
|
|
|
|
if mode in (supported_modes['*A'] + supported_modes['*B']):
|
|
|
|
# Must have parameter.
|
|
|
|
print('%s: 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)
|
|
|
|
print('%s: prefixmode.' % mode)
|
2015-07-05 09:20:45 +02:00
|
|
|
# TODO: handle this properly (issue #16).
|
|
|
|
continue
|
2015-07-05 08:49:28 +02:00
|
|
|
elif prefix == '+' and mode in supported_modes['*C']:
|
|
|
|
# Only has parameter when setting.
|
|
|
|
print('%s: Only has parameter when setting.' % mode)
|
|
|
|
arg = args.pop(0)
|
|
|
|
res.append((prefix + mode, arg))
|
2015-06-21 05:36:35 +02:00
|
|
|
return res
|
|
|
|
|
|
|
|
def applyModes(modelist, changedmodes):
|
2015-06-21 06:06:45 +02:00
|
|
|
modelist = modelist.copy()
|
|
|
|
print('Initial modelist: %s' % modelist)
|
|
|
|
print('Changedmodes: %r' % changedmodes)
|
2015-06-21 05:36:35 +02:00
|
|
|
for mode in changedmodes:
|
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-06-21 05:58:25 +02:00
|
|
|
modelist.add(mode)
|
2015-07-05 08:49:28 +02:00
|
|
|
print('Adding mode %r' % str(mode))
|
2015-06-21 05:36:35 +02:00
|
|
|
else:
|
|
|
|
# We're removing a mode
|
2015-07-05 09:20:45 +02:00
|
|
|
mode[0] = mode[0].replace('-', '+')
|
2015-06-21 05:58:25 +02:00
|
|
|
modelist.discard(mode)
|
2015-07-05 08:49:28 +02:00
|
|
|
print('Removing mode %r' % str(mode))
|
2015-06-21 06:06:45 +02:00
|
|
|
print('Final modelist: %s' % modelist)
|
2015-06-21 05:36:35 +02:00
|
|
|
return modelist
|
2015-06-21 05:54:01 +02:00
|
|
|
|
|
|
|
def joinModes(modes):
|
2015-07-05 08:49:28 +02:00
|
|
|
modelist = ''
|
|
|
|
args = []
|
|
|
|
for modepair in modes:
|
|
|
|
mode, arg = modepair
|
|
|
|
modelist += mode[1]
|
|
|
|
if arg is not None:
|
|
|
|
args.append(arg)
|
|
|
|
s = '+%s %s' % (modelist, ' '.join(args))
|
|
|
|
return s
|
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)
|