06b1bc02f0829cb059539896254028d219f436fb
[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 ) = @_;
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   $address = Email::Valid->address($address)
142     or die "address failed $Email::Valid::Details check.\n";
143
144   #some false laziness w/export_getstatus above
145   my $url;
146   my $urlopt = $self->option($option) or return; #DIFF
147   no strict 'vars';
148   {
149     no strict 'refs';
150     ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
151     ${$_} = $svc_x->$_()         foreach @{ $addl_fields{ $svc_x->table } };
152     $url = eval(qq("$urlopt"));
153   }
154
155   my $req = HTTP::Request::Common::GET( $url );
156   my $ua = LWP::UserAgent->new;
157   my $response = $ua->request($req);
158
159   die $response->code. ' '. $response->message if $response->is_error;
160
161 }
162
163 sub export_setstatus_vacationadd {
164   my( $self, $svc_x, $hr ) = @_;
165   $self->export_setstatus_vacationX( $svc_x, 'add', $hr );
166 }
167
168 sub export_setstatus_vacationdel {
169   my( $self, $svc_x, $hr ) = @_;
170   $self->export_setstatus_vacationX( $svc_x, 'del', $hr );
171 }
172
173 sub export_setstatus_vacationX {
174   my( $self, $svc_x, $action, $hr ) = @_;
175
176   my $option = 'vacation_'. $action. '_url';
177
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})$/ ) {
182       $hr->{$_} = $1;
183     } else {
184       $hr->{$_} = '';
185     }
186   }
187   my $created = $hr->{created};
188   my $enddate = $hr->{enddate};
189
190   #some false laziness w/export_getstatus above
191   my $url;
192   my $urlopt = $self->option($option) or return; #DIFF
193   no strict 'vars';
194   {
195     no strict 'refs';
196     ${$_} = $svc_x->getfield($_) foreach $svc_x->fields;
197     ${$_} = $svc_x->$_()         foreach @{ $addl_fields{ $svc_x->table } };
198     $url = eval(qq("$urlopt"));
199   }
200
201   my $req = HTTP::Request::Common::GET( $url );
202   my $ua = LWP::UserAgent->new;
203   my $response = $ua->request($req);
204
205   die $response->code. ' '. $response->message if $response->is_error;
206
207 }
208
209 1;
210
211 1;