Jacken’s Blog

Adding blacklist to an Asterisk@Home PBX Server

September 26th, 2005 · 14 Comments

Update:The code below does not work in Asterisk 2.0b4 or newer. I will update with instructions on how to get it to work in those versions. Thanks to Dan for pointing this out.

Update:I have changed the code for the remove part. It now requires a key press to accept the number to delete.

I have added a black list function to my Asterisk@Home server and thought that I’ll share it here if someone is interested. To add a number to the blacklist there is two ways of doing it. The easiest is to dial *32. Then you get a voice telling you the number of the last incoming call. To add it to the blacklist, just press 1. This is great for pesky telemarketers. If you want to add it manually, just press *30. Then you can enter the phone number directly on your telephone. To remove a number from the blacklist, press *31 and enter the phone number. If you don’t like the codes I use, just change them.

Here’s how you do it.

Add the code below at the end of extensionscustom.conf. You can edit it from the web interface, just go under Maintenance and Config Edit and select extensionscustom.conf. You have to add it to the custom file, because AMP changes the _additional.conf file every time you update something.


[custom-blacklist-add]
exten => s,1,Playback(enter-num-blacklist)
;exten => s,2,DigitTimeout(5)
exten => s,2,ResponseTimeout(60)
exten => s,3,Read(blacknr,then-press-pound)
exten => s,4,SayDigits(${blacknr})
exten => s,5,Playback(if-correct-press)
exten => s,6,Playback(digits/1)
exten => 1,1,DBput(blacklist/${blacknr}=1)
exten => 1,2,Playback(num-was-successfully)
exten => 1,3,Playback(added)
exten => 1,4,Wait(1)
exten => 1,5,Hangup
exten=_X.,3,Playback(num-was-successfully)
exten=_X.,4,Playback(added)
exten=_X.,5,Hangup

[custom-blacklist-remove]
exten => s,1,Playback(entr-num-rmv-blklist)
exten => s,2,ResponseTimeout(60)
exten => s,3,Read(blacknr,then-press-pound)
exten => s,4,SayDigits(${blacknr})
exten => s,5,Playback(if-correct-press)
exten => s,6,Playback(digits/1)
exten => 1,1,DBdel(blacklist/${blacknr})
exten => 1,2,SayDigits($blacklist)
exten => 1,3,Playback(num-was-successfully)
exten => 1,4,Playback(removed)
exten => 1,5,Hangup

[custom-blacklisted]
exten=s,1,Answer
exten=s,2,Wait(1)
exten=s,3,Playback(nbdy-avail-to-take-call)
exten=s,4,Playback(carried-away-by-monkeys)
exten=s,5,Playback(lots-o-monkeys)
exten=s,6,Hangup

[custom-blacklist-last]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,DBget(number=CALLTRACE/${CALLERIDNUM}) ; goto 104 if no lastcaller
exten => s,4,GotoIf($"${number}" = ""?104,1) ; also if it's blank (caller id blocked)
exten => s,5,Playback(privacy-to-blacklist-last-caller)
exten => s,6,Playback(telephone-number) 
exten => s,7,SayDigits(${number}) 
exten => s,8,Wait,1 
exten => s,9,Playback(if-correct-press)
exten => s,10,Playback(digits/1) 
exten => 1,1,DBput(blacklist/${number}=1)
exten => 1,2,Playback(privacy-blacklisted)
exten => 1,3,Wait,1
exten => 1,4,Hangup
exten => s,104,Playback(unidentified-no-callback) 
exten => s,106,Hangup 

You also need to add a custom function to the from-pstn context. This gets called before the normal incoming call handler. So it checks if the incoming number is in the blacklist. If so, give weird message (or maybe just congestion?)

Add the following at the beginning of extensionscustom.conf. Make sure there’s not an declaration for that context already. If so, just add the lines there.


[from-pstn-custom]
exten => s,1,LookupBlacklist ; If CID blacklisted, goto 102
exten => s,102,Goto(custom-blacklisted,s,1)

And then the last change. Add the following lines in extensions_custom.conf in the [from-internal-custom] context. There’s already some lines there, but just add the following after that.


exten => *30,1,Goto(custom-blacklist-add,s,1)
exten => *31,1,Goto(custom-blacklist-remove,s,1)
exten => *32,1,Goto(custom-blacklist-last,s,1)

You’ll probably like to change the [custom-blacklisted] to do something like congestion or hang up instead of the weird message my version does.

Tags: Asterisk PBX · Technical stuff

14 responses so far ↓

  • 1 dstroot // Oct 18, 2005 at 1:33

    Has anyone pointed out the following three lines are repeated at the bottom of the [custom-blacklist-add] code? Is this a typo?

    exten=X.,3,Playback(num-was-successfully) exten=X.,4,Playback(added) exten=_X.,5,Hangup

    Also in [from-pstn-custom] - where does the LookupBlacklist function come from? Is this a script or function already on the box somewhere? exten => s,1,LookupBlacklist ; If CID blacklisted, goto 102

  • 2 Jacken // Oct 18, 2005 at 1:40

    Yes, you are correct, it’s a typ0. Have removed them now. Thanks!

    The LookupBlacklist is a built in function.

  • 3 dstroot // Oct 21, 2005 at 23:28

    FYI - The “LookupBlacklist” function is not in Asterisk@Home 2.0 Beta 4 - I’m working on a way to add it

  • 4 Kevan // Oct 27, 2005 at 19:16

    When using DID routes in AMP or AAH to send calls directly to an extension bypassing the Incoming call settings the [from-pstn-custom] statements don’t work. If you change the statements to the following it will allow to work either way:

    [from-pstn-custom] exten => _NXXNXXXXXX,1,LookupBlacklist ; If CID blacklisted, goto 102 exten => _NXXNXXXXXX,102,Goto(custom-blacklisted,s,1) exten => _1NXXNXXXXXX,1,LookupBlacklist ; If CID blacklisted, goto 102 exten => _1NXXNXXXXXX,102,Goto(custom-blacklisted,s,1)

  • 5 Jacken // Nov 4, 2005 at 22:13

    I’m guessing that there is a way to call the blacklist function before an incoming DID call. I’ll try to find a way later on.

  • 6 Stefan // Nov 14, 2005 at 3:33

    Thanks for the information. However it would be nice to use your soundfiles used in your example. I’m suspecting one of them contains the sound of a bunch of screaming chimps… :-) Can you post them or a link?

    Regards

    //Stefan

  • 7 Jacken // Nov 14, 2005 at 12:49

    All the files are already included with Asterisk@Home. So the examples should work directly.

  • 8 Daniel Nylander // Nov 23, 2005 at 21:07

    Sweeet :-)

  • 9 gbutters // Dec 11, 2005 at 4:50

    For AAH 2.0 I had to change [custom-blacklist-last]

    exten => s,4,GotoIf($”${number}” = “”?104,1) ; also if it’s blank (caller id blocked) to exten => s,4,GotoIf($[${number} = ""]?104,1) ; also if it’s blank (caller id blocked) to get it to work. Before I made the change it just gave me a busy signal.

  • 10 Anonymous // Jan 12, 2006 at 17:08

    Okay all, we have been seeing a problem with the “lookupblacklist” command in Asterisk @home v2.2. After banging my head against the wall trying to work with the command I gave up and wrote my own script to handle the function. I have implemented this on my *@home server (2.2) and it works flawlessly.

    First let me explain a little about my setup so you can understand why I did it this way. I have 4 DID’s on an IAX pipe from my provider. I do not use POTS or SIP trunks on my * server. I have 2 local and 2 toll free DID’s. One of each are public which go stright to an auto attendant so I don’t care who calls those numbers. The other 2 are private and are for family/friends only. These 2 DID’s actually ring through to the phones, so I want to control who can ring through on these DID’s. I wanted my blacklist lookup to apply ONLY to these 2 DID’s.

    I applied the code to the “extensions_additional.conf” file so I could control which DID’s the code applied to. The code needs to go in the [ext-did] context, appeneded before the already populated code for each DID you want this to apply to.

    [ext-did] include => ext-did-custom ;private number exten => NPANXXXXXX,1,SetGlobalVar(blkstat=9) ; added by Joel exten => NPANXXXXXX,2,DBget(blkstat=blacklist/${CALLERIDNUM}) ; added by Joel exten => NPANXXXXXX,3,GotoIf($["${blkstat}" = "1"]?custom-blacklisted,s,1) ; added by Joel exten => NPANXXXXXX,4,SetVar(FROMDID=NPANXXXXXX) ; from AMP exten => NPANXXXXXX,5,SetVar(FAXRX=disabled) ; from AMP exten => NPANXXXXXX,6,Goto(ext-local,221,1) ; from AMP

    First in line 1, we declare a variable. I did not want to use the pre-defined system variables. I set the initial value to “9″ as this value is not used in the blacklist database so there should be no conflict.

    Next in line 2, we check the database for the “blacklist” family looking to see if the current caller is in the database. If the number exists a value of 1 will be set into the “blkstat” variable.

    Last in line 3, we check the value of the “blkstat” variable. If it is “1″ (the number has been blacklisted) then the caller is sent off to the [custom-blacklisted] script. If not, then the caller gets passed through to the remaining script as gerenated by AMP.

    In hindsight this MAY have worked if put into the [ext-did-custom] context, but I just couldn’t be bothered to mess with it once I had it working.

  • 11 Joel // Jan 12, 2006 at 19:29

    An update to my last post on the blacklist lookup.

    Although it did work flawlessly I realized after posting that if you make any changes in AMP it will over-write the “extensions-additional.conf” file, delelting the custom code.

    Here is how to correct my previous mistake.

    For whichever DID you want to check the blacklist, set the inbound routing in AMP for the DID to go to a custom application: I used “custom-check-private,s,1″ in the field for the app name.

    Then I added the following to the “extensions-custom.conf” file at the bottom

    [custom-check-private] exten => s,1,SetGlobalVar(blkstat=9) ; added by Joel exten => s,2,DBGet(blkstat=blacklist/${CALLERIDNUM}) ; added by Joel exten => s,3,GotoIf($["${blkstat}"="1"]?custom-blacklisted,s,1) ; added by Joel exten => s,4,Goto(ext-local,221,1)

    You will need to manually change line 4 to Goto whatever function you previously has the inbound route goto. Mine was extension 221.

  • 12 CHRISTOPHER // Feb 24, 2006 at 0:00

    Note: On asterisk 1.2+ you must add (j) to the end, in order for it to actually make the priority jump. Like such:

    exten => s,1,LookupBlacklist(j)

  • 13 ever zalazar // Jul 14, 2006 at 5:44

    HI, I need to know about extensions-additional.conf

    Lets say that i need to configure the language for each user when go to read their message. The menu of voicemail has to be in the language that the user choose. That users are not in sip.conf, they come directly from a S.E.R.server, and check the email. HOw can I configure them the language? thanks

  • 14 ryanczak // Apr 14, 2007 at 22:25

    I had to update this a bit to get it to work with Asterisk 1.4.2 (I don’t use Asterisk@home but it should work anyways).

    First I had to replace calltrace as it does not seem to be available in asterisk 1.4.2. I did this by adding the following to the inbound call context:

    Set(DB(lastcallerid/number)=${CALLERID(num)})

    My inbound context looks like this: exten => s,1,LookupBlacklist(j) ; If CID blacklisted, goto 102 exten => s,2,Set(DB(lastcallerid/number)=${CALLERID(num)}) exten => s,3,Dial(SIP/100,30,tr) exten => s,4,Voicemail(u100) exten => s,5,Hangup exten => s,102,Goto(custom-blacklisted,s,1)

    The rest is pretty straight forward. I added autofallthrough=no to the [general] context because yes is now the default value. It needs to be no for this to work properly. I replaced the DBput and DBget calls with the appropriate Set syntax (DBput and DBget have been depreciated). I changed the gotoif in [custom-blacklist-last] because it would not work as written above. Lastly I changed the ResponseTimeout(60) to Set(TIMEOUT(response)=60). Here are the changed blacklist contexts:

    [custom-blacklist-add] exten => s,1,Playback(enter-num-blacklist) exten => s,2,Set(TIMEOUT(response)=60) exten => s,3,Read(blacknr,then-press-pound) exten => s,4,SayDigits(${blacknr}) exten => s,5,Playback(if-correct-press) exten => s,6,Playback(digits/1) ;exten => 1,1,DBput(blacklist/${blacknr}=1) exten => 1,1,Set(DB(blacklist/${blacknr})=1) exten => 1,2,Playback(num-was-successfully) exten => 1,3,Playback(added) exten => 1,4,Wait(1) exten => 1,5,Hangup exten=X.,3,Playback(num-was-successfully) exten=X.,4,Playback(added) exten=_X.,5,Hangup

    [custom-blacklist-remove] exten => s,1,Playback(entr-num-rmv-blklist) exten => s,2,Set(TIMEOUT(response)=60) exten => s,3,Read(blacknr,then-press-pound) exten => s,4,SayDigits(${blacknr}) exten => s,5,Playback(if-correct-press) exten => s,6,Playback(digits/1) exten => 1,1,DBdel(blacklist/${blacknr}) exten => 1,2,SayDigits($blacklist) exten => 1,3,Playback(num-was-successfully) exten => 1,4,Playback(removed) exten => 1,5,Hangup

    [custom-blacklisted] exten=s,1,Answer exten=s,2,Wait(1) exten=s,3,Playback(nbdy-avail-to-take-call) exten=s,4,Playback(carried-away-by-monkeys) exten=s,5,Playback(lots-o-monkeys) exten=s,6,Hangup

    [custom-blacklist-last] exten => s,1,Answer exten => s,2,Wait(1) exten => s,3,Set(number=${DB(lastcallerid/number)}) exten => s,4,GotoIf($["${number}" = ""]?104) ; also if it’s blank (caller id blocked) exten => s,5,Playback(privacy-to-blacklist-last-caller) exten => s,6,Playback(telephone-number) exten => s,7,SayDigits(${number}) exten => s,8,Playback(press) exten => s,9,Playback(digits/1) exten => 1,1,Set(DB(blacklist/${number})=1) exten => 1,2,Playback(privacy-blacklisted) exten => 1,3,Wait,1 exten => 1,4,Hangup exten => s,104,Playback(unidentified-no-callback) exten => s,106,Hangup

You must log in to post a comment.