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.
|
|
|
|
"""
|
|
|
|
|
2015-09-04 20:24:40 +02:00
|
|
|
import sys
|
|
|
|
import os
|
2016-04-25 06:08:07 +02:00
|
|
|
import string
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-12-26 23:45:28 +01:00
|
|
|
# Import hacks to access utils and classes...
|
2015-09-04 20:24:40 +02:00
|
|
|
curdir = os.path.dirname(__file__)
|
|
|
|
sys.path += [curdir, os.path.dirname(curdir)]
|
2015-12-26 23:45:28 +01:00
|
|
|
|
2015-09-04 20:24:40 +02:00
|
|
|
import utils
|
|
|
|
from log import log
|
|
|
|
from classes import *
|
2016-04-25 06:16:41 +02:00
|
|
|
import structures
|
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.
|
|
|
|
"""
|
|
|
|
while ''.join(self.output) in self.irc.servers:
|
|
|
|
# Increment until the SID we have doesn't already exist.
|
|
|
|
self.increment()
|
|
|
|
sid = ''.join(self.output)
|
|
|
|
return sid
|
|
|
|
|
|
|
|
class TS6UIDGenerator(utils.IncrementalUIDGenerator):
|
|
|
|
"""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)
|
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
class TS6BaseProtocol(Protocol):
|
2016-04-06 03:34:54 +02:00
|
|
|
|
|
|
|
def __init__(self, irc):
|
|
|
|
super().__init__(irc)
|
|
|
|
|
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.
|
2016-04-25 06:08:07 +02:00
|
|
|
self.sidgen = TS6SIDGenerator(irc)
|
2016-04-06 03:34:54 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def _send(self, source, msg):
|
|
|
|
"""Sends a TS6-style raw command from a source numeric to the self.irc connection given."""
|
|
|
|
self.irc.send(':%s %s' % (source, msg))
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def parseTS6Args(self, args):
|
|
|
|
"""Similar to parseArgs(), but stripping leading colons from the first argument
|
|
|
|
of a line (usually the sender field)."""
|
|
|
|
args = self.parseArgs(args)
|
|
|
|
args[0] = args[0].split(':', 1)[1]
|
|
|
|
return args
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-04-03 21:50:04 +02:00
|
|
|
def _getOutgoingNick(self, uid):
|
|
|
|
"""
|
|
|
|
Returns the outgoing nick for the given UID. In the base ts6_common implementation,
|
|
|
|
this does nothing, but other modules subclassing this can override it.
|
|
|
|
For example, this can be used to turn PUIDs (used to store legacy, UID-less users)
|
|
|
|
to actual nicks in outgoing messages, so that a remote IRCd can understand it.
|
|
|
|
"""
|
|
|
|
return uid
|
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
### OUTGOING COMMANDS
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-01-17 01:47:35 +01:00
|
|
|
def numeric(self, source, numeric, target, text):
|
2015-11-12 04:40:10 +01:00
|
|
|
"""Sends raw numerics from a server to a remote client, used for WHOIS
|
|
|
|
replies."""
|
2016-04-03 21:50:04 +02:00
|
|
|
# Mangle the target for IRCds that require it.
|
|
|
|
target = self._getOutgoingNick(target)
|
|
|
|
|
2015-11-12 04:40:10 +01:00
|
|
|
self._send(source, '%s %s %s' % (numeric, target, text))
|
|
|
|
|
2016-01-17 01:56:40 +01:00
|
|
|
def kick(self, numeric, channel, target, reason=None):
|
|
|
|
"""Sends kicks from a PyLink client/server."""
|
|
|
|
|
|
|
|
if (not self.irc.isInternalClient(numeric)) and \
|
|
|
|
(not self.irc.isInternalServer(numeric)):
|
|
|
|
raise LookupError('No such PyLink client/server exists.')
|
|
|
|
|
2016-05-01 01:57:38 +02:00
|
|
|
channel = self.irc.toLower(channel)
|
2015-09-06 03:00:57 +02:00
|
|
|
if not reason:
|
|
|
|
reason = 'No reason given'
|
2016-04-03 21:50:04 +02:00
|
|
|
|
|
|
|
# Mangle kick targets for IRCds that require it.
|
|
|
|
target = self._getOutgoingNick(target)
|
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
self._send(numeric, 'KICK %s %s :%s' % (channel, target, reason))
|
2016-01-17 01:56:40 +01:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
# We can pretend the target left by its own will; all we really care about
|
|
|
|
# is that the target gets removed from the channel userlist, and calling
|
|
|
|
# handle_part() does that just fine.
|
|
|
|
self.handle_part(target, 'KICK', [channel])
|
2015-09-04 20:24:40 +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."""
|
2016-01-01 02:28:47 +01:00
|
|
|
if not self.irc.isInternalClient(numeric):
|
2016-01-10 02:44:05 +01:00
|
|
|
raise LookupError('No such PyLink client exists.')
|
2015-09-06 03:00:57 +02:00
|
|
|
self._send(numeric, 'NICK %s %s' % (newnick, int(time.time())))
|
|
|
|
self.irc.users[numeric].nick = newnick
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-01-17 01:51:54 +01:00
|
|
|
def part(self, client, channel, reason=None):
|
2015-09-06 03:00:57 +02:00
|
|
|
"""Sends a part from a PyLink client."""
|
2016-05-01 01:57:38 +02:00
|
|
|
channel = self.irc.toLower(channel)
|
2016-01-01 02:28:47 +01:00
|
|
|
if not self.irc.isInternalClient(client):
|
2016-01-10 02:44:05 +01:00
|
|
|
log.error('(%s) Error trying to part %r from %r (no such client exists)', self.irc.name, client, channel)
|
|
|
|
raise LookupError('No such PyLink client exists.')
|
2015-09-06 03:00:57 +02:00
|
|
|
msg = "PART %s" % channel
|
|
|
|
if reason:
|
|
|
|
msg += " :%s" % reason
|
|
|
|
self._send(client, msg)
|
|
|
|
self.handle_part(client, 'PART', [channel])
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-01-17 01:51:54 +01:00
|
|
|
def quit(self, numeric, reason):
|
2015-09-06 03:00:57 +02:00
|
|
|
"""Quits a PyLink client."""
|
2016-01-01 02:28:47 +01:00
|
|
|
if self.irc.isInternalClient(numeric):
|
2015-09-06 03:00:57 +02:00
|
|
|
self._send(numeric, "QUIT :%s" % reason)
|
|
|
|
self.removeClient(numeric)
|
|
|
|
else:
|
2016-01-10 02:44:05 +01:00
|
|
|
raise LookupError("No such PyLink client exists.")
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-01-17 01:44:23 +01:00
|
|
|
def message(self, numeric, target, text):
|
2015-09-06 03:00:57 +02:00
|
|
|
"""Sends a PRIVMSG from a PyLink client."""
|
2016-01-01 02:28:47 +01:00
|
|
|
if not self.irc.isInternalClient(numeric):
|
2016-01-10 02:44:05 +01:00
|
|
|
raise LookupError('No such PyLink client exists.')
|
2016-04-03 21:50:04 +02:00
|
|
|
|
|
|
|
# Mangle message targets for IRCds that require it.
|
|
|
|
target = self._getOutgoingNick(target)
|
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
self._send(numeric, 'PRIVMSG %s :%s' % (target, text))
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-01-17 01:44:23 +01:00
|
|
|
def notice(self, numeric, target, text):
|
2015-09-06 03:00:57 +02:00
|
|
|
"""Sends a NOTICE from a PyLink client."""
|
2016-01-01 02:28:47 +01:00
|
|
|
if not self.irc.isInternalClient(numeric):
|
2016-01-10 02:44:05 +01:00
|
|
|
raise LookupError('No such PyLink client exists.')
|
2016-04-03 21:50:04 +02:00
|
|
|
|
|
|
|
# Mangle message targets for IRCds that require it.
|
|
|
|
target = self._getOutgoingNick(target)
|
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
self._send(numeric, 'NOTICE %s :%s' % (target, text))
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2016-01-17 02:09:52 +01:00
|
|
|
def topic(self, numeric, target, text):
|
2015-09-07 07:09:09 +02:00
|
|
|
"""Sends a TOPIC change from a PyLink client."""
|
2016-01-01 02:28:47 +01:00
|
|
|
if not self.irc.isInternalClient(numeric):
|
2016-01-10 02:44:05 +01:00
|
|
|
raise LookupError('No such PyLink client exists.')
|
2015-09-06 03:00:57 +02:00
|
|
|
self._send(numeric, 'TOPIC %s :%s' % (target, text))
|
|
|
|
self.irc.channels[target].topic = text
|
|
|
|
self.irc.channels[target].topicset = True
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-12-18 06:18:11 +01:00
|
|
|
def spawnServer(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
|
|
|
|
uplink = uplink or self.irc.sid
|
|
|
|
name = name.lower()
|
|
|
|
desc = desc or self.irc.serverdata.get('serverdesc') or self.irc.botdata['serverdesc']
|
|
|
|
if sid is None: # No sid given; generate one!
|
|
|
|
sid = self.sidgen.next_sid()
|
|
|
|
assert len(sid) == 3, "Incorrect SID length"
|
|
|
|
if sid in self.irc.servers:
|
|
|
|
raise ValueError('A server with SID %r already exists!' % sid)
|
|
|
|
for server in self.irc.servers.values():
|
|
|
|
if name == server.name:
|
|
|
|
raise ValueError('A server named %r already exists!' % name)
|
2016-01-01 02:28:47 +01:00
|
|
|
if not self.irc.isInternalServer(uplink):
|
2016-01-10 02:44:05 +01:00
|
|
|
raise ValueError('Server %r is not a PyLink server!' % uplink)
|
2015-11-15 18:12:21 +01:00
|
|
|
if not utils.isServerName(name):
|
|
|
|
raise ValueError('Invalid server name %r' % name)
|
|
|
|
self._send(uplink, 'SID %s 1 %s :%s' % (name, sid, desc))
|
|
|
|
self.irc.servers[sid] = IrcServer(uplink, name, internal=True, desc=desc)
|
|
|
|
return sid
|
|
|
|
|
2016-01-17 01:53:06 +01:00
|
|
|
def squit(self, source, target, text='No reason given'):
|
2015-11-15 18:12:21 +01:00
|
|
|
"""SQUITs a PyLink server."""
|
|
|
|
# -> SQUIT 9PZ :blah, blah
|
|
|
|
log.debug('source=%s, target=%s', source, target)
|
|
|
|
self._send(source, 'SQUIT %s :%s' % (target, text))
|
|
|
|
self.handle_squit(source, 'SQUIT', [target, text])
|
|
|
|
|
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:
|
|
|
|
self._send(source, 'AWAY :%s' % text)
|
|
|
|
else:
|
|
|
|
self._send(source, 'AWAY')
|
2015-12-18 06:50:50 +01:00
|
|
|
self.irc.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
|
|
|
|
2016-01-17 02:48:22 +01:00
|
|
|
def handle_events(self, data):
|
|
|
|
"""Event handler for TS6 protocols.
|
|
|
|
|
|
|
|
This passes most commands to the various handle_ABCD() functions
|
|
|
|
elsewhere defined protocol modules, coersing various sender prefixes
|
|
|
|
from nicks and server names to UIDs and SIDs respectively,
|
|
|
|
whenever possible.
|
|
|
|
|
|
|
|
Commands sent without an explicit sender prefix will have them set to
|
|
|
|
the SID of the uplink server.
|
|
|
|
"""
|
|
|
|
data = data.split(" ")
|
|
|
|
try: # Message starts with a SID/UID prefix.
|
|
|
|
args = self.parseTS6Args(data)
|
|
|
|
sender = args[0]
|
|
|
|
command = args[1]
|
|
|
|
args = args[2:]
|
|
|
|
# If the sender isn't in UID format, try to convert it automatically.
|
|
|
|
# Unreal's protocol, for example, isn't quite consistent with this yet!
|
|
|
|
sender_server = self._getSid(sender)
|
|
|
|
if sender_server in self.irc.servers:
|
|
|
|
# Sender is a server when converted from name to SID.
|
|
|
|
numeric = sender_server
|
|
|
|
else:
|
|
|
|
# Sender is a user.
|
2016-04-09 07:21:37 +02:00
|
|
|
numeric = self._getUid(sender)
|
2016-01-17 02:48:22 +01:00
|
|
|
|
|
|
|
# parseTS6Args() will raise IndexError if the TS6 sender prefix is missing.
|
|
|
|
except IndexError:
|
|
|
|
# Raw command without an explicit sender; assume it's being sent by our uplink.
|
|
|
|
args = self.parseArgs(data)
|
|
|
|
numeric = self.irc.uplink
|
|
|
|
command = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
|
|
|
|
try:
|
|
|
|
func = getattr(self, 'handle_'+command.lower())
|
|
|
|
except AttributeError: # unhandled command
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
parsed_args = func(numeric, command, args)
|
|
|
|
if parsed_args is not None:
|
|
|
|
return [numeric, command, parsed_args]
|
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_privmsg(self, source, command, args):
|
|
|
|
"""Handles incoming PRIVMSG/NOTICE."""
|
|
|
|
# <- :70MAAAAAA PRIVMSG #dev :afasfsa
|
|
|
|
# <- :70MAAAAAA NOTICE 0ALAAAAAA :afasfsa
|
|
|
|
target = args[0]
|
|
|
|
# We use lowercase channels internally, but uppercase UIDs.
|
|
|
|
if utils.isChannel(target):
|
2016-05-01 01:57:38 +02:00
|
|
|
target = self.irc.toLower(target)
|
2015-09-06 03:00:57 +02:00
|
|
|
return {'target': target, 'text': args[1]}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
handle_notice = handle_privmsg
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_kill(self, source, command, args):
|
|
|
|
"""Handles incoming KILLs."""
|
|
|
|
killed = args[0]
|
2016-01-10 03:34:41 +01:00
|
|
|
# Depending on whether the IRCd sends explicit QUIT messages for
|
|
|
|
# killed clients, the user may or may not have automatically been
|
|
|
|
# removed from our user list.
|
|
|
|
# If not, we have to assume that KILL = QUIT and remove them
|
|
|
|
# ourselves.
|
2015-09-06 03:00:57 +02:00
|
|
|
data = self.irc.users.get(killed)
|
|
|
|
if data:
|
|
|
|
self.removeClient(killed)
|
|
|
|
return {'target': killed, 'text': args[1], 'userdata': data}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_kick(self, source, command, args):
|
|
|
|
"""Handles incoming KICKs."""
|
2016-03-21 01:34:13 +01:00
|
|
|
# :70MAAAAAA KICK #test 70MAAAAAA :some reason
|
2016-05-01 01:57:38 +02:00
|
|
|
channel = self.irc.toLower(args[0])
|
2016-04-09 07:21:37 +02:00
|
|
|
kicked = self._getUid(args[1])
|
2015-09-06 03:00:57 +02:00
|
|
|
self.handle_part(kicked, 'KICK', [channel, args[2]])
|
|
|
|
return {'channel': channel, 'target': kicked, 'text': args[2]}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_error(self, numeric, command, args):
|
|
|
|
"""Handles ERROR messages - these mean that our uplink has disconnected us!"""
|
|
|
|
self.irc.connected.clear()
|
|
|
|
raise ProtocolError('Received an ERROR, disconnecting!')
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_nick(self, numeric, command, args):
|
|
|
|
"""Handles incoming NICK changes."""
|
|
|
|
# <- :70MAAAAAA NICK GL-devel 1434744242
|
|
|
|
oldnick = self.irc.users[numeric].nick
|
|
|
|
newnick = self.irc.users[numeric].nick = args[0]
|
|
|
|
return {'newnick': newnick, 'oldnick': oldnick, 'ts': int(args[1])}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_quit(self, numeric, command, args):
|
2015-09-09 04:51:14 +02:00
|
|
|
"""Handles incoming QUIT commands."""
|
2015-09-06 03:00:57 +02:00
|
|
|
# <- :1SRAAGB4T QUIT :Quit: quit message goes here
|
|
|
|
self.removeClient(numeric)
|
|
|
|
return {'text': args[0]}
|
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]
|
|
|
|
oldnick = self.irc.users[user].nick
|
|
|
|
self.irc.users[user].nick = user
|
|
|
|
return {'target': user, 'ts': int(args[1]), 'oldnick': oldnick}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_squit(self, numeric, command, args):
|
|
|
|
"""Handles incoming SQUITs (netsplits)."""
|
|
|
|
# :70M SQUIT 1ML :Server quit by GL!gl@0::1
|
2015-11-15 18:12:21 +01:00
|
|
|
log.debug('handle_squit args: %s', args)
|
2015-09-06 03:00:57 +02:00
|
|
|
split_server = args[0]
|
|
|
|
affected_users = []
|
2015-10-10 23:58:52 +02:00
|
|
|
log.debug('(%s) Splitting server %s (reason: %s)', self.irc.name, split_server, args[-1])
|
|
|
|
if split_server not in self.irc.servers:
|
|
|
|
log.warning("(%s) Tried to split a server (%s) that didn't exist!", self.irc.name, split_server)
|
|
|
|
return
|
2015-09-06 03:00:57 +02:00
|
|
|
# Prevent RuntimeError: dictionary changed size during iteration
|
|
|
|
old_servers = self.irc.servers.copy()
|
|
|
|
for sid, data in old_servers.items():
|
|
|
|
if data.uplink == split_server:
|
|
|
|
log.debug('Server %s also hosts server %s, removing those users too...', split_server, sid)
|
|
|
|
args = self.handle_squit(sid, 'SQUIT', [sid, "PyLink: Automatically splitting leaf servers of %s" % sid])
|
|
|
|
affected_users += args['users']
|
|
|
|
for user in self.irc.servers[split_server].users.copy():
|
|
|
|
affected_users.append(user)
|
|
|
|
log.debug('Removing client %s (%s)', user, self.irc.users[user].nick)
|
|
|
|
self.removeClient(user)
|
2015-09-12 21:05:26 +02:00
|
|
|
sname = self.irc.servers[split_server].name
|
2015-09-06 03:00:57 +02:00
|
|
|
del self.irc.servers[split_server]
|
|
|
|
log.debug('(%s) Netsplit affected users: %s', self.irc.name, affected_users)
|
2015-09-12 21:05:26 +02:00
|
|
|
return {'target': split_server, 'users': affected_users, 'name': sname}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_topic(self, numeric, command, args):
|
|
|
|
"""Handles incoming TOPIC changes from clients. For topic bursts,
|
2015-09-07 08:18:27 +02:00
|
|
|
TB (TS6/charybdis) and FTOPIC (InspIRCd) are used instead."""
|
2015-09-06 03:00:57 +02:00
|
|
|
# <- :70MAAAAAA TOPIC #test :test
|
2016-05-01 01:57:38 +02:00
|
|
|
channel = self.irc.toLower(args[0])
|
2015-09-06 03:00:57 +02:00
|
|
|
topic = args[1]
|
2016-01-10 05:50:38 +01:00
|
|
|
|
2015-09-13 23:23:09 +02:00
|
|
|
oldtopic = self.irc.channels[channel].topic
|
2015-09-06 03:00:57 +02:00
|
|
|
self.irc.channels[channel].topic = topic
|
|
|
|
self.irc.channels[channel].topicset = True
|
2016-01-10 05:50:38 +01:00
|
|
|
|
|
|
|
return {'channel': channel, 'setter': numeric, 'text': topic,
|
2015-09-13 23:23:09 +02:00
|
|
|
'oldtopic': oldtopic}
|
2015-09-04 20:24:40 +02:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
def handle_part(self, source, command, args):
|
|
|
|
"""Handles incoming PART commands."""
|
2016-05-01 01:57:38 +02:00
|
|
|
channels = self.irc.toLower(args[0]).split(',')
|
2015-09-06 03:00:57 +02:00
|
|
|
for channel in channels:
|
|
|
|
# We should only get PART commands for channels that exist, right??
|
|
|
|
self.irc.channels[channel].removeuser(source)
|
|
|
|
try:
|
|
|
|
self.irc.users[source].channels.discard(channel)
|
|
|
|
except KeyError:
|
|
|
|
log.debug("(%s) handle_part: KeyError trying to remove %r from %r's channel list?", self.irc.name, channel, source)
|
|
|
|
try:
|
|
|
|
reason = args[1]
|
|
|
|
except IndexError:
|
|
|
|
reason = ''
|
|
|
|
# Clear empty non-permanent channels.
|
|
|
|
if not (self.irc.channels[channel].users or ((self.irc.cmodes.get('permanent'), None) in self.irc.channels[channel].modes)):
|
|
|
|
del self.irc.channels[channel]
|
|
|
|
return {'channels': channels, 'text': reason}
|
2015-11-15 18:34:26 +01:00
|
|
|
|
|
|
|
def handle_away(self, numeric, command, args):
|
|
|
|
"""Handles incoming AWAY messages."""
|
|
|
|
# <- :6ELAAAAAB AWAY :Auto-away
|
|
|
|
try:
|
|
|
|
self.irc.users[numeric].away = text = args[0]
|
|
|
|
except IndexError: # User is unsetting away status
|
|
|
|
self.irc.users[numeric].away = text = ''
|
|
|
|
return {'text': text}
|
2016-03-26 19:27:07 +01:00
|
|
|
|
|
|
|
def handle_version(self, numeric, command, args):
|
|
|
|
"""Handles requests for the PyLink server version."""
|
|
|
|
return {} # See coreplugin.py for how this hook is used
|