lookup.agi 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/perl
  2. #
  3. # Use Reverse Lookups to populate valuable information
  4. #
  5. # Copyright (C) 2005 Digium, Inc.
  6. #
  7. # Mark Spencer <markster@digium.com>
  8. #
  9. # Based on work of Joe Fratantoni - BrakeDanceJ - Joe@UnrealDestination.com.
  10. #
  11. # This program is Free Software distributed under the terms of the GNU
  12. # General Public License version 2. See LICENSE for details.
  13. #
  14. #
  15. use LWP::UserAgent;
  16. my %AGI;
  17. my $debug = 0;
  18. $|=1;
  19. sub url_decode {
  20. my @args = @_;
  21. s/%([0-9A-F]{2})/chr hex $1/egios for @args;
  22. s/\"//egios for @args;
  23. return wantarray ? @args : $args[0];
  24. }
  25. while(<STDIN>) {
  26. chomp;
  27. last unless length($_);
  28. if (/^agi_(\w+)\:\s+(.*)$/) {
  29. $AGI{$1} = $2;
  30. }
  31. }
  32. alarm(4);
  33. my $number = $AGI{'callerid'};
  34. $number =~ /(\d+)/;
  35. $number = $1;
  36. die("You must specify a number") unless $number;
  37. my $ua = LWP::UserAgent->new;
  38. $ua->agent("Asterisk");
  39. my $req = HTTP::Request->new(POST => 'http://www.411.com/10668/search/Reverse_Phone');
  40. $req->content_type('application/x-www-form-urlencoded');
  41. $req->content("phone=$number");
  42. my $res = $ua->request($req);
  43. if ($res->is_success) {
  44. my $first, $last, $address, $street, $house, $city, $state, $zip, $phone;
  45. if ($res->content =~ /PAGE: PHONE_NOT_FOUND/) {
  46. # Limited Information
  47. $res->content =~ /is a \s+([A-Za-z -]*), ([A-Z]{2}) \s+based phone number and the registered carrier is (.*)\.\s+/;
  48. ($city, $state, $last) =
  49. map { url_decode($_) } ($1, $2, $3);
  50. $cidname = "$city, $state";
  51. } else {
  52. # Full Information
  53. $res->content =~ /RM_HTML_FIRST_ESC_=(.*)&_RM_HTML_LAST_ESC_=(.*)&_RM_HTML_ADDRESS_ESC_=(.*)&_RM_HTML_STREET_ESC_=(.*)&_RM_HTML_HOUSE_ESC_=(.*)&_RM_HTML_CITY_ESC_=(.*)&_RM_HTML_STATE_ESC_=(.*)&_RM_HTML_ZIP_ESC_=(.*)&_RM_HTML_PHONE_ESC_=(.*)&CITY=(.*)&STATE=(.*)/;
  54. ($first, $last, $address, $street, $house, $city, $state, $zip, $phone) =
  55. map { url_decode($_) } ($1, $2, $3, $4, $5, $6, $7, $8, $9);
  56. my $cidname = $last;
  57. if ($first) {
  58. $cidname = $first . " " . $last;
  59. } else {
  60. $cidname = $last;
  61. }
  62. }
  63. print STDOUT "SET VARIABLE CALLERID(name) \"$cidname\"\n";
  64. <STDIN>;
  65. print STDOUT "SET VARIABLE CALLER_ZIP \"$zip\"\n";
  66. <STDIN>;
  67. print STDOUT "SET VARIABLE CALLER_STATE \"$state\"\n";
  68. <STDIN>;
  69. print STDOUT "SET VARIABLE CALLER_CITY \"$city\"\n";
  70. <STDIN>;
  71. print STDOUT "SET VARIABLE CALLER_ADDRESS \"$address\"\n";
  72. <STDIN>;
  73. print STDOUT "SET VARIABLE CALLER_LAST \"$last\"\n";
  74. <STDIN>;
  75. print STDOUT "SET VARIABLE CALLER_FIRST \"$first\"\n";
  76. <STDIN>;
  77. print STDERR "First: $first\n" .
  78. "Last: $last\n" .
  79. "Address: $address\n" .
  80. "Street: $street\n" .
  81. "House: $house\n" .
  82. "City: $city\n" .
  83. "State: $state\n" .
  84. "Zip: $zip\n" .
  85. "Phone: $phone\n" if $debug;
  86. } else {
  87. print STDERR $res->status_line . "\n";
  88. }