#!/usr/bin/perl -w # # Angelo Conforti based on # http://www.teamforrest.com/blog/voip/89/using-agi-to-get-caller-id-name-cnam/ # http://km.krot.org/code/gc2fab.pl # # Install instruction on Debian: # apt-get install libwww-perl libcrypt-ssleay-perl # # Version 0.1 -> first version # Version 0.2 -> accorciamento del numero di telefono # Version 0.3 -> attinge al database Google Contacts su /var/lib/asterisk/gcontacts.txt # creato con gcontacts.pl --username --password > # use LWP::UserAgent; $|=1; my ($cidnum,$cidname,$name,$accorcia,$tmpnum,$gcontacts); # Se deve o non deve fare ricerche accorciando # fino a $accorcia cifre il callerid (aggiungendo anche un "1" in fondo) $accorcia=3; $gcontacts='/var/lib/asterisk/gcontacts.txt'; #---------------------------------------------------------------- # get asterisk initial info #---------------------------------------------------------------- while() { chomp; last unless length($_); } #---------------------------------------------------------------- # check if we have a caller id #---------------------------------------------------------------- if ($ARGV[0]) { $cidnum = $ARGV[0]; } else { print qq(VERBOSE "ERROR: no callerid provided" 2\n); exit(0); } #---------------------------------------------------------------- # check caller id and split into npa, nxx, and station #---------------------------------------------------------------- if(substr($cidnum,0,1) eq '1'){ $cidnum=substr($cidnum,1); } if(substr($cidnum,0,2) eq '+1'){ $cidnum=substr($cidnum,2); } print qq(VERBOSE "STATUS: CID is cidnumber" 2\n); #---------------------------------------------------------------- # check for cid name #---------------------------------------------------------------- print qq(VERBOSE "STATUS: checking GoogleContacts for name lookup" 2\n); if ($name = gcontactscache_lookup($cidnum)) { $cidname = $name; print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n); print qq(VERBOSE "STATUS: GoogleContacts said name was $cidname " 2\n); exit(0); } else { print qq(VERBOSE "STATUS: unable to find name with GoogleContacts ($cidnum)" 2\n); } print qq(VERBOSE "STATUS: checking PagineBianche for name lookup" 2\n); for (my $iCount = 0; $iCount < $accorcia; $iCount++) { $tmpnum = $cidnum; if ($iCount != 0) { $tmpnum = substr($tmpnum,0,-$iCount); } if ($name = paginebianche_lookup($tmpnum)) { $cidname = $name; print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n); print qq(VERBOSE "STATUS: PagineBianche said name was $cidname " 2\n); exit(0); } else { print qq(VERBOSE "STATUS: unable to find name with PagineBianche ($cidnum/$tmpnum)" 2\n); } if ($accorcia != 0) { # Proviamo con un 1 in fondo? $tmpnum = $tmpnum . "1"; if ($name = paginebianche_lookup($tmpnum)) { $cidname = $name; print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n); print qq(VERBOSE "STATUS: PagineBianche said name was $cidname " 2\n); exit(0); } else { print qq(VERBOSE "STATUS: unable to find name with PagineBianche ($cidnum/$tmpnum)" 2\n); } } } #---------------------------------------------------------------- # return results and exit #---------------------------------------------------------------- print qq(SET VARIABLE CALLERID\(name\) "$cidnum"\n); print qq(VERBOSE "STATUS: Unknown name for $cidnum " 2\n); exit(0); #---------------------------------------------------------------- # parser #---------------------------------------------------------------- # Pagine Bianche sub paginebianche_lookup { my ($number) = @_; my $ua = LWP::UserAgent->new(timeout => 5); my $URL = qq(http://www.paginebianche.it/execute.cgi?ver=default&font=default&btt=1&ts=106&cb=8&l=it&mr=10&rk=&om=&qs=$number); $ua->agent('AsteriskAGIQuery/1'); my $req = new HTTP::Request GET => $URL; my $res = $ua->request($req); if ($res->is_success()) { if ($res->content =~ /

(.*?)<\/h3>/) { my $clidname = $1; # 'sammai fosse un link $clidname =~ s///; $clidname =~ s/<\/a>//; return $clidname; } } return ""; } sub gcontactscache_lookup { my ($number) = @_; my $clidname = ""; if (-e $gcontacts) { open(CONTACTS,"< $gcontacts"); while() { chomp(); ($gnumber,$gclidname) = split(":"); if ($gnumber eq $number) { $clidname = $gclidname; } } close CONTACTS; return $clidname; } return ""; }