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
|
|
|
|
|
|
|
def parseModes(args):
|
|
|
|
"""['+mitl-o', '3', 'person'] => ['+m', '+i', '+t', '-o']
|
|
|
|
|
|
|
|
TODO: handle modes with extra arguments (mainly channel modes like +beIqlk)
|
|
|
|
"""
|
|
|
|
modes = args[0]
|
|
|
|
extramodes = args[1:]
|
|
|
|
if not modes:
|
|
|
|
return ValueError('No modes supplied in parseModes query: %r' % modes)
|
|
|
|
res = []
|
|
|
|
for mode in modes:
|
|
|
|
if mode in '+-':
|
|
|
|
prefix = mode
|
|
|
|
else:
|
|
|
|
res.append(prefix + mode)
|
|
|
|
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:
|
|
|
|
if mode[0] == '+':
|
|
|
|
# We're adding a mode
|
2015-06-21 05:58:25 +02:00
|
|
|
modelist.add(mode)
|
2015-06-21 06:06:45 +02:00
|
|
|
print('Adding mode %r' % mode)
|
2015-06-21 05:36:35 +02:00
|
|
|
else:
|
|
|
|
# We're removing a mode
|
2015-06-21 05:58:25 +02:00
|
|
|
modelist.discard(mode)
|
2015-06-21 06:06:45 +02:00
|
|
|
print('Removing mode %r' % mode)
|
|
|
|
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):
|
|
|
|
return '+' + ''.join(mode[1] for mode in modes)
|
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)
|