Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [cidname-lookup]
- exten => s,1,NoOp(looking up callerid name)
- exten => s,n,GotoIf($["${CALLERID(NAME)}" = "${CALLERID(NUM)}" ]?getname)
- exten => s,n,NoOp(caller id name exists as ${CALLERID(NAME)})
- exten => s,n,Return
- exten => s,n(getname),AGI(mycalleridname.pl,${CALLERID(NUM)})
- exten => s,n,NoOp(Caller ID Name is now ${CALLERID(NAME)})
- exten => s,n,Return
- mycalleridname.pl script:
- #!/usr/bin/perl -w
- #
- # Andrea Riela revision
- # Angelo Conforti <angeloxx@angeloxx.it> 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 <username> --password <password> > <file>
- #
- 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(<STDIN>) {
- 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 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);
- }
- }
- }
- print qq(VERBOSE "STATUS: checking Pronto.it for name lookup" 2\n);
- for (my $iCount = 0; $iCount < $accorcia; $iCount++) {
- $tmpnum = $cidnum;
- if ($iCount != 0) {
- $tmpnum = substr($tmpnum,0,-$iCount);
- }
- if ($name = prontoit_lookup($tmpnum)) {
- $cidname = $name;
- print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n);
- print qq(VERBOSE "STATUS: Pronto.it said name was $cidname " 2\n);
- exit(0);
- } else {
- print qq(VERBOSE "STATUS: unable to find name with Pronto.it ($cidnum/$tmpnum)" 2\n);
- }
- if ($accorcia != 0) {
- # Proviamo con un 1 in fondo?
- $tmpnum = $tmpnum . "1";
- if ($name = prontoit_lookup($tmpnum)) {
- $cidname = $name;
- print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n);
- print qq(VERBOSE "STATUS: Pronto.it said name was $cidname " 2\n);
- exit(0);
- } else {
- print qq(VERBOSE "STATUS: unable to find name with Pronto.it ($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 class="org">(.*?)<\/h3>/) {
- my $clidname = $1;
- # 'sammai fosse un link
- $clidname =~ s/<a href(.*?)>//;
- $clidname =~ s/<\/a>//;
- return $clidname;
- }
- }
- return "";
- }
- # Pronto.it
- sub prontoit_lookup {
- my ($number) = @_;
- my $ua = LWP::UserAgent->new(timeout => 5);
- my $URL = qq(http://www.pronto.it/?q=$number);
- $ua->agent('AsteriskAGIQuery/1');
- my $req = new HTTP::Request GET => $URL;
- my $res = $ua->request($req);
- if ($res->is_success()) {
- if ($res->content =~ /<span class="cognome ">(.+)<\/span>/) {
- my $clidname = $1;
- # 'sammai fosse un link
- $clidname =~ s/<a href(.*?)>//;
- $clidname =~ s/<\/a>//;
- return $clidname;
- }
- }
- return "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement