aboutsummaryrefslogtreecommitdiffstats
path: root/slack
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2024-01-11 00:29:51 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:55 +0100
commit1989a394e96df843a1e5d852e9706cafcf6c0945 (patch)
tree170f758cef926dd429fecef49eb9311fa0435224 /slack
parentfa0001661c400327c4fdebeb226a08bcdb1a5fd1 (diff)
downloadwee-slack-1989a394e96df843a1e5d852e9706cafcf6c0945.tar.gz
Set buffer_pointer to None when buffer doesn't exist
This enforces us to check that buffer_pointer is set, so we don't accidentally operate on the core buffer, which is what is done when buffer_pointer is an empty string.
Diffstat (limited to 'slack')
-rw-r--r--slack/commands.py24
-rw-r--r--slack/slack_buffer.py12
-rw-r--r--slack/slack_conversation.py28
-rw-r--r--slack/slack_workspace.py3
4 files changed, 39 insertions, 28 deletions
diff --git a/slack/commands.py b/slack/commands.py
index ed57953..a866894 100644
--- a/slack/commands.py
+++ b/slack/commands.py
@@ -573,13 +573,13 @@ def completion_thread_hashes_cb(
return weechat.WEECHAT_RC_OK
-def complete_input(slack_buffer: SlackBuffer, query: str):
+def complete_input(buffer: str, slack_buffer: SlackBuffer, query: str):
if (
slack_buffer.completion_context == "ACTIVE_COMPLETION"
and slack_buffer.completion_values
):
- input_value = weechat.buffer_get_string(slack_buffer.buffer_pointer, "input")
- input_pos = weechat.buffer_get_integer(slack_buffer.buffer_pointer, "input_pos")
+ input_value = weechat.buffer_get_string(buffer, "input")
+ input_pos = weechat.buffer_get_integer(buffer, "input_pos")
result = slack_buffer.completion_values[slack_buffer.completion_index]
input_before = removesuffix(input_value[:input_pos], query)
input_after = input_value[input_pos:]
@@ -587,8 +587,8 @@ def complete_input(slack_buffer: SlackBuffer, query: str):
new_pos = input_pos - len(query) + len(result)
with slack_buffer.completing():
- weechat.buffer_set(slack_buffer.buffer_pointer, "input", new_input)
- weechat.buffer_set(slack_buffer.buffer_pointer, "input_pos", str(new_pos))
+ weechat.buffer_set(buffer, "input", new_input)
+ weechat.buffer_set(buffer, "input_pos", str(new_pos))
def nick_suffix():
@@ -598,7 +598,7 @@ def nick_suffix():
async def complete_user_next(
- slack_buffer: SlackBuffer, query: str, is_first_word: bool
+ buffer: str, slack_buffer: SlackBuffer, query: str, is_first_word: bool
):
if slack_buffer.completion_context == "NO_COMPLETION":
slack_buffer.completion_context = "PENDING_COMPLETION"
@@ -618,15 +618,15 @@ async def complete_user_next(
if slack_buffer.completion_index >= len(slack_buffer.completion_values):
slack_buffer.completion_index = 0
- complete_input(slack_buffer, query)
+ complete_input(buffer, slack_buffer, query)
-def complete_previous(slack_buffer: SlackBuffer, query: str) -> int:
+def complete_previous(buffer: str, slack_buffer: SlackBuffer, query: str) -> int:
if slack_buffer.completion_context == "ACTIVE_COMPLETION":
slack_buffer.completion_index -= 1
if slack_buffer.completion_index < 0:
slack_buffer.completion_index = len(slack_buffer.completion_values) - 1
- complete_input(slack_buffer, query)
+ complete_input(buffer, slack_buffer, query)
return weechat.WEECHAT_RC_OK_EAT
return weechat.WEECHAT_RC_OK
@@ -661,10 +661,12 @@ def input_complete_cb(data: str, buffer: str, command: str) -> int:
is_first_word = word_until_cursor == input_before_cursor
if command == "/input complete_next":
- run_async(complete_user_next(slack_buffer, query, is_first_word))
+ run_async(
+ complete_user_next(buffer, slack_buffer, query, is_first_word)
+ )
return weechat.WEECHAT_RC_OK_EAT
else:
- return complete_previous(slack_buffer, query)
+ return complete_previous(buffer, slack_buffer, query)
return weechat.WEECHAT_RC_OK
diff --git a/slack/slack_buffer.py b/slack/slack_buffer.py
index fad5689..3c78ba3 100644
--- a/slack/slack_buffer.py
+++ b/slack/slack_buffer.py
@@ -151,7 +151,7 @@ class SlackBuffer(ABC):
def __init__(self):
self._typing_self_last_sent = 0
# TODO: buffer_pointer may be accessed by buffer_switch before it's initialized
- self.buffer_pointer: str = ""
+ self.buffer_pointer: Optional[str] = None
self.is_loading = False
self.history_pending = False
self.history_pending_messages: List[SlackMessage] = []
@@ -273,6 +273,9 @@ class SlackBuffer(ABC):
await self.buffer_switched_to()
def update_buffer_props(self) -> None:
+ if self.buffer_pointer is None:
+ return
+
name, buffer_props = self.get_name_and_buffer_props()
buffer_props["name"] = self.get_full_name(name)
for key, value in buffer_props.items():
@@ -283,6 +286,9 @@ class SlackBuffer(ABC):
raise NotImplementedError()
async def rerender_message(self, message: SlackMessage):
+ if self.buffer_pointer is None:
+ return
+
modify_buffer_line(
self.buffer_pointer,
message.ts,
@@ -349,7 +355,7 @@ class SlackBuffer(ABC):
def ts_from_index(
self, index: int, message_filter: Optional[Literal["sender_self"]] = None
) -> Optional[SlackTs]:
- if index < 0:
+ if index < 0 or self.buffer_pointer is None:
return
lines = weechat.hdata_pointer(
@@ -498,7 +504,7 @@ class SlackBuffer(ABC):
def _buffer_close_cb(self, data: str, buffer: str) -> int:
if self.buffer_pointer in shared.buffers:
del shared.buffers[self.buffer_pointer]
- self.buffer_pointer = ""
+ self.buffer_pointer = None
self.last_printed_ts = None
self.hotlist_tss.clear()
return weechat.WEECHAT_RC_OK
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py
index 8b2bb67..2c95f4c 100644
--- a/slack/slack_conversation.py
+++ b/slack/slack_conversation.py
@@ -555,7 +555,7 @@ class SlackConversation(SlackBuffer):
self.nicklist_add_nick(user.nick)
def nicklist_add_nick(self, nick: Nick):
- if nick in self._nicklist or self.type == "im":
+ if nick in self._nicklist or self.type == "im" or self.buffer_pointer is None:
return
# TODO: weechat.color.nicklist_away
@@ -568,25 +568,27 @@ class SlackConversation(SlackBuffer):
self._nicklist[nick] = nick_pointer
def nicklist_remove_nick(self, nick: Nick):
- if self.type == "im":
+ if self.type == "im" or self.buffer_pointer is None:
return
nick_pointer = self._nicklist.pop(nick)
weechat.nicklist_remove_nick(self.buffer_pointer, nick_pointer)
def display_thread_replies(self) -> bool:
- buffer_value = weechat.buffer_get_string(
- self.buffer_pointer, "localvar_display_thread_replies_in_channel"
- )
- if buffer_value:
- return bool(weechat.config_string_to_boolean(buffer_value))
+ if self.buffer_pointer is not None:
+ buffer_value = weechat.buffer_get_string(
+ self.buffer_pointer, "localvar_display_thread_replies_in_channel"
+ )
+ if buffer_value:
+ return bool(weechat.config_string_to_boolean(buffer_value))
return shared.config.look.display_thread_replies_in_channel.value
def display_reaction_nicks(self) -> bool:
- buffer_value = weechat.buffer_get_string(
- self.buffer_pointer, "localvar_display_reaction_nicks"
- )
- if buffer_value:
- return bool(weechat.config_string_to_boolean(buffer_value))
+ if self.buffer_pointer is not None:
+ buffer_value = weechat.buffer_get_string(
+ self.buffer_pointer, "localvar_display_reaction_nicks"
+ )
+ if buffer_value:
+ return bool(weechat.config_string_to_boolean(buffer_value))
return shared.config.look.display_reaction_nicks.value
def should_display_message(self, message: SlackMessage) -> bool:
@@ -605,7 +607,7 @@ class SlackConversation(SlackBuffer):
self.history_pending_messages.append(message)
elif self.last_printed_ts is not None:
await self.print_message(message)
- else:
+ elif self.buffer_pointer is not None:
weechat.buffer_set(
self.buffer_pointer, "hotlist", message.priority.value
)
diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py
index 7299857..145a008 100644
--- a/slack/slack_workspace.py
+++ b/slack/slack_workspace.py
@@ -549,7 +549,8 @@ class SlackWorkspace:
or data["type"] == "channel_left"
or data["type"] == "group_left"
):
- weechat.buffer_close(channel.buffer_pointer)
+ if channel.buffer_pointer is not None:
+ weechat.buffer_close(channel.buffer_pointer)
elif data["type"] == "reaction_added" and data["item"]["type"] == "message":
await channel.reaction_add(
SlackTs(data["item"]["ts"]), data["reaction"], data["user"]