adding export to read mailbox status information, RT#15987
[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 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' },
45   %subscriber_fields,
46   %registrar_fields,
47   %dialplan_fields,
48   'did_countrycode' => { label=>'Use country code in DID destination',
49                          type =>'checkbox' },
50 ;
51
52 %info = (
53   'svc'      => [ 'svc_phone', ], # 'part_device',
54   'desc'     => 'Provision phone numbers to NetSapiens',
55   'options'  => \%options,
56   'notes'    => <<'END'
57 Requires installation of
58 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
59 from CPAN.
60 END
61 );
62
63 sub rebless { shift; }
64
65 sub ns_command {
66   my $self = shift;
67   $self->_ns_command('', @_);
68 }
69
70 sub ns_device_command { 
71   my $self = shift;
72   $self->_ns_command('device_', @_);
73 }
74
75 sub _ns_command {
76   my( $self, $prefix, $method, $command ) = splice(@_,0,4);
77
78   # kludge to curb excessive paranoia in LWP 6.0+
79   local $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
80   eval 'use REST::Client';
81   die $@ if $@;
82
83   my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
84
85   my @args = ( $command );
86
87   if ( $method eq 'PUT' ) {
88     my $content = $ns->buildQuery( { @_ } );
89     $content =~ s/^\?//;
90     push @args, $content;
91   } elsif ( $method eq 'GET' ) {
92     $args[0] .= $ns->buildQuery( { @_ } );
93   }
94
95   warn "$me $method ". $self->option($prefix.'url'). join(', ', @args). "\n"
96     if $self->option('debug');
97
98   my $auth = encode_base64( $self->option($prefix.'login'). ':'.
99                             $self->option($prefix.'password')    );
100   push @args, { 'Authorization' => "Basic $auth" };
101
102   $ns->$method( @args );
103   $ns;
104 }
105
106 sub ns_domain {
107   my($self, $svc_phone) = (shift, shift);
108   $svc_phone->domain || $self->option('domain');
109 }
110
111 sub ns_subscriber {
112   my($self, $svc_phone) = (shift, shift);
113
114   my $domain = $self->ns_domain($svc_phone);
115   my $phonenum = $svc_phone->phonenum;
116
117   "/domains_config/$domain/subscriber_config/$phonenum";
118 }
119
120 sub ns_registrar {
121   my($self, $svc_phone) = (shift, shift);
122
123   $self->ns_subscriber($svc_phone).
124     '/registrar_config/'. $self->ns_devicename($svc_phone);
125 }
126
127 sub ns_devicename {
128   my( $self, $svc_phone ) = (shift, shift);
129
130   my $domain = $self->ns_domain($svc_phone);
131   #my $countrycode = $svc_phone->countrycode;
132   my $phonenum    = $svc_phone->phonenum;
133
134   #"sip:$countrycode$phonenum\@$domain";
135   "sip:$phonenum\@$domain";
136 }
137
138 sub ns_dialplan {
139   my($self, $svc_phone) = (shift, shift);
140
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;
146   }
147
148   #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
149   "/domains_config/admin-only/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*,*,*,*,*,*,*";
150 }
151
152 sub ns_device {
153   my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
154
155   #my $countrycode = $svc_phone->countrycode;
156   #my $phonenum    = $svc_phone->phonenum;
157
158   "/phones_config/". lc($phone_device->mac_addr);
159 }
160
161 sub ns_create_or_update {
162   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
163
164   my $domain = $self->ns_domain($svc_phone);
165   #my $countrycode = $svc_phone->countrycode;
166   my $phonenum    = $svc_phone->phonenum;
167
168   #deal w/unaudited netsapiens services?
169   my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
170
171   my( $firstname, $lastname );
172   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
173     $firstname = $1;
174     $lastname  = $2;
175   } else {
176     $firstname = $cust_main->get('first');
177     $lastname  = $cust_main->get('last');
178   }
179
180   my ($email) = ($cust_main->invoicing_list_emailonly, '');
181   my $custnum = $cust_main->custnum;
182
183   # Piece 1 (already done) - User creation
184   
185   $phonenum =~ /^(\d{3})/;
186   my $area_code = $1;
187
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) : () ),
203   );
204
205   if ( $ns->responseCode !~ /^2/ ) {
206      return $ns->responseCode. ' '.
207             join(', ', $self->ns_parse_response( $ns->responseContent ) );
208   }
209
210   #Piece 2 - sip device creation 
211
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),
217   );
218
219   if ( $ns2->responseCode !~ /^2/ ) {
220      return $ns2->responseCode. ' '.
221             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
222   }
223
224   #Piece 3 - DID mapping to user
225
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),
231   );
232
233   if ( $ns3->responseCode !~ /^2/ ) {
234      return $ns3->responseCode. ' '.
235             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
236   }
237
238   '';
239 }
240
241 sub ns_delete {
242   my($self, $svc_phone) = (shift, shift);
243
244   # do the create steps in reverse order, though I'm not sure it matters
245
246   my $ns3 = $self->ns_command( 'DELETE', $self->ns_dialplan($svc_phone) );
247
248   if ( $ns3->responseCode !~ /^2/ ) {
249      return $ns3->responseCode. ' '.
250             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
251   }
252
253   my $ns2 = $self->ns_command( 'DELETE', $self->ns_registrar($svc_phone) );
254
255   if ( $ns2->responseCode !~ /^2/ ) {
256      return $ns2->responseCode. ' '.
257             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
258   }
259
260   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
261
262   if ( $ns->responseCode !~ /^2/ ) {
263      return $ns->responseCode. ' '.
264             join(', ', $self->ns_parse_response( $ns->responseContent ) );
265   }
266
267   '';
268
269 }
270
271 sub ns_parse_response {
272   my( $self, $content ) = ( shift, shift );
273
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;
278   }
279
280   %hash;
281 }
282
283 sub _export_insert {
284   my($self, $svc_phone) = (shift, shift);
285   $self->ns_create_or_update($svc_phone);
286 }
287
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);
293 }
294
295 sub _export_delete {
296   my( $self, $svc_phone ) = (shift, shift);
297
298   $self->ns_delete($svc_phone);
299 }
300
301 sub _export_suspend {
302   my( $self, $svc_phone ) = (shift, shift);
303   $self->ns_create_or_update($svc_phone, 'Deny');
304 }
305
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);
310 }
311
312 sub export_device_insert {
313   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
314
315   my $domain = $self->ns_domain($svc_phone);
316   my $countrycode = $svc_phone->countrycode;
317   my $phonenum    = $svc_phone->phonenum;
318
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,
324 ,
325       #'line2_enable' => 'yes',
326       #'device2'      =>
327       #'line2_ext'    =>
328
329       #'notes' => 
330       'server'       => 'SiPbx',
331       'domain'       => $domain,
332
333       'brand'        => $phone_device->part_device->devicename,
334       
335   );
336
337   if ( $ns->responseCode !~ /^2/ ) {
338      return $ns->responseCode. ' '.
339             join(', ', $self->ns_parse_response( $ns->responseContent ) );
340   }
341
342   '';
343
344 }
345
346 sub export_device_delete {
347   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
348
349   my $ns = $self->ns_device_command(
350     'DELETE', $self->ns_device($svc_phone, $phone_device),
351   );
352
353   if ( $ns->responseCode !~ /^2/ ) {
354      return $ns->responseCode. ' '.
355             join(', ', $self->ns_parse_response( $ns->responseContent ) );
356   }
357
358   '';
359
360 }
361
362
363 sub export_device_replace {
364   my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
365     (shift, shift, shift, shift);
366
367   #?
368   $self->export_device_insert( $svc_phone, $new_phone_device );
369
370 }
371
372 sub export_links {
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>!;
376   '';
377 }
378
379 sub options_named {
380   my $self = shift;
381   map { 
382         my $v = $self->option($_);
383         length($v) ? ($_ => $v) : ()
384       } @_
385 }
386
387 1;