login | register
Fri 29 of Aug, 2008 [07:48 UTC]

voip-info.org

ScopServ LCA Map

Created by: joel_vandal,Last modification on Tue 16 of Aug, 2005 [18:46 UTC]
This script extracts information about the telephone NPA-NXX for a given phone number and then produces a block for Asterisk extensions.conf file that will route all calls for the LCA via a specific macro.

One use for this is to allow 7 and 10-digit dialing for the entire US/Canada, where free local calls are routed via a POTS line and long-distance calls are routed via a VoIP provider.

This script is available under LGPL license.

Copyright (c) 2005 by ScopServ Inc.

Step 1 - Copy the text below into Notepad and name it lca-map.pl


#!/usr/bin/perl

$npa = shift || 819;
$nxx = shift || 473;
$page = shift || 0;
$astout = shift || 0;
my $result;

$cache = "/tmp/npanxx-$npa$nxx.map";
if (-f $cache) {
   ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime,
       $ctime, $blksize, $blocks) = stat($cache);
   
   $max = ( 3600 * 24 * 7);
   $diff = time() - $mtime;
   if ($diff < $max && $size > 0) {
       open(CACHE, $cache);
       while(<CACHE>) {
           print $_;
       }
       close (CACHE);
       exit;
   }
}

open(CACHE, ">/tmp/npanxx-$npa$nxx.map");

&fetchPrefix($npa, $nxx, $page);

sub fetchPrefix($npa, $nxx, $page) {
   
   my $respage = '';
   if ($page > 1) {
       $respage = "-dresultpage=$page";
   }
   
   my $start = 0;
   my $oknext = 0;
   
   my $cmd = sprintf("curl http://members.dandy.net/~czg/lca_prefix.php -m 15 -G -s -dnpa=%s -dnxx=%s %s", $npa, $nxx, $respage);
   open(IN, "$cmd |");
   while(my $line = <IN>) {
       
       if ($line =~ /<tr class=\"rc.\">/) {
           $start = 1;
           $oknext = 1;
           next;
       }
       
       if ($start == 1) {
           # <td headers="npanxx"><a href="lprefix.php?exch=174680">819-383</a></td>
           if ($line =~ /lprefix.php\?exch=(\d+).*?\">(.*?)<\/a>/) {
               $exch_id = $1;
               $exchange = $2;
           }
           
           # <td headers="exchange"><a href="lca.php?exch=174680">Trois-Rivi&egrave;res, QC</a></td>
           if ($line =~ /lca.php\?exch=(\d+).*?\">(.*?)<\/a>/) {
               $location = $2;
           }
           
           # <td headers="switch"><a href="/~czg/prefix.php?switch=TSRVPQAFDMD">TSRVPQAFDMD</a></td>
           if ($line =~ /prefix.php\?switch=(.*?).*?\">(.*?)<\/a>/) {
               $switch = $2;
           }
           
           # <td headers="ocn"><a href="/~czg/prefix.php?ocn=8821">8821 ROGERS WIRELESS</a></td>
           if ($line =~ /prefix.php\?ocn=(.*?).*?\">(.*?)<\/a>/) {
               $ocn_id = $1;
               $ocn = $2;
           }
       }
       
       if ($start == 1 && $line =~ /<\/tr>/) {
           if ($exchange eq "$npa-$nxx") {
               $oknext = 0;
               $result .= "$exchange ";
               
               if ($astout) {
                   print "; Location : $location\n";
                   print "; Switch   : $switch\n";
                   print "; OCN      : $ocn\n\n";
               } else {
               print CACHE "INFO|$location|$switch|$ocn\n";
               print "INFO|$location|$switch|$ocn\n";
                   }
               fetchExchange($exch_id);
           }
           $start = 0;
           next;
       }
       
       if ($oknext == 1 && $line =~ /resultpage=(\d+)&npa=...\">Next<\/a>/) {
           $page = $1;
           &fetchPrefix($npa, $nxx, $page);
       }
       
   }
   close(IN);
   return $result;
}

sub fetchExchange($exch_id) {
   local $cmd = sprintf("curl http://members.dandy.net/~czg/lprefix.php -m 15 -G -s -dexch=%s", $exch_id);

   my $exc_7  = '';
   my $exc_10 = '';
   my $start = 0;
   open(IN, "$cmd |");
   while(my $line = <IN>) {
       if ($line =~ /<pre>/) {
           $start = 1 ;
           next;
       }
       
       if ($start == 1 && $line =~ /<\/pre>/) {
           $start = 0;
           next;
       }

       if ($start == 1) {
           if ($line =~ /^(\d\d\d);(\d\d\d);(.*?);/) {
               $l_npa = $1;
               $l_nxx = $2;
               $l_loc = $3;

               if ($astout) {
                   $exc_10 .= sprintf("exten => _%dNaVs) ; # %s\n", $l_npa, $l_nxx, $astout, $l_loc);
                   $exc_10 .= sprintf("exten => _%d%dXXXX,2,Congestion\n", $l_npa, $l_nxx);
               } else {

                   print CACHE "DATA|$l_npa|$l_nxx|$l_loc\n";
                   print "DATA|$l_npa|$l_nxx|$l_loc\n";
               }
           }
       }
   }
   
   if ($astout) {
       print $exc_10;
   }
}
close(CACHE);


Comments