diff options
-rw-r--r-- | wee_slack.py | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/wee_slack.py b/wee_slack.py index 1329b30..5f4833a 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -380,17 +380,25 @@ def buffer_input_cb(b, buffer, data): if not channel: return w.WEECHAT_RC_OK_EAT reaction = re.match("^\s*(\d*)(\+|-):(.*):\s*$", data) - if not reaction and not data.startswith('s/'): - channel.send_message(data) - # channel.buffer_prnt(channel.server.nick, data) - elif reaction: + 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.count('/') == 3: - old, new = data.split('/')[1:3] - channel.change_previous_message(old.decode("utf-8"), new.decode("utf-8")) + elif data.startswith('s/'): + try: + old, new, flags = re.split(r'(?<!\\)/', data)[1:] + except ValueError: + pass + else: + # Replacement string in re.sub() is a string, not a regex, so get + # rid of escapes. + new = new.replace(r'\/', '/') + old = old.replace(r'\/', '/') + channel.change_previous_message(old.decode("utf-8"), new.decode("utf-8"), flags) + else: + channel.send_message(data) + # channel.buffer_prnt(channel.server.nick, data) channel.mark_read(True) return w.WEECHAT_RC_ERROR @@ -785,13 +793,17 @@ class Channel(object): data = {"channel": self.identifier, "timestamp": timestamp, "name": reaction} async_slack_api_request(self.server.domain, self.server.token, method, data) - def change_previous_message(self, old, new): + def change_previous_message(self, old, new, flags): message = self.my_last_message() if new == "" and old == "": async_slack_api_request(self.server.domain, self.server.token, 'chat.delete', {"channel": self.identifier, "ts": message['ts']}) else: - new_message = message["text"].replace(old, new) - async_slack_api_request(self.server.domain, self.server.token, 'chat.update', {"channel": self.identifier, "ts": message['ts'], "text": new_message.encode("utf-8")}) + num_replace = 1 + if 'g' in flags: + num_replace = 0 + new_message = re.sub(old, new, message["text"], num_replace) + if new_message != message["text"]: + async_slack_api_request(self.server.domain, self.server.token, 'chat.update', {"channel": self.identifier, "ts": message['ts'], "text": new_message.encode("utf-8")}) def my_last_message(self): for message in reversed(self.messages): |