Prefix Area Code With SER

I'm setting up a SER server for routing SIP calls to a PSTN gateway. Calls to the PSTN should always be 10 digits, just to remove any confusion, but I want to allow people to dial just 7 digits. I needed a way to look at the caller's phone number and prefix their destination number with their own area code. Hans fought this issue previously and couldn't get it to work just with SER, but instead had to exec a script. Not ideal, but it'll work. He didn't have the script at hand so I wrote my own in Perl.

First, the SER config snippet:

if (uri =~ "^sip:[0-9]{7}@") {
   exec_dset("/usr/local/ser/add_areacode.pl");
}

And the script:

#!/usr/bin/perl

use strict;
my $DEFAULT_AREA_CODE = '307';
my $to = $ARGV[0];
my $from = $ENV{SIP_HF_FROM};

# if it's not a 7 digit number
&output($to) if ($to !~ m/sip:\d{7}@/);

# if no from header. weird, but whatever.
&output($to) if (!$from);

# find area code addr
$from =~ m/<sip:(\d{3})\d{7}\@/;
my $area = $1;

# no area code? just assume one
$area = $DEFAULT_AREA_CODE if (!$area);

$to =~ s/sip:/sip:$area/;
&output($to);

sub output
{
   my $result = shift;
   print $result;
   exit 0;
}

tags: 

2 Comments

For those interested to do it

For those interested to do it with OpenSER 1.2.0, here is the code snippet - it takes first three digits of From username and prefixes R-URI username with them

if(from_uri==myself && $(ruri.user{s.len}) == 7)
  if($(from.user{s.len}) == 10)
    $ruri.user = $(from.user{s.substr,0,3}}) + $ruri.user;

if($(ruri.user{s.len}) == 7)
  $ruri.user = "307" + $ruri.user;

OpenSER 1.2, yay!

That's beautious. I will try that out in my config. So far I've been very impressed with the capabilities of OpenSER 1.2. In fact, I've migrated my config over and have all but decided to go with it over SER.

Subscribe to Comments for "Prefix Area Code With SER" Subscribe to zmonkey.org - All comments