Return Forbid if number not in whitelist in MYSQL

Hi, I’ve a whitelist in MYSQL with numbers that are allowed to connect in our IVR, because it’s a dynamic list and very long, I want to return Forbidden for every other number that is not in the MYSQL table.

How can I do this?

I 've found this:

exten => _X.,1,MYSQL(Connect connid localhost dbuser dbpass dbname)
exten => _X.,2,MYSQL(Query resultid ${connid} SELECT\ scriptname\ from\ mac2pin\ where\ userid=${CALLERIDNAME})
exten => _X.,3,MYSQL(Fetch fetchid ${resultid} AGIScript)
exten => _X.,4,GotoIf($[${AGIScript} = NULL]?5:7)
exten => _X.,5,AGI(${DefaultAGIScript},${EXTEN})
exten => _X.,6,Goto(_X.,8)
exten => _X.,7,AGI(${AGIScript},${EXTEN})
exten => _X.,8,MYSQL(Clear ${resultid})
exten => _X.,9,MYSQL(Disconnect ${connid})
exten => _X.,10,Hangup

But I’ve no idea how to adapt it to return Forbid if the result from the query is null.

Thanks!

Use Hangup() with the cause you wish to send.

https://wiki.asterisk.org/wiki/display/AST/Hangup+Cause+Mappings