export host selection per service, RT#17914
[freeside.git] / FS / FS / part_export / acct_xmlrpc.pm
1 package FS::part_export::acct_xmlrpc;
2 use base qw( FS::part_export );
3
4 use vars qw( %info ); # $DEBUG );
5 #use Data::Dumper;
6 use Tie::IxHash;
7 use Frontier::Client; #to avoid adding a dependency on RPC::XML just now
8 #use FS::Record qw( qsearch qsearchs );
9 use FS::Schema qw( dbdef );
10
11 #$DEBUG = 1;
12
13 tie my %options, 'Tie::IxHash',
14   'xmlrpc_url'       => { label => 'XML-RPC URL', },
15   'param_style'      => { label   => 'Parameter style',
16                           type    => 'select',
17                           options => [ 'Individual values',
18                                        'Struct of name/value pairs',
19                                      ],
20                         },
21   'insert_method'    => { label => 'Insert method', },
22   'insert_params'    => { label => 'Insert parameters', type=>'textarea', },
23   'replace_method'   => { label => 'Replace method', },
24   'replace_params'   => { label => 'Replace parameters', type=>'textarea', },
25   'delete_method'    => { label => 'Delete method', },
26   'delete_params'    => { label => 'Delete parameters', type=>'textarea', },
27   'suspend_method'   => { label => 'Suspend method', },
28   'suspend_params'   => { label => 'Suspend parameters', type=>'textarea', },
29   'unsuspend_method' => { label => 'Unsuspend method', },
30   'unsuspend_params' => { label => 'Unsuspend parameters', type=>'textarea', },
31 ;
32
33 %info = (
34   'svc'     => 'svc_acct',
35   'desc'    => 'Configurable provisioning of accounts via the XML-RPC protocol',
36   'options' => \%options,
37   'no_machine' => 1,
38   'notes'   => <<'END',
39 Configurable, real-time export of accounts via the XML-RPC protocol.<BR>
40 <BR>
41 If using "Individual values" parameter style, specify one parameter per line.<BR>
42 <BR>
43 If using "Struct of name/value pairs" parameter style, specify one name and
44 value on each line, separated by whitespace.<BR>
45 <BR>
46 The following variables are available for interpolation (prefixed with new_ or
47 old_ for replace operations):
48 <UL>
49   <LI><code>$username</code>
50   <LI><code>$_password</code>
51   <LI><code>$crypt_password</code> - encrypted password
52   <LI><code>$ldap_password</code> - Password in LDAP/RFC2307 format (for example, "{PLAIN}himom", "{CRYPT}94pAVyK/4oIBk" or "{MD5}5426824942db4253f87a1009fd5d2d4")
53   <LI><code>$uid</code>
54   <LI><code>$gid</code>
55   <LI><code>$finger</code> - Real name
56   <LI><code>$dir</code> - home directory
57   <LI><code>$shell</code>
58   <LI><code>$quota</code>
59   <LI><code>@radius_groups</code>
60 <!--  <LI><code>$reasonnum (when suspending)</code>
61   <LI><code>$reasontext (when suspending)</code>
62   <LI><code>$reasontypenum (when suspending)</code>
63   <LI><code>$reasontypetext (when suspending)</code>
64 -->
65 <!--
66   <LI><code>$pkgnum</code>
67   <LI><code>$custnum</code>
68 -->
69   <LI>All other fields in <b>svc_acct</b> are also available.
70 <!--  <LI>The following fields from <b>cust_main</b> are also available (except during replace): company, address1, address2, city, state, zip, county, daytime, night, fax, otaker, agent_custid, locale. -->
71 </UL>
72
73 END
74 );
75
76 sub _export_insert    { shift->_export_command('insert',    @_) }
77 sub _export_delete    { shift->_export_command('delete',    @_) }
78 sub _export_suspend   { shift->_export_command('suspend',   @_) }
79 sub _export_unsuspend { shift->_export_command('unsuspend', @_) }
80
81 sub _export_command {
82   my ( $self, $action, $svc_acct) = (shift, shift, shift);
83   my $method = $self->option($action.'_method');
84   return '' if $method =~ /^\s*$/;
85
86   my @params = split("\n", $self->option($action.'_params') );
87
88   my( @x_param ) = ();
89   my( %x_struct ) = ();
90   foreach my $param (@params) {
91
92     my($name, $value) = ('', '');
93     if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
94       ($name, $value) = split(/\s+/, $param);
95     } else { #'Individual values'
96       $value = $param;
97     }
98
99     if ( $value =~ /^\s*(\$|\@)(\w+)\s*$/ ) {
100       $value = $self->_export_value($2, $svc_acct);
101     }
102
103     if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
104       $x_struct{$name} = $value;
105     } else { #'Individual values'
106       push @x_param, $value;
107     }
108
109   }
110
111   my @x = ();
112   if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
113     @x = ( \%x_struct );
114   } else { #'Individual values'
115     @x = @x_param;
116   }
117
118   #option to queue (or not) ?
119
120   my $conn = Frontier::Client->new( url => $self->option('xmlrpc_url') );
121
122   my $result = $conn->call($method, @x);
123
124   #XXX error checking?  $result?  from the call?
125   '';
126 }
127
128 sub _export_replace {
129   my( $self, $new, $old ) = (shift, shift, shift);
130
131   my $method = $self->option($action.'_method');
132   return '' if $method =~ /^\s*$/;
133
134   my @params = split("\n", $self->option($action.'_params') );
135
136   my( @x_param ) = ();
137   my( %x_struct ) = ();
138   foreach my $param (@params) {
139
140     my($name, $value) = ('', '');
141     if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
142       ($name, $value) = split(/\s+/, $param);
143     } else { #'Individual values'
144       $value = $param;
145     }
146
147     if ( $value =~ /^\s*(\$|\@)(old|new)_(\w+)\s*$/ ) {
148       if ($2 eq 'old' ) {
149         $value = $self->_export_value($3, $old);
150       } elsif ( $2 eq 'new' ) {
151         $value = $self->_export_value($3, $new);
152       } else {
153         die 'guru meditation stella blue';
154       }
155     }
156
157     if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
158       $x_struct{$name} = $value;
159     } else { #'Individual values'
160       push @x_param, $value;
161     }
162
163   }
164
165   my @x = ();
166   if ($self->option('param_style') eq 'Struct of name/value pairs' ) {
167     @x = ( \%x_struct );
168   } else { #'Individual values'
169     @x = @x_param;
170   }
171
172   #option to queue (or not) ?
173
174   my $conn = Frontier::Client->new( url => $self->option('xmlrpc_url') );
175
176   my $result = $conn->call($method, @x);
177
178   #XXX error checking?  $result?  from the call?
179   '';
180
181 }
182
183 #comceptual false laziness w/shellcommands.pm
184 sub _export_value {
185   my( $self, $value, $svc_acct) = (shift, shift, shift);
186
187   my %fields = map { $_=>1 } $svc_acct->fields;
188
189   if ( $fields{$value} ) {
190     my $type = dbdef->table('svc_acct')->column($value)->type;
191     if ( $type =~ /^(int|serial)/i ) {
192       return Frontier::Client->new->int( $svc_acct->$value() );
193     } elsif ( $value =~ /^last_log/ ) {
194       return Frontier::Client->new->date_time( $svc_acct->$value() ); #conversion?
195     } else {
196       return Frontier::Client->new->string( $svc_acct->$value() );
197     }
198   } elsif ( $value eq 'domain' ) {
199     return Frontier::Client->new->string( $svc_acct->domain );
200   } elsif ( $value eq 'crypt_password' ) {
201     return Frontier::Client->new->string( $svc_acct->crypt_password( $self->option('crypt') ) );
202   } elsif ( $value eq 'ldap_password' ) {
203     return Frontier::Client->new->string( $svc_acct->ldap_password($self->option('crypt') ) );
204   } elsif ( $value eq 'radius_groups' ) {
205     my @radius_groups = $svc_acct->radius_groups;
206     #XXX
207   }
208
209 #  my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
210 #  if ( $cust_pkg ) {
211 #    no strict 'vars';
212 #    {
213 #      no strict 'refs';
214 #      foreach my $custf (qw( company address1 address2 city state zip country
215 #                             daytime night fax otaker agent_custid locale
216 #                        ))
217 #      {
218 #        ${$custf} = $cust_pkg->cust_main->$custf();
219 #      }
220 #    }
221 #    $email = ( grep { $_ !~ /^(POST|FAX)$/ } $cust_pkg->cust_main->invoicing_list )[0];
222 #  } else {
223 #    $email = '';
224 #  }
225
226 #  my ($reasonnum, $reasontext, $reasontypenum, $reasontypetext);
227 #  if ( $cust_pkg && $action eq 'suspend' &&
228 #       (my $r = $cust_pkg->last_reason('susp')) )
229 #  {
230 #    $reasonnum = $r->reasonnum;
231 #    $reasontext = $r->reason;
232 #    $reasontypenum = $r->reason_type;
233 #    $reasontypetext = $r->reasontype->type;
234 #
235 #    my %reasonmap = $self->_groups_susp_reason_map;
236 #    my $userspec = '';
237 #    $userspec = $reasonmap{$reasonnum}
238 #      if exists($reasonmap{$reasonnum});
239 #    $userspec = $reasonmap{$reasontext}
240 #      if (!$userspec && exists($reasonmap{$reasontext}));
241 #
242 #    my $suspend_user;
243 #    if ( $userspec =~ /^\d+$/ ) {
244 #      $suspend_user = qsearchs( 'svc_acct', { 'svcnum' => $userspec } );
245 #    } elsif ( $userspec =~ /^\S+\@\S+$/ ) {
246 #      my ($username,$domain) = split(/\@/, $userspec);
247 #      for my $user (qsearch( 'svc_acct', { 'username' => $username } )){
248 #        $suspend_user = $user if $userspec eq $user->email;
249 #      }
250 #    } elsif ($userspec) {
251 #      $suspend_user = qsearchs( 'svc_acct', { 'username' => $userspec } );
252 #    }
253 #  
254 #  @radius_groups = $suspend_user->radius_groups
255 #    if $suspend_user;  
256 #  
257 #  } else {
258 #    $reasonnum = $reasontext = $reasontypenum = $reasontypetext = '';
259 #  }
260
261 #  $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
262 #  $custnum = $cust_pkg ? $cust_pkg->custnum : '';
263
264   '';
265
266 }
267
268 1;
269