cbcf98a263383df1010d3f73acd69f16d1681805
[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     my $tenant = {
45       'tenant'   => $svc_x->title,
46       'maxusers' => $svc_x->max_extensions,
47       #others?  will they not clone?
48     };
49
50     @what_to_clone = qw(routes schedules menus queues voiceprompts moh);
51
52     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_create',
53                                             $tenant,
54                                             $self->option('prototype_tenant'),
55                                             \@what_to_clone,
56                                           );
57
58     #use Data::Dumper;
59     #warn Dumper(\$result);
60     $result eq '0' ? '' : 'Thirdlane API failure';
61
62   } elsif ( $svc_x->isa('FS::svc_phone') ) {
63
64     my $result = $self->_thirdlane_command(
65       'asterisk::rpc_did_create',
66       $svc_x->countrycode. $svc_x->phonenum,
67     );
68
69     #use Data::Dumper;
70     #warn Dumper(\$result);
71     $result eq '0' or return 'Thirdlane API failure (rpc_did_create)';
72
73     return '' unless $svc_x->pbxsvc;
74
75     $result = $self->_thirdlane_command(
76       'asterisk::rpc_did_assign',
77       $svc_x->countrycode. $svc_x->phonenum,
78       $svc_x->pbx_title,
79     );
80
81     #use Data::Dumper;
82     #warn Dumper(\$result);
83     $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_assign)';
84
85   } else {
86     die "guru meditation #10: $svc_x is not FS::svc_pbx or FS::svc_phone";
87   }
88
89 }
90
91 sub _export_replace {
92   my($self, $new, $old) = (shift, shift, shift);
93
94 #  #return "can't change username with thirdlane"
95 #  #  if $old->username ne $new->username;
96 #  #return '' unless $old->_password ne $new->_password;
97 #  $err_or_queue = $self->thirdlane_queue( $new->svcnum,
98 #    'replace', $new->username, $new->_password );
99 #  ref($err_or_queue) ? '' : $err_or_queue;
100
101   if ( $new->isa('FS::svc_pbx') ) {
102
103     #need more info on how the API works for changing names.. can it?
104     return "can't change PBX name with thirdlane (yet?)"
105       if $old->title ne $new->title;
106
107     my $tenant = {
108       'tenant'   => $old->title,
109       'maxusers' => $new->max_extensions,
110       #others?  will they not clone?
111     };
112
113     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_update',
114                                             $tenant
115                                           );
116
117     #use Data::Dumper;
118     #warn Dumper(\$result);
119     $result eq '0' ? '' : 'Thirdlane API failure';
120
121   } elsif ( $new->isa('FS::svc_phone') ) {
122
123     return "can't change DID countrycode with thirdlane"
124       if $old->countrycode ne $new->countrycode;
125     return "can't change DID number with thirdlane"
126       if $old->phonenum ne $new->phonenum;
127
128     if ( $old->pbxsvc != $new->pbxsvc ) {
129
130       if ( $old->pbxsvc ) {
131         my $result = $self->_thirdlane_command(
132           'asterisk::rpc_did_unassign',
133           $new->countrycode. $new->phonenum,
134         );
135         $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
136       }
137
138       if ( $new->pbxsvc ) {
139         my $result = $self->_thirdlane_command(
140           'asterisk::rpc_did_assign',
141           $new->countrycode. $new->phonenum,
142           $new->pbx_title,
143         );
144         $result eq '0' or return 'Thirdlane API failure (rpc_did_assign)';
145       }
146
147
148     }
149
150     '';
151
152   } else {
153     die "guru meditation #11: $new is not FS::svc_pbx or FS::svc_phone";
154   }
155
156 }
157
158 sub _export_delete {
159   my($self, $svc_x) = (shift, shift);
160   #my( $self, $svc_something ) = (shift, shift);
161   #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
162   #  'delete', $svc_something->username );
163   #ref($err_or_queue) ? '' : $err_or_queue;
164
165   if ( $svc_x->isa('FS::svc_pbx') ) {
166
167     my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_delete',
168                                             $svc_x->title,
169                                           );
170
171     #use Data::Dumper;
172     #warn Dumper(\$result);
173     $result eq '0' ? '' : 'Thirdlane API failure';
174
175   } elsif ( $svc_x->isa('FS::svc_phone') ) {
176
177     if ( $svc_x->pbxsvc ) {
178       my $result = $self->_thirdlane_command(
179         'asterisk::rpc_did_unassign',
180         $svc_x->countrycode. $svc_x->phonenum,
181       );
182       $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
183     }
184
185     my $result = $self->_thirdlane_command(
186       'asterisk::rpc_did_delete',
187       $svc_x->countrycode. $svc_x->phonenum,
188     );
189     $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_delete)';
190
191   } else {
192     die "guru meditation #11: $svc_x is not FS::svc_pbx or FS::svc_phone";
193   }
194
195 }
196
197 sub _thirdlane_command {
198   my($self, @param) = @_;
199
200   my $url = 'http://'.
201               $self->option('username'). ':'. $self->option('password'). '@'.
202               $self->machine. '/xmlrpc.cgi';
203
204   warn "$me connecting to $url\n"
205     if $self->option('debug');
206   my $conn = Frontier::Client->new( 'url'   => $url,
207                                     'debug' => $self->option('debug'),
208                                   );
209
210   warn "$me sending command: ". join(' ', @param). "\n"
211     if $self->option('debug');
212   $conn->call(@param);
213   
214 }
215
216   #my( $self, $svc_something ) = (shift, shift);
217   #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
218   #  'delete', $svc_something->username );
219   #ref($err_or_queue) ? '' : $err_or_queue;
220
221 #these three are optional
222 ## fallback for svc_acct will change and restore password
223 #sub _export_suspend {
224 #  my( $self, $svc_something ) = (shift, shift);
225 #  $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
226 #    'suspend', $svc_something->username );
227 #  ref($err_or_queue) ? '' : $err_or_queue;
228 #}
229 #
230 #sub _export_unsuspend {
231 #  my( $self, $svc_something ) = (shift, shift);
232 #  $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
233 #    'unsuspend', $svc_something->username );
234 #  ref($err_or_queue) ? '' : $err_or_queue;
235 #}
236 #
237 #sub export_links {
238 #  my($self, $svc_something, $arrayref) = (shift, shift, shift);
239 #  #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_something->username.
240 #  #                 qq!">!. $svc_something->username. qq!</A>!;
241 #  '';
242 #}
243
244 ####
245 #
246 ##a good idea to queue anything that could fail or take any time
247 #sub thirdlane_queue {
248 #  my( $self, $svcnum, $method ) = (shift, shift, shift);
249 #  my $queue = new FS::queue {
250 #    'svcnum' => $svcnum,
251 #    'job'    => "FS::part_export::thirdlane::thirdlane_$method",
252 #  };
253 #  $queue->insert( @_ ) or $queue;
254 #}
255 #
256 #sub thirdlane_insert { #subroutine, not method
257 #  my( $username, $password ) = @_;
258 #  #do things with $username and $password
259 #}
260 #
261 #sub thirdlane_replace { #subroutine, not method
262 #}
263 #
264 #sub thirdlane_delete { #subroutine, not method
265 #  my( $username ) = @_;
266 #  #do things with $username
267 #}
268 #
269 #sub thirdlane_suspend { #subroutine, not method
270 #}
271 #
272 #sub thirdlane_unsuspend { #subroutine, not method
273 #}
274
275 1;