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') ) {
44 return 'Name must be 19 characters or less (thirdlane restriction?)'
45 if length($svc_x->title) > 19;
47 return 'Name must consist of alphanumerics and spaces only (thirdlane restriction?)'
48 unless $svc_x->title =~ /^[\w\s]+$/;
51 'tenant' => $svc_x->title,
52 'maxusers' => $svc_x->max_extensions,
53 #others? will they not clone?
56 @what_to_clone = qw(routes schedules menus queues voiceprompts moh);
58 my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_create',
60 $self->option('prototype_tenant'),
65 #warn Dumper(\$result);
66 $result eq '0' ? '' : 'Thirdlane API failure';
68 } elsif ( $svc_x->isa('FS::svc_phone') ) {
70 my $result = $self->_thirdlane_command(
71 'asterisk::rpc_did_create',
72 $svc_x->countrycode. $svc_x->phonenum,
76 #warn Dumper(\$result);
77 $result eq '0' or return 'Thirdlane API failure (rpc_did_create)';
79 return '' unless $svc_x->pbxsvc;
81 $result = $self->_thirdlane_command(
82 'asterisk::rpc_did_assign',
83 $svc_x->countrycode. $svc_x->phonenum,
88 #warn Dumper(\$result);
89 $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_assign)';
92 die "guru meditation #10: $svc_x is not FS::svc_pbx or FS::svc_phone";
98 my($self, $new, $old) = (shift, shift, shift);
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;
107 if ( $new->isa('FS::svc_pbx') ) {
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;
114 'tenant' => $old->title,
115 'maxusers' => $new->max_extensions,
116 #others? will they not clone?
119 my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_update',
124 #warn Dumper(\$result);
125 $result eq '0' ? '' : 'Thirdlane API failure';
127 } elsif ( $new->isa('FS::svc_phone') ) {
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;
134 if ( $old->pbxsvc != $new->pbxsvc ) {
136 if ( $old->pbxsvc ) {
137 my $result = $self->_thirdlane_command(
138 'asterisk::rpc_did_unassign',
139 $new->countrycode. $new->phonenum,
141 $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
144 if ( $new->pbxsvc ) {
145 my $result = $self->_thirdlane_command(
146 'asterisk::rpc_did_assign',
147 $new->countrycode. $new->phonenum,
150 $result eq '0' or return 'Thirdlane API failure (rpc_did_assign)';
159 die "guru meditation #11: $new is not FS::svc_pbx or FS::svc_phone";
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;
171 if ( $svc_x->isa('FS::svc_pbx') ) {
173 my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_delete',
178 #warn Dumper(\$result);
179 $result eq '0' ? '' : 'Thirdlane API failure';
181 } elsif ( $svc_x->isa('FS::svc_phone') ) {
183 if ( $svc_x->pbxsvc ) {
184 my $result = $self->_thirdlane_command(
185 'asterisk::rpc_did_unassign',
186 $svc_x->countrycode. $svc_x->phonenum,
188 $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
191 my $result = $self->_thirdlane_command(
192 'asterisk::rpc_did_delete',
193 $svc_x->countrycode. $svc_x->phonenum,
195 $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_delete)';
198 die "guru meditation #11: $svc_x is not FS::svc_pbx or FS::svc_phone";
203 sub _thirdlane_command {
204 my($self, @param) = @_;
207 $self->option('username'). ':'. $self->option('password'). '@'.
208 $self->machine. '/xmlrpc.cgi';
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'),
216 warn "$me sending command: ". join(' ', @param). "\n"
217 if $self->option('debug');
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;
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;
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;
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>!;
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",
259 # $queue->insert( @_ ) or $queue;
262 #sub thirdlane_insert { #subroutine, not method
263 # my( $username, $password ) = @_;
264 # #do things with $username and $password
267 #sub thirdlane_replace { #subroutine, not method
270 #sub thirdlane_delete { #subroutine, not method
271 # my( $username ) = @_;
272 # #do things with $username
275 #sub thirdlane_suspend { #subroutine, not method
278 #sub thirdlane_unsuspend { #subroutine, not method