| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Asterisk::LCR::Dialer - Generic dialer object | ||
| 4 | |||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | use Asterisk::LCR::Dialer; | ||
| 9 | my $dialer = Asterisk::LCR::Dialer::<CONCRETE_CLASS>->new (); | ||
| 10 | |||
| 11 | |||
| 12 | =head1 SUMMARY | ||
| 13 | |||
| 14 | Represents a generic dialing strategy. The strategy is defined | ||
| 15 | in the $self->process() method, which in this package is undefined. | ||
| 16 | |||
| 17 | |||
| 18 | =head1 ATTRIBUTES | ||
| 19 | |||
| 20 | =head2 limit (numerical scalar) | ||
| 21 | |||
| 22 | Limit dialing strategies to 'limit' cheapest routes. | ||
| 23 | Optional. | ||
| 24 | |||
| 25 | =head2 opts (scalar) | ||
| 26 | |||
| 27 | Options to be appended to the dialstring. Example: | ||
| 28 | |||
| 29 | '120|HS(7200)' | ||
| 30 | |||
| 31 | =head1 METHODS | ||
| 32 | |||
| 33 | =cut | ||
| 34 | package Asterisk::LCR::Dialer; | ||
| 35 | use base qw /Asterisk::LCR::Object/; | ||
| 36 | use Asterisk::LCR::Locale; | ||
| 37 | use Config::Mini; | ||
| 38 | use warnings; | ||
| 39 | use strict; | ||
| 40 | |||
| 41 | |||
| 42 | our $STORE = undef; | ||
| 43 | |||
| 44 | |||
| 45 | =head2 $self->locale(); | ||
| 46 | |||
| 47 | Returns the 'locale' attribute. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | sub locale | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | my $loc = $self->{locale}; | ||
| 54 | return $loc ? Asterisk::LCR::Locale->new ($loc) : undef; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | =head2 $self->set_locale ($locale); | ||
| 59 | |||
| 60 | Sets the 'locale' attribute to $locale. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | sub set_locale | ||
| 64 | { | ||
| 65 | my $self = shift; | ||
| 66 | $self->{locale} = shift; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | =head2 $self->limit(); | ||
| 71 | |||
| 72 | Returns the optional 'limit' attribute. | ||
| 73 | |||
| 74 | Returns 100000 if it's not defined (quasi-infinity) | ||
| 75 | |||
| 76 | =cut | ||
| 77 | sub limit | ||
| 78 | { | ||
| 79 | my $self = shift; | ||
| 80 | return $self->{limit} || 100000; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | =head2 $self->set_limit ($limit); | ||
| 85 | |||
| 86 | Sets the optional 'limit' attribute to $limit. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | sub set_limit | ||
| 90 | { | ||
| 91 | my $self = shift; | ||
| 92 | $self->{limit} = shift; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | =head2 $self->opts(); | ||
| 97 | |||
| 98 | Returns the optional 'opts' attribute. | ||
| 99 | |||
| 100 | If the 'opts' attribute is not TRUE, returns an empty string. | ||
| 101 | |||
| 102 | If the 'opts' attribute doesn't start with a pipe symbol, returns | ||
| 103 | it prefixed with |. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | sub opts | ||
| 107 | { | ||
| 108 | my $self = shift; | ||
| 109 | my $opts = $self->{opts}; | ||
| 110 | return '' unless ($opts); | ||
| 111 | |||
| 112 | return ($opts =~ /^\|/) ? $opts : "|$opts"; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | =head2 $self->set_opts ($opts); | ||
| 117 | |||
| 118 | Sets the optional 'opts' attribute to $opts. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | sub set_opts | ||
| 122 | { | ||
| 123 | my $self = shift; | ||
| 124 | $self->{opts} = shift; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | =head2 $self->process ($number); | ||
| 129 | |||
| 130 | Turns $number into a canonical, global number if $number | ||
| 131 | if $self->locale() returns an Asterisk::LCR::Locale object. | ||
| 132 | |||
| 133 | Then calls $self->_process ($number). This method must | ||
| 134 | be defined in subclasses. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | sub process | ||
| 138 | { | ||
| 139 | my $self = shift; | ||
| 140 | my $num = shift; | ||
| 141 | my $loc = $self->locale(); | ||
| 142 | |||
| 143 | $loc and do { $num = $loc->local_to_global ($num) }; | ||
| 144 | $self->_process ($num); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | sub _process | ||
| 149 | { | ||
| 150 | die "Asterisk::LCR::Dialer::process() is a virtual method"; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | =head2 $self->dial ($str); | ||
| 155 | |||
| 156 | Dials string $str, eventually with options $self->opts(); | ||
| 157 | |||
| 158 | =cut | ||
| 159 | sub dial | ||
| 160 | { | ||
| 161 | my $self = shift; | ||
| 162 | my $str = shift || return; | ||
| 163 | $str .= $self->opts(); | ||
| 164 | |||
| 165 | my $agi = $self->{agi} || die '$self->{agi} is not set'; | ||
| 166 | return $agi->exec ("DIAL $str"); | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | =head2 $self->dial_string ($number, $rate) | ||
| 171 | |||
| 172 | Returns a string from a variable template suitable for use | ||
| 173 | within Dial() | ||
| 174 | |||
| 175 | Extract a string dial template from the asterisk config file. | ||
| 176 | |||
| 177 | For example, say $rate->{provider} = 'voipjet'. This method will look for | ||
| 178 | an asterisk variable called 'ASTERISK_LCR_TMPL_VOIPJET'. The grammar to be used | ||
| 179 | for this syntax is: | ||
| 180 | |||
| 181 | VARIABLE: [LOCALE] DIAL_STRING | ||
| 182 | |||
| 183 | LOCALE: (scalar string, i.e. 'us' or 'fr' or even /var/mylocale.txt) | ||
| 184 | |||
| 185 | DIAL_STRING: <whateverwhatever>REPLACEME<whateverwhatever> | ||
| 186 | |||
| 187 | Where REPLACEME will be replaced by the phone number to dial. | ||
| 188 | |||
| 189 | If LOCALE is defined and an Asterisk::LCR::Locale object can be created, | ||
| 190 | then $num is assumed to be given as a canonical, international | ||
| 191 | number. | ||
| 192 | |||
| 193 | For example, if LOCALE is 'fr' and the number is 33575874745, then | ||
| 194 | 05175874745 will be dialled instead. | ||
| 195 | |||
| 196 | Otherwise the number is dialed "as is". | ||
| 197 | |||
| 198 | =cut | ||
| 199 | sub dial_string | ||
| 200 | { | ||
| 201 | my $self = shift; | ||
| 202 | my $num = shift || return; | ||
| 203 | my $rate = shift || return; | ||
| 204 | |||
| 205 | my $provider = $rate->provider(); | ||
| 206 | my $provider_o = Config::Mini::instantiate ("import:$provider"); | ||
| 207 | my $value = $provider_o->{dial}; | ||
| 208 | |||
| 209 | my ($locale, $dialtmpl) = ($value =~ /\s/) ? ( split /\s+/, $value, 2 ) : (undef, $value); | ||
| 210 | for ($locale) | ||
| 211 | { | ||
| 212 | $locale || next; | ||
| 213 | $locale = Asterisk::LCR::Locale->new ($locale) || next; | ||
| 214 | $num = $locale->global_to_local ($num); | ||
| 215 | } | ||
| 216 | |||
| 217 | $dialtmpl || return; | ||
| 218 | $dialtmpl =~ s/REPLACEME/$num/; | ||
| 219 | return ($dialtmpl); | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | =head2 $self->rates ($number); | ||
| 224 | |||
| 225 | Returns an array of rates for $number, sorted cheapest first. | ||
| 226 | |||
| 227 | =cut | ||
| 228 | sub rates | ||
| 229 | { | ||
| 230 | my $self = shift; | ||
| 231 | $STORE ||= Config::Mini::instantiate ('storage') || die 'no storage backend configured...'; | ||
| 232 | return $STORE->list (@_); | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | 1; | ||
| 237 | |||
| 238 | |||
| 239 | __END__ |