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 AGrade BGrade CGrade)],
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);
74 'AccessModes' => [ split(' ', ( $svc_acct->cgp_accessmodes
75 || $self->option('AccessModes') )
78 'RealName' => $svc_acct->finger,
79 'Password' => $svc_acct->_password,
80 map { $quotas{$_} => $svc_acct->$_() }
81 grep $svc_acct->$_(), keys %quotas
83 #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
84 # RPOP modifications, accepts mail to all, add trailer to sent mail
85 #phase 3: archive messages, mailing lists
87 my @options = ( 'CreateAccount',
88 'accountName' => $self->export_username($svc_acct),
89 'accountType' => ( $svc_acct->cgp_type
90 || $self->option('accountType') ),
91 'settings' => \%settings
94 push @options, 'externalFlag' => $self->option('externalFlag')
95 if $self->option('externalFlag');
97 #let's do the create realtime too, for much the same reasons, and to avoid
98 #pain of trying to queue w/dep the prefs & aliases
99 eval { $self->communigate_pro_runcommand( @options ) };
104 $prefs{'DeleteMode'} = $svc_acct->cgp_deletemode if $svc_acct->cgp_deletemode;
105 $prefs{'EmptyTrash'} = $svc_acct->cgp_emptytrash if $svc_acct->cgp_emptytrash;
106 #phase 2: language, time zone, layout, pronto style, send read receipts
108 my $pref_err = $self->communigate_pro_queue( $svc_acct->svcnum,
109 'UpdateAccountPrefs',
110 $self->export_username($svc_acct),
113 warn "WARNING: error queueing UpdateAccountPrefs job: $pref_err"
118 if ( $svc_acct->cgp_aliases ) {
119 my $alias_err = $self->communigate_pro_queue( $svc_acct->svcnum,
121 $self->export_username($svc_acct),
122 [ split(/\s*[,\s]\s*/, $svc_acct->cgp_aliases) ],
124 warn "WARNING: error queueing SetAccountAliases job: $alias_err"
132 sub _export_insert_svc_domain {
133 my( $self, $svc_domain ) = (shift, shift);
135 my $create = $self->option('create_domain') || 'CreateDomain';
138 'DomainAccessModes' => [ split(' ', $svc_domain->cgp_accessmodes ) ],
140 $settings{'AccountsLimit'} = $svc_domain->max_accounts
141 if $svc_domain->max_accounts;
142 $settings{'AdminDomainName'} = $svc_domain->parent_svc_x->domain
143 if $svc_domain->parent_svcnum;
145 my @options = ( $create, $svc_domain->domain, \%settings );
147 eval { $self->communigate_pro_runcommand( @options ) };
151 if ( $svc_domain->cgp_aliases ) {
152 my $alias_err = $self->communigate_pro_queue( $svc_domain->svcnum,
155 split(/\s*[,\s]\s*/, $svc_domain->cgp_aliases),
157 warn "WARNING: error queueing SetDomainAliases job: $alias_err"
162 my $def_err = $self->communigate_pro_queue( $svc_domain->svcnum,
163 'SetAccountDefaults',
165 'PWDAllowed' =>($svc_domain->acct_def_password_selfchange ? 'YES':'NO'),
166 'PasswordRecovery' => ($svc_domain->acct_def_password_recover ? 'YES':'NO'),
167 'AccessModes' => $svc_domain->acct_def_cgp_accessmodes,
168 'MaxAccountSize' => $svc_domain->acct_def_quota,
169 'MaxWebSize' => $svc_domain->acct_def_file_quota,
170 'MaxWebFile' => $svc_domain->acct_def_file_maxnum,
171 'MaxFileSize' => $svc_domain->acct_def_file_maxsize,
173 warn "WARNING: error queueing SetAccountDefaults job: $def_err"
176 #account defaults prefs
177 my $pref_err = $self->communigate_pro_queue( $svc_domain->svcnum,
178 'SetAccountDefaultPrefs',
180 'DeleteMode' => $svc_domain->acct_def_cgp_deletemode,
181 'EmptyTrash' => $svc_domain->acct_def_cgp_emptytrash,
183 warn "WARNING: error queueing SetAccountDefaultPrefs job: $pref_err"
190 sub _export_insert_svc_forward {
191 my( $self, $svc_forward ) = (shift, shift);
193 my $src = $svc_forward->src || $svc_forward->srcsvc_acct->email;
194 my $dst = $svc_forward->dst || $svc_forward->dstsvc_acct->email;
196 #real-time here, presuming CGP does some dup detection?
197 eval { $self->communigate_pro_runcommand( 'CreateForwarder', $src, $dst); };
203 sub _export_replace {
204 my( $self, $new, $old ) = (shift, shift, shift);
206 my $table = $new->table;
207 my $method = "_export_replace_$table";
208 $self->$method($new, $old, @_);
211 sub _export_replace_svc_acct {
212 my( $self, $new, $old ) = (shift, shift, shift);
214 #let's just do the rename part realtime rather than trying to queue
215 #w/dependencies. we don't want FS winding up out-of-sync with the wrong
216 #username and a queued job anyway. right??
217 if ( $self->export_username($old) ne $self->export_username($new) ) {
218 eval { $self->communigate_pro_runcommand(
220 $self->export_username($old),
221 $self->export_username($new),
226 if ( $new->_password ne $old->_password
227 && '*SUSPENDED* '.$old->_password ne $new->_password
229 $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
230 $self->export_username($new), $new->_password
236 $settings{'RealName'} = $new->finger
237 if $old->finger ne $new->finger;
238 $settings{$quotas{$_}} = $new->$_()
239 foreach grep $old->$_() ne $new->$_(), keys %quotas;
240 $settings{'accountType'} = $new->cgp_type
241 if $old->cgp_type ne $new->cgp_type;
242 $settings{'AccessModes'} = $new->cgp_accessmodes
243 if $old->cgp_accessmodes ne $new->cgp_accessmodes
244 || $old->cgp_type ne $new->cgp_type;
246 #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
247 # RPOP modifications, accepts mail to all, add trailer to sent mail
248 #phase 3: archive messages, mailing lists
250 if ( keys %settings ) {
251 my $error = $self->communigate_pro_queue(
253 'UpdateAccountSettings',
254 $self->export_username($new),
257 return $error if $error;
262 $prefs{'DeleteMode'} = $new->cgp_deletemode
263 if $old->cgp_deletemode ne $new->cgp_deletemode;
264 $prefs{'EmptyTrash'} = $new->cgp_emptytrash
265 if $old->cgp_emptytrash ne $new->cgp_emptytrash;
266 #phase 2: language, time zone, layout, pronto style, send read receipts
268 my $pref_err = $self->communigate_pro_queue( $new->svcnum,
269 'UpdateAccountPrefs',
270 $self->export_username($new),
273 warn "WARNING: error queueing UpdateAccountPrefs job: $pref_err"
277 if ( $old->cgp_aliases ne $new->cgp_aliases ) {
278 my $error = $self->communigate_pro_queue(
281 $self->export_username($new),
282 [ split(/\s*[,\s]\s*/, $new->cgp_aliases) ],
284 return $error if $error;
291 sub _export_replace_svc_domain {
292 my( $self, $new, $old ) = (shift, shift, shift);
294 if ( $old->domain ne $new->domain ) {
295 my $error = $self->communigate_pro_queue( $new->svcnum, 'RenameDomain',
296 $old->domain, $new->domain,
298 return $error if $error;
301 $settings{'AccountsLimit'} = $new->max_accounts
302 if $old->max_accounts ne $new->max_accounts;
303 $settings{'DomainAccessModes'} = $new->cgp_accessmodes
304 if $old->cgp_accessmodes ne $new->cgp_accessmodes;
305 $settings{'AdminDomainName'} =
306 $new->parent_svcnum ? $new->parent_svc_x->domain : ''
307 if $old->parent_svcnum != $new->parent_svcnum;
309 if ( keys %settings ) {
310 my $error = $self->communigate_pro_queue( $new->svcnum,
311 'UpdateDomainSettings',
315 return $error if $error;
318 if ( $old->cgp_aliases ne $new->cgp_aliases ) {
319 my $error = $self->communigate_pro_queue( $new->svcnum,
322 split(/\s*[,\s]\s*/, $new->cgp_aliases),
324 return $error if $error;
327 #below this identical to insert... any value to doing an Update here?
328 #not seeing any big one... i guess it would be nice to avoid the update
329 #when things haven't changed
332 my $def_err = $self->communigate_pro_queue( $new->svcnum,
333 'SetAccountDefaults',
335 'PWDAllowed' => ( $new->acct_def_password_selfchange ? 'YES' : 'NO' ),
336 'PasswordRecovery' => ( $new->acct_def_password_recover ? 'YES' : 'NO' ),
337 'AccessModes' => $new->acct_def_cgp_accessmodes,
338 'MaxAccountSize' => $new->acct_def_quota,
339 'MaxWebSize' => $new->acct_def_file_quota,
340 'MaxWebFile' => $new->acct_def_file_maxnum,
341 'MaxFileSize' => $new->acct_def_file_maxsize,
343 warn "WARNING: error queueing SetAccountDefaults job: $def_err"
346 #account defaults prefs
347 my $pref_err = $self->communigate_pro_queue( $new->svcnum,
348 'SetAccountDefaultPrefs',
350 'DeleteMode' => $new->acct_def_cgp_deletemode,
351 'EmptyTrash' => $new->acct_def_cgp_emptytrash,
353 warn "WARNING: error queueing SetAccountDefaultPrefs job: $pref_err"
359 sub _export_replace_svc_forward {
360 my( $self, $new, $old ) = (shift, shift, shift);
362 my $osrc = $old->src || $old->srcsvc_acct->email;
363 my $nsrc = $new->src || $new->srcsvc_acct->email;
364 my $odst = $old->dst || $old->dstsvc_acct->email;
365 my $ndst = $new->dst || $new->dstsvc_acct->email;
367 if ( $odst ne $ndst ) {
369 #no change command, so delete and create (real-time)
370 eval { $self->communigate_pro_runcommand('DeleteForwarder', $osrc) };
372 eval { $self->communigate_pro_runcommand('CreateForwarder', $nsrc, $ndst)};
375 } elsif ( $osrc ne $nsrc ) {
377 #real-time here, presuming CGP does some dup detection?
378 eval { $self->communigate_pro_runcommand( 'RenameForwarder', $osrc, $nsrc)};
382 warn "communigate replace called for svc_forward with no changes\n";#confess
389 my( $self, $svc_x ) = (shift, shift);
391 my $table = $svc_x->table;
392 my $method = "_export_delete_$table";
393 $self->$method($svc_x, @_);
396 sub _export_delete_svc_acct {
397 my( $self, $svc_acct ) = (shift, shift);
399 $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
400 $self->export_username($svc_acct),
404 sub _export_delete_svc_domain {
405 my( $self, $svc_domain ) = (shift, shift);
407 $self->communigate_pro_queue( $svc_domain->svcnum, 'DeleteDomain',
409 #XXX turn on force option for domain deletion?
413 sub _export_delete_svc_forward {
414 my( $self, $svc_forward ) = (shift, shift);
416 $self->communigate_pro_queue( $svc_forward->svcnum, 'DeleteForwarder',
417 ($svc_forward->src || $svc_forward->srcsvc_acct->email),
421 sub _export_suspend {
422 my( $self, $svc_x ) = (shift, shift);
424 my $table = $svc_x->table;
425 my $method = "_export_suspend_$table";
426 $self->$method($svc_x, @_);
430 sub _export_suspend_svc_acct {
431 my( $self, $svc_acct ) = (shift, shift);
433 #XXX is this the desired suspnsion action?
435 $self->communigate_pro_queue(
437 'UpdateAccountSettings',
438 $self->export_username($svc_acct),
439 'AccessModes' => 'Mail',
444 sub _export_suspend_svc_domain {
445 my( $self, $svc_domain) = (shift, shift);
447 #XXX domain operations
452 sub _export_unsuspend {
453 my( $self, $svc_x ) = (shift, shift);
455 my $table = $svc_x->table;
456 my $method = "_export_unsuspend_$table";
457 $self->$method($svc_x, @_);
461 sub _export_unsuspend_svc_acct {
462 my( $self, $svc_acct ) = (shift, shift);
464 $self->communigate_pro_queue(
466 'UpdateAccountSettings',
467 $self->export_username($svc_acct),
468 'AccessModes' => ( $svc_acct->cgp_accessmodes
469 || $self->option('AccessModes') ),
474 sub _export_unsuspend_svc_domain {
475 my( $self, $svc_domain) = (shift, shift);
477 #XXX domain operations
483 sub export_getsettings {
484 my($self, $svc_x) = (shift, shift);
486 my $table = $svc_x->table;
487 my $method = "export_getsettings_$table";
489 $self->can($method) ? $self->$method($svc_x, @_) : '';
493 sub export_getsettings_svc_domain {
494 my($self, $svc_domain, $settingsref, $defaultref ) = @_;
496 my $settings = eval { $self->communigate_pro_runcommand(
502 my $effective_settings = eval { $self->communigate_pro_runcommand(
503 'GetDomainEffectiveSettings',
508 my $acct_defaults = eval { $self->communigate_pro_runcommand(
509 'GetAccountDefaults',
514 my $acct_defaultprefs = eval { $self->communigate_pro_runcommand(
515 'GetAccountDefaultPrefs',
520 %$effective_settings = (
521 %$effective_settings,
522 ( map { ("Acct. Default $_" => $acct_defaults->{$_}); }
523 keys(%$acct_defaults)
525 ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label??
526 keys(%$acct_defaultprefs)
531 ( map { ("Acct. Default $_" => $acct_defaults->{$_}); }
532 keys(%$acct_defaults)
534 ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label??
535 keys(%$acct_defaultprefs)
540 my $aliases = eval { $self->communigate_pro_runcommand(
546 $effective_settings->{'Aliases'} = join(', ', @$aliases);
547 $settings->{'Aliases'} = join(', ', @$aliases);
550 #false laziness w/below
552 my %defaults = map { $_ => 1 }
553 grep !exists(${$settings}{$_}), keys %$effective_settings;
555 foreach my $key ( grep ref($effective_settings->{$_}),
556 keys %$effective_settings )
558 my $value = $effective_settings->{$key};
559 if ( ref($value) eq 'ARRAY' ) {
560 $effective_settings->{$key} = join(' ', @$value);
563 warn "serializing ". ref($value). " for table display not yet handled";
567 %{$settingsref} = %$effective_settings;
568 %{$defaultref} = %defaults;
573 sub export_getsettings_svc_acct {
574 my($self, $svc_acct, $settingsref, $defaultref ) = @_;
576 my $settings = eval { $self->communigate_pro_runcommand(
577 'GetAccountSettings',
582 delete($settings->{'Password'});
584 my $effective_settings = eval { $self->communigate_pro_runcommand(
585 'GetAccountEffectiveSettings',
590 delete($effective_settings->{'Password'});
592 #prefs/effectiveprefs too
594 my $prefs = eval { $self->communigate_pro_runcommand(
600 my $effective_prefs = eval { $self->communigate_pro_runcommand(
601 'GetAccountEffectivePrefs',
606 %$effective_settings = ( %$effective_settings,
607 map { ("Pref $_" => $effective_prefs->{$_}); }
608 keys(%$effective_prefs)
610 %$settings = ( %$settings,
611 map { ("Pref $_" => $prefs->{$_}); }
617 my $aliases = eval { $self->communigate_pro_runcommand(
623 $effective_settings->{'Aliases'} = join(', ', @$aliases);
624 $settings->{'Aliases'} = join(', ', @$aliases);
626 #false laziness w/above
628 my %defaults = map { $_ => 1 }
629 grep !exists(${$settings}{$_}), keys %$effective_settings;
631 foreach my $key ( grep ref($effective_settings->{$_}),
632 keys %$effective_settings )
634 my $value = $effective_settings->{$key};
635 if ( ref($value) eq 'ARRAY' ) {
636 $effective_settings->{$key} = join(' ', @$value);
639 warn "serializing ". ref($value). " for table display not yet handled";
643 %{$settingsref} = %$effective_settings;
644 %{$defaultref} = %defaults;
650 sub communigate_pro_queue {
651 my( $self, $svcnum, $method ) = (shift, shift, shift);
652 my $jobnum = ''; #don't actually care
653 $self->communigate_pro_queue_dep( \$jobnum, $svcnum, $method, @_);
656 sub communigate_pro_queue_dep {
657 my( $self, $jobnumref, $svcnum, $method ) = splice(@_,0,4);
659 my %kludge_methods = (
660 #'CreateAccount' => 'CreateAccount',
661 'UpdateAccountSettings' => 'UpdateAccountSettings',
662 'UpdateAccountPrefs' => 'cp_Scalar_Hash',
663 #'CreateDomain' => 'cp_Scalar_Hash',
664 #'CreateSharedDomain' => 'cp_Scalar_Hash',
665 'UpdateDomainSettings' => 'cp_Scalar_settingsHash',
666 'SetDomainAliases' => 'cp_Scalar_Array',
667 'SetAccountDefaults' => 'cp_Scalar_settingsHash',
668 'UpdateAccountDefaults' => 'cp_Scalar_settingsHash',
669 'SetAccountDefaultPrefs' => 'cp_Scalar_settingsHash',
670 'UpdateAccountDefaultPrefs' => 'cp_Scalar_settingsHash',
672 my $sub = exists($kludge_methods{$method})
673 ? $kludge_methods{$method}
674 : 'communigate_pro_command';
676 my $queue = new FS::queue {
678 'job' => "FS::part_export::communigate_pro::$sub",
680 my $error = $queue->insert(
682 $self->option('port'),
683 $self->option('login'),
684 $self->option('password'),
688 $$jobnumref = $queue->jobnum unless $error;
693 sub communigate_pro_runcommand {
694 my( $self, $method ) = (shift, shift);
696 communigate_pro_command(
698 $self->option('port'),
699 $self->option('login'),
700 $self->option('password'),
707 #XXX one sub per arg prototype is lame. more magic? i suppose queue needs
708 # to store data strctures properly instead of just an arg list. right.
711 my( $machine, $port, $login, $password, $method, $scalar, %hash ) = @_;
712 my @args = ( $scalar, \%hash );
713 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
716 sub cp_Scalar_Array {
717 my( $machine, $port, $login, $password, $method, $scalar, @array ) = @_;
718 my @args = ( $scalar, \@array );
719 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
723 # my( $machine, $port, $login, $password, $method, %hash ) = @_;
724 # my @args = ( \%hash );
725 # communigate_pro_command( $machine, $port, $login, $password, $method, @args );
728 sub cp_Scalar_settingsHash {
729 my( $machine, $port, $login, $password, $method, $domain, %settings ) = @_;
730 for (qw( AccessModes DomainAccessModes )) {
731 $settings{$_} = [split(' ',$settings{$_})] if $settings{$_};
733 my @args = ( 'domain' => $domain, 'settings' => \%settings );
734 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
738 # my( $machine, $port, $login, $password, $method, %args ) = @_;
739 # my $accountName = delete $args{'accountName'};
740 # my $accountType = delete $args{'accountType'};
741 # my $externalFlag = delete $args{'externalFlag'};
742 # $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
743 # my @args = ( accountName => $accountName,
744 # accountType => $accountType,
745 # settings => \%args,
747 # #externalFlag => $externalFlag,
748 # push @args, externalFlag => $externalFlag if $externalFlag;
750 # communigate_pro_command( $machine, $port, $login, $password, $method, @args );
754 sub UpdateAccountSettings {
755 my( $machine, $port, $login, $password, $method, $accountName, %args ) = @_;
756 $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
757 my @args = ( $accountName, \%args );
758 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
761 sub communigate_pro_command { #subroutine, not method
762 my( $machine, $port, $login, $password, $method, @args ) = @_;
766 my $cli = new CGP::CLI( {
767 'PeerAddr' => $machine,
770 'password' => $password,
771 } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";
773 #warn "$method ". Dumper(@args) if $DEBUG;
775 my $return = $cli->$method(@args)
776 or die "Communigate Pro error: ". $cli->getErrMessage. "\n";
778 $cli->Logout; # or die "Can't logout of CGPro: $CGP::ERR_STRING\n";