There is a screening mode available in Asterisk.
Look at Dial parameter “p” or “P”.
That’s basically all you have to set 
You then have to look at the screening status in dial plan:
If you get DIALSTATUS value “DONTCALL” you might want to send the caller to a mailbox or play an announcement.
Example:
exten => yourexten,1,Dial(SIP/1234,30,rP)
same => n,ExecIf($["${DIALSTATUS}" = "TORTURE"]?Playback(music/rickroll.mp3)) ;; Called party wants to torture the caller
same => n,ExecIf($["${DIALSTATUS}" = "DONTCALL"]?Voicemail(yourexten,su)) ;; Called party was reached but told us to not accept this call
same => n,Voicemail(yourexten,su)
same => n,Hangup(${HANGUPCAUSE})
As a note: If you use the uppercase P, you should make sure that anonymous calls with no callerid available have this information correctly set, since Asterisk might remember anonymous callers wrong.
In most cases, these information are set correctly, but if you alter incoming callerids, you should test this case.