about summary refs log tree commit diff
path: root/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'config.py')
-rw-r--r--config.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/config.py b/config.py
new file mode 100644
index 0000000..958c095
--- /dev/null
+++ b/config.py
@@ -0,0 +1,43 @@
+from dataclasses import dataclass
+from os.path     import expanduser
+from re          import compile as re_compile
+from typing      import List, Pattern, Tuple
+
+import yaml
+
+@dataclass
+class Config(object):
+    server:   Tuple[str, int, bool]
+    nickname: str
+    username: str
+    realname: str
+    password: str
+    channel:  str
+
+    sasl: Tuple[str, str]
+
+def load(filepath: str):
+    with open(filepath) as file:
+        config_yaml = yaml.safe_load(file.read())
+
+    nickname = config_yaml["nickname"]
+
+    server   = config_yaml["server"]
+    hostname, port_s = server.split(":", 1)
+    tls      = False
+
+    if port_s.startswith("+"):
+        tls    = True
+        port_s = port_s.lstrip("+")
+    port = int(port_s)
+
+    return Config(
+        (hostname, port, tls),
+        nickname,
+        config_yaml.get("username", nickname),
+        config_yaml.get("realname", nickname),
+        config_yaml["password"],
+        config_yaml["channel"],
+        (config_yaml["sasl"]["username"], config_yaml["sasl"]["password"]),
+    )
+