This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / bin / freeside-fetch
1 #!/usr/bin/perl -w
2
3 use strict;
4 use LWP::UserAgent;
5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw(qsearchs);
7 use FS::Misc qw(send_email);
8
9 my $user = shift or die &usage;
10 my $employeelist = shift or die &usage;
11 my $url = shift or die &usage;
12 adminsuidsetup $user;
13
14 my @employees = split ',', $employeelist;
15
16 foreach my $employee (@employees) {
17
18   $employee =~ /^(\w+)$/;
19
20   my $access_user = qsearchs( 'access_user', { 'username' => $1 } );
21   unless ($access_user) {
22     warn "Can't find employee $employee... skipping";
23     next;
24   }
25
26   my $email_address = $access_user->option('email_address');
27   unless ($email_address) {
28     warn "No email address for $employee... skipping";
29     next;
30   }
31
32   no warnings 'redefine';
33   local *LWP::UserAgent::get_basic_credentials = sub {
34     return ($access_user->username, $access_user->_password);
35   };
36
37   my $ua = new LWP::UserAgent;
38   $ua->timeout(1800); #30m, some reports can take a while
39   $ua->agent("FreesideFetcher/0.1 " . $ua->agent);
40
41   my $req = new HTTP::Request GET => $url;
42   my $res = $ua->request($req);
43
44   my $conf = new FS::Conf;
45   my $subject = $conf->config('email_report-subject') || 'Freeside report';
46
47   my %options = ( 'from'             => $email_address,
48                   'to'               => $email_address,
49                   'subject'          => $subject,
50                   'body'             => $res->content,
51                 );
52
53   $options{'content-type'} = $res->content_type
54     if $res->content_type;
55   $options{'content-encoding'} = $res->content_encoding
56     if $res->content_encoding;
57
58   if ($res->is_success) {
59     send_email %options;
60   }else{
61     warn "fetching $url failed for $employee: " . $res->status_line;
62   }
63 }
64
65 sub usage {
66   die "Usage:\n\n  freeside-fetch user employee[,employee ...] url\n\n";
67 }
68
69 =head1 NAME
70
71 freeside-fetch - Send a freeside page to a list of employees.
72
73 =head1 SYNOPSIS
74
75   freeside-fetch user employee[,employee ...] url
76
77 =head1 DESCRIPTION
78
79   Fetches a web page specified by url as if employee and emails it to
80   employee.  Useful when run out of cron to send freeside web pages.
81
82   user: From the mapsecrets file - a user with access to the freeside database
83
84   employee: the username of an employee to receive the emailed page.  May be a comma separated list
85
86   url: the web page to be received
87
88 =head1 BUGS
89
90   Can leak employee usernames and passwords if requested to access inappropriate urls.
91
92 =cut
93