From 570dba09a65f3f889da739da3d00461023f930b9 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 9 May 2022 15:34:00 -0400 Subject: [PATCH] move to using config.py parser stuff --- .gitignore | 6 +----- account.json.example | 4 ---- config.example.yaml | 16 +++++++++++++++ config.json.example | 7 ------- config.py | 43 +++++++++++++++++++++++++++++++++++++++++ tildeverse.json.example | 7 ------- tooter.py | 24 ++++++++++++++++------- 7 files changed, 77 insertions(+), 30 deletions(-) delete mode 100644 account.json.example create mode 100644 config.example.yaml delete mode 100644 config.json.example create mode 100644 config.py delete mode 100644 tildeverse.json.example diff --git a/.gitignore b/.gitignore index 728b360..98a626a 100644 --- a/.gitignore +++ b/.gitignore @@ -58,9 +58,5 @@ docs/_build/ # PyBuilder target/ -tildeclub.json -tildeteam.json -tildeverse.json -config.json venv/ -account.json +config.yaml diff --git a/account.json.example b/account.json.example deleted file mode 100644 index e9a2417..0000000 --- a/account.json.example +++ /dev/null @@ -1,4 +0,0 @@ -{ - "username": "bensbots", - "password": "my super secret password" -} diff --git a/config.example.yaml b/config.example.yaml new file mode 100644 index 0000000..ab54cb4 --- /dev/null +++ b/config.example.yaml @@ -0,0 +1,16 @@ +server: localhost:6667 +nickname: tooter +channel: "#meta" + +sasl: + username: cloaks + password: hunter3 + +mastodon: + - name: tildeverse + client_id: 1234566 + client_secret: beeeep + access_token: you do not want to know + base_url: https://tilde.zone + channels: + - '#club' diff --git a/config.json.example b/config.json.example deleted file mode 100644 index 4ec47b1..0000000 --- a/config.json.example +++ /dev/null @@ -1,7 +0,0 @@ -{ - "channels": [], - "address": "127.0.0.1", - "port": 6667, - "tls": false, - "botnick": "tooter" -} 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"]), + ) + diff --git a/tildeverse.json.example b/tildeverse.json.example deleted file mode 100644 index 5fdd596..0000000 --- a/tildeverse.json.example +++ /dev/null @@ -1,7 +0,0 @@ -{ - "client_id": "myclientid", - "client_secret": "myclientsecret", - "access_token": "myaccesstoken", - "base_url": "https://tilde.zone", - "channel": "remove this key if you don't want to limit the bot" -} diff --git a/tooter.py b/tooter.py index 8a54a68..6447801 100644 --- a/tooter.py +++ b/tooter.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 -from mastodon import Mastodon -from irctokens import build, Line +from .config import Config from ircrobots import Bot as BaseBot -from ircrobots import Server as BaseServer from ircrobots import ConnectionParams, SASLUserPass +from ircrobots import Server as BaseServer +from irctokens import build, Line +from mastodon import Mastodon import asyncio import emoji import glob @@ -68,6 +69,14 @@ def think(line): class Server(BaseServer): + def __init__(self, + bot: BaseBot, + name: str, + config: Config): + + super().__init__(bot, name) + self._config = config + async def line_send(self, line: Line): print(f"{self.name} > {line.format()}") @@ -102,12 +111,13 @@ class Bot(BaseBot): async def main(): params = ConnectionParams( - config["botnick"], - host=config["address"], - port=config["port"], - tls=config["tls"], + config.get("botnick", "tooter"), + config.get("address", "127.0.0.1"), + config.get("port", 6667), + config.get("tls", False), sasl=SASLUserPass(account["username"], account["password"]), ) + bot = Bot() await bot.add_server("tilde", params) await bot.run()