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