summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/thirdlane.pm
blob: 60c099748d67a1bf35b7c2b21a08d53c425481a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package FS::part_export::thirdlane;

use base qw( FS::part_export );

use vars qw(%info $me);
use Tie::IxHash;
use URI::Escape;
use Frontier::Client;

$me = '['.__PACKAGE__.']';

tie my %options, 'Tie::IxHash',
  #'server'           => { label => 'Thirdlane server name or IP address', },
  'username'         => { label => 'Thirdlane username', },
  'password'         => { label => 'Thirdlane password', },
  'ssl'              => { label => 'Enable HTTPS (SSL) connection',
                          type  => 'checkbox',
                        },
  'port'             => { label => 'Port number if not 80 or 443', },
  'prototype_tenant' => { label => 'Prototype tenant name', },
  'omit_countrycode' => { label => 'Omit country code', type => 'checkbox' },
  'debug'            => { label => 'Checkbox label', type => 'checkbox' },
#  'select_option'   => { label   => 'Select option description',
#                         type    => 'select', options=>[qw(chocolate vanilla)],
#                         default => 'vanilla',
#                       },
#  'textarea_option' => { label   => 'Textarea option description',
#                         type    => 'textarea',
#                         default => 'Default text.',
#                      },
;

%info = (
  'svc'      => [qw( svc_pbx svc_phone svc_acct )],
  'desc'     =>
    'Export tenants, DIDs and admins to Thirdlane PBX manager',
  'options'  => \%options,
  'notes'    => <<'END'
Exports tenants, DIDs and admins to Thirdlane PBX manager using the XML-RPC API.
END
);

sub rebless { shift; }

sub _export_insert {
  my($self, $svc_x) = (shift, shift);

  if ( $svc_x->isa('FS::svc_pbx') ) {

    return 'Name must be 19 characters or less (thirdlane restriction?)'
      if length($svc_x->title) > 19;

    return 'Name must consist of alphanumerics and spaces only (thirdlane restriction?)'
      unless $svc_x->title =~ /^[\w\s]+$/;

    my $tenant = {
      'tenant'   => $svc_x->title,
      'maxusers' => $svc_x->max_extensions,
      #others?  will they not clone?
    };

    @what_to_clone = qw(routes schedules menus queues voiceprompts moh);

    my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_create',
                                            $tenant,
                                            $self->option('prototype_tenant'),
                                            \@what_to_clone,
                                          );

    #use Data::Dumper;
    #warn Dumper(\$result);
    $result eq '0' ? '' : 'Thirdlane API failure (rpc_tenant_create)';

  } elsif ( $svc_x->isa('FS::svc_phone') ) {

    my $result = $self->_thirdlane_command(
      'asterisk::rpc_did_create',
      $self->_thirdlane_did($svc_x)
    );

    #use Data::Dumper;
    #warn Dumper(\$result);
    $result eq '0' or return 'Thirdlane API failure (rpc_did_create)';

    return '' unless $svc_x->pbxsvc;

    $result = $self->_thirdlane_command(
      'asterisk::rpc_did_assign',
      $self->_thirdlane_did($svc_x),
      $svc_x->pbx_title,
    );

    #use Data::Dumper;
    #warn Dumper(\$result);
    $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_assign)';

  } elsif ( $svc_x->isa('FS::svc_acct') ) {

    return 'Must select a PBX' unless $svc_x->pbxsvc;

    my $result = $self->_thirdlane_command(
      'asterisk::rpc_admin_create',
      $svc_x->username,
      $svc_x->_password,
      $svc_x->pbx_title,
    );

    #use Data::Dumper;
    #warn Dumper(\$result);
    $result eq '0' ? '' : 'Thirdlane API failure (rpc_admin_create)';

  } else {
    die "guru meditation #10: $svc_x is not FS::svc_pbx, FS::svc_phone or FS::svc_acct";
  }

}

sub _export_replace {
  my($self, $new, $old) = (shift, shift, shift);

#  #return "can't change username with thirdlane"
#  #  if $old->username ne $new->username;
#  #return '' unless $old->_password ne $new->_password;
#  $err_or_queue = $self->thirdlane_queue( $new->svcnum,
#    'replace', $new->username, $new->_password );
#  ref($err_or_queue) ? '' : $err_or_queue;

  if ( $new->isa('FS::svc_pbx') ) {

    #need more info on how the API works for changing names.. can it?
    return "can't change PBX name with thirdlane (yet?)"
      if $old->title ne $new->title;

    my $tenant = {
      'tenant'   => $old->title,
      'maxusers' => $new->max_extensions,
      #others?  will they not clone?
    };

    my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_update',
                                            $tenant
                                          );

    #use Data::Dumper;
    #warn Dumper(\$result);
    $result eq '0' ? '' : 'Thirdlane API failure (rpc_tenant_update)';

  } elsif ( $new->isa('FS::svc_phone') ) {

    return "can't change DID countrycode with thirdlane"
      if $old->countrycode ne $new->countrycode;
    return "can't change DID number with thirdlane"
      if $old->phonenum ne $new->phonenum;

    if ( $old->pbxsvc != $new->pbxsvc ) {

      if ( $old->pbxsvc ) {
        my $result = $self->_thirdlane_command(
          'asterisk::rpc_did_unassign',
          $self->_thirdlane_did($old),
        );
        $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
      }

      if ( $new->pbxsvc ) {
        my $result = $self->_thirdlane_command(
          'asterisk::rpc_did_assign',
          $self->_thirdlane_did($new),
          $new->pbx_title,
        );
        $result eq '0' or return 'Thirdlane API failure (rpc_did_assign)';
      }


    }

    '';

  } elsif ( $new->isa('FS::svc_acct') ) {

    return "can't change uesrname with thirdlane"
      if $old->username ne $new->username;

    return "can't change password with thirdlane"
      if $old->_password ne $new->_password;

    return "can't change PBX for user with thirdlane"
      if $old->pbxsvc != $new->pbxsvc;

    ''; #we don't care then

  } else {
    die "guru meditation #11: $new is not FS::svc_pbx, FS::svc_phone or FS::svc_acct";
  }

}

sub _export_delete {
  my($self, $svc_x) = (shift, shift);
  #my( $self, $svc_something ) = (shift, shift);
  #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
  #  'delete', $svc_something->username );
  #ref($err_or_queue) ? '' : $err_or_queue;

  if ( $svc_x->isa('FS::svc_pbx') ) {

    my $result = $self->_thirdlane_command( 'asterisk::rpc_tenant_delete',
                                            $svc_x->title,
                                          );

    #use Data::Dumper;
    #warn Dumper(\$result);
    #$result eq '0' ? '' : 'Thirdlane API failure (rpc_tenant_delete)';
    warn "Thirdlane API failure (rpc_tenant_delete); deleting anyway\n"
      if $result ne '0';
    '';

  } elsif ( $svc_x->isa('FS::svc_phone') ) {

    if ( $svc_x->pbxsvc ) {
      my $result = $self->_thirdlane_command(
        'asterisk::rpc_did_unassign',
        $self->_thirdlane_did($svc_x),
      );
      $result eq '0' or return 'Thirdlane API failure (rpc_did_unassign)';
    }

    my $result = $self->_thirdlane_command(
      'asterisk::rpc_did_delete',
      $self->_thirdlane_did($svc_x),
    );
    $result eq '0' ? '' : 'Thirdlane API failure (rpc_did_delete)';

  } elsif ( $svc_x->isa('FS::svc_acct') ) {

    return '' unless $svc_x->pbxsvc; #error out?  nah

    my $result = $self->_thirdlane_command(
      'asterisk::rpc_admin_delete',
      $svc_x->username,
      $svc_x->pbx_title,
    );

    #use Data::Dumper;
    #warn Dumper(\$result);
    #$result eq '0' ? '' : 'Thirdlane API failure (rpc_admin_delete)';
    warn "Thirdlane API failure (rpc_admin_delete); deleting anyway\n"
      if $result ne '0';
    '';

  } else {
    die "guru meditation #12: $svc_x is not FS::svc_pbx, FS::svc_phone or FS::svc_acct";
  }

}

sub _thirdlane_command {
  my($self, @param) = @_;

  my $url = $self->option('ssl') ? 'https://' : 'http://';
  $url .= uri_escape($self->option('username')). ':'.
          uri_escape($self->option('password')). '@'.
          $self->machine;
  $url .= ':'. $self->option('port') if $self->option('port');
  $url .= '/xmlrpc.cgi';

  warn "$me connecting to $url\n"
    if $self->option('debug');
  my $conn = Frontier::Client->new( 'url'   => $url,
                                    #no, spews output to browser
                                    #'debug' => $self->option('debug'),
                                  );

  warn "$me sending command: ". join(' ', @param). "\n"
    if $self->option('debug');
  $conn->call(@param);
  
}

sub _thirdlane_did {
  my($self, $svc_phone) = @_;
  if ( $self->option('omit_countrycode') ) {
    $svc_phone->phonenum;
  } else {
    $svc_phone->countrycode. $svc_phone->phonenum;
  }
}

  #my( $self, $svc_something ) = (shift, shift);
  #$err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
  #  'delete', $svc_something->username );
  #ref($err_or_queue) ? '' : $err_or_queue;

#these three are optional
## fallback for svc_acct will change and restore password
#sub _export_suspend {
#  my( $self, $svc_something ) = (shift, shift);
#  $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
#    'suspend', $svc_something->username );
#  ref($err_or_queue) ? '' : $err_or_queue;
#}
#
#sub _export_unsuspend {
#  my( $self, $svc_something ) = (shift, shift);
#  $err_or_queue = $self->thirdlane_queue( $svc_something->svcnum,
#    'unsuspend', $svc_something->username );
#  ref($err_or_queue) ? '' : $err_or_queue;
#}
#
#sub export_links {
#  my($self, $svc_something, $arrayref) = (shift, shift, shift);
#  #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_something->username.
#  #                 qq!">!. $svc_something->username. qq!</A>!;
#  '';
#}

####
#
##a good idea to queue anything that could fail or take any time
#sub thirdlane_queue {
#  my( $self, $svcnum, $method ) = (shift, shift, shift);
#  my $queue = new FS::queue {
#    'svcnum' => $svcnum,
#    'job'    => "FS::part_export::thirdlane::thirdlane_$method",
#  };
#  $queue->insert( @_ ) or $queue;
#}
#
#sub thirdlane_insert { #subroutine, not method
#  my( $username, $password ) = @_;
#  #do things with $username and $password
#}
#
#sub thirdlane_replace { #subroutine, not method
#}
#
#sub thirdlane_delete { #subroutine, not method
#  my( $username ) = @_;
#  #do things with $username
#}
#
#sub thirdlane_suspend { #subroutine, not method
#}
#
#sub thirdlane_unsuspend { #subroutine, not method
#}

1;