Page 308 - Asterisk™: The Future of Telephony
P. 308
So, in the first two lines of our following block of code we are passing the value
status, and the value contained in the ${E} variable (e.g., 1101) to the HOTDESK_INFO
() function. The two values are then replaced in the SQL statement with ${ARG1} and
${ARG2}, respectfully, the SQL statement is executed, and the value returned is assigned
to the ${E}_STATUS channel variable.
OK, let’s finish writing the pattern-match extension now:
exten => _110[1-5],n,Set(${E}_STATUS=${HOTDESK_INFO(status,${E})})
exten => _110[1-5],n,Set(${E}_PIN=${HOTDESK_INFO(pin,${E})})
exten => _110[1-5],n,GotoIf($[${ISNULL(${${E}_STATUS})}]?invalid_user,1)
; check if ${E}_STATUS is NULL
exten => _110[1-5],n,GotoIf($[${${E}_STATUS} = 1]?logout,1:login,1)
After assigning the value of the status column to the ${E}_STATUS variable (if you dialed
extension 1101, then the variable name would be 1101_STATUS), we check if we received
a value back from the database (error checking). We make use of the ISNULL() function
to perform this check.
The last row in the block checks the status of the phone, and if currently logged in, will
log off the agent. If not already logged in, it will go to extension login, priority 1 within
the same context. ‖
In the version following 1.4 (currently trunk) you can use the
${ODBCROWS} channel variable with statements executed by a readsql.
We could have replaced the GotoIf() with something like:
exten => _110[1-5],n,GotoIf($[${ODBCROWS} < 0]?invalid_user,1)
The login extension runs some initial checks to verify the pin code entered by the agent.
We allow him three tries to enter the correct pin, and if invalid, will send the call to the
login_fail extension (which we will be writing later on).
exten => login,1,NoOp() ; set counter initial value
exten => login,n,Set(PIN_TRIES=0) ; set max number of login attempts
exten => login,n,Set(MAX_PIN_TRIES=3)
exten => login,n(get_pin),NoOp() ; increase pin try counter
exten => login,n,Set(PIN_TRIES=$[${PIN_TRIES} + 1])
exten => login,n,Read(PIN_ENTERED|enter-password|${LEN(${${E}_PIN})})
exten => login,n,GotoIf($[${PIN_ENTERED} = ${${E}_PIN}]?valid_login,1)
exten => login,n,Playback(invalid-pin)
exten => login,n,GotoIf($[${PIN_TRIES} <=${MAX_PIN_TRIES}]?get_pin:login_fail,1)
‖ Remember that in a traditional phone system all extensions must be numbers, but in Asterisk, extensions
can have names as well. A possible advantage of using an extension that’s not a number is that it will be much
harder for a user to dial it from her phone and, thus, more secure. We’re going to use several named extensions
in this example. If you want to be absolutely sure that a malicious user cannot access those named extensions,
simply use the trick that the AEL loader uses: start with a priority other than 1.
280 | Chapter 12: Relational Database Integration