Advertisement
Guest User

nesys

a guest
Mar 29th, 2010
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. [cidname-lookup]
  2. exten => s,1,NoOp(looking up callerid name)
  3. exten => s,n,GotoIf($["${CALLERID(NAME)}" = "${CALLERID(NUM)}" ]?getname)
  4. exten => s,n,NoOp(caller id name exists as ${CALLERID(NAME)})
  5. exten => s,n,Return
  6. exten => s,n(getname),AGI(mycalleridname.pl,${CALLERID(NUM)})
  7. exten => s,n,NoOp(Caller ID Name is now ${CALLERID(NAME)})
  8. exten => s,n,Return
  9.  
  10. mycalleridname.pl script:
  11.  
  12. #!/usr/bin/perl -w
  13. #
  14. # Andrea Riela revision
  15. # Angelo Conforti <angeloxx@angeloxx.it> based on
  16. # http://www.teamforrest.com/blog/voip/89/using-agi-to-get-caller-id-name-cnam/
  17. # http://km.krot.org/code/gc2fab.pl
  18. #
  19. # Install instruction on Debian:
  20. # apt-get install libwww-perl libcrypt-ssleay-perl
  21. #
  22. # Version 0.1 -> first version
  23. # Version 0.2 -> accorciamento del numero di telefono
  24. # Version 0.3 -> attinge al database Google Contacts su /var/lib/asterisk/gcontacts.txt
  25. # creato con gcontacts.pl --username <username> --password <password> > <file>
  26. #
  27.  
  28. use LWP::UserAgent;
  29.  
  30. $|=1;
  31.  
  32. my ($cidnum,$cidname,$name,$accorcia,$tmpnum,$gcontacts);
  33.  
  34. # Se deve o non deve fare ricerche accorciando
  35. # fino a $accorcia cifre il callerid (aggiungendo anche un "1" in fondo)
  36. $accorcia=3;
  37. $gcontacts='/var/lib/asterisk/gcontacts.txt';
  38.  
  39. #----------------------------------------------------------------
  40. # get asterisk initial info
  41. #----------------------------------------------------------------
  42.  
  43. while(<STDIN>) {
  44. chomp;
  45. last unless length($_);
  46. }
  47.  
  48. #----------------------------------------------------------------
  49. # check if we have a caller id
  50. #----------------------------------------------------------------
  51.  
  52. if ($ARGV[0]) {
  53. $cidnum = $ARGV[0];
  54. } else {
  55. print qq(VERBOSE "ERROR: no callerid provided" 2\n);
  56. exit(0);
  57. }
  58.  
  59. #----------------------------------------------------------------
  60. # check caller id and split into npa, nxx, and station
  61. #----------------------------------------------------------------
  62.  
  63. if(substr($cidnum,0,1) eq '1'){
  64. $cidnum=substr($cidnum,1);
  65. }
  66.  
  67. if(substr($cidnum,0,2) eq '+1'){
  68. $cidnum=substr($cidnum,2);
  69. }
  70.  
  71. print qq(VERBOSE "STATUS: CID is cidnumber" 2\n);
  72.  
  73. #----------------------------------------------------------------
  74. # check for cid name
  75. #----------------------------------------------------------------
  76.  
  77. print qq(VERBOSE "STATUS: checking PagineBianche for name lookup" 2\n);
  78.  
  79. for (my $iCount = 0; $iCount < $accorcia; $iCount++) {
  80. $tmpnum = $cidnum;
  81. if ($iCount != 0) {
  82. $tmpnum = substr($tmpnum,0,-$iCount);
  83. }
  84. if ($name = paginebianche_lookup($tmpnum)) {
  85. $cidname = $name;
  86. print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n);
  87. print qq(VERBOSE "STATUS: PagineBianche said name was $cidname " 2\n);
  88. exit(0);
  89. } else {
  90. print qq(VERBOSE "STATUS: unable to find name with PagineBianche ($cidnum/$tmpnum)" 2\n);
  91. }
  92. if ($accorcia != 0) {
  93. # Proviamo con un 1 in fondo?
  94. $tmpnum = $tmpnum . "1";
  95. if ($name = paginebianche_lookup($tmpnum)) {
  96. $cidname = $name;
  97. print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n);
  98. print qq(VERBOSE "STATUS: PagineBianche said name was $cidname " 2\n);
  99. exit(0);
  100. } else {
  101. print qq(VERBOSE "STATUS: unable to find name with PagineBianche ($cidnum/$tmpnum)" 2\n);
  102. }
  103. }
  104. }
  105.  
  106. print qq(VERBOSE "STATUS: checking Pronto.it for name lookup" 2\n);
  107.  
  108. for (my $iCount = 0; $iCount < $accorcia; $iCount++) {
  109. $tmpnum = $cidnum;
  110. if ($iCount != 0) {
  111. $tmpnum = substr($tmpnum,0,-$iCount);
  112. }
  113. if ($name = prontoit_lookup($tmpnum)) {
  114. $cidname = $name;
  115. print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n);
  116. print qq(VERBOSE "STATUS: Pronto.it said name was $cidname " 2\n);
  117. exit(0);
  118. } else {
  119. print qq(VERBOSE "STATUS: unable to find name with Pronto.it ($cidnum/$tmpnum)" 2\n);
  120. }
  121. if ($accorcia != 0) {
  122. # Proviamo con un 1 in fondo?
  123. $tmpnum = $tmpnum . "1";
  124. if ($name = prontoit_lookup($tmpnum)) {
  125. $cidname = $name;
  126. print qq(SET VARIABLE CALLERID\(name\) "$cidname"\n);
  127. print qq(VERBOSE "STATUS: Pronto.it said name was $cidname " 2\n);
  128. exit(0);
  129. } else {
  130. print qq(VERBOSE "STATUS: unable to find name with Pronto.it ($cidnum/$tmpnum)" 2\n);
  131. }
  132. }
  133. }
  134.  
  135. #----------------------------------------------------------------
  136. # return results and exit
  137. #----------------------------------------------------------------
  138.  
  139. print qq(SET VARIABLE CALLERID\(name\) "$cidnum"\n);
  140. print qq(VERBOSE "STATUS: Unknown name for $cidnum " 2\n);
  141. exit(0);
  142.  
  143. #----------------------------------------------------------------
  144. # parser
  145. #----------------------------------------------------------------
  146. # Pagine Bianche
  147. sub paginebianche_lookup {
  148. my ($number) = @_;
  149. my $ua = LWP::UserAgent->new(timeout => 5);
  150. 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);
  151. $ua->agent('AsteriskAGIQuery/1');
  152. my $req = new HTTP::Request GET => $URL;
  153. my $res = $ua->request($req);
  154. if ($res->is_success()) {
  155. if ($res->content =~ /<h3 class="org">(.*?)<\/h3>/) {
  156. my $clidname = $1;
  157. # 'sammai fosse un link
  158. $clidname =~ s/<a href(.*?)>//;
  159. $clidname =~ s/<\/a>//;
  160. return $clidname;
  161. }
  162. }
  163. return "";
  164. }
  165.  
  166. # Pronto.it
  167. sub prontoit_lookup {
  168. my ($number) = @_;
  169. my $ua = LWP::UserAgent->new(timeout => 5);
  170. my $URL = qq(http://www.pronto.it/?q=$number);
  171. $ua->agent('AsteriskAGIQuery/1');
  172. my $req = new HTTP::Request GET => $URL;
  173. my $res = $ua->request($req);
  174. if ($res->is_success()) {
  175. if ($res->content =~ /<span class="cognome ">(.+)<\/span>/) {
  176. my $clidname = $1;
  177. # 'sammai fosse un link
  178. $clidname =~ s/<a href(.*?)>//;
  179. $clidname =~ s/<\/a>//;
  180. return $clidname;
  181. }
  182. }
  183. return "";
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement