Skip to content

Commit

Permalink
Merge pull request #203 from aichaos/bug/201-async-redirect
Browse files Browse the repository at this point in the history
Allow synchronous call tags to work inside redirects
  • Loading branch information
kirsle authored Jan 19, 2017
2 parents 44383d4 + 7e2c878 commit ea760f0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/brain.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ class Brain
# and it returns a promise, otherwise a string is returned
##
processCallTags: (reply, scope, async) ->
reply = reply.replace(/\{__call__\}/ig, "<call>")
reply = reply.replace(/\{\/__call__\}/ig, "</call>")
reply = reply.replace(/«__call__»/ig, "<call>")
reply = reply.replace(/«\/__call__»/ig, "</call>")
callRe = /<call>([\s\S]+?)<\/call>/ig
argsRe = /{__call_arg__}([^{]*){\/__call_arg__}/ig
argsRe = /«__call_arg__»([\s\S]*?\/__call_arg__»/ig

giveup = 0
matches = {}
Expand Down Expand Up @@ -209,7 +209,7 @@ class Brain
return result

_wrapArgumentsInCallTags: (reply) ->
# wrap arguments inside <call></call> in {__call_arg__}{/__call_arg__}
# wrap arguments inside <call></call> in «__call_arg__»«/__call_arg__»
callRegEx = /<call>\s*(.*?)\s*<\/call>/ig
callArgsRegEx = /<call>\s*[^\s]+ (.*)<\/call>/ig

Expand All @@ -234,7 +234,7 @@ class Brain
wrappedArgs = []

for a in args
wrappedArgs.push "{__call_arg__}#{a}{/__call_arg__}"
wrappedArgs.push "«__call_arg__»#{a}«/__call_arg__»"

wrappedCallSignature = wrappedCallSignature.replace(originalArgs,
wrappedArgs.join(' '))
Expand Down Expand Up @@ -437,6 +437,10 @@ class Brain
if matched.redirect?
@say "Redirecting us to #{matched.redirect}"
redirect = @processTags(user, msg, matched.redirect, stars, thatstars, step, scope)

# Execute and resolve *synchronous* <call> tags.
redirect = @processCallTags(redirect, scope, false)

@say "Pretend user said: #{redirect}"
reply = @_getReply(user, redirect, context, step+1, scope)
break
Expand Down Expand Up @@ -860,8 +864,8 @@ class Brain
# Handle all variable-related tags with an iterative regexp approach, to
# allow for nesting of tags in arbitrary ways (think <set a=<get b>>)
# Dummy out the <call> tags first, because we don't handle them right here.
reply = reply.replace(/<call>/ig, "{__call__}")
reply = reply.replace(/<\/call>/ig, "{/__call__}")
reply = reply.replace(/<call>/ig, "«__call__»")
reply = reply.replace(/<\/call>/ig, "«/__call__»")

while true
# This regexp will match a <tag> which contains no other tag inside it,
Expand Down Expand Up @@ -966,6 +970,10 @@ class Brain
break

target = utils.strip match[1]

# Resolve any *synchronous* <call> tags right now before redirecting.
target = @processCallTags(target, scope, false)

@say "Inline redirection to: #{target}"
subreply = @_getReply(user, target, "normal", step+1, scope)
reply = reply.replace(new RegExp("\\{@" + utils.quotemeta(match[1]) + "\\}", "i"), subreply)
Expand Down
4 changes: 2 additions & 2 deletions src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,9 @@ class Parser
if line.match(/[A-Z\\.]/)
return "Triggers can't contain uppercase letters, backslashes or
dots in UTF-8 mode"
else if line.match(/[^a-z0-9(|)\[\]*_#@{}<>=\s]/)
else if line.match(/[^a-z0-9(|)\[\]*_#@{}<>=\/\s]/)
return "Triggers may only contain lowercase letters, numbers, and
these symbols: ( | ) [ ] * _ # { } < > ="
these symbols: ( | ) [ ] * _ # { } < > = /"
else if line.match(/\(\||\|\)/)
return "Piped alternations can't begin or end with a |"
else if line.match(/\([^\)].+\|\|.+\)/)
Expand Down
21 changes: 21 additions & 0 deletions test/test-objects.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,27 @@ exports.test_objects_in_conditions = (test) ->
)
)

exports.test_objects_in_redirects = (test) ->
bot = new TestCase(test, """
> object echo javascript
var message = args.join(" ");
return message;
< object
+ hello bot
- Hello human.
+ redirect to *
@ <call>echo <star></call>
+ inline to *
- "<sentence>": {@ <call>echo <star></call>}
""")
bot.reply("hello bot", "Hello human.")
bot.reply("Redirect to Hello Bot", "Hello human.")
bot.reply("Inline to hello bot", '"Hello bot": Hello human.')
test.done()

exports.test_line_breaks_in_call = (test) ->
bot = new TestCase(test, """
> object macro javascript
Expand Down

0 comments on commit ea760f0

Please sign in to comment.