add default feature list to netsapiens export, RT#17319
[freeside.git] / FS / FS / part_export / netsapiens.pm
1 package FS::part_export::netsapiens;
2
3 use vars qw(@ISA $me %info);
4 use MIME::Base64;
5 use Tie::IxHash;
6 use FS::part_export;
7 use Date::Format qw( time2str );
8
9 @ISA = qw(FS::part_export);
10 $me = '[FS::part_export::netsapiens]';
11
12 #These export options set default values for the various commands
13 #to create/update objects.  Add more options as needed.
14
15 my %tristate = ( type => 'select', options => [ '', 'yes', 'no' ]);
16
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 },
23 ;
24
25 tie my %registrar_fields, 'Tie::IxHash',
26   'authenticate_register' => { label=>'Authenticate Registration', %tristate },
27   'authentication_realm'  => { label=>'Authentication Realm' },
28 ;
29
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' },
34 ;
35
36 my %features = (
37   'for' => 'Forward',
38   'fnr' => 'Forward Not Registered',
39   'fna' => 'Forward No Answer',
40   'fbu' => 'Forward Busy',
41   'dnd' => 'Do-Not-Disturb',
42   'sim' => 'Simultaneous Ring',
43 );
44
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' },
55   %subscriber_fields,
56   'features'        => { label        => 'Default features',
57                          type         => 'select',
58                          multiple     => 1,
59                          options      => [ keys %features ],
60                          option_label => sub { $features{$_[0]}; },
61                        },
62   %registrar_fields,
63   %dialplan_fields,
64   'did_countrycode' => { label=>'Use country code in DID destination',
65                          type =>'checkbox' },
66 ;
67
68 %info = (
69   'svc'      => [ 'svc_phone', ], # 'part_device',
70   'desc'     => 'Provision phone numbers to NetSapiens',
71   'options'  => \%options,
72   'notes'    => <<'END'
73 Requires installation of
74 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
75 from CPAN.
76 END
77 );
78
79 # http://devguide.netsapiens.com/
80
81 sub rebless { shift; }
82
83 sub ns_command {
84   my $self = shift;
85   $self->_ns_command('', @_);
86 }
87
88 sub ns_device_command { 
89   my $self = shift;
90   $self->_ns_command('device_', @_);
91 }
92
93 sub _ns_command {
94   my( $self, $prefix, $method, $command ) = splice(@_,0,4);
95
96   # kludge to curb excessive paranoia in LWP 6.0+
97   local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
98   eval 'use REST::Client';
99   die $@ if $@;
100
101   my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
102
103   my @args = ( $command );
104
105   if ( $method eq 'PUT' ) {
106     my $content = $ns->buildQuery( { @_ } );
107     $content =~ s/^\?//;
108     push @args, $content;
109   } elsif ( $method eq 'GET' ) {
110     $args[0] .= $ns->buildQuery( { @_ } );
111   }
112
113   warn "$me $method ". $self->option($prefix.'url'). join(', ', @args). "\n"
114     if $self->option('debug');
115
116   my $auth = encode_base64( $self->option($prefix.'login'). ':'.
117                             $self->option($prefix.'password')    );
118   push @args, { 'Authorization' => "Basic $auth" };
119
120   $ns->$method( @args );
121   $ns;
122 }
123
124 sub ns_domain {
125   my($self, $svc_phone) = (shift, shift);
126   my $domain = $svc_phone->domain || $self->option('domain');
127
128   $domain =~ s/\.\w{2,4}$//
129     if $self->option('domain_no_tld');
130   
131   $domain;
132 }
133
134 sub ns_subscriber {
135   my($self, $svc_phone) = (shift, shift);
136
137   my $domain = $self->ns_domain($svc_phone);
138   my $phonenum = $svc_phone->phonenum;
139
140   "/domains_config/$domain/subscriber_config/$phonenum";
141 }
142
143 sub ns_registrar {
144   my($self, $svc_phone) = (shift, shift);
145
146   $self->ns_subscriber($svc_phone).
147     '/registrar_config/'. $self->ns_devicename($svc_phone);
148 }
149
150 sub ns_feature {
151   my($self, $svc_phone, $feature) = (shift, shift, shift);
152
153   $self->ns_subscriber($svc_phone).
154     "/feature_config/$feature,*,*,*,*";
155
156 }
157
158 sub ns_devicename {
159   my( $self, $svc_phone ) = (shift, shift);
160
161   my $domain = $self->ns_domain($svc_phone);
162   #my $countrycode = $svc_phone->countrycode;
163   my $phonenum    = $svc_phone->phonenum;
164
165   #"sip:$countrycode$phonenum\@$domain";
166   "sip:$phonenum\@$domain";
167 }
168
169 sub ns_dialplan {
170   my($self, $svc_phone) = (shift, shift);
171
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;
177   }
178
179   #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
180   "/domains_config/admin-only/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*,*,*,*,*,*,*";
181 }
182
183 sub ns_device {
184   my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
185
186   #my $countrycode = $svc_phone->countrycode;
187   #my $phonenum    = $svc_phone->phonenum;
188
189   "/phones_config/". lc($phone_device->mac_addr);
190 }
191
192 sub ns_create_or_update {
193   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
194
195   my $domain = $self->ns_domain($svc_phone);
196   #my $countrycode = $svc_phone->countrycode;
197   my $phonenum    = $svc_phone->phonenum;
198
199   #deal w/unaudited netsapiens services?
200   my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
201
202   my( $firstname, $lastname );
203   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
204     $firstname = $1;
205     $lastname  = $2;
206   } else {
207     $firstname = $cust_main->get('first');
208     $lastname  = $cust_main->get('last');
209   }
210
211   my ($email) = ($cust_main->invoicing_list_emailonly, '');
212   my $custnum = $cust_main->custnum;
213
214   ###
215   # Piece 1 (already done) - User creation
216   ###
217   
218   $phonenum =~ /^(\d{3})/;
219   my $area_code = $1;
220
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) : () ),
236   );
237
238   if ( $ns->responseCode !~ /^2/ ) {
239      return $ns->responseCode. ' '.
240             join(', ', $self->ns_parse_response( $ns->responseContent ) );
241   }
242
243   ###
244   # Piece 1.5 - feature creation
245   ###
246   foreach $feature (split /\s+/, $self->option('features') ) {
247
248     my $nsf = $self->ns_command( 'PUT', $self->ns_feature($svc_phone, $feature),
249       'control'    => 'd', #User Control, disable
250       'expires'    => 'never',
251       #'ts'         => '', #?
252       #'parameters' => '',
253       'hour_match' => '*',
254       'time_frame' => '*',
255       'activation' => 'now',
256     );
257
258     if ( $nsf->responseCode !~ /^2/ ) {
259        return $nsf->responseCode. ' '.
260               join(', ', $self->ns_parse_response( $ns->responseContent ) );
261     }
262
263   }
264
265   ###
266   # Piece 2 - sip device creation 
267   ###
268
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),
274   );
275
276   if ( $ns2->responseCode !~ /^2/ ) {
277      return $ns2->responseCode. ' '.
278             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
279   }
280
281   ###
282   # Piece 3 - DID mapping to user
283   ###
284
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),
290   );
291
292   if ( $ns3->responseCode !~ /^2/ ) {
293      return $ns3->responseCode. ' '.
294             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
295   }
296
297   '';
298 }
299
300 sub ns_delete {
301   my($self, $svc_phone) = (shift, shift);
302
303   # do the create steps in reverse order, though I'm not sure it matters
304
305   my $ns3 = $self->ns_command( 'DELETE', $self->ns_dialplan($svc_phone) );
306
307   if ( $ns3->responseCode !~ /^2/ ) {
308      return $ns3->responseCode. ' '.
309             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
310   }
311
312   my $ns2 = $self->ns_command( 'DELETE', $self->ns_registrar($svc_phone) );
313
314   if ( $ns2->responseCode !~ /^2/ ) {
315      return $ns2->responseCode. ' '.
316             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
317   }
318
319   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
320
321   if ( $ns->responseCode !~ /^2/ ) {
322      return $ns->responseCode. ' '.
323             join(', ', $self->ns_parse_response( $ns->responseContent ) );
324   }
325
326   '';
327
328 }
329
330 sub ns_parse_response {
331   my( $self, $content ) = ( shift, shift );
332
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;
337   }
338
339   %hash;
340 }
341
342 sub _export_insert {
343   my($self, $svc_phone) = (shift, shift);
344   $self->ns_create_or_update($svc_phone);
345 }
346
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);
352 }
353
354 sub _export_delete {
355   my( $self, $svc_phone ) = (shift, shift);
356
357   $self->ns_delete($svc_phone);
358 }
359
360 sub _export_suspend {
361   my( $self, $svc_phone ) = (shift, shift);
362   $self->ns_create_or_update($svc_phone, 'Deny');
363 }
364
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);
369 }
370
371 sub export_device_insert {
372   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
373
374   my $domain = $self->ns_domain($svc_phone);
375   my $countrycode = $svc_phone->countrycode;
376   my $phonenum    = $svc_phone->phonenum;
377
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,
383 ,
384       #'line2_enable' => 'yes',
385       #'device2'      =>
386       #'line2_ext'    =>
387
388       #'notes' => 
389       'server'       => 'SiPbx',
390       'domain'       => $domain,
391
392       'brand'        => $phone_device->part_device->devicename,
393       
394   );
395
396   if ( $ns->responseCode !~ /^2/ ) {
397      return $ns->responseCode. ' '.
398             join(', ', $self->ns_parse_response( $ns->responseContent ) );
399   }
400
401   '';
402
403 }
404
405 sub export_device_delete {
406   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
407
408   my $ns = $self->ns_device_command(
409     'DELETE', $self->ns_device($svc_phone, $phone_device),
410   );
411
412   if ( $ns->responseCode !~ /^2/ ) {
413      return $ns->responseCode. ' '.
414             join(', ', $self->ns_parse_response( $ns->responseContent ) );
415   }
416
417   '';
418
419 }
420
421
422 sub export_device_replace {
423   my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
424     (shift, shift, shift, shift);
425
426   #?
427   $self->export_device_insert( $svc_phone, $new_phone_device );
428
429 }
430
431 sub export_links {
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>!;
435   '';
436 }
437
438 sub options_named {
439   my $self = shift;
440   map { 
441         my $v = $self->option($_);
442         length($v) ? ($_ => $v) : ()
443       } @_
444 }
445
446 1;