From c01f238a34ad846f8732c8bf97fbbd96fbf6a7ae Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Sun, 18 Dec 2016 15:31:12 -0800 Subject: Fix framework message position boundary splits The framework would take the length of the prefix in bytes, and then search for a command either directly after the byte length _and_ plus one for a command. Instead, ensure that the message is longer than the prefix. --- src/ext/framework/mod.rs | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'src/ext/framework') diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs index 777736f..8047c20 100644 --- a/src/ext/framework/mod.rs +++ b/src/ext/framework/mod.rs @@ -284,36 +284,39 @@ impl Framework { let res = command::positions(&context, &message.content, &self.configuration); let positions = match res { - Some(positions) => positions, + Some(mut positions) => { + // First, take out the prefixes that are as long as _or_ longer + // than the message, to avoid character boundary violations. + positions.retain(|p| *p < message.content.len()); + + // Ensure that there is _at least one_ position remaining. There + // is no point in continuing if there is not. + if positions.is_empty() { + return; + } + + positions + }, None => return, }; - // Ensure that the message length is at least longer than a prefix - // length. There's no point in checking further ahead if there's nothing - // _to_ check. - if positions.iter().all(|p| message.content.len() <= *p) { - return; - } - 'outer: for position in positions { let mut built = String::new(); + let round = message.content.chars() + .skip(position) + .collect::(); + let round = round.trim() + .split_whitespace() + .collect::>(); for i in 0..self.configuration.depth { if i != 0 { built.push(' '); } - built.push_str(match { - message.content - .split_at(position) - .1 - .trim() - .split_whitespace() - .collect::>() - .get(i) - } { + built.push_str(match round.get(i) { Some(piece) => piece, - None => continue, + None => continue 'outer, }); let groups = self.groups.clone(); -- cgit v1.2.3