autosupport: Add 'pjproject show buildopts'
[asterisk/asterisk.git] / contrib / scripts / lookup.agi
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
26 while(<STDIN>) {
27         chomp;
28         last unless length($_);
29         if (/^agi_(\w+)\:\s+(.*)$/) {
30                 $AGI{$1} = $2;
31         }
32 }
33
34 alarm(4);
35 my $number = $AGI{'callerid'};
36 $number =~ /(\d+)/;
37 $number = $1;
38 die("You must specify a number") unless $number;
39 my $ua = LWP::UserAgent->new;
40 $ua->agent("Asterisk");
41 my $req = HTTP::Request->new(POST => 'http://www.411.com/10668/search/Reverse_Phone');
42 $req->content_type('application/x-www-form-urlencoded');
43 $req->content("phone=$number");
44 my $res = $ua->request($req);
45 if ($res->is_success) {
46         my $first, $last, $address, $street, $house, $city, $state, $zip, $phone;
47         if ($res->content =~ /PAGE: PHONE_NOT_FOUND/) {
48                 # Limited Information
49                 $res->content =~ /is a \s+([A-Za-z -]*), ([A-Z]{2}) \s+based phone number and the registered carrier is (.*)\.\s+/;
50                 ($city, $state, $last) =
51                         map  { url_decode($_) } ($1, $2, $3);
52                 $cidname = "$city, $state";
53         } else {
54                 # Full Information
55                 $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=(.*)/;
56                 ($first, $last, $address, $street, $house, $city, $state, $zip, $phone) =
57                         map  { url_decode($_) } ($1, $2, $3, $4, $5, $6, $7, $8, $9);
58                 my $cidname = $last;
59                 if ($first) {
60                         $cidname = $first . " " . $last;
61                 } else {
62                         $cidname = $last;
63                 }
64         }
65         print STDOUT "SET VARIABLE CALLERID(name) \"$cidname\"\n";
66         <STDIN>;
67         print STDOUT "SET VARIABLE CALLER_ZIP \"$zip\"\n";
68         <STDIN>;
69         print STDOUT "SET VARIABLE CALLER_STATE \"$state\"\n";
70         <STDIN>;
71         print STDOUT "SET VARIABLE CALLER_CITY \"$city\"\n";
72         <STDIN>;
73         print STDOUT "SET VARIABLE CALLER_ADDRESS \"$address\"\n";
74         <STDIN>;
75         print STDOUT "SET VARIABLE CALLER_LAST \"$last\"\n";
76         <STDIN>;
77         print STDOUT "SET VARIABLE CALLER_FIRST \"$first\"\n";
78         <STDIN>;
79         print STDERR "First: $first\n" .
80                   "Last: $last\n" .
81                   "Address: $address\n" .
82                   "Street: $street\n" .
83                   "House: $house\n" .
84                   "City: $city\n" .
85                   "State: $state\n" .
86                   "Zip: $zip\n" .
87                   "Phone: $phone\n" if $debug;
88 } else {
89         print STDERR $res->status_line . "\n";
90 }