File Coverage

blib/lib/Mail/Message/Replace/MailInternet.pm
Criterion Covered Total %
statement 34 178 19.1
branch 0 60 0.0
condition 0 64 0.0
subroutine 12 48 25.0
pod 33 35 94.2
total 79 385 20.5


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Mail-Message version 4.04.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2001-2026 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11              
12             package Mail::Message::Replace::MailInternet;{
13             our $VERSION = '4.04';
14             }
15              
16 1     1   1609 use parent 'Mail::Message';
  1         3  
  1         8  
17              
18 1     1   86 use strict;
  1         2  
  1         26  
19 1     1   5 use warnings;
  1         3  
  1         60  
20              
21 1     1   5 use Log::Report 'mail-message', import => [ qw/__x error/ ];
  1         3  
  1         9  
22              
23 1     1   181 use Mail::Box::FastScalar ();
  1         3  
  1         70  
24 1     1   11 use Mail::Box::Parser::Perl ();
  1         2  
  1         23  
25 1     1   7 use Mail::Message::Body::Lines ();
  1         2  
  1         30  
26              
27 1     1   6 use IO::Handle ();
  1         1  
  1         26  
28 1     1   6 use File::Spec ();
  1         2  
  1         35  
29              
30 1     1   6 use Scalar::Util qw/blessed/;
  1         2  
  1         2971  
31              
32             #--------------------
33              
34             sub new(@)
35 0     0 1   { my $class = shift;
36 0 0         my $data = @_ % 2 ? shift : undef;
37 0 0         $class = __PACKAGE__ if $class eq 'Mail::Internet';
38 0           $class->SUPER::new(@_, raw_data => $data);
39             }
40              
41             sub init($)
42 0     0 0   { my ($self, $args) = @_;
43 0   0       $args->{head_type} ||= 'Mail::Message::Replace::MailHeader';
44 0   0       $args->{head} ||= $args->{Header};
45 0   0       $args->{body} ||= $args->{Body};
46 0           $self->SUPER::init($args);
47              
48 0   0       $self->{MI_wrap} = $args->{FoldLength} || 79;
49 0           $self->{MI_mail_from} = $args->{MailFrom};
50 0 0         $self->{MI_modify} = exists $args->{Modify} ? $args->{Modify} : 1;
51              
52             $self->processRawData($self->{raw_data}, !defined $args->{Header},
53 0 0         !defined $args->{Body}) if defined $self->{raw_data};
54              
55 0           $self;
56             }
57              
58             sub processRawData($$$)
59 0     0 0   { my ($self, $data, $get_head, $get_body) = @_;
60 0 0 0       $get_head || $get_body or return $self;
61              
62 0           my ($filename, $lines);
63 0 0 0       if(ref $data eq 'ARRAY')
    0 0        
64 0           { $filename = 'array of lines';
65 0           $lines = $data;
66             }
67             elsif(ref $data eq 'GLOB' || (blessed $data && $data->isa('IO::Handle')))
68 0           { $filename = 'file (' . (ref $data) . ')';
69 0           $lines = [ $data->getlines ];
70             }
71             else
72 0           { error __x"Mail::Internet does not support {what UNKNOWN} data.", what => $data;
73             }
74              
75 0 0         @$lines or return;
76              
77 0           my $buffer = join '', @$lines;
78 0           my $file = Mail::Box::FastScalar->new(\$buffer);
79 0           my $parser = Mail::Box::Parser::Perl->new(filename => $filename, file => $file, trusted => 1);
80              
81 0           my $head;
82 0 0         if($get_head)
83 0 0         { my $from = $lines->[0] =~ m/^From / ? shift @$lines : undef;
84              
85             my $head = $self->{MM_head_type}->new(
86             MailFrom => $self->{MI_mail_from},
87             Modify => $self->{MI_modify},
88             FoldLength => $self->{MI_wrap}
89 0           );
90 0           $head->read($parser);
91 0 0         $head->mail_from($from) if defined $from;
92 0           $self->head($head);
93             }
94             else
95 0           { $head = $self->head;
96             }
97              
98 0 0         $self->storeBody($self->readBody($parser, $head)) if $get_body;
99 0           $self->addReport($parser);
100 0           $parser->stop;
101 0           $self;
102             }
103              
104              
105             sub dup()
106 0     0 1   { my $self = shift;
107 0           (ref $self)->coerce($self->clone);
108             }
109              
110              
111 0     0 1   sub empty() { $_[0]->DESTROY }
112              
113             #--------------------
114              
115             sub MailFrom(;$)
116 0     0 1   { my $self = shift;
117 0 0         @_ ? ($self->{MI_mail_from} = shift) : $self->{MU_mail_from};
118             }
119              
120             #--------------------
121              
122             sub read($@)
123 0     0 1   { my $thing = shift;
124              
125 0 0         blessed $thing
126             or return $thing->SUPER::read(@_); # Mail::Message behavior
127              
128             # Mail::Header emulation
129 0           my $data = shift;
130 0           $thing->processRawData($data, 1, 1);
131             }
132              
133              
134             sub read_body($)
135 0     0 1   { my ($self, $data) = @_;
136 0           $self->processRawData($data, 0, 1);
137             }
138              
139              
140             sub read_header($)
141 0     0 1   { my ($self, $data) = @_;
142 0           $self->processRawData($data, 1, 0);
143             }
144              
145              
146             sub extract($)
147 0     0 1   { my ($self, $data) = @_;
148 0           $self->processRawData($data, 1, 1);
149             }
150              
151              
152             sub reply(@)
153 0     0 1   { my ($self, %args) = @_;
154              
155 0           my $reply_head = $self->{MM_head_type}->new;
156 0   0       my $home = $ENV{HOME} || File::Spec->curdir;
157 0           my $headtemp = File::Spec->catfile($home, '.mailhdr');
158              
159 0 0         if(open my $head, '<:raw', $headtemp)
160 0           { my $parser = Mail::Box::Parser::Perl->new(filename => $headtemp, file => $head, trusted => 1);
161 0           $reply_head->read($parser);
162 0           $parser->close;
163             }
164              
165 0   0       $args{quote} ||= delete $args{Inline} || '>';
      0        
166 0   0       $args{group_reply} ||= delete $args{ReplyAll} || 0;
      0        
167 0   0       my $keep = delete $args{Keep} || [];
168 0   0       my $exclude = delete $args{Exclude} || [];
169              
170 0           my $reply = $self->SUPER::reply(%args);
171 0           my $head = $self->head;
172              
173 0           $reply_head->add($_->clone) for map $head->get($_), @$keep;
174 0           $reply_head->reset($_) for @$exclude;
175              
176 0           (ref $self)->coerce($reply);
177             }
178              
179              
180             sub add_signature(;$)
181 0     0 1   { my $self = shift;
182 0   0       my $fn = shift // File::Spec->catfile($ENV{HOME} || File::Spec->curdir, '.signature');
      0        
183 0           $self->sign(File => $fn);
184             }
185              
186              
187             sub sign(@)
188 0     0 1   { my ($self, $args) = @_;
189 0           my $sig;
190              
191 0 0         if(my $filename = delete $self->{File})
    0          
192 0           { $sig = Mail::Message::Body->new(file => $filename);
193             }
194             elsif(my $sign = delete $self->{Signature})
195 0           { $sig = Mail::Message::Body->new(data => $sign);
196             }
197              
198 0 0         defined $sig or return;
199              
200 0           my $body = $self->decoded->stripSignature;
201 0           my $set = $body->concatenate($body, "-- \n", $sig);
202 0 0         $self->body($set) if defined $set;
203 0           $set;
204             }
205              
206             #--------------------
207              
208             sub send($@)
209 0     0 1   { my ($self, $type, %args) = @_;
210 0           $self->send(via => $type);
211             }
212              
213              
214             #--------------------
215              
216             sub head(;$)
217 0     0 1   { my $self = shift;
218 0 0         return $self->SUPER::head(@_) if @_;
219 0   0       $self->SUPER::head // $self->{MM_head_type}->new(message => $self);
220             }
221              
222              
223 0     0 1   sub header(;$) { shift->head->header(@_) }
224              
225              
226 0     0 1   sub fold(;$) { shift->head->fold(@_) }
227              
228              
229 0     0 1   sub fold_length(;$$) { shift->head->fold_length(@_) }
230              
231              
232 0     0 1   sub combine($;$) { shift->head->combine(@_) }
233              
234              
235 0     0 1   sub print_header(@) { shift->head->print(@_) }
236              
237              
238 0     0 1   sub clean_header() { $_[0]->header }
239              
240              
241       0 1   sub tidy_headers() { }
242              
243              
244 0     0 1   sub add(@) { shift->head->add(@_) }
245              
246              
247 0     0 1   sub replace(@) { shift->head->replace(@_) }
248              
249              
250 0     0 1   sub get(@) { shift->head->get(@_) }
251              
252              
253             sub delete(@)
254 0     0 1   { my $self = shift;
255 0 0         @_ ? $self->head->delete(@_) : $self->SUPER::delete;
256             }
257              
258             #--------------------
259              
260             sub body(@)
261 0     0 1   { my $self = shift;
262              
263 0 0         unless(@_)
264 0           { my $body = $self->body;
265 0 0         return defined $body ? (scalar $body->lines) : [];
266             }
267              
268 0 0         my $data = ref $_[0] eq 'ARRAY' ? shift : \@_;
269 0           my $body = Mail::Message::Body::Lines->new(data => $data);
270 0           $self->body($body);
271              
272 0           $body;
273             }
274              
275              
276 0     0 1   sub print_body(@) { shift->SUPER::body->print(@_) }
277              
278              
279 0     0 1   sub bodyObject(;$) { shift->SUPER::body(@_) }
280              
281              
282             sub remove_sig(;$)
283 0     0 1   { my ($self, $lines) = @_;
284 0   0       my $stripped = $self->decoded->stripSignature(max_lines => $lines // 10);
285 0 0         $self->body($stripped) if defined $stripped;
286 0           $stripped;
287             }
288              
289              
290             sub tidy_body(;$)
291 0     0 1   { my $self = shift;
292              
293 0 0         my $body = $self->body or return;
294 0           my @body = $body->lines;
295              
296 0   0       shift @body while @body && $body[ 0] =~ m/^\s*$/;
297 0   0       pop @body while @body && $body[-1] =~ m/^\s*$/;
298              
299 0 0         return $body if $body->nrLines == @body;
300 0           my $new = Mail::Message::Body::Lines->new(based_on => $body, data=>\@body);
301 0           $self->body($new);
302             }
303              
304              
305             sub smtpsend(@)
306 0     0 1   { my ($self, %args) = @_;
307 0   0       my $from = $args{MailFrom} || $ENV{MAILADDRESS} || $ENV{USER} || 'unknown';
308 0   0       $args{helo} ||= delete $args{Hello};
309 0   0       $args{port} ||= delete $args{Port};
310 0   0       $args{smtp_debug} ||= delete $args{Debug};
311              
312 0           my $host = $args{Host};
313 0 0         unless(defined $host)
314 0           { my $hosts = $ENV{SMTPHOSTS};
315 0 0         $host = (split /\:/, $hosts)[0] if defined $hosts;
316             }
317 0           $args{host} = $host;
318              
319 0           $self->send(via => 'smtp', %args);
320             }
321              
322             #--------------------
323              
324             sub as_mbox_string()
325 0     0 1   { my $self = shift;
326 0           my $mboxmsg = Mail::Box::Mbox->coerce($self);
327              
328 0           my $buffer = '';
329 0           my $file = Mail::Box::FastScalar->new(\$buffer);
330 0           $mboxmsg->print($file);
331 0           $buffer;
332             }
333              
334             #--------------------
335              
336             BEGIN {
337 1     1   12 no warnings;
  1         4  
  1         113  
338             *Mail::Internet::new = sub (@) {
339 0     0     my $class = shift;
340 0           Mail::Message::Replace::MailInternet->new(@_);
341 1     1   152 };
342             }
343              
344              
345             sub isa($)
346 0     0 1   { my ($thing, $class) = @_;
347 0 0         $class eq 'Mail::Internet' ? 1 : $thing->SUPER::isa($class);
348             }
349              
350             #--------------------
351              
352 0     0 1   sub coerce() { $_[0]->notImplemented }
353              
354             1;