diff --git a/LICENSE b/LICENSE index e6d06e9..3bbbcfa 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Tom Casavant +Copyright (c) 2022 Tom Casavant Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/base-config.yaml b/base-config.yaml new file mode 100644 index 0000000..19fee94 --- /dev/null +++ b/base-config.yaml @@ -0,0 +1,2 @@ +# Reddit URL, set to old.reddit.com, etc. +reddit_base: reddit.com # https://{reddit_base}/r/... diff --git a/maubot.yaml b/maubot.yaml index 31809bc..b81b00b 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -9,7 +9,7 @@ maubot: 0.1.0 id: casavant.tom.reddit # A PEP 440 compliant version string. -version: 1.4.0 +version: 1.5.0 # The SPDX license identifier for the plugin. https://spdx.org/licenses/ # Optional, assumes all rights reserved if omitted. @@ -32,8 +32,8 @@ main_class: RedditPlugin database: false # Extra files that the upcoming build tool should include in the mbp file. -#extra_files: -#- base-config.yaml +extra_files: + - base-config.yaml #- LICENSE # List of dependencies diff --git a/reddit.py b/reddit.py index 1824eac..2b6d2df 100644 --- a/reddit.py +++ b/reddit.py @@ -1,17 +1,31 @@ from random import choice -from typing import List, Tuple +from typing import List, Tuple, Type import urllib.parse from maubot import Plugin, MessageEvent from maubot.handlers import command +from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper +# Setup config file +class Config(BaseProxyConfig): + def do_update(self, helper: ConfigUpdateHelper) -> None: + helper.copy("reddit_base") class RedditPlugin(Plugin): + async def start(self) -> None: + await super().start() + self.config.load_and_update() + + @classmethod + def get_config_class(cls) -> Type[BaseProxyConfig]: + return Config + @command.passive("((^| )r\/)([^\s^,^.]+)", multiple=True) async def handler(self, evt: MessageEvent, subs: List[Tuple[str, str]]) -> None: await evt.mark_read() subreddits = [] # List of all subreddits given by user for _, r_slash, __, sub_str in subs: - link = "https://reddit.com/r/{}".format(urllib.parse.quote(sub_str.lower())) + base_url = self.config["reddit_base"] + link = "https://{}/r/{}".format(base_url, urllib.parse.quote(sub_str.lower())) async with self.http.head( link, headers={"User-agent": "redditmaubot"}, allow_redirects=True