1 package FS::part_export::netsapiens;
3 use vars qw(@ISA $me %info);
7 use Date::Format qw( time2str );
8 use Regexp::Common qw/URI/;
10 @ISA = qw(FS::part_export);
11 $me = '[FS::part_export::netsapiens]';
13 #These export options set default values for the various commands
14 #to create/update objects. Add more options as needed.
16 my %tristate = ( type => 'select', options => [ '', 'yes', 'no' ]);
18 tie my %subscriber_fields, 'Tie::IxHash',
19 'admin_vmail' => { label=>'VMail Prov.', %tristate },
20 'dial_plan' => { label=>'Dial Translation' },
21 'dial_policy' => { label=>'Dial Permission' },
22 'call_limit' => { label=>'Call Limit' },
23 'domain_dir' => { label=>'Dir Lst', %tristate },
26 tie my %registrar_fields, 'Tie::IxHash',
27 'authenticate_register' => { label=>'Authenticate Registration', %tristate },
28 'authentication_realm' => { label=>'Authentication Realm' },
31 tie my %dialplan_fields, 'Tie::IxHash',
32 'responder' => { label=>'Application' }, #this could be nicer
33 'from_name' => { label=>'Source Name Translation' },
34 'from_user' => { label=>'Source User Translation' },
39 'fnr' => 'Forward Not Registered',
40 'fna' => 'Forward No Answer',
41 'fbu' => 'Forward Busy',
42 'dnd' => 'Do-Not-Disturb',
43 'sim' => 'Simultaneous Ring',
46 tie my %options, 'Tie::IxHash',
47 'login' => { label=>'NetSapiens tac2 User API username' },
48 'password' => { label=>'NetSapiens tac2 User API password' },
49 'url' => { label=>'NetSapiens tac2 User URL' },
50 'device_login' => { label=>'NetSapiens tac2 Device API username' },
51 'device_password' => { label=>'NetSapiens tac2 Device API password' },
52 'device_url' => { label=>'NetSapiens tac2 Device URL' },
53 'domain' => { label=>'NetSapiens Domain' },
54 'domain_no_tld' => { label=>'Omit TLD from domains', type=>'checkbox' },
55 'debug' => { label=>'Enable debugging', type=>'checkbox' },
57 'features' => { label => 'Default features',
60 options => [ keys %features ],
61 option_label => sub { $features{$_[0]}; },
65 'did_countrycode' => { label=>'Use country code in DID destination',
70 'svc' => [ 'svc_phone', ], # 'part_device',
71 'desc' => 'Provision phone numbers to NetSapiens',
72 'options' => \%options,
74 Requires installation of
75 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
80 # http://devguide.netsapiens.com/
82 sub rebless { shift; }
86 my ($self, $options) = @_;
88 my $rex = qr/$RE{URI}{HTTP}{-scheme => qr|https?|}/; # match any "http:" or "https:" URL
90 for my $key (qw/url device_url/) {
91 if ($$options{$key} && ($$options{$key} !~ $rex)) {
92 return "Invalid (URL): " . $$options{$key};
102 $self->_ns_command('', @_);
105 sub ns_device_command {
107 $self->_ns_command('device_', @_);
111 my( $self, $prefix, $method, $command ) = splice(@_,0,4);
113 # kludge to curb excessive paranoia in LWP 6.0+
114 local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
115 eval 'use REST::Client';
118 my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
120 my @args = ( $command );
122 if ( $method eq 'PUT' ) {
123 my $content = $ns->buildQuery( { @_ } );
125 push @args, $content;
126 } elsif ( $method eq 'GET' ) {
127 $args[0] .= $ns->buildQuery( { @_ } );
130 warn "$me $method ". $self->option($prefix.'url'). join(', ', @args). "\n"
131 if $self->option('debug');
133 my $auth = encode_base64( $self->option($prefix.'login'). ':'.
134 $self->option($prefix.'password') );
135 push @args, { 'Authorization' => "Basic $auth" };
137 $ns->$method( @args );
142 my($self, $svc_phone) = (shift, shift);
143 my $domain = $svc_phone->domain || $self->option('domain');
145 $domain =~ s/\.\w{2,4}$//
146 if $self->option('domain_no_tld');
152 my($self, $svc_phone) = (shift, shift);
154 my $domain = $self->ns_domain($svc_phone);
155 my $phonenum = $svc_phone->phonenum;
157 "/domains_config/$domain/subscriber_config/$phonenum";
161 my($self, $svc_phone) = (shift, shift);
163 $self->ns_subscriber($svc_phone).
164 '/registrar_config/'. $self->ns_devicename($svc_phone);
168 my($self, $svc_phone, $feature) = (shift, shift, shift);
170 $self->ns_subscriber($svc_phone).
171 "/feature_config/$feature,*,*,*,*";
176 my( $self, $svc_phone ) = (shift, shift);
178 my $domain = $self->ns_domain($svc_phone);
179 #my $countrycode = $svc_phone->countrycode;
180 my $phonenum = $svc_phone->phonenum;
182 #"sip:$countrycode$phonenum\@$domain";
183 "sip:$phonenum\@$domain";
187 my($self, $svc_phone) = (shift, shift);
189 my $countrycode = $svc_phone->countrycode || '1';
190 my $phonenum = $svc_phone->phonenum;
191 # Only in the dialplan destination, nowhere else
192 if ( $self->option('did_countrycode') ) {
193 $phonenum = $countrycode . $phonenum;
196 #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
197 "/domains_config/admin-only/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*,*,*,*,*,*,*";
201 my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
203 #my $countrycode = $svc_phone->countrycode;
204 #my $phonenum = $svc_phone->phonenum;
206 "/phones_config/". lc($phone_device->mac_addr);
209 sub ns_create_or_update {
210 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
212 my $domain = $self->ns_domain($svc_phone);
213 #my $countrycode = $svc_phone->countrycode;
214 my $phonenum = $svc_phone->phonenum;
216 #deal w/unaudited netsapiens services?
217 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
219 my( $firstname, $lastname );
220 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
224 $firstname = $cust_main->get('first');
225 $lastname = $cust_main->get('last');
228 my ($email) = ($cust_main->invoicing_list_emailonly, '');
229 my $custnum = $cust_main->custnum;
232 # Piece 1 (already done) - User creation
235 $phonenum =~ /^(\d{3})/;
238 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
239 'subscriber_login' => $phonenum.'@'.$domain,
240 'firstname' => $firstname,
241 'lastname' => $lastname,
242 'subscriber_pin' => $svc_phone->pin,
243 'callid_name' => "$firstname $lastname",
244 'callid_nmbr' => $phonenum,
245 'callid_emgr' => $phonenum,
246 'email_address' => $email,
247 'area_code' => $area_code,
248 'srv_code' => $custnum,
249 'date_created' => time2str('%Y-%m-%d %H:%M:%S', time),
250 $self->options_named(keys %subscriber_fields),
251 # allow this to be overridden for suspend
252 ( $dial_policy ? ('dial_policy' => $dial_policy) : () ),
255 if ( $ns->responseCode !~ /^2/ ) {
256 return $ns->responseCode. ' '.
257 join(', ', $self->ns_parse_response( $ns->responseContent ) );
261 # Piece 1.5 - feature creation
263 foreach $feature (split /\s+/, $self->option('features') ) {
265 my $nsf = $self->ns_command( 'PUT', $self->ns_feature($svc_phone, $feature),
266 'control' => 'd', #User Control, disable
267 'expires' => 'never',
272 'activation' => 'now',
275 if ( $nsf->responseCode !~ /^2/ ) {
276 return $nsf->responseCode. ' '.
277 join(', ', $self->ns_parse_response( $ns->responseContent ) );
283 # Piece 2 - sip device creation
286 my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
287 'termination_match' => $self->ns_devicename($svc_phone),
288 'authentication_key'=> $svc_phone->sip_password,
289 'srv_code' => $custnum,
290 $self->options_named(keys %registrar_fields),
293 if ( $ns2->responseCode !~ /^2/ ) {
294 return $ns2->responseCode. ' '.
295 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
299 # Piece 3 - DID mapping to user
302 my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
303 'to_user' => $phonenum,
304 'to_host' => $domain,
305 'plan_description' => "$custnum: $lastname, $firstname", #config?
306 $self->options_named(keys %dialplan_fields),
309 if ( $ns3->responseCode !~ /^2/ ) {
310 return $ns3->responseCode. ' '.
311 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
318 my($self, $svc_phone) = (shift, shift);
320 # do the create steps in reverse order, though I'm not sure it matters
322 my $ns3 = $self->ns_command( 'DELETE', $self->ns_dialplan($svc_phone) );
324 if ( $ns3->responseCode !~ /^2/ ) {
325 return $ns3->responseCode. ' '.
326 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
329 my $ns2 = $self->ns_command( 'DELETE', $self->ns_registrar($svc_phone) );
331 if ( $ns2->responseCode !~ /^2/ ) {
332 return $ns2->responseCode. ' '.
333 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
336 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
338 if ( $ns->responseCode !~ /^2/ ) {
339 return $ns->responseCode. ' '.
340 join(', ', $self->ns_parse_response( $ns->responseContent ) );
347 sub ns_parse_response {
348 my( $self, $content ) = ( shift, shift );
350 #try to screen-scrape something useful
351 tie my %hash, Tie::IxHash;
352 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
353 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
360 my($self, $svc_phone) = (shift, shift);
361 $self->ns_create_or_update($svc_phone);
364 sub _export_replace {
365 my( $self, $new, $old ) = (shift, shift, shift);
366 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
367 if $old->phonenum ne $new->phonenum;
368 $self->_export_insert($new);
372 my( $self, $svc_phone ) = (shift, shift);
374 $self->ns_delete($svc_phone);
377 sub _export_suspend {
378 my( $self, $svc_phone ) = (shift, shift);
379 $self->ns_create_or_update($svc_phone, 'Deny');
382 sub _export_unsuspend {
383 my( $self, $svc_phone ) = (shift, shift);
384 #$self->ns_create_or_update($svc_phone, 'Permit All');
385 $self->_export_insert($svc_phone);
388 sub export_device_insert {
389 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
391 my $domain = $self->ns_domain($svc_phone);
392 my $countrycode = $svc_phone->countrycode;
393 my $phonenum = $svc_phone->phonenum;
395 my $ns = $self->ns_device_command(
396 'PUT', $self->ns_device($svc_phone, $phone_device),
397 'line1_enable' => 'yes',
398 'device1' => $self->ns_devicename($svc_phone),
399 'line1_ext' => $phonenum,
401 #'line2_enable' => 'yes',
409 'brand' => $phone_device->part_device->devicename,
413 if ( $ns->responseCode !~ /^2/ ) {
414 return $ns->responseCode. ' '.
415 join(', ', $self->ns_parse_response( $ns->responseContent ) );
422 sub export_device_delete {
423 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
425 my $ns = $self->ns_device_command(
426 'DELETE', $self->ns_device($svc_phone, $phone_device),
429 if ( $ns->responseCode !~ /^2/ ) {
430 return $ns->responseCode. ' '.
431 join(', ', $self->ns_parse_response( $ns->responseContent ) );
439 sub export_device_replace {
440 my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
441 (shift, shift, shift, shift);
444 $self->export_device_insert( $svc_phone, $new_phone_device );
449 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
450 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
451 # qq!">!. $svc_phone->username. qq!</A>!;
458 my $v = $self->option($_);
459 length($v) ? ($_ => $v) : ()