Fix some bugs I introduced with the conversion to utils.getSocket and improve

DCC's error checking.
This commit is contained in:
James Vega 2004-06-20 18:03:11 +00:00
parent 1383da0f1a
commit 847227622f
3 changed files with 19 additions and 10 deletions

View File

@ -61,13 +61,12 @@ class DCC(callbacks.Privmsg):
try: try:
host = ircutils.hostFromHostmask(irc.prefix) host = ircutils.hostFromHostmask(irc.prefix)
try: try:
inet = utils.getSocket(host) sock = utils.getSocket(host)
except socket.error, e: except socket.error, e:
s = 'Error connecting to %s: %s' s = 'Error connecting to %s: %s'
self.log.warning(s, host, e) self.log.warning(s, host, e)
irc.replyError() irc.replyError()
return return
sock = socket.socket(inet, socket.SOCK_STREAM)
sock.settimeout(60) sock.settimeout(60)
if conf.supybot.externalIP(): if conf.supybot.externalIP():
ip = conf.supybot.externalIP() ip = conf.supybot.externalIP()
@ -81,21 +80,32 @@ class DCC(callbacks.Privmsg):
irc.replyError() irc.replyError()
return return
i = ircutils.dccIP(ip) i = ircutils.dccIP(ip)
try:
sock.bind((host, 0)) sock.bind((host, 0))
except socket.error, e:
irc.error('Unable to initiate DCC CHAT.')
return
port = sock.getsockname()[1] port = sock.getsockname()[1]
self.log.info('DCC CHAT port opened at (%s, %s)', host, port) self.log.info('DCC CHAT port opened at (%s, %s)', host, port)
sock.listen(1) sock.listen(1)
irc.queueMsg(ircmsgs.privmsg(msg.nick, irc.queueMsg(ircmsgs.privmsg(msg.nick,
'\x01DCC CHAT chat %s %s\x01' % \ '\x01DCC CHAT chat %s %s\x01' % \
(i, port))) (i, port)))
try:
(realSock, addr) = sock.accept() (realSock, addr) = sock.accept()
except socket.timeout:
self.log.info('DCC CHAT timed out.')
return
self.log.info('DCC CHAT accepted from %s', addr) self.log.info('DCC CHAT accepted from %s', addr)
for line in textwrap.wrap(text, 80): for line in textwrap.wrap(text, 80):
realSock.send(line) realSock.send(line)
realSock.send('\n') realSock.send('\n')
finally: finally:
self.log.info('Finally closing sock and realSock.') self.log.info('Finally closing sock and realSock.')
try:
sock.close() sock.close()
except UnboundLocalError:
pass
try: try:
realSock.close() realSock.close()
except UnboundLocalError: except UnboundLocalError:

View File

@ -140,12 +140,11 @@ class SocketDriver(drivers.IrcDriver):
return return
self.irc.reset() self.irc.reset()
try: try:
inet = utils.getSocket(self.server[0]) self.conn = utils.getSocket(self.server[0])
except socket.error, e: except socket.error, e:
log.warning('Error connecting to %s: %s', self.server[0], e) log.warning('Error connecting to %s: %s', self.server[0], e)
self.reconnect(wait=True) self.reconnect(wait=True)
return return
self.conn = socket.socket(inet, socket.SOCK_STREAM)
# We allow more time for the connect here, since it might take longer. # We allow more time for the connect here, since it might take longer.
# At least 10 seconds. # At least 10 seconds.
self.conn.settimeout(max(10, conf.supybot.drivers.poll()*10)) self.conn.settimeout(max(10, conf.supybot.drivers.poll()*10))

View File

@ -564,9 +564,9 @@ def getSocket(host):
except socket.error: except socket.error:
raise raise
if isIP(host): if isIP(host):
return socket.socket(socket.INET, socket.SOCK_STREAM) return socket.socket(socket.AF_INET, socket.SOCK_STREAM)
elif isIPV6(host): elif isIPV6(host):
return socket.socket(socket.INET6, socket.SOCK_STREAM) return socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else: else:
raise socket.error, 'Something wonky happened.' raise socket.error, 'Something wonky happened.'