557aad91dd93ae0cd70ff67fa01f6b4c4070ba9f
[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_username {
12   my($self, $svc_acct) = (shift, shift);
13   $svc_acct->email;
14 }
15
16 sub _export_insert {
17   my( $self, $svc_acct ) = (shift, shift);
18   my @options = ( $svc_acct->svcnum, 'CreateAccount',
19     'accountName'    => $self->export_username($svc_acct),
20     'accountType'    => $self->option('accountType'),
21     'AccessModes'    => $self->option('AccessModes'),
22     'RealName'       => $svc_acct->finger,
23     'Password'       => $svc_acct->_password,
24   );
25   push @options, 'MaxAccountSize' => $svc_acct->quota if $svc_acct->quota;
26   push @options, 'externalFlag'   => $self->option('externalFlag')
27     if $self->option('externalFlag');
28
29   $self->communigate_pro_queue( @options );
30 }
31
32 sub _export_replace {
33   my( $self, $new, $old ) = (shift, shift, shift);
34   return "can't (yet) change username with CommuniGate Pro"
35     if $old->username ne $new->username;
36   return "can't (yet) change domain with CommuniGate Pro"
37     if $self->export_username($old) ne $self->export_username($new);
38   return "can't (yet) change GECOS with CommuniGate Pro"
39     if $old->finger ne $new->finger;
40   return "can't (yet) change quota with CommuniGate Pro"
41     if $old->quota ne $new->quota;
42   return '' unless $old->username ne $new->username
43                 || $old->_password ne $new->_password
44                 || $old->finger ne $new->finger
45                 || $old->quota ne $new->quota;
46
47   return '' if '*SUSPENDED* '. $old->_password eq $new->_password;
48
49   #my $err_or_queue = $self->communigate_pro_queue( $new->svcnum,'RenameAccount',
50   #  $old->email, $new->email );
51   #return $err_or_queue unless ref($err_or_queue);
52   #my $jobnum = $err_or_queue->jobnum;
53
54   $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
55                                 $self->export_username($new), $new->_password        )
56     if $new->_password ne $old->_password;
57
58 }
59
60 sub _export_delete {
61   my( $self, $svc_acct ) = (shift, shift);
62   $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
63     $self->export_username($svc_acct),
64   );
65 }
66
67 sub _export_suspend {
68   my( $self, $svc_acct ) = (shift, shift);
69   $self->communigate_pro_queue( $svc_acct->svcnum, 'UpdateAccountSettings',
70     'accountName' => $self->export_username($svc_acct),
71     'AccessModes' => 'Mail',
72   );
73 }
74
75 sub _export_unsuspend {
76   my( $self, $svc_acct ) = (shift, shift);
77   $self->communigate_pro_queue( $svc_acct->svcnum, 'UpdateAccountSettings',
78     'accountName' => $self->export_username($svc_acct),
79     'AccessModes' => $self->option('AccessModes'),
80   );
81 }
82
83 sub communigate_pro_queue {
84   my( $self, $svcnum, $method ) = (shift, shift, shift);
85   my @kludge_methods = qw(CreateAccount UpdateAccountSettings);
86   my $sub = 'communigate_pro_command';
87   $sub = $method if grep { $method eq $_ } @kludge_methods;
88   my $queue = new FS::queue {
89     'svcnum' => $svcnum,
90     'job'    => "FS::part_export::communigate_pro::$sub",
91   };
92   $queue->insert(
93     $self->machine,
94     $self->option('port'),
95     $self->option('login'),
96     $self->option('password'),
97     $method,
98     @_,
99   );
100
101 }
102
103 sub CreateAccount {
104   my( $machine, $port, $login, $password, $method, %args ) = @_;
105   my $accountName  = delete $args{'accountName'};
106   my $accountType  = delete $args{'accountType'};
107   my $externalFlag = delete $args{'externalFlag'};
108   $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
109   my @args = ( accountName => $accountName,
110                accountType  => $accountType,
111                settings     => \%args,
112              );
113                #externalFlag => $externalFlag,
114   push @args, externalFlag => $externalFlag if $externalFlag;
115
116   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
117
118 }
119
120 sub UpdateAccountSettings {
121   my( $machine, $port, $login, $password, $method, %args ) = @_;
122   my $accountName  = delete $args{'accountName'};
123   $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
124   @args = ( $accountName, \%args );
125   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
126 }
127
128 sub communigate_pro_command { #subroutine, not method
129   my( $machine, $port, $login, $password, $method, @args ) = @_;
130
131   eval "use CGP::CLI";
132
133   my $cli = new CGP::CLI( {
134     'PeerAddr' => $machine,
135     'PeerPort' => $port,
136     'login'    => $login,
137     'password' => $password,
138   } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";
139
140   $cli->$method(@args) or die "CGPro error: ". $cli->getErrMessage;
141
142   $cli->Logout or die "Can't logout of CGPro: $CGP::ERR_STRING\n";
143
144 }