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' },
36 tie my %options, 'Tie::IxHash',
37 'login' => { label=>'NetSapiens tac2 User API username' },
38 'password' => { label=>'NetSapiens tac2 User API password' },
39 'url' => { label=>'NetSapiens tac2 User URL' },
40 'device_login' => { label=>'NetSapiens tac2 Device API username' },
41 'device_password' => { label=>'NetSapiens tac2 Device API password' },
42 'device_url' => { label=>'NetSapiens tac2 Device URL' },
43 'domain' => { label=>'NetSapiens Domain' },
44 'debug' => { label=>'Enable debugging', type=>'checkbox' },
48 'did_countrycode' => { label=>'Use country code in DID destination',
53 'svc' => [ 'svc_phone', ], # 'part_device',
54 'desc' => 'Provision phone numbers to NetSapiens',
55 'options' => \%options,
57 Requires installation of
58 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
63 sub rebless { shift; }
67 $self->_ns_command('', @_);
70 sub ns_device_command {
72 $self->_ns_command('device_', @_);
76 my( $self, $prefix, $method, $command ) = splice(@_,0,4);
78 # kludge to curb excessive paranoia in LWP 6.0+
79 local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
80 eval 'use REST::Client';
83 my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
85 my @args = ( $command );
87 if ( $method eq 'PUT' ) {
88 my $content = $ns->buildQuery( { @_ } );
91 } elsif ( $method eq 'GET' ) {
92 $args[0] .= $ns->buildQuery( { @_ } );
95 warn "$me $method ". $self->option($prefix.'url'). join(', ', @args). "\n"
96 if $self->option('debug');
98 my $auth = encode_base64( $self->option($prefix.'login'). ':'.
99 $self->option($prefix.'password') );
100 push @args, { 'Authorization' => "Basic $auth" };
102 $ns->$method( @args );
107 my($self, $svc_phone) = (shift, shift);
108 $svc_phone->domain || $self->option('domain');
112 my($self, $svc_phone) = (shift, shift);
114 my $domain = $self->ns_domain($svc_phone);
115 my $phonenum = $svc_phone->phonenum;
117 "/domains_config/$domain/subscriber_config/$phonenum";
121 my($self, $svc_phone) = (shift, shift);
123 $self->ns_subscriber($svc_phone).
124 '/registrar_config/'. $self->ns_devicename($svc_phone);
128 my( $self, $svc_phone ) = (shift, shift);
130 my $domain = $self->ns_domain($svc_phone);
131 #my $countrycode = $svc_phone->countrycode;
132 my $phonenum = $svc_phone->phonenum;
134 #"sip:$countrycode$phonenum\@$domain";
135 "sip:$phonenum\@$domain";
139 my($self, $svc_phone) = (shift, shift);
141 my $countrycode = $svc_phone->countrycode || '1';
142 my $phonenum = $svc_phone->phonenum;
143 # Only in the dialplan destination, nowhere else
144 if ( $self->option('did_countrycode') ) {
145 $phonenum = $countrycode . $phonenum;
148 #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
149 "/domains_config/admin-only/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*,*,*,*,*,*,*";
153 my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
155 #my $countrycode = $svc_phone->countrycode;
156 #my $phonenum = $svc_phone->phonenum;
158 "/phones_config/". lc($phone_device->mac_addr);
161 sub ns_create_or_update {
162 my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
164 my $domain = $self->ns_domain($svc_phone);
165 #my $countrycode = $svc_phone->countrycode;
166 my $phonenum = $svc_phone->phonenum;
168 #deal w/unaudited netsapiens services?
169 my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
171 my( $firstname, $lastname );
172 if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
176 $firstname = $cust_main->get('first');
177 $lastname = $cust_main->get('last');
180 my ($email) = ($cust_main->invoicing_list_emailonly, '');
181 my $custnum = $cust_main->custnum;
183 # Piece 1 (already done) - User creation
185 $phonenum =~ /^(\d{3})/;
188 my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone),
189 'subscriber_login' => $phonenum.'@'.$domain,
190 'firstname' => $firstname,
191 'lastname' => $lastname,
192 'subscriber_pin' => $svc_phone->pin,
193 'callid_name' => "$firstname $lastname",
194 'callid_nmbr' => $phonenum,
195 'callid_emgr' => $phonenum,
196 'email_address' => $email,
197 'area_code' => $area_code,
198 'srv_code' => $custnum,
199 'date_created' => time2str('%Y-%m-%d %H:%M:%S', time),
200 $self->options_named(keys %subscriber_fields),
201 # allow this to be overridden for suspend
202 ( $dial_policy ? ('dial_policy' => $dial_policy) : () ),
205 if ( $ns->responseCode !~ /^2/ ) {
206 return $ns->responseCode. ' '.
207 join(', ', $self->ns_parse_response( $ns->responseContent ) );
210 #Piece 2 - sip device creation
212 my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
213 'termination_match' => $self->ns_devicename($svc_phone),
214 'authentication_key'=> $svc_phone->sip_password,
215 'srv_code' => $custnum,
216 $self->options_named(keys %registrar_fields),
219 if ( $ns2->responseCode !~ /^2/ ) {
220 return $ns2->responseCode. ' '.
221 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
224 #Piece 3 - DID mapping to user
226 my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
227 'to_user' => $phonenum,
228 'to_host' => $domain,
229 'plan_description' => "$custnum: $lastname, $firstname", #config?
230 $self->options_named(keys %dialplan_fields),
233 if ( $ns3->responseCode !~ /^2/ ) {
234 return $ns3->responseCode. ' '.
235 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
242 my($self, $svc_phone) = (shift, shift);
244 # do the create steps in reverse order, though I'm not sure it matters
246 my $ns3 = $self->ns_command( 'DELETE', $self->ns_dialplan($svc_phone) );
248 if ( $ns3->responseCode !~ /^2/ ) {
249 return $ns3->responseCode. ' '.
250 join(', ', $self->ns_parse_response( $ns3->responseContent ) );
253 my $ns2 = $self->ns_command( 'DELETE', $self->ns_registrar($svc_phone) );
255 if ( $ns2->responseCode !~ /^2/ ) {
256 return $ns2->responseCode. ' '.
257 join(', ', $self->ns_parse_response( $ns2->responseContent ) );
260 my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
262 if ( $ns->responseCode !~ /^2/ ) {
263 return $ns->responseCode. ' '.
264 join(', ', $self->ns_parse_response( $ns->responseContent ) );
271 sub ns_parse_response {
272 my( $self, $content ) = ( shift, shift );
274 #try to screen-scrape something useful
275 tie my %hash, Tie::IxHash;
276 while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
277 ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
284 my($self, $svc_phone) = (shift, shift);
285 $self->ns_create_or_update($svc_phone);
288 sub _export_replace {
289 my( $self, $new, $old ) = (shift, shift, shift);
290 return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
291 if $old->phonenum ne $new->phonenum;
292 $self->_export_insert($new);
296 my( $self, $svc_phone ) = (shift, shift);
298 $self->ns_delete($svc_phone);
301 sub _export_suspend {
302 my( $self, $svc_phone ) = (shift, shift);
303 $self->ns_create_or_update($svc_phone, 'Deny');
306 sub _export_unsuspend {
307 my( $self, $svc_phone ) = (shift, shift);
308 #$self->ns_create_or_update($svc_phone, 'Permit All');
309 $self->_export_insert($svc_phone);
312 sub export_device_insert {
313 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
315 my $domain = $self->ns_domain($svc_phone);
316 my $countrycode = $svc_phone->countrycode;
317 my $phonenum = $svc_phone->phonenum;
319 my $ns = $self->ns_device_command(
320 'PUT', $self->ns_device($svc_phone, $phone_device),
321 'line1_enable' => 'yes',
322 'device1' => $self->ns_devicename($svc_phone),
323 'line1_ext' => $phonenum,
325 #'line2_enable' => 'yes',
333 'brand' => $phone_device->part_device->devicename,
337 if ( $ns->responseCode !~ /^2/ ) {
338 return $ns->responseCode. ' '.
339 join(', ', $self->ns_parse_response( $ns->responseContent ) );
346 sub export_device_delete {
347 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
349 my $ns = $self->ns_device_command(
350 'DELETE', $self->ns_device($svc_phone, $phone_device),
353 if ( $ns->responseCode !~ /^2/ ) {
354 return $ns->responseCode. ' '.
355 join(', ', $self->ns_parse_response( $ns->responseContent ) );
363 sub export_device_replace {
364 my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
365 (shift, shift, shift, shift);
368 $self->export_device_insert( $svc_phone, $new_phone_device );
373 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
374 #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
375 # qq!">!. $svc_phone->username. qq!</A>!;
382 my $v = $self->option($_);
383 length($v) ? ($_ => $v) : ()