aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
authorTollef Fog Heen <tfheen@err.no>2016-11-28 21:33:39 +0100
committerTollef Fog Heen <tfheen@err.no>2016-11-28 21:33:39 +0100
commita04a0c89c86f9393d98643fff6066b1c1a234a54 (patch)
tree5c08f820d3ff276a39a31c4b60f935b841eddadd /wee_slack.py
parent12f022738915169683e6842e010b4061f918441f (diff)
downloadwee-slack-a04a0c89c86f9393d98643fff6066b1c1a234a54.tar.gz
Make message editing use actual regexes
If you use s/foo/bar/, use actual regexes for this, since that's what some of us expect. Also add support for /g to do it globally. Don't add any way to have the old functionality, if too many people complain we can have that behind another syntax or a flag. Based heavily on the code by @terminalmage Fixes #219, #197
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py32
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):