import torrus 1.0.9
[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', ], # 'part_device',
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'). join(', ', @args). "\n"
65     if $self->option('debug');
66
67   my $auth = encode_base64( $self->option($prefix.'login'). ':'.
68                             $self->option($prefix.'password')    );
69   push @args, { 'Authorization' => "Basic $auth" };
70
71   $ns->$method( @args );
72   $ns;
73 }
74
75 sub ns_domain {
76   my($self, $svc_phone) = (shift, shift);
77   $svc_phone->domain || $self->option('domain');
78 }
79
80 sub ns_subscriber {
81   my($self, $svc_phone) = (shift, shift);
82
83   my $domain = $self->ns_domain($svc_phone);
84   my $phonenum = $svc_phone->phonenum;
85
86   "/domains_config/$domain/subscriber_config/$phonenum";
87 }
88
89 sub ns_registrar {
90   my($self, $svc_phone) = (shift, shift);
91
92   $self->ns_subscriber($svc_phone).
93     '/registrar_config/'. $self->ns_devicename($svc_phone);
94 }
95
96 sub ns_devicename {
97   my( $self, $svc_phone ) = (shift, shift);
98
99   my $domain = $self->ns_domain($svc_phone);
100   #my $countrycode = $svc_phone->countrycode;
101   my $phonenum    = $svc_phone->phonenum;
102
103   #"sip:$countrycode$phonenum\@$domain";
104   "sip:$phonenum\@$domain";
105 }
106
107 sub ns_dialplan {
108   my($self, $svc_phone) = (shift, shift);
109
110   #my $countrycode = $svc_phone->countrycode;
111   my $phonenum    = $svc_phone->phonenum;
112
113   #"/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum\@*"
114   "/dialplans/DID+Table/dialplan_config/sip:$phonenum\@*"
115 }
116
117 sub ns_device {
118   my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
119
120   #my $countrycode = $svc_phone->countrycode;
121   #my $phonenum    = $svc_phone->phonenum;
122
123   "/phones_config/". lc($phone_device->mac_addr);
124 }
125
126 sub ns_create_or_update {
127   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
128
129   my $domain = $self->ns_domain($svc_phone);
130   #my $countrycode = $svc_phone->countrycode;
131   my $phonenum    = $svc_phone->phonenum;
132
133   my( $firstname, $lastname );
134   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
135     $firstname = $1;
136     $lastname  = $2;
137   } else {
138     #deal w/unaudited netsapiens services?
139     my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
140     $firstname = $cust_main->get('first');
141     $lastname  = $cust_main->get('last');
142   }
143
144   # Piece 1 (already done) - User creation
145
146   my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone), 
147     'subscriber_login' => $phonenum.'@'.$domain,
148     'firstname'        => $firstname,
149     'lastname'         => $lastname,
150     'subscriber_pin'   => $svc_phone->pin,
151     'dial_plan'        => 'Default', #config?
152     'dial_policy'      => $dial_policy,
153   );
154
155   if ( $ns->responseCode !~ /^2/ ) {
156      return $ns->responseCode. ' '.
157             join(', ', $self->ns_parse_response( $ns->responseContent ) );
158   }
159
160   #Piece 2 - sip device creation 
161
162   my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
163     'termination_match' => $self->ns_devicename($svc_phone)
164   );
165
166   if ( $ns2->responseCode !~ /^2/ ) {
167      return $ns2->responseCode. ' '.
168             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
169   }
170
171   #Piece 3 - DID mapping to user
172
173   my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
174     'to_user' => $phonenum,
175     'to_host' => $domain,
176   );
177
178   if ( $ns3->responseCode !~ /^2/ ) {
179      return $ns3->responseCode. ' '.
180             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
181   }
182
183   '';
184 }
185
186 sub ns_delete {
187   my($self, $svc_phone) = (shift, shift);
188
189   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
190
191   #delete other things?
192
193   if ( $ns->responseCode !~ /^2/ ) {
194      return $ns->responseCode. ' '.
195             join(', ', $self->ns_parse_response( $ns->responseContent ) );
196   }
197
198   '';
199
200 }
201
202 sub ns_parse_response {
203   my( $self, $content ) = ( shift, shift );
204
205   #try to screen-scrape something useful
206   tie my %hash, Tie::IxHash;
207   while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
208     ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
209   }
210
211   %hash;
212 }
213
214 sub _export_insert {
215   my($self, $svc_phone) = (shift, shift);
216   $self->ns_create_or_update($svc_phone, 'Permit All');
217 }
218
219 sub _export_replace {
220   my( $self, $new, $old ) = (shift, shift, shift);
221   return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
222     if $old->phonenum ne $new->phonenum;
223   $self->_export_insert($new);
224 }
225
226 sub _export_delete {
227   my( $self, $svc_phone ) = (shift, shift);
228
229   $self->ns_delete($svc_phone);
230 }
231
232 sub _export_suspend {
233   my( $self, $svc_phone ) = (shift, shift);
234   $self->ns_create_or_update($svc_phone, 'Deny');
235 }
236
237 sub _export_unsuspend {
238   my( $self, $svc_phone ) = (shift, shift);
239   #$self->ns_create_or_update($svc_phone, 'Permit All');
240   $self->_export_insert($svc_phone);
241 }
242
243 sub export_device_insert {
244   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
245
246   my $domain = $self->ns_domain($svc_phone);
247   my $countrycode = $svc_phone->countrycode;
248   my $phonenum    = $svc_phone->phonenum;
249
250   my $device = $self->ns_devicename($svc_phone);
251
252   my $ns = $self->ns_device_command(
253     'PUT', $self->ns_device($svc_phone, $phone_device),
254       'line1_enable' => 'yes',
255       'device1'      => $self->ns_devicename($svc_phone),
256       'line1_ext'    => $phonenum,
257 ,
258       #'line2_enable' => 'yes',
259       #'device2'      =>
260       #'line2_ext'    =>
261
262       #'notes' => 
263       'server'       => 'SiPbx',
264       'domain'       => $domain,
265
266       'brand'        => $phone_device->part_device->devicename,
267       
268   );
269
270   if ( $ns->responseCode !~ /^2/ ) {
271      return $ns->responseCode. ' '.
272             join(', ', $self->ns_parse_response( $ns->responseContent ) );
273   }
274
275   '';
276
277 }
278
279 sub export_device_delete {
280   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
281
282   my $ns = $self->ns_device_command(
283     'DELETE', $self->ns_device($svc_phone, $phone_device),
284   );
285
286   if ( $ns->responseCode !~ /^2/ ) {
287      return $ns->responseCode. ' '.
288             join(', ', $self->ns_parse_response( $ns->responseContent ) );
289   }
290
291   '';
292
293 }
294
295
296 sub export_device_replace {
297   my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
298     (shift, shift, shift, shift);
299
300   #?
301   $self->export_device_insert( $svc_phone, $new_phone_device );
302
303 }
304
305 sub export_links {
306   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
307   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
308   #                 qq!">!. $svc_phone->username. qq!</A>!;
309   '';
310 }
311
312 1;