RADIUS counter/limit accounting with realms stored as @realm in UserName column inste...
[freeside.git] / FS / FS / part_export / http_status.pm
1 package FS::part_export::http_status;
2 use base qw( FS::part_export );
3
4 use strict;
5 use warnings;
6 use vars qw( %info $DEBUG );
7 use URI::Escape;
8 use LWP::UserAgent;
9 use HTTP::Request::Common;
10 use Email::Valid;
11
12 tie my %options, 'Tie::IxHash',
13   'url' => { label => 'URL', },
14   'blacklist_add_url' => { label => 'Optional blacklist add URL', },
15   'blacklist_del_url' => { label => 'Optional blacklist delete URL', },
16   'whitelist_add_url' => { label => 'Optional whitelist add URL', },
17   'whitelist_del_url' => { label => 'Optional whitelist delete URL', },
18   'vacation_add_url'  => { label => 'Optional vacation message add URL', },
19   'vacation_del_url'  => { label => 'Optional vacation message delete URL', },
20
21   #'user'     => { label => 'Username', default=>'' },
22   #'password' => { label => 'Password', default => '' },
23 ;
24
25 %info = (
26   'svc'     => [ 'svc_acct', 'svc_dsl', ],
27   'desc'    => 'Retrieve status information via HTTP or HTTPS',
28   'options' => \%options,
29   'notes'   => <<'END'
30 Fields from the service can be substituted in the URL as $field.
31
32 Optionally, spam black/whitelist addresees and a vacation message may be
33 modified via HTTP or HTTPS as well.
34 END
35 );
36
37 $DEBUG = 1;
38
39 sub rebless { shift; }
40
41 our %addl_fields = (
42   'svc_acct' => [qw( email ) ],
43   'svc_dsl'  => [qw( gateway_access_or_phonenum ) ],
44 );
45
46 #some NOPs for required subroutines, to avoid throwing the exceptions in the
47 # part_export.pm fallbacks
48 sub _export_insert  { '' };
49 sub _export_replace { '' };
50 sub _export_delete  { '' };
51
52 sub export_getstatus {
53   my( $self, $svc_x, $htmlref, $hashref ) = @_;
54
55   my $url;
56   my $urlopt = $self->option('url');
57   no strict 'vars';
58   {
59     no strict 'refs';
60     ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
61     ${$_} = $svc_x->$_()         foreach @{ $addl_fields{ $svc_x->table } };
62     $url = eval(qq("$urlopt"));
63   }
64
65   my $req = HTTP::Request::Common::GET( $url );
66   my $ua = LWP::UserAgent->new;
67   my $response = $ua->request($req);
68
69   if ( $svc_x->table eq 'svc_dsl' ) {
70
71     $$htmlref = $response->is_error ? $response->error_as_HTML
72                                     : $response->content;
73
74     #hash data not yet implemented for svc_dsl
75
76   } elsif ( $svc_x->table eq 'svc_acct' ) {
77
78     #this whole section is rather specific to fibernetics and should be an
79     # option or callback or something
80
81     # to,from,wb_value
82
83     use Text::CSV_XS;
84     my $csv = Text::CSV_XS->new;
85
86     my @lines = split("\n", $response->content);
87     pop @lines if $lines[-1] eq '';
88     my $header = shift @lines;
89     $csv->parse($header) or return;
90     my @header = $csv->fields;
91
92     while ( my $line = shift @lines ) {
93       $csv->parse($line) or next;
94       my @fields = $csv->fields;
95       my %hash = map { $_ => shift(@fields) } @header;
96
97       if ( defined $hash{'wb_value'} ) {
98         if ( $hash{'wb_value'} =~ /^[WA]/i ) { #Whitelist/Allow
99           push @{ $hashref->{'whitelist'} }, $hash{'from'};
100         } else { # if ( $hash{'wb_value'} =~ /^[BD]/i ) { #Blacklist/Deny
101           push @{ $hashref->{'blacklist'} }, $hash{'from'};
102         }
103       }
104
105       for (qw( created enddate )) {
106         $hash{$_} = '' if $hash{$_} =~ /^0000-/;
107         $hash{$_} = (split(' ', $hash{$_}))[0];
108       }
109
110       next unless $hash{'active'};
111       $hashref->{"vacation_$_"} = $hash{$_} || ''
112         foreach qw( active subject body created enddate );
113
114     }
115
116   } #else { die 'guru meditation #295'; }
117
118 }
119
120 sub export_setstatus_listadd {
121   my( $self, $svc_x, $hr ) = @_;
122   $self->export_setstatus_listX( $svc_x, 'add', $hr->{list}, $hr->{address} );
123 }
124
125 sub export_setstatus_listdel {
126   my( $self, $svc_x, $hr ) = @_;
127   $self->export_setstatus_listX( $svc_x, 'del', $hr->{list}, $hr->{address} );
128 }
129
130 sub export_setstatus_listX {
131   my( $self, $svc_x, $action, $list, $address_item ) = @_;
132
133   my $option;
134   if ( $list =~ /^[WA]/i ) { #Whitelist/Allow
135     $option = 'whitelist_';
136   } else { # if ( $hash{'wb_value'} =~ /^[BD]/i ) { #Blacklist/Deny
137     $option = 'blacklist_';
138   }
139   $option .= $action. '_url';
140
141   my $address;
142   unless ( $address = Email::Valid->address($address_item) ) {
143
144     if ( $address_item =~ /^(\@[\w\-\.]+\.\w{2,63})$/ ) { # "@domain"
145       $address = $1;
146     } else {
147       die "address failed $Email::Valid::Details check.\n";
148     }
149
150   }
151
152   #some false laziness w/export_getstatus above
153   my $url;
154   my $urlopt = $self->option($option) or return; #DIFF
155   no strict 'vars';
156   {
157     no strict 'refs';
158     ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
159     ${$_} = $svc_x->$_()         foreach @{ $addl_fields{ $svc_x->table } };
160     $url = eval(qq("$urlopt"));
161   }
162
163   my $req = HTTP::Request::Common::GET( $url );
164   my $ua = LWP::UserAgent->new;
165   my $response = $ua->request($req);
166
167   die $response->code. ' '. $response->message if $response->is_error;
168
169 }
170
171 sub export_setstatus_vacationadd {
172   my( $self, $svc_x, $hr ) = @_;
173   $self->export_setstatus_vacationX( $svc_x, 'add', $hr );
174 }
175
176 sub export_setstatus_vacationdel {
177   my( $self, $svc_x, $hr ) = @_;
178   $self->export_setstatus_vacationX( $svc_x, 'del', $hr );
179 }
180
181 sub export_setstatus_vacationX {
182   my( $self, $svc_x, $action, $hr ) = @_;
183
184   my $option = 'vacation_'. $action. '_url';
185
186   my $subject = uri_escape($hr->{subject});
187   my $body    = uri_escape($hr->{body});
188   for (qw( created enddate )) {
189     if ( $hr->{$_} =~ /^(\d{4}-\d{2}-\d{2})$/ ) {
190       $hr->{$_} = $1;
191     } else {
192       $hr->{$_} = '';
193     }
194   }
195   my $created = $hr->{created};
196   my $enddate = $hr->{enddate};
197
198   #some false laziness w/export_getstatus above
199   my $url;
200   my $urlopt = $self->option($option) or return; #DIFF
201   no strict 'vars';
202   {
203     no strict 'refs';
204     ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
205     ${$_} = $svc_x->$_()         foreach @{ $addl_fields{ $svc_x->table } };
206     $url = eval(qq("$urlopt"));
207   }
208
209   my $req = HTTP::Request::Common::GET( $url );
210   my $ua = LWP::UserAgent->new;
211   my $response = $ua->request($req);
212
213   die $response->code. ' '. $response->message if $response->is_error;
214
215 }
216
217 1;
218
219 1;