From 6aff7ddcbba5554d9cc0eb1630ed808743c09ba3 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 2 Jun 2020 09:41:35 +0200 Subject: [PATCH] Escape quotes in reply/redact completion message hints --- matrix/completion.py | 11 +++++++---- matrix/utils.py | 16 ++++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/matrix/completion.py b/matrix/completion.py index 48a8261..a1ac559 100644 --- a/matrix/completion.py +++ b/matrix/completion.py @@ -137,13 +137,16 @@ def matrix_message_completion_cb(data, completion_item, buffer, completion): if not event_id: continue - message = line.message + # Make sure we'll be able to reliably detect the end of the + # quoted snippet + message_fmt = line.message.replace("\\", "\\\\") \ + .replace('"', '\\"') - if len(message) > REDACTION_COMP_LEN + 2: - message = message[:REDACTION_COMP_LEN] + ".." + if len(message_fmt) > REDACTION_COMP_LEN + 2: + message_fmt = message_fmt[:REDACTION_COMP_LEN] + ".." item = ('{event_id}|"{message}"').format( - event_id=event_id, message=message + event_id=event_id, message=message_fmt ) W.hook_completion_list_add( diff --git a/matrix/utils.py b/matrix/utils.py index f233ca7..ce5f9d1 100644 --- a/matrix/utils.py +++ b/matrix/utils.py @@ -182,10 +182,18 @@ def parse_redact_args(args): event_id, rest = (args, "") if had_example_text: - try: - _, _, reason = rest.split("\"", 2) - except ValueError: - reason = None + rest = rest.lstrip() + reason = None # until it has been correctly determined + if rest[0] == '"': + escaped = False + for i in range(1, len(rest)): + if escaped: + escaped = False + elif rest[i] == "\\": + escaped = True + elif rest[i] == '"': + reason = rest[i+1:] + break else: reason = rest