diff --git a/scripts/weechatrn.py b/scripts/weechatrn.py index 39c09d8..3209914 100644 --- a/scripts/weechatrn.py +++ b/scripts/weechatrn.py @@ -1,5 +1,24 @@ import weechat import json +from collections import UserDict + + +class Options(UserDict[str, str]): + """Helper object for accessing configuration settings.""" + + def __init__(self): + UserDict[str, str].__init__(self) + + def push_tokens(self) -> list[str]: + """Return comma separated push tokens as an array.""" + if script_options["push_tokens"]: + return self.data["push_tokens"].split(",") + return [] + + def notify_current_buffer(self): + """Return notify_current_buffer option as boolean.""" + return weechat.config_string_to_boolean(self.data["notify_current_buffer"]) + script_options_default = { "push_tokens": ( @@ -11,7 +30,7 @@ "Option to send notifications for the current buffer", ), } -script_options: dict[str, str] = {} +script_options = Options() def weechatrn_cb(data: str, buffer: str, args: str) -> int: @@ -19,11 +38,7 @@ def weechatrn_cb(data: str, buffer: str, args: str) -> int: Command to allow managing push tokens without the need to allow /set access to relay clients. """ - tokens = ( - script_options["push_tokens"].split(",") - if script_options["push_tokens"] - else [] - ) + tokens = script_options.push_tokens() if args not in tokens: tokens.append(args) weechat.config_set_plugin("push_tokens", ",".join(tokens)) @@ -58,7 +73,7 @@ def priv_msg_cb( return weechat.WEECHAT_RC_OK if ( - not weechat.config_string_to_boolean(script_options["notify_current_buffer"]) + not script_options.notify_current_buffer() and weechat.current_buffer() == buffer ): return weechat.WEECHAT_RC_OK @@ -83,11 +98,7 @@ def send_push(title: str, body: str) -> None: "title": "Notification title", "body": "Notification body" }] """ - push_tokens = ( - script_options["push_tokens"].split(",") - if script_options["push_tokens"] - else [] - ) + push_tokens = script_options.push_tokens() if push_tokens == []: return @@ -125,7 +136,7 @@ def remove_unregistered_devices(response: str) -> None: except json.JSONDecodeError: pass else: - tokens = script_options["push_tokens"].split(",") + tokens = script_options.push_tokens() for index, status in enumerate(statuses["data"]): if (