from dataclasses import dataclass, InitVar
from enum import Enum, auto
from getpass import getuser
+from threading import Thread
from typing import (Any, Callable, Collection, Generic, Iterable, Iterator,
Optional, Self, Set, TypeVar)
# ourselves
-from ircplom.events import AffectiveEvent, QueueMixin
+from ircplom.events import (
+ AffectiveEvent, CrashingException, ExceptionEvent, QueueMixin)
from ircplom.irc_conn import (
BaseIrcConnection, IrcConnAbortException, IrcMessage,
ILLEGAL_NICK_CHARS, ILLEGAL_NICK_FIRSTCHARS, ISUPPORT_DEFAULTS, PORT_SSL)
def connect(self) -> None:
'Attempt to open connection, on success perform session init steps.'
self.db.connection_state = 'connecting'
- try:
- self.conn = self._cls_conn(
- hostname=self.db.hostname, port=self.db.port,
- _q_out=self._q_out, client_id=self.client_id)
- except IrcConnAbortException as e:
- self.db.connection_state = f'failed to connect: {e}'
- return
- self.db.connection_state = 'connected'
- self.caps.start_negotation()
- self.send('USER', self.db.user_wanted,
- '0', '*', self.db.realname)
- self.send('NICK', self.db.nick_wanted,)
+
+ def connect(self) -> None:
+ try:
+ self.conn = self._cls_conn(
+ hostname=self.db.hostname, port=self.db.port,
+ _q_out=self._q_out, client_id=self.client_id)
+ except IrcConnAbortException as e:
+ self.db.connection_state = f'failed to connect: {e}'
+ except Exception as e: # pylint: disable=broad-exception-caught
+ self._put(ExceptionEvent(CrashingException(e)))
+ else:
+ self.db.connection_state = 'connected'
+ self.caps.start_negotation()
+ self.send('USER', self.db.user_wanted,
+ '0', '*', self.db.realname)
+ self.send('NICK', self.db.nick_wanted,)
+
+ # Do this in a thread, not to block flow of other (e.g. TUI) events.
+ Thread(target=connect, daemon=True, args=(self,)).start()
def close(self) -> None:
'Close connection and wipe memory of its states.'