1 package FS::part_export::thirdlane;
3 use base qw( FS::part_export );
5 use vars qw(%info $me);
9 $me = '['.__PACKAGE__.']';
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',
21 # 'textarea_option' => { label => 'Textarea option description',
23 # default => 'Default text.',
28 'svc' => [qw( svc_pbx svc_phone )],
30 'Export tenants and DIDs to Thirdlane PBX manager',
31 'options' => \%options,
33 Exports tenants and DIDs to Thirdlane PBX manager using the XML-RPC API.
37 sub rebless { shift; }
40 my($self, $svc_x) = (shift, shift);
42 if ( $svc_x->isa('FS::svc_pbx') ) {
45 'tenant' => $svc_x->title,
46 'maxusers' => $svc_x->max_extensions,
47 #others? will they not clone?
50 @what_to_clone = qw(routes schedules menus queues voiceprompts moh);
52 my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_create',
54 $self->option('prototype_tenant'),
59 #warn Dumper(\$result);
60 $result eq '0' ? '' : 'Thirdlane API failure';
62 } elsif ( $svc_x->isa('FS::svc_phone') ) {
64 my $result = $self->_thirdlane_command(
65 'asterisk::rpc_did_create',
66 $svc_x->countrycode. $svc_x->phonenum,
70 #warn Dumper(\$result);
71 $result eq '0' or return 'Thirdlane API failure (rpc_did_create)';
73 return '' unless $svc_x->pbxsvc;
75 $result = $self->_thirdlane_command(
76 'asterisk::rpc_did_assign',
77 $svc_x->countrycode. $svc_x->phonenum,
82 #warn Dumper(\$result);
83 $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_assign)';
86 die "guru meditation #10: $svc_x is not FS::svc_pbx or FS::svc_phone";
92 my($self, $new, $old) = (shift, shift, shift);
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;
101 if ( $new->isa('FS::svc_pbx') ) {
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;
108 'tenant' => $old->title,
109 'maxusers' => $new->max_extensions,
110 #others? will they not clone?
113 my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_update',
118 #warn Dumper(\$result);
119 $result eq '0' ? '' : 'Thirdlane API failure';
121 } elsif ( $new->isa('FS::svc_phone') ) {
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;
128 if ( $old->pbxsvc != $new->pbxsvc ) {
130 if ( $old->pbxsvc ) {
131 my $result = $self->_thirdlane_command(
132 'asterisk::rpc_did_unassign',
133 $new->countrycode. $new->phonenum,
135 $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
138 if ( $new->pbxsvc ) {
139 my $result = $self->_thirdlane_command(
140 'asterisk::rpc_did_assign',
141 $new->countrycode. $new->phonenum,
144 $result eq '0' or return 'Thirdlane API failure (rpc_did_assign)';
153 die "guru meditation #11: $new is not FS::svc_pbx or FS::svc_phone";
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;
165 if ( $svc_x->isa('FS::svc_pbx') ) {
167 my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_delete',
172 #warn Dumper(\$result);
173 $result eq '0' ? '' : 'Thirdlane API failure';
175 } elsif ( $svc_x->isa('FS::svc_phone') ) {
177 if ( $svc_x->pbxsvc ) {
178 my $result = $self->_thirdlane_command(
179 'asterisk::rpc_did_unassign',
180 $svc_x->countrycode. $svc_x->phonenum,
182 $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
185 my $result = $self->_thirdlane_command(
186 'asterisk::rpc_did_delete',
187 $svc_x->countrycode. $svc_x->phonenum,
189 $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_delete)';
192 die "guru meditation #11: $svc_x is not FS::svc_pbx or FS::svc_phone";
197 sub _thirdlane_command {
198 my($self, @param) = @_;
201 $self->option('username'). ':'. $self->option('password'). '@'.
202 $self->machine. '/xmlrpc.cgi';
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'),
210 warn "$me sending command: ". join(' ', @param). "\n"
211 if $self->option('debug');
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;
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;
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;
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>!;
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",
253 # $queue->insert( @_ ) or $queue;
256 #sub thirdlane_insert { #subroutine, not method
257 # my( $username, $password ) = @_;
258 # #do things with $username and $password
261 #sub thirdlane_replace { #subroutine, not method
264 #sub thirdlane_delete { #subroutine, not method
265 # my( $username ) = @_;
266 # #do things with $username
269 #sub thirdlane_suspend { #subroutine, not method
272 #sub thirdlane_unsuspend { #subroutine, not method