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