1 package FS::part_export::netsapiens;
3 use vars qw(@ISA $me %info);
7 use Date::Format qw( time2str );
9 @ISA = qw(FS::part_export);
10 $me = '[FS::part_export::netsapiens]';
12 #These export options set default values for the various commands
13 #to create/update objects. Add more options as needed.
15 my %tristate = ( type => 'select', options => [ '', 'yes', 'no' ]);
17 tie my %subscriber_fields, 'Tie::IxHash',
18 'admin_vmail' => { label=>'VMail Prov.', %tristate },
19 'dial_plan' => { label=>'Dial Translation' },
20 'dial_policy' => { label=>'Dial Permission' },
21 'call_limit' => { label=>'Call Limit' },
22 'domain_dir' => { label=>'Dir Lst', %tristate },
25 tie my %registrar_fields, 'Tie::IxHash',
26 'authenticate_register' => { label=>'Authenticate Registration', %tristate },
27 'authentication_realm' => { label=>'Authentication Realm' },
30 tie my %dialplan_fields, 'Tie::IxHash',
31 'responder' => { label=>'Application' }, #this could be nicer
32 'from_name' => { label=>'Source Name Translation' },
33 'from_user' => { label=>'Source User Translation' },
38 'fnr' => 'Forward Not Registered',
39 'fna' => 'Forward No Answer',
40 'fbu' => 'Forward Busy',
41 'dnd' => 'Do-Not-Disturb',
42 'sim' => 'Simultaneous Ring',
45 tie my %options, 'Tie::IxHash',
46 'login' => { label=>'NetSapiens tac2 User API username' },
47 'password' => { label=>'NetSapiens tac2 User API password' },
48 'url' => { label=>'NetSapiens tac2 User URL' },
49 'device_login' => { label=>'NetSapiens tac2 Device API username' },
50 'device_password' => { label=>'NetSapiens tac2 Device API password' },
51 'device_url' => { label=>'NetSapiens tac2 Device URL' },
52 'domain' => { label=>'NetSapiens Domain' },
53 'domain_no_tld' => { label=>'Omit TLD from domains', type=>'checkbox' },
54 'debug' => { label=>'Enable debugging', type=>'checkbox' },
56 'features' => { label => 'Default features',
59 options => [ keys %features ],
60 option_label => sub { $features{$_[0]}; },
64 'did_countrycode' => { label=>'Use country code in DID destination',
69 'svc' => [ 'svc_phone', ], # 'part_device',
70 'desc' => 'Provision phone numbers to NetSapiens',
71 'options' => \%options,
73 Requires installation of
74 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
79 # http://devguide.netsapiens.com/
81 sub rebless { shift; }
85 $self->_ns_command('', @_);
88 sub ns_device_command {
90 $self->_ns_command('device_', @_);
94 my( $self, $prefix, $method, $command ) = splice(@_,0,4);
96 # kludge to curb excessive paranoia in LWP 6.0+
97 local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
98 eval 'use REST::Client';
101 my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
103 my @args = ( $command );
105 if ( $method eq 'PUT' ) {
106 my $content = $ns->buildQuery( { @_ } );
108 push @args, $content;
109 } elsif ( $method eq 'GET' ) {
110 $args[0] .= $ns->buildQuery( { @_ } );
113 warn "$me $method ". $self->option($prefix.'url'). join(', ', @args). "\n"
114 if $self->option('debug');
116 my $auth = encode_base64( $self->option($prefix.'login'). ':'.
117 $self->option($prefix.'password') );
118 push @args, { 'Authorization' => "Basic $auth" };
120 $ns->$method( @args );
125 my($self, $svc_phone) = (shift, shift);
126 my $domain = $svc_phone->domain || $self->option('domain');
128 $domain =~ s/\.\w{2,4}$//
129 if $self->option('domain_no_tld');
135 my($self, $svc_phone) = (shift, shift);
137 my $domain = $self->ns_domain($svc_phone);
138 my $phonenum = $svc_phone->phonenum;
140 "/domains_config/$domain/subscriber_config/$phonenum";
144 my($self, $svc_phone) = (shift, shift);
146 $self->ns_subscriber($svc_phone).
147 '/registrar_config/'. $self->ns_devicename($svc_phone);
151 my($self, $svc_phone, $feature) = (shift, shift, shift);
153 $self->ns_subscriber($svc_phone).
154 "/feature_config/$feature,*,*,*,*";
159 my( $self, $svc_phone ) = (shift, shift);
161 my $domain = $self->ns_domain($svc_phone);
162 #my $countrycode = $svc_phone->countrycode;
163 my $phonenum = $svc_phone->phonenum;
165 #"sip:$countrycode$phonenum\@$domain";
166 "sip:$phonenum\@$domain";
170 my($self, $svc_phone) = (shift, shift);
172 my $countrycode = $svc_phone->countrycode || '1';
173 my $phonenum = $svc_phone->phonenum;
174 # Only in the dialplan destination, nowhere else
175 if ( $self->option('did_countrycode') ) {
176 $phonenum = $countrycode . $phonenum;
179 #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
180 "/domains_config/admin-only/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*,*,*,*,*,*,*";
184 my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
186 #my $countrycode = $svc_phone->countrycode;
187 #my $phonenum = $svc_phone->phonenum;
189 "/phones_config/". lc($phone_device->mac_addr);
192 sub ns_create_or_update {
193 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
195 my $domain = $self->ns_domain($svc_phone);
196 #my $countrycode = $svc_phone->countrycode;
197 my $phonenum = $svc_phone->phonenum;
199 #deal w/unaudited netsapiens services?
200 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
202 my( $firstname, $lastname );
203 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
207 $firstname = $cust_main->get('first');
208 $lastname = $cust_main->get('last');
211 my ($email) = ($cust_main->invoicing_list_emailonly, '');
212 my $custnum = $cust_main->custnum;
215 # Piece 1 (already done) - User creation
218 $phonenum =~ /^(\d{3})/;
221 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
222 'subscriber_login' => $phonenum.'@'.$domain,
223 'firstname' => $firstname,
224 'lastname' => $lastname,
225 'subscriber_pin' => $svc_phone->pin,
226 'callid_name' => "$firstname $lastname",
227 'callid_nmbr' => $phonenum,
228 'callid_emgr' => $phonenum,
229 'email_address' => $email,
230 'area_code' => $area_code,
231 'srv_code' => $custnum,
232 'date_created' => time2str('%Y-%m-%d %H:%M:%S', time),
233 $self->options_named(keys %subscriber_fields),
234 # allow this to be overridden for suspend
235 ( $dial_policy ? ('dial_policy' => $dial_policy) : () ),
238 if ( $ns->responseCode !~ /^2/ ) {
239 return $ns->responseCode. ' '.
240 join(', ', $self->ns_parse_response( $ns->responseContent ) );
244 # Piece 1.5 - feature creation
246 foreach $feature (split /\s+/, $self->option('features') ) {
248 my $nsf = $self->ns_command( 'PUT', $self->ns_feature($feature),
249 'control' => 'd', #User Control, disable
250 'expires' => 'never',
255 'activation' => 'now',
258 if ( $nsf->responseCode !~ /^2/ ) {
259 return $nsf->responseCode. ' '.
260 join(', ', $self->ns_parse_response( $ns->responseContent ) );
266 # Piece 2 - sip device creation
269 my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
270 'termination_match' => $self->ns_devicename($svc_phone),
271 'authentication_key'=> $svc_phone->sip_password,
272 'srv_code' => $custnum,
273 $self->options_named(keys %registrar_fields),
276 if ( $ns2->responseCode !~ /^2/ ) {
277 return $ns2->responseCode. ' '.
278 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
282 # Piece 3 - DID mapping to user
285 my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
286 'to_user' => $phonenum,
287 'to_host' => $domain,
288 'plan_description' => "$custnum: $lastname, $firstname", #config?
289 $self->options_named(keys %dialplan_fields),
292 if ( $ns3->responseCode !~ /^2/ ) {
293 return $ns3->responseCode. ' '.
294 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
301 my($self, $svc_phone) = (shift, shift);
303 # do the create steps in reverse order, though I'm not sure it matters
305 my $ns3 = $self->ns_command( 'DELETE', $self->ns_dialplan($svc_phone) );
307 if ( $ns3->responseCode !~ /^2/ ) {
308 return $ns3->responseCode. ' '.
309 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
312 my $ns2 = $self->ns_command( 'DELETE', $self->ns_registrar($svc_phone) );
314 if ( $ns2->responseCode !~ /^2/ ) {
315 return $ns2->responseCode. ' '.
316 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
319 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
321 if ( $ns->responseCode !~ /^2/ ) {
322 return $ns->responseCode. ' '.
323 join(', ', $self->ns_parse_response( $ns->responseContent ) );
330 sub ns_parse_response {
331 my( $self, $content ) = ( shift, shift );
333 #try to screen-scrape something useful
334 tie my %hash, Tie::IxHash;
335 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
336 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
343 my($self, $svc_phone) = (shift, shift);
344 $self->ns_create_or_update($svc_phone);
347 sub _export_replace {
348 my( $self, $new, $old ) = (shift, shift, shift);
349 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
350 if $old->phonenum ne $new->phonenum;
351 $self->_export_insert($new);
355 my( $self, $svc_phone ) = (shift, shift);
357 $self->ns_delete($svc_phone);
360 sub _export_suspend {
361 my( $self, $svc_phone ) = (shift, shift);
362 $self->ns_create_or_update($svc_phone, 'Deny');
365 sub _export_unsuspend {
366 my( $self, $svc_phone ) = (shift, shift);
367 #$self->ns_create_or_update($svc_phone, 'Permit All');
368 $self->_export_insert($svc_phone);
371 sub export_device_insert {
372 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
374 my $domain = $self->ns_domain($svc_phone);
375 my $countrycode = $svc_phone->countrycode;
376 my $phonenum = $svc_phone->phonenum;
378 my $ns = $self->ns_device_command(
379 'PUT', $self->ns_device($svc_phone, $phone_device),
380 'line1_enable' => 'yes',
381 'device1' => $self->ns_devicename($svc_phone),
382 'line1_ext' => $phonenum,
384 #'line2_enable' => 'yes',
392 'brand' => $phone_device->part_device->devicename,
396 if ( $ns->responseCode !~ /^2/ ) {
397 return $ns->responseCode. ' '.
398 join(', ', $self->ns_parse_response( $ns->responseContent ) );
405 sub export_device_delete {
406 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
408 my $ns = $self->ns_device_command(
409 'DELETE', $self->ns_device($svc_phone, $phone_device),
412 if ( $ns->responseCode !~ /^2/ ) {
413 return $ns->responseCode. ' '.
414 join(', ', $self->ns_parse_response( $ns->responseContent ) );
422 sub export_device_replace {
423 my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
424 (shift, shift, shift, shift);
427 $self->export_device_insert( $svc_phone, $new_phone_device );
432 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
433 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
434 # qq!">!. $svc_phone->username. qq!</A>!;
441 my $v = $self->option($_);
442 length($v) ? ($_ => $v) : ()