text
stringlengths
0
152
extension.
270
|
Chapter 15: Relational Database Integration
At the login extension the dialplan runs some initial checks to verify the PIN code
entered by the agent. (Additionally, we’ve used the FILTER() function to make sure
only numbers were entered to help avoid some SQL injection issues.) We allow them
three tries to enter the correct PIN, and if all tries are invalid we’ll hang up:
exten => login,1,NoOp() ; set initial counter values
same => n,Set(PIN_TRIES=1) ; pin tries counter
same => n,Set(MAX_PIN_TRIES=3) ; set max number of login attempts
same => n,Playback(silence/1) ; play back some silence so first prompt is
; not cut off
same => n(get_pin),NoOp()
same => n,Set(PIN_TRIES=$[${PIN_TRIES} + 1]) ; increase pin try counter
same => n,Read(PIN_ENTERED,enter-password,${LEN(${${HotdeskExtension}_PIN})})
same => n,Set(PIN_ENTERED=${FILTER(0-9,${PIN_ENTERED})})
same => n,GotoIf($["${PIN_ENTERED}" = "${${HotdeskExtension}_PIN}"]?valid:invalid)
same => n,Hangup()
same => n(invalid),Playback(vm-invalidpassword)
same => n,GotoIf($[${PIN_TRIES} <= ${MAX_PIN_TRIES}]?get_pin)
same => n,Playback(goodbye)
same => n,Hangup()
same => n(valid),Noop(Valid PIN)
If the PIN entered matches, we continue with the login process through the (valid)
label. First we utilize the CHANNEL variable to figure out which phone device the agent
is calling from. The CHANNEL variable is usually populated with something similar to
PJSIP/HOTDESK_1-ab4034c, so we make use of the CUT() function to first strip off the
PJSIP/ component of the string. We then strip off the -ab4034c part of the string,
and what remains is what we want (HOTDESK_1):5
same => n(valid),Noop(Valid PIN)
; CUT off the channel technology and assign it to the LOCATION variable
same => n,Set(LOCATION=${CUT(CHANNEL,/,2)})
; CUT off the unique identifier and save the remainder to the LOCATION variable
same => n,Set(LOCATION=${CUT(LOCATION,-,1)})
; we'll come back to this shortly
We’re going to create and use some more functions in the func_odbc.conf file: HOT
DESK_CHECK_SET(), which will determine if other users are already assigned to this
phone; HOTDESK_STATUS(), which will assign the phone to this agent; and HOT
DESK_CLEAR_SET(), which will clear any other users currently assigned to this phone
(who perhaps forgot to log out).
In our func_odbc.conf file we’ll need to create the following functions:
; func_odbc.conf
[CHECK_SET]
prefix=HOTDESK
5 Yes, you can nest functions within functions, and so do this all on one line. We didn’t do so as it’s more diffi‐
cult to debug, and doesn’t affect performance.
Getting Funky with func_odbc: Hot-Desking
|
271
dsn=asterisk
synopsis=Check if this set is already assigned to somebody.
readsql=SELECT COUNT(status) FROM pbx.ast_hotdesk WHERE status = '1'
readsql+= AND endpoint = '${ARG1}'
[STATUS]
prefix=HOTDESK
dsn=asterisk
synopsis=Assign hotdesk extension to this endpoint/set.
writesql=UPDATE pbx.ast_hotdesk SET status = '${SQL_ESC(${VAL1})}',
writesql+= endpoint = '${SQL_ESC(${VAL2})}'
writesql+= WHERE extension = '${SQL_ESC(${ARG1})}'
[CLEAR_SET]
prefix=HOTDESK
dsn=asterisk
synopsis=Clear all instances of this endpoint
writesql= UPDATE pbx.ast_hotdesk SET status=0,endpoint=NULL
writesql+= WHERE endpoint='${SQL_ESC(${VAL1})}'
Due to line-length limitations in the book, we’ve broken the
readsql and writesql commands into multiple lines using the +=
syntax, which tells Asterisk to append the contents after readsql+=
to the most recently defined readsql= value (or writesql and
writesql+). The usage of += is applicable not only to the readsql
option, but can be used in other places in other .conf files within
Asterisk.
In our dialplan, we’ll need to call the function we just created, and pass call flow to
the forcelogout label if somebody is already logged into this set:
same => n(valid),Noop(Valid PIN)