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