commands: Change the way the redact command parses it's arguments.
This fixes #104.
This commit is contained in:
parent
a58d21710b
commit
5924264139
4 changed files with 87 additions and 12 deletions
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"'
|
||||
|
|
Loading…
Reference in a new issue