File Coverage

blib/lib/Mail/Qmail/Queue/Receive/Body.pm
Criterion Covered Total %
statement 28 29 96.5
branch 2 6 33.3
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 42 47 89.3


line stmt bran cond sub pod time code
1             package Mail::Qmail::Queue::Receive::Body;
2             our $VERSION = 0.02;
3             #
4             # Copyright 2006 Scott Gifford
5             #
6             # This library is free software; you can redistribute it and/or
7             # modify it under the same terms as Perl itself.
8              
9 4     4   32936 use warnings;
  4         8  
  4         147  
10 4     4   22 use strict;
  4         9  
  4         135  
11              
12 4     4   24 use constant QQ_BODY_FD => 0;
  4         6  
  4         334  
13              
14             =head1 NAME
15              
16             Mail::Qmail::Queue::Receive::Body - Receive message body when emulating qmail-queue
17              
18             =head1 SYNOPSIS
19              
20             use Mail::Qmail::Queue::Receive::Body;
21              
22             my $qq_body = Mail::Qmail::Queue::Receive::Body->new
23             or die "Couldn't get qmail-queue body\n"
24              
25             print "Message body: ",$qq_body->body,"\n";
26              
27             my $fh = $qq_body->body_fh
28             or die "Error getting body handle: $!\n";
29             while (<$fh>) {
30             s/perl/Pathologically Eclectic Rubbish Lister/gi;
31             print;
32             }
33             $fh->close
34             or die "Error closing message: $!\n";
35              
36             =head1 DESCRIPTION
37              
38             C is designed for use in
39             C emulation. This is a way of modifying the behavior of
40             qmail by replacing its queueing mechanism with your own program, which
41             may modify or reject the message, then call the real C
42             program to queue the message. This is commonly done with Bruce
43             Guenter's QMAILQUEUE patch
44             (L), also included in
45             netqmail (L). This patch
46             lets you override the standard C program by setting the
47             environment variable C. It can also be done by renaming
48             the original C, installing your script in its place, and
49             having your script call the renamed C to inject the
50             message.
51              
52             For a simplified interface, see L. To
53             read the message envelope, see L.
54             To re-inject the message, see L.
55              
56             Note that the specifications for C's interface require
57             that the message be read before the envelope.
58              
59             The constructor and methods of this class will C if they
60             encounter a serious error. If you would prefer different behavior,
61             use C to catch these and handle them as exceptions.
62              
63             =cut
64              
65 4     4   1065 use Mail::Qmail::Queue::Error qw(:errcodes :fail);
  4         39  
  4         2196  
66              
67             =head2 CONSTRUCTOR
68              
69             =over 4
70              
71             =item new ( %options )
72              
73             Creates a new qmail-queue message body reader, but does not start
74             reading it.
75              
76             Available options are:
77              
78             =over 4
79              
80             =item FileHandle
81              
82             Read the body from the specified file handle, instead of the default
83             of file desriptor 0.
84              
85             =back
86              
87             =back
88              
89             =cut
90              
91             sub new
92             {
93 4     4 1 70 my $class = shift;
94 4         15 my %o = @_;
95 4         14 my $self = bless {}, $class;
96            
97 4 50       16 if ($o{FileHandle})
98             {
99 4         21 $self->{_fh} = $o{FileHandle};
100             }
101             else
102             {
103 0 0       0 $self->{_fh} = FileHandle->new_from_fd(QQ_BODY_FD,"r")
104             or permfail QQ_EXIT_READERR, "Couldn't open FD 0 to read message: $!\n";
105             }
106 4         19 $self;
107             }
108              
109             =head2 METHODS
110              
111             =over 4
112              
113             =item body_fh( )
114              
115             Returns a filehandle from which the body can be read.
116              
117             =cut
118              
119             sub body_fh
120             {
121 1     1 1 2 my $self = shift;
122 1         4 return $self->{_fh};
123             }
124              
125              
126             =item close( )
127              
128             Closes the filehandle with the message body, and returns the result of
129             the C.
130              
131             =cut
132              
133             sub close
134             {
135 4     4 1 1481 my $self = shift;
136 4         69 return close($self->{_fh});
137             }
138              
139              
140             =item body( )
141              
142             Returns the entire body as a string, then closes the filehandle. Note
143             that this can consume a lot of memory for a very large message;
144             reading it from the handle returned by the C method will be
145             more efficient.
146              
147             =cut
148              
149             sub body
150             {
151 3     3 1 14 my $self = shift;
152 3         10 my $fh = $self->{_fh};
153 3         12 local $/ = undef;
154 3         94 my $body = <$fh>;
155 3 50       15 $self->close()
156             or die "Error closing message body filehandle: $!\n";
157 3         34 return $body;
158             }
159              
160             =back
161              
162             =head1 SEE ALSO
163              
164             L, L,
165             L, L.
166              
167             =head1 COPYRIGHT
168              
169             Copyright 2006 Scott Gifford.
170              
171             This library is free software; you can redistribute it and/or
172             modify it under the same terms as Perl itself.
173              
174             =cut
175              
176              
177             1;