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