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