diff --git a/rivescript/parser.py b/rivescript/parser.py index 1a09d9c..951c39d 100644 --- a/rivescript/parser.py +++ b/rivescript/parser.py @@ -535,12 +535,12 @@ def check_syntax(self, cmd, line): if parts[0] == "begin" and len(parts) > 1: return "The 'begin' label takes no additional arguments, should be verbatim '> begin'" elif parts[0] == "topic": - match = re.match(RE.name_syntax, line) - if match: + search = re.search(RE.name_syntax, line) + if search: return "Topics should be lowercased and contain only numbers and letters" elif parts[0] == "object": - match = re.match(RE.name_syntax, line) - if match: + search = re.search(RE.obj_syntax, line) # Upper case is allowed + if search: return "Objects can only contain numbers and letters" elif cmd == '+' or cmd == '%' or cmd == '@': # + Trigger, % Previous, @ Redirect @@ -585,12 +585,12 @@ def check_syntax(self, cmd, line): # In UTF-8 mode, most symbols are allowed. if self.utf8: - match = re.match(RE.utf8_trig, line) - if match: + search = re.search(RE.utf8_trig, line) + if search: return "Triggers can't contain uppercase letters, backslashes or dots in UTF-8 mode." else: - match = re.match(RE.trig_syntax, line) - if match: + search = re.search(RE.trig_syntax, line) + if search: return "Triggers may only contain lowercase letters, numbers, and these symbols: ( | ) [ ] * _ # @ { } < > =" elif cmd == '-' or cmd == '^' or cmd == '/': # - Trigger, ^ Continue, / Comment diff --git a/rivescript/regexp.py b/rivescript/regexp.py index 45ce8e3..5df6dc0 100644 --- a/rivescript/regexp.py +++ b/rivescript/regexp.py @@ -24,6 +24,7 @@ class RE(object): array = re.compile(r'\@(.+?)\b') def_syntax = re.compile(r'^.+(?:\s+.+|)\s*=\s*.+?$') name_syntax = re.compile(r'[^a-z0-9_\-\s]') + obj_syntax = re.compile(r'[^A-Za-z0-9_\-\s]') utf8_trig = re.compile(r'[A-Z\\.]') trig_syntax = re.compile(r'[^a-z0-9(\|)\[\]*_#@{}<>=\s]') cond_syntax = re.compile(r'^.+?\s*(?:==|eq|!=|ne|<>|<|<=|>|>=)\s*.+?=>.+?$') diff --git a/tests/test_format_message.py b/tests/test_format_message.py index 4ccbb38..b21e625 100644 --- a/tests/test_format_message.py +++ b/tests/test_format_message.py @@ -28,4 +28,32 @@ def test_format_triggers(self): """) self.reply("hi there", "hi there") - self.reply("hi here", "hi here") \ No newline at end of file + self.reply("hi here", "hi here") + + def test_invalid_character_raise_exception(self): + self.assertRaises(Exception, self.new, """ + + $hello + - hi + """) # This test passes with `match`, which only check at the beginning + self.assertRaises(Exception, self.new, """ + + hello$ + - hi + """) # This test does not pass because the beginning is good, no $ + self.assertRaises(Exception, self.new, """ + > topic Greetings + + hello + - hi + object hash %perl + my ($rs, $args) = @_; + my $method = shift @{$args}; + object hash Perl + my ($rs, $args) = @_; + my $method = shift @{$args}; +