7f5cece59afa033b79e4755da94dce7ebf395c0d
[freeside.git] / FS / FS / part_export / communigate_pro.pm
1 package FS::part_export::communigate_pro;
2
3 use strict;
4 use vars qw(@ISA %info %options %quotas $DEBUG);
5 use Data::Dumper;
6 use Tie::IxHash;
7 use FS::part_export;
8 use FS::queue;
9
10 @ISA = qw(FS::part_export);
11
12 $DEBUG = 1;
13
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)',
19                        type    => 'select',
20                        options => [qw(MultiMailbox TextMailbox MailDirMailbox AGrade BGrade CGrade)],
21                        default => 'MultiMailbox',
22                      },
23   'externalFlag'  => { label   => 'Create accounts with an external (visible for legacy mailers) INBOX.',
24                        type    => 'checkbox',
25                      },
26   'AccessModes'   => { label   => 'Access modes (default when not specified in service)',
27                        default => 'Mail POP IMAP PWD WebMail WebSite',
28                      },
29   'create_domain' => { label   => 'Domain creation API call',
30                        type    => 'select',
31                        options => [qw( CreateDomain CreateSharedDomain )],
32                      }
33 ;
34
35 %info = (
36   'svc'     => [qw( svc_acct svc_domain svc_forward svc_mailinglist )],
37   'desc'    => 'Real-time export of accounts, domains, mail forwards and mailing lists to a CommuniGate Pro mail server',
38   'options' => \%options,
39   'notes'   => <<'END'
40 Real time export of accounts, domains, mail forwards and mailing lists to a
41 <a href="http://www.stalker.com/CommuniGatePro/">CommuniGate Pro</a>
42 mail server.  The
43 <a href="http://www.stalker.com/CGPerl/">CommuniGate Pro Perl Interface</a>
44 must be installed as CGP::CLI.
45 END
46 );
47
48 %quotas = (
49   'quota'        => 'MaxAccountSize',
50   'file_quota'   => 'MaxWebSize',
51   'file_maxnum'  => 'MaxWebFiles',
52   'file_maxsize' => 'MaxFileSize',
53 );
54
55 sub rebless { shift; }
56
57 sub export_username {
58   my($self, $svc_acct) = (shift, shift);
59   $svc_acct->email;
60 }
61
62 sub _export_insert {
63   my( $self, $svc_x ) = (shift, shift);
64
65   my $table = $svc_x->table;
66   my $method = "_export_insert_$table";
67   $self->$method($svc_x, @_);
68 }
69
70 sub _export_insert_svc_acct {
71   my( $self, $svc_acct ) = (shift, shift);
72
73   my %settings = (
74     'AccessModes'    => [ split(' ', ( $svc_acct->cgp_accessmodes
75                                        || $self->option('AccessModes') )
76                                )
77                         ],
78     'RealName'       => $svc_acct->finger,
79     'Password'       => $svc_acct->_password,
80     map { $quotas{$_} => $svc_acct->$_() }
81         grep $svc_acct->$_(), keys %quotas
82   );
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
86
87   my @options = ( 'CreateAccount',
88     'accountName'    => $self->export_username($svc_acct),
89     'accountType'    => ( $svc_acct->cgp_type
90                           || $self->option('accountType') ), 
91     'settings'       => \%settings
92   );
93
94   push @options, 'externalFlag'   => $self->option('externalFlag')
95     if $self->option('externalFlag');
96
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 ) };
100   return $@ if $@;
101
102   #preferences
103   my %prefs = ();
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
107   if ( keys %prefs ) {
108     my $pref_err = $self->communigate_pro_queue( $svc_acct->svcnum,
109       'UpdateAccountPrefs',
110       $self->export_username($svc_acct),
111       %prefs,
112     );
113    warn "WARNING: error queueing UpdateAccountPrefs job: $pref_err"
114     if $pref_err;
115   }
116
117   #aliases
118   if ( $svc_acct->cgp_aliases ) {
119     my $alias_err = $self->communigate_pro_queue( $svc_acct->svcnum,
120       'SetAccountAliases',
121       $self->export_username($svc_acct),
122       [ split(/\s*[,\s]\s*/, $svc_acct->cgp_aliases) ],
123     );
124     warn "WARNING: error queueing SetAccountAliases job: $alias_err"
125       if $alias_err;
126   }
127
128   '';
129
130 }
131
132 sub _export_insert_svc_domain {
133   my( $self, $svc_domain ) = (shift, shift);
134
135   my $create = $self->option('create_domain') || 'CreateDomain';
136
137   my %settings = (
138     'DomainAccessModes'    => [ split(' ', $svc_domain->cgp_accessmodes ) ],
139   );
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;
144
145   my @options = ( $create, $svc_domain->domain, \%settings );
146
147   eval { $self->communigate_pro_runcommand( @options ) };
148   return $@ if $@;
149
150   #aliases
151   if ( $svc_domain->cgp_aliases ) {
152     my $alias_err = $self->communigate_pro_queue( $svc_domain->svcnum,
153       'SetDomainAliases',
154       $svc_domain->domain,
155       split(/\s*[,\s]\s*/, $svc_domain->cgp_aliases),
156     );
157     warn "WARNING: error queueing SetDomainAliases job: $alias_err"
158       if $alias_err;
159   }
160
161   #account defaults
162   my $def_err = $self->communigate_pro_queue( $svc_domain->svcnum,
163     'SetAccountDefaults',
164     $svc_domain->domain,
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,
172   );
173   warn "WARNING: error queueing SetAccountDefaults job: $def_err"
174     if $def_err;
175
176   #account defaults prefs
177   my $pref_err = $self->communigate_pro_queue( $svc_domain->svcnum,
178     'SetAccountDefaultPrefs',
179     $svc_domain->domain,
180     'DeleteMode' => $svc_domain->acct_def_cgp_deletemode,
181     'EmptyTrash' => $svc_domain->acct_def_cgp_emptytrash,
182   );
183   warn "WARNING: error queueing SetAccountDefaultPrefs job: $pref_err"
184     if $pref_err;
185
186   '';
187
188 }
189
190 sub _export_insert_svc_forward {
191   my( $self, $svc_forward ) = (shift, shift);
192
193   my $src = $svc_forward->src || $svc_forward->srcsvc_acct->email;
194   my $dst = $svc_forward->dst || $svc_forward->dstsvc_acct->email;
195
196   #real-time here, presuming CGP does some dup detection?
197   eval { $self->communigate_pro_runcommand( 'CreateForwarder', $src, $dst); };
198   return $@ if $@;
199
200   '';
201 }
202
203 sub _export_insert_svc_mailinglist {
204   my( $self, $svc_mlist ) = (shift, shift);
205
206   my @members = map $_->email_address,
207                     $svc_mlist->mailinglist->mailinglistmember;
208
209   #real-time here, presuming CGP does some dup detection
210   eval { $self->communigate_pro_runcommand(
211            'CreateGroup',
212            $svc_mlist->username.'@'.$svc_mlist->domain,
213            { 'RealName'      => $svc_mlist->listname,
214              'SetReplyTo'    => ( $svc_mlist->reply_to         ? 'YES' : 'NO' ),
215              'RemoveAuthor'  => ( $svc_mlist->remove_from      ? 'YES' : 'NO' ),
216              'RejectAuto'    => ( $svc_mlist->reject_auto      ? 'YES' : 'NO' ),
217              'RemoveToAndCc' => ( $svc_mlist->remove_to_and_cc ? 'YES' : 'NO' ),
218              'Members'       => \@members,
219            }
220          );
221        };
222   return $@ if $@;
223
224   '';
225
226 }
227
228 sub _export_replace {
229   my( $self, $new, $old ) = (shift, shift, shift);
230
231   my $table = $new->table;
232   my $method = "_export_replace_$table";
233   $self->$method($new, $old, @_);
234 }
235
236 sub _export_replace_svc_acct {
237   my( $self, $new, $old ) = (shift, shift, shift);
238
239   #let's just do the rename part realtime rather than trying to queue
240   #w/dependencies.  we don't want FS winding up out-of-sync with the wrong
241   #username and a queued job anyway.  right??
242   if ( $self->export_username($old) ne $self->export_username($new) ) {
243     eval { $self->communigate_pro_runcommand(
244       'RenameAccount',
245       $self->export_username($old),
246       $self->export_username($new),
247     ) };
248     return $@ if $@;
249   }
250
251   if ( $new->_password ne $old->_password
252        && '*SUSPENDED* '.$old->_password ne $new->_password
253   ) {
254     $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
255                                   $self->export_username($new), $new->_password
256                                 );
257   }
258
259   my %settings = ();
260
261   $settings{'RealName'} = $new->finger
262     if $old->finger ne $new->finger;
263   $settings{$quotas{$_}} = $new->$_()
264     foreach grep $old->$_() ne $new->$_(), keys %quotas;
265   $settings{'accountType'} = $new->cgp_type
266     if $old->cgp_type ne $new->cgp_type;
267   $settings{'AccessModes'} = $new->cgp_accessmodes
268     if $old->cgp_accessmodes ne $new->cgp_accessmodes
269     || $old->cgp_type ne $new->cgp_type;
270
271   #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
272   # RPOP modifications, accepts mail to all, add trailer to sent mail
273   #phase 3: archive messages, mailing lists
274
275   if ( keys %settings ) {
276     my $error = $self->communigate_pro_queue(
277       $new->svcnum,
278       'UpdateAccountSettings',
279       $self->export_username($new),
280       %settings,
281     );
282     return $error if $error;
283   }
284
285   #preferences
286   my %prefs = ();
287   $prefs{'DeleteMode'} = $new->cgp_deletemode
288     if $old->cgp_deletemode ne $new->cgp_deletemode;
289   $prefs{'EmptyTrash'} = $new->cgp_emptytrash
290     if $old->cgp_emptytrash ne $new->cgp_emptytrash;
291   #phase 2: language, time zone, layout, pronto style, send read receipts
292   if ( keys %prefs ) {
293     my $pref_err = $self->communigate_pro_queue( $new->svcnum,
294       'UpdateAccountPrefs',
295       $self->export_username($new),
296       %prefs,
297     );
298    warn "WARNING: error queueing UpdateAccountPrefs job: $pref_err"
299     if $pref_err;
300   }
301
302   if ( $old->cgp_aliases ne $new->cgp_aliases ) {
303     my $error = $self->communigate_pro_queue(
304       $new->svcnum,
305       'SetAccountAliases',
306       $self->export_username($new),
307       [ split(/\s*[,\s]\s*/, $new->cgp_aliases) ],
308     );
309     return $error if $error;
310   }
311
312   '';
313
314 }
315
316 sub _export_replace_svc_domain {
317   my( $self, $new, $old ) = (shift, shift, shift);
318
319   if ( $old->domain ne $new->domain ) {
320     my $error = $self->communigate_pro_queue( $new->svcnum, 'RenameDomain',
321       $old->domain, $new->domain,
322     );
323     return $error if $error;
324   }
325   my %settings = ();
326   $settings{'AccountsLimit'} = $new->max_accounts
327     if $old->max_accounts ne $new->max_accounts;
328   $settings{'DomainAccessModes'} = $new->cgp_accessmodes
329     if $old->cgp_accessmodes ne $new->cgp_accessmodes;
330   $settings{'AdminDomainName'} =
331     $new->parent_svcnum ? $new->parent_svc_x->domain : ''
332       if $old->parent_svcnum != $new->parent_svcnum;
333
334   if ( keys %settings ) {
335     my $error = $self->communigate_pro_queue( $new->svcnum,
336       'UpdateDomainSettings',
337       $new->domain,
338       %settings,
339     );
340     return $error if $error;
341   }
342
343   if ( $old->cgp_aliases ne $new->cgp_aliases ) {
344     my $error = $self->communigate_pro_queue( $new->svcnum,
345       'SetDomainAliases',
346       $new->domain,
347       split(/\s*[,\s]\s*/, $new->cgp_aliases),
348     );
349     return $error if $error;
350   }
351
352   #below this identical to insert... any value to doing an Update here?
353   #not seeing any big one... i guess it would be nice to avoid the update
354   #when things haven't changed
355
356   #account defaults
357   my $def_err = $self->communigate_pro_queue( $new->svcnum,
358     'SetAccountDefaults',
359     $new->domain,
360     'PWDAllowed'       => ( $new->acct_def_password_selfchange ? 'YES' : 'NO' ),
361     'PasswordRecovery' => ( $new->acct_def_password_recover    ? 'YES' : 'NO' ),
362     'AccessModes'      => $new->acct_def_cgp_accessmodes,
363     'MaxAccountSize'   => $new->acct_def_quota,
364     'MaxWebSize'       => $new->acct_def_file_quota,
365     'MaxWebFile'       => $new->acct_def_file_maxnum,
366     'MaxFileSize'      => $new->acct_def_file_maxsize,
367   );
368   warn "WARNING: error queueing SetAccountDefaults job: $def_err"
369     if $def_err;
370
371   #account defaults prefs
372   my $pref_err = $self->communigate_pro_queue( $new->svcnum,
373     'SetAccountDefaultPrefs',
374     $new->domain,
375     'DeleteMode' => $new->acct_def_cgp_deletemode,
376     'EmptyTrash' => $new->acct_def_cgp_emptytrash,
377   );
378   warn "WARNING: error queueing SetAccountDefaultPrefs job: $pref_err"
379     if $pref_err;
380
381   '';
382 }
383
384 sub _export_replace_svc_forward {
385   my( $self, $new, $old ) = (shift, shift, shift);
386
387   my $osrc = $old->src || $old->srcsvc_acct->email;
388   my $nsrc = $new->src || $new->srcsvc_acct->email;
389   my $odst = $old->dst || $old->dstsvc_acct->email;
390   my $ndst = $new->dst || $new->dstsvc_acct->email;
391
392   if ( $odst ne $ndst ) {
393
394     #no change command, so delete and create (real-time)
395     eval { $self->communigate_pro_runcommand('DeleteForwarder', $osrc) };
396     return $@ if $@;
397     eval { $self->communigate_pro_runcommand('CreateForwarder', $nsrc, $ndst)};
398     return $@ if $@;
399
400   } elsif ( $osrc ne $nsrc ) {
401
402     #real-time here, presuming CGP does some dup detection?
403     eval { $self->communigate_pro_runcommand( 'RenameForwarder', $osrc, $nsrc)};
404     return $@ if $@;
405
406   } else {
407     warn "communigate replace called for svc_forward with no changes\n";#confess
408   }
409
410   '';
411 }
412
413 sub _export_replace_svc_mailinglist {
414   my( $self, $new, $old ) = (shift, shift, shift);
415
416   my $oldGroupName = $old->username.'@'.$old->domain;
417   my $newGroupName = $new->username.'@'.$new->domain;
418
419   if ( $oldGroupName ne $newGroupName ) {
420     eval { $self->communigate_pro_runcommand(
421              'RenameGroup', $oldGroupName, $newGroupName ); };
422     return $@ if $@;
423   }
424
425   my @members = map $_->email_address,
426                 $new->mailinglist->mailinglistmember;
427
428   #real-time here, presuming CGP does some dup detection
429   eval { $self->communigate_pro_runcommand(
430            'SetGroup', $newGroupName,
431            { 'RealName'      => $new->listname,
432              'SetReplyTo'    => ( $new->reply_to         ? 'YES' : 'NO' ),
433              'RemoveAuthor'  => ( $new->remove_from      ? 'YES' : 'NO' ),
434              'RejectAuto'    => ( $new->reject_auto      ? 'YES' : 'NO' ),
435              'RemoveToAndCc' => ( $new->remove_to_and_cc ? 'YES' : 'NO' ),
436              'Members'       => \@members,
437            }
438          );
439        };
440   return $@ if $@;
441
442   '';
443
444 }
445
446 sub _export_delete {
447   my( $self, $svc_x ) = (shift, shift);
448
449   my $table = $svc_x->table;
450   my $method = "_export_delete_$table";
451   $self->$method($svc_x, @_);
452 }
453
454 sub _export_delete_svc_acct {
455   my( $self, $svc_acct ) = (shift, shift);
456
457   $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
458     $self->export_username($svc_acct),
459   );
460 }
461
462 sub _export_delete_svc_domain {
463   my( $self, $svc_domain ) = (shift, shift);
464
465   $self->communigate_pro_queue( $svc_domain->svcnum, 'DeleteDomain',
466     $svc_domain->domain,
467     #XXX turn on force option for domain deletion?
468   );
469 }
470
471 sub _export_delete_svc_forward {
472   my( $self, $svc_forward ) = (shift, shift);
473
474   $self->communigate_pro_queue( $svc_forward->svcnum, 'DeleteForwarder',
475     ($svc_forward->src || $svc_forward->srcsvc_acct->email),
476   );
477 }
478
479 sub _export_delete_svc_mailinglist {
480   my( $self, $svc_mailinglist ) = (shift, shift);
481
482   #real-time here, presuming CGP does some dup detection
483   eval { $self->communigate_pro_runcommand(
484            'DeleteGroup',
485            $svc_mailinglist->username.'@'.$svc_mailinglist->domain,
486          );
487        };
488   return $@ if $@;
489
490   '';
491
492 }
493
494 sub _export_suspend {
495   my( $self, $svc_x ) = (shift, shift);
496
497   my $table = $svc_x->table;
498   my $method = "_export_suspend_$table";
499   $self->$method($svc_x, @_);
500
501 }
502
503 sub _export_suspend_svc_acct {
504   my( $self, $svc_acct ) = (shift, shift);
505
506   #XXX is this the desired suspnsion action?
507
508    $self->communigate_pro_queue(
509     $svc_acct->svcnum,
510     'UpdateAccountSettings',
511     $self->export_username($svc_acct),
512     'AccessModes' => 'Mail',
513   );
514
515 }
516
517 sub _export_suspend_svc_domain {
518   my( $self, $svc_domain) = (shift, shift);
519
520   #XXX domain operations
521   '';
522
523 }
524
525 sub _export_unsuspend {
526   my( $self, $svc_x ) = (shift, shift);
527
528   my $table = $svc_x->table;
529   my $method = "_export_unsuspend_$table";
530   $self->$method($svc_x, @_);
531
532 }
533
534 sub _export_unsuspend_svc_acct {
535   my( $self, $svc_acct ) = (shift, shift);
536
537   $self->communigate_pro_queue(
538     $svc_acct->svcnum,
539     'UpdateAccountSettings',
540     $self->export_username($svc_acct),
541     'AccessModes' => ( $svc_acct->cgp_accessmodes
542                          || $self->option('AccessModes') ),
543   );
544
545 }
546
547 sub _export_unsuspend_svc_domain {
548   my( $self, $svc_domain) = (shift, shift);
549
550   #XXX domain operations
551   '';
552
553 }
554
555 sub export_mailinglistmember_insert {
556   my( $self, $svc_mailinglist, $mailinglistmember ) = (shift, shift, shift);
557   $svc_mailinglist->replace();
558 }
559
560 sub export_mailinglistmember_replace {
561   my( $self, $svc_mailinglist, $new, $old ) = (shift, shift, shift, shift);
562   die "no way to do this from the UI right now";
563 }
564
565 sub export_mailinglistmember_delete {
566   my( $self, $svc_mailinglist, $mailinglistmember ) = (shift, shift, shift);
567   $svc_mailinglist->replace();
568 }
569
570 sub export_getsettings {
571   my($self, $svc_x) = (shift, shift);
572
573   my $table = $svc_x->table;
574   my $method = "export_getsettings_$table";
575
576   $self->can($method) ? $self->$method($svc_x, @_) : '';
577
578 }
579
580 sub export_getsettings_svc_domain {
581   my($self, $svc_domain, $settingsref, $defaultref ) = @_;
582
583   my $settings = eval { $self->communigate_pro_runcommand(
584     'GetDomainSettings',
585     $svc_domain->domain
586   ) };
587   return $@ if $@;
588
589   my $effective_settings = eval { $self->communigate_pro_runcommand(
590     'GetDomainEffectiveSettings',
591     $svc_domain->domain
592   ) };
593   return $@ if $@;
594
595   my $acct_defaults = eval { $self->communigate_pro_runcommand(
596     'GetAccountDefaults',
597     $svc_domain->domain
598   ) };
599   return $@ if $@;
600
601   my $acct_defaultprefs = eval { $self->communigate_pro_runcommand(
602     'GetAccountDefaultPrefs',
603     $svc_domain->domain
604   ) };
605   return $@ if $@;
606
607   %$effective_settings = (
608     %$effective_settings,
609     ( map { ("Acct. Default $_" => $acct_defaults->{$_}); }
610           keys(%$acct_defaults)
611     ),
612     ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label??
613           keys(%$acct_defaultprefs)
614     ),
615   );
616   %$settings = (
617     %$settings,
618     ( map { ("Acct. Default $_" => $acct_defaults->{$_}); }
619           keys(%$acct_defaults)
620     ),
621     ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label??
622           keys(%$acct_defaultprefs)
623     ),
624   );
625
626   #aliases too
627   my $aliases = eval { $self->communigate_pro_runcommand(
628     'GetDomainAliases',
629     $svc_domain->domain
630   ) };
631   return $@ if $@;
632
633   $effective_settings->{'Aliases'} = join(', ', @$aliases);
634   $settings->{'Aliases'}           = join(', ', @$aliases);
635
636
637   #false laziness w/below
638   
639   my %defaults = map { $_ => 1 }
640                    grep !exists(${$settings}{$_}), keys %$effective_settings;
641
642   foreach my $key ( grep ref($effective_settings->{$_}),
643                     keys %$effective_settings )
644   {
645     my $value = $effective_settings->{$key};
646     if ( ref($value) eq 'ARRAY' ) {
647       $effective_settings->{$key} = join(' ', @$value);
648     } else {
649       #XXX
650       warn "serializing ". ref($value). " for table display not yet handled";
651     }
652   }
653
654   %{$settingsref} = %$effective_settings;
655   %{$defaultref} = %defaults;
656
657   '';
658 }
659
660 sub export_getsettings_svc_acct {
661   my($self, $svc_acct, $settingsref, $defaultref ) = @_;
662
663   my $settings = eval { $self->communigate_pro_runcommand(
664     'GetAccountSettings',
665     $svc_acct->email
666   ) };
667   return $@ if $@;
668
669   delete($settings->{'Password'});
670
671   my $effective_settings = eval { $self->communigate_pro_runcommand(
672     'GetAccountEffectiveSettings',
673     $svc_acct->email
674   ) };
675   return $@ if $@;
676
677   delete($effective_settings->{'Password'});
678
679   #prefs/effectiveprefs too
680
681   my $prefs = eval { $self->communigate_pro_runcommand(
682     'GetAccountPrefs',
683     $svc_acct->email
684   ) };
685   return $@ if $@;
686
687   my $effective_prefs = eval { $self->communigate_pro_runcommand(
688     'GetAccountEffectivePrefs',
689     $svc_acct->email
690   ) };
691   return $@ if $@;
692
693   %$effective_settings = ( %$effective_settings,
694                            map { ("Pref $_" => $effective_prefs->{$_}); }
695                                keys(%$effective_prefs)
696                          );
697   %$settings = ( %$settings,
698                  map { ("Pref $_" => $prefs->{$_}); }
699                      keys(%$prefs)
700                );
701
702   #aliases too
703
704   my $aliases = eval { $self->communigate_pro_runcommand(
705     'GetAccountAliases',
706     $svc_acct->email
707   ) };
708   return $@ if $@;
709
710   $effective_settings->{'Aliases'} = join(', ', @$aliases);
711   $settings->{'Aliases'}           = join(', ', @$aliases);
712
713   #false laziness w/above
714
715   my %defaults = map { $_ => 1 }
716                    grep !exists(${$settings}{$_}), keys %$effective_settings;
717
718   foreach my $key ( grep ref($effective_settings->{$_}),
719                     keys %$effective_settings )
720   {
721     my $value = $effective_settings->{$key};
722     if ( ref($value) eq 'ARRAY' ) {
723       $effective_settings->{$key} = join(' ', @$value);
724     } else {
725       #XXX
726       warn "serializing ". ref($value). " for table display not yet handled";
727     }
728   }
729
730   %{$settingsref} = %$effective_settings;
731   %{$defaultref} = %defaults;
732
733   '';
734
735 }
736
737 sub export_getsettings_svc_mailinglist {
738   my($self, $svc_mailinglist, $settingsref, $defaultref ) = @_;
739
740   my $settings = eval { $self->communigate_pro_runcommand(
741     'GetGroup',
742     $svc_mailinglist->username.'@'.$svc_mailinglist->domain,
743   ) };
744   return $@ if $@;
745
746   $settings->{'Members'} = join(', ', @{ $settings->{'Members'} } );
747
748   %{$settingsref} = %$settings;
749
750   '';
751 }
752
753 sub communigate_pro_queue {
754   my( $self, $svcnum, $method ) = (shift, shift, shift);
755   my $jobnum = ''; #don't actually care
756   $self->communigate_pro_queue_dep( \$jobnum, $svcnum, $method, @_);
757 }
758
759 sub communigate_pro_queue_dep {
760   my( $self, $jobnumref, $svcnum, $method ) = splice(@_,0,4);
761
762   my %kludge_methods = (
763     #'CreateAccount'             => 'CreateAccount',
764     'UpdateAccountSettings'     => 'UpdateAccountSettings',
765     'UpdateAccountPrefs'        => 'cp_Scalar_Hash',
766     #'CreateDomain'              => 'cp_Scalar_Hash',
767     #'CreateSharedDomain'        => 'cp_Scalar_Hash',
768     'UpdateDomainSettings'      => 'cp_Scalar_settingsHash',
769     'SetDomainAliases'          => 'cp_Scalar_Array',
770     'SetAccountDefaults'        => 'cp_Scalar_settingsHash',
771     'UpdateAccountDefaults'     => 'cp_Scalar_settingsHash',
772     'SetAccountDefaultPrefs'    => 'cp_Scalar_settingsHash',
773     'UpdateAccountDefaultPrefs' => 'cp_Scalar_settingsHash',
774   );
775   my $sub = exists($kludge_methods{$method})
776               ? $kludge_methods{$method}
777               : 'communigate_pro_command';
778
779   my $queue = new FS::queue {
780     'svcnum' => $svcnum,
781     'job'    => "FS::part_export::communigate_pro::$sub",
782   };
783   my $error = $queue->insert(
784     $self->machine,
785     $self->option('port'),
786     $self->option('login'),
787     $self->option('password'),
788     $method,
789     @_,
790   );
791   $$jobnumref = $queue->jobnum unless $error;
792
793   return $error;
794 }
795
796 sub communigate_pro_runcommand {
797   my( $self, $method ) = (shift, shift);
798
799   communigate_pro_command(
800     $self->machine,
801     $self->option('port'),
802     $self->option('login'),
803     $self->option('password'),
804     $method,
805     @_,
806   );
807
808 }
809
810 #XXX one sub per arg prototype is lame.  more magic?  i suppose queue needs
811 # to store data strctures properly instead of just an arg list.  right.
812
813 sub cp_Scalar_Hash {
814   my( $machine, $port, $login, $password, $method, $scalar, %hash ) = @_;
815   my @args = ( $scalar, \%hash );
816   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
817 }
818
819 sub cp_Scalar_Array {
820   my( $machine, $port, $login, $password, $method, $scalar, @array ) = @_;
821   my @args = ( $scalar, \@array );
822   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
823 }
824
825 #sub cp_Hash {
826 #  my( $machine, $port, $login, $password, $method, %hash ) = @_;
827 #  my @args = ( \%hash );
828 #  communigate_pro_command( $machine, $port, $login, $password, $method, @args );
829 #}
830
831 sub cp_Scalar_settingsHash {
832   my( $machine, $port, $login, $password, $method, $domain, %settings ) = @_;
833   for (qw( AccessModes DomainAccessModes )) {
834     $settings{$_} = [split(' ',$settings{$_})] if $settings{$_};
835   }
836   my @args = ( 'domain' => $domain, 'settings' => \%settings );
837   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
838 }
839
840 #sub CreateAccount {
841 #  my( $machine, $port, $login, $password, $method, %args ) = @_;
842 #  my $accountName  = delete $args{'accountName'};
843 #  my $accountType  = delete $args{'accountType'};
844 #  my $externalFlag = delete $args{'externalFlag'};
845 #  $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
846 #  my @args = ( accountName  => $accountName,
847 #               accountType  => $accountType,
848 #               settings     => \%args,
849 #             );
850 #               #externalFlag => $externalFlag,
851 #  push @args, externalFlag => $externalFlag if $externalFlag;
852 #
853 #  communigate_pro_command( $machine, $port, $login, $password, $method, @args );
854 #
855 #}
856
857 sub UpdateAccountSettings {
858   my( $machine, $port, $login, $password, $method, $accountName, %args ) = @_;
859   $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
860   my @args = ( $accountName, \%args );
861   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
862 }
863
864 sub communigate_pro_command { #subroutine, not method
865   my( $machine, $port, $login, $password, $method, @args ) = @_;
866
867   eval "use CGP::CLI";
868
869   my $cli = new CGP::CLI( {
870     'PeerAddr' => $machine,
871     'PeerPort' => $port,
872     'login'    => $login,
873     'password' => $password,
874   } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";
875
876   #warn "$method ". Dumper(@args) if $DEBUG;
877
878   my $return = $cli->$method(@args)
879     or die "Communigate Pro error: ". $cli->getErrMessage. "\n";
880
881   $cli->Logout; # or die "Can't logout of CGPro: $CGP::ERR_STRING\n";
882
883   $return;
884
885 }
886
887 1;
888