aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py76
1 files changed, 56 insertions, 20 deletions
diff --git a/wee_slack.py b/wee_slack.py
index 9a15b9a..5cc841c 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -32,7 +32,7 @@ SCRIPT_DESC = "Extends weechat for typing notification/search/etc on slack.com"
BACKLOG_SIZE = 200
SCROLLBACK_SIZE = 500
-CACHE_VERSION = "3"
+CACHE_VERSION = "4"
SLACK_API_TRANSLATOR = {
"channel": {
@@ -132,6 +132,7 @@ class SlackServer(object):
def __init__(self, token):
self.nick = None
self.name = None
+ self.team = None
self.domain = None
self.server_buffer_name = None
self.login_data = None
@@ -213,6 +214,7 @@ class SlackServer(object):
def connected_to_slack(self, login_data):
if login_data["ok"]:
+ self.team = login_data["team"]["domain"]
self.domain = login_data["team"]["domain"] + ".slack.com"
dbg("connected to {}".format(self.domain))
self.identifier = self.domain
@@ -339,6 +341,8 @@ class SlackServer(object):
def buffer_input_cb(b, buffer, data):
channel = channels.find(buffer)
+ if not channel:
+ return w.WEECHAT_RC_OK_EAT
reaction = re.match("(\d*)(\+|-):(.*):", data)
if not reaction and not data.startswith('s/'):
channel.send_message(data)
@@ -419,6 +423,11 @@ class Channel(object):
w.buffer_set(self.channel_buffer, "localvar_set_type", 'private')
else:
w.buffer_set(self.channel_buffer, "localvar_set_type", 'channel')
+ if self.server.alias:
+ w.buffer_set(self.channel_buffer, "localvar_set_server", self.server.alias)
+ else:
+ w.buffer_set(self.channel_buffer, "localvar_set_server", self.server.team)
+ w.buffer_set(self.channel_buffer, "localvar_set_channel", self.name)
w.buffer_set(self.channel_buffer, "short_name", self.name)
buffer_list_update_next()
@@ -541,7 +550,7 @@ class Channel(object):
return " ".join(message)
def set_topic(self, topic):
- topic = topic.encode('ascii', 'ignore')
+ topic = topic.encode('utf-8')
w.buffer_set(self.channel_buffer, "title", topic)
def open(self, update_remote=True):
@@ -711,17 +720,17 @@ class Channel(object):
self.buffer_redraw()
return True
- def add_reaction(self, ts, reaction):
+ def add_reaction(self, ts, reaction, user):
if self.has_message(ts):
message_index = self.messages.index(ts)
- self.messages[message_index].add_reaction(reaction)
+ self.messages[message_index].add_reaction(reaction, user)
self.change_message(ts)
return True
- def remove_reaction(self, ts, reaction):
+ def remove_reaction(self, ts, reaction, user):
if self.has_message(ts):
message_index = self.messages.index(ts)
- self.messages[message_index].remove_reaction(reaction)
+ self.messages[message_index].remove_reaction(reaction, user)
self.change_message(ts)
return True
@@ -844,6 +853,9 @@ class User(object):
return [self.name, "@" + self.name, self.identifier]
def set_active(self):
+ if self.deleted:
+ return
+
self.presence = "active"
for channel in self.server.channels:
if channel.has_user(self.identifier):
@@ -854,6 +866,9 @@ class User(object):
buffer_list_update_next()
def set_inactive(self):
+ if self.deleted:
+ return
+
self.presence = "away"
for channel in self.server.channels:
if channel.has_user(self.identifier):
@@ -933,23 +948,24 @@ class Message(object):
new_text = unicode(new_text, 'utf-8')
self.message_json["text"] = new_text
- def add_reaction(self, reaction):
+ def add_reaction(self, reaction, user):
if "reactions" in self.message_json:
found = False
for r in self.message_json["reactions"]:
- if r["name"] == reaction:
- r["count"] += 1
+ if r["name"] == reaction and user not in r["users"]:
+ r["users"].append(user)
found = True
+
if not found:
- self.message_json["reactions"].append({u"count": 1, u"name": reaction})
+ self.message_json["reactions"].append({u"name": reaction, u"users": [user]})
else:
- self.message_json["reactions"] = [{u"count": 1, u"name": reaction}]
+ self.message_json["reactions"] = [{u"name": reaction, u"users": [user]}]
- def remove_reaction(self, reaction):
+ def remove_reaction(self, reaction, user):
if "reactions" in self.message_json:
for r in self.message_json["reactions"]:
- if r["name"] == reaction:
- r["count"] -= 1
+ if r["name"] == reaction and user in r["users"]:
+ r["users"].remove(user)
else:
pass
@@ -1477,6 +1493,18 @@ def process_channel_archive(message_json):
channel.detach_buffer()
+def process_group_join(message_json):
+ process_channel_join(message_json)
+
+
+def process_group_leave(message_json):
+ process_channel_leave(message_json)
+
+
+def process_group_topic(message_json):
+ process_channel_topic(message_json)
+
+
def process_group_left(message_json):
server = servers.find(message_json["_server"])
server.channels.find(message_json["channel"]).close(False)
@@ -1559,14 +1587,14 @@ def process_error(message_json):
def process_reaction_added(message_json):
if message_json["item"].get("type") == "message":
channel = channels.find(message_json["item"]["channel"])
- channel.add_reaction(message_json["item"]["ts"], message_json["reaction"])
+ channel.add_reaction(message_json["item"]["ts"], message_json["reaction"], message_json["user"])
else:
dbg("Reaction to item type not supported: " + str(message_json))
def process_reaction_removed(message_json):
if message_json["item"].get("type") == "message":
channel = channels.find(message_json["item"]["channel"])
- channel.remove_reaction(message_json["item"]["ts"], message_json["reaction"])
+ channel.remove_reaction(message_json["item"]["ts"], message_json["reaction"], message_json["user"])
else:
dbg("Reaction to item type not supported: " + str(message_json))
@@ -1577,9 +1605,14 @@ def create_reaction_string(reactions):
else:
reaction_string = ' ['
for r in reactions:
- if r["count"] > 0:
+ if len(r["users"]) > 0:
count += 1
- reaction_string += ":{}:{} ".format(r["name"], r["count"])
+ if show_reaction_nicks:
+ nicks = [resolve_ref("@{}".format(user)) for user in r["users"]]
+ users = "({})".format(",".join(nicks))
+ else:
+ users = len(r["users"])
+ reaction_string += ":{}:{} ".format(r["name"], users)
reaction_string = reaction_string[:-1] + ']'
if count == 0:
reaction_string = ''
@@ -1649,7 +1682,7 @@ def render_message(message_json, force=False):
def process_message(message_json, cache=True):
try:
# send these subtype messages elsewhere
- known_subtypes = ["message_changed", 'message_deleted', 'channel_join', 'channel_leave', 'channel_topic', 'bot_enable', 'bot_disable']
+ known_subtypes = ["message_changed", 'message_deleted', 'channel_join', 'channel_leave', 'channel_topic', 'group_join', 'group_leave', 'group_topic', 'bot_enable', 'bot_disable']
if "subtype" in message_json and message_json["subtype"] in known_subtypes:
proc[message_json["subtype"]](message_json)
@@ -2153,7 +2186,7 @@ def create_slack_debug_buffer():
def config_changed_cb(data, option, value):
global slack_api_token, distracting_channels, colorize_nicks, colorize_private_chats, slack_debug, debug_mode, \
- unfurl_ignore_alt_text, colorize_messages
+ unfurl_ignore_alt_text, colorize_messages, show_reaction_nicks
slack_api_token = w.config_get_plugin("slack_api_token")
@@ -2167,6 +2200,7 @@ def config_changed_cb(data, option, value):
if debug_mode != '' and debug_mode != 'false':
create_slack_debug_buffer()
colorize_private_chats = w.config_string_to_boolean(w.config_get_plugin("colorize_private_chats"))
+ show_reaction_nicks = w.config_string_to_boolean(w.config_get_plugin("show_reaction_nicks"))
unfurl_ignore_alt_text = False
if w.config_get_plugin('unfurl_ignore_alt_text') != "0":
@@ -2237,6 +2271,8 @@ if __name__ == "__main__":
w.config_set_plugin('unfurl_ignore_alt_text', "0")
if not w.config_get_plugin('switch_buffer_on_join'):
w.config_set_plugin('switch_buffer_on_join', "1")
+ if not w.config_get_plugin('show_reaction_nicks'):
+ w.config_set_plugin('show_reaction_nicks', "0")
if w.config_get_plugin('channels_not_on_current_server_color'):
w.config_option_unset('channels_not_on_current_server_color')