Working:
exten => _11XX,1,NoOp(Starting verification of the extension status ${EXTEN})
exten => _11XX,n,Set(clidlen=${LEN(${CALLERID(num)})})
exten => _11XX,n,GotoIf($[${clidlen} <= 4]?vrfext:call)
exten => _11XX,n(vrfext),AGI(vrfext.sh,${EXTEN})
exten => _11XX,n(vrfext),GotoIf($["${result}" = "yes"]?busy:call)
exten => _11XX,n(call),Goto(ext-local,${EXTEN},1)
exten => _11XX,n(busy),PlayBack(vm-theperson)
exten => _11XX,n(busy),SayDigits(${EXTEN})
exten => _11XX,n(busy),PlayBack(not available)
exten => _11XX,n(busy),Set(MESSAGE(from)=sip:${EXTEN})
exten => _11XX,n(busy),Set(MESSAGE(body)=Call Me Back ${CALLERID(name)} ${CALLERID(num)})
exten => _11XX,n(busy),MessageSend(sip:${EXTEN},sip:${CALLERID(num)})
exten => _11XX,n(busy),Goto(ext-local,${EXTEN},1)
This logic verifies if the user that is being called has a line if the condition is fulfilled, it indicates to the user who is calling that the person is in a call, but even then the call is send to the user, can be some urgent or of great importance, also send a text to the destination phone indicating that the call is returning.
now in the company where I work with 400 employees know that the other user is on call and avoid letting the phone ring for an indefinite period of time and this does not bother the user who is on call.
the dial plan verifies that the call is only internal if it is external, the call passes even though the user is on the telephone.
Now the only problem is that I have to add a block for each block range of extensions configured in the pbx
this version send the call to the mailbox and verifiy if the user have mailbox
exten => _56XX,n(vrfvm),AGI(vrfvm.sh,${EXTEN})
exten => _56XX,n(vrfvm),GotoIf($["${result}" = "yes"]?onvm:offvm)
exten => _56XX,n(onvm),Set(MESSAGE(from)=sip:${EXTEN})
exten => _56XX,n(onvm),Set(MESSAGE(body)=Call Me Back ${CALLERID(name)} ${CALLERID(num)})
exten => _56XX,n(onvm),MessageSend(sip:${CALLERID(num)},sip:${EXTEN})
exten => _56XX,n(onvm),VoiceMail(${EXTEN})
exten => _56XX,n(offvm),NoOp("No VoiceMail")
exten => _56XX,n(offvm),PlayBack(not available)
exten => _56XX,n(offvm),Set(MESSAGE(from)=sip:${EXTEN})
exten => _56XX,n(offvm),Set(MESSAGE(body)=Call Me Back ${CALLERID(name)} ${CALLERID(num)})
exten => _56XX,n(offvm),MessageSend(sip:${CALLERID(num)},sip:${EXTEN})