File Coverage

blib/lib/MailX/Qmail/Queue/Message.pm
Criterion Covered Total %
statement 14 44 31.8
branch 0 14 0.0
condition 0 6 0.0
subroutine 5 11 45.4
pod 5 5 100.0
total 24 80 30.0


line stmt bran cond sub pod time code
1 1     1   17 use 5.014;
  1         3  
2 1     1   6 use warnings;
  1         2  
  1         52  
3              
4             package MailX::Qmail::Queue::Message;
5              
6             our $VERSION = '1.0';
7              
8 1     1   6 use base 'Mail::Qmail::Queue::Message';
  1         2  
  1         523  
9              
10 1     1   15874 use Mail::Address;
  1         2388  
  1         33  
11 1     1   463 use Mail::Header;
  1         4323  
  1         379  
12              
13             # Use inside-out attributes to avoid interference with base class:
14             my ( %header, %body );
15              
16             sub header {
17 0     0 1   my $self = shift;
18 0 0         return $header{$self} if exists $header{$self};
19 0 0         open my $fh, '<', $self->body_ref or die 'Cannot read message';
20 0           $header{$self} = Mail::Header->new($fh);
21 0           local $/;
22 0           $body{$self} = <$fh>;
23 0           $header{$self};
24             }
25              
26             sub header_from {
27 0     0 1   my $self = shift;
28 0 0         my $from = $self->header->get('From') or return;
29 0           ($from) = Mail::Address->parse($from);
30 0           $from;
31             }
32              
33             sub helo {
34 0     0 1   my $header = shift->header;
35 0 0         my $received = $header->get('Received') or return;
36 0 0 0       $received =~ /^from .*? \(HELO (.*?)\) /
37             or $received =~ /^from (\S+) \(/
38             or return;
39 0           $1;
40             }
41              
42             sub add_header {
43 0     0 1   my $self = shift;
44 0           ${ $self->body_ref } = join "\n", @_, $self->body;
  0            
45 0           delete $header{$self};
46 0           $self;
47             }
48              
49             sub replace_header {
50 0     0 1   my ( $self, $header ) = @_;
51 0 0         $self->header unless exists $body{$self}; # force parsing
52 0 0 0       $header = $header->as_string if ref $header && $header->can('as_string');
53 0           ${ $self->body_ref } = join "\n", $header, $body{$self};
  0            
54 0           delete $header{$self};
55 0           $self;
56             }
57              
58             sub DESTROY {
59 0     0     my $self = shift;
60 0           delete $header{$self};
61 0           delete $body{$self};
62             }
63              
64             1;
65              
66             __END__
67              
68             =head1 NAME
69              
70             MailX::Qmail::Queue::Message - extensions to Mail::Qmail::Queue::Message
71              
72             =head1 DESCRIPTION
73              
74             This class extends L<Mail::Qmail::Queue::Message>.
75              
76             =head1 METHODS
77              
78             =over 4
79              
80             =item ->header
81              
82             get the header of the incoming message as L<Mail::Header> object
83              
84             =item ->header_from
85              
86             get the C<From:> header field of the incoming message as L<Mail::Address> object
87              
88             =item ->helo
89              
90             get the C<HELO>/C<EHLO> string used by the client
91              
92             =item ->add_header
93              
94             Add header fields to the message.
95             Expects C<Field: Value> as argument, without newlines at the end.
96              
97             =item ->replace_header($header)
98              
99             Replace the whole header of the message.
100             C<$header> should either be a properly formatted e-mail header
101             or an object with an C<as_string> method which produces such a string,
102             e.g. a L<Mail::Header> object.
103              
104             =back
105              
106             =head1 BUGS
107              
108             Please report any bugs or feature requests to
109             C<bug-mail-qmail-filter at rt.cpan.org>, or through the web interface at
110             L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mail-Qmail-Filter>.
111             I will be notified, and then you'll automatically be notified of progress on
112             your bug as I make changes.
113              
114             =head1 SUPPORT
115              
116             You can find documentation for this module with the perldoc command.
117              
118             perldoc Mail::Qmail::Filter
119              
120             You can also look for information at:
121              
122             =over 4
123              
124             =item * RT: CPAN's request tracker (report bugs here)
125              
126             L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Mail-Qmail-Filter>
127              
128             =item * AnnoCPAN: Annotated CPAN documentation
129              
130             L<https://annocpan.org/dist/Mail-Qmail-Filter>
131              
132             =item * CPAN Ratings
133              
134             L<https://cpanratings.perl.org/dist/Mail-Qmail-Filter>
135              
136             =item * Search CPAN
137              
138             L<https://metacpan.org/release/Mail-Qmail-Filter>
139              
140             =back
141              
142             =head1 ACKNOWLEDGEMENTS
143             =head1 LICENSE AND COPYRIGHT
144              
145             Copyright 2019 Martin Sluka.
146              
147             This program is free software; you can redistribute it and/or modify it
148             under the terms of the the Artistic License (2.0). You may obtain a
149             copy of the full license at:
150              
151             L<http://www.perlfoundation.org/artistic_license_2_0>
152              
153             Any use, modification, and distribution of the Standard or Modified
154             Versions is governed by this Artistic License. By using, modifying or
155             distributing the Package, you accept this license. Do not use, modify,
156             or distribute the Package, if you do not accept this license.
157              
158             If your Modified Version has been derived from a Modified Version made
159             by someone other than you, you are nevertheless required to ensure that
160             your Modified Version complies with the requirements of this license.
161              
162             This license does not grant you the right to use any trademark, service
163             mark, tradename, or logo of the Copyright Holder.
164              
165             This license includes the non-exclusive, worldwide, free-of-charge
166             patent license to make, have made, use, offer to sell, sell, import and
167             otherwise transfer the Package with respect to any patent claims
168             licensable by the Copyright Holder that are necessarily infringed by the
169             Package. If you institute patent litigation (including a cross-claim or
170             counterclaim) against any party alleging that the Package constitutes
171             direct or contributory patent infringement, then this Artistic License
172             to you shall terminate on the date that such litigation is filed.
173              
174             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
175             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
176             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
177             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
178             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
179             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
180             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
181             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182              
183             =cut