Socket driver: implement ssl connection support.

This commit is contained in:
Daniel Folkinshteyn 2010-07-11 02:29:02 -04:00
parent 6df6d477f1
commit a9e2fc7bed

View File

@ -46,6 +46,13 @@ import supybot.drivers as drivers
import supybot.schedule as schedule import supybot.schedule as schedule
from supybot.utils.iter import imap from supybot.utils.iter import imap
try:
import ssl
except ImportError:
drivers.log.debug('ssl module is not available, '
'cannot connect to SSL servers.')
ssl = None
class SocketDriver(drivers.IrcDriver, drivers.ServersMixin): class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
def __init__(self, irc): def __init__(self, irc):
self.irc = irc self.irc = irc
@ -61,12 +68,7 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
self.writeCheckTime = None self.writeCheckTime = None
self.nextReconnectTime = None self.nextReconnectTime = None
self.resetDelay() self.resetDelay()
# Only connect to non-SSL servers self.connect()
if self.networkGroup.get('ssl').value:
drivers.log.error('The Socket driver can not connect to SSL '
'servers. Try the Twisted driver instead.')
else:
self.connect()
def getDelay(self): def getDelay(self):
ret = self.currentDelay ret = self.currentDelay
@ -139,6 +141,12 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
self.irc.feedMsg(msg) self.irc.feedMsg(msg)
except socket.timeout: except socket.timeout:
pass pass
except ssl.SSLError, e:
if e.args[0] == 'The read operation timed out':
pass
else:
self._handleSocketError(e)
return
except socket.error, e: except socket.error, e:
self._handleSocketError(e) self._handleSocketError(e)
return return
@ -163,6 +171,14 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
drivers.log.connect(self.currentServer) drivers.log.connect(self.currentServer)
try: try:
self.conn = utils.net.getSocket(server[0]) self.conn = utils.net.getSocket(server[0])
if self.networkGroup.get('ssl').value:
if ssl:
self.plainconn = self.conn
self.conn = ssl.wrap_socket(self.conn)
else:
drivers.log.error('ssl module not available, '
'cannot connect to SSL servers.')
return
vhost = conf.supybot.protocols.irc.vhost() vhost = conf.supybot.protocols.irc.vhost()
self.conn.bind((vhost, 0)) self.conn.bind((vhost, 0))
except socket.error, e: except socket.error, e: