Skip to content

Commit dc64d00

Browse files
trygveaaflashcode
authored andcommitted
slack.py 2.10.1: Merge with mainline
This is version 2.10.1 of slack.py (currently the most recent), copied over from the wee-slack repo.
1 parent e02f9e7 commit dc64d00

File tree

1 file changed

+75
-27
lines changed

1 file changed

+75
-27
lines changed

python/slack.py

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676

7777
SCRIPT_NAME = "slack"
7878
SCRIPT_AUTHOR = "Trygve Aaberge <[email protected]>"
79-
SCRIPT_VERSION = "2.10.0"
79+
SCRIPT_VERSION = "2.10.1"
8080
SCRIPT_LICENSE = "MIT"
8181
SCRIPT_DESC = "Extends WeeChat for typing notification/search/etc on slack.com"
8282
REPO_URL = "https://github.com/wee-slack/wee-slack"
@@ -1777,9 +1777,11 @@ def connect(self):
17771777
# only http proxy is currently supported
17781778
proxy = ProxyWrapper()
17791779
timeout = config.slack_timeout / 1000
1780+
cookie = SlackRequest(self.team, "").options()["cookie"]
17801781
if proxy.has_proxy:
17811782
ws = create_connection(
17821783
self.ws_url,
1784+
cookie=cookie,
17831785
timeout=timeout,
17841786
sslopt=sslopt_ca_certs,
17851787
http_proxy_host=proxy.proxy_address,
@@ -1788,7 +1790,10 @@ def connect(self):
17881790
)
17891791
else:
17901792
ws = create_connection(
1791-
self.ws_url, timeout=timeout, sslopt=sslopt_ca_certs
1793+
self.ws_url,
1794+
cookie=cookie,
1795+
timeout=timeout,
1796+
sslopt=sslopt_ca_certs,
17921797
)
17931798

17941799
self.hook = w.hook_fd(
@@ -2519,7 +2524,7 @@ def buffer_prnt(
25192524
w.prnt_date_tags(self.channel_buffer, ts.major, tags, data)
25202525
if no_log:
25212526
w.buffer_set(self.channel_buffer, "print_hooks_enabled", "1")
2522-
if backlog or self_msg:
2527+
if backlog or (self_msg and tagset != "join"):
25232528
self.mark_read(ts, update_remote=False, force=True)
25242529

25252530
def store_message(self, message_to_store):
@@ -3421,7 +3426,7 @@ def render(self, force=False):
34213426

34223427
text += unfurl_refs(unwrap_attachments(self, text))
34233428
text += unfurl_refs(unwrap_files(self, self.message_json, text))
3424-
text += unfurl_refs(unwrap_huddle(self, self.message_json))
3429+
text += unfurl_refs(unwrap_huddle(self, self.message_json, text))
34253430
text = unhtmlescape(text.lstrip().replace("\t", " "))
34263431

34273432
text += create_reactions_string(
@@ -3438,7 +3443,10 @@ def render(self, force=False):
34383443
),
34393444
)
34403445

3441-
text = replace_string_with_emoji(text)
3446+
# replace_string_with_emoji() was called on blocks earlier via
3447+
# unfurl_blocks(), so exclude them here
3448+
text_to_replace = text[len(blocks_rendered) :]
3449+
text = text[: len(blocks_rendered)] + replace_string_with_emoji(text_to_replace)
34423450

34433451
self.message_json["_rendered_text"] = text
34443452
return text
@@ -4135,6 +4143,9 @@ def process_pong(message_json, eventrouter, team, channel, metadata):
41354143
def process_message(
41364144
message_json, eventrouter, team, channel, metadata, history_message=False
41374145
):
4146+
if channel is None:
4147+
return
4148+
41384149
subtype = message_json.get("subtype")
41394150
if (
41404151
not history_message
@@ -4318,7 +4329,7 @@ def process_reply(message_json, eventrouter, team, channel, metadata):
43184329

43194330
def process_channel_marked(message_json, eventrouter, team, channel, metadata):
43204331
ts = message_json.get("ts")
4321-
if ts:
4332+
if ts and channel is not None:
43224333
channel.mark_read(ts=ts, force=True, update_remote=False)
43234334
else:
43244335
dbg("tried to mark something weird {}".format(message_json))
@@ -4343,7 +4354,14 @@ def process_thread_marked(message_json, eventrouter, team, channel, metadata):
43434354

43444355

43454356
def process_channel_joined(message_json, eventrouter, team, channel, metadata):
4346-
channel.update_from_message_json(message_json["channel"])
4357+
if channel is None:
4358+
channel = create_channel_from_info(
4359+
eventrouter, message_json["channel"], team, team.myidentifier, team.users
4360+
)
4361+
team.channels[message_json["channel"]["id"]] = channel
4362+
else:
4363+
channel.update_from_message_json(message_json["channel"])
4364+
43474365
channel.open()
43484366

43494367

@@ -4356,6 +4374,8 @@ def process_channel_created(message_json, eventrouter, team, channel, metadata):
43564374

43574375

43584376
def process_channel_rename(message_json, eventrouter, team, channel, metadata):
4377+
if channel is None:
4378+
return
43594379
channel.set_name(message_json["channel"]["name"])
43604380

43614381

@@ -4402,6 +4422,9 @@ def process_group_joined(message_json, eventrouter, team, channel, metadata):
44024422

44034423
def process_reaction_added(message_json, eventrouter, team, channel, metadata):
44044424
channel = team.channels.get(message_json["item"].get("channel"))
4425+
if channel is None:
4426+
return
4427+
44054428
if message_json["item"].get("type") == "message":
44064429
ts = SlackTS(message_json["item"]["ts"])
44074430

@@ -4415,6 +4438,9 @@ def process_reaction_added(message_json, eventrouter, team, channel, metadata):
44154438

44164439
def process_reaction_removed(message_json, eventrouter, team, channel, metadata):
44174440
channel = team.channels.get(message_json["item"].get("channel"))
4441+
if channel is None:
4442+
return
4443+
44184444
if message_json["item"].get("type") == "message":
44194445
ts = SlackTS(message_json["item"]["ts"])
44204446

@@ -4434,7 +4460,10 @@ def process_subteam_created(subteam_json, eventrouter, team, channel, metadata):
44344460

44354461

44364462
def process_subteam_updated(subteam_json, eventrouter, team, channel, metadata):
4437-
current_subteam_info = team.subteams[subteam_json["subteam"]["id"]]
4463+
current_subteam_info = team.subteams.get(subteam_json["subteam"]["id"])
4464+
if current_subteam_info is None:
4465+
return
4466+
44384467
is_member = team.myidentifier in subteam_json["subteam"].get("users", [])
44394468
new_subteam_info = SlackSubteam(
44404469
team.identifier, is_member=is_member, **subteam_json["subteam"]
@@ -4606,7 +4635,7 @@ def unfurl_blocks(blocks):
46064635
lines = [
46074636
"> {}".format(line)
46084637
for e in element["elements"]
4609-
for line in unfurl_block_element(e).split("\n")
4638+
for line in unfurl_block_rich_text_element(e).split("\n")
46104639
]
46114640
block_text.extend(lines)
46124641
elif element["type"] == "rich_text_preformatted":
@@ -4737,7 +4766,7 @@ def unfurl_rich_text_section(block):
47374766
texts.extend(reversed(colors_remove))
47384767
texts.extend(colors_apply)
47394768
texts.extend(characters_apply)
4740-
texts.append(unfurl_block_element(element))
4769+
texts.append(unfurl_block_rich_text_element(element))
47414770
prev_element = element
47424771

47434772
text = "".join(texts)
@@ -4748,16 +4777,9 @@ def unfurl_rich_text_section(block):
47484777
return text
47494778

47504779

4751-
def unfurl_block_element(element):
4752-
if element["type"] == "mrkdwn":
4753-
return render_formatting(element["text"])
4754-
elif element["type"] in ["text", "plain_text"]:
4755-
return element["text"]
4756-
elif element["type"] == "image":
4757-
if element.get("alt_text"):
4758-
return "{} ({})".format(element["image_url"], element["alt_text"])
4759-
else:
4760-
return element["image_url"]
4780+
def unfurl_block_rich_text_element(element):
4781+
if element["type"] == "text":
4782+
return htmlescape(element["text"])
47614783
elif element["type"] == "link":
47624784
text = element.get("text")
47634785
if text and text != element["url"]:
@@ -4777,6 +4799,24 @@ def unfurl_block_element(element):
47774799
return resolve_ref("@{}".format(element["range"]))
47784800
elif element["type"] == "channel":
47794801
return resolve_ref("#{}".format(element["channel_id"]))
4802+
else:
4803+
dbg("Unsupported rich text element: '{}'".format(json.dumps(element)), level=4)
4804+
return colorize_string(
4805+
config.color_deleted,
4806+
'<<Unsupported rich text element type "{}">>'.format(element["type"]),
4807+
)
4808+
4809+
4810+
def unfurl_block_element(element):
4811+
if element["type"] == "mrkdwn":
4812+
return render_formatting(element["text"])
4813+
elif element["type"] == "plain_text":
4814+
return element["text"]
4815+
elif element["type"] == "image":
4816+
if element.get("alt_text"):
4817+
return "{} ({})".format(element["image_url"], element["alt_text"])
4818+
else:
4819+
return element["image_url"]
47804820
else:
47814821
dbg("Unsupported block element: '{}'".format(json.dumps(element)), level=4)
47824822
return colorize_string(
@@ -4832,6 +4872,10 @@ def unfurl_ref(match):
48324872
return re.sub(r"<([^|>]*)(?:\|([^>]*))?>", unfurl_ref, text)
48334873

48344874

4875+
def htmlescape(text):
4876+
return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
4877+
4878+
48354879
def unhtmlescape(text):
48364880
return text.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&")
48374881

@@ -4974,19 +5018,14 @@ def unwrap_attachments(message, text_before):
49745018
return "\n".join(attachment_texts)
49755019

49765020

4977-
def unwrap_huddle(message, message_json):
5021+
def unwrap_huddle(message, message_json, text_before):
49785022
"""
49795023
If huddle is linked to message, append huddle information and link
49805024
to connect.
49815025
"""
49825026
huddle_texts = []
49835027

49845028
if "room" in message_json:
4985-
for block in message_json.get("blocks"):
4986-
for element in block.get("elements"):
4987-
for element2 in element.get("elements"):
4988-
huddle_texts.append(element2.get("text"))
4989-
49905029
if "name" in message_json.get("room"):
49915030
room_name = message_json.get("room").get("name")
49925031

@@ -5000,6 +5039,8 @@ def unwrap_huddle(message, message_json):
50005039
)
50015040
)
50025041

5042+
if text_before:
5043+
huddle_texts.insert(0, "")
50035044
return "\n".join(huddle_texts)
50045045

50055046

@@ -5263,7 +5304,7 @@ def tag(
52635304
slack_tag = "slack_{}".format(tagset or "default")
52645305
nick_tag = ["nick_{}".format(user).replace(" ", "_")] if user else []
52655306
tags = [ts_tag, slack_tag] + nick_tag + tagsets.get(tagset, [])
5266-
if self_msg or backlog:
5307+
if (self_msg and tagset != "join") or backlog:
52675308
tags = tags_set_notify_none(tags)
52685309
if self_msg:
52695310
tags += ["self_msg"]
@@ -5956,7 +5997,14 @@ def command_reply(data, current_buffer, args):
59565997
In either case, -alsochannel also sends the reply to the parent channel.
59575998
"""
59585999
channel = EVENTROUTER.weechat_controller.buffers[current_buffer]
6000+
59596001
parts = args.split(None, 1)
6002+
if len(parts) < 1:
6003+
w.prnt(
6004+
"", 'Too few arguments for command "/reply" (help on command: /help reply)'
6005+
)
6006+
return w.WEECHAT_RC_ERROR
6007+
59606008
if parts[0] == "-alsochannel":
59616009
args = parts[1]
59626010
broadcast = True

0 commit comments

Comments
 (0)