This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / FS / part_export / radiator.pm
1 package FS::part_export::radiator;
2
3 use vars qw(@ISA %info $radusers);
4 use Tie::IxHash;
5 use FS::part_export::sqlradius;
6
7 tie my %options, 'Tie::IxHash', %FS::part_export::sqlradius::options;
8
9 %info = (
10   'svc'      => 'svc_acct',
11   'desc'     => 'Real-time export to RADIATOR',
12   'options'  => \%options,
13   'nodomain' => '',
14   'notes' => <<'END',
15 Real-time export of the <b>radusers</b> table to any SQL database in
16 <a href="http://www.open.com.au/radiator/">Radiator</a>-native format.
17 To setup accounting, see the RADIATOR documentation for hooks to update
18 a standard <b>radacct</b> table.
19 END
20 );
21
22 @ISA = qw(FS::part_export::sqlradius); #for regular sqlradius accounting
23
24 $radusers = 'RADUSERS'; #MySQL is case sensitive about table names!  huh
25
26 #sub export_username {
27 #  my($self, $svc_acct) = (shift, shift);
28 #  $svc_acct->email;
29 #}
30
31 sub _export_insert {
32   my( $self, $svc_acct ) = (shift, shift);
33
34   $self->radiator_queue(
35     $svc_acct->svcnum,
36     'insert',
37     $self->_radiator_hash($svc_acct),
38   );
39 }
40
41 sub _export_replace {
42   my( $self, $new, $old ) = (shift, shift, shift);
43
44 #  return "can't (yet) change domain with radiator export"
45 #    if $old->domain ne $new->domain;
46 #  return "can't (yet) change username with radiator export"
47 #    if $old->username ne $new->username;
48
49   $self->radiator_queue(
50     $new->svcnum,
51     'replace',
52     $self->export_username($old),
53     $self->_radiator_hash($new),
54   );
55 }
56
57 sub _export_delete {
58   my( $self, $svc_acct ) = (shift, shift);
59
60   $self->radiator_queue(
61     $svc_acct->svcnum,
62     'delete',
63     $self->export_username($svc_acct),
64   );
65 }
66
67 sub _radiator_hash {
68   my( $self, $svc_acct ) = @_;
69   my %hash = (
70     'username'  => $self->export_username($svc_acct),
71     'pass_word' => $svc_acct->crypt_password,
72     'fullname'  => $svc_acct->finger,
73     map { my $method = "radius_$_"; $_ => $svc_acct->$method(); }
74         qw( framed_filter_id framed_mtu framed_netmask framed_protocol
75             framed_routing login_host login_service login_tcp_port )
76   );
77   $hash{'timeleft'} = $svc_acct->seconds
78     if $svc_acct->seconds =~ /^\d+$/;
79   $hash{'staticaddress'} = $svc_acct->slipip
80     if $svc_acct->slipip =~ /^[\d\.]+$/; # and $self->slipip ne '0.0.0.0';
81
82   $hash{'servicename'} = ( $svc_acct->radius_groups )[0];
83
84   my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
85   $hash{'validto'} = $cust_pkg->bill
86     if $cust_pkg && $cust_pkg->part_pkg->is_prepaid && $cust_pkg->bill;
87
88   #some other random stuff, should probably be attributes or virtual fields
89   #$hash{'state'} = 0; #only inserts
90   #$hash{'badlogins'} = 0; #only inserts
91   $hash{'maxlogins'} = 1;
92   $hash{'addeddate'} = $cust_pkg->setup
93     if $cust_pkg && $cust_pkg->setup;
94   $hash{'validfrom'} = $cust_pkg->last_bill || $cust_pkg->setup
95     if $cust_pkg &&  ( $cust_pkg->last_bill || $cust_pkg->setup );
96   $hash{'state'} = $cust_pkg->susp ? 1 : 0
97     if $cust_pkg;
98
99   %hash;
100 }
101
102 sub radiator_queue {
103   my( $self, $svcnum, $method ) = (shift, shift, shift);
104   my $queue = new FS::queue {
105     'svcnum' => $svcnum,
106     'job'    => "FS::part_export::radiator::radiator_$method",
107   };
108   $queue->insert(
109     $self->option('datasrc'),
110     $self->option('username'),
111     $self->option('password'),
112     @_,
113   ); # or $queue;
114 }
115
116 sub radiator_insert { #subroutine, not method
117   my $dbh = radiator_connect(shift, shift, shift);
118   my %hash = @_;
119   $hash{'state'} = 0; #see "random stuff" above
120   $hash{'badlogins'} = 0; #see "random stuff" above
121
122   my $sth = $dbh->prepare(
123     "INSERT INTO $radusers ( ". join(', ', keys %hash ). ' ) '.
124       'VALUES ( '. join(', ', map '?', keys %hash ). ' ) '
125   ) or die $dbh->errstr;
126   $sth->execute( values %hash )
127     or die $sth->errstr;
128
129   $dbh->disconnect;
130
131 }
132
133 sub radiator_replace { #subroutine, not method
134   my $dbh = radiator_connect(shift, shift, shift);
135   my ( $old_username, %hash ) = @_;
136
137   my $sth = $dbh->prepare(
138     "UPDATE $radusers SET ". join(', ', map " $_ = ?", keys %hash ).
139       ' WHERE username = ?'
140   ) or die $dbh->errstr;
141   $sth->execute( values(%hash), $old_username )
142     or die $sth->errstr;
143
144   $dbh->disconnect;
145 }
146
147 sub radiator_delete { #subroutine, not method
148   my $dbh = radiator_connect(shift, shift, shift);
149   my ( $username ) = @_;
150
151   my $sth = $dbh->prepare(
152     "DELETE FROM $radusers WHERE username = ?"
153   ) or die $dbh->errstr;
154   $sth->execute( $username )
155     or die $sth->errstr;
156
157   $dbh->disconnect;
158 }
159
160
161 sub radiator_connect {
162   #my($datasrc, $username, $password) = @_;
163   #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
164   DBI->connect(@_) or die $DBI::errstr;
165 }
166
167 1;