svc_pbx.title uniqueness (kludgy) and force to alphanumeric+space and 19 char max...
[freeside.git] / FS / FS / part_export / thirdlane.pm
1 package FS::part_export::thirdlane;
2
3 use base qw( FS::part_export );
4
5 use vars qw(%info $me);
6 use Tie::IxHash;
7 use Frontier::Client;
8
9 $me = '['.__PACKAGE__.']';
10
11 tie my %options, 'Tie::IxHash',
12   #'server'           => { label => 'Thirdlane server name or IP address', },
13   'username'         => { label => 'Thirdlane username', },
14   'password'         => { label => 'Thirdlane password', },
15   'prototype_tenant' => { label => 'Prototype tenant name', },
16   'debug'            => { label => 'Checkbox label', type => 'checkbox' },
17 #  'select_option'   => { label   => 'Select option description',
18 #                         type    => 'select', options=>[qw(chocolate vanilla)],
19 #                         default => 'vanilla',
20 #                       },
21 #  'textarea_option' => { label   => 'Textarea option description',
22 #                         type    => 'textarea',
23 #                         default => 'Default text.',
24 #                      },
25 ;
26
27 %info = (
28   'svc'      => [qw( svc_pbx svc_phone )],
29   'desc'     =>
30     'Export tenants and DIDs to Thirdlane PBX manager',
31   'options'  => \%options,
32   'notes'    => <<'END'
33 Exports tenants and DIDs to Thirdlane PBX manager using the XML-RPC API.
34 END
35 );
36
37 sub rebless { shift; }
38
39 sub _export_insert {
40   my($self, $svc_x) = (shift, shift);
41
42   if ( $svc_x->isa('FS::svc_pbx') ) {
43
44     return 'Name must be 19 characters or less (thirdlane restriction?)'
45       if length($svc_x->title) > 19;
46
47     return 'Name must consist of alphanumerics and spaces only (thirdlane restriction?)'
48       unless $svc_x->title =~ /^[\w\s]+$/;
49
50     my $tenant = {
51       'tenant'   => $svc_x->title,
52       'maxusers' => $svc_x->max_extensions,
53       #others?  will they not clone?
54     };
55
56     @what_to_clone = qw(routes schedules menus queues voiceprompts moh);
57
58     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_create',
59                                             $tenant,
60                                             $self->option('prototype_tenant'),
61                                             \@what_to_clone,
62                                           );
63
64     #use Data::Dumper;
65     #warn Dumper(\$result);
66     $result eq '0' ? '' : 'Thirdlane API failure';
67
68   } elsif ( $svc_x->isa('FS::svc_phone') ) {
69
70     my $result = $self->_thirdlane_command(
71       'asterisk::rpc_did_create',
72       $svc_x->countrycode. $svc_x->phonenum,
73     );
74
75     #use Data::Dumper;
76     #warn Dumper(\$result);
77     $result eq '0' or return 'Thirdlane API failure (rpc_did_create)';
78
79     return '' unless $svc_x->pbxsvc;
80
81     $result = $self->_thirdlane_command(
82       'asterisk::rpc_did_assign',
83       $svc_x->countrycode. $svc_x->phonenum,
84       $svc_x->pbx_title,
85     );
86
87     #use Data::Dumper;
88     #warn Dumper(\$result);
89     $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_assign)';
90
91   } else {
92     die "guru meditation #10: $svc_x is not FS::svc_pbx or FS::svc_phone";
93   }
94
95 }
96
97 sub _export_replace {
98   my($self, $new, $old) = (shift, shift, shift);
99
100 #  #return "can't change username with thirdlane"
101 #  #  if $old->username ne $new->username;
102 #  #return '' unless $old->_password ne $new->_password;
103 #  $err_or_queue = $self->thirdlane_queue( $new->svcnum,
104 #    'replace', $new->username, $new->_password );
105 #  ref($err_or_queue) ? '' : $err_or_queue;
106
107   if ( $new->isa('FS::svc_pbx') ) {
108
109     #need more info on how the API works for changing names.. can it?
110     return "can't change PBX name with thirdlane (yet?)"
111       if $old->title ne $new->title;
112
113     my $tenant = {
114       'tenant'   => $old->title,
115       'maxusers' => $new->max_extensions,
116       #others?  will they not clone?
117     };
118
119     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_update',
120                                             $tenant
121                                           );
122
123     #use Data::Dumper;
124     #warn Dumper(\$result);
125     $result eq '0' ? '' : 'Thirdlane API failure';
126
127   } elsif ( $new->isa('FS::svc_phone') ) {
128
129     return "can't change DID countrycode with thirdlane"
130       if $old->countrycode ne $new->countrycode;
131     return "can't change DID number with thirdlane"
132       if $old->phonenum ne $new->phonenum;
133
134     if ( $old->pbxsvc != $new->pbxsvc ) {
135
136       if ( $old->pbxsvc ) {
137         my $result = $self->_thirdlane_command(
138           'asterisk::rpc_did_unassign',
139           $new->countrycode. $new->phonenum,
140         );
141         $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
142       }
143
144       if ( $new->pbxsvc ) {
145         my $result = $self->_thirdlane_command(
146           'asterisk::rpc_did_assign',
147           $new->countrycode. $new->phonenum,
148           $new->pbx_title,
149         );
150         $result eq '0' or return 'Thirdlane API failure (rpc_did_assign)';
151       }
152
153
154     }
155
156     '';
157
158   } else {
159     die "guru meditation #11: $new is not FS::svc_pbx or FS::svc_phone";
160   }
161
162 }
163
164 sub _export_delete {
165   my($self, $svc_x) = (shift, shift);
166   #my( $self, $svc_something ) = (shift, shift);
167   #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
168   #  'delete', $svc_something->username );
169   #ref($err_or_queue) ? '' : $err_or_queue;
170
171   if ( $svc_x->isa('FS::svc_pbx') ) {
172
173     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_delete',
174                                             $svc_x->title,
175                                           );
176
177     #use Data::Dumper;
178     #warn Dumper(\$result);
179     $result eq '0' ? '' : 'Thirdlane API failure';
180
181   } elsif ( $svc_x->isa('FS::svc_phone') ) {
182
183     if ( $svc_x->pbxsvc ) {
184       my $result = $self->_thirdlane_command(
185         'asterisk::rpc_did_unassign',
186         $svc_x->countrycode. $svc_x->phonenum,
187       );
188       $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
189     }
190
191     my $result = $self->_thirdlane_command(
192       'asterisk::rpc_did_delete',
193       $svc_x->countrycode. $svc_x->phonenum,
194     );
195     $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_delete)';
196
197   } else {
198     die "guru meditation #11: $svc_x is not FS::svc_pbx or FS::svc_phone";
199   }
200
201 }
202
203 sub _thirdlane_command {
204   my($self, @param) = @_;
205
206   my $url = 'http://'.
207               $self->option('username'). ':'. $self->option('password'). '@'.
208               $self->machine. '/xmlrpc.cgi';
209
210   warn "$me connecting to $url\n"
211     if $self->option('debug');
212   my $conn = Frontier::Client->new( 'url'   => $url,
213                                     'debug' => $self->option('debug'),
214                                   );
215
216   warn "$me sending command: ". join(' ', @param). "\n"
217     if $self->option('debug');
218   $conn->call(@param);
219   
220 }
221
222   #my( $self, $svc_something ) = (shift, shift);
223   #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
224   #  'delete', $svc_something->username );
225   #ref($err_or_queue) ? '' : $err_or_queue;
226
227 #these three are optional
228 ## fallback for svc_acct will change and restore password
229 #sub _export_suspend {
230 #  my( $self, $svc_something ) = (shift, shift);
231 #  $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
232 #    'suspend', $svc_something->username );
233 #  ref($err_or_queue) ? '' : $err_or_queue;
234 #}
235 #
236 #sub _export_unsuspend {
237 #  my( $self, $svc_something ) = (shift, shift);
238 #  $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
239 #    'unsuspend', $svc_something->username );
240 #  ref($err_or_queue) ? '' : $err_or_queue;
241 #}
242 #
243 #sub export_links {
244 #  my($self, $svc_something, $arrayref) = (shift, shift, shift);
245 #  #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_something->username.
246 #  #                 qq!">!. $svc_something->username. qq!</A>!;
247 #  '';
248 #}
249
250 ####
251 #
252 ##a good idea to queue anything that could fail or take any time
253 #sub thirdlane_queue {
254 #  my( $self, $svcnum, $method ) = (shift, shift, shift);
255 #  my $queue = new FS::queue {
256 #    'svcnum' => $svcnum,
257 #    'job'    => "FS::part_export::thirdlane::thirdlane_$method",
258 #  };
259 #  $queue->insert( @_ ) or $queue;
260 #}
261 #
262 #sub thirdlane_insert { #subroutine, not method
263 #  my( $username, $password ) = @_;
264 #  #do things with $username and $password
265 #}
266 #
267 #sub thirdlane_replace { #subroutine, not method
268 #}
269 #
270 #sub thirdlane_delete { #subroutine, not method
271 #  my( $username ) = @_;
272 #  #do things with $username
273 #}
274 #
275 #sub thirdlane_suspend { #subroutine, not method
276 #}
277 #
278 #sub thirdlane_unsuspend { #subroutine, not method
279 #}
280
281 1;