netsapiens integration: deb dep on REST::Client rather than throw a runtime error...
[freeside.git] / FS / FS / part_export / netsapiens.pm
1 package FS::part_export::netsapiens;
2 use base qw( FS::part_export );
3
4 use vars qw( $me %info );
5 use MIME::Base64;
6 use Tie::IxHash;
7 use Date::Format qw( time2str );
8 use Regexp::Common qw( URI );
9 use REST::Client;
10
11 $me = '[FS::part_export::netsapiens]';
12
13 #These export options set default values for the various commands
14 #to create/update objects.  Add more options as needed.
15
16 my %tristate = ( type => 'select', options => [ '', 'yes', 'no' ]);
17
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 },
24 ;
25
26 tie my %registrar_fields, 'Tie::IxHash',
27   'authenticate_register' => { label=>'Authenticate Registration', %tristate },
28   'authentication_realm'  => { label=>'Authentication Realm' },
29 ;
30
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' },
35 ;
36
37 my %features = (
38   'for' => 'Forward',
39   'fnr' => 'Forward Not Registered',
40   'fna' => 'Forward No Answer',
41   'fbu' => 'Forward Busy',
42   'dnd' => 'Do-Not-Disturb',
43   'sim' => 'Simultaneous Ring',
44 );
45
46 my %feature_param = (
47   'dnd' => 'n/a',
48   'sim' => '$phonenum',
49 );
50
51 tie my %options, 'Tie::IxHash',
52   'login'           => { label=>'NetSapiens tac2 User API username' },
53   'password'        => { label=>'NetSapiens tac2 User API password' },
54   'url'             => { label=>'NetSapiens tac2 User URL' },
55   'device_login'    => { label=>'NetSapiens tac2 Device API username' },
56   'device_password' => { label=>'NetSapiens tac2 Device API password' },
57   'device_url'      => { label=>'NetSapiens tac2 Device URL' },
58   'domain'          => { label=>'NetSapiens Domain' },
59   'domain_no_tld'   => { label=>'Omit TLD from domains', type=>'checkbox' },
60   'debug'           => { label=>'Enable debugging', type=>'checkbox' },
61   %subscriber_fields,
62   'features'        => { label        => 'Default features',
63                          type         => 'select',
64                          multiple     => 1,
65                          options      => [ keys %features ],
66                          option_label => sub { $features{$_[0]}; },
67                        },
68   %registrar_fields,
69   %dialplan_fields,
70   'did_countrycode' => { label=>'Use country code in DID destination',
71                          type =>'checkbox' },
72 ;
73
74 %info = (
75   'svc'        => [qw( svc_phone part_device )],
76   'desc'       => 'Provision phone numbers to NetSapiens',
77   'options'    => \%options,
78   'no_machine' => 1,
79   'notes'      => <<'END'
80 END
81 );
82
83 # http://devguide.netsapiens.com/
84
85 sub rebless { shift; }
86
87
88 sub check_options {
89   my ($self, $options) = @_;
90         
91   my $rex = qr/$RE{URI}{HTTP}{-scheme => qr|https?|}/;                  # match any "http:" or "https:" URL
92         
93   for my $key (qw/url device_url/) {
94     if ($$options{$key} && ($$options{$key} !~ $rex)) {
95       return "Invalid (URL): " . $$options{$key};
96     }
97   }
98   return '';
99 }
100
101
102
103 sub ns_command {
104   my $self = shift;
105   $self->_ns_command('', @_);
106 }
107
108 sub ns_device_command { 
109   my $self = shift;
110   $self->_ns_command('device_', @_);
111 }
112
113 sub _ns_command {
114   my( $self, $prefix, $method, $command ) = splice(@_,0,4);
115
116   # kludge to curb excessive paranoia in LWP 6.0+
117   local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
118
119   my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
120
121   my @args = ( $command );
122
123   if ( $method eq 'PUT' ) {
124     my $content = $ns->buildQuery( { @_ } );
125     $content =~ s/^\?//;
126     push @args, $content;
127   } elsif ( $method eq 'GET' ) {
128     $args[0] .= $ns->buildQuery( { @_ } );
129   }
130
131   warn "$me $method ". $self->option($prefix.'url'). join(', ', @args). "\n"
132     if $self->option('debug');
133
134   my $auth = encode_base64( $self->option($prefix.'login'). ':'.
135                             $self->option($prefix.'password')    );
136   push @args, { 'Authorization' => "Basic $auth" };
137
138   $ns->$method( @args );
139   $ns;
140 }
141
142 sub ns_domain {
143   my($self, $svc_phone) = (shift, shift);
144   my $domain = $svc_phone->domain || $self->option('domain');
145
146   $domain =~ s/\.\w{2,4}$//
147     if $self->option('domain_no_tld');
148   
149   $domain;
150 }
151
152 sub ns_subscriber {
153   my($self, $svc_phone) = (shift, shift);
154
155   my $domain = $self->ns_domain($svc_phone);
156   my $phonenum = $svc_phone->phonenum;
157
158   "/domains_config/$domain/subscriber_config/$phonenum";
159 }
160
161 sub ns_registrar {
162   my($self, $svc_phone) = (shift, shift);
163
164   $self->ns_subscriber($svc_phone).
165     '/registrar_config/'. $self->ns_devicename($svc_phone);
166 }
167
168 sub ns_feature {
169   my($self, $svc_phone, $feature) = (shift, shift, shift);
170
171   $self->ns_subscriber($svc_phone).
172     "/feature_config/$feature,*,*,*,*";
173
174 }
175
176 sub ns_devicename {
177   my( $self, $svc_phone ) = (shift, shift);
178
179   my $domain = $self->ns_domain($svc_phone);
180   #my $countrycode = $svc_phone->countrycode;
181   my $phonenum    = $svc_phone->phonenum;
182
183   #"sip:$countrycode$phonenum\@$domain";
184   "sip:$phonenum\@$domain";
185 }
186
187 sub ns_dialplan {
188   my($self, $svc_phone) = (shift, shift);
189
190   my $countrycode = $svc_phone->countrycode || '1';
191   my $phonenum    = $svc_phone->phonenum;
192   # Only in the dialplan destination, nowhere else
193   if ( $self->option('did_countrycode') ) {
194     $phonenum = $countrycode . $phonenum;
195   }
196
197   #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
198   "/domains_config/admin-only/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*,*,*,*,*,*,*";
199 }
200
201 sub ns_device {
202   my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
203
204   #my $countrycode = $svc_phone->countrycode;
205   #my $phonenum    = $svc_phone->phonenum;
206
207   "/phones_config/". lc($phone_device->mac_addr);
208 }
209
210 sub ns_create_or_update {
211   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
212
213   my $domain = $self->ns_domain($svc_phone);
214   #my $countrycode = $svc_phone->countrycode;
215   my $phonenum    = $svc_phone->phonenum;
216
217   #deal w/unaudited netsapiens services?
218   my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
219
220   my( $firstname, $lastname );
221   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
222     $firstname = $1;
223     $lastname  = $2;
224   } else {
225     $firstname = $cust_main->get('first');
226     $lastname  = $cust_main->get('last');
227   }
228
229   my ($email) = ($cust_main->invoicing_list_emailonly, '');
230   my $custnum = $cust_main->custnum;
231
232   ###
233   # Piece 1 (already done) - User creation
234   ###
235   
236   $phonenum =~ /^(\d{3})/;
237   my $area_code = $1;
238
239   my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone), 
240     'subscriber_login' => $phonenum.'@'.$domain,
241     'firstname'        => $firstname,
242     'lastname'         => $lastname,
243     'subscriber_pin'   => $svc_phone->pin,
244     'callid_name'      => "$firstname $lastname",
245     'callid_nmbr'      => $phonenum,
246     'callid_emgr'      => $phonenum,
247     'email_address'    => $email,
248     'area_code'        => $area_code,
249     'srv_code'         => $custnum,
250     'date_created'     => time2str('%Y-%m-%d %H:%M:%S', time),
251     $self->options_named(keys %subscriber_fields),
252     # allow this to be overridden for suspend
253     ( $dial_policy ? ('dial_policy' => $dial_policy) : () ),
254   );
255
256   if ( $ns->responseCode !~ /^2/ ) {
257      return $ns->responseCode. ' '.
258             join(', ', $self->ns_parse_response( $ns->responseContent ) );
259   }
260
261   ###
262   # Piece 1.5 - feature creation
263   ###
264   foreach $feature (split /\s+/, $self->option('features') ) {
265
266     my $param= exists($feature_param{$feature}) ? $feature_param{$feature} : '';
267     $param = $phonenum if $param eq '$phonenum';
268
269     my $nsf = $self->ns_command( 'PUT', $self->ns_feature($svc_phone, $feature),
270       'control'    => 'd', #User Control, disable
271       'expires'    => 'never',
272       #'ts'         => '', #?
273       'parameters' => $param,
274       'hour_match' => '*',
275       'time_frame' => '*',
276       'activation' => 'now',
277     );
278
279     if ( $nsf->responseCode !~ /^2/ ) {
280        return $nsf->responseCode. ' '.
281               join(', ', $self->ns_parse_response( $ns->responseContent ) );
282     }
283
284   }
285
286   ###
287   # Piece 2 - sip device creation 
288   ###
289
290   my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
291     'termination_match' => $self->ns_devicename($svc_phone),
292     'authentication_key'=> $svc_phone->sip_password,
293     'srv_code'          => $custnum,
294     $self->options_named(keys %registrar_fields),
295   );
296
297   if ( $ns2->responseCode !~ /^2/ ) {
298      return $ns2->responseCode. ' '.
299             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
300   }
301
302   ###
303   # Piece 3 - DID mapping to user
304   ###
305
306   my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
307     'to_user' => $phonenum,
308     'to_host' => $domain,
309     'plan_description' => "$custnum: $lastname, $firstname", #config?
310     $self->options_named(keys %dialplan_fields),
311   );
312
313   if ( $ns3->responseCode !~ /^2/ ) {
314      return $ns3->responseCode. ' '.
315             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
316   }
317
318   '';
319 }
320
321 sub ns_delete {
322   my($self, $svc_phone) = (shift, shift);
323
324   # do the create steps in reverse order, though I'm not sure it matters
325
326   my $ns3 = $self->ns_command( 'DELETE', $self->ns_dialplan($svc_phone) );
327
328   if ( $ns3->responseCode !~ /^2/ ) {
329      return $ns3->responseCode. ' '.
330             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
331   }
332
333   my $ns2 = $self->ns_command( 'DELETE', $self->ns_registrar($svc_phone) );
334
335   if ( $ns2->responseCode !~ /^2/ ) {
336      return $ns2->responseCode. ' '.
337             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
338   }
339
340   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
341
342   if ( $ns->responseCode !~ /^2/ ) {
343      return $ns->responseCode. ' '.
344             join(', ', $self->ns_parse_response( $ns->responseContent ) );
345   }
346
347   '';
348
349 }
350
351 sub ns_parse_response {
352   my( $self, $content ) = ( shift, shift );
353
354   #try to screen-scrape something useful
355   tie my %hash, Tie::IxHash;
356   while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
357     ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
358   }
359
360   %hash;
361 }
362
363 sub _export_insert {
364   my($self, $svc_phone) = (shift, shift);
365   $self->ns_create_or_update($svc_phone);
366 }
367
368 sub _export_replace {
369   my( $self, $new, $old ) = (shift, shift, shift);
370   return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
371     if $old->phonenum ne $new->phonenum;
372   $self->_export_insert($new);
373 }
374
375 sub _export_delete {
376   my( $self, $svc_phone ) = (shift, shift);
377
378   $self->ns_delete($svc_phone);
379 }
380
381 sub _export_suspend {
382   my( $self, $svc_phone ) = (shift, shift);
383   $self->ns_create_or_update($svc_phone, 'Deny');
384 }
385
386 sub _export_unsuspend {
387   my( $self, $svc_phone ) = (shift, shift);
388   #$self->ns_create_or_update($svc_phone, 'Permit All');
389   $self->_export_insert($svc_phone);
390 }
391
392 sub export_device_insert {
393   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
394
395   my $domain = $self->ns_domain($svc_phone);
396   my $countrycode = $svc_phone->countrycode;
397   my $phonenum    = $svc_phone->phonenum;
398
399   my $ns = $self->ns_device_command(
400     'PUT', $self->ns_device($svc_phone, $phone_device),
401       'line1_enable' => 'yes',
402       'device1'      => $self->ns_devicename($svc_phone),
403       'line1_ext'    => $phonenum,
404 ,
405       #'line2_enable' => 'yes',
406       #'device2'      =>
407       #'line2_ext'    =>
408
409       #'notes' => 
410       'server'       => 'SiPbx',
411       'domain'       => $domain,
412
413       'brand'        => $phone_device->part_device->devicename,
414       
415   );
416
417   if ( $ns->responseCode !~ /^2/ ) {
418      return $ns->responseCode. ' '.
419             join(', ', $self->ns_parse_response( $ns->responseContent ) );
420   }
421
422   '';
423
424 }
425
426 sub export_device_delete {
427   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
428
429   my $ns = $self->ns_device_command(
430     'DELETE', $self->ns_device($svc_phone, $phone_device),
431   );
432
433   if ( $ns->responseCode !~ /^2/ ) {
434      return $ns->responseCode. ' '.
435             join(', ', $self->ns_parse_response( $ns->responseContent ) );
436   }
437
438   '';
439
440 }
441
442
443 sub export_device_replace {
444   my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
445     (shift, shift, shift, shift);
446
447   #?
448   $self->export_device_insert( $svc_phone, $new_phone_device );
449
450 }
451
452 sub export_links {
453   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
454   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
455   #                 qq!">!. $svc_phone->username. qq!</A>!;
456   '';
457 }
458
459 sub options_named {
460   my $self = shift;
461   map { 
462         my $v = $self->option($_);
463         length($v) ? ($_ => $v) : ()
464       } @_
465 }
466
467 1;