76
76
77
77
SCRIPT_NAME = "slack"
78
78
SCRIPT_AUTHOR = "Trygve Aaberge <[email protected] >"
79
- SCRIPT_VERSION = "2.10.0 "
79
+ SCRIPT_VERSION = "2.10.1 "
80
80
SCRIPT_LICENSE = "MIT"
81
81
SCRIPT_DESC = "Extends WeeChat for typing notification/search/etc on slack.com"
82
82
REPO_URL = "https://github.com/wee-slack/wee-slack"
@@ -1777,9 +1777,11 @@ def connect(self):
1777
1777
# only http proxy is currently supported
1778
1778
proxy = ProxyWrapper ()
1779
1779
timeout = config .slack_timeout / 1000
1780
+ cookie = SlackRequest (self .team , "" ).options ()["cookie" ]
1780
1781
if proxy .has_proxy :
1781
1782
ws = create_connection (
1782
1783
self .ws_url ,
1784
+ cookie = cookie ,
1783
1785
timeout = timeout ,
1784
1786
sslopt = sslopt_ca_certs ,
1785
1787
http_proxy_host = proxy .proxy_address ,
@@ -1788,7 +1790,10 @@ def connect(self):
1788
1790
)
1789
1791
else :
1790
1792
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 ,
1792
1797
)
1793
1798
1794
1799
self .hook = w .hook_fd (
@@ -2519,7 +2524,7 @@ def buffer_prnt(
2519
2524
w .prnt_date_tags (self .channel_buffer , ts .major , tags , data )
2520
2525
if no_log :
2521
2526
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" ) :
2523
2528
self .mark_read (ts , update_remote = False , force = True )
2524
2529
2525
2530
def store_message (self , message_to_store ):
@@ -3421,7 +3426,7 @@ def render(self, force=False):
3421
3426
3422
3427
text += unfurl_refs (unwrap_attachments (self , text ))
3423
3428
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 ))
3425
3430
text = unhtmlescape (text .lstrip ().replace ("\t " , " " ))
3426
3431
3427
3432
text += create_reactions_string (
@@ -3438,7 +3443,10 @@ def render(self, force=False):
3438
3443
),
3439
3444
)
3440
3445
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 )
3442
3450
3443
3451
self .message_json ["_rendered_text" ] = text
3444
3452
return text
@@ -4135,6 +4143,9 @@ def process_pong(message_json, eventrouter, team, channel, metadata):
4135
4143
def process_message (
4136
4144
message_json , eventrouter , team , channel , metadata , history_message = False
4137
4145
):
4146
+ if channel is None :
4147
+ return
4148
+
4138
4149
subtype = message_json .get ("subtype" )
4139
4150
if (
4140
4151
not history_message
@@ -4318,7 +4329,7 @@ def process_reply(message_json, eventrouter, team, channel, metadata):
4318
4329
4319
4330
def process_channel_marked (message_json , eventrouter , team , channel , metadata ):
4320
4331
ts = message_json .get ("ts" )
4321
- if ts :
4332
+ if ts and channel is not None :
4322
4333
channel .mark_read (ts = ts , force = True , update_remote = False )
4323
4334
else :
4324
4335
dbg ("tried to mark something weird {}" .format (message_json ))
@@ -4343,7 +4354,14 @@ def process_thread_marked(message_json, eventrouter, team, channel, metadata):
4343
4354
4344
4355
4345
4356
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
+
4347
4365
channel .open ()
4348
4366
4349
4367
@@ -4356,6 +4374,8 @@ def process_channel_created(message_json, eventrouter, team, channel, metadata):
4356
4374
4357
4375
4358
4376
def process_channel_rename (message_json , eventrouter , team , channel , metadata ):
4377
+ if channel is None :
4378
+ return
4359
4379
channel .set_name (message_json ["channel" ]["name" ])
4360
4380
4361
4381
@@ -4402,6 +4422,9 @@ def process_group_joined(message_json, eventrouter, team, channel, metadata):
4402
4422
4403
4423
def process_reaction_added (message_json , eventrouter , team , channel , metadata ):
4404
4424
channel = team .channels .get (message_json ["item" ].get ("channel" ))
4425
+ if channel is None :
4426
+ return
4427
+
4405
4428
if message_json ["item" ].get ("type" ) == "message" :
4406
4429
ts = SlackTS (message_json ["item" ]["ts" ])
4407
4430
@@ -4415,6 +4438,9 @@ def process_reaction_added(message_json, eventrouter, team, channel, metadata):
4415
4438
4416
4439
def process_reaction_removed (message_json , eventrouter , team , channel , metadata ):
4417
4440
channel = team .channels .get (message_json ["item" ].get ("channel" ))
4441
+ if channel is None :
4442
+ return
4443
+
4418
4444
if message_json ["item" ].get ("type" ) == "message" :
4419
4445
ts = SlackTS (message_json ["item" ]["ts" ])
4420
4446
@@ -4434,7 +4460,10 @@ def process_subteam_created(subteam_json, eventrouter, team, channel, metadata):
4434
4460
4435
4461
4436
4462
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
+
4438
4467
is_member = team .myidentifier in subteam_json ["subteam" ].get ("users" , [])
4439
4468
new_subteam_info = SlackSubteam (
4440
4469
team .identifier , is_member = is_member , ** subteam_json ["subteam" ]
@@ -4606,7 +4635,7 @@ def unfurl_blocks(blocks):
4606
4635
lines = [
4607
4636
"> {}" .format (line )
4608
4637
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 " )
4610
4639
]
4611
4640
block_text .extend (lines )
4612
4641
elif element ["type" ] == "rich_text_preformatted" :
@@ -4737,7 +4766,7 @@ def unfurl_rich_text_section(block):
4737
4766
texts .extend (reversed (colors_remove ))
4738
4767
texts .extend (colors_apply )
4739
4768
texts .extend (characters_apply )
4740
- texts .append (unfurl_block_element (element ))
4769
+ texts .append (unfurl_block_rich_text_element (element ))
4741
4770
prev_element = element
4742
4771
4743
4772
text = "" .join (texts )
@@ -4748,16 +4777,9 @@ def unfurl_rich_text_section(block):
4748
4777
return text
4749
4778
4750
4779
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" ])
4761
4783
elif element ["type" ] == "link" :
4762
4784
text = element .get ("text" )
4763
4785
if text and text != element ["url" ]:
@@ -4777,6 +4799,24 @@ def unfurl_block_element(element):
4777
4799
return resolve_ref ("@{}" .format (element ["range" ]))
4778
4800
elif element ["type" ] == "channel" :
4779
4801
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" ]
4780
4820
else :
4781
4821
dbg ("Unsupported block element: '{}'" .format (json .dumps (element )), level = 4 )
4782
4822
return colorize_string (
@@ -4832,6 +4872,10 @@ def unfurl_ref(match):
4832
4872
return re .sub (r"<([^|>]*)(?:\|([^>]*))?>" , unfurl_ref , text )
4833
4873
4834
4874
4875
+ def htmlescape (text ):
4876
+ return text .replace ("&" , "&" ).replace ("<" , "<" ).replace (">" , ">" )
4877
+
4878
+
4835
4879
def unhtmlescape (text ):
4836
4880
return text .replace ("<" , "<" ).replace (">" , ">" ).replace ("&" , "&" )
4837
4881
@@ -4974,19 +5018,14 @@ def unwrap_attachments(message, text_before):
4974
5018
return "\n " .join (attachment_texts )
4975
5019
4976
5020
4977
- def unwrap_huddle (message , message_json ):
5021
+ def unwrap_huddle (message , message_json , text_before ):
4978
5022
"""
4979
5023
If huddle is linked to message, append huddle information and link
4980
5024
to connect.
4981
5025
"""
4982
5026
huddle_texts = []
4983
5027
4984
5028
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
-
4990
5029
if "name" in message_json .get ("room" ):
4991
5030
room_name = message_json .get ("room" ).get ("name" )
4992
5031
@@ -5000,6 +5039,8 @@ def unwrap_huddle(message, message_json):
5000
5039
)
5001
5040
)
5002
5041
5042
+ if text_before :
5043
+ huddle_texts .insert (0 , "" )
5003
5044
return "\n " .join (huddle_texts )
5004
5045
5005
5046
@@ -5263,7 +5304,7 @@ def tag(
5263
5304
slack_tag = "slack_{}" .format (tagset or "default" )
5264
5305
nick_tag = ["nick_{}" .format (user ).replace (" " , "_" )] if user else []
5265
5306
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 :
5267
5308
tags = tags_set_notify_none (tags )
5268
5309
if self_msg :
5269
5310
tags += ["self_msg" ]
@@ -5956,7 +5997,14 @@ def command_reply(data, current_buffer, args):
5956
5997
In either case, -alsochannel also sends the reply to the parent channel.
5957
5998
"""
5958
5999
channel = EVENTROUTER .weechat_controller .buffers [current_buffer ]
6000
+
5959
6001
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
+
5960
6008
if parts [0 ] == "-alsochannel" :
5961
6009
args = parts [1 ]
5962
6010
broadcast = True
0 commit comments