2015-12-07 02:40:13 +01:00
|
|
|
"""
|
|
|
|
classes.py - Base classes for PyLink IRC Services.
|
|
|
|
|
|
|
|
This module contains the base classes used by PyLink, including threaded IRC
|
|
|
|
connections and objects used to represent IRC servers, users, and channels.
|
|
|
|
|
|
|
|
Here be dragons.
|
|
|
|
"""
|
|
|
|
|
2015-07-18 07:52:55 +02:00
|
|
|
import threading
|
2015-08-26 05:37:15 +02:00
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
import ssl
|
|
|
|
import hashlib
|
2015-09-13 22:47:04 +02:00
|
|
|
from copy import deepcopy
|
2016-05-01 01:54:11 +02:00
|
|
|
import inspect
|
2016-07-21 08:47:31 +02:00
|
|
|
import re
|
2017-04-01 02:41:56 +02:00
|
|
|
from collections import defaultdict
|
2017-02-25 07:27:11 +01:00
|
|
|
import ipaddress
|
2017-04-01 02:41:56 +02:00
|
|
|
import queue
|
2015-07-08 03:07:20 +02:00
|
|
|
|
2016-07-20 02:44:22 +02:00
|
|
|
try:
|
|
|
|
import ircmatch
|
|
|
|
except ImportError:
|
2016-12-10 02:15:53 +01:00
|
|
|
raise ImportError("PyLink requires ircmatch to function; please install it and try again.")
|
2016-07-07 08:11:36 +02:00
|
|
|
|
2017-03-05 09:00:11 +01:00
|
|
|
from . import world, utils, structures, conf, __version__
|
2016-06-21 03:18:54 +02:00
|
|
|
from .log import *
|
2015-08-26 05:37:15 +02:00
|
|
|
|
|
|
|
### Exceptions
|
|
|
|
|
2017-03-26 23:13:26 +02:00
|
|
|
class ProtocolError(RuntimeError):
|
2015-08-26 05:37:15 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
### Internal classes (users, servers, channels)
|
|
|
|
|
2017-02-25 07:28:26 +01:00
|
|
|
class Irc(utils.DeprecatedAttributesObject):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Base IRC object for PyLink."""
|
|
|
|
|
|
|
|
def __init__(self, netname, proto, conf):
|
|
|
|
"""
|
|
|
|
Initializes an IRC object. This takes 3 variables: the network name
|
|
|
|
(a string), the name of the protocol module to use for this connection,
|
|
|
|
and a configuration object.
|
|
|
|
"""
|
2017-03-05 09:14:43 +01:00
|
|
|
self.deprecated_attributes = {
|
|
|
|
'conf': 'Deprecated since 1.2; consider switching to conf.conf',
|
|
|
|
'botdata': "Deprecated since 1.2; consider switching to conf.conf['bot']",
|
|
|
|
}
|
2017-02-25 07:28:26 +01:00
|
|
|
|
2016-01-31 08:33:03 +01:00
|
|
|
self.loghandlers = []
|
2016-04-02 21:10:56 +02:00
|
|
|
self.name = netname
|
2015-12-07 02:40:13 +01:00
|
|
|
self.conf = conf
|
2016-07-29 07:22:47 +02:00
|
|
|
self.sid = None
|
2015-12-07 02:40:13 +01:00
|
|
|
self.serverdata = conf['servers'][netname]
|
|
|
|
self.botdata = conf['bot']
|
2016-07-03 09:12:23 +02:00
|
|
|
self.protoname = proto.__name__.split('.')[-1] # Remove leading pylinkirc.protocols.
|
2015-12-07 02:40:13 +01:00
|
|
|
self.proto = proto.Class(self)
|
2016-06-25 03:13:30 +02:00
|
|
|
self.pingfreq = self.serverdata.get('pingfreq') or 90
|
|
|
|
self.pingtimeout = self.pingfreq * 2
|
2015-12-07 02:40:13 +01:00
|
|
|
|
2017-04-01 02:41:56 +02:00
|
|
|
self.queue = None
|
2016-08-22 01:46:57 +02:00
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
self.connected = threading.Event()
|
|
|
|
self.aborted = threading.Event()
|
2017-04-01 01:25:28 +02:00
|
|
|
self.reply_lock = threading.RLock()
|
2015-12-07 02:40:13 +01:00
|
|
|
|
2016-06-21 19:54:07 +02:00
|
|
|
self.pingTimer = None
|
|
|
|
|
2017-03-11 09:21:30 +01:00
|
|
|
# Sets the multiplier for autoconnect delay (grows with time).
|
|
|
|
self.autoconnect_active_multiplier = 1
|
|
|
|
|
2016-03-25 22:54:29 +01:00
|
|
|
self.initVars()
|
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
if world.testing:
|
|
|
|
# HACK: Don't thread if we're running tests.
|
|
|
|
self.connect()
|
|
|
|
else:
|
2016-01-10 04:38:27 +01:00
|
|
|
self.connection_thread = threading.Thread(target=self.connect,
|
|
|
|
name="Listener for %s" %
|
|
|
|
self.name)
|
2015-12-07 02:40:13 +01:00
|
|
|
self.connection_thread.start()
|
|
|
|
|
2016-01-23 22:13:38 +01:00
|
|
|
def logSetup(self):
|
|
|
|
"""
|
|
|
|
Initializes any channel loggers defined for the current network.
|
|
|
|
"""
|
|
|
|
try:
|
2017-03-05 09:00:11 +01:00
|
|
|
channels = conf.conf['logging']['channels'][self.name]
|
2016-01-23 22:13:38 +01:00
|
|
|
except KeyError: # Not set up; just ignore.
|
|
|
|
return
|
|
|
|
|
|
|
|
log.debug('(%s) Setting up channel logging to channels %r', self.name,
|
|
|
|
channels)
|
2016-01-31 08:33:03 +01:00
|
|
|
|
|
|
|
if not self.loghandlers:
|
|
|
|
# Only create handlers if they haven't already been set up.
|
|
|
|
|
|
|
|
for channel, chandata in channels.items():
|
|
|
|
# Fetch the log level for this channel block.
|
|
|
|
level = None
|
|
|
|
if chandata is not None:
|
|
|
|
level = chandata.get('loglevel')
|
|
|
|
|
|
|
|
handler = PyLinkChannelLogger(self, channel, level=level)
|
|
|
|
self.loghandlers.append(handler)
|
|
|
|
log.addHandler(handler)
|
2016-01-23 22:13:38 +01:00
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
def initVars(self):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""
|
|
|
|
(Re)sets an IRC object to its default state. This should be called when
|
|
|
|
an IRC object is first created, and on every reconnection to a network.
|
|
|
|
"""
|
2016-06-25 03:13:30 +02:00
|
|
|
self.pingfreq = self.serverdata.get('pingfreq') or 90
|
2016-06-06 16:34:48 +02:00
|
|
|
self.pingtimeout = self.pingfreq * 3
|
2015-10-03 08:07:57 +02:00
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
self.pseudoclient = None
|
|
|
|
self.lastping = time.time()
|
2015-09-03 07:05:47 +02:00
|
|
|
|
2017-04-01 02:41:56 +02:00
|
|
|
self.maxsendq = self.serverdata.get('maxsendq', 4096)
|
|
|
|
self.queue = queue.Queue(self.maxsendq)
|
2016-08-22 01:46:57 +02:00
|
|
|
|
2016-07-01 03:22:45 +02:00
|
|
|
# Internal variable to set the place and caller of the last command (in PM
|
2015-09-26 18:05:44 +02:00
|
|
|
# or in a channel), used by fantasy command support.
|
|
|
|
self.called_by = None
|
2016-07-01 03:22:45 +02:00
|
|
|
self.called_in = None
|
2015-09-26 18:05:44 +02:00
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
# Intialize the server, channel, and user indexes to be populated by
|
|
|
|
# our protocol module. For the server index, we can add ourselves right
|
|
|
|
# now.
|
2016-07-29 06:49:16 +02:00
|
|
|
self.servers = {}
|
2015-08-26 05:37:15 +02:00
|
|
|
self.users = {}
|
2016-04-25 06:11:36 +02:00
|
|
|
self.channels = structures.KeyedDefaultdict(IrcChannel)
|
2015-12-07 02:40:13 +01:00
|
|
|
|
|
|
|
# This sets the list of supported channel and user modes: the default
|
|
|
|
# RFC1459 modes are implied. Named modes are used here to make
|
|
|
|
# protocol-independent code easier to write, as mode chars vary by
|
|
|
|
# IRCd.
|
|
|
|
# Protocol modules should add to and/or replace this with what their
|
|
|
|
# protocol supports. This can be a hardcoded list or something
|
|
|
|
# negotiated on connect, depending on the nature of their protocol.
|
2015-08-26 05:37:15 +02:00
|
|
|
self.cmodes = {'op': 'o', 'secret': 's', 'private': 'p',
|
|
|
|
'noextmsg': 'n', 'moderated': 'm', 'inviteonly': 'i',
|
|
|
|
'topiclock': 't', 'limit': 'l', 'ban': 'b',
|
|
|
|
'voice': 'v', 'key': 'k',
|
2015-12-07 02:40:13 +01:00
|
|
|
# This fills in the type of mode each mode character is.
|
|
|
|
# A-type modes are list modes (i.e. bans, ban exceptions, etc.),
|
|
|
|
# B-type modes require an argument to both set and unset,
|
|
|
|
# but there can only be one value at a time
|
|
|
|
# (i.e. cmode +k).
|
|
|
|
# C-type modes require an argument to set but not to unset
|
|
|
|
# (one sets "+l limit" and # "-l"),
|
|
|
|
# and D-type modes take no arguments at all.
|
2015-08-26 05:37:15 +02:00
|
|
|
'*A': 'b',
|
|
|
|
'*B': 'k',
|
|
|
|
'*C': 'l',
|
|
|
|
'*D': 'imnpstr'}
|
|
|
|
self.umodes = {'invisible': 'i', 'snomask': 's', 'wallops': 'w',
|
|
|
|
'oper': 'o',
|
2016-08-12 03:11:41 +02:00
|
|
|
'*A': '', '*B': '', '*C': '', '*D': 'iosw'}
|
2015-08-26 05:37:15 +02:00
|
|
|
|
|
|
|
# This max nick length starts off as the config value, but may be
|
|
|
|
# overwritten later by the protocol module if such information is
|
2016-06-28 07:39:18 +02:00
|
|
|
# received. It defaults to 30.
|
|
|
|
self.maxnicklen = self.serverdata.get('maxnicklen', 30)
|
2015-12-07 02:40:13 +01:00
|
|
|
|
|
|
|
# Defines a list of supported prefix modes.
|
2015-08-26 05:37:15 +02:00
|
|
|
self.prefixmodes = {'o': '@', 'v': '+'}
|
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
# Defines the uplink SID (to be filled in by protocol module).
|
2015-08-26 05:37:15 +02:00
|
|
|
self.uplink = None
|
|
|
|
self.start_ts = int(time.time())
|
|
|
|
|
2016-01-23 22:13:38 +01:00
|
|
|
# Set up channel logging for the network
|
|
|
|
self.logSetup()
|
|
|
|
|
2016-08-22 01:46:57 +02:00
|
|
|
def processQueue(self):
|
|
|
|
"""Loop to process outgoing queue data."""
|
|
|
|
while not self.aborted.is_set():
|
2017-03-29 07:38:54 +02:00
|
|
|
throttle_time = self.serverdata.get('throttle_time', 0.005)
|
2017-04-01 02:41:56 +02:00
|
|
|
data = self.queue.get(throttle_time)
|
|
|
|
if data:
|
|
|
|
self._send(data)
|
2017-03-25 02:40:02 +01:00
|
|
|
log.debug('(%s) Stopping queue thread as aborted is set', self.name)
|
2016-08-22 01:46:57 +02:00
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
def connect(self):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""
|
|
|
|
Runs the connect loop for the IRC object. This is usually called by
|
|
|
|
__init__ in a separate thread to allow multiple concurrent connections.
|
|
|
|
"""
|
2015-08-26 05:37:15 +02:00
|
|
|
while True:
|
2016-11-03 06:34:02 +01:00
|
|
|
|
2016-07-13 09:38:19 +02:00
|
|
|
self.aborted.clear()
|
2016-03-26 01:14:16 +01:00
|
|
|
self.initVars()
|
2016-07-29 06:34:00 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.proto.validateServerConf()
|
|
|
|
except AssertionError as e:
|
|
|
|
log.exception("(%s) Configuration error: %s", self.name, e)
|
|
|
|
return
|
|
|
|
|
2015-10-03 08:07:57 +02:00
|
|
|
ip = self.serverdata["ip"]
|
|
|
|
port = self.serverdata["port"]
|
2015-08-26 05:37:15 +02:00
|
|
|
checks_ok = True
|
|
|
|
try:
|
2015-12-07 02:40:13 +01:00
|
|
|
# Set the socket type (IPv6 or IPv4).
|
2015-10-03 08:17:57 +02:00
|
|
|
stype = socket.AF_INET6 if self.serverdata.get("ipv6") else socket.AF_INET
|
2015-12-07 02:40:13 +01:00
|
|
|
|
|
|
|
# Creat the socket.
|
2015-10-03 08:17:57 +02:00
|
|
|
self.socket = socket.socket(stype)
|
2015-08-26 05:37:15 +02:00
|
|
|
self.socket.setblocking(0)
|
2015-12-07 02:40:13 +01:00
|
|
|
|
2017-01-02 21:30:24 +01:00
|
|
|
# Set the socket bind if applicable.
|
|
|
|
if 'bindhost' in self.serverdata:
|
|
|
|
self.socket.bind((self.serverdata['bindhost'], 0))
|
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
# Set the connection timeouts. Initial connection timeout is a
|
|
|
|
# lot smaller than the timeout after we've connected; this is
|
|
|
|
# intentional.
|
2015-08-26 05:37:15 +02:00
|
|
|
self.socket.settimeout(self.pingfreq)
|
2015-12-07 02:40:13 +01:00
|
|
|
|
2016-06-11 20:29:11 +02:00
|
|
|
# Resolve hostnames if it's not an IP address already.
|
|
|
|
old_ip = ip
|
|
|
|
ip = socket.getaddrinfo(ip, port, stype)[0][-1][0]
|
|
|
|
log.debug('(%s) Resolving address %s to %s', self.name, old_ip, ip)
|
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
# Enable SSL if set to do so. This requires a valid keyfile and
|
|
|
|
# certfile to be present.
|
2015-08-26 05:37:15 +02:00
|
|
|
self.ssl = self.serverdata.get('ssl')
|
|
|
|
if self.ssl:
|
|
|
|
log.info('(%s) Attempting SSL for this connection...', self.name)
|
|
|
|
certfile = self.serverdata.get('ssl_certfile')
|
|
|
|
keyfile = self.serverdata.get('ssl_keyfile')
|
2016-07-29 07:49:05 +02:00
|
|
|
|
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
|
|
|
|
# Disable SSLv2 and SSLv3 - these are insecure
|
|
|
|
context.options |= ssl.OP_NO_SSLv2
|
|
|
|
context.options |= ssl.OP_NO_SSLv3
|
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
if certfile and keyfile:
|
|
|
|
try:
|
2016-06-26 19:02:27 +02:00
|
|
|
context.load_cert_chain(certfile, keyfile)
|
2015-08-26 05:37:15 +02:00
|
|
|
except OSError:
|
|
|
|
log.exception('(%s) Caught OSError trying to '
|
|
|
|
'initialize the SSL connection; '
|
|
|
|
'are "ssl_certfile" and '
|
|
|
|
'"ssl_keyfile" set correctly?',
|
|
|
|
self.name)
|
|
|
|
checks_ok = False
|
2016-07-29 07:49:05 +02:00
|
|
|
|
|
|
|
self.socket = context.wrap_socket(self.socket)
|
2015-12-07 02:40:13 +01:00
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
log.info("Connecting to network %r on %s:%s", self.name, ip, port)
|
|
|
|
self.socket.connect((ip, port))
|
|
|
|
self.socket.settimeout(self.pingtimeout)
|
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
# If SSL was enabled, optionally verify the certificate
|
|
|
|
# fingerprint for some added security. I don't bother to check
|
|
|
|
# the entire certificate for validity, since most IRC networks
|
|
|
|
# self-sign their certificates anyways.
|
2015-08-26 05:37:15 +02:00
|
|
|
if self.ssl and checks_ok:
|
|
|
|
peercert = self.socket.getpeercert(binary_form=True)
|
2016-01-31 08:04:13 +01:00
|
|
|
|
|
|
|
# Hash type is configurable using the ssl_fingerprint_type
|
|
|
|
# value, and defaults to sha256.
|
|
|
|
hashtype = self.serverdata.get('ssl_fingerprint_type', 'sha256').lower()
|
|
|
|
|
|
|
|
try:
|
|
|
|
hashfunc = getattr(hashlib, hashtype)
|
|
|
|
except AttributeError:
|
|
|
|
log.error('(%s) Unsupported SSL certificate fingerprint type %r given, disconnecting...',
|
|
|
|
self.name, hashtype)
|
|
|
|
checks_ok = False
|
2015-08-26 05:37:15 +02:00
|
|
|
else:
|
2016-01-31 08:04:13 +01:00
|
|
|
fp = hashfunc(peercert).hexdigest()
|
|
|
|
expected_fp = self.serverdata.get('ssl_fingerprint')
|
|
|
|
|
|
|
|
if expected_fp and checks_ok:
|
|
|
|
if fp != expected_fp:
|
|
|
|
# SSL Fingerprint doesn't match; break.
|
|
|
|
log.error('(%s) Uplink\'s SSL certificate '
|
|
|
|
'fingerprint (%s) does not match the '
|
|
|
|
'one configured: expected %r, got %r; '
|
|
|
|
'disconnecting...', self.name, hashtype,
|
|
|
|
expected_fp, fp)
|
|
|
|
checks_ok = False
|
|
|
|
else:
|
|
|
|
log.info('(%s) Uplink SSL certificate fingerprint '
|
|
|
|
'(%s) verified: %r', self.name, hashtype,
|
|
|
|
fp)
|
|
|
|
else:
|
2016-03-08 05:30:24 +01:00
|
|
|
log.info('(%s) Uplink\'s SSL certificate fingerprint (%s) '
|
2016-01-31 08:04:13 +01:00
|
|
|
'is %r. You can enhance the security of your '
|
|
|
|
'link by specifying this in a "ssl_fingerprint"'
|
|
|
|
' option in your server block.', self.name,
|
|
|
|
hashtype, fp)
|
2015-08-26 05:37:15 +02:00
|
|
|
|
|
|
|
if checks_ok:
|
2016-08-22 01:46:57 +02:00
|
|
|
|
|
|
|
self.queue_thread = threading.Thread(name="Queue thread for %s" % self.name,
|
|
|
|
target=self.processQueue, daemon=True)
|
|
|
|
self.queue_thread.start()
|
|
|
|
|
2016-07-29 07:22:47 +02:00
|
|
|
self.sid = self.serverdata.get("sid")
|
|
|
|
# All our checks passed, get the protocol module to connect and run the listen
|
|
|
|
# loop. This also updates any SID values should the protocol module do so.
|
2015-09-06 03:00:57 +02:00
|
|
|
self.proto.connect()
|
2016-07-29 06:49:16 +02:00
|
|
|
|
|
|
|
log.info('(%s) Enumerating our own SID %s', self.name, self.sid)
|
2016-08-13 04:18:56 +02:00
|
|
|
host = self.hostname()
|
2016-07-29 07:22:47 +02:00
|
|
|
|
|
|
|
self.servers[self.sid] = IrcServer(None, host, internal=True,
|
|
|
|
desc=self.serverdata.get('serverdesc')
|
2017-03-05 09:00:11 +01:00
|
|
|
or conf.conf['bot']['serverdesc'])
|
2016-07-29 06:49:16 +02:00
|
|
|
|
2016-01-06 04:35:16 +01:00
|
|
|
log.info('(%s) Starting ping schedulers....', self.name)
|
|
|
|
self.schedulePing()
|
2015-08-26 05:37:15 +02:00
|
|
|
log.info('(%s) Server ready; listening for data.', self.name)
|
2017-03-11 09:21:30 +01:00
|
|
|
self.autoconnect_active_multiplier = 1 # Reset any extra autoconnect delays
|
2015-08-26 05:37:15 +02:00
|
|
|
self.run()
|
2015-12-07 02:40:13 +01:00
|
|
|
else: # Configuration error :(
|
2015-08-26 05:37:15 +02:00
|
|
|
log.error('(%s) A configuration error was encountered '
|
|
|
|
'trying to set up this connection. Please check'
|
|
|
|
' your configuration file and try again.',
|
|
|
|
self.name)
|
2017-03-26 23:25:02 +02:00
|
|
|
# self.run() or the protocol module it called raised an exception, meaning we've disconnected!
|
2017-03-26 23:13:26 +02:00
|
|
|
# Note: socket.error, ConnectionError, IOError, etc. are included in OSError since Python 3.3,
|
|
|
|
# so we don't need to explicitly catch them here.
|
|
|
|
# We also catch SystemExit here as a way to abort out connection threads properly, and stop the
|
|
|
|
# IRC connection from freezing instead.
|
|
|
|
except (OSError, RuntimeError, SystemExit) as e:
|
2016-04-26 04:14:34 +02:00
|
|
|
log.error('(%s) Disconnected from IRC: %s: %s',
|
|
|
|
self.name, type(e).__name__, str(e))
|
2016-01-10 05:23:23 +01:00
|
|
|
|
2016-06-15 19:55:47 +02:00
|
|
|
self.disconnect()
|
2015-12-07 02:40:13 +01:00
|
|
|
|
|
|
|
# If autoconnect is enabled, loop back to the start. Otherwise,
|
|
|
|
# return and stop.
|
2015-08-26 05:37:15 +02:00
|
|
|
autoconnect = self.serverdata.get('autoconnect')
|
2017-03-11 09:21:30 +01:00
|
|
|
|
|
|
|
# Sets the autoconnect growth multiplier (e.g. a value of 2 multiplies the autoconnect
|
|
|
|
# time by 2 on every failure, etc.)
|
|
|
|
autoconnect_multiplier = self.serverdata.get('autoconnect_multiplier', 2)
|
|
|
|
autoconnect_max = self.serverdata.get('autoconnect_max', 1800)
|
|
|
|
# These values must at least be 1.
|
|
|
|
autoconnect_multiplier = max(autoconnect_multiplier, 1)
|
|
|
|
autoconnect_max = max(autoconnect_max, 1)
|
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
log.debug('(%s) Autoconnect delay set to %s seconds.', self.name, autoconnect)
|
2015-10-03 08:20:59 +02:00
|
|
|
if autoconnect is not None and autoconnect >= 1:
|
2017-03-11 09:21:30 +01:00
|
|
|
log.debug('(%s) Multiplying autoconnect delay %s by %s.', self.name, autoconnect, self.autoconnect_active_multiplier)
|
|
|
|
autoconnect *= self.autoconnect_active_multiplier
|
|
|
|
# Add a cap on the max. autoconnect delay, so that we don't go on forever...
|
|
|
|
autoconnect = min(autoconnect, autoconnect_max)
|
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
log.info('(%s) Going to auto-reconnect in %s seconds.', self.name, autoconnect)
|
2017-03-11 08:55:29 +01:00
|
|
|
# Continue when either self.aborted is set or the autoconnect time passes.
|
|
|
|
# Compared to time.sleep(), this allows us to stop connections quicker if we
|
|
|
|
# break while while for autoconnect.
|
|
|
|
self.aborted.clear()
|
|
|
|
self.aborted.wait(autoconnect)
|
2016-11-03 06:34:02 +01:00
|
|
|
|
2017-03-11 09:21:30 +01:00
|
|
|
# Store in the local state what the autoconnect multiplier currently is.
|
|
|
|
self.autoconnect_active_multiplier *= autoconnect_multiplier
|
|
|
|
|
2016-11-03 06:34:02 +01:00
|
|
|
if self not in world.networkobjects.values():
|
|
|
|
log.debug('Stopping stale connect loop for old connection %r', self.name)
|
|
|
|
return
|
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
else:
|
2015-10-03 08:20:59 +02:00
|
|
|
log.info('(%s) Stopping connect loop (autoconnect value %r is < 1).', self.name, autoconnect)
|
2015-08-26 05:37:15 +02:00
|
|
|
return
|
|
|
|
|
2016-01-10 04:38:27 +01:00
|
|
|
def disconnect(self):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Handle disconnects from the remote server."""
|
2017-02-26 07:06:43 +01:00
|
|
|
was_successful = self.connected.is_set()
|
|
|
|
log.debug('(%s) disconnect: got %s for was_successful state', self.name, was_successful)
|
2016-01-10 04:38:27 +01:00
|
|
|
|
2016-06-15 19:55:47 +02:00
|
|
|
log.debug('(%s) disconnect: Clearing self.connected state.', self.name)
|
2015-08-26 05:37:15 +02:00
|
|
|
self.connected.clear()
|
2016-01-10 04:38:27 +01:00
|
|
|
|
2016-01-31 08:33:03 +01:00
|
|
|
log.debug('(%s) Removing channel logging handlers due to disconnect.', self.name)
|
|
|
|
while self.loghandlers:
|
|
|
|
log.removeHandler(self.loghandlers.pop())
|
2016-01-23 22:13:38 +01:00
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
try:
|
2016-06-15 19:55:47 +02:00
|
|
|
log.debug('(%s) disconnect: Shutting down socket.', self.name)
|
2016-01-10 04:38:27 +01:00
|
|
|
self.socket.shutdown(socket.SHUT_RDWR)
|
2015-08-26 05:37:15 +02:00
|
|
|
except: # Socket timed out during creation; ignore
|
2016-03-26 01:03:25 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
self.socket.close()
|
2015-08-26 05:37:15 +02:00
|
|
|
|
2016-01-10 04:38:27 +01:00
|
|
|
if self.pingTimer:
|
|
|
|
log.debug('(%s) Canceling pingTimer at %s due to disconnect() call', self.name, time.time())
|
|
|
|
self.pingTimer.cancel()
|
2015-09-03 07:05:47 +02:00
|
|
|
|
2016-06-15 19:55:47 +02:00
|
|
|
log.debug('(%s) disconnect: Setting self.aborted to True.', self.name)
|
|
|
|
self.aborted.set()
|
|
|
|
|
2017-02-26 07:06:43 +01:00
|
|
|
# Internal hook signifying that a network has disconnected.
|
|
|
|
self.callHooks([None, 'PYLINK_DISCONNECT', {'was_successful': was_successful}])
|
|
|
|
|
2016-06-15 19:55:47 +02:00
|
|
|
log.debug('(%s) disconnect: Clearing state via initVars().', self.name)
|
|
|
|
self.initVars()
|
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
def run(self):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Main IRC loop which listens for messages."""
|
|
|
|
# Some magic below cause this to work, though anything that's
|
|
|
|
# not encoded in UTF-8 doesn't work very well.
|
2015-08-26 05:37:15 +02:00
|
|
|
buf = b""
|
|
|
|
data = b""
|
2015-09-03 07:05:47 +02:00
|
|
|
while not self.aborted.is_set():
|
2016-01-10 05:24:46 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
data = self.socket.recv(2048)
|
|
|
|
except OSError:
|
|
|
|
# Suppress socket read warnings from lingering recv() calls if
|
|
|
|
# we've been told to shutdown.
|
|
|
|
if self.aborted.is_set():
|
|
|
|
return
|
|
|
|
raise
|
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
buf += data
|
2015-12-29 20:23:06 +01:00
|
|
|
if not data:
|
2016-04-26 04:14:34 +02:00
|
|
|
log.error('(%s) No data received, disconnecting!', self.name)
|
2015-08-26 05:37:15 +02:00
|
|
|
return
|
|
|
|
elif (time.time() - self.lastping) > self.pingtimeout:
|
2016-04-26 04:14:34 +02:00
|
|
|
log.error('(%s) Connection timed out.', self.name)
|
2015-08-26 05:37:15 +02:00
|
|
|
return
|
|
|
|
while b'\n' in buf:
|
|
|
|
line, buf = buf.split(b'\n', 1)
|
|
|
|
line = line.strip(b'\r')
|
2015-09-03 07:05:47 +02:00
|
|
|
# FIXME: respect other encodings?
|
2015-08-26 05:37:15 +02:00
|
|
|
line = line.decode("utf-8", "replace")
|
2015-09-19 19:31:43 +02:00
|
|
|
self.runline(line)
|
|
|
|
|
|
|
|
def runline(self, line):
|
|
|
|
"""Sends a command to the protocol module."""
|
|
|
|
log.debug("(%s) <- %s", self.name, line)
|
|
|
|
try:
|
|
|
|
hook_args = self.proto.handle_events(line)
|
|
|
|
except Exception:
|
|
|
|
log.exception('(%s) Caught error in handle_events, disconnecting!', self.name)
|
|
|
|
log.error('(%s) The offending line was: <- %s', self.name, line)
|
2015-10-10 06:35:42 +02:00
|
|
|
self.aborted.set()
|
2015-09-19 19:31:43 +02:00
|
|
|
return
|
|
|
|
# Only call our hooks if there's data to process. Handlers that support
|
|
|
|
# hooks will return a dict of parsed arguments, which can be passed on
|
|
|
|
# to plugins and the like. For example, the JOIN handler will return
|
|
|
|
# something like: {'channel': '#whatever', 'users': ['UID1', 'UID2',
|
|
|
|
# 'UID3']}, etc.
|
|
|
|
if hook_args is not None:
|
|
|
|
self.callHooks(hook_args)
|
2015-08-26 05:37:15 +02:00
|
|
|
|
2015-12-22 19:46:34 +01:00
|
|
|
return hook_args
|
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
def callHooks(self, hook_args):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Calls a hook function with the given hook args."""
|
2015-08-26 05:37:15 +02:00
|
|
|
numeric, command, parsed_args = hook_args
|
|
|
|
# Always make sure TS is sent.
|
|
|
|
if 'ts' not in parsed_args:
|
|
|
|
parsed_args['ts'] = int(time.time())
|
|
|
|
hook_cmd = command
|
|
|
|
hook_map = self.proto.hook_map
|
2015-12-27 01:43:40 +01:00
|
|
|
|
|
|
|
# If the hook name is present in the protocol module's hook_map, then we
|
|
|
|
# should set the hook name to the name that points to instead.
|
|
|
|
# For example, plugins will read SETHOST as CHGHOST, EOS (end of sync)
|
|
|
|
# as ENDBURST, etc.
|
2015-08-26 05:37:15 +02:00
|
|
|
if command in hook_map:
|
|
|
|
hook_cmd = hook_map[command]
|
2015-12-27 01:43:40 +01:00
|
|
|
|
|
|
|
# However, individual handlers can also return a 'parse_as' key to send
|
|
|
|
# their payload to a different hook. An example of this is "/join 0"
|
|
|
|
# being interpreted as leaving all channels (PART).
|
2015-08-26 05:37:15 +02:00
|
|
|
hook_cmd = parsed_args.get('parse_as') or hook_cmd
|
2015-12-27 01:43:40 +01:00
|
|
|
|
|
|
|
log.debug('(%s) Raw hook data: [%r, %r, %r] received from %s handler '
|
|
|
|
'(calling hook %s)', self.name, numeric, hook_cmd, parsed_args,
|
|
|
|
command, hook_cmd)
|
|
|
|
|
|
|
|
# Iterate over registered hook functions, catching errors accordingly.
|
2015-09-27 19:53:25 +02:00
|
|
|
for hook_func in world.hooks[hook_cmd]:
|
2015-08-26 05:37:15 +02:00
|
|
|
try:
|
2015-09-26 18:55:44 +02:00
|
|
|
log.debug('(%s) Calling hook function %s from plugin "%s"', self.name,
|
|
|
|
hook_func, hook_func.__module__)
|
2015-08-26 05:37:15 +02:00
|
|
|
hook_func(self, numeric, command, parsed_args)
|
|
|
|
except Exception:
|
|
|
|
# We don't want plugins to crash our servers...
|
2015-11-08 19:56:09 +01:00
|
|
|
log.exception('(%s) Unhandled exception caught in hook %r from plugin "%s"',
|
|
|
|
self.name, hook_func, hook_func.__module__)
|
|
|
|
log.error('(%s) The offending hook data was: %s', self.name,
|
|
|
|
hook_args)
|
2015-08-26 05:37:15 +02:00
|
|
|
continue
|
|
|
|
|
2016-08-22 01:46:57 +02:00
|
|
|
def _send(self, data):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Sends raw text to the uplink server."""
|
2015-08-26 05:37:15 +02:00
|
|
|
# Safeguard against newlines in input!! Otherwise, each line gets
|
|
|
|
# treated as a separate command, which is particularly nasty.
|
|
|
|
data = data.replace('\n', ' ')
|
|
|
|
data = data.encode("utf-8") + b"\n"
|
|
|
|
stripped_data = data.decode("utf-8").strip("\n")
|
|
|
|
log.debug("(%s) -> %s", self.name, stripped_data)
|
2016-08-22 01:46:57 +02:00
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
try:
|
|
|
|
self.socket.send(data)
|
|
|
|
except (OSError, AttributeError):
|
|
|
|
log.debug("(%s) Dropping message %r; network isn't connected!", self.name, stripped_data)
|
|
|
|
|
2016-08-22 01:46:57 +02:00
|
|
|
def send(self, data, queue=True):
|
|
|
|
"""send() wrapper with optional queueing support."""
|
|
|
|
if queue:
|
2017-04-01 02:41:56 +02:00
|
|
|
# XXX: we don't really know how to handle blocking queues yet, so
|
|
|
|
# it's better to not expose that yet.
|
|
|
|
self.queue.put_nowait(data)
|
2016-08-22 01:46:57 +02:00
|
|
|
else:
|
|
|
|
self._send(data)
|
|
|
|
|
2015-08-26 05:37:15 +02:00
|
|
|
def schedulePing(self):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Schedules periodic pings in a loop."""
|
2016-01-17 02:11:23 +01:00
|
|
|
self.proto.ping()
|
2016-01-10 04:38:27 +01:00
|
|
|
|
2016-01-06 04:35:16 +01:00
|
|
|
self.pingTimer = threading.Timer(self.pingfreq, self.schedulePing)
|
|
|
|
self.pingTimer.daemon = True
|
2016-01-10 04:38:27 +01:00
|
|
|
self.pingTimer.name = 'Ping timer loop for %s' % self.name
|
2016-01-06 04:35:16 +01:00
|
|
|
self.pingTimer.start()
|
2016-01-10 04:38:27 +01:00
|
|
|
|
2016-01-06 04:35:16 +01:00
|
|
|
log.debug('(%s) Ping scheduled at %s', self.name, time.time())
|
2015-08-26 05:37:15 +02:00
|
|
|
|
2015-09-29 04:25:45 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return "<classes.Irc object for %r>" % self.name
|
|
|
|
|
2016-04-25 06:37:23 +02:00
|
|
|
### General utility functions
|
2016-01-01 02:28:47 +01:00
|
|
|
def callCommand(self, source, text):
|
|
|
|
"""
|
|
|
|
Calls a PyLink bot command. source is the caller's UID, and text is the
|
|
|
|
full, unparsed text of the message.
|
|
|
|
"""
|
2016-05-14 22:26:13 +02:00
|
|
|
world.services['pylink'].call_cmd(self, source, text)
|
2016-01-01 02:28:47 +01:00
|
|
|
|
2017-02-25 01:28:01 +01:00
|
|
|
def msg(self, target, text, notice=None, source=None, loopback=True):
|
2016-01-01 02:28:47 +01:00
|
|
|
"""Handy function to send messages/notices to clients. Source
|
|
|
|
is optional, and defaults to the main PyLink client if not specified."""
|
2016-08-27 18:50:53 +02:00
|
|
|
if not text:
|
|
|
|
return
|
|
|
|
|
2017-03-11 08:19:43 +01:00
|
|
|
if not (source or self.pseudoclient):
|
|
|
|
# No explicit source set and our main client wasn't available; abort.
|
|
|
|
return
|
2016-01-01 02:28:47 +01:00
|
|
|
source = source or self.pseudoclient.uid
|
2016-08-27 18:50:53 +02:00
|
|
|
|
2016-01-01 02:28:47 +01:00
|
|
|
if notice:
|
2016-01-17 01:44:23 +01:00
|
|
|
self.proto.notice(source, target, text)
|
2016-01-01 02:28:47 +01:00
|
|
|
cmd = 'PYLINK_SELF_NOTICE'
|
|
|
|
else:
|
2016-01-17 01:44:23 +01:00
|
|
|
self.proto.message(source, target, text)
|
2016-01-01 02:28:47 +01:00
|
|
|
cmd = 'PYLINK_SELF_PRIVMSG'
|
|
|
|
|
2016-09-01 03:28:13 +02:00
|
|
|
if loopback:
|
|
|
|
# Determines whether we should send a hook for this msg(), to relay things like services
|
|
|
|
# replies across relay.
|
|
|
|
self.callHooks([source, cmd, {'target': target, 'text': text}])
|
|
|
|
|
2017-04-01 01:25:28 +02:00
|
|
|
def _reply(self, text, notice=None, source=None, private=None, force_privmsg_in_private=False,
|
2016-09-01 03:28:13 +02:00
|
|
|
loopback=True):
|
2017-04-01 01:25:28 +02:00
|
|
|
"""
|
|
|
|
Core of the reply() function - replies to the last caller in the right context
|
|
|
|
(channel or PM).
|
|
|
|
"""
|
|
|
|
if private is None:
|
|
|
|
# Allow using private replies as the default, if no explicit setting was given.
|
|
|
|
private = conf.conf['bot'].get("prefer_private_replies")
|
|
|
|
|
|
|
|
# Private reply is enabled, or the caller was originally a PM
|
|
|
|
if private or (self.called_in in self.users):
|
|
|
|
if not force_privmsg_in_private:
|
|
|
|
# For private replies, the default is to override the notice=True/False argument,
|
|
|
|
# and send replies as notices regardless. This is standard behaviour for most
|
|
|
|
# IRC services, but can be disabled if force_privmsg_in_private is given.
|
|
|
|
notice = True
|
|
|
|
target = self.called_by
|
|
|
|
else:
|
|
|
|
target = self.called_in
|
2016-07-01 03:22:45 +02:00
|
|
|
|
2017-04-01 01:25:28 +02:00
|
|
|
self.msg(target, text, notice=notice, source=source, loopback=loopback)
|
2016-07-01 03:22:45 +02:00
|
|
|
|
2017-04-01 01:25:28 +02:00
|
|
|
def reply(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Replies to the last caller in the right context (channel or PM).
|
|
|
|
|
|
|
|
This function wraps around _reply() and can be monkey-patched in a thread-safe manner
|
|
|
|
to temporarily redirect plugin output to another target.
|
|
|
|
"""
|
|
|
|
with self.reply_lock:
|
|
|
|
self._reply(*args, **kwargs)
|
2016-01-01 02:28:47 +01:00
|
|
|
|
2017-02-25 01:28:01 +01:00
|
|
|
def error(self, text, **kwargs):
|
2016-11-19 06:55:03 +01:00
|
|
|
"""Replies with an error to the last caller in the right context (channel or PM)."""
|
|
|
|
# This is a stub to alias error to reply
|
2017-02-25 01:28:01 +01:00
|
|
|
self.reply("Error: %s" % text, **kwargs)
|
2016-12-10 02:15:53 +01:00
|
|
|
|
2016-05-01 01:57:38 +02:00
|
|
|
def toLower(self, text):
|
|
|
|
"""Returns a lowercase representation of text based on the IRC object's
|
|
|
|
casemapping (rfc1459 or ascii)."""
|
|
|
|
if self.proto.casemapping == 'rfc1459':
|
|
|
|
text = text.replace('{', '[')
|
|
|
|
text = text.replace('}', ']')
|
|
|
|
text = text.replace('|', '\\')
|
|
|
|
text = text.replace('~', '^')
|
2016-07-13 22:38:56 +02:00
|
|
|
# Encode the text as bytes first, and then lowercase it so that only ASCII characters are
|
|
|
|
# changed. Unicode in channel names, etc. is case sensitive because IRC is just that old of
|
|
|
|
# a protocol!!!
|
|
|
|
return text.encode().lower().decode()
|
2016-05-01 01:57:38 +02:00
|
|
|
|
2016-04-25 06:37:23 +02:00
|
|
|
def parseModes(self, target, args):
|
|
|
|
"""Parses a modestring list into a list of (mode, argument) tuples.
|
|
|
|
['+mitl-o', '3', 'person'] => [('+m', None), ('+i', None), ('+t', None), ('+l', '3'), ('-o', 'person')]
|
|
|
|
"""
|
|
|
|
# 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.
|
2016-06-25 22:08:49 +02:00
|
|
|
|
|
|
|
if type(args) == str:
|
|
|
|
# If the modestring was given as a string, split it into a list.
|
|
|
|
args = args.split()
|
|
|
|
|
2016-04-25 06:37:23 +02:00
|
|
|
assert args, 'No valid modes were supplied!'
|
|
|
|
usermodes = not utils.isChannel(target)
|
|
|
|
prefix = ''
|
|
|
|
modestring = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
if usermodes:
|
|
|
|
log.debug('(%s) Using self.umodes for this query: %s', self.name, self.umodes)
|
|
|
|
|
|
|
|
if target not in self.users:
|
2016-10-03 07:06:46 +02:00
|
|
|
log.debug('(%s) Possible desync! Mode target %s is not in the users index.', self.name, target)
|
2016-04-25 06:37:23 +02:00
|
|
|
return [] # Return an empty mode list
|
|
|
|
|
|
|
|
supported_modes = self.umodes
|
|
|
|
oldmodes = self.users[target].modes
|
|
|
|
else:
|
|
|
|
log.debug('(%s) Using self.cmodes for this query: %s', self.name, self.cmodes)
|
|
|
|
|
|
|
|
supported_modes = self.cmodes
|
|
|
|
oldmodes = self.channels[target].modes
|
|
|
|
res = []
|
|
|
|
for mode in modestring:
|
|
|
|
if mode in '+-':
|
|
|
|
prefix = mode
|
|
|
|
else:
|
|
|
|
if not prefix:
|
|
|
|
prefix = '+'
|
|
|
|
arg = None
|
|
|
|
log.debug('Current mode: %s%s; args left: %s', prefix, mode, args)
|
|
|
|
try:
|
2016-07-22 04:11:19 +02:00
|
|
|
if mode in self.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)
|
|
|
|
# Convert nicks to UIDs implicitly; most IRCds will want
|
|
|
|
# this already.
|
|
|
|
arg = self.nickToUid(arg) or arg
|
|
|
|
if arg not in self.users: # Target doesn't exist, skip it.
|
|
|
|
log.debug('(%s) Skipping setting mode "%s %s"; the '
|
|
|
|
'target doesn\'t seem to exist!', self.name,
|
|
|
|
mode, arg)
|
|
|
|
continue
|
|
|
|
elif mode in (supported_modes['*A'] + supported_modes['*B']):
|
2016-04-25 06:37:23 +02:00
|
|
|
# Must have parameter.
|
|
|
|
log.debug('Mode %s: This mode must have parameter.', mode)
|
|
|
|
arg = args.pop(0)
|
2016-10-08 05:38:45 +02:00
|
|
|
if prefix == '-':
|
2017-01-09 02:31:50 +01:00
|
|
|
if mode in supported_modes['*B'] and arg == '*':
|
|
|
|
# Charybdis allows unsetting +k without actually
|
|
|
|
# knowing the key by faking the argument when unsetting
|
|
|
|
# as a single "*".
|
|
|
|
# We'd need to know the real argument of +k for us to
|
|
|
|
# be able to unset the mode.
|
|
|
|
oldarg = dict(oldmodes).get(mode)
|
|
|
|
if oldarg:
|
|
|
|
# Set the arg to the old one on the channel.
|
|
|
|
arg = oldarg
|
|
|
|
log.debug("Mode %s: coersing argument of '*' to %r.", mode, arg)
|
|
|
|
|
2016-10-08 05:38:45 +02:00
|
|
|
log.debug('(%s) parseModes: checking if +%s %s is in old modes list: %s', self.name, mode, arg, oldmodes)
|
2017-01-09 02:31:50 +01:00
|
|
|
|
2016-10-08 05:38:45 +02:00
|
|
|
if (mode, arg) not in oldmodes:
|
|
|
|
# Ignore attempts to unset bans that don't exist.
|
|
|
|
log.debug("(%s) parseModes(): ignoring removal of non-existent list mode +%s %s", self.name, mode, arg)
|
|
|
|
continue
|
2017-01-09 02:31:50 +01:00
|
|
|
|
2016-04-25 06:37:23 +02:00
|
|
|
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)',
|
|
|
|
self.name, target, mode, modestring)
|
|
|
|
continue # Skip this mode; don't error out completely.
|
|
|
|
res.append((prefix + mode, arg))
|
|
|
|
return res
|
|
|
|
|
|
|
|
def applyModes(self, target, changedmodes):
|
|
|
|
"""Takes a list of parsed IRC modes, and applies them on the given target.
|
|
|
|
|
|
|
|
The target can be either a channel or a user; this is handled automatically."""
|
|
|
|
usermodes = not utils.isChannel(target)
|
|
|
|
log.debug('(%s) Using usermodes for this query? %s', self.name, usermodes)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if usermodes:
|
|
|
|
old_modelist = self.users[target].modes
|
|
|
|
supported_modes = self.umodes
|
|
|
|
else:
|
|
|
|
old_modelist = self.channels[target].modes
|
|
|
|
supported_modes = self.cmodes
|
|
|
|
except KeyError:
|
|
|
|
log.warning('(%s) Possible desync? Mode target %s is unknown.', self.name, target)
|
|
|
|
return
|
|
|
|
|
|
|
|
modelist = set(old_modelist)
|
|
|
|
log.debug('(%s) Applying modes %r on %s (initial modelist: %s)', self.name, changedmodes, target, modelist)
|
|
|
|
for mode in changedmodes:
|
|
|
|
# Chop off the +/- part that parseModes gives; it's meaningless for a mode list.
|
|
|
|
try:
|
|
|
|
real_mode = (mode[0][1], mode[1])
|
|
|
|
except IndexError:
|
|
|
|
real_mode = mode
|
|
|
|
|
|
|
|
if not usermodes:
|
|
|
|
# We only handle +qaohv for now. Iterate over every supported mode:
|
|
|
|
# if the IRCd supports this mode and it is the one being set, add/remove
|
|
|
|
# the person from the corresponding prefix mode list (e.g. c.prefixmodes['op']
|
|
|
|
# for ops).
|
|
|
|
for pmode, pmodelist in self.channels[target].prefixmodes.items():
|
|
|
|
if pmode in self.cmodes and real_mode[0] == self.cmodes[pmode]:
|
|
|
|
log.debug('(%s) Initial prefixmodes list: %s', self.name, pmodelist)
|
|
|
|
if mode[0][0] == '+':
|
|
|
|
pmodelist.add(mode[1])
|
|
|
|
else:
|
|
|
|
pmodelist.discard(mode[1])
|
|
|
|
|
|
|
|
log.debug('(%s) Final prefixmodes list: %s', self.name, pmodelist)
|
|
|
|
|
|
|
|
if real_mode[0] in self.prefixmodes:
|
|
|
|
# Don't add prefix modes to IrcChannel.modes; they belong in the
|
|
|
|
# prefixmodes mapping handled above.
|
|
|
|
log.debug('(%s) Not adding mode %s to IrcChannel.modes because '
|
|
|
|
'it\'s a prefix mode.', self.name, str(mode))
|
|
|
|
continue
|
|
|
|
|
2016-05-14 20:58:22 +02:00
|
|
|
if mode[0][0] != '-':
|
2016-04-25 06:37:23 +02:00
|
|
|
# We're adding a mode
|
|
|
|
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 self.cmodes['*A']:
|
|
|
|
# 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',
|
|
|
|
self.name, real_mode, target, str(existing))
|
|
|
|
[modelist.discard(oldmode) for oldmode in existing]
|
|
|
|
modelist.add(real_mode)
|
|
|
|
log.debug('(%s) Adding mode %r on %s', self.name, real_mode, target)
|
|
|
|
else:
|
|
|
|
log.debug('(%s) Removing mode %r on %s', self.name, real_mode, target)
|
|
|
|
# We're removing a mode
|
|
|
|
if real_mode[1] is None:
|
|
|
|
# We're removing a mode that only takes arguments when setting.
|
|
|
|
# Remove all mode entries that use the same letter as the one
|
|
|
|
# we're unsetting.
|
|
|
|
for oldmode in modelist.copy():
|
|
|
|
if oldmode[0] == real_mode[0]:
|
|
|
|
modelist.discard(oldmode)
|
|
|
|
else:
|
|
|
|
modelist.discard(real_mode)
|
|
|
|
log.debug('(%s) Final modelist: %s', self.name, modelist)
|
2016-07-05 09:41:15 +02:00
|
|
|
try:
|
|
|
|
if usermodes:
|
|
|
|
self.users[target].modes = modelist
|
|
|
|
else:
|
|
|
|
self.channels[target].modes = modelist
|
|
|
|
except KeyError:
|
|
|
|
log.warning("(%s) Invalid MODE target %s (usermodes=%s)", self.name, target, usermodes)
|
2016-04-25 06:37:23 +02:00
|
|
|
|
2016-05-01 01:33:46 +02:00
|
|
|
@staticmethod
|
|
|
|
def _flip(mode):
|
|
|
|
"""Flips a mode character."""
|
|
|
|
# Make it a list first, strings don't support item assignment
|
|
|
|
mode = list(mode)
|
|
|
|
if mode[0] == '-': # Query is something like "-n"
|
|
|
|
mode[0] = '+' # Change it to "+n"
|
|
|
|
elif mode[0] == '+':
|
|
|
|
mode[0] = '-'
|
|
|
|
else: # No prefix given, assume +
|
|
|
|
mode.insert(0, '-')
|
|
|
|
return ''.join(mode)
|
|
|
|
|
|
|
|
def reverseModes(self, target, modes, oldobj=None):
|
|
|
|
"""Reverses/Inverts the mode string or mode list given.
|
|
|
|
|
|
|
|
Optionally, an oldobj argument can be given to look at an earlier state of
|
|
|
|
a channel/user object, e.g. for checking the op status of a mode setter
|
|
|
|
before their modes are processed and added to the channel state.
|
|
|
|
|
|
|
|
This function allows both mode strings or mode lists. Example uses:
|
|
|
|
"+mi-lk test => "-mi+lk test"
|
|
|
|
"mi-k test => "-mi+k test"
|
|
|
|
[('+m', None), ('+r', None), ('+l', '3'), ('-o', 'person')
|
|
|
|
=> {('-m', None), ('-r', None), ('-l', None), ('+o', 'person')})
|
|
|
|
{('s', None), ('+o', 'whoever') => {('-s', None), ('-o', 'whoever')})
|
|
|
|
"""
|
|
|
|
origtype = type(modes)
|
|
|
|
# If the query is a string, we have to parse it first.
|
|
|
|
if origtype == str:
|
|
|
|
modes = self.parseModes(target, modes.split(" "))
|
|
|
|
# Get the current mode list first.
|
|
|
|
if utils.isChannel(target):
|
|
|
|
c = oldobj or self.channels[target]
|
|
|
|
oldmodes = c.modes.copy()
|
|
|
|
possible_modes = self.cmodes.copy()
|
|
|
|
# For channels, this also includes the list of prefix modes.
|
|
|
|
possible_modes['*A'] += ''.join(self.prefixmodes)
|
|
|
|
for name, userlist in c.prefixmodes.items():
|
|
|
|
try:
|
|
|
|
oldmodes.update([(self.cmodes[name], u) for u in userlist])
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
oldmodes = self.users[target].modes
|
|
|
|
possible_modes = self.umodes
|
|
|
|
newmodes = []
|
|
|
|
log.debug('(%s) reverseModes: old/current mode list for %s is: %s', self.name,
|
|
|
|
target, oldmodes)
|
|
|
|
for char, arg in modes:
|
|
|
|
# Mode types:
|
|
|
|
# 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.
|
|
|
|
mchar = char[-1]
|
|
|
|
if mchar in possible_modes['*B'] + possible_modes['*C']:
|
|
|
|
# We need to find the current mode list, so we can reset arguments
|
|
|
|
# for modes that have arguments. For example, setting +l 30 on a channel
|
|
|
|
# that had +l 50 set should give "+l 30", not "-l".
|
|
|
|
oldarg = [m for m in oldmodes if m[0] == mchar]
|
|
|
|
if oldarg: # Old mode argument for this mode existed, use that.
|
|
|
|
oldarg = oldarg[0]
|
|
|
|
mpair = ('+%s' % oldarg[0], oldarg[1])
|
|
|
|
else: # Not found, flip the mode then.
|
|
|
|
# Mode takes no arguments when unsetting.
|
|
|
|
if mchar in possible_modes['*C'] and char[0] != '-':
|
|
|
|
arg = None
|
|
|
|
mpair = (self._flip(char), arg)
|
|
|
|
else:
|
|
|
|
mpair = (self._flip(char), arg)
|
|
|
|
if char[0] != '-' and (mchar, arg) in oldmodes:
|
|
|
|
# Mode is already set.
|
|
|
|
log.debug("(%s) reverseModes: skipping reversing '%s %s' with %s since we're "
|
|
|
|
"setting a mode that's already set.", self.name, char, arg, mpair)
|
|
|
|
continue
|
|
|
|
elif char[0] == '-' and (mchar, arg) not in oldmodes and mchar in possible_modes['*A']:
|
|
|
|
# We're unsetting a prefixmode that was never set - don't set it in response!
|
|
|
|
# Charybdis lacks verification for this server-side.
|
|
|
|
log.debug("(%s) reverseModes: skipping reversing '%s %s' with %s since it "
|
|
|
|
"wasn't previously set.", self.name, char, arg, mpair)
|
|
|
|
continue
|
|
|
|
newmodes.append(mpair)
|
|
|
|
|
|
|
|
log.debug('(%s) reverseModes: new modes: %s', self.name, newmodes)
|
|
|
|
if origtype == str:
|
|
|
|
# If the original query is a string, send it back as a string.
|
|
|
|
return self.joinModes(newmodes)
|
|
|
|
else:
|
|
|
|
return set(newmodes)
|
|
|
|
|
|
|
|
@staticmethod
|
2016-08-01 06:45:38 +02:00
|
|
|
def joinModes(modes, sort=False):
|
2016-05-01 01:33:46 +02:00
|
|
|
"""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
|
|
|
|
modelist = ''
|
|
|
|
args = []
|
2016-07-01 04:52:06 +02:00
|
|
|
|
|
|
|
# Sort modes alphabetically like a conventional IRCd.
|
2016-08-01 06:45:38 +02:00
|
|
|
if sort:
|
|
|
|
modes = sorted(modes)
|
2016-07-01 04:52:06 +02:00
|
|
|
|
2016-05-01 01:33:46 +02:00
|
|
|
for modepair in modes:
|
|
|
|
mode, arg = modepair
|
|
|
|
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
|
|
|
|
modelist += mode
|
|
|
|
if arg is not None:
|
|
|
|
args.append(arg)
|
|
|
|
if not modelist.startswith(('+', '-')):
|
|
|
|
# Our starting mode didn't have a prefix with it. Assume '+'.
|
|
|
|
modelist = '+' + modelist
|
|
|
|
if args:
|
|
|
|
# Add the args if there are any.
|
|
|
|
modelist += ' %s' % ' '.join(args)
|
|
|
|
return modelist
|
|
|
|
|
2017-01-02 21:15:44 +01:00
|
|
|
@classmethod
|
2017-01-09 06:19:26 +01:00
|
|
|
def wrapModes(cls, modes, limit, max_modes_per_msg=0):
|
2017-01-02 21:08:22 +01:00
|
|
|
"""
|
|
|
|
Takes a list of modes and wraps it across multiple lines.
|
|
|
|
"""
|
|
|
|
strings = []
|
|
|
|
|
|
|
|
# This process is slightly trickier than just wrapping arguments, because modes create
|
|
|
|
# positional arguments that can't be separated from its character.
|
|
|
|
queued_modes = []
|
|
|
|
total_length = 0
|
|
|
|
|
|
|
|
last_prefix = '+'
|
|
|
|
orig_modes = modes.copy()
|
2017-01-07 07:12:49 +01:00
|
|
|
modes = list(modes)
|
2017-01-02 21:08:22 +01:00
|
|
|
while modes:
|
|
|
|
# PyLink mode lists come in the form [('+t', None), ('-b', '*!*@someone'), ('+l', 3)]
|
|
|
|
# The +/- part is optional depending on context, and should either:
|
|
|
|
# 1) The prefix of the last mode.
|
|
|
|
# 2) + (adding modes), if no prefix was ever given
|
|
|
|
next_mode = modes.pop(0)
|
|
|
|
|
|
|
|
modechar, arg = next_mode
|
|
|
|
prefix = modechar[0]
|
|
|
|
if prefix not in '+-':
|
|
|
|
prefix = last_prefix
|
|
|
|
# Explicitly add the prefix to the mode character to prevent
|
|
|
|
# ambiguity when passing it to joinModes().
|
|
|
|
modechar = prefix + modechar
|
|
|
|
# XXX: because tuples are immutable, we have to replace the entire modepair..
|
|
|
|
next_mode = (modechar, arg)
|
|
|
|
|
|
|
|
# Figure out the length that the next mode will add to the buffer. If we're changing
|
|
|
|
# from + to - (setting to removing modes) or vice versa, we'll need two characters
|
|
|
|
# ("+" or "-") plus the mode char itself.
|
|
|
|
next_length = 1
|
|
|
|
if prefix != last_prefix:
|
|
|
|
next_length += 1
|
|
|
|
|
|
|
|
# Replace the last_prefix with the current one for the next iteration.
|
|
|
|
last_prefix = prefix
|
|
|
|
|
|
|
|
if arg:
|
|
|
|
# This mode has an argument, so add the length of that and a space.
|
|
|
|
next_length += 1
|
|
|
|
next_length += len(arg)
|
|
|
|
|
|
|
|
assert next_length <= limit, \
|
|
|
|
"wrapModes: Mode %s is too long for the given length %s" % (next_mode, limit)
|
|
|
|
|
2017-01-09 06:19:26 +01:00
|
|
|
# Check both message length and max. modes per msg if enabled.
|
|
|
|
if (next_length + total_length) <= limit and ((not max_modes_per_msg) or len(queued_modes) < max_modes_per_msg):
|
2017-01-02 21:08:22 +01:00
|
|
|
# We can fit this mode in the next message; add it.
|
|
|
|
total_length += next_length
|
|
|
|
log.debug('wrapModes: Adding mode %s to queued modes', str(next_mode))
|
|
|
|
queued_modes.append(next_mode)
|
|
|
|
log.debug('wrapModes: queued modes: %s', queued_modes)
|
|
|
|
else:
|
|
|
|
# Otherwise, create a new message by joining the previous queue.
|
|
|
|
# Then, add our current mode.
|
2017-01-02 21:15:44 +01:00
|
|
|
strings.append(cls.joinModes(queued_modes))
|
2017-01-02 21:08:22 +01:00
|
|
|
queued_modes.clear()
|
|
|
|
|
|
|
|
log.debug('wrapModes: cleared queue (length %s) and now adding %s', limit, str(next_mode))
|
|
|
|
queued_modes.append(next_mode)
|
|
|
|
total_length = next_length
|
|
|
|
else:
|
|
|
|
# Everything fit in one line, so just use that.
|
2017-01-02 21:15:44 +01:00
|
|
|
strings.append(cls.joinModes(queued_modes))
|
2017-01-02 21:08:22 +01:00
|
|
|
|
|
|
|
log.debug('wrapModes: returning %s for %s', strings, orig_modes)
|
|
|
|
return strings
|
|
|
|
|
2016-05-01 02:00:28 +02:00
|
|
|
def version(self):
|
|
|
|
"""
|
|
|
|
Returns a detailed version string including the PyLink daemon version,
|
|
|
|
the protocol module in use, and the server hostname.
|
|
|
|
"""
|
2016-08-13 04:18:56 +02:00
|
|
|
fullversion = 'PyLink-%s. %s :[protocol:%s]' % (__version__, self.hostname(), self.protoname)
|
2016-05-01 02:00:28 +02:00
|
|
|
return fullversion
|
|
|
|
|
2016-08-13 04:18:56 +02:00
|
|
|
def hostname(self):
|
|
|
|
"""
|
|
|
|
Returns the server hostname used by PyLink on the given server.
|
|
|
|
"""
|
|
|
|
return self.serverdata.get('hostname', world.fallback_hostname)
|
|
|
|
|
2016-04-25 06:37:23 +02:00
|
|
|
### State checking functions
|
2016-01-01 02:28:47 +01:00
|
|
|
def nickToUid(self, nick):
|
|
|
|
"""Looks up the UID of a user with the given nick, if one is present."""
|
2016-05-01 01:57:38 +02:00
|
|
|
nick = self.toLower(nick)
|
2016-01-01 02:28:47 +01:00
|
|
|
for k, v in self.users.copy().items():
|
2016-05-01 01:57:38 +02:00
|
|
|
if self.toLower(v.nick) == nick:
|
2016-01-01 02:28:47 +01:00
|
|
|
return k
|
|
|
|
|
|
|
|
def isInternalClient(self, numeric):
|
|
|
|
"""
|
2016-11-10 03:55:13 +01:00
|
|
|
Returns whether the given client numeric (UID) is a PyLink client.
|
2016-01-01 02:28:47 +01:00
|
|
|
"""
|
2016-11-10 03:55:13 +01:00
|
|
|
sid = self.getServer(numeric)
|
|
|
|
if sid and self.servers[sid].internal:
|
|
|
|
return True
|
2016-01-01 02:53:33 +01:00
|
|
|
return False
|
2016-01-01 02:28:47 +01:00
|
|
|
|
|
|
|
def isInternalServer(self, sid):
|
|
|
|
"""Returns whether the given SID is an internal PyLink server."""
|
|
|
|
return (sid in self.servers and self.servers[sid].internal)
|
|
|
|
|
|
|
|
def getServer(self, numeric):
|
|
|
|
"""Finds the SID of the server a user is on."""
|
2016-11-10 03:52:51 +01:00
|
|
|
userobj = self.users.get(numeric)
|
|
|
|
if userobj:
|
|
|
|
return userobj.server
|
2016-01-01 02:28:47 +01:00
|
|
|
|
2016-05-01 01:44:37 +02:00
|
|
|
def isManipulatableClient(self, uid):
|
|
|
|
"""
|
|
|
|
Returns whether the given user is marked as an internal, manipulatable
|
|
|
|
client. Usually, automatically spawned services clients should have this
|
|
|
|
set True to prevent interactions with opers (like mode changes) from
|
|
|
|
causing desyncs.
|
|
|
|
"""
|
|
|
|
return self.isInternalClient(uid) and self.users[uid].manipulatable
|
|
|
|
|
2016-11-10 04:09:59 +01:00
|
|
|
def getServiceBot(self, uid):
|
2016-05-15 01:17:12 +02:00
|
|
|
"""
|
|
|
|
Checks whether the given UID is a registered service bot. If True,
|
|
|
|
returns the cooresponding ServiceBot object.
|
|
|
|
"""
|
2016-11-10 04:07:01 +01:00
|
|
|
userobj = self.users.get(uid)
|
|
|
|
if not userobj:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Look for the "service" attribute in the IrcUser object, if one exists.
|
|
|
|
try:
|
|
|
|
sname = userobj.service
|
|
|
|
# Warn if the service name we fetched isn't a registered service.
|
|
|
|
if sname not in world.services.keys():
|
|
|
|
log.warning("(%s) User %s / %s had a service bot record to a service that doesn't "
|
|
|
|
"exist (%s)!", self.name, uid, userobj.nick, sname)
|
|
|
|
return world.services.get(sname)
|
|
|
|
except AttributeError:
|
2016-05-15 01:17:12 +02:00
|
|
|
return False
|
|
|
|
|
2016-05-01 01:44:37 +02:00
|
|
|
def getHostmask(self, user, realhost=False, ip=False):
|
|
|
|
"""
|
|
|
|
Returns the hostmask of the given user, if present. If the realhost option
|
|
|
|
is given, return the real host of the user instead of the displayed host.
|
|
|
|
If the ip option is given, return the IP address of the user (this overrides
|
|
|
|
realhost)."""
|
|
|
|
userobj = self.users.get(user)
|
|
|
|
|
|
|
|
try:
|
|
|
|
nick = userobj.nick
|
|
|
|
except AttributeError:
|
|
|
|
nick = '<unknown-nick>'
|
|
|
|
|
|
|
|
try:
|
|
|
|
ident = userobj.ident
|
|
|
|
except AttributeError:
|
|
|
|
ident = '<unknown-ident>'
|
|
|
|
|
|
|
|
try:
|
|
|
|
if ip:
|
|
|
|
host = userobj.ip
|
|
|
|
elif realhost:
|
|
|
|
host = userobj.realhost
|
|
|
|
else:
|
|
|
|
host = userobj.host
|
|
|
|
except AttributeError:
|
|
|
|
host = '<unknown-host>'
|
|
|
|
|
|
|
|
return '%s!%s@%s' % (nick, ident, host)
|
|
|
|
|
2016-07-07 07:28:55 +02:00
|
|
|
def getFriendlyName(self, entityid):
|
|
|
|
"""
|
2017-02-18 22:32:48 +01:00
|
|
|
Returns the friendly name of a SID or UID (server name for SIDs, nick for UID).
|
|
|
|
"""
|
2016-07-07 07:28:55 +02:00
|
|
|
if entityid in self.servers:
|
|
|
|
return self.servers[entityid].name
|
|
|
|
elif entityid in self.users:
|
|
|
|
return self.users[entityid].nick
|
|
|
|
else:
|
|
|
|
raise KeyError("Unknown UID/SID %s" % entityid)
|
|
|
|
|
2017-02-18 22:32:48 +01:00
|
|
|
def getFullNetworkName(self):
|
|
|
|
"""
|
|
|
|
Returns the full network name (as defined by the "netname" option), or the
|
|
|
|
short network name if that isn't defined.
|
|
|
|
"""
|
|
|
|
return self.serverdata.get('netname', self.name)
|
|
|
|
|
2016-05-01 01:54:11 +02:00
|
|
|
def isOper(self, uid, allowAuthed=True, allowOper=True):
|
|
|
|
"""
|
|
|
|
Returns whether the given user has operator status on PyLink. This can be achieved
|
|
|
|
by either identifying to PyLink as admin (if allowAuthed is True),
|
|
|
|
or having user mode +o set (if allowOper is True). At least one of
|
|
|
|
allowAuthed or allowOper must be True for this to give any meaningful
|
|
|
|
results.
|
|
|
|
"""
|
|
|
|
if uid in self.users:
|
|
|
|
if allowOper and ("o", None) in self.users[uid].modes:
|
|
|
|
return True
|
2016-07-30 05:15:31 +02:00
|
|
|
elif allowAuthed and self.users[uid].account:
|
2016-05-01 01:54:11 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def checkAuthenticated(self, uid, allowAuthed=True, allowOper=True):
|
|
|
|
"""
|
|
|
|
Checks whether the given user has operator status on PyLink, raising
|
2016-08-25 09:43:44 +02:00
|
|
|
NotAuthorizedError and logging the access denial if not.
|
2016-05-01 01:54:11 +02:00
|
|
|
"""
|
2017-02-25 03:42:58 +01:00
|
|
|
log.warning("(%s) Irc.checkAuthenticated() is deprecated as of PyLink 1.2 and may be "
|
|
|
|
"removed in a future relase. Consider migrating to the PyLink Permissions API.",
|
|
|
|
self.name)
|
2016-05-01 01:54:11 +02:00
|
|
|
lastfunc = inspect.stack()[1][3]
|
|
|
|
if not self.isOper(uid, allowAuthed=allowAuthed, allowOper=allowOper):
|
|
|
|
log.warning('(%s) Access denied for %s calling %r', self.name,
|
|
|
|
self.getHostmask(uid), lastfunc)
|
2016-08-25 09:43:44 +02:00
|
|
|
raise utils.NotAuthorizedError("You are not authenticated!")
|
2016-05-01 01:54:11 +02:00
|
|
|
return True
|
|
|
|
|
2016-07-07 08:11:36 +02:00
|
|
|
def matchHost(self, glob, target, ip=True, realhost=True):
|
|
|
|
"""
|
|
|
|
Checks whether the given host, or given UID's hostmask matches the given nick!user@host
|
|
|
|
glob.
|
|
|
|
|
2017-02-26 07:26:37 +01:00
|
|
|
If the target given is a UID, and the 'ip' or 'realhost' options are True, this will also
|
|
|
|
match against the target's IP address and real host, respectively.
|
|
|
|
|
|
|
|
This function respects IRC casemappings (rfc1459 and ascii). If the given target is a UID,
|
|
|
|
and the 'ip' option is enabled, the host portion of the glob is also matched as a CIDR
|
|
|
|
range.
|
2016-07-07 08:11:36 +02:00
|
|
|
"""
|
|
|
|
# Get the corresponding casemapping value used by ircmatch.
|
2016-07-24 06:51:26 +02:00
|
|
|
if self.proto.casemapping == 'rfc1459':
|
|
|
|
casemapping = 0
|
|
|
|
else:
|
|
|
|
casemapping = 1
|
2016-07-07 08:11:36 +02:00
|
|
|
|
2016-07-07 09:25:50 +02:00
|
|
|
# Try to convert target into a UID. If this fails, it's probably a hostname.
|
|
|
|
target = self.nickToUid(target) or target
|
|
|
|
|
2017-05-05 04:04:03 +02:00
|
|
|
# Allow queries like !$exttarget to invert the given match.
|
|
|
|
invert = glob.startswith('!')
|
|
|
|
if invert:
|
|
|
|
glob = glob.lstrip('!')
|
|
|
|
|
|
|
|
def match_host_core():
|
|
|
|
"""
|
|
|
|
Core processor for matchHost(), minus the inversion check.
|
|
|
|
"""
|
|
|
|
# Work with variables in the matchHost() scope, from
|
|
|
|
# http://stackoverflow.com/a/8178808
|
|
|
|
nonlocal glob
|
|
|
|
|
|
|
|
# Prepare a list of hosts to check against.
|
|
|
|
if target in self.users:
|
|
|
|
if glob.startswith('$'):
|
|
|
|
# Exttargets start with $. Skip regular ban matching and find the matching ban handler.
|
|
|
|
glob = glob.lstrip('$')
|
|
|
|
exttargetname = glob.split(':', 1)[0]
|
|
|
|
handler = world.exttarget_handlers.get(exttargetname)
|
|
|
|
|
|
|
|
if handler:
|
|
|
|
# Handler exists. Return what it finds.
|
|
|
|
result = handler(self, glob, target)
|
|
|
|
log.debug('(%s) Got %s from exttarget %s in matchHost() glob $%s for target %s',
|
|
|
|
self.name, result, exttargetname, glob, target)
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
log.debug('(%s) Unknown exttarget %s in matchHost() glob $%s', self.name,
|
|
|
|
exttargetname, glob)
|
|
|
|
return False
|
2016-07-07 09:25:50 +02:00
|
|
|
|
2017-05-05 04:04:03 +02:00
|
|
|
hosts = {self.getHostmask(target)}
|
2016-07-07 08:11:36 +02:00
|
|
|
|
2017-05-05 04:04:03 +02:00
|
|
|
if ip:
|
|
|
|
hosts.add(self.getHostmask(target, ip=True))
|
2016-07-07 08:11:36 +02:00
|
|
|
|
2017-05-05 04:04:03 +02:00
|
|
|
# HACK: support CIDR hosts in the hosts portion
|
|
|
|
try:
|
|
|
|
header, cidrtarget = glob.split('@', 1)
|
|
|
|
log.debug('(%s) Processing CIDRs for %s (full host: %s)', self.name,
|
|
|
|
cidrtarget, glob)
|
|
|
|
# Try to parse the host portion as a CIDR range
|
|
|
|
network = ipaddress.ip_network(cidrtarget)
|
|
|
|
|
|
|
|
log.debug('(%s) Found CIDR for %s, replacing target host with IP %s', self.name,
|
|
|
|
realhost, target)
|
|
|
|
real_ip = self.users[target].ip
|
|
|
|
if ipaddress.ip_address(real_ip) in network:
|
|
|
|
# If the CIDR matches, hack around the host matcher by pretending that
|
|
|
|
# the lookup target was the IP and not the CIDR range!
|
|
|
|
glob = '@'.join((header, real_ip))
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if realhost:
|
|
|
|
hosts.add(self.getHostmask(target, realhost=True))
|
|
|
|
|
|
|
|
else: # We were given a host, use that.
|
|
|
|
hosts = [target]
|
|
|
|
|
|
|
|
# Iterate over the hosts to match using ircmatch.
|
|
|
|
for host in hosts:
|
|
|
|
if ircmatch.match(casemapping, glob, host):
|
|
|
|
return True
|
2016-07-07 08:11:36 +02:00
|
|
|
|
2017-05-05 04:04:03 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
result = match_host_core()
|
|
|
|
if invert:
|
|
|
|
result = not result
|
|
|
|
return result
|
2016-07-07 08:11:36 +02:00
|
|
|
|
2015-06-07 07:17:45 +02:00
|
|
|
class IrcUser():
|
2015-12-07 02:40:13 +01:00
|
|
|
"""PyLink IRC user class."""
|
2016-11-10 03:40:16 +01:00
|
|
|
def __init__(self, nick, ts, uid, server, ident='null', host='null',
|
2015-06-07 07:17:45 +02:00
|
|
|
realname='PyLink dummy client', realhost='null',
|
2016-03-26 20:55:23 +01:00
|
|
|
ip='0.0.0.0', manipulatable=False, opertype='IRC Operator'):
|
2015-06-07 07:17:45 +02:00
|
|
|
self.nick = nick
|
|
|
|
self.ts = ts
|
|
|
|
self.uid = uid
|
|
|
|
self.ident = ident
|
|
|
|
self.host = host
|
|
|
|
self.realhost = realhost
|
|
|
|
self.ip = ip
|
|
|
|
self.realname = realname
|
2016-02-08 03:09:02 +01:00
|
|
|
self.modes = set() # Tracks user modes
|
2016-11-10 03:40:16 +01:00
|
|
|
self.server = server
|
2015-06-07 07:17:45 +02:00
|
|
|
|
2016-02-08 03:09:02 +01:00
|
|
|
# Tracks PyLink identification status
|
2016-07-30 05:15:31 +02:00
|
|
|
self.account = ''
|
2016-02-08 03:09:02 +01:00
|
|
|
|
2016-03-26 20:55:23 +01:00
|
|
|
# Tracks oper type (for display only)
|
|
|
|
self.opertype = opertype
|
|
|
|
|
2016-07-30 05:15:31 +02:00
|
|
|
# Tracks external services identification status
|
2016-02-08 03:09:02 +01:00
|
|
|
self.services_account = ''
|
|
|
|
|
|
|
|
# Tracks channels the user is in
|
2015-07-16 21:20:40 +02:00
|
|
|
self.channels = set()
|
2016-02-08 03:09:02 +01:00
|
|
|
|
|
|
|
# Tracks away message status
|
2015-08-12 13:17:01 +02:00
|
|
|
self.away = ''
|
2015-06-07 07:17:45 +02:00
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
# This sets whether the client should be marked as manipulatable.
|
|
|
|
# Plugins like bots.py's commands should take caution against
|
|
|
|
# manipulating these "protected" clients, to prevent desyncs and such.
|
|
|
|
# For "serious" service clients, this should always be False.
|
2015-09-18 04:01:54 +02:00
|
|
|
self.manipulatable = manipulatable
|
|
|
|
|
2015-06-07 07:17:45 +02:00
|
|
|
def __repr__(self):
|
2017-03-12 07:49:48 +01:00
|
|
|
return 'IrcUser(%s/%s)' % (self.uid, self.nick)
|
2015-06-07 07:17:45 +02:00
|
|
|
|
|
|
|
class IrcServer():
|
2015-12-07 02:40:13 +01:00
|
|
|
"""PyLink IRC server class.
|
2015-06-22 00:00:33 +02:00
|
|
|
|
|
|
|
uplink: The SID of this IrcServer instance's uplink. This is set to None
|
|
|
|
for the main PyLink PseudoServer!
|
|
|
|
name: The name of the server.
|
|
|
|
internal: Whether the server is an internal PyLink PseudoServer.
|
|
|
|
"""
|
2016-01-10 04:15:39 +01:00
|
|
|
|
2015-09-12 19:39:05 +02:00
|
|
|
def __init__(self, uplink, name, internal=False, desc="(None given)"):
|
2015-06-07 07:17:45 +02:00
|
|
|
self.uplink = uplink
|
2015-08-29 21:35:06 +02:00
|
|
|
self.users = set()
|
2015-06-22 00:00:33 +02:00
|
|
|
self.internal = internal
|
|
|
|
self.name = name.lower()
|
2015-09-12 19:39:05 +02:00
|
|
|
self.desc = desc
|
2016-01-10 04:15:39 +01:00
|
|
|
|
2015-06-07 07:17:45 +02:00
|
|
|
def __repr__(self):
|
2017-03-12 07:49:48 +01:00
|
|
|
return 'IrcServer(%s)' % self.name
|
2015-06-07 08:04:11 +02:00
|
|
|
|
|
|
|
class IrcChannel():
|
2015-12-07 02:40:13 +01:00
|
|
|
"""PyLink IRC channel class."""
|
2016-03-20 01:01:39 +01:00
|
|
|
def __init__(self, name=None):
|
2015-12-07 02:40:13 +01:00
|
|
|
# Initialize variables, such as the topic, user list, TS, who's opped, etc.
|
2015-06-07 18:43:13 +02:00
|
|
|
self.users = set()
|
2017-01-01 09:37:12 +01:00
|
|
|
self.modes = set()
|
2015-07-07 00:33:23 +02:00
|
|
|
self.topic = ''
|
2015-07-07 04:00:20 +02:00
|
|
|
self.ts = int(time.time())
|
2016-03-20 01:25:04 +01:00
|
|
|
self.prefixmodes = {'op': set(), 'halfop': set(), 'voice': set(),
|
|
|
|
'owner': set(), 'admin': set()}
|
2015-07-05 21:48:39 +02:00
|
|
|
|
2015-12-07 02:40:13 +01:00
|
|
|
# Determines whether a topic has been set here or not. Protocol modules
|
|
|
|
# should set this.
|
|
|
|
self.topicset = False
|
|
|
|
|
2016-03-20 01:01:39 +01:00
|
|
|
# Saves the channel name (may be useful to plugins, etc.)
|
|
|
|
self.name = name
|
|
|
|
|
2015-06-07 08:04:11 +02:00
|
|
|
def __repr__(self):
|
2017-03-12 07:49:48 +01:00
|
|
|
return 'IrcChannel(%s)' % self.name
|
2015-07-04 03:07:01 +02:00
|
|
|
|
2015-07-05 21:48:39 +02:00
|
|
|
def removeuser(self, target):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Removes a user from a channel."""
|
2015-07-05 21:48:39 +02:00
|
|
|
for s in self.prefixmodes.values():
|
|
|
|
s.discard(target)
|
|
|
|
self.users.discard(target)
|
|
|
|
|
2015-09-13 22:47:04 +02:00
|
|
|
def deepcopy(self):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Returns a deep copy of the channel object."""
|
2015-09-13 22:47:04 +02:00
|
|
|
return deepcopy(self)
|
|
|
|
|
2016-03-20 01:32:32 +01:00
|
|
|
def isVoice(self, uid):
|
|
|
|
"""Returns whether the given user is voice in the channel."""
|
|
|
|
return uid in self.prefixmodes['voice']
|
|
|
|
|
|
|
|
def isHalfop(self, uid):
|
|
|
|
"""Returns whether the given user is halfop in the channel."""
|
|
|
|
return uid in self.prefixmodes['halfop']
|
|
|
|
|
|
|
|
def isOp(self, uid):
|
|
|
|
"""Returns whether the given user is op in the channel."""
|
|
|
|
return uid in self.prefixmodes['op']
|
|
|
|
|
|
|
|
def isAdmin(self, uid):
|
|
|
|
"""Returns whether the given user is admin (&) in the channel."""
|
|
|
|
return uid in self.prefixmodes['admin']
|
|
|
|
|
|
|
|
def isOwner(self, uid):
|
|
|
|
"""Returns whether the given user is owner (~) in the channel."""
|
|
|
|
return uid in self.prefixmodes['owner']
|
|
|
|
|
2016-03-20 01:37:38 +01:00
|
|
|
def isVoicePlus(self, uid):
|
|
|
|
"""Returns whether the given user is voice or above in the channel."""
|
|
|
|
# If the user has any prefix mode, it has to be voice or greater.
|
|
|
|
return bool(self.getPrefixModes(uid))
|
|
|
|
|
|
|
|
def isHalfopPlus(self, uid):
|
|
|
|
"""Returns whether the given user is halfop or above in the channel."""
|
|
|
|
for mode in ('halfop', 'op', 'admin', 'owner'):
|
|
|
|
if uid in self.prefixmodes[mode]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def isOpPlus(self, uid):
|
|
|
|
"""Returns whether the given user is op or above in the channel."""
|
|
|
|
for mode in ('op', 'admin', 'owner'):
|
|
|
|
if uid in self.prefixmodes[mode]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-07-12 00:21:17 +02:00
|
|
|
@staticmethod
|
|
|
|
def sortPrefixes(key):
|
|
|
|
"""
|
|
|
|
Implements a sorted()-compatible sorter for prefix modes, giving each one a
|
|
|
|
numeric value.
|
|
|
|
"""
|
|
|
|
values = {'owner': 100, 'admin': 10, 'op': 5, 'halfop': 4, 'voice': 3}
|
|
|
|
|
|
|
|
# Default to highest value (1000) for unknown modes, should we choose to
|
|
|
|
# support them.
|
|
|
|
return values.get(key, 1000)
|
|
|
|
|
2016-03-20 01:54:18 +01:00
|
|
|
def getPrefixModes(self, uid, prefixmodes=None):
|
|
|
|
"""Returns a list of all named prefix modes the given user has in the channel.
|
|
|
|
|
|
|
|
Optionally, a prefixmodes argument can be given to look at an earlier state of
|
|
|
|
the channel's prefix modes mapping, e.g. for checking the op status of a mode
|
|
|
|
setter before their modes are processed and added to the channel state.
|
|
|
|
"""
|
2016-03-20 01:32:32 +01:00
|
|
|
|
|
|
|
if uid not in self.users:
|
2016-03-20 02:00:44 +01:00
|
|
|
raise KeyError("User %s does not exist or is not in the channel" % uid)
|
2016-03-20 01:32:32 +01:00
|
|
|
|
|
|
|
result = []
|
2016-03-20 01:54:18 +01:00
|
|
|
prefixmodes = prefixmodes or self.prefixmodes
|
2016-03-20 01:32:32 +01:00
|
|
|
|
2016-03-20 01:54:18 +01:00
|
|
|
for mode, modelist in prefixmodes.items():
|
2016-03-20 01:32:32 +01:00
|
|
|
if uid in modelist:
|
|
|
|
result.append(mode)
|
|
|
|
|
2016-07-12 00:21:17 +02:00
|
|
|
return sorted(result, key=self.sortPrefixes)
|
2016-03-20 01:32:32 +01:00
|
|
|
|
2015-09-06 03:00:57 +02:00
|
|
|
class Protocol():
|
2016-01-01 02:28:47 +01:00
|
|
|
"""Base Protocol module class for PyLink."""
|
2015-09-06 03:00:57 +02:00
|
|
|
def __init__(self, irc):
|
|
|
|
self.irc = irc
|
|
|
|
self.casemapping = 'rfc1459'
|
|
|
|
self.hook_map = {}
|
|
|
|
|
2016-07-13 07:08:01 +02:00
|
|
|
# Lock for updateTS to make sure only one thread can change the channel TS at one time.
|
|
|
|
self.ts_lock = threading.Lock()
|
|
|
|
|
2016-07-29 06:34:00 +02:00
|
|
|
# Lists required conf keys for the server block.
|
|
|
|
self.conf_keys = {'ip', 'port', 'hostname', 'sid', 'sidrange', 'protocol', 'sendpass',
|
|
|
|
'recvpass'}
|
|
|
|
|
2017-03-16 07:32:47 +01:00
|
|
|
# Defines a set of PyLink protocol capabilities
|
|
|
|
self.protocol_caps = set()
|
|
|
|
|
2016-07-29 06:34:00 +02:00
|
|
|
def validateServerConf(self):
|
|
|
|
"""Validates that the server block given contains the required keys."""
|
|
|
|
for k in self.conf_keys:
|
|
|
|
assert k in self.irc.serverdata, "Missing option %r in server block for network %s." % (k, self.irc.name)
|
|
|
|
|
|
|
|
port = self.irc.serverdata['port']
|
|
|
|
assert type(port) == int and 0 < port < 65535, "Invalid port %r for network %s" % (port, self.irc.name)
|
|
|
|
|
2017-02-19 04:47:36 +01:00
|
|
|
@staticmethod
|
|
|
|
def parseArgs(args):
|
2017-02-19 04:58:24 +01:00
|
|
|
"""
|
|
|
|
Parses a string or list of of RFC1459-style arguments, where ":" may
|
2015-09-25 03:03:15 +02:00
|
|
|
be used for multi-word arguments that last until the end of a line.
|
|
|
|
"""
|
2017-02-19 04:58:24 +01:00
|
|
|
if isinstance(args, str):
|
|
|
|
args = args.split(' ')
|
|
|
|
|
2015-09-25 03:03:15 +02:00
|
|
|
real_args = []
|
|
|
|
for idx, arg in enumerate(args):
|
|
|
|
if arg.startswith(':') and idx != 0:
|
2017-02-19 04:47:36 +01:00
|
|
|
# ":" is used to begin multi-word arguments that last until the end of the message.
|
|
|
|
# Use list splicing here to join them into one argument, and then add it to our list of args.
|
|
|
|
joined_arg = ' '.join(args[idx:])[1:] # Cut off the leading : as well
|
|
|
|
real_args.append(joined_arg)
|
2015-09-25 03:03:15 +02:00
|
|
|
break
|
2017-02-19 04:47:36 +01:00
|
|
|
real_args.append(arg)
|
|
|
|
|
2015-09-25 03:03:15 +02:00
|
|
|
return real_args
|
|
|
|
|
2017-03-24 06:41:12 +01:00
|
|
|
def hasCap(self, capab):
|
2017-03-16 07:46:13 +01:00
|
|
|
"""
|
|
|
|
Returns whether this protocol module instance has the requested capability.
|
|
|
|
"""
|
|
|
|
return capab.lower() in self.protocol_caps
|
|
|
|
|
2015-09-25 03:03:15 +02:00
|
|
|
def removeClient(self, numeric):
|
|
|
|
"""Internal function to remove a client from our internal state."""
|
|
|
|
for c, v in self.irc.channels.copy().items():
|
|
|
|
v.removeuser(numeric)
|
|
|
|
# Clear empty non-permanent channels.
|
|
|
|
if not (self.irc.channels[c].users or ((self.irc.cmodes.get('permanent'), None) in self.irc.channels[c].modes)):
|
|
|
|
del self.irc.channels[c]
|
|
|
|
assert numeric not in v.users, "IrcChannel's removeuser() is broken!"
|
|
|
|
|
2016-04-02 07:55:03 +02:00
|
|
|
sid = self.irc.getServer(numeric)
|
2015-09-25 03:03:15 +02:00
|
|
|
log.debug('Removing client %s from self.irc.users', numeric)
|
|
|
|
del self.irc.users[numeric]
|
|
|
|
log.debug('Removing client %s from self.irc.servers[%s].users', numeric, sid)
|
|
|
|
self.irc.servers[sid].users.discard(numeric)
|
|
|
|
|
2017-04-09 23:44:36 +02:00
|
|
|
def updateTS(self, sender, channel, their_ts, modes=None):
|
2015-12-26 23:47:23 +01:00
|
|
|
"""
|
2016-06-23 06:34:16 +02:00
|
|
|
Merges modes of a channel given the remote TS and a list of modes.
|
2015-12-26 23:47:23 +01:00
|
|
|
"""
|
|
|
|
|
2016-07-27 08:30:55 +02:00
|
|
|
# Okay, so the situation is that we have 6 possible TS/sender combinations:
|
|
|
|
|
|
|
|
# | our TS lower | TS equal | their TS lower
|
|
|
|
# mode origin is us | OVERWRITE | MERGE | IGNORE
|
|
|
|
# mode origin is uplink | IGNORE | MERGE | OVERWRITE
|
|
|
|
|
2017-04-09 23:44:36 +02:00
|
|
|
if modes is None:
|
|
|
|
modes = []
|
|
|
|
|
2016-06-23 07:26:25 +02:00
|
|
|
def _clear():
|
2016-07-30 20:41:33 +02:00
|
|
|
log.debug("(%s) Clearing local modes from channel %s due to TS change", self.irc.name,
|
2016-06-24 22:15:26 +02:00
|
|
|
channel)
|
2015-11-08 19:49:09 +01:00
|
|
|
self.irc.channels[channel].modes.clear()
|
|
|
|
for p in self.irc.channels[channel].prefixmodes.values():
|
2016-07-30 20:41:33 +02:00
|
|
|
for user in p.copy():
|
|
|
|
if not self.irc.isInternalClient(user):
|
|
|
|
p.discard(user)
|
2015-11-08 19:49:09 +01:00
|
|
|
|
2016-06-23 07:26:25 +02:00
|
|
|
def _apply():
|
2016-06-25 21:54:56 +02:00
|
|
|
if modes:
|
|
|
|
log.debug("(%s) Applying modes on channel %s (TS ok)", self.irc.name,
|
|
|
|
channel)
|
|
|
|
self.irc.applyModes(channel, modes)
|
2016-06-23 07:26:25 +02:00
|
|
|
|
2016-07-13 07:08:01 +02:00
|
|
|
# Use a lock so only one thread can change a channel's TS at once: this prevents race
|
|
|
|
# conditions from desyncing the channel list.
|
|
|
|
with self.ts_lock:
|
|
|
|
our_ts = self.irc.channels[channel].ts
|
|
|
|
assert type(our_ts) == int, "Wrong type for our_ts (expected int, got %s)" % type(our_ts)
|
|
|
|
assert type(their_ts) == int, "Wrong type for their_ts (expected int, got %s)" % type(their_ts)
|
|
|
|
|
|
|
|
# Check if we're the mode sender based on the UID / SID given.
|
|
|
|
our_mode = self.irc.isInternalClient(sender) or self.irc.isInternalServer(sender)
|
|
|
|
|
2016-07-27 08:30:55 +02:00
|
|
|
log.debug("(%s/%s) our_ts: %s; their_ts: %s; is the mode origin us? %s", self.irc.name,
|
|
|
|
channel, our_ts, their_ts, our_mode)
|
2016-07-13 07:08:01 +02:00
|
|
|
|
2016-07-27 08:30:55 +02:00
|
|
|
if their_ts == our_ts:
|
2016-07-13 07:08:01 +02:00
|
|
|
log.debug("(%s/%s) remote TS of %s is equal to our %s; mode query %s",
|
|
|
|
self.irc.name, channel, their_ts, our_ts, modes)
|
|
|
|
# Their TS is equal to ours. Merge modes.
|
2016-07-11 06:35:17 +02:00
|
|
|
_apply()
|
2016-06-23 06:34:16 +02:00
|
|
|
|
2016-07-30 20:41:33 +02:00
|
|
|
elif (their_ts < our_ts):
|
2017-01-07 06:56:22 +01:00
|
|
|
if their_ts < 750000:
|
|
|
|
log.warning('(%s) Possible desync? Not setting bogus TS %s on channel %s', self.irc.name, their_ts, channel)
|
|
|
|
else:
|
|
|
|
log.debug('(%s) Resetting channel TS of %s from %s to %s (remote has lower TS)',
|
|
|
|
self.irc.name, channel, our_ts, their_ts)
|
|
|
|
self.irc.channels[channel].ts = their_ts
|
2016-07-29 23:43:19 +02:00
|
|
|
|
2016-07-30 20:41:33 +02:00
|
|
|
# Remote TS was lower and we're receiving modes. Clear the modelist and apply theirs.
|
2016-07-11 06:35:17 +02:00
|
|
|
|
2016-07-29 23:43:19 +02:00
|
|
|
_clear()
|
2016-07-27 08:30:55 +02:00
|
|
|
_apply()
|
2016-06-23 06:34:16 +02:00
|
|
|
|
2016-04-11 04:00:44 +02:00
|
|
|
def _getSid(self, sname):
|
|
|
|
"""Returns the SID of a server with the given name, if present."""
|
|
|
|
name = sname.lower()
|
|
|
|
for k, v in self.irc.servers.items():
|
|
|
|
if v.name.lower() == name:
|
|
|
|
return k
|
|
|
|
else:
|
|
|
|
return sname # Fall back to given text instead of None
|
|
|
|
|
|
|
|
def _getUid(self, target):
|
|
|
|
"""Converts a nick argument to its matching UID. This differs from irc.nickToUid()
|
|
|
|
in that it returns the original text instead of None, if no matching nick is found."""
|
|
|
|
target = self.irc.nickToUid(target) or target
|
|
|
|
return target
|
|
|
|
|
2017-02-19 04:58:24 +01:00
|
|
|
@classmethod
|
|
|
|
def parsePrefixedArgs(cls, args):
|
2016-07-17 07:41:52 +02:00
|
|
|
"""Similar to parseArgs(), but stripping leading colons from the first argument
|
|
|
|
of a line (usually the sender field)."""
|
2017-02-19 04:58:24 +01:00
|
|
|
args = cls.parseArgs(args)
|
2016-07-17 07:41:52 +02:00
|
|
|
args[0] = args[0].split(':', 1)[1]
|
|
|
|
return args
|
|
|
|
|
2016-07-18 07:34:12 +02:00
|
|
|
def _squit(self, numeric, command, args):
|
|
|
|
"""Handles incoming SQUITs."""
|
|
|
|
|
|
|
|
split_server = self._getSid(args[0])
|
|
|
|
|
2016-08-04 21:55:24 +02:00
|
|
|
# Normally we'd only need to check for our SID as the SQUIT target, but Nefarious
|
|
|
|
# actually uses the uplink server as the SQUIT target.
|
|
|
|
# <- ABAAE SQ nefarious.midnight.vpn 0 :test
|
|
|
|
if split_server in (self.irc.sid, self.irc.uplink):
|
|
|
|
raise ProtocolError('SQUIT received: (reason: %s)' % args[-1])
|
|
|
|
|
2016-07-18 07:34:12 +02:00
|
|
|
affected_users = []
|
2016-08-09 05:58:34 +02:00
|
|
|
affected_nicks = defaultdict(list)
|
2016-07-18 07:34:12 +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
|
|
|
|
|
|
|
|
# Prevent RuntimeError: dictionary changed size during iteration
|
|
|
|
old_servers = self.irc.servers.copy()
|
2016-08-09 05:58:34 +02:00
|
|
|
old_channels = self.irc.channels.copy()
|
|
|
|
|
2016-07-18 07:34:12 +02:00
|
|
|
# Cycle through our list of servers. If any server's uplink is the one that is being SQUIT,
|
|
|
|
# remove them and all their users too.
|
|
|
|
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)
|
|
|
|
# Recursively run SQUIT on any other hubs this server may have been connected to.
|
2016-07-18 08:08:47 +02:00
|
|
|
args = self._squit(sid, 'SQUIT', [sid, "0",
|
|
|
|
"PyLink: Automatically splitting leaf servers of %s" % sid])
|
2016-07-18 07:34:12 +02:00
|
|
|
affected_users += args['users']
|
|
|
|
|
|
|
|
for user in self.irc.servers[split_server].users.copy():
|
|
|
|
affected_users.append(user)
|
2016-08-04 21:50:04 +02:00
|
|
|
nick = self.irc.users[user].nick
|
2016-08-09 05:58:34 +02:00
|
|
|
|
|
|
|
# Nicks affected is channel specific for SQUIT:. This makes Clientbot's SQUIT relaying
|
|
|
|
# much easier to implement.
|
|
|
|
for name, cdata in old_channels.items():
|
|
|
|
if user in cdata.users:
|
|
|
|
affected_nicks[name].append(nick)
|
|
|
|
|
2016-08-04 21:50:04 +02:00
|
|
|
log.debug('Removing client %s (%s)', user, nick)
|
2016-07-18 07:34:12 +02:00
|
|
|
self.removeClient(user)
|
|
|
|
|
2016-08-04 21:50:04 +02:00
|
|
|
serverdata = self.irc.servers[split_server]
|
|
|
|
sname = serverdata.name
|
|
|
|
uplink = serverdata.uplink
|
|
|
|
|
2016-07-18 07:34:12 +02:00
|
|
|
del self.irc.servers[split_server]
|
|
|
|
log.debug('(%s) Netsplit affected users: %s', self.irc.name, affected_users)
|
|
|
|
|
|
|
|
return {'target': split_server, 'users': affected_users, 'name': sname,
|
2016-08-09 05:58:34 +02:00
|
|
|
'uplink': uplink, 'nicks': affected_nicks, 'serverdata': serverdata,
|
2016-08-28 02:42:07 +02:00
|
|
|
'channeldata': old_channels}
|
2016-07-18 07:34:12 +02:00
|
|
|
|
2016-12-17 07:27:41 +01:00
|
|
|
@staticmethod
|
|
|
|
def parseCapabilities(args, fallback=''):
|
2016-07-21 08:47:31 +02:00
|
|
|
"""
|
|
|
|
Parses a string of capabilities in the 005 / RPL_ISUPPORT format.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if type(args) == str:
|
|
|
|
args = args.split(' ')
|
|
|
|
|
|
|
|
caps = {}
|
|
|
|
for cap in args:
|
|
|
|
try:
|
|
|
|
# Try to split it as a KEY=VALUE pair.
|
|
|
|
key, value = cap.split('=', 1)
|
|
|
|
except ValueError:
|
|
|
|
key = cap
|
2016-12-17 07:27:41 +01:00
|
|
|
value = fallback
|
2016-07-21 08:47:31 +02:00
|
|
|
caps[key] = value
|
|
|
|
|
|
|
|
return caps
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parsePrefixes(args):
|
|
|
|
"""
|
|
|
|
Separates prefixes field like "(qaohv)~&@%+" into a dict mapping mode characters to mode
|
|
|
|
prefixes.
|
|
|
|
"""
|
|
|
|
prefixsearch = re.search(r'\(([A-Za-z]+)\)(.*)', args)
|
|
|
|
return dict(zip(prefixsearch.group(1), prefixsearch.group(2)))
|
2016-08-09 03:12:07 +02:00
|
|
|
|
|
|
|
def handle_error(self, numeric, command, args):
|
|
|
|
"""Handles ERROR messages - these mean that our uplink has disconnected us!"""
|
|
|
|
raise ProtocolError('Received an ERROR, disconnecting!')
|