2084f152b6f1883b8609a8b7eb6a976460b7a9e6
[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 )],
37   'desc'    => 'Real-time export of accounts and domains to a CommuniGate Pro mail server',
38   'options' => \%options,
39   'notes'   => <<'END'
40 Real time export of accounts and domains to a
41 <a href="http://www.stalker.com/CommuniGatePro/">CommuniGate Pro</a>
42 mail server.  The
43 <a href="http://www.stalker.com/CGPerl/">CommuniGate Pro Perl Interface</a>
44 must be installed as CGP::CLI.
45 END
46 );
47
48 %quotas = (
49   'quota'        => 'MaxAccountSize',
50   'file_quota'   => 'MaxWebSize',
51   'file_maxnum'  => 'MaxWebFiles',
52   'file_maxsize' => 'MaxFileSize',
53 );
54
55 sub rebless { shift; }
56
57 sub export_username {
58   my($self, $svc_acct) = (shift, shift);
59   $svc_acct->email;
60 }
61
62 sub _export_insert {
63   my( $self, $svc_x ) = (shift, shift);
64
65   my $table = $svc_x->table;
66   my $method = "_export_insert_$table";
67   $self->$method($svc_x, @_);
68 }
69
70 sub _export_insert_svc_acct {
71   my( $self, $svc_acct ) = (shift, shift);
72
73   my %settings = (
74     'AccessModes'    => [ split(' ', ( $svc_acct->cgp_accessmodes
75                                        || $self->option('AccessModes') )
76                                )
77                         ],
78     'RealName'       => $svc_acct->finger,
79     'Password'       => $svc_acct->_password,
80     map { $quotas{$_} => $svc_acct->$_() }
81         grep $svc_acct->$_(), keys %quotas
82   );
83   #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
84   # RPOP modifications, accepts mail to all, add trailer to sent mail
85   #phase 3: archive messages, mailing lists
86
87   my @options = ( 'CreateAccount',
88     'accountName'    => $self->export_username($svc_acct),
89     'accountType'    => ( $svc_acct->cgp_type
90                           || $self->option('accountType') ), 
91     'settings'       => \%settings
92   );
93
94   push @options, 'externalFlag'   => $self->option('externalFlag')
95     if $self->option('externalFlag');
96
97   #let's do the create realtime too, for much the same reasons, and to avoid
98   #pain of trying to queue w/dep the prefs & aliases
99   eval { $self->communigate_pro_runcommand( @options ) };
100   return $@ if $@;
101
102   #preferences
103   my %prefs = ();
104   $prefs{'DeleteMode'} = $svc_acct->cgp_deletemode if $svc_acct->cgp_deletemode;
105   $prefs{'EmptyTrash'} = $svc_acct->cgp_emptytrash if $svc_acct->cgp_emptytrash;
106   #phase 2: language, time zone, layout, pronto style, send read receipts
107   if ( keys %prefs ) {
108     my $pref_err = $self->communigate_pro_queue( $svc_acct->svcnum,
109       'UpdateAccountPrefs',
110       $self->export_username($svc_acct),
111       %prefs,
112     );
113    warn "WARNING: error queueing UpdateAccountPrefs job: $pref_err"
114     if $pref_err;
115   }
116
117   #aliases
118   if ( $svc_acct->cgp_aliases ) {
119     my $alias_err = $self->communigate_pro_queue( $svc_acct->svcnum,
120       'SetAccountAliases',
121       $self->export_username($svc_acct),
122       [ split(/\s*[,\s]\s*/, $svc_acct->cgp_aliases) ],
123     );
124     warn "WARNING: error queueing SetAccountAliases job: $alias_err"
125       if $alias_err;
126   }
127
128   '';
129
130 }
131
132 sub _export_insert_svc_domain {
133   my( $self, $svc_domain ) = (shift, shift);
134
135   my $create = $self->option('create_domain') || 'CreateDomain';
136
137   my %settings = (
138     'DomainAccessModes'    => [ split(' ', $svc_domain->cgp_accessmodes ) ],
139   );
140   $settings{'AccountsLimit'} = $svc_domain->max_accounts
141     if $svc_domain->max_accounts;
142   $settings{'AdminDomainName'} = $svc_domain->parent_svc_x->domain
143     if $svc_domain->parent_svcnum;
144
145   my @options = ( $create, $svc_domain->domain, \%settings );
146
147   eval { $self->communigate_pro_runcommand( @options ) };
148   return $@ if $@;
149
150   #aliases
151   if ( $svc_domain->cgp_aliases ) {
152     my $alias_err = $self->communigate_pro_queue( $svc_domain->svcnum,
153       'SetDomainAliases',
154       $svc_domain->domain,
155       split(/\s*[,\s]\s*/, $svc_domain->cgp_aliases),
156     );
157     warn "WARNING: error queueing SetDomainAliases job: $alias_err"
158       if $alias_err;
159   }
160
161   #account defaults
162   my $def_err = $self->communigate_pro_queue( $svc_domain->svcnum,
163     'SetAccountDefaults',
164     $svc_domain->domain,
165     'PWDAllowed'     =>($svc_domain->acct_def_password_selfchange ? 'YES':'NO'),
166     'PasswordRecovery' => ($svc_domain->acct_def_password_recover ? 'YES':'NO'),
167     'AccessModes'      => $svc_domain->acct_def_cgp_accessmodes,
168     'MaxAccountSize'   => $svc_domain->acct_def_quota,
169     'MaxWebSize'       => $svc_domain->acct_def_file_quota,
170     'MaxWebFile'       => $svc_domain->acct_def_file_maxnum,
171     'MaxFileSize'      => $svc_domain->acct_def_file_maxsize,
172   );
173   warn "WARNING: error queueing SetAccountDefaults job: $def_err"
174     if $def_err;
175
176   #account defaults prefs
177   my $pref_err = $self->communigate_pro_queue( $svc_domain->svcnum,
178     'SetAccountDefaultPrefs',
179     $svc_domain->domain,
180     'DeleteMode' => $svc_domain->acct_def_cgp_deletemode,
181     'EmptyTrash' => $svc_domain->acct_def_cgp_emptytrash,
182   );
183   warn "WARNING: error queueing SetAccountDefaultPrefs job: $pref_err"
184     if $pref_err;
185
186   '';
187
188 }
189
190 sub _export_insert_svc_forward {
191   my( $self, $svc_forward ) = (shift, shift);
192
193   my $src = $svc_forward->src || $svc_forward->srcsvc_acct->email;
194   my $dst = $svc_forward->dst || $svc_forward->dstsvc_acct->email;
195
196   #real-time here, presuming CGP does some dup detection?
197   eval { $self->communigate_pro_runcommand( 'CreateForwarder', $src, $dst); };
198   return $@ if $@;
199
200   '';
201 }
202
203 sub _export_replace {
204   my( $self, $new, $old ) = (shift, shift, shift);
205
206   my $table = $new->table;
207   my $method = "_export_replace_$table";
208   $self->$method($new, $old, @_);
209 }
210
211 sub _export_replace_svc_acct {
212   my( $self, $new, $old ) = (shift, shift, shift);
213
214   #let's just do the rename part realtime rather than trying to queue
215   #w/dependencies.  we don't want FS winding up out-of-sync with the wrong
216   #username and a queued job anyway.  right??
217   if ( $self->export_username($old) ne $self->export_username($new) ) {
218     eval { $self->communigate_pro_runcommand(
219       'RenameAccount',
220       $self->export_username($old),
221       $self->export_username($new),
222     ) };
223     return $@ if $@;
224   }
225
226   if ( $new->_password ne $old->_password
227        && '*SUSPENDED* '.$old->_password ne $new->_password
228   ) {
229     $self->communigate_pro_queue( $new->svcnum, 'SetAccountPassword',
230                                   $self->export_username($new), $new->_password
231                                 );
232   }
233
234   my %settings = ();
235
236   $settings{'RealName'} = $new->finger
237     if $old->finger ne $new->finger;
238   $settings{$quotas{$_}} = $new->$_()
239     foreach grep $old->$_() ne $new->$_(), keys %quotas;
240   $settings{'accountType'} = $new->cgp_type
241     if $old->cgp_type ne $new->cgp_type;
242   $settings{'AccessModes'} = $new->cgp_accessmodes
243     if $old->cgp_accessmodes ne $new->cgp_accessmodes
244     || $old->cgp_type ne $new->cgp_type;
245
246   #phase 2: pwdallowed, passwordrecovery, allowed mail rules,
247   # RPOP modifications, accepts mail to all, add trailer to sent mail
248   #phase 3: archive messages, mailing lists
249
250   if ( keys %settings ) {
251     my $error = $self->communigate_pro_queue(
252       $new->svcnum,
253       'UpdateAccountSettings',
254       $self->export_username($new),
255       %settings,
256     );
257     return $error if $error;
258   }
259
260   #preferences
261   my %prefs = ();
262   $prefs{'DeleteMode'} = $new->cgp_deletemode
263     if $old->cgp_deletemode ne $new->cgp_deletemode;
264   $prefs{'EmptyTrash'} = $new->cgp_emptytrash
265     if $old->cgp_emptytrash ne $new->cgp_emptytrash;
266   #phase 2: language, time zone, layout, pronto style, send read receipts
267   if ( keys %prefs ) {
268     my $pref_err = $self->communigate_pro_queue( $new->svcnum,
269       'UpdateAccountPrefs',
270       $self->export_username($new),
271       %prefs,
272     );
273    warn "WARNING: error queueing UpdateAccountPrefs job: $pref_err"
274     if $pref_err;
275   }
276
277   if ( $old->cgp_aliases ne $new->cgp_aliases ) {
278     my $error = $self->communigate_pro_queue(
279       $new->svcnum,
280       'SetAccountAliases',
281       $self->export_username($new),
282       [ split(/\s*[,\s]\s*/, $new->cgp_aliases) ],
283     );
284     return $error if $error;
285   }
286
287   '';
288
289 }
290
291 sub _export_replace_svc_domain {
292   my( $self, $new, $old ) = (shift, shift, shift);
293
294   if ( $old->domain ne $new->domain ) {
295     my $error = $self->communigate_pro_queue( $new->svcnum, 'RenameDomain',
296       $old->domain, $new->domain,
297     );
298     return $error if $error;
299   }
300   my %settings = ();
301   $settings{'AccountsLimit'} = $new->max_accounts
302     if $old->max_accounts ne $new->max_accounts;
303   $settings{'DomainAccessModes'} = $new->cgp_accessmodes
304     if $old->cgp_accessmodes ne $new->cgp_accessmodes;
305   $settings{'AdminDomainName'} =
306     $new->parent_svcnum ? $new->parent_svc_x->domain : ''
307       if $old->parent_svcnum != $new->parent_svcnum;
308
309   if ( keys %settings ) {
310     my $error = $self->communigate_pro_queue( $new->svcnum,
311       'UpdateDomainSettings',
312       $new->domain,
313       %settings,
314     );
315     return $error if $error;
316   }
317
318   if ( $old->cgp_aliases ne $new->cgp_aliases ) {
319     my $error = $self->communigate_pro_queue( $new->svcnum,
320       'SetDomainAliases',
321       $new->domain,
322       split(/\s*[,\s]\s*/, $new->cgp_aliases),
323     );
324     return $error if $error;
325   }
326
327   #below this identical to insert... any value to doing an Update here?
328   #not seeing any big one... i guess it would be nice to avoid the update
329   #when things haven't changed
330
331   #account defaults
332   my $def_err = $self->communigate_pro_queue( $new->svcnum,
333     'SetAccountDefaults',
334     $new->domain,
335     'PWDAllowed'       => ( $new->acct_def_password_selfchange ? 'YES' : 'NO' ),
336     'PasswordRecovery' => ( $new->acct_def_password_recover    ? 'YES' : 'NO' ),
337     'AccessModes'      => $new->acct_def_cgp_accessmodes,
338     'MaxAccountSize'   => $new->acct_def_quota,
339     'MaxWebSize'       => $new->acct_def_file_quota,
340     'MaxWebFile'       => $new->acct_def_file_maxnum,
341     'MaxFileSize'      => $new->acct_def_file_maxsize,
342   );
343   warn "WARNING: error queueing SetAccountDefaults job: $def_err"
344     if $def_err;
345
346   #account defaults prefs
347   my $pref_err = $self->communigate_pro_queue( $new->svcnum,
348     'SetAccountDefaultPrefs',
349     $new->domain,
350     'DeleteMode' => $new->acct_def_cgp_deletemode,
351     'EmptyTrash' => $new->acct_def_cgp_emptytrash,
352   );
353   warn "WARNING: error queueing SetAccountDefaultPrefs job: $pref_err"
354     if $pref_err;
355
356   '';
357 }
358
359 sub _export_replace_svc_forward {
360   my( $self, $new, $old ) = (shift, shift, shift);
361
362   my $osrc = $old->src || $old->srcsvc_acct->email;
363   my $nsrc = $new->src || $new->srcsvc_acct->email;
364   my $odst = $old->dst || $old->dstsvc_acct->email;
365   my $ndst = $new->dst || $new->dstsvc_acct->email;
366
367   if ( $odst ne $ndst ) {
368
369     #no change command, so delete and create (real-time)
370     eval { $self->communigate_pro_runcommand('DeleteForwarder', $osrc) };
371     return $@ if $@;
372     eval { $self->communigate_pro_runcommand('CreateForwarder', $nsrc, $ndst)};
373     return $@ if $@;
374
375   } elsif ( $osrc ne $nsrc ) {
376
377     #real-time here, presuming CGP does some dup detection?
378     eval { $self->communigate_pro_runcommand( 'RenameForwarder', $osrc, $nsrc)};
379     return $@ if $@;
380
381   } else {
382     warn "communigate replace called for svc_forward with no changes\n";#confess
383   }
384
385   '';
386 }
387
388 sub _export_delete {
389   my( $self, $svc_x ) = (shift, shift);
390
391   my $table = $svc_x->table;
392   my $method = "_export_delete_$table";
393   $self->$method($svc_x, @_);
394 }
395
396 sub _export_delete_svc_acct {
397   my( $self, $svc_acct ) = (shift, shift);
398
399   $self->communigate_pro_queue( $svc_acct->svcnum, 'DeleteAccount',
400     $self->export_username($svc_acct),
401   );
402 }
403
404 sub _export_delete_svc_domain {
405   my( $self, $svc_domain ) = (shift, shift);
406
407   $self->communigate_pro_queue( $svc_domain->svcnum, 'DeleteDomain',
408     $svc_domain->domain,
409     #XXX turn on force option for domain deletion?
410   );
411 }
412
413 sub _export_delete_svc_forward {
414   my( $self, $svc_forward ) = (shift, shift);
415
416   $self->communigate_pro_queue( $svc_forward->svcnum, 'DeleteForwarder',
417     ($svc_forward->src || $svc_forward->srcsvc_acct->email),
418   );
419 }
420
421 sub _export_suspend {
422   my( $self, $svc_x ) = (shift, shift);
423
424   my $table = $svc_x->table;
425   my $method = "_export_suspend_$table";
426   $self->$method($svc_x, @_);
427
428 }
429
430 sub _export_suspend_svc_acct {
431   my( $self, $svc_acct ) = (shift, shift);
432
433   #XXX is this the desired suspnsion action?
434
435    $self->communigate_pro_queue(
436     $svc_acct->svcnum,
437     'UpdateAccountSettings',
438     $self->export_username($svc_acct),
439     'AccessModes' => 'Mail',
440   );
441
442 }
443
444 sub _export_suspend_svc_domain {
445   my( $self, $svc_domain) = (shift, shift);
446
447   #XXX domain operations
448   '';
449
450 }
451
452 sub _export_unsuspend {
453   my( $self, $svc_x ) = (shift, shift);
454
455   my $table = $svc_x->table;
456   my $method = "_export_unsuspend_$table";
457   $self->$method($svc_x, @_);
458
459 }
460
461 sub _export_unsuspend_svc_acct {
462   my( $self, $svc_acct ) = (shift, shift);
463
464   $self->communigate_pro_queue(
465     $svc_acct->svcnum,
466     'UpdateAccountSettings',
467     $self->export_username($svc_acct),
468     'AccessModes' => ( $svc_acct->cgp_accessmodes
469                          || $self->option('AccessModes') ),
470   );
471
472 }
473
474 sub _export_unsuspend_svc_domain {
475   my( $self, $svc_domain) = (shift, shift);
476
477   #XXX domain operations
478   '';
479
480 }
481
482
483 sub export_getsettings {
484   my($self, $svc_x) = (shift, shift);
485
486   my $table = $svc_x->table;
487   my $method = "export_getsettings_$table";
488
489   $self->can($method) ? $self->$method($svc_x, @_) : '';
490
491 }
492
493 sub export_getsettings_svc_domain {
494   my($self, $svc_domain, $settingsref, $defaultref ) = @_;
495
496   my $settings = eval { $self->communigate_pro_runcommand(
497     'GetDomainSettings',
498     $svc_domain->domain
499   ) };
500   return $@ if $@;
501
502   my $effective_settings = eval { $self->communigate_pro_runcommand(
503     'GetDomainEffectiveSettings',
504     $svc_domain->domain
505   ) };
506   return $@ if $@;
507
508   my $acct_defaults = eval { $self->communigate_pro_runcommand(
509     'GetAccountDefaults',
510     $svc_domain->domain
511   ) };
512   return $@ if $@;
513
514   my $acct_defaultprefs = eval { $self->communigate_pro_runcommand(
515     'GetAccountDefaultPrefs',
516     $svc_domain->domain
517   ) };
518   return $@ if $@;
519
520   %$effective_settings = (
521     %$effective_settings,
522     ( map { ("Acct. Default $_" => $acct_defaults->{$_}); }
523           keys(%$acct_defaults)
524     ),
525     ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label??
526           keys(%$acct_defaultprefs)
527     ),
528   );
529   %$settings = (
530     %$settings,
531     ( map { ("Acct. Default $_" => $acct_defaults->{$_}); }
532           keys(%$acct_defaults)
533     ),
534     ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label??
535           keys(%$acct_defaultprefs)
536     ),
537   );
538
539   #aliases too
540   my $aliases = eval { $self->communigate_pro_runcommand(
541     'GetDomainAliases',
542     $svc_domain->domain
543   ) };
544   return $@ if $@;
545
546   $effective_settings->{'Aliases'} = join(', ', @$aliases);
547   $settings->{'Aliases'}           = join(', ', @$aliases);
548
549
550   #false laziness w/below
551   
552   my %defaults = map { $_ => 1 }
553                    grep !exists(${$settings}{$_}), keys %$effective_settings;
554
555   foreach my $key ( grep ref($effective_settings->{$_}),
556                     keys %$effective_settings )
557   {
558     my $value = $effective_settings->{$key};
559     if ( ref($value) eq 'ARRAY' ) {
560       $effective_settings->{$key} = join(' ', @$value);
561     } else {
562       #XXX
563       warn "serializing ". ref($value). " for table display not yet handled";
564     }
565   }
566
567   %{$settingsref} = %$effective_settings;
568   %{$defaultref} = %defaults;
569
570   '';
571 }
572
573 sub export_getsettings_svc_acct {
574   my($self, $svc_acct, $settingsref, $defaultref ) = @_;
575
576   my $settings = eval { $self->communigate_pro_runcommand(
577     'GetAccountSettings',
578     $svc_acct->email
579   ) };
580   return $@ if $@;
581
582   delete($settings->{'Password'});
583
584   my $effective_settings = eval { $self->communigate_pro_runcommand(
585     'GetAccountEffectiveSettings',
586     $svc_acct->email
587   ) };
588   return $@ if $@;
589
590   delete($effective_settings->{'Password'});
591
592   #prefs/effectiveprefs too
593
594   my $prefs = eval { $self->communigate_pro_runcommand(
595     'GetAccountPrefs',
596     $svc_acct->email
597   ) };
598   return $@ if $@;
599
600   my $effective_prefs = eval { $self->communigate_pro_runcommand(
601     'GetAccountEffectivePrefs',
602     $svc_acct->email
603   ) };
604   return $@ if $@;
605
606   %$effective_settings = ( %$effective_settings,
607                            map { ("Pref $_" => $effective_prefs->{$_}); }
608                                keys(%$effective_prefs)
609                          );
610   %$settings = ( %$settings,
611                  map { ("Pref $_" => $prefs->{$_}); }
612                      keys(%$prefs)
613                );
614
615   #aliases too
616
617   my $aliases = eval { $self->communigate_pro_runcommand(
618     'GetAccountAliases',
619     $svc_acct->email
620   ) };
621   return $@ if $@;
622
623   $effective_settings->{'Aliases'} = join(', ', @$aliases);
624   $settings->{'Aliases'}           = join(', ', @$aliases);
625
626   #false laziness w/above
627
628   my %defaults = map { $_ => 1 }
629                    grep !exists(${$settings}{$_}), keys %$effective_settings;
630
631   foreach my $key ( grep ref($effective_settings->{$_}),
632                     keys %$effective_settings )
633   {
634     my $value = $effective_settings->{$key};
635     if ( ref($value) eq 'ARRAY' ) {
636       $effective_settings->{$key} = join(' ', @$value);
637     } else {
638       #XXX
639       warn "serializing ". ref($value). " for table display not yet handled";
640     }
641   }
642
643   %{$settingsref} = %$effective_settings;
644   %{$defaultref} = %defaults;
645
646   '';
647
648 }
649
650 sub communigate_pro_queue {
651   my( $self, $svcnum, $method ) = (shift, shift, shift);
652   my $jobnum = ''; #don't actually care
653   $self->communigate_pro_queue_dep( \$jobnum, $svcnum, $method, @_);
654 }
655
656 sub communigate_pro_queue_dep {
657   my( $self, $jobnumref, $svcnum, $method ) = splice(@_,0,4);
658
659   my %kludge_methods = (
660     #'CreateAccount'             => 'CreateAccount',
661     'UpdateAccountSettings'     => 'UpdateAccountSettings',
662     'UpdateAccountPrefs'        => 'cp_Scalar_Hash',
663     #'CreateDomain'              => 'cp_Scalar_Hash',
664     #'CreateSharedDomain'        => 'cp_Scalar_Hash',
665     'UpdateDomainSettings'      => 'cp_Scalar_settingsHash',
666     'SetDomainAliases'          => 'cp_Scalar_Array',
667     'SetAccountDefaults'        => 'cp_Scalar_settingsHash',
668     'UpdateAccountDefaults'     => 'cp_Scalar_settingsHash',
669     'SetAccountDefaultPrefs'    => 'cp_Scalar_settingsHash',
670     'UpdateAccountDefaultPrefs' => 'cp_Scalar_settingsHash',
671   );
672   my $sub = exists($kludge_methods{$method})
673               ? $kludge_methods{$method}
674               : 'communigate_pro_command';
675
676   my $queue = new FS::queue {
677     'svcnum' => $svcnum,
678     'job'    => "FS::part_export::communigate_pro::$sub",
679   };
680   my $error = $queue->insert(
681     $self->machine,
682     $self->option('port'),
683     $self->option('login'),
684     $self->option('password'),
685     $method,
686     @_,
687   );
688   $$jobnumref = $queue->jobnum unless $error;
689
690   return $error;
691 }
692
693 sub communigate_pro_runcommand {
694   my( $self, $method ) = (shift, shift);
695
696   communigate_pro_command(
697     $self->machine,
698     $self->option('port'),
699     $self->option('login'),
700     $self->option('password'),
701     $method,
702     @_,
703   );
704
705 }
706
707 #XXX one sub per arg prototype is lame.  more magic?  i suppose queue needs
708 # to store data strctures properly instead of just an arg list.  right.
709
710 sub cp_Scalar_Hash {
711   my( $machine, $port, $login, $password, $method, $scalar, %hash ) = @_;
712   my @args = ( $scalar, \%hash );
713   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
714 }
715
716 sub cp_Scalar_Array {
717   my( $machine, $port, $login, $password, $method, $scalar, @array ) = @_;
718   my @args = ( $scalar, \@array );
719   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
720 }
721
722 #sub cp_Hash {
723 #  my( $machine, $port, $login, $password, $method, %hash ) = @_;
724 #  my @args = ( \%hash );
725 #  communigate_pro_command( $machine, $port, $login, $password, $method, @args );
726 #}
727
728 sub cp_Scalar_settingsHash {
729   my( $machine, $port, $login, $password, $method, $domain, %settings ) = @_;
730   for (qw( AccessModes DomainAccessModes )) {
731     $settings{$_} = [split(' ',$settings{$_})] if $settings{$_};
732   }
733   my @args = ( 'domain' => $domain, 'settings' => \%settings );
734   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
735 }
736
737 #sub CreateAccount {
738 #  my( $machine, $port, $login, $password, $method, %args ) = @_;
739 #  my $accountName  = delete $args{'accountName'};
740 #  my $accountType  = delete $args{'accountType'};
741 #  my $externalFlag = delete $args{'externalFlag'};
742 #  $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
743 #  my @args = ( accountName  => $accountName,
744 #               accountType  => $accountType,
745 #               settings     => \%args,
746 #             );
747 #               #externalFlag => $externalFlag,
748 #  push @args, externalFlag => $externalFlag if $externalFlag;
749 #
750 #  communigate_pro_command( $machine, $port, $login, $password, $method, @args );
751 #
752 #}
753
754 sub UpdateAccountSettings {
755   my( $machine, $port, $login, $password, $method, $accountName, %args ) = @_;
756   $args{'AccessModes'} = [ split(' ', $args{'AccessModes'}) ];
757   my @args = ( $accountName, \%args );
758   communigate_pro_command( $machine, $port, $login, $password, $method, @args );
759 }
760
761 sub communigate_pro_command { #subroutine, not method
762   my( $machine, $port, $login, $password, $method, @args ) = @_;
763
764   eval "use CGP::CLI";
765
766   my $cli = new CGP::CLI( {
767     'PeerAddr' => $machine,
768     'PeerPort' => $port,
769     'login'    => $login,
770     'password' => $password,
771   } ) or die "Can't login to CGPro: $CGP::ERR_STRING\n";
772
773   #warn "$method ". Dumper(@args) if $DEBUG;
774
775   my $return = $cli->$method(@args)
776     or die "Communigate Pro error: ". $cli->getErrMessage. "\n";
777
778   $cli->Logout; # or die "Can't logout of CGPro: $CGP::ERR_STRING\n";
779
780   $return;
781
782 }
783
784 1;
785