home · contact · privacy
Add reading of config files to auto-connect, auto-join.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 26 Sep 2025 02:24:13 +0000 (04:24 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 26 Sep 2025 02:24:13 +0000 (04:24 +0200)
src/ircplom/client.py
src/ircplom/client_tui.py
src/ircplom/testing.py

index 1473807b4019100c360d640638add7cefa80c63c..b6d0c24f11477c9c01b1c6e7af1ddc4bead96c0e 100644 (file)
@@ -813,8 +813,10 @@ class Client(ABC, ClientQueueMixin):
     conn: Optional[IrcConnection] = None
     _cls_conn: type[IrcConnection] = IrcConnection
 
-    def __init__(self, conn_setup: IrcConnSetup, **kwargs) -> None:
+    def __init__(self, conn_setup: IrcConnSetup, channels: set[str], **kwargs
+                 ) -> None:
         self.client_id = conn_setup.hostname
+        self._autojoins = channels
         super().__init__(client_id=self.client_id, **kwargs)
         self.db = _ClientDb(on_update=self._on_update)
         self.db.clear()
@@ -825,7 +827,6 @@ class Client(ABC, ClientQueueMixin):
             self.db.port = PORT_SSL
         if not self.db.user_wanted:
             self.db.user_wanted = getuser()
-        self._autojoins: set[str] = set()
 
     def connect(self) -> None:
         'Attempt to open connection, on success perform session init steps.'
index 5413577a1a47424f486bb7f8b6016c08c17ccdec..190d0dcbe9ab84c4824758e81d6155aa37ad276b 100644 (file)
@@ -3,6 +3,7 @@
 from enum import Enum, auto
 from getpass import getuser
 from pathlib import Path
+from tomllib import load as toml_load
 from typing import Any, Callable, Optional, Sequence
 # ourselves
 from ircplom.tui_base import (BaseTui, PromptWidget, TuiEvent, Window,
@@ -26,6 +27,7 @@ _LOG_PREFIX_OUT = '>'
 _LOG_PREFIX_IN = '<'
 
 _PATH_LOGS = Path.home().joinpath('.local', 'share', 'ircplom', 'logs')
+_PATH_CONFIG = Path.home().joinpath('.config', 'ircplom', 'ircplom.toml')
 
 
 class _LogScope(Enum):
@@ -427,10 +429,34 @@ class _ClientWindowsManager:
 
 class ClientTui(BaseTui):
     'TUI expanded towards Client features.'
+    _path_config: Optional[Path] = _PATH_CONFIG
 
     def __init__(self, **kwargs) -> None:
         super().__init__(**kwargs)
         self._client_mngrs: dict[str, _ClientWindowsManager] = {}
+        config = {}
+        if self._path_config is not None:
+            if self._path_config.exists():
+                self.log(f'Found config at {self._path_config}, reading …')
+                assert self._path_config.is_file()
+                with open(self._path_config, 'rb') as f:
+                    config = toml_load(f)
+            else:
+                self.log(
+                    f'No config at {self._path_config}, proceeding without.')
+        for server_conf in config.get('server', []):
+            self.log(f'Connecting: {server_conf}')
+            hostname = server_conf['hostname']
+            port = server_conf.get('port', -1)
+            nickname = server_conf.get('nickname', getuser())
+            password = server_conf.get('password', '')
+            username = server_conf.get('username', '')
+            realname = server_conf.get('realname', nickname)
+            setup = IrcConnSetup(
+                    hostname, port, nickname, username, realname, password)
+            self._put(NewClientEvent(self._new_client(
+                setup, set(server_conf.get('channels', [])))))
+        self.redraw_affected()
 
     def _log_target_wins(self, **kwargs) -> Sequence[Window]:
         if (scope := kwargs.get('scope', None)):
@@ -449,8 +475,10 @@ class ClientTui(BaseTui):
         if getattr(self._client_mngrs[client_id], todo)(**kwargs) is not False:
             self.redraw_affected()
 
-    def _new_client(self, conn_setup: IrcConnSetup) -> 'ClientKnowingTui':
-        return ClientKnowingTui(_q_out=self._q_out, conn_setup=conn_setup)
+    def _new_client(self, conn_setup: IrcConnSetup, channels: set[str]
+                    ) -> 'ClientKnowingTui':
+        return ClientKnowingTui(
+                _q_out=self._q_out, conn_setup=conn_setup, channels=channels)
 
     def cmd__connect(self,
                      host_port: str,
@@ -480,8 +508,9 @@ class ClientTui(BaseTui):
         else:
             username = ''
             realname = username_realname
-        self._put(NewClientEvent(self._new_client(IrcConnSetup(
-            hostname, port, nickname, username, realname, password))))
+        setup = IrcConnSetup(
+                hostname, port, nickname, username, realname, password)
+        self._put(NewClientEvent(self._new_client(setup, set())))
         return None
 
 
index bc87e55c2592e4367982ebd536517178ce230d31..4fbab34726bcb596a65d03194fa91975ca0b37a7 100644 (file)
@@ -92,6 +92,7 @@ class _TestClientKnowingTui(ClientKnowingTui):
 
 class TestingClientTui(ClientTui):
     'Collects keypresses via TestTerminal and test file, compares log results.'
+    _path_config = None
     _clients: list[_TestClientKnowingTui]
 
     def __init__(self, **kwargs) -> None:
@@ -104,9 +105,11 @@ class TestingClientTui(ClientTui):
         self._playbook_idx = -1
         self._play_till_next_log()
 
-    def _new_client(self, conn_setup: IrcConnSetup) -> _TestClientKnowingTui:
+    def _new_client(self, conn_setup: IrcConnSetup, channels: set[str]
+                    ) -> _TestClientKnowingTui:
         self._clients += [_TestClientKnowingTui(_q_out=self._q_out,
-                                                conn_setup=conn_setup)]
+                                                conn_setup=conn_setup,
+                                                channels=channels)]
         return self._clients[-1]
 
     def log(self, msg: str, **kwargs) -> tuple[tuple[int, ...], str]: