97c946ac212da339b232b2c5c37047f7b6878763
[freeside.git] / FS / FS / part_export / communigate_pro.pm
1 package FS::part_export::communigate_pro;
2
3 use vars qw(@ISA);
4 use FS::part_export;
5 use FS::queue;
6
7 @ISA = qw(FS::part_export);
8
9 sub rebless { shift; }
10
11 sub _export_insert {
12   my( $self, $svc_acct ) = (shift, shift);
13   $self->communigate_pro_queue( $svc_acct->svcnum, 'CreateAccount',
14     'accountName'    => $svc_acct->email,
15     'accountType'    => $self->option('accountType'),
16     'externalFlag'   => $self->option('externalFlag'),
17     'AccessModes'    => $self->option('AccessModes'),
18     'RealName'       => $svc_acct->finger,
19     'MaxAccountSize' => $svc_acct->quota,
20   );
21 }
22
23 sub _export_replace {
24   my( $self, $new, $old ) = (shift, shift, shift);
25   return "can't (yet) change domain with CommuniGate Pro"
26     if $old->domain ne $new->domain;
27   return "can't (yet) change username with CommuniGate Pro"
28     if $old->username ne $new->username;
29   return "can't (yet) change GECOS with CommuniGate Pro"
30     if $old->finger ne $new->finger;
31   return "can't (yet) change quota with CommuniGate Pro"
32     if $old->quota ne $new->quota;
33   return '' unless $old->username ne $new->username
34                 || $old->_password ne $new->_password
35                 || $old->finger ne $new->finger
36                 || $old->quota ne $new->quota;
37
38   #my $err_or_queue = $self->communigate_pro_queue( $new->svcnum,'RenameAccount',
39   #  $old->email, $new->email );
40   #return $err_or_queue unless ref($err_or_queue);
41   #my $jobnum = $err_or_queue->jobnum;
42
43   $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
44                                 $new->email, $new->_password        )
45     if $new->_password ne $old->_password;
46
47 }
48
49 sub _export_delete {
50   my( $self, $svc_acct ) = (shift, shift);
51   $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
52     $svc_acct->email,
53   );
54 }
55
56 #sub _export_suspend {
57 #}
58
59 #sub _export_unsuspend {
60 #}
61
62 sub communigate_pro_queue {
63   my( $self, $svcnum, $method ) = (shift, shift, shift);
64   my $sub = $method eq 'CreateAccount'
65               ? 'CreateAccount'
66               : 'communigate_pro_command';
67   my $queue = new FS::queue {
68     'svcnum' => $svcnum,
69     'job'    => "FS::part_export::communigate_pro::$sub",
70   };
71   $queue->insert(
72     $self->machine,
73     $self->option('port'),
74     $self->option('login'),
75     $self->option('password'),
76     $method,
77     @_,
78   );
79
80 }
81
82 sub CreateAccount {
83   my( $machine, $port, $login, $password, $method, @args ) = @_;
84
85   my %args = @args;
86   my $accountName  = delete $args{'accountName'};
87   my $accountType  = delete $args{'accountType'};
88   my $externalFlag = delete $args{'externalFlag'};
89   $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
90   @args = ( accountName => $accountName, [ settings => \%args ],
91             accountType => $accountType, externalFlag => $externalFlag, );
92
93   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
94
95 }
96
97 sub communigate_pro_command { #subroutine, not method
98   my( $machine, $port, $login, $password, $method, @args ) = @_;
99
100   eval "use CGP::CLI";
101
102   my $cli = new CGP::CLI( {
103     'PeerAddr' => $machine,
104     'PeerPort' => $port,
105     'login'    => $login,
106     'password' => $password,
107   } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";
108
109   $cli->$method(@args) or die "CGPro error: ". $cli->getErrMessage;
110
111   $cli->Logout or die "Can't logout of CGPro: $CGP::ERR_STRING\n";
112
113 }