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