2005-01-19 14:14:38 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2008-2009, James McCoy
|
2021-10-17 09:54:06 +02:00
|
|
|
# Copyright (c) 2010-2021, Valentin Lorentz
|
2005-01-19 14:14:38 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Contains various drivers (network, file, and otherwise) for using IRC objects.
|
|
|
|
"""
|
|
|
|
|
2019-12-08 15:54:48 +01:00
|
|
|
import time
|
2005-01-19 14:14:38 +01:00
|
|
|
import socket
|
2019-12-07 23:33:04 +01:00
|
|
|
from collections import namedtuple
|
2005-01-19 14:14:38 +01:00
|
|
|
|
2019-12-08 15:54:48 +01:00
|
|
|
from .. import conf, ircdb, ircmsgs, ircutils, log as supylog, utils
|
2015-08-11 16:50:23 +02:00
|
|
|
from ..utils import minisix
|
2005-01-19 14:14:38 +01:00
|
|
|
|
2019-12-07 23:33:04 +01:00
|
|
|
|
2021-01-11 23:22:21 +01:00
|
|
|
Server = namedtuple('Server', 'hostname port attempt force_tls_verification')
|
2019-12-07 23:33:04 +01:00
|
|
|
# force_tls_verification=True implies two things:
|
|
|
|
# 1. force TLS to be enabled for this server
|
|
|
|
# 2. ensure there is some kind of verification. If the user did not enable
|
|
|
|
# any, use standard PKI validation.
|
|
|
|
|
|
|
|
|
2005-01-19 14:14:38 +01:00
|
|
|
_drivers = {}
|
2016-02-09 18:31:30 +01:00
|
|
|
_deadDrivers = set()
|
2005-01-19 14:14:38 +01:00
|
|
|
_newDrivers = []
|
|
|
|
|
|
|
|
class IrcDriver(object):
|
|
|
|
"""Base class for drivers."""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
add(self.name(), self)
|
2009-01-28 06:31:45 +01:00
|
|
|
super(IrcDriver, self).__init__(*args, **kwargs)
|
2005-01-19 14:14:38 +01:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def die(self):
|
2021-06-21 01:07:50 +02:00
|
|
|
# The end of any overridden die method should be
|
2005-01-19 14:14:38 +01:00
|
|
|
# "super(Class, self).die()", in order to make
|
|
|
|
# sure this (and anything else later added) is done.
|
|
|
|
remove(self.name())
|
|
|
|
|
|
|
|
def reconnect(self, wait=False):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return repr(self)
|
|
|
|
|
|
|
|
class ServersMixin(object):
|
|
|
|
def __init__(self, irc, servers=()):
|
2019-12-08 15:54:48 +01:00
|
|
|
self.networkName = irc.network
|
2005-01-19 14:14:38 +01:00
|
|
|
self.networkGroup = conf.supybot.networks.get(irc.network)
|
|
|
|
self.servers = servers
|
2008-12-08 21:38:11 +01:00
|
|
|
super(ServersMixin, self).__init__()
|
2005-01-31 16:22:48 +01:00
|
|
|
|
2005-01-19 14:14:38 +01:00
|
|
|
def _getServers(self):
|
2005-01-31 16:22:48 +01:00
|
|
|
# We do this, rather than utils.iter.cycle the servers in __init__,
|
2005-01-19 14:14:38 +01:00
|
|
|
# because otherwise registry updates given as setValues or sets
|
|
|
|
# wouldn't be visible until a restart.
|
|
|
|
return self.networkGroup.servers()[:] # Be sure to copy!
|
|
|
|
|
|
|
|
def _getNextServer(self):
|
|
|
|
if not self.servers:
|
|
|
|
self.servers = self._getServers()
|
|
|
|
assert self.servers, 'Servers value for %s is empty.' % \
|
|
|
|
self.networkGroup._name
|
|
|
|
server = self.servers.pop(0)
|
2019-12-08 15:54:48 +01:00
|
|
|
self.currentServer = self._applyStsPolicy(server)
|
|
|
|
return self.currentServer
|
|
|
|
|
|
|
|
def _applyStsPolicy(self, server):
|
|
|
|
network = ircdb.networks.getNetwork(self.networkName)
|
2021-09-03 20:15:18 +02:00
|
|
|
(policy_port, policy) = network.stsPolicies.get(
|
|
|
|
server.hostname, (None, None))
|
2019-12-08 15:54:48 +01:00
|
|
|
lastDisconnect = network.lastDisconnectTimes.get(server.hostname)
|
|
|
|
|
|
|
|
if policy is None or lastDisconnect is None:
|
2019-12-08 21:25:59 +01:00
|
|
|
log.debug('No STS policy, or never disconnected from this server. %r %r',
|
|
|
|
policy, lastDisconnect)
|
2019-12-08 15:54:48 +01:00
|
|
|
return server
|
|
|
|
|
|
|
|
# The policy was stored, which means it was received on a secure
|
|
|
|
# connection.
|
2023-06-10 08:28:08 +02:00
|
|
|
policy = ircutils.parseStsPolicy(log, policy, tls_connection=True)
|
2019-12-08 15:54:48 +01:00
|
|
|
|
|
|
|
if lastDisconnect + policy['duration'] < time.time():
|
2019-12-08 21:25:59 +01:00
|
|
|
log.info('STS policy expired, removing.')
|
2019-12-08 15:54:48 +01:00
|
|
|
network.expireStsPolicy(server.hostname)
|
|
|
|
return server
|
|
|
|
|
2021-09-03 20:15:18 +02:00
|
|
|
if server.port == policy_port:
|
2021-06-02 16:51:09 +02:00
|
|
|
log.info('Using STS policy, port %s', server.port)
|
|
|
|
else:
|
|
|
|
log.info('Using STS policy: changing port from %s to %s.',
|
2021-09-03 20:15:18 +02:00
|
|
|
server.port, policy_port)
|
2019-12-08 21:25:59 +01:00
|
|
|
|
2019-12-08 15:54:48 +01:00
|
|
|
# Change the port, and force TLS verification, as required by the STS
|
|
|
|
# specification.
|
2021-09-03 20:15:18 +02:00
|
|
|
return Server(server.hostname, policy_port, server.attempt,
|
2019-12-08 15:54:48 +01:00
|
|
|
force_tls_verification=True)
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
self.onDisconnect()
|
|
|
|
|
|
|
|
def onDisconnect(self):
|
|
|
|
network = ircdb.networks.getNetwork(self.networkName)
|
|
|
|
network.addDisconnection(self.currentServer.hostname)
|
2005-01-31 16:22:48 +01:00
|
|
|
|
2005-01-19 14:14:38 +01:00
|
|
|
|
|
|
|
def empty():
|
|
|
|
"""Returns whether or not the driver loop is empty."""
|
|
|
|
return (len(_drivers) + len(_newDrivers)) == 0
|
|
|
|
|
|
|
|
def add(name, driver):
|
|
|
|
"""Adds a given driver the loop with the given name."""
|
|
|
|
_newDrivers.append((name, driver))
|
|
|
|
|
|
|
|
def remove(name):
|
|
|
|
"""Removes the driver with the given name from the loop."""
|
2016-02-09 18:31:30 +01:00
|
|
|
_deadDrivers.add(name)
|
2005-01-19 14:14:38 +01:00
|
|
|
|
|
|
|
def run():
|
|
|
|
"""Runs the whole driver loop."""
|
2015-08-08 22:20:14 +02:00
|
|
|
for (name, driver) in _drivers.items():
|
2005-01-19 14:14:38 +01:00
|
|
|
try:
|
|
|
|
if name not in _deadDrivers:
|
|
|
|
driver.run()
|
|
|
|
except:
|
|
|
|
log.exception('Uncaught exception in in drivers.run:')
|
2016-02-09 18:31:30 +01:00
|
|
|
_deadDrivers.add(name)
|
2005-01-19 14:14:38 +01:00
|
|
|
for name in _deadDrivers:
|
|
|
|
try:
|
|
|
|
driver = _drivers[name]
|
|
|
|
if hasattr(driver, 'irc') and driver.irc is not None:
|
|
|
|
# The Schedule driver has no irc object, or it's None.
|
|
|
|
driver.irc.driver = None
|
|
|
|
driver.irc = None
|
|
|
|
log.info('Removing driver %s.', name)
|
|
|
|
del _drivers[name]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
while _newDrivers:
|
|
|
|
(name, driver) = _newDrivers.pop()
|
|
|
|
log.debug('Adding new driver %s.', name)
|
2016-02-09 18:31:30 +01:00
|
|
|
_deadDrivers.discard(name)
|
2005-01-19 14:14:38 +01:00
|
|
|
if name in _drivers:
|
|
|
|
log.warning('Driver %s already added, killing it.', name)
|
|
|
|
_drivers[name].die()
|
|
|
|
del _drivers[name]
|
|
|
|
_drivers[name] = driver
|
|
|
|
|
|
|
|
class Log(object):
|
|
|
|
"""This is used to have a nice, consistent interface for drivers to use."""
|
2021-05-26 23:43:23 +02:00
|
|
|
def connect(self, server, socks_proxy=None):
|
|
|
|
if socks_proxy:
|
|
|
|
socks_phrase = ' Via SOCKS proxy %s'
|
|
|
|
else:
|
|
|
|
socks_phrase = ''
|
|
|
|
socks_proxy = ''
|
|
|
|
self.info('Connecting to %s:%s.' + socks_phrase,
|
|
|
|
server.hostname, server.port, socks_proxy)
|
2005-01-19 14:14:38 +01:00
|
|
|
|
|
|
|
def connectError(self, server, e):
|
|
|
|
if isinstance(e, Exception):
|
|
|
|
if isinstance(e, socket.gaierror):
|
|
|
|
e = e.args[1]
|
|
|
|
else:
|
2005-02-15 14:57:57 +01:00
|
|
|
e = utils.exnToString(e)
|
2019-12-08 15:54:48 +01:00
|
|
|
self.warning('Error connecting to %s:%s: %s',
|
|
|
|
server.hostname, server.port, e)
|
2005-01-19 14:14:38 +01:00
|
|
|
|
|
|
|
def disconnect(self, server, e=None):
|
|
|
|
if e:
|
|
|
|
if isinstance(e, Exception):
|
2005-02-15 14:57:57 +01:00
|
|
|
e = utils.exnToString(e)
|
2005-01-19 14:14:38 +01:00
|
|
|
else:
|
|
|
|
e = str(e)
|
|
|
|
if not e.endswith('.'):
|
|
|
|
e += '.'
|
2019-12-08 15:54:48 +01:00
|
|
|
self.warning('Disconnect from %s:%s: %s',
|
|
|
|
server.hostname, server.port, e)
|
2005-01-19 14:14:38 +01:00
|
|
|
else:
|
|
|
|
self.info('Disconnect from %s.', server)
|
|
|
|
|
|
|
|
def reconnect(self, network, when=None):
|
|
|
|
s = 'Reconnecting to %s' % network
|
|
|
|
if when is not None:
|
2015-08-10 20:24:11 +02:00
|
|
|
if not isinstance(when, minisix.string_types):
|
2005-01-19 14:14:38 +01:00
|
|
|
when = self.timestamp(when)
|
|
|
|
s += ' at %s.' % when
|
|
|
|
else:
|
|
|
|
s += '.'
|
|
|
|
self.info(s)
|
|
|
|
|
|
|
|
def die(self, irc):
|
|
|
|
self.info('Driver for %s dying.', irc)
|
|
|
|
|
|
|
|
debug = staticmethod(supylog.debug)
|
|
|
|
info = staticmethod(supylog.info)
|
|
|
|
warning = staticmethod(supylog.warning)
|
|
|
|
error = staticmethod(supylog.warning)
|
|
|
|
critical = staticmethod(supylog.critical)
|
|
|
|
timestamp = staticmethod(supylog.timestamp)
|
|
|
|
exception = staticmethod(supylog.exception)
|
|
|
|
|
|
|
|
log = Log()
|
2005-01-31 16:22:48 +01:00
|
|
|
|
2005-01-19 14:14:38 +01:00
|
|
|
def newDriver(irc, moduleName=None):
|
|
|
|
"""Returns a new driver for the given server using the irc given and using
|
|
|
|
conf.supybot.driverModule to determine what driver to pick."""
|
|
|
|
# XXX Eventually this should be made to load the drivers from a
|
|
|
|
# configurable directory in addition to the installed one.
|
|
|
|
if moduleName is None:
|
|
|
|
moduleName = conf.supybot.drivers.module()
|
|
|
|
if moduleName == 'default':
|
2006-04-13 16:26:06 +02:00
|
|
|
moduleName = 'supybot.drivers.Socket'
|
2005-01-19 14:14:38 +01:00
|
|
|
elif not moduleName.startswith('supybot.drivers.'):
|
|
|
|
moduleName = 'supybot.drivers.' + moduleName
|
|
|
|
driverModule = __import__(moduleName, {}, {}, ['not empty'])
|
|
|
|
log.debug('Creating new driver (%s) for %s.', moduleName, irc)
|
|
|
|
driver = driverModule.Driver(irc)
|
|
|
|
irc.driver = driver
|
|
|
|
return driver
|
|
|
|
|
|
|
|
def parseMsg(s):
|
2021-03-13 18:32:36 +01:00
|
|
|
# It may be tempting to strip all whitespaces here, but it's important
|
|
|
|
# to preserve them, because they matter for multiline messages.
|
|
|
|
# https://ircv3.net/specs/extensions/multiline
|
|
|
|
s = s.strip('\r\n')
|
2005-01-19 14:14:38 +01:00
|
|
|
if s:
|
|
|
|
msg = ircmsgs.IrcMsg(s)
|
|
|
|
return msg
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|