README.variables 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. EXTENSION LOGIC :
  2. There are two levels of parameter evaluation done in asterisk in
  3. extensions.conf.
  4. The first, and most frequently used, is the substitution of variable
  5. references with their values.
  6. Then there are the evaluations done in $[ .. ]. This will be
  7. discussed below.
  8. ___________________________
  9. PARAMETER QUOTING:
  10. ---------------------------
  11. exten => s,5,BackGround,blabla
  12. The parameter (blabla) can be quoted ("blabla"). In this case, a
  13. comma does not terminate the field. However, the double quotes
  14. will be passed down to the Background command, in this example.
  15. Also, characters special to variable substitution, expression evaluation, etc
  16. (see below), can be quoted. For example, to literally use a $ on the
  17. string "$1231", quote it with a preceding \. Special characters that must
  18. be quoted to be used, are [ ] $ " \. (to write \ itself, use \\).
  19. These Double quotes and escapes are evaluated at the level of the
  20. asterisk config file parser.
  21. Double quotes can also be used inside expressions, as discussed below.
  22. ___________________________
  23. VARIABLES:
  24. ---------------------------
  25. Parameter strings can include variables. Variable names are arbitrary strings.
  26. They are stored in the respective channel structure.
  27. To set a variable to a particular value, do :
  28. ;exten => 1,2,SetVar,varname=value
  29. You can substitute the value of a variable everywhere using ${variablename}.
  30. For example, to stringwise append $lala to $blabla and store result in $koko,
  31. do:
  32. ;exten => 1,2,SetVar,koko=${blabla}${lala}
  33. There are also the following special variables:
  34. ${ACCOUNTCODE} Account code (if specified)
  35. ${CALLERID} Caller ID
  36. ${CALLERIDNAME} Caller ID Name only
  37. ${CALLERIDNUM} Caller ID Number only
  38. ${CALLINGPRES} PRI Caller ID presentation for incoming calls
  39. ${CHANNEL} Current channel name
  40. ${CONTEXT} Current context
  41. ${DATETIME} Current date time in the format: DDMMYYYY-HH:MM:SS
  42. ${DNID} Dialed Number Identifier
  43. ${ENUM} Result of application EnumLookup
  44. ${EPOCH} Current unix style epoch
  45. ${EXTEN} Current extension
  46. ${ENV(VAR)} Environmental variable VAR
  47. ${HANGUPCAUSE} Asterisk hangup cause
  48. ${INVALID_EXTEN}The invalid called extension (used in the "i" extension)
  49. ${LANGUAGE} Current language
  50. ${LEN(VAR)} String length of VAR (integer)
  51. ${MEETMESECS} Number of seconds a user participated in a MeetMe conference
  52. ${PRIORITY} Current priority
  53. ${RDNIS} Redirected Dial Number ID Service
  54. ${SIPCALLID} SIP Call-ID: header verbatim (for logging or CDR matching)
  55. ${SIPDOMAIN} SIP destination domain of an inbound call (if appropriate)
  56. ${SIPUSERAGENT} SIP user agent
  57. ${TIMESTAMP} Current date time in the format: YYYYMMDD-HHMMSS
  58. ${TXTCIDNAME} Result of application TXTCIDName
  59. ${UNIQUEID} Current call unique identifier
  60. NOTE: Attempting to set any of these "special" variables will not change
  61. their value, but will not display any warning either.
  62. The dial() application sets the following variables:
  63. ${DIALEDPEERNAME} Dialed peer name
  64. ${DIALEDPEERNUMBER} Dialed peer number
  65. ${DIALEDTIME} Total time for the call in seconds (Network time).
  66. ${ANSWEREDTIME} Time from answer to end of call in seconds (Billable time).
  67. ${DIALSTATUS} Status of the call, one of:
  68. CHANUNAVAIL | CONGESTION | BUSY | NOANSWER | ANSWER | CANCEL
  69. There are two reference modes - reference by value and reference by name.
  70. To refer to a variable with its name (as an argument to a function that
  71. requires a variable), just write the name. To refer to the variable's value,
  72. enclose it inside ${}. For example, SetVar takes as the first argument
  73. (before the =) a variable name, so:
  74. ;exten => 1,2,SetVar,koko=lala
  75. ;exten => 1,3,SetVar,${koko}=blabla
  76. stores to the variable "koko" the value "lala" and to variable "lala" the
  77. value "blabla".
  78. In fact, everything contained ${here} is just replaced with the value of
  79. the variable "here".
  80. ___________________________
  81. EXPRESSIONS:
  82. ---------------------------
  83. Everything contained inside a bracket pair prefixed by a $ (like $[this]) is
  84. considered as an expression and it is evaluated. Evaluation works similar to
  85. (but is done on a later stage than) variable substitution: the expression
  86. (including the square brackets) is replaced by the result of the expression
  87. evaluation. The arguments and operands of the expression MUST BE separated
  88. by at least one space.
  89. For example, after the sequence:
  90. exten => 1,1,SetVar,"lala=$[1 + 2]";
  91. exten => 1,2,SetVar,"koko=$[2 * ${lala}]";
  92. the value of variable koko is "6".
  93. And, further:
  94. exten => 1,1,SetVar,"lala=$[1+2]";
  95. will not work as you might have expected. Since all the chars in the single
  96. token "1+2" are not numbers, it will be evaluated as the string "1+2". Again,
  97. please do not forget, that this is a very simple parsing engine, and it
  98. uses a space (at least one), to separate "tokens".
  99. and, further:
  100. exten => 1,1,SetVar,"lala=$[ 1 + 2 ]";
  101. will parse as intended. Extra spaces are ignored.
  102. ___________________________
  103. SPACES INSIDE VARIABLE
  104. ---------------------------
  105. If the variable being evaluated contains spaces, there can be problems.
  106. For these cases, double quotes around text that may contain spaces
  107. will force the surrounded text to be evaluated as a single token.
  108. The double quotes will be counted as part of that lexical token.
  109. As an example:
  110. exten => s,6,GotoIf($[ "${CALLERIDNAME}" : "Privacy Manager" ]?callerid-liar|s|1:s|7)
  111. The variable CALLERIDNAME could evaluate to "DELOREAN MOTORS" (with a space)
  112. but the above will evaluate to:
  113. "DELOREAN MOTORS" : "Privacy Manager"
  114. and will evaluate to 0.
  115. The above without double quotes would have evaluated to:
  116. DELOREAN MOTORS : Privacy Manager
  117. and will result in syntax errors, because token DELOREAN is immediately
  118. followed by token MOTORS and the expression parser will not know how to
  119. evaluate this expression.
  120. _____________________
  121. OPERATORS
  122. ---------------------
  123. Operators are listed below in order of increasing precedence. Operators
  124. with equal precedence are grouped within { } symbols.
  125. expr1 | expr2
  126. Return the evaluation of expr1 if it is neither an empty string
  127. nor zero; otherwise, returns the evaluation of expr2.
  128. expr1 & expr2
  129. Return the evaluation of expr1 if neither expression evaluates to
  130. an empty string or zero; otherwise, returns zero.
  131. expr1 {=, >, >=, <, <=, !=} expr2
  132. Return the results of integer comparison if both arguments are
  133. integers; otherwise, returns the results of string comparison
  134. using the locale-specific collation sequence. The result of each
  135. comparison is 1 if the specified relation is true, or 0 if the
  136. relation is false.
  137. expr1 {+, -} expr2
  138. Return the results of addition or subtraction of integer-valued
  139. arguments.
  140. expr1 {*, /, %} expr2
  141. Return the results of multiplication, integer division, or
  142. remainder of integer-valued arguments.
  143. expr1 : expr2
  144. The `:' operator matches expr1 against expr2, which must be a
  145. regular expression. The regular expression is anchored to the
  146. beginning of the string with an implicit `^'.
  147. If the match succeeds and the pattern contains at least one regu-
  148. lar expression subexpression `\(...\)', the string correspond-
  149. ing to `\1' is returned; otherwise the matching operator
  150. returns the number of characters matched. If the match fails and
  151. the pattern contains a regular expression subexpression the null
  152. string is returned; otherwise 0.
  153. Parentheses are used for grouping in the usual manner.
  154. The parser must be parsed with bison (bison is REQUIRED - yacc cannot
  155. produce pure parsers, which are reentrant)
  156. ___________________________
  157. CONDITIONALS
  158. ---------------------------
  159. There is one conditional operator - the conditional goto :
  160. ;exten => 1,2,gotoif,condition?label1:label2
  161. If condition is true go to label1, else go to label2. Labels are interpreted
  162. exactly as in the normal goto command.
  163. "condition" is just a string. If the string is empty or "0", the condition
  164. is considered to be false, if it's anything else, the condition is true.
  165. This is designed to be used together with the expression syntax described
  166. above, eg :
  167. exten => 1,2,gotoif,$[${CALLERID} = 123456]?2|1:3|1
  168. Example of use :
  169. exten => s,2,SetVar,"vara=1"
  170. exten => s,3,SetVar,"varb=$[${vara} + 2]"
  171. exten => s,4,SetVar,"varc=$[${varb} * 2]"
  172. exten => s,5,GotoIf,"$[${varc} = 6]?99|1:s|6";
  173. ___________________________
  174. PARSE ERRORS
  175. ---------------------------
  176. Syntax errors are now output with 3 lines.
  177. If the extensions.conf file contains a line like:
  178. exten => s,6,GotoIf($[ "${CALLERIDNUM}" = "3071234567" & "${CALLERIDNAME}" : "Privacy Manager" ]?callerid-liar|s|1:s|7)
  179. You may see an error in /var/log/asterisk/messages like this:
  180. May 3 15:58:53 WARNING[1234455344]: ast_yyerror(): syntax error: parse error; Input:
  181. "3072312154" : "3071234567" & & "Steves Extension" : "Privacy Manager"
  182. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  183. ^
  184. The first line shows the string passed to the expression parser. This
  185. string is the result of the variable replacements, etc. This way, you
  186. can see the actual string that went into the parser.
  187. The second line usually shows a string of '^' chars, that show what's
  188. been legally parsed so far.
  189. And the third line shows where the parser was (lookahead token lexing,
  190. etc), when the parse hit the rocks. A single '^' here. The error is
  191. going to be somewhere between the last '^' on the second line, and the
  192. '^' on the third line. That's right, in the example above, there are two
  193. '&' chars, separated by a space, and this is a definite no-no!
  194. ___________________________
  195. NULL STRINGS
  196. ---------------------------
  197. Testing to see if a string is null can be done in one of two different ways:
  198. exten => _XX.,1,GotoIf($["${calledid}" != ""]?3)
  199. exten => _XX.,1,GotoIf($[foo${calledid} != foo]?3)
  200. The second example above is the way suggested by the WIKI. It will
  201. work as long as there are no spaces in the evaluated value.
  202. The first way should work in all cases, and indeed, might now
  203. be the safest way to handle this situation.
  204. ___________________________
  205. WARNING
  206. ---------------------------
  207. If you need to do complicated things with strings, asterisk expressions
  208. is most likely NOT the best way to go about it. AGI scripts are an
  209. excellent option to this need, and make available the full power of
  210. whatever language you desire, be it Perl, C, C++, Cobol, RPG, Java,
  211. Snobol, PL/I, Scheme, Common Lisp, Shell scripts, Tcl, Forth, Modula,
  212. Pascal, APL, assembler, etc.