Home | Asterisk
Caller ID name lookup AGI that uses NANPA data

It's probably not as cool as a script to wget names from switchboard.com or some other place like that, but it might be more accurate. I imagine there's a name lookup AGI out there some place like that, but I haven't run across it yet. Drop me a line if you know of one: drclaw at yakko[...]. As far as I know, there's not another NANPA lookup AGI out there, but it's not hard to do, or all that useful (granted I'd rather my PSTN provider run something even this generic first before passing me CID info, but I guess PSTN gripes are one of the reason we run Asterisk in the first place.

Update: Recently learned about Asterisk Super CallerID which is probably a lot cooler.
Download AGI: http://yakko.cs.wmich.edu/~drclaw/asterisk/cidname/nanpacid.tar.gz

To set it up, just:
  tar -zxvf nanpacid.tar.gz
  mv ./nanpacid/* /var/lib/asterisk/agi-bin/
  chmod u+x /var/lib/asterisk/agi-bin/nanpacid.agi

Then call the AGI before your Dial() statement in extensions.conf
  exten => s,1,AGI(nanpacid.agi)
  exten => s,2,Dial(IAX2/iaxy)

  The tarball includes a gziped NAPA data file (nanpa.gz) that is out of date, but probably mostly good -- optionally you can go download a more recent collection of NANPA data from: http://www.nanpa.com/reports/reports_cocodes_assign.html.
  Download all the "utilized codes" and unzip them into some directory (e.g. /var/lib/asterisk/nanpa-data/). If you take that route you'll need to update the NANPA_DATA= line below to something like /var/lib/asterisk/nanpa-data/* You'll also want to change DISP=zcat to DISP=cat
#!/bin/bash

#------------------------------------------------------------------#
#                                                                  
# I dislike seeing my VoIP company's name show up on my caller ID  
# for all my incoming calls.  This script is meant to replace      
# that boring data with something slightly useful.                 0
#                                                                  x
# 2005.05.12                                                       1
# drclaw (drclaw@yakko.cs.wmich.edu)                               A
#                                                                  4
# You can update the NANPA data from http://www.nanpa.net          
# at the time I wrote this you could find what you needed at       
# http://www.nanpa.com/reports/reports_cocodes_assign.html         
#                                                                  
#------------------------------------------------------------------#

# [NANPA data source]
# It should be ok to point this to a directory of files if you don't
# want to concatenate all the NANPA files together into one file

NANPA_DATA="/var/lib/asterisk/agi-bin/nanpa.gz"

# Change this next line to cat if you don't have your NANPA file(s) gziped
# (you might want to do that if this runs slow on your box)

DISP=zcat

# Thanks to Sunny Woo for the variable/value pair setting
# and checkresults() function

declare -a array
while read -e ARG && [ "$ARG" ] ; do
	array=(` echo $ARG | sed -e 's/://'`)
	export ${array[0]}=${array[1]}
done

checkresults() {
	while read line
	do
	case ${line:0:4} in
	"200 " ) echo $line >&2
	         return;;
	"510 " ) echo $line >&2
	         return;;	
	"520 " ) echo $line >&2
	         return;;
	*      ) echo $line >&2;;	#keep on reading those Invlid command
					#command syntax until "520 End ..."
	esac
	done
}
# You can find Sunny's complete example at:
# http://lists.digium.com/pipermail/asterisk-users/2003-July/015744.html


NPA=`echo $agi_callerid |cut -b 1-3`
NXX=`echo $agi_callerid |cut -b 4-6`
NPANXX=$NPA"-"$NXX

# [NANPA variables to play with]
# If you aren't going to use some of the variables below you might
# want to comment them out to help the script complete quicker
# if speed is important you probably want to uncompress nanpa.gz
# and make the necessary changes above (that's how I run it personally)

STATE=`$DISP $NANPA_DATA |grep $NPANXX |cut -b 1-3 |sed 's/^[ ^t\x09]*//' |sed 's/[ ^t\x09]*$//'`
OCN=`$DISP $NANPA_DATA |grep $NPANXX |cut -b 13-16 |sed 's/^[ ^t\x09]*//' |sed 's/[ ^t\x09]*$//'`
COMPANY=`$DISP $NANPA_DATA |grep $NPANXX |cut -b 18-78 |sed 's/^[ ^t\x09\x22]*//' |sed 's/[ ^t\x09\x22]*$//'`
RATECENTER=`$DISP $NANPA_DATA |grep $NPANXX |cut -b 79-90 |sed 's/^[ ^t\x09]*//' |sed 's/[ ^t\x09]*$//'`
SWITCH=`$DISP $NANPA_DATA |grep $NPANXX |cut -b 91-113 |sed 's/^[ ^t\x09]*//' |sed 's/[ ^t\x09]*$//'`

# [CallerID Name to Display]
# Change CIDNAME to use the variables you want, here's a couple examples
# CIDNAME="$SWITCH" #show COMMON LANGUAGEŽ Location Identification for CNAM
# CIDNAME="$COMPANY" #show phone company associated with CPN for CNAM
# CIDNAME="OCN: $OCN" #Not much fun, more of an example of using literal text

CIDNAME="$RATECENTER, $STATE"

# Truncate CIDNAME to make your caller ID box happy.  I don't know what the
# actual standard is, but mine does 16 digits for the number 18 characters
# for the name.  If you know the max, or where I could find specs for the
# standard(s) with cidname alphabet, max lengths, etc. please e-mail

CIDNAME=`echo $CIDNAME |cut -b 1-16`

echo "Setting CallerID to: \"$CIDNAME\"<$agi_callerid>" >&2
echo "SET CALLERID \"$CIDNAME\"<$agi_callerid>"
checkresults

exit 0;