From 107958aed6b2035f479af8eee24ad2adaedf3e7f Mon Sep 17 00:00:00 2001 From: Windos Date: Thu, 11 Oct 2018 13:41:03 +1300 Subject: [PATCH 1/4] Adding array of special character, and escaping them This should fix issue #124. Tested using standard (!) prefixes, and indicated issue prefix (.), and other random ones (a) Basically, some characters are regex special characters... '.' is a wildcard... so it was matching on literally everything. Adding an escape character (only when it's needed) allows the match to be literal. --- PoshBot/Classes/Bot.ps1 | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/PoshBot/Classes/Bot.ps1 b/PoshBot/Classes/Bot.ps1 index 00115a9c..62d1a50a 100644 --- a/PoshBot/Classes/Bot.ps1 +++ b/PoshBot/Classes/Bot.ps1 @@ -32,6 +32,8 @@ class Bot : BaseLogger { hidden [System.Collections.Arraylist] $_PossibleCommandPrefixes = (New-Object System.Collections.ArrayList) + hidden [System.Collections.ArrayList] $_regexSpecialCharacters = @('[','\','^','$','.','|','?','*','+','(', ')','{', '}') + hidden [MiddlewareConfiguration] $_Middleware hidden [bool]$LazyLoadComplete = $false @@ -291,7 +293,12 @@ class Bot : BaseLogger { [bool]IsBotCommand([Message]$Message) { $parsedCommand = [CommandParser]::Parse($Message) foreach ($prefix in $this._PossibleCommandPrefixes ) { - if ($parsedCommand.command -match "^$prefix") { + if ($prefix -in $this._regexSpecialCharacters) { + $regexEscape = '\' + } else { + $regexEscape = $null + } + if ($parsedCommand.command -match "^$regexEscape$prefix") { $this.LogDebug('Message is a bot command') return $true } @@ -519,7 +526,12 @@ class Bot : BaseLogger { $firstWord = ($Message.Text -split ' ')[0] foreach ($prefix in $this._PossibleCommandPrefixes) { - if ($firstWord -match "^$prefix") { + if ($prefix -in $this._regexSpecialCharacters) { + $regexEscape = '\' + } else { + $regexEscape = $null + } + if ($firstWord -match "^$regexEscape$prefix") { $Message.Text = $Message.Text.TrimStart($prefix).Trim() } } From 386dbad5b8c7e9e3630eedeb2a54c9ab59edd6cf Mon Sep 17 00:00:00 2001 From: Windos Date: Thu, 11 Oct 2018 15:06:03 +1300 Subject: [PATCH 2/4] Revert "Adding array of special character, and escaping them" This reverts commit 107958aed6b2035f479af8eee24ad2adaedf3e7f. --- PoshBot/Classes/Bot.ps1 | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/PoshBot/Classes/Bot.ps1 b/PoshBot/Classes/Bot.ps1 index 62d1a50a..00115a9c 100644 --- a/PoshBot/Classes/Bot.ps1 +++ b/PoshBot/Classes/Bot.ps1 @@ -32,8 +32,6 @@ class Bot : BaseLogger { hidden [System.Collections.Arraylist] $_PossibleCommandPrefixes = (New-Object System.Collections.ArrayList) - hidden [System.Collections.ArrayList] $_regexSpecialCharacters = @('[','\','^','$','.','|','?','*','+','(', ')','{', '}') - hidden [MiddlewareConfiguration] $_Middleware hidden [bool]$LazyLoadComplete = $false @@ -293,12 +291,7 @@ class Bot : BaseLogger { [bool]IsBotCommand([Message]$Message) { $parsedCommand = [CommandParser]::Parse($Message) foreach ($prefix in $this._PossibleCommandPrefixes ) { - if ($prefix -in $this._regexSpecialCharacters) { - $regexEscape = '\' - } else { - $regexEscape = $null - } - if ($parsedCommand.command -match "^$regexEscape$prefix") { + if ($parsedCommand.command -match "^$prefix") { $this.LogDebug('Message is a bot command') return $true } @@ -526,12 +519,7 @@ class Bot : BaseLogger { $firstWord = ($Message.Text -split ' ')[0] foreach ($prefix in $this._PossibleCommandPrefixes) { - if ($prefix -in $this._regexSpecialCharacters) { - $regexEscape = '\' - } else { - $regexEscape = $null - } - if ($firstWord -match "^$regexEscape$prefix") { + if ($firstWord -match "^$prefix") { $Message.Text = $Message.Text.TrimStart($prefix).Trim() } } From 61dc27e9ca168376d6e5260615c917a72b5b634c Mon Sep 17 00:00:00 2001 From: Windos Date: Thu, 11 Oct 2018 15:08:54 +1300 Subject: [PATCH 3/4] Using the regex escape method Thanks for the suggestion @ChrisLGardner, much cleaner! --- PoshBot/Classes/Bot.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PoshBot/Classes/Bot.ps1 b/PoshBot/Classes/Bot.ps1 index 00115a9c..94ca929e 100644 --- a/PoshBot/Classes/Bot.ps1 +++ b/PoshBot/Classes/Bot.ps1 @@ -291,6 +291,7 @@ class Bot : BaseLogger { [bool]IsBotCommand([Message]$Message) { $parsedCommand = [CommandParser]::Parse($Message) foreach ($prefix in $this._PossibleCommandPrefixes ) { + $prefix = [regex]::Escape($prefix) if ($parsedCommand.command -match "^$prefix") { $this.LogDebug('Message is a bot command') return $true @@ -519,6 +520,7 @@ class Bot : BaseLogger { $firstWord = ($Message.Text -split ' ')[0] foreach ($prefix in $this._PossibleCommandPrefixes) { + $prefix = [regex]::Escape($prefix) if ($firstWord -match "^$prefix") { $Message.Text = $Message.Text.TrimStart($prefix).Trim() } From 2e2fd9afc923af557a0a3750873039df9c22c19b Mon Sep 17 00:00:00 2001 From: Windos Date: Thu, 11 Oct 2018 15:31:29 +1300 Subject: [PATCH 4/4] Assign to different variable Given it's used to trim the string, an escape character would have caused issues here. --- PoshBot/Classes/Bot.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PoshBot/Classes/Bot.ps1 b/PoshBot/Classes/Bot.ps1 index 94ca929e..d53e9de5 100644 --- a/PoshBot/Classes/Bot.ps1 +++ b/PoshBot/Classes/Bot.ps1 @@ -520,8 +520,8 @@ class Bot : BaseLogger { $firstWord = ($Message.Text -split ' ')[0] foreach ($prefix in $this._PossibleCommandPrefixes) { - $prefix = [regex]::Escape($prefix) - if ($firstWord -match "^$prefix") { + $prefixEscaped = [regex]::Escape($prefix) + if ($firstWord -match "^$prefixEscaped") { $Message.Text = $Message.Text.TrimStart($prefix).Trim() } }