Reverse Australia: Difference between revisions

From KlavoWiki
Jump to navigationJump to search
No edit summary
(37 intermediate revisions by the same user not shown)
Line 1: Line 1:
= About =
= About =
[http://www.reverseaustralia.com/ Reverse Australia] is the premier free reverse look-up service for mobiles and landlines within Australia.  
[http://www.reverseaustralia.com/ Reverse Australia] is the premier free reverse look-up service for mobiles and landlines within Australia.<br>
 
You can use the below AGI file without the requirement to use the reverse lookups.  If you change the variable $query_online to equal 0 then the script will never lookup the information online and will only use the data in the MySQL tables.  You would do this if you want to maintain your own caller name lookups.


= Prerequisite =
= Prerequisite =
== Arch Linux ==
If you are tyring to accomplish this using Arch Linux then you will need the following packages from pacman rather than the yum packages below.  Everything else is the same, just miss the yum commands and the rpmforge install.
<pre>
pacman -S perl-cpan-perl-releases perl-libwww perl-extutils-depends perl-lwp-protocol-https
</pre>


== Asterisk AGI Perl module ==
== Asterisk AGI Perl module ==
<pre>
<pre>
wget http://search.cpan.org/CPAN/authors/id/J/JA/JAMESGOL/asterisk-perl-1.01.tar.gz
yum install perl-CPAN perl-devel perl-ExtUtils-MakeMaker perl-libwww-perl perl-CPAN perl-Time-HiRes mysql-devel
tar xvzf asterisk-perl-1.01.tar.gz
 
 
wget http://search.cpan.org/CPAN/authors/id/J/JA/JAMESGOL/asterisk-perl-1.03.tar.gz
tar xvzf asterisk-perl-1.03.tar.gz


cd asterisk-perl-1.01
cd asterisk-perl-1.03


perl Makefile.PL
perl Makefile.PL
Line 16: Line 27:
</pre>
</pre>


== rpmforge ==
CentOS : Temporarily disable IPv6 otherwise LWP will fail at POP3 check.
<pre>
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6
</pre>
<pre>
<pre>
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install DBI'
rpm -Uhv rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e "install Bundle::LWP"
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e "install DBD::mysql"
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e "install CGI::Util"
</pre>
</pre>


==  perl-LWP-UserAgent-Determined ==
==  perl-LWP-UserAgent-Determined ==
For CentOS 7 64 Bit only.
<pre>
<pre>
yum -y install perl-LWP-UserAgent-Determined
rpm -Uhv http://repo.openfusion.net/centos7-x86_64/perl-LWP-UserAgent-Determined-1.07-1.of.el7.noarch.rpm
</pre>
</pre>


Line 48: Line 66:


=== modified table ===
=== modified table ===
I have a WEB site that allows one to edit the contents of the Database.  To use the web site to edit/add/delete entries you will need to use the following table.
I have a WEB site that allows one to edit the contents of the Database.  The web site allows one to edit/add/delete entries. You will need to use the following table. This table and the web site also allows one to use 3 or 4 digit speed dialling.  The speed dial implemention is displayed below.
 
<pre>
<pre>
CREATE TABLE IF NOT EXISTS `cid` (
CREATE TABLE `callers` (
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `name` varchar(60) NOT NULL,
   `name` varchar(60) NOT NULL,
   `number` varchar(10) NOT NULL,
   `number` varchar(20) NOT NULL,
   PRIMARY KEY (`id`)
  `speed` varchar(4) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=81 ;
   PRIMARY KEY (`id`),
  UNIQUE KEY `speed_UNIQUE` (`speed`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
</pre>
</pre>


Line 83: Line 104:
<pre>
<pre>
[incoming-call]
[incoming-call]
exten => s,1,GotoIf($["${CALLERID(num)}"=""]?:3)
exten => _XX.,1,GotoIf($["${CALLERID(name)}" = "${CALLERID(num)}"]?:3)
same  =>   2,Set(CALLERID(name)="Unknown")
same  =>     n,Set(CALLERID(name)="")
same  =>   3,GotoIf($["${CALLERID(name)}"="${CALLERID(num)}"]?:5)
same  =>     n,GotoIf($["${CALLERID(name)}" != ""]?:6)
same  =>   4,Set(CALLERID(name)=)
same  =>     n,Set(CALLERID(name)=Unknown)
same  =>   5,AGI(cid-lookup-v4.agi)
same  =>     n,AGI(cid-lookup-v4.agi)
same  =>   6,Dial(${EVERYONE},19,tw)
same  =>     n,Dial(${EVERYONE},29,tw)
same  =>   7,VoiceMail(200@default,us)
same  =>     n,VoiceMail(200@default,us)
same  =>   8,HangUp
same  =>     n,HangUp
</pre>
</pre>


<br>'''cid-lookup-v4.agi'''
<br>'''cid-lookup-v4.agi'''
<br>This agi file has one modification to the original, and that is $db_table.  I have added a variable for the table name.
<pre>
<pre>
#!/usr/bin/perl
#!/usr/bin/perl
Line 111: Line 133:
my $db_username = "username";
my $db_username = "username";
my $db_password = "password";
my $db_password = "password";
my $db_table = "callers";


## If you access the web via a proxy, you can define it here
## If you access the web via a proxy, you can define it here
Line 122: Line 145:
## Open the SQL database and search for the number calling...
## Open the SQL database and search for the number calling...
my $dbh = DBI->connect("dbi:mysql:$db_name;host=$db_host", $db_username, $db_password) or die "Error connecting to database!\n";
my $dbh = DBI->connect("dbi:mysql:$db_name;host=$db_host", $db_username, $db_password) or die "Error connecting to database!\n";
my $sth = $dbh->prepare("SELECT name FROM cid WHERE number=?");
my $sth = $dbh->prepare("SELECT name FROM $db_table WHERE number=?");
$sth->execute($number) or die "SQL Error: $DBI::errstr\n";
$sth->execute($number) or die "SQL Error: $DBI::errstr\n";
my $name = $sth->fetchrow;
my $name = $sth->fetchrow;
Line 140: Line 163:
$name =~ s/\t//g;
$name =~ s/\t//g;
$name =~ s/,//g;
$name =~ s/,//g;
         $sth = $dbh->prepare("INSERT INTO cid (name,number) VALUES (?,?)");
         $sth = $dbh->prepare("INSERT INTO $db_table (name,number) VALUES (?,?)");
         $sth->execute($name, $number);
         $sth->execute($name, $number);
} else {
} else {
Line 153: Line 176:
print("NOOP Nothing found. Setting to unknown\n");
print("NOOP Nothing found. Setting to unknown\n");
$name = "Unknown Caller";
$name = "Unknown Caller";
$sth = $dbh->prepare("INSERT INTO cid (name,number) VALUES (?,?)");
$sth = $dbh->prepare("INSERT INTO $db_table (name,number) VALUES (?,?)");
$sth->execute($number, $number);
$sth->execute($number, $number);
}
}
Line 159: Line 182:


$AGI->set_callerid("\"$name <$number>\"");
$AGI->set_callerid("\"$name <$number>\"");
</pre>
= WEB Site Implementation =
The WEB site will allow you to add, create, delete and edit entries in the MySQL database. This will allow you to easily create speed dial entries for numbers and modify incoming call display.
You can use this WEB site for free for personal use only.  If you operate a home business, small office, large company then you must purchase a license.  The license will allow you to run the WEB site on an individual server and not on multiple servers within your organisation.  For licensing details please email sales at tech42 dot com dot au
For a copy of the WEB site please email david at klaverstyn dot com dot au.
== Installation ==
<pre>
cd /var/www/html
unzip cid-sd.zip
chmod 646 ./cid/settings.ini
</pre>
=== settings.ini ===
Edit the settings.ini file located in the cid folder.  Make sure that you have valid entries for the following example variables.
# client = "http://pbx.mywebsite.local/cid/"
# database = "asterisk"
# table = "callers"
# username = "ciduser"
# password = "passw0rd"
==== Securing settings.ini ====
To ensure users can not access the details of settings.ini from the web site create the following file in the cid folder.<br><br>
'''.htaccess'''
<pre>
<Files settings.ini>
  order allow,deny
  deny from all
</pre>
=== speed dial ===
<pre>
cp /var/www/html/cid/_api/* /var/lib/asterisk/agi-bin/
chmod +x /var/lib/asterisk/agi-bin/phpagi*
chmod +x /var/lib/asterisk/agi-bin/speed-dial.php
</pre>
You need to edit the '''speed-dial.php''' file and change the required settings.
<pre>
$config['host'] = 'localhost';        // FQDN, localhost, IP, etc
$config['database'] = 'reverseau';    // Database Name
$config['table'] = 'callers';        // Table Name
$config['username'] = 'ciduser';      // MySQL username
$config['password'] = 'passw0rd';    // MySQL password
</pre>
== Asterisk Dialplan ==
You can use any 3 or 4 digit speed dial number.  In my example below I have used a 3 digit number starting with 4.  As long as the speed dial entries from the Caller Identification WEB site matches the dial plan numbers then all will be good.
<pre>
;Speed dial Numbers 4xx
exten => _4XX,1,AGI(speed-dial.php,${EXTEN})
exten => _4XX,2,Goto(internal,${response},1)
exten => _4XX,3,HangUp
exten => sd-invalid,1,PlayBack(you-dialed-wrong-number&please-try-again)
</pre>
</pre>


[[Category : Asterisk]]
[[Category : Asterisk]]

Revision as of 22:11, 15 July 2015

About

Reverse Australia is the premier free reverse look-up service for mobiles and landlines within Australia.

You can use the below AGI file without the requirement to use the reverse lookups. If you change the variable $query_online to equal 0 then the script will never lookup the information online and will only use the data in the MySQL tables. You would do this if you want to maintain your own caller name lookups.

Prerequisite

Arch Linux

If you are tyring to accomplish this using Arch Linux then you will need the following packages from pacman rather than the yum packages below. Everything else is the same, just miss the yum commands and the rpmforge install.

pacman -S perl-cpan-perl-releases perl-libwww perl-extutils-depends perl-lwp-protocol-https

Asterisk AGI Perl module

yum install perl-CPAN perl-devel perl-ExtUtils-MakeMaker perl-libwww-perl perl-CPAN perl-Time-HiRes mysql-devel


wget http://search.cpan.org/CPAN/authors/id/J/JA/JAMESGOL/asterisk-perl-1.03.tar.gz
tar xvzf asterisk-perl-1.03.tar.gz

cd asterisk-perl-1.03

perl Makefile.PL
make all
make install

CentOS : Temporarily disable IPv6 otherwise LWP will fail at POP3 check.

echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install DBI'
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e "install Bundle::LWP"
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e "install DBD::mysql"
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e "install CGI::Util"

perl-LWP-UserAgent-Determined

For CentOS 7 64 Bit only.

rpm -Uhv http://repo.openfusion.net/centos7-x86_64/perl-LWP-UserAgent-Determined-1.07-1.of.el7.noarch.rpm

Reverse Australia Key

Log onto the Reverse Australia web site and select Request an API Key. You will need to log onto the site with a Facebook account. If you don't have one then you'll need to create one otherwise you're screwed.

MySQL Database

Create a database with the name of your choice with a table name of cid.

mysqladmin create reverseau

table

mysql
use reverseau;

CREATE TABLE `cid` (
`name` text NOT NULL,
`number` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

modified table

I have a WEB site that allows one to edit the contents of the Database. The web site allows one to edit/add/delete entries. You will need to use the following table. This table and the web site also allows one to use 3 or 4 digit speed dialling. The speed dial implemention is displayed below.

CREATE TABLE `callers` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(60) NOT NULL,
  `number` varchar(20) NOT NULL,
  `speed` varchar(4) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `speed_UNIQUE` (`speed`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

assign permissions

GRANT ALL ON reverseau.* to ciduser@localhost identified by 'passw0rd';
flush privileges;

cid-lookup.agi

The latest version is available from CRC. Download and move the file to /var/lib/asterisk/agi-bin/ (Thanks Steven Haigh for this wonder script.)

wget http://www.crc.id.au/files/cid-lookup-v4.agi
mv cid-lookup-v4.agi /var/lib/asterisk/agi-bin/
chmod +x /var/lib/asterisk/agi-bin/cid-lookup-v4.agi

Edit the file and change the following constants to the required values.

  1. APIKEY
  2. db_host
  3. db_name
  4. db_username
  5. db_password

Asterisk Dialplan Implementation

[incoming-call]
exten => _XX.,1,GotoIf($["${CALLERID(name)}" = "${CALLERID(num)}"]?:3)
same  =>      n,Set(CALLERID(name)="")
same  =>      n,GotoIf($["${CALLERID(name)}" != ""]?:6)
same  =>      n,Set(CALLERID(name)=Unknown)
same  =>      n,AGI(cid-lookup-v4.agi)
same  =>      n,Dial(${EVERYONE},29,tw)
same  =>      n,VoiceMail(200@default,us)
same  =>      n,HangUp


cid-lookup-v4.agi
This agi file has one modification to the original, and that is $db_table. I have added a variable for the table name.

#!/usr/bin/perl
use strict;
use Asterisk::AGI;
use LWP::UserAgent;
use DBI;

#### Your Reverse Australia Developers Key:
#### http://www.reverseaustralia.com/developer/
my $APIKEY = "";
my $query_online = 1;

## MySQL Database details.
my $db_host = "localhost";
my $db_name = "asterisk";
my $db_username = "username";
my $db_password = "password";
my $db_table = "callers";

## If you access the web via a proxy, you can define it here
#$ in the format "http://my.proxy.com:8080"
my $http_proxy = "";

my $AGI = new Asterisk::AGI;
my %input = $AGI->ReadParse();
my $number = $input{'callerid'};

## Open the SQL database and search for the number calling...
my $dbh = DBI->connect("dbi:mysql:$db_name;host=$db_host", $db_username, $db_password) or die "Error connecting to database!\n";
my $sth = $dbh->prepare("SELECT name FROM $db_table WHERE number=?");
$sth->execute($number) or die "SQL Error: $DBI::errstr\n";
my $name = $sth->fetchrow;

## If we still can't find anything, search the online database if enabled.
if ( $name eq "" and $name ne "anonymous" ) {
	if ( $query_online and $APIKEY ne "" ) {
		my $ua = new LWP::UserAgent;
		$ua->proxy("http", $http_proxy);
		$ua->agent("cid-lookup-v4.agi");
		my $req = new HTTP::Request GET => 'http://api.reverseaustralia.com/cidlookup.php?format=text&key='.$APIKEY.'&q='.$number.'&extended=0';
		my $res = $ua->request($req);
		if ($res->is_success) {
			## We don't need any real parsing on non-extended searches.
			## Set the caller ID and add the results to the database as a cache.
			$name = $res->content;
			$name =~ s/\t//g;
			$name =~ s/,//g;
        		$sth = $dbh->prepare("INSERT INTO $db_table (name,number) VALUES (?,?)");
        		$sth->execute($name, $number);
		} else {
			print("NOOP Error looking up online directory");
		}
	}

	## If we got nothing from the database, and nothing from the online directory
	## (if enabled) we fall back to this. Add the details to the database so someone
	## can come along and add a name later.
	if ( $name eq "" or $name eq "Not found") {
		print("NOOP Nothing found. Setting to unknown\n");
		$name = "Unknown Caller";
		$sth = $dbh->prepare("INSERT INTO $db_table (name,number) VALUES (?,?)");
		$sth->execute($number, $number);
	}
}

$AGI->set_callerid("\"$name <$number>\"");

WEB Site Implementation

The WEB site will allow you to add, create, delete and edit entries in the MySQL database. This will allow you to easily create speed dial entries for numbers and modify incoming call display.

You can use this WEB site for free for personal use only. If you operate a home business, small office, large company then you must purchase a license. The license will allow you to run the WEB site on an individual server and not on multiple servers within your organisation. For licensing details please email sales at tech42 dot com dot au

For a copy of the WEB site please email david at klaverstyn dot com dot au.

Installation

cd /var/www/html
unzip cid-sd.zip
chmod 646 ./cid/settings.ini

settings.ini

Edit the settings.ini file located in the cid folder. Make sure that you have valid entries for the following example variables.

  1. client = "http://pbx.mywebsite.local/cid/"
  2. database = "asterisk"
  3. table = "callers"
  4. username = "ciduser"
  5. password = "passw0rd"

Securing settings.ini

To ensure users can not access the details of settings.ini from the web site create the following file in the cid folder.

.htaccess

<Files settings.ini>
  order allow,deny
  deny from all

speed dial

cp /var/www/html/cid/_api/* /var/lib/asterisk/agi-bin/
chmod +x /var/lib/asterisk/agi-bin/phpagi*
chmod +x /var/lib/asterisk/agi-bin/speed-dial.php

You need to edit the speed-dial.php file and change the required settings.

$config['host'] = 'localhost';        // FQDN, localhost, IP, etc
$config['database'] = 'reverseau';    // Database Name
$config['table'] = 'callers';         // Table Name
$config['username'] = 'ciduser';      // MySQL username
$config['password'] = 'passw0rd';     // MySQL password

Asterisk Dialplan

You can use any 3 or 4 digit speed dial number. In my example below I have used a 3 digit number starting with 4. As long as the speed dial entries from the Caller Identification WEB site matches the dial plan numbers then all will be good.

;Speed dial Numbers 4xx
exten => _4XX,1,AGI(speed-dial.php,${EXTEN})
exten => _4XX,2,Goto(internal,${response},1)
exten => _4XX,3,HangUp

exten => sd-invalid,1,PlayBack(you-dialed-wrong-number&please-try-again)