aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py58
1 files changed, 39 insertions, 19 deletions
diff --git a/wee_slack.py b/wee_slack.py
index 4d78fc2..680ff6b 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -610,15 +610,17 @@ def buffer_input_callback(signal, buffer_ptr, data):
eventrouter = eval(signal)
channel = eventrouter.weechat_controller.get_channel_from_buffer_ptr(buffer_ptr)
if not channel:
- return w.WEECHAT_RC_OK_EAT
+ return w.WEECHAT_RC_ERROR
reaction = re.match("^\s*(\d*)(\+|-):(.*):\s*$", data)
+ substitute = re.match("^(\d*)s/", data)
if reaction:
if reaction.group(2) == "+":
channel.send_add_reaction(int(reaction.group(1) or 1), reaction.group(3))
elif reaction.group(2) == "-":
channel.send_remove_reaction(int(reaction.group(1) or 1), reaction.group(3))
- elif data.startswith('s/'):
+ elif substitute:
+ msgno = int(substitute.group(1) or 1)
try:
old, new, flags = re.split(r'(?<!\\)/', data)[1:]
except ValueError:
@@ -628,11 +630,11 @@ def buffer_input_callback(signal, buffer_ptr, data):
# rid of escapes.
new = new.replace(r'\/', '/')
old = old.replace(r'\/', '/')
- channel.edit_previous_message(old, new, flags)
+ channel.edit_nth_previous_message(msgno, old, new, flags)
else:
channel.send_message(data)
# this is probably wrong channel.mark_read(update_remote=True, force=True)
- return w.WEECHAT_RC_ERROR
+ return w.WEECHAT_RC_OK
def buffer_switch_callback(signal, sig_type, data):
@@ -697,7 +699,7 @@ def typing_notification_cb(signal, sig_type, data):
if typing_timer + 4 < now:
current_buffer = w.current_buffer()
channel = EVENTROUTER.weechat_controller.buffers.get(current_buffer, None)
- if channel:
+ if channel and channel.type != "thread":
identifier = channel.identifier
request = {"type": "typing", "channel": identifier}
channel.team.send_to_websocket(request, expect_reply=False)
@@ -975,6 +977,7 @@ class SlackTeam(object):
self.channel_buffer = w.buffer_new("{}".format(self.preferred_name), "buffer_input_callback", "EVENTROUTER", "", "")
self.eventrouter.weechat_controller.register_buffer(self.channel_buffer, self)
w.buffer_set(self.channel_buffer, "localvar_set_type", 'server')
+ w.buffer_set(self.channel_buffer, "localvar_set_nick", self.nick)
if w.config_string(w.config_get('irc.look.server_buffer')) == 'merge_with_core':
w.buffer_merge(self.channel_buffer, w.buffer_search_main())
w.buffer_set(self.channel_buffer, "nicklist", "1")
@@ -1148,7 +1151,7 @@ class SlackChannel(object):
def set_unread_count_display(self, count):
self.unread_count_display = count
- if (self.unread_count_display > 0):
+ for c in range(self.unread_count_display):
if self.type == "im":
w.buffer_set(self.channel_buffer, "hotlist", "2")
else:
@@ -1236,6 +1239,7 @@ class SlackChannel(object):
else:
w.buffer_set(self.channel_buffer, "localvar_set_type", 'channel')
w.buffer_set(self.channel_buffer, "localvar_set_channel", self.formatted_name())
+ w.buffer_set(self.channel_buffer, "localvar_set_nick", self.team.nick)
w.buffer_set(self.channel_buffer, "short_name", self.formatted_name(style="sidebar", enable_color=True))
self.render_topic()
self.eventrouter.weechat_controller.set_refresh_buffer_list(True)
@@ -1343,8 +1347,8 @@ class SlackChannel(object):
modify_buffer_line(self.channel_buffer, text, ts.major, ts.minor)
return True
- def edit_previous_message(self, old, new, flags):
- message = self.my_last_message()
+ def edit_nth_previous_message(self, n, old, new, flags):
+ message = self.my_last_message(n)
if new == "" and old == "":
s = SlackRequest(self.team.token, "chat.delete", {"channel": self.identifier, "ts": message['ts']}, team_hash=self.team.team_hash, channel_identifier=self.identifier)
self.eventrouter.receive(s)
@@ -1357,11 +1361,13 @@ class SlackChannel(object):
s = SlackRequest(self.team.token, "chat.update", {"channel": self.identifier, "ts": message['ts'], "text": new_message}, team_hash=self.team.team_hash, channel_identifier=self.identifier)
self.eventrouter.receive(s)
- def my_last_message(self):
+ def my_last_message(self, msgno):
for message in reversed(self.sorted_message_keys()):
m = self.messages[message]
if "user" in m.message_json and "text" in m.message_json and m.message_json["user"] == self.team.myidentifier:
- return m.message_json
+ msgno -= 1
+ if msgno == 0:
+ return m.message_json
def is_visible(self):
return w.buffer_get_integer(self.channel_buffer, "hidden") == 0
@@ -1778,6 +1784,7 @@ class SlackThreadChannel(object):
self.channel_buffer = w.buffer_new(self.formatted_name(style="long_default"), "buffer_input_callback", "EVENTROUTER", "", "")
self.eventrouter.weechat_controller.register_buffer(self.channel_buffer, self)
w.buffer_set(self.channel_buffer, "localvar_set_type", 'channel')
+ w.buffer_set(self.channel_buffer, "localvar_set_nick", self.parent_message.team.nick)
w.buffer_set(self.channel_buffer, "localvar_set_channel", self.formatted_name())
w.buffer_set(self.channel_buffer, "short_name", self.formatted_name(style="sidebar", enable_color=True))
time_format = w.config_string(w.config_get("weechat.look.buffer_time_format"))
@@ -2339,9 +2346,9 @@ def subprocess_message_deleted(message_json, eventrouter, channel, team):
def subprocess_channel_topic(message_json, eventrouter, channel, team):
- text = unfurl_refs(message_json["text"], ignore_alt_text=False)
+ text = unhtmlescape(unfurl_refs(message_json["text"], ignore_alt_text=False))
channel.buffer_prnt(w.prefix("network").rstrip(), text, message_json["ts"], tagset="muted")
- channel.render_topic(message_json["topic"])
+ channel.render_topic(unhtmlescape(message_json["topic"]))
def process_reply(message_json, eventrouter, **kwargs):
@@ -2510,10 +2517,7 @@ def render(message_json, team, channel, force=False):
text += unfurl_refs(unwrap_attachments(message_json, text_before), ignore_alt_text=config.unfurl_ignore_alt_text)
text = text.lstrip()
- text = text.replace("\t", " ")
- text = text.replace("&lt;", "<")
- text = text.replace("&gt;", ">")
- text = text.replace("&amp;", "&")
+ text = unhtmlescape(text.replace("\t", " "))
if message_json.get('mrkdwn', True):
text = render_formatting(text)
@@ -2532,9 +2536,16 @@ def linkify_text(message, team, channel):
usernames = team.get_username_map()
channels = team.get_channel_map()
message = (message
+ # Replace IRC formatting chars with Slack formatting chars.
.replace('\x02', '*')
.replace('\x1D', '_')
.replace('\x1F', config.map_underline_to)
+ # Escape chars that have special meaning to Slack. Note that we do not
+ # (and should not) perform a full URL escaping here.
+ # See https://api.slack.com/docs/message-formatting for details.
+ .replace('<', '&lt')
+ .replace('>', '&gt')
+ .replace('&', '&amp')
.split(' '))
for item in enumerate(message):
targets = re.match('^\s*([@#])([\w.-]+[\w. -])(\W*)', item[1])
@@ -2596,6 +2607,12 @@ def unfurl_ref(ref, ignore_alt_text=False):
return display_text
+def unhtmlescape(text):
+ return text.replace("&lt;", "<") \
+ .replace("&gt;", ">") \
+ .replace("&amp;", "&")
+
+
def unwrap_attachments(message_json, text_before):
attachment_text = ''
a = message_json.get("attachments", None)
@@ -2848,14 +2865,17 @@ def msg_command_cb(data, current_buffer, args):
dbg("msg_command_cb")
aargs = args.split(None, 2)
who = aargs[1]
- command_talk(data, current_buffer, who)
+ if who == "*":
+ who = EVENTROUTER.weechat_controller.buffers[current_buffer].slack_name
+ else:
+ command_talk(data, current_buffer, who)
if len(aargs) > 2:
message = aargs[2]
team = EVENTROUTER.weechat_controller.buffers[current_buffer].team
cmap = team.get_channel_map()
if who in cmap:
- channel = team.channels[cmap[channel]]
+ channel = team.channels[cmap[who]]
channel.send_message(message)
return w.WEECHAT_RC_OK_EAT
@@ -3106,7 +3126,7 @@ def command_status(data, current_buffer, args):
profile = {"status_text":text,"status_emoji":emoji}
- s = SlackRequest(team.token, "users.profile.set", {"profile": profile}, team_hash=team.team_hash, channel_identifier=channel.identifier)
+ s = SlackRequest(team.token, "users.profile.set", {"profile": profile}, team_hash=team.team_hash)
EVENTROUTER.receive(s)