Limnoria/src/drivers/__init__.py

251 lines
8.9 KiB
Python
Raw Normal View History

2005-01-19 14:14:38 +01:00
###
# Copyright (c) 2002-2004, Jeremiah Fincher
# Copyright (c) 2008-2009, James McCoy
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.
"""
import time
2005-01-19 14:14:38 +01:00
import socket
from collections import namedtuple
2005-01-19 14:14:38 +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
Server = namedtuple('Server', 'hostname port force_tls_verification')
# 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 = {}
_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)
super(IrcDriver, self).__init__(*args, **kwargs)
2005-01-19 14:14:38 +01:00
def run(self):
raise NotImplementedError
def die(self):
# The end of any overrided die method should be
# "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=()):
self.networkName = irc.network
2005-01-19 14:14:38 +01:00
self.networkGroup = conf.supybot.networks.get(irc.network)
self.servers = servers
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)
self.currentServer = self._applyStsPolicy(server)
return self.currentServer
def _applyStsPolicy(self, server):
network = ircdb.networks.getNetwork(self.networkName)
policy = network.stsPolicies.get(server.hostname)
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)
return server
# The policy was stored, which means it was received on a secure
# connection.
policy = ircutils.parseStsPolicy(log, policy, parseDuration=True)
if lastDisconnect + policy['duration'] < time.time():
2019-12-08 21:25:59 +01:00
log.info('STS policy expired, removing.')
network.expireStsPolicy(server.hostname)
return server
2019-12-08 21:25:59 +01:00
log.info('Using STS policy: changing port from %s to %s.',
server.port, policy['port'])
# Change the port, and force TLS verification, as required by the STS
# specification.
return Server(server.hostname, policy['port'],
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."""
_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:')
_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)
_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."""
def connect(self, server):
self.info('Connecting to %s:%s.',
server.hostname, server.port)
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:
e = utils.exnToString(e)
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):
e = utils.exnToString(e)
2005-01-19 14:14:38 +01:00
else:
e = str(e)
if not e.endswith('.'):
e += '.'
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':
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):
s = s.strip()
if s:
msg = ircmsgs.IrcMsg(s)
return msg
else:
return None
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: