diff --git a/matrix/commands.py b/matrix/commands.py index a74f99c..f2f15fd 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -30,7 +30,7 @@ from .colors import Formatted from .globals import SERVERS, W, UPLOADS, SCRIPT_NAME from .server import MatrixServer from .utf import utf8_decode -from .utils import key_from_value +from .utils import key_from_value, parse_redact_args from .uploads import UploadsBuffer, Upload try: @@ -1280,14 +1280,9 @@ def matrix_redact_command_cb(data, buffer, args): if buffer in server.buffers.values(): room_buffer = server.find_room_from_ptr(buffer) - matches = re.match( - r"^(\$[a-zA-Z0-9]+:([a-z0-9])(([a-z0-9-]{1,61})?[a-z0-9]{1})?" - r"(\.[a-z0-9](([a-z0-9-]{1,61})?[a-z0-9]{1})?)?" - r"(\.[a-zA-Z]{2,4})+)(:\".*\")? ?(.*)?$", - args - ) + event_id, reason = parse_redact_args(args) - if not matches: + if not event_id: message = ( "{prefix}matrix: Invalid command " "arguments (see /help redact)" @@ -1295,9 +1290,6 @@ def matrix_redact_command_cb(data, buffer, args): W.prnt("", message) return W.WEECHAT_RC_ERROR - groups = matches.groups() - event_id, reason = (groups[0], groups[-1]) - lines = room_buffer.weechat_buffer.find_lines( partial(predicate, event_id), max_lines=1 ) diff --git a/matrix/completion.py b/matrix/completion.py index 578309e..48a8261 100644 --- a/matrix/completion.py +++ b/matrix/completion.py @@ -142,7 +142,7 @@ def matrix_message_completion_cb(data, completion_item, buffer, completion): if len(message) > REDACTION_COMP_LEN + 2: message = message[:REDACTION_COMP_LEN] + ".." - item = ('{event_id}:"{message}"').format( + item = ('{event_id}|"{message}"').format( event_id=event_id, message=message ) diff --git a/matrix/utils.py b/matrix/utils.py index 6c29003..f233ca7 100644 --- a/matrix/utils.py +++ b/matrix/utils.py @@ -166,3 +166,34 @@ def text_block(text, margin=0): def colored_text_block(text, margin=0, color_pair=""): """ Like text_block, but also colors it.""" return string_color_and_reset(text_block(text, margin=margin), color_pair) + +def parse_redact_args(args): + args = args.strip() + + had_example_text = False + + try: + event_id, rest = args.split("|", 1) + had_example_text = True + except ValueError: + try: + event_id, rest = args.split(" ", 1) + except ValueError: + event_id, rest = (args, "") + + if had_example_text: + try: + _, _, reason = rest.split("\"", 2) + except ValueError: + reason = None + else: + reason = rest + + event_id = event_id.strip() + if reason: + reason = reason.strip() + # The reason might be an empty string, set it to None if so + else: + reason = None + + return event_id, reason diff --git a/tests/buffer_test.py b/tests/buffer_test.py index e146ac5..0ba5697 100644 --- a/tests/buffer_test.py +++ b/tests/buffer_test.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from matrix.buffer import WeechatChannelBuffer +from matrix.utils import parse_redact_args class TestClass(object): @@ -14,3 +15,54 @@ class TestClass(object): b = WeechatChannelBuffer("test_buffer_name", "example.org", "alice") b.message("alice", "hello world", 0, 0) assert b + + def test_redact_args_parse(self): + args = '$81wbnOYZllVZJcstsnXpq7dmugA775-JT4IB-uPT680|"Hello world" No specific reason' + event_id, reason = parse_redact_args(args) + assert event_id == '$81wbnOYZllVZJcstsnXpq7dmugA775-JT4IB-uPT680' + assert reason == 'No specific reason' + + args = '$15677776791893pZSXx:example.org|"Hello world" No reason at all' + event_id, reason = parse_redact_args(args) + assert event_id == '$15677776791893pZSXx:example.org' + assert reason == 'No reason at all' + + args = '$15677776791893pZSXx:example.org No reason at all' + event_id, reason = parse_redact_args(args) + assert event_id == '$15677776791893pZSXx:example.org' + assert reason == 'No reason at all' + + args = '$81wbnOYZllVZJcstsnXpq7dmugA775-JT4IB-uPT680 No specific reason' + event_id, reason = parse_redact_args(args) + assert event_id == '$81wbnOYZllVZJcstsnXpq7dmugA775-JT4IB-uPT680' + assert reason == 'No specific reason' + + args = '$81wbnOYZllVZJcstsnXpq7dmugA775-JT4IB-uPT680' + event_id, reason = parse_redact_args(args) + assert event_id == '$81wbnOYZllVZJcstsnXpq7dmugA775-JT4IB-uPT680' + assert reason == None + + args = '$15677776791893pZSXx:example.org' + event_id, reason = parse_redact_args(args) + assert event_id == '$15677776791893pZSXx:example.org' + assert reason == None + + args = ' ' + event_id, reason = parse_redact_args(args) + assert event_id == '' + assert reason == None + + args = '$15677776791893pZSXx:example.org|"Hello world"' + event_id, reason = parse_redact_args(args) + assert event_id == '$15677776791893pZSXx:example.org' + assert reason == None + + args = '$15677776791893pZSXx:example.org|"Hello world' + event_id, reason = parse_redact_args(args) + assert event_id == '$15677776791893pZSXx:example.org' + assert reason == None + + args = '$15677776791893pZSXx:example.org "Hello world"' + event_id, reason = parse_redact_args(args) + assert event_id == '$15677776791893pZSXx:example.org' + assert reason == '"Hello world"'