Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
luk3yx committed Dec 14, 2022
1 parent a0fcb45 commit 2b24352
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
15 changes: 12 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ Notes:
- This changelog may contain typographical errors, it is still a
work-in-progress.

## 1.9.1 - 2022-12-14

### Changed

- Fixed handling of socket timeouts when trying to recover nickname
- The socket is now closed if there's a connection timeout while writing data
since the socket may have partially written data.
- Removed use of the deprecated `socket.error`

## 1.9.0 - 2022-12-13

### Added

- miniirc will now attempt to regain the originally specified nickname if it
cannot used when connecting. For compatibility, `irc.nick` will return the
current nickname while connected, however changing it will change the
desired nickname. This may change in the future.
cannot used when connecting. For compatibility, `irc.nick` will return the
current nickname while connected, however changing it will change the
desired nickname. This may change in the future.

### Changed

Expand Down
36 changes: 22 additions & 14 deletions miniirc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import atexit, threading, time, select, socket, ssl, sys, warnings

# The version string and tuple
ver = __version_info__ = (1, 9, 0)
version = 'miniirc IRC framework v1.9.0'
__version__ = '1.9.0'
ver = __version_info__ = (1, 9, 1)
version = 'miniirc IRC framework v1.9.1'
__version__ = '1.9.1'

# __all__ and _default_caps
__all__ = ['CmdHandler', 'Handler', 'IRC']
Expand Down Expand Up @@ -335,8 +335,17 @@ def quote(self, *msg, force=None, tags=None):
# Otherwise wait for the socket to become writable
select.select((), (self.sock,), (self.sock,),
self.ping_timeout or self.ping_interval)
except (AttributeError, BrokenPipeError, socket.timeout):
# TODO: Consider not silently ignoring timeouts
except socket.timeout:
# Abort the connection if there was a timeout because the data may
# have been partially written
try:
self.sock.close()
except OSError:
pass

if force:
raise
except (AttributeError, BrokenPipeError):
if force:
raise
finally:
Expand Down Expand Up @@ -541,15 +550,20 @@ def _main(self):
select.select((), (self.sock,), (self.sock,),
self.ping_timeout or self.ping_interval)

except (OSError, socket.error) as e:
# Attempt to change nicknames every 30 seconds
if (self._keepnick_active and
time.monotonic() > self._last_keepnick_attempt + 30):
self.send('NICK', self._desired_nick, force=True)
self._last_keepnick_attempt = time.monotonic()
except OSError as e:
self.debug('Lost connection!', repr(e))
self.disconnect(auto_reconnect=True)
while self.persist:
time.sleep(5)
self.debug('Reconnecting...')
try:
self.connect()
except (OSError, socket.error):
except OSError:
self.debug('Failed to reconnect!')
self.connected = None
else:
Expand All @@ -565,20 +579,14 @@ def _main(self):
self.debug('<<<', line)
try:
result = self._parse(line)
except:
except Exception:
result = None
if isinstance(result, tuple) and len(result) == 4:
self._handle(*result)
else:
self.debug('Ignored message:', line)
del raw

# Attempt to change nicknames every 30 seconds
if (self._keepnick_active and
time.monotonic() > self._last_keepnick_attempt + 30):
self.send('NICK', self._desired_nick)
self._last_keepnick_attempt = time.monotonic()

def wait_until_disconnected(self, *, _timeout=None):
# The main thread may be replaced on reconnects
while self._main_thread and self._main_thread.is_alive():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='miniirc',
version='1.9.0',
version='1.9.1',
py_modules=['miniirc'],
author='luk3yx',
description='A lightweight IRC framework.',
Expand Down

0 comments on commit 2b24352

Please sign in to comment.