Refactored Irc/Driver interaction.

This commit is contained in:
Jeremy Fincher 2004-01-01 20:12:01 +00:00
parent 26995a7017
commit 2b32ccdc7d
4 changed files with 29 additions and 26 deletions

View File

@ -51,7 +51,7 @@ import schedule
class AsyncoreRunnerDriver(drivers.IrcDriver):
def run(self):
log.printf(repr(asyncore.socket_map))
log.debug(repr(asyncore.socket_map))
try:
asyncore.poll(conf.poll)
except:
@ -101,7 +101,7 @@ class AsyncoreDriver(asynchat.async_chat, object):
def found_terminator(self):
start = time.time()
msg = ircmsgs.IrcMsg(self.buffer)
log.verbose('Time to parse IrcMsg: %s', time.time()-start)
log.debug('Time to parse IrcMsg: %s', time.time()-start)
self.buffer = ''
try:
self.irc.feedMsg(msg)

View File

@ -36,7 +36,7 @@ import fix
import copy
import sets
import time
from itertools import imap, chain
from itertools import imap, chain, cycle
import log
import conf
@ -106,7 +106,7 @@ class IrcCallback(IrcCommandDispatcher):
log.exception(s)
def reset(self):
"""Resets the callback. Called when reconnected to the server."""
"""Resets the callback. Called when reconnecting to the server."""
pass
def die(self):
@ -407,6 +407,7 @@ class Irc(IrcCommandDispatcher):
world.ircs.append(self)
self.originalNick = intern(nick)
self.nick = self.originalNick
self.nickmods = cycle(conf.nickmods)
self.password = password
self.user = intern(user or nick) # Default to nick
self.ident = intern(ident or nick) # Ditto.
@ -417,7 +418,6 @@ class Irc(IrcCommandDispatcher):
self.callbacks = callbacks
self.state = IrcState()
self.queue = IrcMsgQueue()
self._nickmods = copy.copy(conf.nickmods)
self.lastTake = 0
self.server = None
self.afterConnect = False
@ -431,7 +431,7 @@ class Irc(IrcCommandDispatcher):
self.queue.enqueue(ircmsgs.user(self.ident, self.user))
def reset(self):
"""Resets the Irc object. Useful for handling reconnects."""
"""Resets the Irc object. Called when the driver reconnects."""
self.nick = self.originalNick
self.prefix = '%s!%s@%s' % (self.nick, self.ident, 'unset.domain')
self.state.reset()
@ -540,9 +540,14 @@ class Irc(IrcCommandDispatcher):
return None
def do001(self, msg):
"""Prints some logging."""
"""Does some logging."""
log.info('Received 001 from the server.')
log.info('Hostmasks of user 0: %r' % ircdb.users.getUser(0).hostmasks)
log.info('Hostmasks of user 0: %r', ircdb.users.getUser(0).hostmasks)
def do002(self, msg):
"""Logs the ircd version."""
(beginning, version) = rsplit(msg.args[0], maxsplit=1)
log.info('Server %s has version %s', self.server, version)
def doPing(self, msg):
"""Handles PING messages."""
@ -558,22 +563,15 @@ class Irc(IrcCommandDispatcher):
def do433(self, msg):
"""Handles 'nickname already in use' messages."""
if not self._nickmods:
self._nickmods = conf.nickmods[:]
self.sendMsg(ircmsgs.nick(self._nickmods.pop(0) % self.originalNick))
self.sendMsg(ircmsgs.nick(self.nickmods.next() % self.originalNick))
do432 = do433
def doError(self, msg):
"""Handles ERROR messages."""
if msg.args[0].startswith('Closing Link'):
if hasattr(self.driver, 'scheduleReconnect'):
self.driver.scheduleReconnect()
self.driver.die()
self.driver.reconnect()
elif 'too fast' in msg.args[0]:
if hasattr(self.driver, 'reconnectWaitsIndex'):
newIndex = len(self.driver.reconnectWaits)-1
self.driver.reconnectWaitsIndex = newIndex
self.driver.die()
self.driver.reconnect(wait=True)
def doNick(self, msg):
"""Handles NICK messages."""
@ -594,6 +592,8 @@ class Irc(IrcCommandDispatcher):
self.prefix = intern(self.prefix)
log.info('Changing user 0 hostmask to %r' % self.prefix)
elif conf.followIdentificationThroughNickChanges:
# We use elif here because this means it's someone else's nick
# change, not our own.
try:
id = ircdb.users.getUserId(msg.prefix)
u = ircdb.users.getUser(id)
@ -670,8 +670,6 @@ class Irc(IrcCommandDispatcher):
log.info('Irc object for %s dying.' % self.server)
for callback in self.callbacks:
callback.die()
if self.driver is not None:
self.driver.die()
if self in world.ircs:
world.ircs.remove(self)

View File

@ -98,7 +98,7 @@ class SocketDriver(drivers.IrcDriver):
for line in lines:
start = time.time()
msg = ircmsgs.IrcMsg(line)
log.verbose('Time to parse IrcMsg: %s', time.time()-start)
log.debug('Time to parse IrcMsg: %s', time.time()-start)
try:
self.irc.feedMsg(msg)
except:
@ -113,7 +113,11 @@ class SocketDriver(drivers.IrcDriver):
return
self._sendIfMsgs()
def reconnect(self):
def reconnect(self, wait=False):
self.conn.close()
if wait:
self._scheduleReconnect()
return
self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.conn.settimeout(conf.poll*10) # Allow more time for connect.
if self.reconnectWaitsIndex < len(self.reconnectWaits)-1:
@ -124,14 +128,15 @@ class SocketDriver(drivers.IrcDriver):
except socket.error, e:
if e.args[0] != 115:
log.warning('Error connecting to %s: %s', self.server, e)
self.die()
self.reconnect(wait=True)
self.connected = True
self.reconnectWaitPeriodsIndex = 0
def die(self):
self.irc.die()
def _scheduleReconnect(self):
self.irc.reset()
self.conn.close()
self.connected = False
when = time.time() + self.reconnectWaits[self.reconnectWaitsIndex]
whenS = time.strftime(conf.logTimestampFormat, time.localtime(when))

View File

@ -62,7 +62,7 @@ class SupyIrcProtocol(LineReceiver):
def lineReceived(self, line):
start = time.time()
msg = ircmsgs.IrcMsg(line)
log.verbose('Time to parse IrcMsg: %s', time.time()-start)
log.debug('Time to parse IrcMsg: %s', time.time()-start)
try:
self.factory.irc.feedMsg(msg)
except: