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 ) = @_;
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';
141 $address = Email::Valid->address($address)
142 or die "address failed $Email::Valid::Details check.\n";
144 #some false laziness w/export_getstatus above
146 my $urlopt = $self->option($option) or return; #DIFF
150 ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
151 ${$_} = $svc_x->$_() foreach @{ $addl_fields{ $svc_x->table } };
152 $url = eval(qq("$urlopt"));
155 my $req = HTTP::Request::Common::GET( $url );
156 my $ua = LWP::UserAgent->new;
157 my $response = $ua->request($req);
159 die $response->code. ' '. $response->message if $response->is_error;
163 sub export_setstatus_vacationadd {
164 my( $self, $svc_x, $hr ) = @_;
165 $self->export_setstatus_vacationX( $svc_x, 'add', $hr );
168 sub export_setstatus_vacationdel {
169 my( $self, $svc_x, $hr ) = @_;
170 $self->export_setstatus_vacationX( $svc_x, 'del', $hr );
173 sub export_setstatus_vacationX {
174 my( $self, $svc_x, $action, $hr ) = @_;
176 my $option = 'vacation_'. $action. '_url';
178 my $subject = uri_escape($hr->{subject});
179 my $body = uri_escape($hr->{body});
180 for (qw( created enddate )) {
181 if ( $hr->{$_} =~ /^(\d{4}-\d{2}-\d{2})$/ ) {
187 my $created = $hr->{created};
188 my $enddate = $hr->{enddate};
190 #some false laziness w/export_getstatus above
192 my $urlopt = $self->option($option) or return; #DIFF
196 ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
197 ${$_} = $svc_x->$_() foreach @{ $addl_fields{ $svc_x->table } };
198 $url = eval(qq("$urlopt"));
201 my $req = HTTP::Request::Common::GET( $url );
202 my $ua = LWP::UserAgent->new;
203 my $response = $ua->request($req);
205 die $response->code. ' '. $response->message if $response->is_error;