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