Netsapiens export; check URLs for validity. (#14919)
[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 use Regexp::Common qw/URI/;
9
10 @ISA = qw(FS::part_export);
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 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' },
56   %subscriber_fields,
57   'features'        => { label        => 'Default features',
58                          type         => 'select',
59                          multiple     => 1,
60                          options      => [ keys %features ],
61                          option_label => sub { $features{$_[0]}; },
62                        },
63   %registrar_fields,
64   %dialplan_fields,
65   'did_countrycode' => { label=>'Use country code in DID destination',
66                          type =>'checkbox' },
67 ;
68
69 %info = (
70   'svc'      => [ 'svc_phone', ], # 'part_device',
71   'desc'     => 'Provision phone numbers to NetSapiens',
72   'options'  => \%options,
73   'notes'    => <<'END'
74 Requires installation of
75 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
76 from CPAN.
77 END
78 );
79
80 # http://devguide.netsapiens.com/
81
82 sub rebless { shift; }
83
84
85 sub check_options {
86         my ($self, $options) = @_;
87         
88         my $rex = qr/$RE{URI}{HTTP}{-scheme => qr|https?|}/;                    # match any "http:" or "https:" URL
89         
90         for my $key (qw/url device_url/) {
91                 if ($$options{$key} && ($$options{$key} !~ $rex)) {
92                                 return "Invalid (URL): " . $$options{$key};
93                 }
94         }
95         return '';
96 }
97
98
99
100 sub ns_command {
101   my $self = shift;
102   $self->_ns_command('', @_);
103 }
104
105 sub ns_device_command { 
106   my $self = shift;
107   $self->_ns_command('device_', @_);
108 }
109
110 sub _ns_command {
111   my( $self, $prefix, $method, $command ) = splice(@_,0,4);
112
113   # kludge to curb excessive paranoia in LWP 6.0+
114   local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
115   eval 'use REST::Client';
116   die $@ if $@;
117
118   my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
119
120   my @args = ( $command );
121
122   if ( $method eq 'PUT' ) {
123     my $content = $ns->buildQuery( { @_ } );
124     $content =~ s/^\?//;
125     push @args, $content;
126   } elsif ( $method eq 'GET' ) {
127     $args[0] .= $ns->buildQuery( { @_ } );
128   }
129
130   warn "$me $method ". $self->option($prefix.'url'). join(', ', @args). "\n"
131     if $self->option('debug');
132
133   my $auth = encode_base64( $self->option($prefix.'login'). ':'.
134                             $self->option($prefix.'password')    );
135   push @args, { 'Authorization' => "Basic $auth" };
136
137   $ns->$method( @args );
138   $ns;
139 }
140
141 sub ns_domain {
142   my($self, $svc_phone) = (shift, shift);
143   my $domain = $svc_phone->domain || $self->option('domain');
144
145   $domain =~ s/\.\w{2,4}$//
146     if $self->option('domain_no_tld');
147   
148   $domain;
149 }
150
151 sub ns_subscriber {
152   my($self, $svc_phone) = (shift, shift);
153
154   my $domain = $self->ns_domain($svc_phone);
155   my $phonenum = $svc_phone->phonenum;
156
157   "/domains_config/$domain/subscriber_config/$phonenum";
158 }
159
160 sub ns_registrar {
161   my($self, $svc_phone) = (shift, shift);
162
163   $self->ns_subscriber($svc_phone).
164     '/registrar_config/'. $self->ns_devicename($svc_phone);
165 }
166
167 sub ns_feature {
168   my($self, $svc_phone, $feature) = (shift, shift, shift);
169
170   $self->ns_subscriber($svc_phone).
171     "/feature_config/$feature,*,*,*,*";
172
173 }
174
175 sub ns_devicename {
176   my( $self, $svc_phone ) = (shift, shift);
177
178   my $domain = $self->ns_domain($svc_phone);
179   #my $countrycode = $svc_phone->countrycode;
180   my $phonenum    = $svc_phone->phonenum;
181
182   #"sip:$countrycode$phonenum\@$domain";
183   "sip:$phonenum\@$domain";
184 }
185
186 sub ns_dialplan {
187   my($self, $svc_phone) = (shift, shift);
188
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;
194   }
195
196   #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
197   "/domains_config/admin-only/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*,*,*,*,*,*,*";
198 }
199
200 sub ns_device {
201   my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
202
203   #my $countrycode = $svc_phone->countrycode;
204   #my $phonenum    = $svc_phone->phonenum;
205
206   "/phones_config/". lc($phone_device->mac_addr);
207 }
208
209 sub ns_create_or_update {
210   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
211
212   my $domain = $self->ns_domain($svc_phone);
213   #my $countrycode = $svc_phone->countrycode;
214   my $phonenum    = $svc_phone->phonenum;
215
216   #deal w/unaudited netsapiens services?
217   my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
218
219   my( $firstname, $lastname );
220   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
221     $firstname = $1;
222     $lastname  = $2;
223   } else {
224     $firstname = $cust_main->get('first');
225     $lastname  = $cust_main->get('last');
226   }
227
228   my ($email) = ($cust_main->invoicing_list_emailonly, '');
229   my $custnum = $cust_main->custnum;
230
231   ###
232   # Piece 1 (already done) - User creation
233   ###
234   
235   $phonenum =~ /^(\d{3})/;
236   my $area_code = $1;
237
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) : () ),
253   );
254
255   if ( $ns->responseCode !~ /^2/ ) {
256      return $ns->responseCode. ' '.
257             join(', ', $self->ns_parse_response( $ns->responseContent ) );
258   }
259
260   ###
261   # Piece 1.5 - feature creation
262   ###
263   foreach $feature (split /\s+/, $self->option('features') ) {
264
265     my $nsf = $self->ns_command( 'PUT', $self->ns_feature($svc_phone, $feature),
266       'control'    => 'd', #User Control, disable
267       'expires'    => 'never',
268       #'ts'         => '', #?
269       #'parameters' => '',
270       'hour_match' => '*',
271       'time_frame' => '*',
272       'activation' => 'now',
273     );
274
275     if ( $nsf->responseCode !~ /^2/ ) {
276        return $nsf->responseCode. ' '.
277               join(', ', $self->ns_parse_response( $ns->responseContent ) );
278     }
279
280   }
281
282   ###
283   # Piece 2 - sip device creation 
284   ###
285
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),
291   );
292
293   if ( $ns2->responseCode !~ /^2/ ) {
294      return $ns2->responseCode. ' '.
295             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
296   }
297
298   ###
299   # Piece 3 - DID mapping to user
300   ###
301
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),
307   );
308
309   if ( $ns3->responseCode !~ /^2/ ) {
310      return $ns3->responseCode. ' '.
311             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
312   }
313
314   '';
315 }
316
317 sub ns_delete {
318   my($self, $svc_phone) = (shift, shift);
319
320   # do the create steps in reverse order, though I'm not sure it matters
321
322   my $ns3 = $self->ns_command( 'DELETE', $self->ns_dialplan($svc_phone) );
323
324   if ( $ns3->responseCode !~ /^2/ ) {
325      return $ns3->responseCode. ' '.
326             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
327   }
328
329   my $ns2 = $self->ns_command( 'DELETE', $self->ns_registrar($svc_phone) );
330
331   if ( $ns2->responseCode !~ /^2/ ) {
332      return $ns2->responseCode. ' '.
333             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
334   }
335
336   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
337
338   if ( $ns->responseCode !~ /^2/ ) {
339      return $ns->responseCode. ' '.
340             join(', ', $self->ns_parse_response( $ns->responseContent ) );
341   }
342
343   '';
344
345 }
346
347 sub ns_parse_response {
348   my( $self, $content ) = ( shift, shift );
349
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;
354   }
355
356   %hash;
357 }
358
359 sub _export_insert {
360   my($self, $svc_phone) = (shift, shift);
361   $self->ns_create_or_update($svc_phone);
362 }
363
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);
369 }
370
371 sub _export_delete {
372   my( $self, $svc_phone ) = (shift, shift);
373
374   $self->ns_delete($svc_phone);
375 }
376
377 sub _export_suspend {
378   my( $self, $svc_phone ) = (shift, shift);
379   $self->ns_create_or_update($svc_phone, 'Deny');
380 }
381
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);
386 }
387
388 sub export_device_insert {
389   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
390
391   my $domain = $self->ns_domain($svc_phone);
392   my $countrycode = $svc_phone->countrycode;
393   my $phonenum    = $svc_phone->phonenum;
394
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,
400 ,
401       #'line2_enable' => 'yes',
402       #'device2'      =>
403       #'line2_ext'    =>
404
405       #'notes' => 
406       'server'       => 'SiPbx',
407       'domain'       => $domain,
408
409       'brand'        => $phone_device->part_device->devicename,
410       
411   );
412
413   if ( $ns->responseCode !~ /^2/ ) {
414      return $ns->responseCode. ' '.
415             join(', ', $self->ns_parse_response( $ns->responseContent ) );
416   }
417
418   '';
419
420 }
421
422 sub export_device_delete {
423   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
424
425   my $ns = $self->ns_device_command(
426     'DELETE', $self->ns_device($svc_phone, $phone_device),
427   );
428
429   if ( $ns->responseCode !~ /^2/ ) {
430      return $ns->responseCode. ' '.
431             join(', ', $self->ns_parse_response( $ns->responseContent ) );
432   }
433
434   '';
435
436 }
437
438
439 sub export_device_replace {
440   my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
441     (shift, shift, shift, shift);
442
443   #?
444   $self->export_device_insert( $svc_phone, $new_phone_device );
445
446 }
447
448 sub export_links {
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>!;
452   '';
453 }
454
455 sub options_named {
456   my $self = shift;
457   map { 
458         my $v = $self->option($_);
459         length($v) ? ($_ => $v) : ()
460       } @_
461 }
462
463 1;