text
stringlengths
0
152
same => n,Set(LOCATION=${CUT(CHANNEL,/,2)})
same => n,Set(LOCATION=${CUT(LOCATION,-,1)})
; We'll come back to this shortly ; you can remove this comment/line
same => n(checkset),Set(SET_USED=${HOTDESK_CHECK_SET(${LOCATION})})
same => n,GotoIf($[${SET_USED} > 0]?forcelogout)
; Set status for agent to '1' and update the location/endpoint
same => n(set_login_status),Set(HOTDESK_STATUS(${HotdeskExtension})=1,${LOCATION})
same => n,Noop(ODBCROWS is ${ODBCROWS})
same => n,GotoIf($[${ODBCROWS} < 1]?error,1)
same => n,Playback(agent-loginok)
same => n,Hangup()
same => n(forcelogout),NoOp()
; set all currently logged-in users on this device to logged-out status
same => n,Set(HOTDESK_CLEAR_SET()=${LOCATION})
same => n,Goto(checkset) ; return to logging in
There are some potentially new concepts we’ve just introduced in the examples.
Specifically, the syntax in the HOTDESK_STATUS() function has a few new tricks you
might have noticed. We now have both ${VALx} and ${ARGx} variables in our SQL
statement.
272
|
Chapter 15: Relational Database Integration
We’ve wrapped the ${VALx} and ${ARGx} values in the SQL_ESC()
function as well, which will escape characters such as backticks that
could be used in an SQL injection attack.
These contain the information we pass to the function from the dialplan. In this case,
we have two VAL variables and a single ARG variable that were set from the dialplan via
this statement:
same => n(set_login_status),Set(HOTDESK_STATUS(${HotdeskExtension})=1,${LOCATION})
Notice the syntax is slightly different from that of the read-style function. This signals
to Asterisk that you want to perform a write (this is the same structural syntax as that
used for other dialplan functions).
We are including the value of the ${HotdeskExtension} variable in our call to the
HOTDESK_STATUS() function (which then becomes the ${ARG1} variable for that func‐
tion in func_odbc.conf). However, we are also passing two values, '1' and ${LOCA
TION}. These will be associated in the function by the ${VAL1} and ${VAL2} variables,
respectively.
Using SQL Directly in Your Dialplan
Some people would prefer to write their SQL statements in the dialplan directly, as
opposed to crafting a custom function for each type of database transaction they
might want to perform.
In theory, you could create just one function in func_odbc.conf like this:
[SQL]
prefix=GENERIC
dsn=asterisk
readsql=${SQL_ESC(${ARG1})}
writesql=${SQL_ESC(${VALUE})} ; Whole value, un-parsed
Then, in your dialplan you could write pretty much any sort of SQL you wanted (pro‐
vided the ODBC connector could handle it, which has nothing to do with Asterisk).
That one function above would then submit whatever string you specified directly to
the ODBC connection to your database.6
Some would argue this makes for more confusion in your dialplan; others will insist
that the benefit of having a much simpler func_odbc.conf file is worth it:
same => n,Set(result=${GENERIC_SQL(SELECT col FROM table WHERE ...)})
same => n,Verbose(1,${result})
6 It could also pose a needless security risk.
Getting Funky with func_odbc: Hot-Desking
|
273
same => n,Set(GENERIC_SQL()=UPDATE table SET field="VAL" WHERE ...)
same => n,Verbose(1,ODBC_RESULT is ${OBDBC_RESULT})
We believe it’s generally better to build specific functions in func_odbc.conf to handle
queries from your dialplan; however, there’s no denying the temptation to use one
function to handle all SQL queries.
Multirow Functionality with func_odbc
Asterisk has a multirow mode that allows it to handle multiple rows of data returned
from the database. For example, if we were to create a dialplan function in
func_odbc.conf that returned all available extensions, we would need to enable multi‐
row mode for the function. This would cause the function to work a little differently,
returning an ID number that could then be passed to the ODBC_FETCH() function to
return each row in turn.
A simple example follows. Suppose we have the following func_odbc.conf:
[AVAILABLE_EXTENS]
prefix=HOTDESK