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