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