2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:38:19 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.fix as fix
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import sys
|
2004-08-10 09:39:23 +02:00
|
|
|
import time
|
2004-08-21 09:28:27 +02:00
|
|
|
import socket
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-07-31 06:58:53 +02:00
|
|
|
import supybot.log as supylog
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
2004-08-01 16:42:25 +02:00
|
|
|
import supybot.utils as utils
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ircmsgs as ircmsgs
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
_drivers = {}
|
|
|
|
_deadDrivers = []
|
|
|
|
_newDrivers = []
|
|
|
|
|
|
|
|
class IrcDriver(object):
|
2003-10-04 13:47:21 +02:00
|
|
|
"""Base class for drivers."""
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self):
|
2004-07-27 07:17:53 +02:00
|
|
|
add(self.name(), self)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
# The end of any overrided die method should be
|
2004-01-01 21:12:40 +01:00
|
|
|
# "super(Class, self).die()", in order to make
|
2003-03-12 07:26:59 +01:00
|
|
|
# sure this (and anything else later added) is done.
|
2004-07-27 07:17:53 +02:00
|
|
|
remove(self.name())
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-01-01 21:12:40 +01:00
|
|
|
def reconnect(self, wait=False):
|
2003-04-21 06:32:42 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def name(self):
|
2003-09-03 13:06:02 +02:00
|
|
|
return repr(self)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-07-31 06:58:53 +02:00
|
|
|
class ServersMixin(object):
|
|
|
|
def __init__(self, irc, servers=()):
|
|
|
|
self.networkGroup = conf.supybot.networks.get(irc.network)
|
|
|
|
self.servers = servers
|
|
|
|
super(ServersMixin, self).__init__(irc)
|
|
|
|
|
|
|
|
def _getServers(self):
|
|
|
|
# We do this, rather than itertools.cycle the servers in __init__,
|
|
|
|
# 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 = '%s:%s' % server
|
|
|
|
return server
|
|
|
|
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def empty():
|
2003-10-04 13:47:21 +02:00
|
|
|
"""Returns whether or not the driver loop is empty."""
|
2003-03-12 07:26:59 +01:00
|
|
|
return (len(_drivers) + len(_newDrivers)) == 0
|
|
|
|
|
|
|
|
def add(name, driver):
|
2003-10-04 13:47:21 +02:00
|
|
|
"""Adds a given driver the loop with the given name."""
|
2003-03-12 07:26:59 +01:00
|
|
|
_newDrivers.append((name, driver))
|
|
|
|
|
|
|
|
def remove(name):
|
2003-10-04 13:47:21 +02:00
|
|
|
"""Removes the driver with the given name from the loop."""
|
2003-03-12 07:26:59 +01:00
|
|
|
_deadDrivers.append(name)
|
|
|
|
|
|
|
|
def run():
|
2003-10-04 13:47:21 +02:00
|
|
|
"""Runs the whole driver loop."""
|
2003-03-12 07:26:59 +01:00
|
|
|
for (name, driver) in _drivers.iteritems():
|
|
|
|
try:
|
|
|
|
if name not in _deadDrivers:
|
|
|
|
driver.run()
|
|
|
|
except:
|
2003-11-26 19:21:12 +01:00
|
|
|
log.exception('Uncaught exception in in drivers.run:')
|
2003-03-12 07:26:59 +01:00
|
|
|
_deadDrivers.append(name)
|
|
|
|
for name in _deadDrivers:
|
|
|
|
try:
|
2004-01-20 17:07:03 +01:00
|
|
|
driver = _drivers[name]
|
2004-02-12 07:50:26 +01:00
|
|
|
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
|
2004-01-20 17:07:03 +01:00
|
|
|
driver.irc = None
|
2004-07-27 07:17:53 +02:00
|
|
|
log.info('Removing driver %s.', name)
|
2003-03-12 07:26:59 +01:00
|
|
|
del _drivers[name]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
while _newDrivers:
|
2003-10-04 13:47:21 +02:00
|
|
|
(name, driver) = _newDrivers.pop()
|
2004-07-27 07:17:53 +02:00
|
|
|
log.debug('Adding new driver %s.', name)
|
2003-10-04 13:47:21 +02:00
|
|
|
if name in _drivers:
|
2004-07-27 07:17:53 +02:00
|
|
|
log.warning('Driver %s already added, killing it.', name)
|
2003-10-04 13:47:21 +02:00
|
|
|
_drivers[name].die()
|
|
|
|
del _drivers[name]
|
|
|
|
_drivers[name] = driver
|
2003-04-20 01:52:34 +02:00
|
|
|
|
2004-07-31 06:58:53 +02:00
|
|
|
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.', server)
|
|
|
|
|
|
|
|
def connectError(self, server, e):
|
|
|
|
if isinstance(e, Exception):
|
2004-08-21 09:28:27 +02:00
|
|
|
if isinstance(e, socket.gaierror):
|
|
|
|
e = e.args[1]
|
|
|
|
else:
|
|
|
|
e = utils.exnToString(e)
|
2004-07-31 06:58:53 +02:00
|
|
|
self.warning('Error connecting to %s: %s', server, e)
|
|
|
|
|
|
|
|
def disconnect(self, server, e=None):
|
|
|
|
if e:
|
|
|
|
if isinstance(e, Exception):
|
|
|
|
e = utils.exnToString(e)
|
|
|
|
self.warning('Disconnect from %s: %s.', server, e)
|
|
|
|
else:
|
|
|
|
self.info('Disconnect from %s.', server)
|
|
|
|
|
|
|
|
def reconnect(self, network, when=None):
|
|
|
|
s = 'Reconnecting to %s' % network
|
|
|
|
if when is not None:
|
|
|
|
if not isinstance(when, basestring):
|
|
|
|
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)
|
2004-08-10 09:39:23 +02:00
|
|
|
stat = staticmethod(supylog.stat)
|
2004-07-31 06:58:53 +02:00
|
|
|
|
|
|
|
log = Log()
|
|
|
|
|
2004-07-30 08:52:21 +02:00
|
|
|
def newDriver(irc, moduleName=None):
|
2004-01-18 08:58:26 +01:00
|
|
|
"""Returns a new driver for the given server using the irc given and using
|
|
|
|
conf.supybot.driverModule to determine what driver to pick."""
|
|
|
|
if moduleName is None:
|
|
|
|
moduleName = conf.supybot.drivers.module()
|
2004-04-30 04:13:42 +02:00
|
|
|
if moduleName == 'default':
|
|
|
|
try:
|
|
|
|
import twistedDrivers
|
2004-07-24 07:18:26 +02:00
|
|
|
moduleName = 'supybot.twistedDrivers'
|
2004-04-30 04:13:42 +02:00
|
|
|
except ImportError:
|
2004-07-24 07:18:26 +02:00
|
|
|
del sys.modules['supybot.twistedDrivers']
|
|
|
|
moduleName = 'supybot.socketDrivers'
|
|
|
|
elif not moduleName.startswith('supybot.'):
|
|
|
|
moduleName = 'supybot.' + moduleName
|
|
|
|
driverModule = __import__(moduleName, {}, {}, ['not empty'])
|
2004-07-30 08:52:21 +02:00
|
|
|
log.debug('Creating new driver for %s.', irc)
|
|
|
|
driver = driverModule.Driver(irc)
|
2003-04-20 01:52:34 +02:00
|
|
|
irc.driver = driver
|
|
|
|
return driver
|
|
|
|
|
2004-08-10 09:39:23 +02:00
|
|
|
def parseMsg(s):
|
|
|
|
start = time.time()
|
|
|
|
msg = ircmsgs.IrcMsg(s)
|
|
|
|
log.stat('Time to parse IrcMsg: %s', time.time()-start)
|
|
|
|
return msg
|
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|