1 package FS::part_export::http_status;
2 use base qw( FS::part_export );
6 use vars qw( %info $DEBUG );
9 use HTTP::Request::Common;
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', },
21 #'user' => { label => 'Username', default=>'' },
22 #'password' => { label => 'Password', default => '' },
26 'svc' => [ 'svc_acct', 'svc_dsl', ],
27 'desc' => 'Retrieve status information via HTTP or HTTPS',
28 'options' => \%options,
30 Fields from the service can be substituted in the URL as $field.
32 Optionally, spam black/whitelist addresees and a vacation message may be
33 modified via HTTP or HTTPS as well.
39 sub rebless { shift; }
42 'svc_acct' => [qw( email ) ],
43 'svc_dsl' => [qw( gateway_access_or_phonenum ) ],
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 { '' };
52 sub export_getstatus {
53 my( $self, $svc_x, $htmlref, $hashref ) = @_;
56 my $urlopt = $self->option('url');
60 ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
61 ${$_} = $svc_x->$_() foreach @{ $addl_fields{ $svc_x->table } };
62 $url = eval(qq("$urlopt"));
65 my $req = HTTP::Request::Common::GET( $url );
66 my $ua = LWP::UserAgent->new;
67 my $response = $ua->request($req);
69 if ( $svc_x->table eq 'svc_dsl' ) {
71 $$htmlref = $response->is_error ? $response->error_as_HTML
74 #hash data not yet implemented for svc_dsl
76 } elsif ( $svc_x->table eq 'svc_acct' ) {
78 #this whole section is rather specific to fibernetics and should be an
79 # option or callback or something
84 my $csv = Text::CSV_XS->new;
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;
92 while ( my $line = shift @lines ) {
93 $csv->parse($line) or next;
94 my @fields = $csv->fields;
95 my %hash = map { $_ => shift(@fields) } @header;
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'};
105 for (qw( created enddate )) {
106 $hash{$_} = '' if $hash{$_} =~ /^0000-/;
107 $hash{$_} = (split(' ', $hash{$_}))[0];
110 next unless $hash{'active'};
111 $hashref->{"vacation_$_"} = $hash{$_} || ''
112 foreach qw( active subject body created enddate );
116 } #else { die 'guru meditation #295'; }
120 sub export_setstatus_listadd {
121 my( $self, $svc_x, $hr ) = @_;
122 $self->export_setstatus_listX( $svc_x, 'add', $hr->{list}, $hr->{address} );
125 sub export_setstatus_listdel {
126 my( $self, $svc_x, $hr ) = @_;
127 $self->export_setstatus_listX( $svc_x, 'del', $hr->{list}, $hr->{address} );
130 sub export_setstatus_listX {
131 my( $self, $svc_x, $action, $list, $address_item ) = @_;
134 if ( $list =~ /^[WA]/i ) { #Whitelist/Allow
135 $option = 'whitelist_';
136 } else { # if ( $hash{'wb_value'} =~ /^[BD]/i ) { #Blacklist/Deny
137 $option = 'blacklist_';
139 $option .= $action. '_url';
142 unless ( $address = Email::Valid->address($address_item) ) {
144 if ( $address_item =~ /^(\@[\w\-\.]+\.\w{2,63})$/ ) { # "@domain"
147 die "address failed $Email::Valid::Details check.\n";
152 #some false laziness w/export_getstatus above
154 my $urlopt = $self->option($option) or return; #DIFF
158 ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
159 ${$_} = $svc_x->$_() foreach @{ $addl_fields{ $svc_x->table } };
160 $url = eval(qq("$urlopt"));
163 my $req = HTTP::Request::Common::GET( $url );
164 my $ua = LWP::UserAgent->new;
165 my $response = $ua->request($req);
167 die $response->code. ' '. $response->message if $response->is_error;
171 sub export_setstatus_vacationadd {
172 my( $self, $svc_x, $hr ) = @_;
173 $self->export_setstatus_vacationX( $svc_x, 'add', $hr );
176 sub export_setstatus_vacationdel {
177 my( $self, $svc_x, $hr ) = @_;
178 $self->export_setstatus_vacationX( $svc_x, 'del', $hr );
181 sub export_setstatus_vacationX {
182 my( $self, $svc_x, $action, $hr ) = @_;
184 my $option = 'vacation_'. $action. '_url';
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})$/ ) {
195 my $created = $hr->{created};
196 my $enddate = $hr->{enddate};
198 #some false laziness w/export_getstatus above
200 my $urlopt = $self->option($option) or return; #DIFF
204 ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
205 ${$_} = $svc_x->$_() foreach @{ $addl_fields{ $svc_x->table } };
206 $url = eval(qq("$urlopt"));
209 my $req = HTTP::Request::Common::GET( $url );
210 my $ua = LWP::UserAgent->new;
211 my $response = $ua->request($req);
213 die $response->code. ' '. $response->message if $response->is_error;