#!/usr/bin/perl # PiPhone Asterisk AGI script # goes in /var/lib/asterisk/agi-bin # Christopher Poole # email username: poolecl # email server: yahoo.ca # updated 13-march-2011 # add somthing to your /etc/asterisk/extensions_custom.conf like this # [from-sip-external] # exten => pi,1,Answer() # exten => pi,n,AGI(pi.agi) # exten => pi,n,Macro(hangupcall,) use Asterisk::AGI; use DBI; $AGI = new Asterisk::AGI; $dbh = DBI->connect('DBI:mysql:piphone', 'your mysql username', 'your mysql passwod'); # note, you need a # mysql database named piphone # table named phones # fields named callerid and digit # pull AGI variables into %input %input = $AGI->ReadParse(); $AGI->verbose("channel $input{channel}", 1); $AGI->verbose("callerid $input{callerid}", 1); $callerid = $input{callerid}; if ($callerid =~ m/.*unknown.*/i) { $callerid = ""; } $callerid =~ s/\W//g; my $file=0; my $filename="/var/lib/asterisk/agi-bin/pi100m.hexbin."; my $filetoopen; my $digits; my $digit1; my $digit2; my $position = &get_last_position($callerid); if ($position > 0) { my $press; $press = $AGI->exec("Background", "pi-resume"); if ($press != ord('1') && $press != ord('2')) { # if we didn't get an appropreat answer, wait a bit more for a digit $press = $AGI->wait_for_digit(4000); } if ($press == ord('2')) { # press 2 to restart, all other choices end up resuming $position = 0; } } else { # new caller, give them a little bit to hear before we start talking # Sleep for 500 milliseconds select(undef, undef, undef, 0.5); } if ($position == 0) { $AGI->say_digits(3); $AGI->exec("PLAYBACK", "point"); } $file = int ($position / 100000000); $seekspot = $position % 100000000; $seekspot = int ($seekspot / 2); while (1) { $filetoopen=sprintf("%s%03u", $filename, $file++); open FILE, $filetoopen or last; $AGI->verbose("opened $filetoopen", 1); if ($seekspot > 0) { seek FILE, ($seekspot), 0; read (FILE, $digits, 1); $digit1 = (ord $digits & 0xf0) >> 4; $digit2 = ord $digits & 0x0f; if (!($position % 2)) { # only print first digit of the pair if are in an even position; &say_digit($callerid, \$position, $digit1); } &say_digit($callerid, \$position, $digit2); $seekspot = 0; } while (read (FILE, $digits, 1) != 0) { $digit1 = (ord $digits & 0xf0) >> 4; $digit2 = ord $digits & 0x0f; &say_digit($callerid, \$position, $digit1); &say_digit($callerid, \$position, $digit2); } close FILE; } $AGI->verbose("wow, ran out of pi digits!", 1); sub say_digit($callerid, $position_ref, $digit) { my ($callerid, $position_ref, $digit) = @_; $AGI->say_digits("$digit"); $$position_ref++; &update_position($callerid, $$position_ref); } sub get_last_position($callerid) { my ($callerid) = @_; my $sth; $query = "SELECT digit FROM phones WHERE callerid = $callerid"; $sth = $dbh->prepare($query); $sth->execute(); return $sth->fetchrow(); } sub update_position($callerid, $position) { my ($callerid, $position) = @_; my $sth; $query = "INSERT DELAYED INTO phones SET callerid = $callerid, digit = $position ON DUPLICATE KEY UPDATE digit = $position"; $sth = $dbh->prepare($query); $sth->execute(); }