blib/lib/oEdtk/FatalsToEmail.pm

filter: Statementstmt 16.9% 9 / 53 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 40.0% 2 / 5 Podpod 50.0% 1 / 2 totaltotal 13.3% 12 / 90 CC 16 SCAR 48.1
CC
16
Cov
11%
send_mail
182.0
import
20.0
trapper
2.0
CRAP 122.5
« blib/lib/oEdtk/dataEdtk.pm ↑ Summary lib/oEdtk/Main.pm »
1 #!/usr/bin/perl
2 package oEdtk::FatalsToEmail;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '2.1131';
7  
8 # by Randal L. Schwartz http://www.stonehenge.com/merlyn/LinuxMag/col14.html
9  
10 #require oEdtk::FatalsToEmail;
11 #use oEdtk::FatalsToEmail qw(
12 # Address gu@mnt0.fr
13 # Mailhost srv.smnt0.fr
14 # );
15  
16 1 my %config = (
17 Address => "webmaster", # email address
18 Mailhost => "localhost", # mail server
19 Cache => undef, # undef means don't use
20 Seconds => 60,
21 );
22  
23 0 ▶sub import {
Subroutine import: not called CC=4 SCAR=30.0
24 0 my $package = shift;
25 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
26 0 my $key = ucfirst lc shift;
27 0 ▶ die "ERROR: missing argument to $key" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
28 0 ▶ die "ERROR: unknown argument $key" unless exists $config{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config{$key} 0 0 0
29 0 $config{$key} = shift;
30 }
31 }
32  
33 1 $SIG{__DIE__} = \&trapper;
34  
35 0 ▶sub trapper {
Subroutine trapper: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my $message = shift;
37 0 my $time = localtime;
38 0 my ( $pack, $file, $line ) = caller;
39  
40 0 my $prefix = localtime;
41 0 $prefix .= ":$$:$file:$line: ";
42 0 $message =~ s/^/$prefix/mig;
43  
44 0 print STDOUT <<END;
45 Content-Type: text/html
46  
47 <h1>Sorry!</h1>
48 <p>An error has occurred; details have been logged.
49 Please try your request again later.
50
51 END
52  
53 0 send_mail($message);
54  
55 0 die "ERROR: ${prefix}died - email sent to $config{Address} via $config{Mailhost}\n";
56 }
57  
58 0 ▶sub send_mail {
Subroutine send_mail: not called CC=13 SCAR=52.0
Pod documented
59 0 my $message = shift;
60  
61 0 eval {
62 ## do I need to cache this?
63 0 ▶ if ( defined( my $cache = $config{Cache} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cache = $config{"Cache"})) 0 0 0
64 0 my $cache_fh;
65 0 ▶ if ( open( $cache_fh, '+<', $cache ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $cache_fh, '+<', $cache) { } 0 0 0
66 0 flock $cache_fh, 2;
67 ## it's mine, see if it's old enough
68 0 ▶ if ( time - ( stat($cache_fh) )[9] > $config{Seconds} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - (stat $cache_fh)[9] > $config{'Seconds'}) { } 0 0 0
69 ## yes, suck any content, and zero the file
70 0 my $buf;
71 0 ▶ $buf .= "\n...[truncated]...\n" if read( $cache_fh, $buf, 8192 ) >= 8192;
Branch: 1 branch
BranchTrueFalseTotal
if read($cache_fh, $buf, 8192) >= 8192 0 0 0
72 0 $message = $buf . $message;
73 0 seek $cache_fh, 0, 0;
74 0 truncate $cache_fh, 0;
75 0 close $cache_fh;
76 }
77 else {
78 ## no, so just drop the stuff at the end
79 0 seek $cache_fh, 0, 2;
80 0 print $cache_fh $message;
81 0 close $cache_fh;
82 0 return;
83 }
84 }
85 else {
86 ## it doesn't exist, so create an empty file for stamping, and email
87 0 ▶ open( $cache_fh, '>>', $cache ) or die "ERROR: Cannot create $cache: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $cache_fh, ">>", $cache or die "ERROR: Cannot create ${cache}: $!" 0 0 0
88 0 close $cache_fh;
89 }
90 }
91 0 ▶ eval { require Net::SMTP; 1 } or die "ERROR: no Net::SMTP";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Net::SMTP; 1 } } or die "ERROR: no Net::SMTP" 0 0 0
92 my $mail = Net::SMTP->new( $config{Mailhost} )
93 0 ▶ or die "Net::SMTP->new returned $@";
Branch: 1 branch
BranchTrueFalseTotal
my $mail = "Net::SMTP"->new($config{"Mailhost"}) or die "Net::SMTP->new returned $@" 0 0 0
94 0 ▶ $mail->mail( $config{Address} ) or die "ERROR: from= $@";
Branch: 1 branch
BranchTrueFalseTotal
$mail->mail($config{"Address"}) or die "ERROR: from= $@" 0 0 0
95 0 ▶ $mail->to( $config{Address} ) or die "ERROR: to= $@";
Branch: 1 branch
BranchTrueFalseTotal
$mail->to($config{"Address"}) or die "ERROR: to= $@" 0 0 0
96 0 ▶ $mail->data( "Subject: CGI FATAL ERROR in $0\n\n", $message )
Branch: 1 branch
BranchTrueFalseTotal
$mail->data("Subject: CGI FATAL ERROR in $0\n\n", $message) or die "ERROR: data= $@" 0 0 0
97 or die "ERROR: data= $@";
98 0 ▶ $mail->quit or die "ERROR: quit said $@";
Branch: 1 branch
BranchTrueFalseTotal
$mail->quit or die "ERROR: quit said $@" 0 0 0
99 };
100  
101 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
102 0 die "ERROR: $message(send_mail saw $@)\n";
103 }
104 }
105  
106 =head1 NAME
107  
108 oEdtk::FatalsToEmail - turn fatal errors into an email
109  
110 =head1 SYNOPSIS
111  
112 use oEdtk::FatalsToEmail
113 Address => 'admin@example.com',
114 Mailhost => 'smtp.example.com';
115  
116 =head1 DESCRIPTION
117  
118 Installs a C<__DIE__> handler that prefixes the fatal message with its location,
119 prints a short apology, mails the details and then re-dies. The C<Address>,
120 C<Mailhost>, C<Cache> and C<Seconds> parameters are given at load time; when a
121 C<Cache> file is set, messages that arrive within C<Seconds> are coalesced into a
122 single email. Adapted from Randal L. Schwartz (Stonehenge).
123  
124 =head1 FUNCTIONS
125  
126 =head2 import
127  
128 use oEdtk::FatalsToEmail Address => $address, Mailhost => $smtp;
129  
130 Set the module parameters at load time. An unknown key or a missing value is an
131 error.
132  
133 =head2 send_mail
134  
135 send_mail($message);
136  
137 Email C<$message> to the configured address through C<Net::SMTP>, optionally
138 through the cache file. Dies when the transfer fails.
139  
140 The C<trapper> sub is the internal handler installed as C<$SIG{__DIE__}>; it is
141 not part of the public API.
142  
143 =head1 AUTHOR
144  
145 The oEdtk team.
146  
147 =cut
« blib/lib/oEdtk/dataEdtk.pm ↑ Summary lib/oEdtk/Main.pm »