2015-12-26 23:45:28 +01:00
|
|
|
"""
|
|
|
|
ts6_common.py: Common base protocol class with functions shared by the UnrealIRCd, InspIRCd, and TS6 protocol modules.
|
|
|
|
"""
|
|
|
|
|
2016-04-25 06:08:07 +02:00
|
|
|
import string
|
2016-07-11 06:10:44 +02:00
|
|
|
import time
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2017-03-05 09:06:19 +01:00
|
|
|
from pylinkirc import utils, structures, conf
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc.classes import *
|
|
|
|
from pylinkirc.log import log
|
2016-07-05 09:24:23 +02:00
|
|
|
from pylinkirc.protocols.ircs2s_common import *
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-04-25 06:08:07 +02:00
|
|
|
class TS6SIDGenerator():
|
|
|
|
"""
|
|
|
|
TS6 SID Generator. <query> is a 3 character string with any combination of
|
|
|
|
uppercase letters, digits, and #'s. it 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, irc):
|
|
|
|
self.irc = irc
|
|
|
|
try:
|
|
|
|
self.query = query = list(irc.serverdata["sidrange"])
|
|
|
|
except KeyError:
|
|
|
|
raise RuntimeError('(%s) "sidrange" is missing from your server configuration block!' % irc.name)
|
|
|
|
|
|
|
|
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):
|
|
|
|
# Iterate over each character in the query string we got, along
|
|
|
|
# with its index in the string.
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Increments the SID generator to the next available SID.
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
|
|
|
def next_sid(self):
|
|
|
|
"""
|
|
|
|
Returns the next unused TS6 SID for the server.
|
|
|
|
"""
|
2017-06-30 06:51:02 +02:00
|
|
|
while ''.join(self.output) in self.irc.servers:
|
2016-04-25 06:08:07 +02:00
|
|
|
# Increment until the SID we have doesn't already exist.
|
|
|
|
self.increment()
|
|
|
|
sid = ''.join(self.output)
|
|
|
|
return sid
|
|
|
|
|
2017-08-29 04:42:10 +02:00
|
|
|
class TS6UIDGenerator(IncrementalUIDGenerator):
|
2016-04-25 06:08:07 +02:00
|
|
|
"""Implements an incremental TS6 UID Generator."""
|
|
|
|
|
|
|
|
def __init__(self, sid):
|
|
|
|
# Define the options for IncrementalUIDGenerator, and then
|
|
|
|
# initialize its functions.
|
|
|
|
# TS6 UIDs are 6 characters in length (9 including the SID).
|
|
|
|
# They go from ABCDEFGHIJKLMNOPQRSTUVWXYZ -> 0123456789 -> wrap around:
|
|
|
|
# e.g. AAAAAA, AAAAAB ..., AAAAA8, AAAAA9, AAAABA, etc.
|
|
|
|
self.allowedchars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456879'
|
|
|
|
self.length = 6
|
|
|
|
super().__init__(sid)
|
|
|
|
|
2016-07-05 09:24:23 +02:00
|
|
|
class TS6BaseProtocol(IRCS2SProtocol):
|
2017-06-25 10:12:22 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2016-04-06 03:34:54 +02:00
|
|
|
|
2016-04-25 06:16:41 +02:00
|
|
|
# Dictionary of UID generators (one for each server).
|
|
|
|
self.uidgen = structures.KeyedDefaultdict(TS6UIDGenerator)
|
2016-04-06 03:34:54 +02:00
|
|
|
|
|
|
|
# SID generator for TS6.
|
2017-06-25 10:12:22 +02:00
|
|
|
self.sidgen = TS6SIDGenerator(self)
|
2016-04-06 03:34:54 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
### OUTGOING COMMANDS
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-07-05 08:38:54 +02:00
|
|
|
def kill(self, numeric, target, reason):
|
|
|
|
"""Sends a kill from a PyLink client/server."""
|
|
|
|
|
2017-06-30 07:56:14 +02:00
|
|
|
if (not self.is_internal_client(numeric)) and \
|
|
|
|
(not self.is_internal_server(numeric)):
|
2016-07-05 08:38:54 +02:00
|
|
|
raise LookupError('No such PyLink client/server exists.')
|
|
|
|
|
|
|
|
# From TS6 docs:
|
|
|
|
# KILL:
|
|
|
|
# parameters: target user, path
|
|
|
|
|
|
|
|
# The format of the path parameter is some sort of description of the source of
|
|
|
|
# the kill followed by a space and a parenthesized reason. To avoid overflow,
|
|
|
|
# it is recommended not to add anything to the path.
|
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
assert target in self.users, "Unknown target %r for kill()!" % target
|
2016-07-05 08:38:54 +02:00
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
if numeric in self.users:
|
2016-07-05 08:38:54 +02:00
|
|
|
# Killer was an user. Follow examples of setting the path to be "killer.host!killer.nick".
|
2017-06-25 11:03:12 +02:00
|
|
|
userobj = self.users[numeric]
|
2016-07-05 08:38:54 +02:00
|
|
|
killpath = '%s!%s' % (userobj.host, userobj.nick)
|
2017-06-25 11:03:12 +02:00
|
|
|
elif numeric in self.servers:
|
2016-07-05 08:38:54 +02:00
|
|
|
# Sender was a server; killpath is just its name.
|
2017-06-25 11:03:12 +02:00
|
|
|
killpath = self.servers[numeric].name
|
2016-07-05 08:38:54 +02:00
|
|
|
else:
|
|
|
|
# Invalid sender?! This shouldn't happen, but make the killpath our server name anyways.
|
|
|
|
log.warning('(%s) Invalid sender %s for kill(); using our server name instead.',
|
2017-06-25 11:03:12 +02:00
|
|
|
self.name, numeric)
|
|
|
|
killpath = self.servers[self.sid].name
|
2016-07-05 08:38:54 +02:00
|
|
|
|
2017-06-25 08:40:41 +02:00
|
|
|
self._send_with_prefix(numeric, 'KILL %s :%s (%s)' % (target, killpath, reason))
|
2017-06-28 01:21:30 +02:00
|
|
|
self._remove_client(target)
|
2016-07-05 08:38:54 +02:00
|
|
|
|
2016-01-17 01:51:04 +01:00
|
|
|
def nick(self, numeric, newnick):
|
2015-09-06 03:00:57 +02:00
|
|
|
"""Changes the nick of a PyLink client."""
|
2017-06-30 07:56:14 +02:00
|
|
|
if not self.is_internal_client(numeric):
|
2016-01-10 02:44:05 +01:00
|
|
|
raise LookupError('No such PyLink client exists.')
|
2016-07-11 06:10:44 +02:00
|
|
|
|
2017-06-25 08:40:41 +02:00
|
|
|
self._send_with_prefix(numeric, 'NICK %s %s' % (newnick, int(time.time())))
|
2016-07-11 06:10:44 +02:00
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[numeric].nick = newnick
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-07-11 06:10:44 +02:00
|
|
|
# Update the NICK TS.
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[numeric].ts = int(time.time())
|
2016-07-11 06:10:44 +02:00
|
|
|
|
2017-07-01 06:27:02 +02:00
|
|
|
def spawn_server(self, name, sid=None, uplink=None, desc=None, endburst_delay=0):
|
2015-11-15 18:12:21 +01:00
|
|
|
"""
|
|
|
|
Spawns a server off a PyLink server. desc (server description)
|
|
|
|
defaults to the one in the config. uplink defaults to the main PyLink
|
|
|
|
server, and sid (the server ID) is automatically generated if not
|
|
|
|
given.
|
2015-12-18 06:18:11 +01:00
|
|
|
|
|
|
|
Note: TS6 doesn't use a specific ENDBURST command, so the endburst_delay
|
|
|
|
option will be ignored if given.
|
2015-11-15 18:12:21 +01:00
|
|
|
"""
|
|
|
|
# -> :0AL SID test.server 1 0XY :some silly pseudoserver
|
2017-06-25 11:03:12 +02:00
|
|
|
uplink = uplink or self.sid
|
2015-11-15 18:12:21 +01:00
|
|
|
name = name.lower()
|
2017-06-25 11:03:12 +02:00
|
|
|
desc = desc or self.serverdata.get('serverdesc') or conf.conf['bot']['serverdesc']
|
2017-09-24 07:36:16 +02:00
|
|
|
|
2015-11-15 18:12:21 +01:00
|
|
|
if sid is None: # No sid given; generate one!
|
|
|
|
sid = self.sidgen.next_sid()
|
|
|
|
assert len(sid) == 3, "Incorrect SID length"
|
2017-06-25 11:03:12 +02:00
|
|
|
if sid in self.servers:
|
2015-11-15 18:12:21 +01:00
|
|
|
raise ValueError('A server with SID %r already exists!' % sid)
|
2017-06-25 11:03:12 +02:00
|
|
|
for server in self.servers.values():
|
2015-11-15 18:12:21 +01:00
|
|
|
if name == server.name:
|
|
|
|
raise ValueError('A server named %r already exists!' % name)
|
2017-06-30 07:56:14 +02:00
|
|
|
if not self.is_internal_server(uplink):
|
2016-01-10 02:44:05 +01:00
|
|
|
raise ValueError('Server %r is not a PyLink server!' % uplink)
|
2017-08-29 05:07:12 +02:00
|
|
|
if not self.is_server_name(name):
|
2015-11-15 18:12:21 +01:00
|
|
|
raise ValueError('Invalid server name %r' % name)
|
2017-09-24 07:36:16 +02:00
|
|
|
|
2017-08-25 22:53:45 +02:00
|
|
|
self.servers[sid] = Server(self, uplink, name, internal=True, desc=desc)
|
2017-09-24 07:36:16 +02:00
|
|
|
self._send_with_prefix(uplink, 'SID %s %s %s :%s' % (name, self.servers[sid].hopcount, sid, desc))
|
2015-11-15 18:12:21 +01:00
|
|
|
return sid
|
|
|
|
|
2016-01-17 01:40:36 +01:00
|
|
|
def away(self, source, text):
|
2015-11-15 18:34:26 +01:00
|
|
|
"""Sends an AWAY message from a PyLink client. <text> can be an empty string
|
|
|
|
to unset AWAY status."""
|
|
|
|
if text:
|
2017-06-25 08:40:41 +02:00
|
|
|
self._send_with_prefix(source, 'AWAY :%s' % text)
|
2015-11-15 18:34:26 +01:00
|
|
|
else:
|
2017-06-25 08:40:41 +02:00
|
|
|
self._send_with_prefix(source, 'AWAY')
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[source].away = text
|
2015-11-15 18:34:26 +01:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
### HANDLERS
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2017-12-21 09:04:55 +01:00
|
|
|
def handle_knock(self, numeric, command, args):
|
|
|
|
"""Handles channel KNOCKs."""
|
|
|
|
# InspIRCd:
|
|
|
|
# <- :70MAAAAAA ENCAP * KNOCK #blah :abcdefg
|
|
|
|
# Charybdis:
|
|
|
|
# <- :42XAAAAAC KNOCK #endlessvoid
|
|
|
|
# UnrealIRCd propagates knocks as a channel notice to all ops, so this handler is not used there.
|
|
|
|
channel = args[0]
|
|
|
|
try:
|
|
|
|
text = args[1]
|
|
|
|
except IndexError:
|
|
|
|
text = ''
|
|
|
|
return {'channel': channel, 'text': text}
|
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_nick(self, numeric, command, args):
|
|
|
|
"""Handles incoming NICK changes."""
|
|
|
|
# <- :70MAAAAAA NICK GL-devel 1434744242
|
2017-06-25 11:03:12 +02:00
|
|
|
oldnick = self.users[numeric].nick
|
|
|
|
newnick = self.users[numeric].nick = args[0]
|
2016-07-11 06:10:44 +02:00
|
|
|
|
|
|
|
# Update the nick TS.
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[numeric].ts = ts = int(args[1])
|
2016-07-11 06:10:44 +02:00
|
|
|
|
|
|
|
return {'newnick': newnick, 'oldnick': oldnick, 'ts': ts}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_save(self, numeric, command, args):
|
|
|
|
"""Handles incoming SAVE messages, used to handle nick collisions."""
|
|
|
|
# In this below example, the client Derp_ already exists,
|
|
|
|
# and trying to change someone's nick to it will cause a nick
|
2016-01-10 03:34:41 +01:00
|
|
|
# collision. On TS6 IRCds, this will simply set the collided user's
|
2015-09-06 03:00:57 +02:00
|
|
|
# nick to its UID.
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
# <- :70MAAAAAA PRIVMSG 0AL000001 :nickclient PyLink Derp_
|
|
|
|
# -> :0AL000001 NICK Derp_ 1433728673
|
|
|
|
# <- :70M SAVE 0AL000001 1433728673
|
|
|
|
user = args[0]
|
2017-06-25 11:03:12 +02:00
|
|
|
oldnick = self.users[user].nick
|
|
|
|
self.users[user].nick = user
|
2016-07-11 06:13:56 +02:00
|
|
|
|
2016-07-11 06:20:47 +02:00
|
|
|
# TS6 SAVE sets nick TS to 100. This is hardcoded in InspIRCd and
|
|
|
|
# charybdis.
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[user].ts = 100
|
2016-07-11 06:13:56 +02:00
|
|
|
|
2016-07-11 06:20:47 +02:00
|
|
|
return {'target': user, 'ts': 100, 'oldnick': oldnick}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2017-08-11 04:50:32 +02:00
|
|
|
def handle_server(self, numeric, command, args):
|
|
|
|
"""Handles the SERVER command, used for introducing older (TS5) servers."""
|
|
|
|
# <- :services.int SERVER a.bc 2 :(H) [GL] test jupe
|
|
|
|
servername = args[0].lower()
|
|
|
|
sdesc = args[-1]
|
2017-08-25 22:53:45 +02:00
|
|
|
self.servers[servername] = Server(self, numeric, servername, desc=sdesc)
|
2017-08-11 04:50:32 +02:00
|
|
|
return {'name': servername, 'sid': None, 'text': sdesc}
|
|
|
|
|
2017-08-11 04:42:04 +02:00
|
|
|
def handle_sid(self, numeric, command, args):
|
|
|
|
"""Handles the SID command, used for introducing remote servers by our uplink."""
|
|
|
|
# <- SID services.int 2 00A :Shaltúre IRC Services
|
|
|
|
# parameters: server name, hopcount, sid, server description
|
|
|
|
sname = args[0].lower()
|
|
|
|
sid = args[2]
|
|
|
|
sdesc = args[-1]
|
2017-08-25 22:53:45 +02:00
|
|
|
self.servers[sid] = Server(self, numeric, sname, desc=sdesc)
|
2017-08-11 04:42:04 +02:00
|
|
|
return {'name': sname, 'sid': sid, 'text': sdesc}
|
|
|
|
|
2016-07-12 08:21:08 +02:00
|
|
|
def handle_svsnick(self, source, command, args):
|
|
|
|
"""Handles SVSNICK (forced nickname change attempts)."""
|
|
|
|
# InspIRCd:
|
|
|
|
# <- :00A ENCAP 902 SVSNICK 902AAAAAB Guest53593 :1468299404
|
|
|
|
# This is rewritten to SVSNICK with args ['902AAAAAB', 'Guest53593', '1468299404']
|
|
|
|
|
|
|
|
# UnrealIRCd:
|
|
|
|
# <- :services.midnight.vpn SVSNICK GL Guest87795 1468303726
|
2017-06-17 02:12:48 +02:00
|
|
|
return {'target': self._get_UID(args[0]), 'newnick': args[1]}
|