last nits on netsapiens export, RT#5226
[freeside.git] / FS / FS / part_export / netsapiens.pm
1 package FS::part_export::netsapiens;
2
3 use vars qw(@ISA $me %info);
4 use URI;
5 use MIME::Base64;
6 use Tie::IxHash;
7 use FS::part_export;
8
9 @ISA = qw(FS::part_export);
10 $me = '[FS::part_export::netsapiens]';
11
12 tie my %options, 'Tie::IxHash',
13   'login'           => { label=>'NetSapiens tac2 User API username' },
14   'password'        => { label=>'NetSapiens tac2 User API password' },
15   'url'             => { label=>'NetSapiens tac2 User URL' },
16   'device_login'    => { label=>'NetSapiens tac2 Device API username' },
17   'device_password' => { label=>'NetSapiens tac2 Device API password' },
18   'device_url'      => { label=>'NetSapiens tac2 Device URL' },
19   'domain'          => { label=>'NetSapiens Domain' },
20   'debug'           => { label=>'Enable debugging', type=>'checkbox' },
21 ;
22
23 %info = (
24   'svc'      => 'svc_phone',
25   'desc'     => 'Provision phone numbers to NetSapiens',
26   'options'  => \%options,
27   'notes'    => <<'END'
28 Requires installation of
29 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
30 from CPAN.
31 END
32 );
33
34 sub rebless { shift; }
35
36 sub ns_command {
37   my $self = shift;
38   $self->_ns_command('', @_);
39 }
40
41 sub ns_device_command { 
42   my $self = shift;
43   $self->_ns_command('device_', @_);
44 }
45
46 sub _ns_command {
47   my( $self, $prefix, $method, $command ) = splice(@_,0,4);
48
49   eval 'use REST::Client';
50   die $@ if $@;
51
52   my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
53
54   my @args = ( $command );
55
56   if ( $method eq 'PUT' ) {
57     my $content = $ns->buildQuery( { @_ } );
58     $content =~ s/^\?//;
59     push @args, $content;
60   } elsif ( $method eq 'GET' ) {
61     $args[0] .= $ns->buildQuery( { @_ } );
62   }
63
64   warn "$me $method ". $self->option($prefix.'url').
65        " $command ". join(', ', @_). "\n"
66     if $self->option('debug');
67
68   my $auth = encode_base64( $self->option($prefix.'login'). ':'.
69                             $self->option($prefix.'password')    );
70   push @args, { 'Authorization' => "Basic $auth" };
71
72   $ns->$method( @args );
73   $ns;
74 }
75
76 sub ns_subscriber {
77   my($self, $svc_phone) = (shift, shift);
78
79   my $domain = $self->option('domain');
80   my $phonenum = $svc_phone->phonenum;
81
82   "/domains_config/$domain/subscriber_config/$phonenum";
83 }
84
85 sub ns_registrar {
86   my($self, $svc_phone) = (shift, shift);
87
88   $self->ns_subscriber($svc_phone).
89     '/registrar_config/'. $self->ns_devicename($svc_phone);
90 }
91
92 sub ns_devicename {
93   my( $self, $svc_phone ) = (shift, shift);
94
95   my $domain = $self->option('domain');
96   #my $countrycode = $svc_phone->countrycode;
97   my $phonenum    = $svc_phone->phonenum;
98
99   #"sip:$countrycode$phonenum\@$domain";
100   "sip:$phonenum\@$domain";
101 }
102
103 sub ns_dialplan {
104   my($self, $svc_phone) = (shift, shift);
105
106   #my $countrycode = $svc_phone->countrycode;
107   my $phonenum    = $svc_phone->phonenum;
108
109   #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
110   "/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*"
111 }
112
113 sub ns_device {
114   my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
115
116   #my $countrycode = $svc_phone->countrycode;
117   #my $phonenum    = $svc_phone->phonenum;
118
119   "/phones_config/". lc($phone_device->mac_addr);
120 }
121
122 sub ns_create_or_update {
123   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
124
125   my $domain = $self->option('domain');
126   #my $countrycode = $svc_phone->countrycode;
127   my $phonenum    = $svc_phone->phonenum;
128
129   my( $firstname, $lastname );
130   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
131     $firstname = $1;
132     $lastname  = $2;
133   } else {
134     #deal w/unaudited netsapiens services?
135     my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
136     $firstname = $cust_main->get('first');
137     $lastname  = $cust_main->get('last');
138   }
139
140   # Piece 1 (already done) - User creation
141
142   my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone), 
143     'subscriber_login' => $phonenum.'@'.$domain,
144     'firstname'        => $firstname,
145     'lastname'         => $lastname,
146     'subscriber_pin'   => $svc_phone->pin,
147     'dial_plan'        => 'Default', #config?
148     'dial_policy'      => $dial_policy,
149   );
150
151   if ( $ns->responseCode !~ /^2/ ) {
152      return $ns->responseCode. ' '.
153             join(', ', $self->ns_parse_response( $ns->responseContent ) );
154   }
155
156   #Piece 2 - sip device creation 
157
158   my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
159     'termination_match' => $self->ns_devicename($svc_phone)
160   );
161
162   if ( $ns2->responseCode !~ /^2/ ) {
163      return $ns2->responseCode. ' '.
164             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
165   }
166
167   #Piece 3 - DID mapping to user
168
169   my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
170     'to_user' => $phonenum,
171     'to_host' => $domain,
172   );
173
174   if ( $ns3->responseCode !~ /^2/ ) {
175      return $ns3->responseCode. ' '.
176             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
177   }
178
179   '';
180 }
181
182 sub ns_delete {
183   my($self, $svc_phone) = (shift, shift);
184
185   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
186
187   #delete other things?
188
189   if ( $ns->responseCode !~ /^2/ ) {
190      return $ns->responseCode. ' '.
191             join(', ', $self->ns_parse_response( $ns->responseContent ) );
192   }
193
194   '';
195
196 }
197
198 sub ns_parse_response {
199   my( $self, $content ) = ( shift, shift );
200
201   #try to screen-scrape something useful
202   tie my %hash, Tie::IxHash;
203   while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
204     ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
205   }
206
207   %hash;
208 }
209
210 sub _export_insert {
211   my($self, $svc_phone) = (shift, shift);
212   $self->ns_create_or_update($svc_phone, 'Permit All');
213 }
214
215 sub _export_replace {
216   my( $self, $new, $old ) = (shift, shift, shift);
217   return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
218     if $old->phonenum ne $new->phonenum;
219   $self->_export_insert($new);
220 }
221
222 sub _export_delete {
223   my( $self, $svc_phone ) = (shift, shift);
224
225   $self->ns_delete($svc_phone);
226 }
227
228 sub _export_suspend {
229   my( $self, $svc_phone ) = (shift, shift);
230   $self->ns_create_or_update($svc_phone, 'Deny');
231 }
232
233 sub _export_unsuspend {
234   my( $self, $svc_phone ) = (shift, shift);
235   #$self->ns_create_or_update($svc_phone, 'Permit All');
236   $self->_export_insert($svc_phone);
237 }
238
239 sub export_device_insert {
240   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
241
242   #my $domain = $self->option('domain');
243   my $countrycode = $svc_phone->countrycode;
244   my $phonenum    = $svc_phone->phonenum;
245
246   my $device = $self->ns_devicename($svc_phone);
247
248   my $ns = $self->ns_device_command(
249     'PUT', $self->ns_device($svc_phone, $phone_device),
250       'line1_enable' => 'yes',
251       'device1'      => $self->ns_devicename($svc_phone),
252       'line1_ext'    => $phonenum,
253 ,
254       #'line2_enable' => 'yes',
255       #'device2'      =>
256       #'line2_ext'    =>
257
258       #'notes' => 
259       'server'       => 'SiPbx',
260       'domain'       => $self->option('domain'),
261
262       'brand'        => $phone_device->part_device->devicename,
263       
264   );
265
266   if ( $ns->responseCode !~ /^2/ ) {
267      return $ns->responseCode. ' '.
268             join(', ', $self->ns_parse_response( $ns->responseContent ) );
269   }
270
271   '';
272
273 }
274
275 sub export_device_delete {
276   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
277
278   my $ns = $self->ns_device_command(
279     'DELETE', $self->ns_device($svc_phone, $phone_device),
280   );
281
282   if ( $ns->responseCode !~ /^2/ ) {
283      return $ns->responseCode. ' '.
284             join(', ', $self->ns_parse_response( $ns->responseContent ) );
285   }
286
287   '';
288
289 }
290
291
292 sub export_device_replace {
293   my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
294     (shift, shift, shift, shift);
295
296   #?
297   $self->export_device_insert( $svc_phone, $new_phone_device );
298
299 }
300
301 sub export_links {
302   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
303   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
304   #                 qq!">!. $svc_phone->username. qq!</A>!;
305   '';
306 }
307
308 1;