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' => ( $svc_acct->cgp_accessmodes
75 || $self->option('AccessModes') ),
76 'RealName' => $svc_acct->finger,
77 'Password' => $svc_acct->_password,
78 map { $quotas{$_} => $svc_acct->$_() }
79 grep $svc_acct->$_(), keys %quotas
81 #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
82 # RPOP modifications, accepts mail to all, add trailer to sent mail
83 #phase 3: archive messages, mailing lists
85 my @options = ( 'CreateAccount',
86 'accountName' => $self->export_username($svc_acct),
87 'accountType' => ( $svc_acct->cgp_type
88 || $self->option('accountType') ),
89 'settings' => \%settings
92 push @options, 'externalFlag' => $self->option('externalFlag')
93 if $self->option('externalFlag');
95 #let's do the create realtime too, for much the same reasons, and to avoid
96 #pain of trying to queue w/dep the prefs & aliases
97 eval { $self->communigate_pro_runcommand( @options ) };
102 $prefs{'DeleteMode'} = $svc_acct->cgp_deletemode if $svc_acct->cgp_deletemode;
103 $prefs{'EmptyTrash'} = $svc_acct->cgp_emptytrash if $svc_acct->cgp_emptytrash;
104 #phase 2: language, time zone, layout, pronto style, send read receipts
106 my $pref_err = $self->communigate_pro_queue( $svc_acct->svcnum,
107 'UpdateAccountPrefs',
108 $self->export_username($svc_acct),
111 warn "WARNING: error queueing UpdateAccountPrefs job: $pref_err"
116 if ( $svc_acct->cgp_aliases ) {
117 my $alias_err = $self->communigate_pro_queue( $svc_acct->svcnum,
119 $self->export_username($svc_acct),
120 [ split(/\s*,\s*/, $svc_acct->cgp_aliases) ],
122 warn "WARNING: error queueing SetAccountAliases job: $alias_err"
130 sub _export_insert_svc_domain {
131 my( $self, $svc_domain ) = (shift, shift);
133 my $create = $self->option('create_domain') || 'CreateDomain';
135 my @options = ( $svc_domain->svcnum, $create, $svc_domain->domain,
136 #other domain creation options?
138 push @options, 'AccountsLimit' => $svc_domain->max_accounts
139 if $svc_domain->max_accounts;
141 $self->communigate_pro_queue( @options );
144 sub _export_insert_svc_forward {
145 my( $self, $svc_forward ) = (shift, shift);
147 my $src = $svc_forward->src || $svc_forward->srcsvc_acct->email;
148 my $dst = $svc_forward->dst || $svc_forward->dstsvc_acct->email;
150 #real-time here, presuming CGP does some dup detection?
151 eval { $self->communigate_pro_runcommand( 'CreateForwarder', $src, $dst); };
157 sub _export_replace {
158 my( $self, $new, $old ) = (shift, shift, shift);
160 my $table = $new->table;
161 my $method = "_export_replace_$table";
162 $self->$method($new, $old, @_);
165 sub _export_replace_svc_acct {
166 my( $self, $new, $old ) = (shift, shift, shift);
168 #let's just do the rename part realtime rather than trying to queue
169 #w/dependencies. we don't want FS winding up out-of-sync with the wrong
170 #username and a queued job anyway. right??
171 if ( $self->export_username($old) ne $self->export_username($new) ) {
172 eval { $self->communigate_pro_runcommand(
174 $self->export_username($old),
175 $self->export_username($new),
180 if ( $new->_password ne $old->_password
181 && '*SUSPENDED* '.$old->_password ne $new->_password
183 $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
184 $self->export_username($new), $new->_password
190 $settings{'RealName'} = $new->finger
191 if $old->finger ne $new->finger;
192 $settings{$quotas{$_}} = $new->$_()
193 foreach grep $old->$_() ne $new->$_(), keys %quotas;
194 $settings{'accountType'} = $new->cgp_type
195 if $old->cgp_type ne $new->cgp_type;
196 $settings{'AccessModes'} = $new->cgp_accessmodes
197 if $old->cgp_accessmodes ne $new->cgp_accessmodes
198 || $old->cgp_type ne $new->cgp_type;
200 #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
201 # RPOP modifications, accepts mail to all, add trailer to sent mail
202 #phase 3: archive messages, mailing lists
204 if ( keys %settings ) {
205 my $error = $self->communigate_pro_queue(
207 'UpdateAccountSettings',
208 $self->export_username($new),
211 return $error if $error;
216 $prefs{'DeleteMode'} = $new->cgp_deletemode
217 if $old->cgp_deletemode ne $new->cgp_deletemode;
218 $prefs{'EmptyTrash'} = $new->cgp_emptytrash
219 if $old->cgp_emptytrash ne $new->cgp_emptytrash;
220 #phase 2: language, time zone, layout, pronto style, send read receipts
222 my $pref_err = $self->communigate_pro_queue( $new->svcnum,
223 'UpdateAccountPrefs',
224 $self->export_username($new),
227 warn "WARNING: error queueing UpdateAccountPrefs job: $pref_err"
231 if ( $old->cgp_aliases ne $new->cgp_aliases ) {
232 my $error = $self->communigate_pro_queue(
235 $self->export_username($new),
236 [ split(/\s*,\s*/, $new->cgp_aliases) ],
238 return $error if $error;
245 sub _export_replace_svc_domain {
246 my( $self, $new, $old ) = (shift, shift, shift);
248 if ( $old->domain ne $new->domain ) {
249 my $error = $self->communigate_pro_queue( $new->svcnum, 'RenameDomain',
250 $old->domain, $new->domain,
252 return $error if $error;
255 if ( $old->max_accounts ne $new->max_accounts ) {
256 my $error = $self->communigate_pro_queue( $new->svcnum,
257 'UpdateDomainSettings',
259 'AccountsLimit' => ($new->max_accounts || 'default'),
261 return $error if $error;
264 #other kinds of changes?
269 sub _export_replace_svc_forward {
270 my( $self, $new, $old ) = (shift, shift, shift);
272 my $osrc = $old->src || $old->srcsvc_acct->email;
273 my $nsrc = $new->src || $new->srcsvc_acct->email;
274 my $odst = $old->dst || $old->dstsvc_acct->email;
275 my $ndst = $new->dst || $new->dstsvc_acct->email;
277 if ( $odst ne $ndst ) {
279 #no change command, so delete and create (real-time)
280 eval { $self->communigate_pro_runcommand('DeleteForwarder', $osrc) };
282 eval { $self->communigate_pro_runcommand('CreateForwarder', $nsrc, $ndst)};
285 } elsif ( $osrc ne $nsrc ) {
287 #real-time here, presuming CGP does some dup detection?
288 eval { $self->communigate_pro_runcommand( 'RenameForwarder', $osrc, $nsrc)};
292 warn "communigate replace called for svc_forward with no changes\n";#confess
299 my( $self, $svc_x ) = (shift, shift);
301 my $table = $svc_x->table;
302 my $method = "_export_delete_$table";
303 $self->$method($svc_x, @_);
306 sub _export_delete_svc_acct {
307 my( $self, $svc_acct ) = (shift, shift);
309 $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
310 $self->export_username($svc_acct),
314 sub _export_delete_svc_domain {
315 my( $self, $svc_domain ) = (shift, shift);
317 $self->communigate_pro_queue( $svc_domain->svcnum, 'DeleteDomain',
319 #XXX turn on force option for domain deletion?
323 sub _export_delete_svc_forward {
324 my( $self, $svc_forward ) = (shift, shift);
326 $self->communigate_pro_queue( $svc_forward->svcnum, 'DeleteForwarder',
327 ($svc_forward->src || $svc_forward->srcsvc_acct->email),
331 sub _export_suspend {
332 my( $self, $svc_x ) = (shift, shift);
334 my $table = $svc_x->table;
335 my $method = "_export_suspend_$table";
336 $self->$method($svc_x, @_);
340 sub _export_suspend_svc_acct {
341 my( $self, $svc_acct ) = (shift, shift);
343 #XXX is this the desired suspnsion action?
345 $self->communigate_pro_queue(
347 'UpdateAccountSettings',
348 $self->export_username($svc_acct),
349 'AccessModes' => 'Mail',
354 sub _export_suspend_svc_domain {
355 my( $self, $svc_domain) = (shift, shift);
357 #XXX domain operations
362 sub _export_unsuspend {
363 my( $self, $svc_x ) = (shift, shift);
365 my $table = $svc_x->table;
366 my $method = "_export_unsuspend_$table";
367 $self->$method($svc_x, @_);
371 sub _export_unsuspend_svc_acct {
372 my( $self, $svc_acct ) = (shift, shift);
374 $self->communigate_pro_queue(
376 'UpdateAccountSettings',
377 $self->export_username($svc_acct),
378 'AccessModes' => ( $svc_acct->cgp_accessmodes
379 || $self->option('AccessModes') ),
384 sub _export_unsuspend_svc_domain {
385 my( $self, $svc_domain) = (shift, shift);
387 #XXX domain operations
393 sub export_getsettings {
394 my($self, $svc_x) = (shift, shift);
396 my $table = $svc_x->table;
397 my $method = "export_getsettings_$table";
399 $self->can($method) ? $self->$method($svc_x, @_) : '';
403 sub export_getsettings_svc_domain {
404 my($self, $svc_domain, $settingsref, $defaultref ) = @_;
406 my $settings = eval { $self->communigate_pro_runcommand(
412 my $effective_settings = eval { $self->communigate_pro_runcommand(
413 'GetDomainEffectiveSettings',
418 my $acct_defaults = eval { $self->communigate_pro_runcommand(
419 'GetAccountDefaults',
424 #warn Dumper($acct_defaults);
426 %$effective_settings = ( %$effective_settings,
427 map { ("Acct. Default $_" => $acct_defaults->{$_}); }
428 keys(%$acct_defaults)
431 #false laziness w/below
433 my %defaults = map { $_ => 1 }
434 grep !exists(${$settings}{$_}), keys %$effective_settings;
436 foreach my $key ( grep ref($effective_settings->{$_}),
437 keys %$effective_settings )
439 my $value = $effective_settings->{$key};
440 if ( ref($value) eq 'ARRAY' ) {
441 $effective_settings->{$key} = join(' ', @$value);
444 warn "serializing ". ref($value). " for table display not yet handled";
448 %{$settingsref} = %$effective_settings;
449 %{$defaultref} = %defaults;
454 sub export_getsettings_svc_acct {
455 my($self, $svc_acct, $settingsref, $defaultref ) = @_;
457 my $settings = eval { $self->communigate_pro_runcommand(
458 'GetAccountSettings',
463 delete($settings->{'Password'});
465 my $effective_settings = eval { $self->communigate_pro_runcommand(
466 'GetAccountEffectiveSettings',
471 delete($effective_settings->{'Password'});
473 #prefs/effectiveprefs too
475 my $prefs = eval { $self->communigate_pro_runcommand(
481 my $effective_prefs = eval { $self->communigate_pro_runcommand(
482 'GetAccountEffectivePrefs',
487 %$effective_settings = ( %$effective_settings,
488 map { ("Pref $_" => $effective_prefs->{$_}); }
489 keys(%$effective_prefs)
491 %$settings = ( %$settings,
492 map { ("Pref $_" => $prefs->{$_}); }
498 my $aliases = eval { $self->communigate_pro_runcommand(
504 $effective_settings->{'Aliases'} = join(', ', @$aliases);
505 $settings->{'Aliases'} = join(', ', @$aliases);
507 #false laziness w/above
509 my %defaults = map { $_ => 1 }
510 grep !exists(${$settings}{$_}), keys %$effective_settings;
512 foreach my $key ( grep ref($effective_settings->{$_}),
513 keys %$effective_settings )
515 my $value = $effective_settings->{$key};
516 if ( ref($value) eq 'ARRAY' ) {
517 $effective_settings->{$key} = join(' ', @$value);
520 warn "serializing ". ref($value). " for table display not yet handled";
524 %{$settingsref} = %$effective_settings;
525 %{$defaultref} = %defaults;
531 sub communigate_pro_queue {
532 my( $self, $svcnum, $method ) = (shift, shift, shift);
533 my $jobnum = ''; #don't actually care
534 $self->communigate_pro_queue_dep( \$jobnum, $svcnum, $method, @_);
537 sub communigate_pro_queue_dep {
538 my( $self, $jobnumref, $svcnum, $method ) = splice(@_,0,4);
540 my %kludge_methods = (
541 'CreateAccount' => 'CreateAccount',
542 'UpdateAccountSettings' => 'UpdateAccountSettings',
543 'UpdateAccountPrefs' => 'cp_Scalar_Hash',
544 'CreateDomain' => 'cp_Scalar_Hash',
545 'CreateSharedDomain' => 'cp_Scalar_Hash',
546 'UpdateDomainSettings' => 'UpdateDomainSettings',
548 my $sub = exists($kludge_methods{$method})
549 ? $kludge_methods{$method}
550 : 'communigate_pro_command';
552 my $queue = new FS::queue {
554 'job' => "FS::part_export::communigate_pro::$sub",
556 my $error = $queue->insert(
558 $self->option('port'),
559 $self->option('login'),
560 $self->option('password'),
564 $$jobnumref = $queue->jobnum unless $error;
569 sub communigate_pro_runcommand {
570 my( $self, $method ) = (shift, shift);
572 communigate_pro_command(
574 $self->option('port'),
575 $self->option('login'),
576 $self->option('password'),
583 #XXX one sub per arg prototype is lame. more magic? i suppose queue needs
584 # to store data strctures properly instead of just an arg list. right.
587 my( $machine, $port, $login, $password, $method, $scalar, %hash ) = @_;
588 my @args = ( $scalar, \%hash );
589 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
593 # my( $machine, $port, $login, $password, $method, %hash ) = @_;
594 # my @args = ( \%hash );
595 # communigate_pro_command( $machine, $port, $login, $password, $method, @args );
598 sub UpdateDomainSettings {
599 my( $machine, $port, $login, $password, $method, $domain, %settings ) = @_;
600 my @args = ( 'domain' => $domain, 'settings' => \%settings );
601 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
605 my( $machine, $port, $login, $password, $method, %args ) = @_;
606 my $accountName = delete $args{'accountName'};
607 my $accountType = delete $args{'accountType'};
608 my $externalFlag = delete $args{'externalFlag'};
609 $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
610 my @args = ( accountName => $accountName,
611 accountType => $accountType,
614 #externalFlag => $externalFlag,
615 push @args, externalFlag => $externalFlag if $externalFlag;
617 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
621 sub UpdateAccountSettings {
622 my( $machine, $port, $login, $password, $method, $accountName, %args ) = @_;
623 $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
624 my @args = ( $accountName, \%args );
625 communigate_pro_command( $machine, $port, $login, $password, $method, @args );
628 sub communigate_pro_command { #subroutine, not method
629 my( $machine, $port, $login, $password, $method, @args ) = @_;
633 my $cli = new CGP::CLI( {
634 'PeerAddr' => $machine,
637 'password' => $password,
638 } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";
640 #warn "$method ". Dumper(@args) if $DEBUG;
642 my $return = $cli->$method(@args)
643 or die "Communigate Pro error: ". $cli->getErrMessage. "\n";
645 $cli->Logout; # or die "Can't logout of CGPro: $CGP::ERR_STRING\n";