1 package FS::part_export::communigate_pro;
4 use vars qw(@ISA %info %options %quotas $DEBUG);
10 @ISA = qw(FS::part_export);
14 tie %options, 'Tie::IxHash',
15 'port' => { label =>'Port number', default=>'106', },
16 'login' => { label =>'The administrator account name. The name can contain a domain part.', },
17 'password' => { label =>'The administrator account password.', },
18 'accountType' => { label => 'Type for newly-created accounts (default when not specified in service)',
20 options => [qw(MultiMailbox TextMailbox MailDirMailbox)],
21 default => 'MultiMailbox',
23 'externalFlag' => { label => 'Create accounts with an external (visible for legacy mailers) INBOX.',
26 'AccessModes' => { label => 'Access modes (default when not specified in service)',
27 default => 'Mail POP IMAP PWD WebMail WebSite',
29 'create_domain' => { label => 'Domain creation API call',
31 options => [qw( CreateDomain CreateSharedDomain )],
36 'svc' => [qw( svc_acct svc_domain svc_forward )],
37 'desc' => 'Real-time export of accounts and domains to a CommuniGate Pro mail server',
38 'options' => \%options,
40 Real time export of accounts and domains to a
41 <a href="http://www.stalker.com/CommuniGatePro/">CommuniGate Pro</a>
43 <a href="http://www.stalker.com/CGPerl/">CommuniGate Pro Perl Interface</a>
44 must be installed as CGP::CLI.
49 'quota' => 'MaxAccountSize',
50 'file_quota' => 'MaxWebSize',
51 'file_maxnum' => 'MaxWebFiles',
52 'file_maxsize' => 'MaxFileSize',
55 sub rebless { shift; }
58 my($self, $svc_acct) = (shift, shift);
63 my( $self, $svc_x ) = (shift, shift);
65 my $table = $svc_x->table;
66 my $method = "_export_insert_$table";
67 $self->$method($svc_x, @_);
70 sub _export_insert_svc_acct {
71 my( $self, $svc_acct ) = (shift, shift);
73 my @options = ( $svc_acct->svcnum, 'CreateAccount',
74 'accountName' => $self->export_username($svc_acct),
75 'accountType' => ( $svc_acct->cgp_type
76 || $self->option('accountType') ),
77 'AccessModes' => ( $svc_acct->cgp_accessmodes
78 || $self->option('AccessModes') ),
79 'RealName' => $svc_acct->finger,
80 'Password' => $svc_acct->_password,
83 push @options, $quotas{$_} => $svc_acct->$_()
84 foreach grep $svc_acct->$_(), keys %quotas;
86 #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
87 # RPOP modifications, accepts mail to all, add trailer to sent mail
88 #phase 3: archive messages, mailing lists
90 push @options, 'externalFlag' => $self->option('externalFlag')
91 if $self->option('externalFlag');
93 #XXX preferences phase 1: message delete method, on logout remove trash
94 #phase 2: language, time zone, layout, pronto style, send read receipts
96 $self->communigate_pro_queue( @options );
100 sub _export_insert_svc_domain {
101 my( $self, $svc_domain ) = (shift, shift);
103 my $create = $self->option('create_domain') || 'CreateDomain';
105 my @options = ( $svc_domain->svcnum, $create, $svc_domain->domain,
106 #other domain creation options?
108 push @options, 'AccountsLimit' => $svc_domain->max_accounts
109 if $svc_domain->max_accounts;
111 $self->communigate_pro_queue( @options );
114 #sub _export_insert_svc_forward {
117 sub _export_replace {
118 my( $self, $new, $old ) = (shift, shift, shift);
120 my $table = $new->table;
121 my $method = "_export_replace_$table";
122 $self->$method($new, $old, @_);
125 sub _export_replace_svc_acct {
126 my( $self, $new, $old ) = (shift, shift, shift);
128 #let's just do the rename part realtime rather than trying to queue
129 #w/dependencies. we don't want FS winding up out-of-sync with the wrong
130 #username and a queued job anyway. right??
131 if ( $self->export_username($old) ne $self->export_username($new) ) {
133 eval { $self->communigate_pro_runcommand(
135 $self->export_username($old),
136 $self->export_username($new),
141 if ( $new->_password ne $old->_password
142 && '*SUSPENDED* '.$old->_password ne $new->_password
144 $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
145 $self->export_username($new), $new->_password
151 $settings{'RealName'} = $new->finger
152 if $old->finger ne $new->finger;
153 $settings{$quotas{$_}} = $new->$_()
154 foreach grep $old->$_() ne $new->$_(), keys %quotas;
155 $settings{'accountType'} = $new->cgp_type
156 if $old->cgp_type ne $new->cgp_type;
157 $settings{'AccessModes'} = $new->cgp_accessmodes
158 if $old->cgp_accessmodes ne $new->cgp_accessmodes
159 || $old->cgp_type ne $new->cgp_type;
161 #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
162 # RPOP modifications, accepts mail to all, add trailer to sent mail
163 #phase 3: archive messages, mailing lists
165 if ( keys %settings ) {
166 my $error = $self->communigate_pro_queue(
168 'UpdateAccountSettings',
169 $self->export_username($new),
172 return $error if $error;
175 #XXX preferences phase 1: message delete method, on logout remove trash
176 #phase 2: language, time zone, layout, pronto style, send read receipts
182 sub _export_replace_svc_domain {
183 my( $self, $new, $old ) = (shift, shift, shift);
185 if ( $old->domain ne $new->domain ) {
186 my $error = $self->communigate_pro_queue( $new->svcnum, 'RenameDomain',
187 $old->domain, $new->domain,
189 return $error if $error;
192 if ( $old->max_accounts ne $new->max_accounts ) {
193 my $error = $self->communigate_pro_queue( $new->svcnum,
194 'UpdateDomainSettings',
196 'AccountsLimit' => ($new->max_accounts || 'default'),
198 return $error if $error;
201 #other kinds of changes?
207 my( $self, $svc_x ) = (shift, shift);
209 my $table = $svc_x->table;
210 my $method = "_export_delete_$table";
211 $self->$method($svc_x, @_);
214 sub _export_delete_svc_acct {
215 my( $self, $svc_acct ) = (shift, shift);
217 $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
218 $self->export_username($svc_acct),
223 sub _export_delete_svc_domain {
224 my( $self, $svc_domain ) = (shift, shift);
226 $self->communigate_pro_queue( $svc_domain->svcnum, 'DeleteDomain',
228 #XXX turn on force option for domain deletion?
233 sub _export_suspend {
234 my( $self, $svc_x ) = (shift, shift);
236 my $table = $svc_x->table;
237 my $method = "_export_suspend_$table";
238 $self->$method($svc_x, @_);
242 sub _export_suspend_svc_acct {
243 my( $self, $svc_acct ) = (shift, shift);
245 #XXX is this the desired suspnsion action?
247 $self->communigate_pro_queue(
249 'UpdateAccountSettings',
250 $self->export_username($svc_acct),
251 'AccessModes' => 'Mail',
256 sub _export_suspend_svc_domain {
257 my( $self, $svc_domain) = (shift, shift);
259 #XXX domain operations
264 sub _export_unsuspend {
265 my( $self, $svc_x ) = (shift, shift);
267 my $table = $svc_x->table;
268 my $method = "_export_unsuspend_$table";
269 $self->$method($svc_x, @_);
273 sub _export_unsuspend_svc_acct {
274 my( $self, $svc_acct ) = (shift, shift);
276 $self->communigate_pro_queue(
278 'UpdateAccountSettings',
279 $self->export_username($svc_acct),
280 'AccessModes' => ( $svc_acct->cgp_accessmodes
281 || $self->option('AccessModes') ),
286 sub _export_unsuspend_svc_domain {
287 my( $self, $svc_domain) = (shift, shift);
289 #XXX domain operations
295 sub export_getsettings {
296 my($self, $svc_x) = (shift, shift);
298 my $table = $svc_x->table;
299 my $method = "export_getsettings_$table";
301 $self->can($method) ? $self->$method($svc_x, @_) : '';
305 sub export_getsettings_svc_domain {
306 my($self, $svc_domain, $settingsref, $defaultref ) = @_;
308 my $settings = eval { $self->communigate_pro_runcommand(
314 my $effective_settings = eval { $self->communigate_pro_runcommand(
315 'GetDomainEffectiveSettings',
320 my $acct_defaults = eval { $self->communigate_pro_runcommand(
321 'GetAccountDefaults',
326 #warn Dumper($acct_defaults);
328 %$effective_settings = ( %$effective_settings,
329 map { ("Acct. Default $_" => $acct_defaults->{$_}); }
330 keys(%$acct_defaults)
333 #false laziness w/below
335 my %defaults = map { $_ => 1 }
336 grep !exists(${$settings}{$_}), keys %$effective_settings;
338 foreach my $key ( grep ref($effective_settings->{$_}),
339 keys %$effective_settings )
341 my $value = $effective_settings->{$key};
342 if ( ref($value) eq 'ARRAY' ) {
343 $effective_settings->{$key} = join(' ', @$value);
346 warn "serializing ". ref($value). " for table display not yet handled";
350 %{$settingsref} = %$effective_settings;
351 %{$defaultref} = %defaults;
356 sub export_getsettings_svc_acct {
357 my($self, $svc_acct, $settingsref, $defaultref ) = @_;
359 my $settings = eval { $self->communigate_pro_runcommand(
360 'GetAccountSettings',
365 delete($settings->{'Password'});
367 my $effective_settings = eval { $self->communigate_pro_runcommand(
368 'GetAccountEffectiveSettings',
373 delete($effective_settings->{'Password'});
375 #prefs/effectiveprefs too
377 my $prefs = eval { $self->communigate_pro_runcommand(
383 my $effective_prefs = eval { $self->communigate_pro_runcommand(
384 'GetAccountEffectivePrefs',
389 %$effective_settings = ( %$effective_settings,
390 map { ("Pref $_" => $effective_prefs->{$_}); }
391 keys(%$effective_prefs)
393 %$settings = ( %$settings,
394 map { ("Pref $_" => $prefs->{$_}); }
398 #false laziness w/above
400 my %defaults = map { $_ => 1 }
401 grep !exists(${$settings}{$_}), keys %$effective_settings;
403 foreach my $key ( grep ref($effective_settings->{$_}),
404 keys %$effective_settings )
406 my $value = $effective_settings->{$key};
407 if ( ref($value) eq 'ARRAY' ) {
408 $effective_settings->{$key} = join(' ', @$value);
411 warn "serializing ". ref($value). " for table display not yet handled";
415 %{$settingsref} = %$effective_settings;
416 %{$defaultref} = %defaults;
422 sub communigate_pro_queue {
423 my( $self, $svcnum, $method ) = (shift, shift, shift);
424 my $jobnum = ''; #don't actually care
425 $self->communigate_pro_queue_dep( \$jobnum, $svcnum, $method, @_);
428 sub communigate_pro_queue_dep {
429 my( $self, $jobnumref, $svcnum, $method ) = splice(@_,0,4);
431 my %kludge_methods = (
432 'CreateAccount' => 'CreateAccount',
433 'UpdateAccountSettings' => 'UpdateAccountSettings',
434 'CreateDomain' => 'cp_Scalar_Hash',
435 'CreateSharedDomain' => 'cp_Scalar_Hash',
436 'UpdateDomainSettings' => 'UpdateDomainSettings',
438 my $sub = exists($kludge_methods{$method})
439 ? $kludge_methods{$method}
440 : 'communigate_pro_command';
442 my $queue = new FS::queue {
444 'job' => "FS::part_export::communigate_pro::$sub",
446 my $error = $queue->insert(
448 $self->option('port'),
449 $self->option('login'),
450 $self->option('password'),
454 $$jobnumref = $queue->jobnum unless $error;
459 sub communigate_pro_runcommand {
460 my( $self, $method ) = (shift, shift);
462 communigate_pro_command(
464 $self->option('port'),
465 $self->option('login'),
466 $self->option('password'),
473 #XXX one sub per arg prototype is lame. more magic? i suppose queue needs
474 # to store data strctures properly instead of just an arg list. right.
477 my( $machine, $port, $login, $password, $method, $scalar, %hash ) = @_;
478 my @args = ( $scalar, \%hash );
479 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
483 # my( $machine, $port, $login, $password, $method, %hash ) = @_;
484 # my @args = ( \%hash );
485 # communigate_pro_command( $machine, $port, $login, $password, $method, @args );
488 sub UpdateDomainSettings {
489 my( $machine, $port, $login, $password, $method, $domain, %settings ) = @_;
490 my @args = ( 'domain' => $domain, 'settings' => \%settings );
491 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
495 my( $machine, $port, $login, $password, $method, %args ) = @_;
496 my $accountName = delete $args{'accountName'};
497 my $accountType = delete $args{'accountType'};
498 my $externalFlag = delete $args{'externalFlag'};
499 $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
500 my @args = ( accountName => $accountName,
501 accountType => $accountType,
504 #externalFlag => $externalFlag,
505 push @args, externalFlag => $externalFlag if $externalFlag;
507 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
511 sub UpdateAccountSettings {
512 my( $machine, $port, $login, $password, $method, $accountName, %args ) = @_;
513 $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
514 my @args = ( $accountName, \%args );
515 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
518 sub communigate_pro_command { #subroutine, not method
519 my( $machine, $port, $login, $password, $method, @args ) = @_;
523 my $cli = new CGP::CLI( {
524 'PeerAddr' => $machine,
527 'password' => $password,
528 } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";
530 #warn "$method ". Dumper(@args) if $DEBUG;
532 my $return = $cli->$method(@args)
533 or die "Communigate Pro error: ". $cli->getErrMessage;
535 $cli->Logout; # or die "Can't logout of CGPro: $CGP::ERR_STRING\n";