summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-fetch
blob: f689bfd93e2c08e93d3c7c11f66ffc4b2e848242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearchs);
use FS::Misc qw(send_email);

my $user = shift or die &usage;
my $employeelist = shift or die &usage;
my $url = shift or die &usage;
adminsuidsetup $user;

my @employees = split ',', $employeelist;

foreach my $employee (@employees) {

  $employee =~ /^(\w+)$/;

  my $access_user = qsearchs( 'access_user', { 'username' => $1 } );
  unless ($access_user) {
    warn "Can't find employee $employee... skipping";
    next;
  }

  my $email_address = $access_user->option('email_address');
  unless ($email_address) {
    warn "No email address for $employee... skipping";
    next;
  }

  no warnings 'redefine';
  local *LWP::UserAgent::get_basic_credentials = sub {
    return ($access_user->username, $access_user->_password);
  };

  my $ua = new LWP::UserAgent;
  $ua->timeout(1800); #30m, some reports can take a while
  $ua->agent("FreesideFetcher/0.1 " . $ua->agent);

  my $req = new HTTP::Request GET => $url;
  my $res = $ua->request($req);

  my $conf = new FS::Conf;
  my $subject = $conf->config('email_report-subject') || 'Freeside report';

  my %options = ( 'from'             => $email_address,
                  'to'               => $email_address,
                  'subject'          => $subject,
                  'body'             => $res->content,
                );

  $options{'content-type'} = $res->content_type
    if $res->content_type;
  $options{'content-encoding'} = $res->content_encoding
    if $res->content_encoding;

  if ($res->is_success) {
    send_email %options;
  }else{
    warn "fetching $url failed for $employee: " . $res->status_line;
  }
}

sub usage {
  die "Usage:\n\n  freeside-fetch user employee[,employee ...] url\n\n";
}

=head1 NAME

freeside-fetch - Send a freeside page to a list of employees.

=head1 SYNOPSIS

  freeside-fetch user employee[,employee ...] url

=head1 DESCRIPTION

  Fetches a web page specified by url as if employee and emails it to
  employee.  Useful when run out of cron to send freeside web pages.

  user: From the mapsecrets file - a user with access to the freeside database

  employee: the username of an employee to receive the emailed page.  May be a comma separated list

  url: the web page to be received

=head1 BUGS

  Can leak employee usernames and passwords if requested to access inappropriate urls.

=cut