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