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