summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/communigate_pro.pm
blob: 557aad91dd93ae0cd70ff67fa01f6b4c4070ba9f (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
package FS::part_export::communigate_pro;

use vars qw(@ISA);
use FS::part_export;
use FS::queue;

@ISA = qw(FS::part_export);

sub rebless { shift; }

sub export_username {
  my($self, $svc_acct) = (shift, shift);
  $svc_acct->email;
}

sub _export_insert {
  my( $self, $svc_acct ) = (shift, shift);
  my @options = ( $svc_acct->svcnum, 'CreateAccount',
    'accountName'    => $self->export_username($svc_acct),
    'accountType'    => $self->option('accountType'),
    'AccessModes'    => $self->option('AccessModes'),
    'RealName'       => $svc_acct->finger,
    'Password'       => $svc_acct->_password,
  );
  push @options, 'MaxAccountSize' => $svc_acct->quota if $svc_acct->quota;
  push @options, 'externalFlag'   => $self->option('externalFlag')
    if $self->option('externalFlag');

  $self->communigate_pro_queue( @options );
}

sub _export_replace {
  my( $self, $new, $old ) = (shift, shift, shift);
  return "can't (yet) change username with CommuniGate Pro"
    if $old->username ne $new->username;
  return "can't (yet) change domain with CommuniGate Pro"
    if $self->export_username($old) ne $self->export_username($new);
  return "can't (yet) change GECOS with CommuniGate Pro"
    if $old->finger ne $new->finger;
  return "can't (yet) change quota with CommuniGate Pro"
    if $old->quota ne $new->quota;
  return '' unless $old->username ne $new->username
                || $old->_password ne $new->_password
                || $old->finger ne $new->finger
                || $old->quota ne $new->quota;

  return '' if '*SUSPENDED* '. $old->_password eq $new->_password;

  #my $err_or_queue = $self->communigate_pro_queue( $new->svcnum,'RenameAccount',
  #  $old->email, $new->email );
  #return $err_or_queue unless ref($err_or_queue);
  #my $jobnum = $err_or_queue->jobnum;

  $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
                                $self->export_username($new), $new->_password        )
    if $new->_password ne $old->_password;

}

sub _export_delete {
  my( $self, $svc_acct ) = (shift, shift);
  $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
    $self->export_username($svc_acct),
  );
}

sub _export_suspend {
  my( $self, $svc_acct ) = (shift, shift);
  $self->communigate_pro_queue( $svc_acct->svcnum, 'UpdateAccountSettings',
    'accountName' => $self->export_username($svc_acct),
    'AccessModes' => 'Mail',
  );
}

sub _export_unsuspend {
  my( $self, $svc_acct ) = (shift, shift);
  $self->communigate_pro_queue( $svc_acct->svcnum, 'UpdateAccountSettings',
    'accountName' => $self->export_username($svc_acct),
    'AccessModes' => $self->option('AccessModes'),
  );
}

sub communigate_pro_queue {
  my( $self, $svcnum, $method ) = (shift, shift, shift);
  my @kludge_methods = qw(CreateAccount UpdateAccountSettings);
  my $sub = 'communigate_pro_command';
  $sub = $method if grep { $method eq $_ } @kludge_methods;
  my $queue = new FS::queue {
    'svcnum' => $svcnum,
    'job'    => "FS::part_export::communigate_pro::$sub",
  };
  $queue->insert(
    $self->machine,
    $self->option('port'),
    $self->option('login'),
    $self->option('password'),
    $method,
    @_,
  );

}

sub CreateAccount {
  my( $machine, $port, $login, $password, $method, %args ) = @_;
  my $accountName  = delete $args{'accountName'};
  my $accountType  = delete $args{'accountType'};
  my $externalFlag = delete $args{'externalFlag'};
  $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
  my @args = ( accountName => $accountName,
               accountType  => $accountType,
               settings     => \%args,
             );
               #externalFlag => $externalFlag,
  push @args, externalFlag => $externalFlag if $externalFlag;

  communigate_pro_command( $machine, $port, $login, $password, $method, @args );

}

sub UpdateAccountSettings {
  my( $machine, $port, $login, $password, $method, %args ) = @_;
  my $accountName  = delete $args{'accountName'};
  $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
  @args = ( $accountName, \%args );
  communigate_pro_command( $machine, $port, $login, $password, $method, @args );
}

sub communigate_pro_command { #subroutine, not method
  my( $machine, $port, $login, $password, $method, @args ) = @_;

  eval "use CGP::CLI";

  my $cli = new CGP::CLI( {
    'PeerAddr' => $machine,
    'PeerPort' => $port,
    'login'    => $login,
    'password' => $password,
  } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";

  $cli->$method(@args) or die "CGPro error: ". $cli->getErrMessage;

  $cli->Logout or die "Can't logout of CGPro: $CGP::ERR_STRING\n";

}