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
|
|
|
|
from random import randint
|
2015-08-26 05:37:15 +02:00
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
import ssl
|
|
|
|
import hashlib
|
2015-09-13 22:47:04 +02:00
|
|
|
from copy import deepcopy
|
2015-07-08 03:07:20 +02:00
|
|
|
|
2016-01-23 22:13:38 +01:00
|
|
|
from log import *
|
2015-08-29 18:39:33 +02:00
|
|
|
import world
|
2015-09-26 18:05:44 +02:00
|
|
|
import utils
|
2015-08-26 05:37:15 +02:00
|
|
|
|
|
|
|
### Exceptions
|
|
|
|
|
|
|
|
class ProtocolError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
### Internal classes (users, servers, channels)
|
|
|
|
|
|
|
|
class Irc():
|
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.
|
|
|
|
"""
|
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
|
|
|
|
self.serverdata = conf['servers'][netname]
|
|
|
|
self.sid = self.serverdata["sid"]
|
|
|
|
self.botdata = conf['bot']
|
|
|
|
self.protoname = proto.__name__
|
|
|
|
self.proto = proto.Class(self)
|
|
|
|
self.pingfreq = self.serverdata.get('pingfreq') or 30
|
|
|
|
self.pingtimeout = self.pingfreq * 2
|
|
|
|
|
|
|
|
self.connected = threading.Event()
|
|
|
|
self.aborted = threading.Event()
|
|
|
|
|
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-06 04:35:16 +01:00
|
|
|
self.pingTimer = None
|
2015-12-07 02:40:13 +01:00
|
|
|
|
2016-01-23 22:13:38 +01:00
|
|
|
def logSetup(self):
|
|
|
|
"""
|
|
|
|
Initializes any channel loggers defined for the current network.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
channels = self.conf['logging']['channels'][self.name]
|
|
|
|
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.
|
|
|
|
"""
|
2015-10-03 08:07:57 +02:00
|
|
|
self.sid = self.serverdata["sid"]
|
|
|
|
self.botdata = self.conf['bot']
|
|
|
|
self.pingfreq = self.serverdata.get('pingfreq') or 30
|
|
|
|
self.pingtimeout = self.pingfreq * 2
|
|
|
|
|
2015-09-03 07:05:47 +02:00
|
|
|
self.connected.clear()
|
|
|
|
self.aborted.clear()
|
2015-08-26 05:37:15 +02:00
|
|
|
self.pseudoclient = None
|
|
|
|
self.lastping = time.time()
|
2015-09-03 07:05:47 +02:00
|
|
|
|
2015-09-26 18:05:44 +02:00
|
|
|
# Internal variable to set the place the last command was called (in PM
|
|
|
|
# or in a channel), used by fantasy command support.
|
|
|
|
self.called_by = None
|
|
|
|
|
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.
|
2015-09-12 19:39:05 +02:00
|
|
|
self.servers = {self.sid: IrcServer(None, self.serverdata['hostname'],
|
|
|
|
internal=True, desc=self.serverdata.get('serverdesc')
|
|
|
|
or self.botdata['serverdesc'])}
|
2015-08-26 05:37:15 +02:00
|
|
|
self.users = {}
|
2016-03-20 01:01:39 +01:00
|
|
|
self.channels = utils.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',
|
|
|
|
'*A': '', '*B': '', '*C': 's', '*D': 'iow'}
|
|
|
|
|
|
|
|
# This max nick length starts off as the config value, but may be
|
|
|
|
# overwritten later by the protocol module if such information is
|
|
|
|
# received. Note that only some IRCds (InspIRCd) give us nick length
|
|
|
|
# during link, so it is still required that the config value be set!
|
|
|
|
self.maxnicklen = self.serverdata['maxnicklen']
|
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()
|
|
|
|
|
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-03-26 01:14:16 +01:00
|
|
|
self.initVars()
|
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
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
# 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')
|
|
|
|
if certfile and keyfile:
|
|
|
|
try:
|
|
|
|
self.socket = ssl.wrap_socket(self.socket,
|
|
|
|
certfile=certfile,
|
|
|
|
keyfile=keyfile)
|
|
|
|
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
|
2015-12-07 02:40:13 +01:00
|
|
|
else: # SSL was misconfigured, abort.
|
2015-08-26 05:37:15 +02:00
|
|
|
log.error('(%s) SSL certfile/keyfile was not set '
|
|
|
|
'correctly, aborting... ', self.name)
|
|
|
|
checks_ok = False
|
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:
|
2015-12-07 02:40:13 +01:00
|
|
|
# All our checks passed, get the protocol module to connect
|
|
|
|
# and run the listen loop.
|
2015-09-06 03:00:57 +02:00
|
|
|
self.proto.connect()
|
2015-08-26 05:37:15 +02:00
|
|
|
self.spawnMain()
|
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)
|
|
|
|
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)
|
|
|
|
except (socket.error, ProtocolError, ConnectionError) as e:
|
2015-12-07 02:40:13 +01:00
|
|
|
# self.run() or the protocol module it called raised an
|
|
|
|
# exception, meaning we've disconnected!
|
2015-08-26 05:37:15 +02:00
|
|
|
log.warning('(%s) Disconnected from IRC: %s: %s',
|
|
|
|
self.name, type(e).__name__, str(e))
|
2016-01-10 05:23:23 +01:00
|
|
|
|
|
|
|
if not self.aborted.is_set():
|
|
|
|
# Only start a disconnection process if one doesn't already
|
|
|
|
# exist.
|
|
|
|
self.disconnect()
|
2015-12-07 02:40:13 +01:00
|
|
|
|
2016-01-10 04:38:27 +01:00
|
|
|
# Internal hook signifying that a network has disconnected.
|
|
|
|
self.callHooks([None, 'PYLINK_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')
|
|
|
|
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:
|
2015-08-26 05:37:15 +02:00
|
|
|
log.info('(%s) Going to auto-reconnect in %s seconds.', self.name, autoconnect)
|
|
|
|
time.sleep(autoconnect)
|
|
|
|
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."""
|
2016-01-10 04:38:27 +01: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
|
|
|
|
|
|
|
log.debug('(%s) _disconnect: Setting self.aborted to True.', self.name)
|
|
|
|
self.aborted.set()
|
|
|
|
|
2016-03-25 22:59:07 +01:00
|
|
|
log.debug('(%s) disconnect: Clearing state via initVars().', self.name)
|
|
|
|
self.initVars()
|
|
|
|
|
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-03-26 01:03:25 +01: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
|
|
|
|
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:
|
|
|
|
log.warning('(%s) No data received, disconnecting!', self.name)
|
2015-08-26 05:37:15 +02:00
|
|
|
return
|
|
|
|
elif (time.time() - self.lastping) > self.pingtimeout:
|
|
|
|
log.warning('(%s) Connection timed out.', self.name)
|
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
try:
|
|
|
|
self.socket.send(data)
|
|
|
|
except (OSError, AttributeError):
|
|
|
|
log.debug("(%s) Dropping message %r; network isn't connected!", self.name, stripped_data)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def spawnMain(self):
|
2015-12-07 02:40:13 +01:00
|
|
|
"""Spawns the main PyLink client."""
|
2015-08-26 05:37:15 +02:00
|
|
|
nick = self.botdata.get('nick') or 'PyLink'
|
|
|
|
ident = self.botdata.get('ident') or 'pylink'
|
|
|
|
host = self.serverdata["hostname"]
|
|
|
|
log.info('(%s) Connected! Spawning main client %s.', self.name, nick)
|
|
|
|
olduserobj = self.pseudoclient
|
2015-09-18 04:01:54 +02:00
|
|
|
self.pseudoclient = self.proto.spawnClient(nick, ident, host,
|
|
|
|
modes={("+o", None)},
|
2016-03-26 20:49:10 +01:00
|
|
|
manipulatable=True,
|
|
|
|
opertype="PyLink Service")
|
2015-08-26 05:37:15 +02:00
|
|
|
for chan in self.serverdata['channels']:
|
2016-01-17 01:36:45 +01:00
|
|
|
self.proto.join(self.pseudoclient.uid, chan)
|
2015-08-26 05:37:15 +02:00
|
|
|
# PyLink internal hook called when spawnMain is called and the
|
|
|
|
# contents of Irc().pseudoclient change.
|
|
|
|
self.callHooks([self.sid, 'PYLINK_SPAWNMAIN', {'olduser': olduserobj}])
|
2015-07-05 21:48:39 +02:00
|
|
|
|
2015-09-29 04:25:45 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return "<classes.Irc object for %r>" % self.name
|
|
|
|
|
2016-01-01 02:28:47 +01:00
|
|
|
### Utility functions
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
cmd_args = text.strip().split(' ')
|
|
|
|
cmd = cmd_args[0].lower()
|
|
|
|
cmd_args = cmd_args[1:]
|
|
|
|
if cmd not in world.commands:
|
|
|
|
self.msg(self.called_by or source, 'Error: Unknown command %r.' % cmd)
|
|
|
|
log.info('(%s) Received unknown command %r from %s', self.name, cmd, utils.getHostmask(self, source))
|
|
|
|
return
|
|
|
|
log.info('(%s) Calling command %r for %s', self.name, cmd, utils.getHostmask(self, source))
|
|
|
|
for func in world.commands[cmd]:
|
|
|
|
try:
|
|
|
|
func(self, source, cmd_args)
|
|
|
|
except utils.NotAuthenticatedError:
|
|
|
|
self.msg(self.called_by or source, 'Error: You are not authorized to perform this operation.')
|
|
|
|
except Exception as e:
|
|
|
|
log.exception('Unhandled exception caught in command %r', cmd)
|
|
|
|
self.msg(self.called_by or source, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e)))
|
|
|
|
|
|
|
|
def msg(self, target, text, notice=False, source=None):
|
|
|
|
"""Handy function to send messages/notices to clients. Source
|
|
|
|
is optional, and defaults to the main PyLink client if not specified."""
|
|
|
|
source = source or self.pseudoclient.uid
|
|
|
|
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'
|
|
|
|
self.callHooks([source, cmd, {'target': target, 'text': text}])
|
|
|
|
|
|
|
|
def reply(self, text, notice=False, source=None):
|
|
|
|
"""Replies to the last caller in the right context (channel or PM)."""
|
|
|
|
self.msg(self.called_by, text, notice=notice, source=source)
|
|
|
|
|
|
|
|
def nickToUid(self, nick):
|
|
|
|
"""Looks up the UID of a user with the given nick, if one is present."""
|
|
|
|
nick = utils.toLower(self, nick)
|
|
|
|
for k, v in self.users.copy().items():
|
|
|
|
if utils.toLower(self, v.nick) == nick:
|
|
|
|
return k
|
|
|
|
|
|
|
|
def isInternalClient(self, numeric):
|
|
|
|
"""
|
|
|
|
Checks whether the given numeric is a PyLink Client,
|
|
|
|
returning the SID of the server it's on if so.
|
|
|
|
"""
|
|
|
|
for sid in self.servers:
|
|
|
|
if self.servers[sid].internal and numeric in self.servers[sid].users:
|
|
|
|
return sid
|
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."""
|
|
|
|
for server in self.servers:
|
|
|
|
if numeric in self.servers[server].users:
|
|
|
|
return server
|
|
|
|
|
2015-06-07 07:17:45 +02:00
|
|
|
class IrcUser():
|
2015-12-07 02:40:13 +01:00
|
|
|
"""PyLink IRC user class."""
|
2015-06-07 07:17:45 +02:00
|
|
|
def __init__(self, nick, ts, uid, ident='null', host='null',
|
|
|
|
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
|
2015-06-07 07:17:45 +02:00
|
|
|
|
2016-02-08 03:09:02 +01:00
|
|
|
# Tracks PyLink identification status
|
|
|
|
self.identified = ''
|
|
|
|
|
2016-03-26 20:55:23 +01:00
|
|
|
# Tracks oper type (for display only)
|
|
|
|
self.opertype = opertype
|
|
|
|
|
2016-02-08 03:09:02 +01:00
|
|
|
# Tracks services identification status
|
|
|
|
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):
|
2016-01-10 04:15:39 +01:00
|
|
|
return 'IrcUser(%s)' % self.__dict__
|
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):
|
2016-01-10 04:15:39 +01:00
|
|
|
return 'IrcServer(%s)' % self.__dict__
|
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()
|
2015-08-31 18:09:03 +02:00
|
|
|
self.modes = {('n', None), ('t', None)}
|
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):
|
2016-01-10 04:15:39 +01:00
|
|
|
return 'IrcChannel(%s)' % self.__dict__
|
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-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)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
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 = {}
|
|
|
|
|
2015-09-25 03:03:15 +02:00
|
|
|
def parseArgs(self, args):
|
|
|
|
"""Parses a string of RFC1459-style arguments split into a list, where ":" may
|
|
|
|
be used for multi-word arguments that last until the end of a line.
|
|
|
|
"""
|
|
|
|
real_args = []
|
|
|
|
for idx, arg in enumerate(args):
|
|
|
|
real_args.append(arg)
|
|
|
|
# If the argument starts with ':' and ISN'T the first argument.
|
|
|
|
# The first argument is used for denoting the source UID/SID.
|
|
|
|
if arg.startswith(':') and idx != 0:
|
|
|
|
# : is used for multi-word arguments that last until the end
|
|
|
|
# of the message. We can use list splicing here to turn them all
|
|
|
|
# into one argument.
|
|
|
|
# Set the last arg to a joined version of the remaining args
|
|
|
|
arg = args[idx:]
|
|
|
|
arg = ' '.join(arg)[1:]
|
|
|
|
# Cut the original argument list right before the multi-word arg,
|
|
|
|
# and then append the multi-word arg.
|
|
|
|
real_args = args[:idx]
|
|
|
|
real_args.append(arg)
|
|
|
|
break
|
|
|
|
return real_args
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-11-08 19:49:09 +01:00
|
|
|
def updateTS(self, channel, their_ts):
|
2015-12-26 23:47:23 +01:00
|
|
|
"""
|
|
|
|
Compares the current TS of the channel given with the new TS, resetting
|
|
|
|
all modes we have if the one given is older.
|
|
|
|
"""
|
|
|
|
|
2015-11-08 19:49:09 +01:00
|
|
|
our_ts = self.irc.channels[channel].ts
|
2015-12-26 23:47:23 +01:00
|
|
|
|
2015-11-08 19:49:09 +01:00
|
|
|
if their_ts < our_ts:
|
|
|
|
# Channel timestamp was reset on burst
|
|
|
|
log.debug('(%s) Setting channel TS of %s to %s from %s',
|
|
|
|
self.irc.name, channel, their_ts, our_ts)
|
|
|
|
self.irc.channels[channel].ts = their_ts
|
|
|
|
# When TS is reset, clear all modes we currently have
|
|
|
|
self.irc.channels[channel].modes.clear()
|
|
|
|
for p in self.irc.channels[channel].prefixmodes.values():
|
|
|
|
p.clear()
|
|
|
|
|
2016-01-01 02:28:47 +01:00
|
|
|
### FakeIRC classes, used for test cases
|
|
|
|
|
|
|
|
class FakeIRC(Irc):
|
|
|
|
"""Fake IRC object used for unit tests."""
|
|
|
|
def connect(self):
|
|
|
|
self.messages = []
|
|
|
|
self.hookargs = []
|
|
|
|
self.hookmsgs = []
|
|
|
|
self.socket = None
|
|
|
|
self.initVars()
|
|
|
|
self.spawnMain()
|
|
|
|
self.connected = threading.Event()
|
|
|
|
self.connected.set()
|
|
|
|
|
|
|
|
def run(self, data):
|
|
|
|
"""Queues a message to the fake IRC server."""
|
|
|
|
log.debug('<- ' + data)
|
|
|
|
hook_args = self.proto.handle_events(data)
|
|
|
|
if hook_args is not None:
|
|
|
|
self.hookmsgs.append(hook_args)
|
|
|
|
self.callHooks(hook_args)
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
self.messages.append(data)
|
|
|
|
log.debug('-> ' + data)
|
|
|
|
|
|
|
|
def takeMsgs(self):
|
|
|
|
"""Returns a list of messages sent by the protocol module since
|
|
|
|
the last takeMsgs() call, so we can track what has been sent."""
|
|
|
|
msgs = self.messages
|
|
|
|
self.messages = []
|
|
|
|
return msgs
|
|
|
|
|
|
|
|
def takeCommands(self, msgs):
|
|
|
|
"""Returns a list of commands parsed from the output of takeMsgs()."""
|
|
|
|
sidprefix = ':' + self.sid
|
|
|
|
commands = []
|
|
|
|
for m in msgs:
|
|
|
|
args = m.split()
|
|
|
|
if m.startswith(sidprefix):
|
|
|
|
commands.append(args[1])
|
|
|
|
else:
|
|
|
|
commands.append(args[0])
|
|
|
|
return commands
|
|
|
|
|
|
|
|
def takeHooks(self):
|
|
|
|
"""Returns a list of hook arguments sent by the protocol module since
|
|
|
|
the last takeHooks() call."""
|
|
|
|
hookmsgs = self.hookmsgs
|
|
|
|
self.hookmsgs = []
|
|
|
|
return hookmsgs
|
|
|
|
|
2015-09-07 08:39:10 +02:00
|
|
|
class FakeProto(Protocol):
|
2015-07-08 03:07:20 +02:00
|
|
|
"""Dummy protocol module for testing purposes."""
|
2015-09-07 08:39:10 +02:00
|
|
|
def handle_events(self, data):
|
2015-07-08 03:07:20 +02:00
|
|
|
pass
|
|
|
|
|
2015-09-07 08:39:10 +02:00
|
|
|
def connect(self):
|
2015-07-08 03:07:20 +02:00
|
|
|
pass
|
2015-07-18 07:52:55 +02:00
|
|
|
|
2015-09-07 08:39:10 +02:00
|
|
|
def spawnClient(self, nick, *args, **kwargs):
|
2015-09-13 07:26:38 +02:00
|
|
|
uid = str(randint(1, 10000000000))
|
2015-07-18 07:52:55 +02:00
|
|
|
ts = int(time.time())
|
2015-09-07 08:39:10 +02:00
|
|
|
self.irc.users[uid] = user = IrcUser(nick, ts, uid)
|
2015-07-18 07:52:55 +02:00
|
|
|
return user
|
|
|
|
|
2016-01-17 01:36:45 +01:00
|
|
|
def join(self, client, channel):
|
2015-09-07 08:39:10 +02:00
|
|
|
self.irc.channels[channel].users.add(client)
|
|
|
|
self.irc.users[client].channels.add(channel)
|
|
|
|
|
|
|
|
FakeProto.Class = FakeProto
|