Socket driver: implement ssl connection support.

Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
Daniel Folkinshteyn 2010-07-11 02:29:02 -04:00 committed by James McCoy
parent 4661acb3a3
commit df5391dcf7

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,11 +68,6 @@ 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
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() self.connect()
def getDelay(self): def getDelay(self):
@ -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: