AGI Variable Value Syntax

I just did an update to my Seaskirt Python API wrapper to correctly handle quoting and unquoting of variable values when setting/getting them via AGI.

The details of how to do the escaping are sketched out rather briefly at voip-info.org here.

I also had a look at the VERBOSE AGI command. Wouldn’t you know it, the message argument for this follows a slightly different syntax (no special meaning for quotation marks).

So I have added a method call that knows how to quote the message for this command.

Just to be pedantic…

Do you quote the variable name to allow spaces within the name?

I’ve never found a use case, but Asterisk does allow it.

Some may call it “pedantic”, I call it “completeness”. If Asterisk allows it in the API, I should support it in my API wrapper.

Yes, it seems to allow spaces in variable names in the dial plan, e.g.

exten => s,n,Set("multi word"= these \"are( \\some\" words )

which elicits the warning

Please avoid unnecessary spaces on variables as it may lead to unexpected results (‘“multi word”’ set to ’ these "are( \some" words ').”

So far so good? But how do I access that variable in AGI? I tried

GET VARIABLE multi\ word

and

GET VARIABLE "multi word"

but neither one returns a value, just “200 result=0” (i.e. indicating no such variable is currently defined.)

Maybe the assignment didn’t work? The next line in the dialplan was

exten => s,n,Verbose("multi word = ${multi word}")

but the console just showed

– Executing [s@agi-syntax-test:3] Verbose(“Local/s@agi-syntax-test-0000003b;2”, ""multi word = “”) in new stack
multi word =

In other words, the variable value is blank. Did I do it wrong? Or is Asterisk not handling it properly?

I figured it out! Seems either quotes around the variable name or backslash escapes on special characters will work, same as with the variable value. The following sequence in my test dialplan

exten => s,n,Set(multi word= these \"are( \\some\" words )
exten => s,n,Verbose("multi word = ${multi word}")
exten => s,n,AGI(/usr/local/asterisk/agi-bin/agi_syntax_test,"multi word","re zult",${multi word})
exten => s,n,Verbose("re zult = ${re zult}")

tells my test AGI to retrieve the value of the variable “multi word”, do some fiddling on it, and set the result as the value of the variable “re zult”, which is then displayed on the console.

While I was at it, I corrected the handling of multiline responses (e.g. the explanation of how to do it right if Asterisk sees a syntax error in your request). It helped that I was able to trigger such errors on demand. :wink:

OK, so far I have figured out I can use a common quoting convention for variable names and values, and also for things like the VERBOSE message argument.

Next I was looking at EXEC: from AGI, the syntax is

EXEC appname arguments

where arguments is a comma-separated list of arguments to the application. What happens if an argument contains a space? No problem, I can use quotes or backslashes to escape these. But what if there’s a comma in the argument value?

Turns out this is not allowed. I have tried quotes and backslashes, but either way, the comma always seems to terminate the argument value.