Asterisk::LCR::Dialer - Generic dialer object


Asterisk-LCR documentation Contained in the Asterisk-LCR distribution.

Index


Code Index:

NAME

Top

Asterisk::LCR::Dialer - Generic dialer object

SYNOPSIS

Top

  use Asterisk::LCR::Dialer;
  my $dialer = Asterisk::LCR::Dialer::<CONCRETE_CLASS>->new ();




SUMMARY

Top

Represents a generic dialing strategy. The strategy is defined in the $self->process() method, which in this package is undefined.

ATTRIBUTES

Top

limit (numerical scalar)

Limit dialing strategies to 'limit' cheapest routes. Optional.

opts (scalar)

Options to be appended to the dialstring. Example:

  '120|HS(7200)'

METHODS

Top

$self->locale();

Returns the 'locale' attribute.

$self->set_locale ($locale);

Sets the 'locale' attribute to $locale.

$self->limit();

Returns the optional 'limit' attribute.

Returns 100000 if it's not defined (quasi-infinity)

$self->set_limit ($limit);

Sets the optional 'limit' attribute to $limit.

$self->opts();

Returns the optional 'opts' attribute.

If the 'opts' attribute is not TRUE, returns an empty string.

If the 'opts' attribute doesn't start with a pipe symbol, returns it prefixed with |.

$self->set_opts ($opts);

Sets the optional 'opts' attribute to $opts.

$self->process ($number);

Turns $number into a canonical, global number if $number if $self->locale() returns an Asterisk::LCR::Locale object.

Then calls $self->_process ($number). This method must be defined in subclasses.

$self->dial ($str);

Dials string $str, eventually with options $self->opts();

$self->dial_string ($number, $rate)

Returns a string from a variable template suitable for use within Dial()

Extract a string dial template from the asterisk config file.

For example, say $rate->{provider} = 'voipjet'. This method will look for an asterisk variable called 'ASTERISK_LCR_TMPL_VOIPJET'. The grammar to be used for this syntax is:

  VARIABLE: [LOCALE] DIAL_STRING

  LOCALE: (scalar string, i.e. 'us' or 'fr' or even /var/mylocale.txt)

  DIAL_STRING: <whateverwhatever>REPLACEME<whateverwhatever>

Where REPLACEME will be replaced by the phone number to dial.

If LOCALE is defined and an Asterisk::LCR::Locale object can be created, then $num is assumed to be given as a canonical, international number.

For example, if LOCALE is 'fr' and the number is 33575874745, then 05175874745 will be dialled instead.

Otherwise the number is dialed "as is".

$self->rates ($number);

Returns an array of rates for $number, sorted cheapest first.


Asterisk-LCR documentation Contained in the Asterisk-LCR distribution.
package Asterisk::LCR::Dialer;
use base qw /Asterisk::LCR::Object/;
use Asterisk::LCR::Locale;
use Config::Mini;
use warnings;
use strict;


our $STORE = undef;


sub locale
{
    my $self = shift;
    my $loc  = $self->{locale};
    return $loc ? Asterisk::LCR::Locale->new ($loc) : undef;
}


sub set_locale
{
    my $self = shift;
    $self->{locale} = shift;
}


sub limit
{
    my $self = shift;
    return $self->{limit} || 100000;
}


sub set_limit
{
    my $self = shift;
    $self->{limit} = shift;
}


sub opts
{
    my $self = shift;
    my $opts = $self->{opts};
    return '' unless ($opts);
    
    return ($opts =~ /^\|/) ? $opts : "|$opts";
}


sub set_opts
{
    my $self = shift;
    $self->{opts} = shift;
}


sub process
{
    my $self = shift;
    my $num  = shift;
    my $loc  = $self->locale();
    
    $loc and do { $num = $loc->local_to_global ($num) };
    $self->_process ($num);
}


sub _process
{
    die "Asterisk::LCR::Dialer::process() is a virtual method";
}


sub dial
{
    my $self = shift;
    my $str  = shift || return;
    $str .= $self->opts();

    my $agi  = $self->{agi} || die '$self->{agi} is not set';
    return $agi->exec ("DIAL $str");
}


sub dial_string
{
    my $self = shift;
    my $num  = shift || return;
    my $rate = shift || return;
    
    my $provider   = $rate->provider();
    my $provider_o = Config::Mini::instantiate ("import:$provider");
    my $value      = $provider_o->{dial};
    
    my ($locale, $dialtmpl) = ($value =~ /\s/) ? ( split /\s+/, $value, 2 ) : (undef, $value);
    for ($locale)
    {
    	$locale || next;
    	$locale = Asterisk::LCR::Locale->new ($locale) || next;
    	$num = $locale->global_to_local ($num);
    }
    
    $dialtmpl || return;
    $dialtmpl =~ s/REPLACEME/$num/;
    return ($dialtmpl);
}


sub rates
{
    my $self  = shift;
    $STORE  ||= Config::Mini::instantiate ('storage') || die 'no storage backend configured...';
    return $STORE->list (@_);
}


1;


__END__