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