From 9ad8757ab557995a049736ca784b770ce95e377b Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Sun, 15 Jan 2023 01:44:40 +0100 Subject: Rename init --- slack/init.py | 110 ------------------------------------------------------ slack/register.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 110 deletions(-) delete mode 100644 slack/init.py create mode 100644 slack/register.py (limited to 'slack') diff --git a/slack/init.py b/slack/init.py deleted file mode 100644 index dd3c080..0000000 --- a/slack/init.py +++ /dev/null @@ -1,110 +0,0 @@ -from __future__ import annotations - -import socket - -import weechat -from websocket import WebSocketConnectionClosedException - -from slack.commands import register_commands -from slack.config import SlackConfig -from slack.shared import shared -from slack.slack_conversation import get_conversation_from_buffer_pointer -from slack.task import create_task, sleep -from slack.util import get_callback_name, with_color - -SCRIPT_AUTHOR = "Trygve Aaberge " -SCRIPT_LICENSE = "MIT" -SCRIPT_DESC = "Extends weechat for typing notification/search/etc on slack.com" -REPO_URL = "https://github.com/wee-slack/wee-slack" - - -def shutdown_cb(): - weechat.config_write(shared.config.weechat_config.pointer) - return weechat.WEECHAT_RC_OK - - -def signal_buffer_switch_cb(data: str, signal: str, buffer_pointer: str) -> int: - conversation = get_conversation_from_buffer_pointer(buffer_pointer) - if conversation: - create_task(conversation.fill_history()) - return weechat.WEECHAT_RC_OK - - -def modifier_input_text_display_with_cursor_cb( - data: str, modifier: str, buffer_pointer: str, string: str -) -> str: - prefix = "" - conversation = get_conversation_from_buffer_pointer(buffer_pointer) - if conversation: - input_delim_color = weechat.config_string( - weechat.config_get("weechat.bar.input.color_delim") - ) - input_delim_start = with_color(input_delim_color, "[") - input_delim_end = with_color(input_delim_color, "]") - if not conversation.workspace.is_connected: - prefix += ( - f"{input_delim_start}" - f"{with_color(shared.config.color.disconnected.value, 'disconnected')}" - f"{input_delim_end} " - ) - if conversation.is_loading: - prefix += ( - f"{input_delim_start}" - f"{with_color(shared.config.color.loading.value, 'loading')}" - f"{input_delim_end} " - ) - return prefix + string - - -def ws_ping_cb(data: str, remaining_calls: int) -> int: - for workspace in shared.workspaces.values(): - if workspace.is_connected: - try: - workspace.ws.ping() - # workspace.last_ping_time = time.time() - except (WebSocketConnectionClosedException, socket.error) as e: - # TODO: Handle error - # handle_socket_error(e, team, "ping") - print(e) - return weechat.WEECHAT_RC_OK - - -async def init(): - auto_connect = weechat.info_get("auto_connect", "") == "1" - if auto_connect: - await sleep(1) # Defer auto connect to ensure the logger plugin is loaded - for workspace in shared.workspaces.values(): - if workspace.config.autoconnect.value: - await workspace.connect() - - -def main(): - if weechat.register( - shared.SCRIPT_NAME, - SCRIPT_AUTHOR, - shared.SCRIPT_VERSION, - SCRIPT_LICENSE, - SCRIPT_DESC, - get_callback_name(shutdown_cb), - "", - ): - shared.weechat_version = int(weechat.info_get("version_number", "") or 0) - shared.workspaces = {} - shared.config = SlackConfig() - shared.config.config_read() - register_commands() - - weechat.hook_signal( - "buffer_switch", get_callback_name(signal_buffer_switch_cb), "" - ) - weechat.hook_signal( - "window_switch", get_callback_name(signal_buffer_switch_cb), "" - ) - weechat.hook_modifier( - "input_text_display_with_cursor", - get_callback_name(modifier_input_text_display_with_cursor_cb), - "", - ) - weechat.hook_timer(5000, 0, 0, get_callback_name(ws_ping_cb), "") - - create_task(init()) diff --git a/slack/register.py b/slack/register.py new file mode 100644 index 0000000..924e0eb --- /dev/null +++ b/slack/register.py @@ -0,0 +1,110 @@ +from __future__ import annotations + +import socket + +import weechat +from websocket import WebSocketConnectionClosedException + +from slack.commands import register_commands +from slack.config import SlackConfig +from slack.shared import shared +from slack.slack_conversation import get_conversation_from_buffer_pointer +from slack.task import create_task, sleep +from slack.util import get_callback_name, with_color + +SCRIPT_AUTHOR = "Trygve Aaberge " +SCRIPT_LICENSE = "MIT" +SCRIPT_DESC = "Extends weechat for typing notification/search/etc on slack.com" +REPO_URL = "https://github.com/wee-slack/wee-slack" + + +def shutdown_cb(): + weechat.config_write(shared.config.weechat_config.pointer) + return weechat.WEECHAT_RC_OK + + +def signal_buffer_switch_cb(data: str, signal: str, buffer_pointer: str) -> int: + conversation = get_conversation_from_buffer_pointer(buffer_pointer) + if conversation: + create_task(conversation.fill_history()) + return weechat.WEECHAT_RC_OK + + +def modifier_input_text_display_with_cursor_cb( + data: str, modifier: str, buffer_pointer: str, string: str +) -> str: + prefix = "" + conversation = get_conversation_from_buffer_pointer(buffer_pointer) + if conversation: + input_delim_color = weechat.config_string( + weechat.config_get("weechat.bar.input.color_delim") + ) + input_delim_start = with_color(input_delim_color, "[") + input_delim_end = with_color(input_delim_color, "]") + if not conversation.workspace.is_connected: + prefix += ( + f"{input_delim_start}" + f"{with_color(shared.config.color.disconnected.value, 'disconnected')}" + f"{input_delim_end} " + ) + if conversation.is_loading: + prefix += ( + f"{input_delim_start}" + f"{with_color(shared.config.color.loading.value, 'loading')}" + f"{input_delim_end} " + ) + return prefix + string + + +def ws_ping_cb(data: str, remaining_calls: int) -> int: + for workspace in shared.workspaces.values(): + if workspace.is_connected: + try: + workspace.ws.ping() + # workspace.last_ping_time = time.time() + except (WebSocketConnectionClosedException, socket.error) as e: + # TODO: Handle error + # handle_socket_error(e, team, "ping") + print(e) + return weechat.WEECHAT_RC_OK + + +async def init_async(): + auto_connect = weechat.info_get("auto_connect", "") == "1" + if auto_connect: + await sleep(1) # Defer auto connect to ensure the logger plugin is loaded + for workspace in shared.workspaces.values(): + if workspace.config.autoconnect.value: + await workspace.connect() + + +def register(): + if weechat.register( + shared.SCRIPT_NAME, + SCRIPT_AUTHOR, + shared.SCRIPT_VERSION, + SCRIPT_LICENSE, + SCRIPT_DESC, + get_callback_name(shutdown_cb), + "", + ): + shared.weechat_version = int(weechat.info_get("version_number", "") or 0) + shared.workspaces = {} + shared.config = SlackConfig() + shared.config.config_read() + register_commands() + + weechat.hook_signal( + "buffer_switch", get_callback_name(signal_buffer_switch_cb), "" + ) + weechat.hook_signal( + "window_switch", get_callback_name(signal_buffer_switch_cb), "" + ) + weechat.hook_modifier( + "input_text_display_with_cursor", + get_callback_name(modifier_input_text_display_with_cursor_cb), + "", + ) + weechat.hook_timer(5000, 0, 0, get_callback_name(ws_ping_cb), "") + + create_task(init_async()) -- cgit