This commit was generated by cvs2svn to compensate for changes in r8593,
[freeside.git] / FS / FS / part_export / communigate_pro.pm
1 package FS::part_export::communigate_pro;
2
3 use vars qw(@ISA %info %options);
4 use Tie::IxHash;
5 use FS::part_export;
6 use FS::queue;
7
8 @ISA = qw(FS::part_export);
9
10 tie %options, 'Tie::IxHash',
11   'port'     => { label=>'Port number', default=>'106', },
12   'login'    => { label=>'The administrator account name.  The name can contain a domain part.', },
13   'password' => { label=>'The administrator account password.', },
14   'accountType' => { label=>'Type for newly-created accounts',
15                      type=>'select',
16                      options=>[qw( MultiMailbox TextMailbox MailDirMailbox )],
17                      default=>'MultiMailbox',
18                    },
19   'externalFlag' => { label=> 'Create accounts with an external (visible for legacy mailers) INBOX.',
20                       type=>'checkbox',
21                     },
22   'AccessModes' => { label=>'Access modes',
23                      default=>'Mail POP IMAP PWD WebMail WebSite',
24                    },
25 ;
26
27 %info = (
28   'svc'     => 'svc_acct',
29   'desc'    => 'Real-time export to a CommuniGate Pro mail server',
30   'options' => \%options,
31   'notes'   => <<'END'
32 Real time export to a
33 <a href="http://www.stalker.com/CommuniGatePro/">CommuniGate Pro</a>
34 mail server.  The
35 <a href="http://www.stalker.com/CGPerl/">CommuniGate Pro Perl Interface</a>
36 must be installed as CGP::CLI.
37 END
38 );
39
40 sub rebless { shift; }
41
42 sub export_username {
43   my($self, $svc_acct) = (shift, shift);
44   $svc_acct->email;
45 }
46
47 sub _export_insert {
48   my( $self, $svc_acct ) = (shift, shift);
49   my @options = ( $svc_acct->svcnum, 'CreateAccount',
50     'accountName'    => $self->export_username($svc_acct),
51     'accountType'    => $self->option('accountType'),
52     'AccessModes'    => $self->option('AccessModes'),
53     'RealName'       => $svc_acct->finger,
54     'Password'       => $svc_acct->_password,
55   );
56   push @options, 'MaxAccountSize' => $svc_acct->quota if $svc_acct->quota;
57   push @options, 'externalFlag'   => $self->option('externalFlag')
58     if $self->option('externalFlag');
59
60   $self->communigate_pro_queue( @options );
61 }
62
63 sub _export_replace {
64   my( $self, $new, $old ) = (shift, shift, shift);
65   return "can't (yet) change username with CommuniGate Pro"
66     if $old->username ne $new->username;
67   return "can't (yet) change domain with CommuniGate Pro"
68     if $self->export_username($old) ne $self->export_username($new);
69   return "can't (yet) change GECOS with CommuniGate Pro"
70     if $old->finger ne $new->finger;
71   return "can't (yet) change quota with CommuniGate Pro"
72     if $old->quota ne $new->quota;
73   return '' unless $old->username ne $new->username
74                 || $old->_password ne $new->_password
75                 || $old->finger ne $new->finger
76                 || $old->quota ne $new->quota;
77
78   return '' if '*SUSPENDED* '. $old->_password eq $new->_password;
79
80   #my $err_or_queue = $self->communigate_pro_queue( $new->svcnum,'RenameAccount',
81   #  $old->email, $new->email );
82   #return $err_or_queue unless ref($err_or_queue);
83   #my $jobnum = $err_or_queue->jobnum;
84
85   $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
86                                 $self->export_username($new), $new->_password        )
87     if $new->_password ne $old->_password;
88
89 }
90
91 sub _export_delete {
92   my( $self, $svc_acct ) = (shift, shift);
93   $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
94     $self->export_username($svc_acct),
95   );
96 }
97
98 sub _export_suspend {
99   my( $self, $svc_acct ) = (shift, shift);
100   $self->communigate_pro_queue( $svc_acct->svcnum, 'UpdateAccountSettings',
101     'accountName' => $self->export_username($svc_acct),
102     'AccessModes' => 'Mail',
103   );
104 }
105
106 sub _export_unsuspend {
107   my( $self, $svc_acct ) = (shift, shift);
108   $self->communigate_pro_queue( $svc_acct->svcnum, 'UpdateAccountSettings',
109     'accountName' => $self->export_username($svc_acct),
110     'AccessModes' => $self->option('AccessModes'),
111   );
112 }
113
114 sub communigate_pro_queue {
115   my( $self, $svcnum, $method ) = (shift, shift, shift);
116   my @kludge_methods = qw(CreateAccount UpdateAccountSettings);
117   my $sub = 'communigate_pro_command';
118   $sub = $method if grep { $method eq $_ } @kludge_methods;
119   my $queue = new FS::queue {
120     'svcnum' => $svcnum,
121     'job'    => "FS::part_export::communigate_pro::$sub",
122   };
123   $queue->insert(
124     $self->machine,
125     $self->option('port'),
126     $self->option('login'),
127     $self->option('password'),
128     $method,
129     @_,
130   );
131
132 }
133
134 sub CreateAccount {
135   my( $machine, $port, $login, $password, $method, %args ) = @_;
136   my $accountName  = delete $args{'accountName'};
137   my $accountType  = delete $args{'accountType'};
138   my $externalFlag = delete $args{'externalFlag'};
139   $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
140   my @args = ( accountName => $accountName,
141                accountType  => $accountType,
142                settings     => \%args,
143              );
144                #externalFlag => $externalFlag,
145   push @args, externalFlag => $externalFlag if $externalFlag;
146
147   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
148
149 }
150
151 sub UpdateAccountSettings {
152   my( $machine, $port, $login, $password, $method, %args ) = @_;
153   my $accountName  = delete $args{'accountName'};
154   $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
155   @args = ( $accountName, \%args );
156   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
157 }
158
159 sub communigate_pro_command { #subroutine, not method
160   my( $machine, $port, $login, $password, $method, @args ) = @_;
161
162   eval "use CGP::CLI";
163
164   my $cli = new CGP::CLI( {
165     'PeerAddr' => $machine,
166     'PeerPort' => $port,
167     'login'    => $login,
168     'password' => $password,
169   } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";
170
171   $cli->$method(@args) or die "CGPro error: ". $cli->getErrMessage;
172
173   $cli->Logout; # or die "Can't logout of CGPro: $CGP::ERR_STRING\n";
174
175 }
176
177 1;
178