File: Lines.pm

package info (click to toggle)
libmail-message-perl 4.01-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,556 kB
  • sloc: perl: 10,588; makefile: 4
file content (181 lines) | stat: -rw-r--r-- 4,032 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# This code is part of Perl distribution Mail-Message version 4.01.
# The POD got stripped from this file by OODoc version 3.05.
# For contributors see file ChangeLog.

# This software is copyright (c) 2001-2025 by Mark Overmeer.

# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later


package Mail::Box::Parser::Lines;{
our $VERSION = '4.01';
}

use parent 'Mail::Box::Parser';

use strict;
use warnings;

use Log::Report   'mail-message', import => [ qw/__x panic warning/ ];

use Mail::Message::Field   ();

#--------------------

sub init(@)
{	my ($self, $args) = @_;
	$self->SUPER::init($args);

	$self->{MBPL_lines}  = $args->{lines}  or panic "No lines";
	$self->{MBPL_source} = $args->{source} or panic "No source";
	$self;
}

#--------------------

sub lines()  { $_[0]->{MBPL_lines} }
sub source() { $_[0]->{MBPL_source} }

#--------------------

my $is_empty_line = qr/^\015?\012?$/;

sub readHeader()
{	my $self  = shift;
	my $lines = $self->lines;
	my @ret;

  LINE:
	while(@$lines)
	{	my $line = shift @$lines;
		last if $line =~ $is_empty_line;

		my ($name, $body) = split /\s*\:\s*/, $line, 2;

		unless(defined $body)
		{	warning __x"unexpected end of header in {source}:\n {line}", source => $self->source, line => $line;

			if(@ret && $self->fixHeaderErrors)
			{	$ret[-1][1] .= ' '.$line;  # glue err line to previous field
				next LINE;
			}

			unshift @$lines, $line;
			last LINE;
		}

		# Collect folded lines
		$body .= shift @$lines
			while @$lines && $lines->[0] =~ m!^[ \t]!;

		push @ret, [ $name, $body ];
	}

	(undef, undef, @ret);
}

sub _is_good_end()
{	my $self  = shift;

	# No seps, then when have to trust it.
	my $sep = $self->activeSeparator // return 1;

	# Find first non-empty line on specified location.
	my $lines = $self->lines;
	my $skip  = 0;
	while($skip < @$lines && $lines->[$skip] =~ $is_empty_line) { $skip++ }
	$skip < @$lines or return 1;

	my $line = $lines->[$skip];

		substr($line, 0, length $sep) eq $sep
	&& ($sep ne 'From ' || $line =~ m/ (?:19[6-9]|20[0-3])[0-9]\b/ );
}

sub readSeparator()
{	my $self  = shift;
	my $sep   = $self->activeSeparator // return ();
	my $lines = $self->lines;

	my $skip  = 0;
	while($skip < @$lines && $lines->[$skip] =~ $is_empty_line) { $skip++ }

	$skip < @$lines
		or return ();

	my $line  = $lines->[$skip];
	substr($line, 0, length $sep) eq $sep
		or return ();

	splice @$lines, 0, $skip+1;
	(undef, $line);
}

sub _read_stripped_lines(;$$)
{	my ($self, $exp_chars, $exp_lines) = @_;
	my $seps    = $self->separators;
	my $lines   = $self->lines;
	my $take    = [];

	if(@$seps)
	{
	  LINE:
		while(1)
		{	my $line  = shift @$lines or last LINE;

			foreach my $sep (@$seps)
			{	substr($line, 0, length $sep) eq $sep or next;

				# Some apps fail to escape take starting with From
				next if $sep eq 'From ' && $line !~ m/ 19[789][0-9]| 20[0-9][0-9]/;

				unshift @$lines, $line;   # keep separator
				last LINE;
			}

			push @$take, $line;
		}

		if(@$take && $take->[-1] =~ s/\015?\012\z//)
		{	# Keep an empty line to signal the existence of a preamble, but
			# remove a second.
			pop @$take if @$seps==1 && @$take > 1 && length($take->[-1])==0;
		}
	}
	else # File without separators.
	{	$take = $lines;
	}

	if($self->stripGt)
	{	s/^\>(\>*From\s)/$1/ for @$take;
	}

	unless($self->trusted)
	{	s/\015// for @$take;
	}

	$take;
}

sub bodyAsString(;$$)
{	my ($self, $exp_chars, $exp_lines) = @_;
	my $take = $self->_read_stripped_lines($exp_chars, $exp_lines);
	return (undef, undef, join('', @$take));
}

sub bodyAsList(;$$)
{	my ($self, $exp_chars, $exp_lines) = @_;
	my $take = $self->_read_stripped_lines($exp_chars, $exp_lines);
	(undef, undef, $take);
}

sub bodyAsFile($;$$)
{	my ($self, $out, $exp_chars, $exp_lines) = @_;
	my $take = $self->_read_stripped_lines($exp_chars, $exp_lines);
	$out->print($_) for @$take;
	(undef, undef, scalar @$take);
}

1;