Skip to content

Commit 3f7ee87

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

File tree

1 file changed

+102
-31
lines changed

1 file changed

+102
-31
lines changed

python/slack.py

Lines changed: 102 additions & 31 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.2"
79+
SCRIPT_VERSION = "2.11.0"
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"
@@ -1691,6 +1691,7 @@ def create_buffer(self):
16911691
self.eventrouter.weechat_controller.register_buffer(
16921692
self.channel_buffer, self
16931693
)
1694+
w.buffer_set(self.channel_buffer, "input_prompt", self.nick)
16941695
w.buffer_set(self.channel_buffer, "input_multiline", "1")
16951696
w.buffer_set(self.channel_buffer, "localvar_set_type", "server")
16961697
w.buffer_set(self.channel_buffer, "localvar_set_slack_type", self.type)
@@ -2421,6 +2422,7 @@ def create_buffer(self):
24212422
self.eventrouter.weechat_controller.register_buffer(
24222423
self.channel_buffer, self
24232424
)
2425+
w.buffer_set(self.channel_buffer, "input_prompt", self.team.nick)
24242426
w.buffer_set(self.channel_buffer, "input_multiline", "1")
24252427
w.buffer_set(
24262428
self.channel_buffer, "localvar_set_type", get_localvar_type(self.type)
@@ -2887,12 +2889,12 @@ class SlackDMChannel(SlackChannel):
28872889
has some important differences.
28882890
"""
28892891

2890-
def __init__(self, eventrouter, users, **kwargs):
2892+
def __init__(self, eventrouter, users, myidentifier, **kwargs):
28912893
dmuser = kwargs["user"]
28922894
kwargs["name"] = users[dmuser].name if dmuser in users else dmuser
28932895
super(SlackDMChannel, self).__init__(eventrouter, "im", **kwargs)
28942896
self.update_color()
2895-
self.members = {self.user}
2897+
self.members = {myidentifier, self.user}
28962898
if dmuser in users:
28972899
self.set_topic(create_user_status_string(users[dmuser].profile))
28982900

@@ -3210,6 +3212,7 @@ def create_buffer(self):
32103212
self.eventrouter.weechat_controller.register_buffer(
32113213
self.channel_buffer, self
32123214
)
3215+
w.buffer_set(self.channel_buffer, "input_prompt", self.team.nick)
32133216
w.buffer_set(self.channel_buffer, "input_multiline", "1")
32143217
w.buffer_set(
32153218
self.channel_buffer,
@@ -3729,7 +3732,9 @@ def handle_rtmstart(login_data, eventrouter, team, channel, metadata):
37293732
channels[item["id"]] = SlackChannel(eventrouter, **item)
37303733

37313734
for item in login_data["ims"]:
3732-
channels[item["id"]] = SlackDMChannel(eventrouter, users, **item)
3735+
channels[item["id"]] = SlackDMChannel(
3736+
eventrouter, users, login_data["self"]["id"], **item
3737+
)
37333738

37343739
for item in login_data["mpims"]:
37353740
channels[item["id"]] = SlackMPDMChannel(
@@ -4111,6 +4116,16 @@ def process_pref_change(message_json, eventrouter, team, channel, metadata):
41114116
team.set_muted_channels(message_json["value"])
41124117
elif message_json["name"] == "highlight_words":
41134118
team.set_highlight_words(message_json["value"])
4119+
elif message_json["name"] == "all_notifications_prefs":
4120+
new_prefs = json.loads(message_json["value"])
4121+
new_muted_channels = set(
4122+
channel_id
4123+
for channel_id, prefs in new_prefs["channels"].items()
4124+
if prefs["muted"]
4125+
)
4126+
team.set_muted_channels(",".join(new_muted_channels))
4127+
global_keywords = new_prefs["global"]["global_keywords"]
4128+
team.set_highlight_words(global_keywords)
41144129
else:
41154130
dbg("Preference change not implemented: {}\n".format(message_json["name"]))
41164131

@@ -4123,7 +4138,9 @@ def process_user_change(message_json, eventrouter, team, channel, metadata):
41234138
profile = message_json["user"]["profile"]
41244139
if user:
41254140
user.update_status(profile.get("status_emoji"), profile.get("status_text"))
4126-
dmchannel = team.find_channel_by_members({user.identifier}, channel_type="im")
4141+
dmchannel = team.find_channel_by_members(
4142+
{team.myidentifier, user.identifier}, channel_type="im"
4143+
)
41274144
if dmchannel:
41284145
dmchannel.set_topic(create_user_status_string(profile))
41294146

@@ -4373,7 +4390,8 @@ def process_channel_created(message_json, eventrouter, team, channel, metadata):
43734390
item["is_member"] = False
43744391
channel = SlackChannel(eventrouter, team=team, **item)
43754392
team.channels[item["id"]] = channel
4376-
team.buffer_prnt("Channel created: {}".format(channel.name))
4393+
if config.log_channel_created:
4394+
team.buffer_prnt("Channel created: {}".format(channel.name))
43774395

43784396

43794397
def process_channel_rename(message_json, eventrouter, team, channel, metadata):
@@ -4384,7 +4402,9 @@ def process_channel_rename(message_json, eventrouter, team, channel, metadata):
43844402

43854403
def process_im_created(message_json, eventrouter, team, channel, metadata):
43864404
item = message_json["channel"]
4387-
channel = SlackDMChannel(eventrouter, team=team, users=team.users, **item)
4405+
channel = SlackDMChannel(
4406+
eventrouter, team.users, team.myidentifier, team=team, **item
4407+
)
43884408
team.channels[item["id"]] = channel
43894409
team.buffer_prnt("IM channel created: {}".format(channel.name))
43904410

@@ -5251,11 +5271,14 @@ def modify_buffer_line(buffer_pointer, ts, new_text):
52515271

52525272

52535273
def nick_from_profile(profile, username):
5254-
full_name = profile.get("real_name") or username
5255-
if config.use_full_names:
5256-
nick = full_name
5274+
if config.use_usernames:
5275+
nick = username
52575276
else:
5258-
nick = profile.get("display_name") or full_name
5277+
full_name = profile.get("real_name") or username
5278+
if config.use_full_names:
5279+
nick = full_name
5280+
else:
5281+
nick = profile.get("display_name") or full_name
52595282
return nick.replace(" ", "")
52605283

52615284

@@ -5324,7 +5347,12 @@ def tag(
53245347

53255348

53265349
def set_own_presence_active(team):
5327-
slackbot = team.get_channel_map()["Slackbot"]
5350+
if config.use_usernames:
5351+
nick_slackbot = "slackbot"
5352+
else:
5353+
nick_slackbot = "Slackbot"
5354+
5355+
slackbot = team.get_channel_map()[nick_slackbot]
53285356
channel = team.channels[slackbot]
53295357
request = {"type": "typing", "channel": channel.identifier}
53305358
channel.team.send_to_websocket(request, expect_reply=False)
@@ -5716,11 +5744,21 @@ def extra_info_function(channel):
57165744
@utf8_decode
57175745
def command_users(data, current_buffer, args):
57185746
"""
5719-
/slack users
5747+
/slack users [regex]
57205748
List the users in the current team.
5749+
If regex is given show only users that match the case-insensitive regex.
57215750
"""
57225751
team = EVENTROUTER.weechat_controller.buffers[current_buffer].team
5723-
return print_users_info(team, "Users", team.users.values())
5752+
5753+
if args:
5754+
pat = re.compile(args, flags=re.IGNORECASE)
5755+
users = [v for v in team.users.values() if pat.search(v.name)]
5756+
header = 'Users that match "{}"'.format(args)
5757+
else:
5758+
users = team.users.values()
5759+
header = "Users"
5760+
5761+
return print_users_info(team, header, users)
57245762

57255763

57265764
@slack_buffer_required
@@ -6780,16 +6818,16 @@ class PluginConfig(object):
67806818
"colorize_private_chats": Setting(
67816819
default="false", desc="Whether to use nick-colors in DM windows."
67826820
),
6783-
"debug_mode": Setting(
6784-
default="false",
6785-
desc="Open a dedicated buffer for debug messages and start logging"
6786-
" to it. How verbose the logging is depends on log_level.",
6787-
),
67886821
"debug_level": Setting(
67896822
default="3",
67906823
desc="Show only this level of debug info (or higher) when"
67916824
" debug_mode is on. Lower levels -> more messages.",
67926825
),
6826+
"debug_mode": Setting(
6827+
default="false",
6828+
desc="Open a dedicated buffer for debug messages and start logging"
6829+
" to it. How verbose the logging is depends on log_level.",
6830+
),
67936831
"distracting_channels": Setting(default="", desc="List of channels to hide."),
67946832
"external_user_suffix": Setting(
67956833
default="*", desc="The suffix appended to nicks to indicate external users."
@@ -6812,6 +6850,10 @@ class PluginConfig(object):
68126850
"link_previews": Setting(
68136851
default="true", desc="Show previews of website content linked by teammates."
68146852
),
6853+
"log_channel_created": Setting(
6854+
default="true",
6855+
desc='Log "Channel created" in the Server buffer.',
6856+
),
68156857
"map_underline_to": Setting(
68166858
default="_",
68176859
desc="When sending underlined text to slack, use this formatting"
@@ -6827,6 +6869,10 @@ class PluginConfig(object):
68276869
" all highlights, but not other messages. all: Show all activity,"
68286870
" like other channels.",
68296871
),
6872+
"never_away": Setting(
6873+
default="false",
6874+
desc='Poke Slack every five minutes so that it never marks you "away".',
6875+
),
68306876
"notify_subscribed_threads": Setting(
68316877
default="auto",
68326878
desc="Control if you want to see a notification in the team buffer when a"
@@ -6839,10 +6885,6 @@ class PluginConfig(object):
68396885
desc="Control if you want to see a notification in the team buffer when a"
68406886
"usergroup's handle has changed, either true or false.",
68416887
),
6842-
"never_away": Setting(
6843-
default="false",
6844-
desc='Poke Slack every five minutes so that it never marks you "away".',
6845-
),
68466888
"record_events": Setting(
68476889
default="false", desc="Log all traffic from Slack to disk as JSON."
68486890
),
@@ -6915,12 +6957,6 @@ class PluginConfig(object):
69156957
default="false",
69166958
desc="When enabled shows thread messages in the parent channel.",
69176959
),
6918-
"unfurl_ignore_alt_text": Setting(
6919-
default="false",
6920-
desc='When displaying ("unfurling") links to channels/users/etc,'
6921-
' ignore the "alt text" present in the message and instead use the'
6922-
" canonical name of the thing being linked to.",
6923-
),
69246960
"unfurl_auto_link_display": Setting(
69256961
default="both",
69266962
desc='When displaying ("unfurling") links to channels/users/etc,'
@@ -6931,6 +6967,12 @@ class PluginConfig(object):
69316967
' the user, "url" to only display the url or "both" (the default)'
69326968
" to display both.",
69336969
),
6970+
"unfurl_ignore_alt_text": Setting(
6971+
default="false",
6972+
desc='When displaying ("unfurling") links to channels/users/etc,'
6973+
' ignore the "alt text" present in the message and instead use the'
6974+
" canonical name of the thing being linked to.",
6975+
),
69346976
"unhide_buffers_with_activity": Setting(
69356977
default="false",
69366978
desc="When activity occurs on a buffer, unhide it even if it was"
@@ -6943,6 +6985,11 @@ class PluginConfig(object):
69436985
" false (the default), display names will be used if set, with a"
69446986
" fallback to the full name if display name is not set.",
69456987
),
6988+
"use_usernames": Setting(
6989+
default="false",
6990+
desc="Use usernames as the nicks for all users. Takes priority"
6991+
" over use_full_names. Default false.",
6992+
),
69466993
}
69476994

69486995
# Set missing settings to their defaults. Load non-missing settings from
@@ -7259,7 +7306,9 @@ def handle_getPresence(response_json, eventrouter, team, channel, metadata):
72597306

72607307
def create_channel_from_info(eventrouter, channel_info, team, myidentifier, users):
72617308
if channel_info.get("is_im"):
7262-
return SlackDMChannel(eventrouter, users, team=team, **channel_info)
7309+
return SlackDMChannel(
7310+
eventrouter, users, myidentifier, team=team, **channel_info
7311+
)
72637312
elif channel_info.get("is_mpim"):
72647313
return SlackMPDMChannel(
72657314
eventrouter, users, myidentifier, team=team, **channel_info
@@ -7327,6 +7376,28 @@ def handle_rtmconnect(response_json, eventrouter, team, channel, metadata):
73277376
"away" if initial_data["presence"]["manual_away"] else "active"
73287377
)
73297378

7379+
try:
7380+
all_notifications_prefs = json.loads(
7381+
initial_data["prefs"].get("all_notifications_prefs")
7382+
)
7383+
global_keywords = all_notifications_prefs.get("global", {}).get(
7384+
"global_keywords"
7385+
)
7386+
except json.decoder.JSONDecodeError:
7387+
global_keywords = None
7388+
7389+
if global_keywords is None:
7390+
print_error(
7391+
"global_keywords not found in users.prefs.get", warning=True
7392+
)
7393+
dbg(
7394+
"global_keywords not found in users.prefs.get. Response of users.prefs.get: {}".format(
7395+
json.dumps(initial_data["prefs"])
7396+
),
7397+
level=5,
7398+
)
7399+
global_keywords = ""
7400+
73307401
team_info = {
73317402
"id": team_id,
73327403
"name": response_json["team"]["id"],
@@ -7351,7 +7422,7 @@ def handle_rtmconnect(response_json, eventrouter, team, channel, metadata):
73517422
bots,
73527423
channels,
73537424
muted_channels=initial_data["prefs"]["muted_channels"],
7354-
highlight_words=initial_data["prefs"]["highlight_words"],
7425+
highlight_words=global_keywords,
73557426
)
73567427
eventrouter.register_team(team)
73577428
team.connect()

0 commit comments

Comments
 (0)