File Coverage

blib/lib/Mail/Message/Field/Structured.pm
Criterion Covered Total %
statement 80 86 93.0
branch 20 24 83.3
condition 14 16 87.5
subroutine 14 15 93.3
pod 8 9 88.8
total 136 150 90.6


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::Field::Structured;{
13             our $VERSION = '4.04';
14             }
15              
16 28     28   18099 use parent 'Mail::Message::Field::Full';
  28         88  
  28         231  
17              
18 28     28   2891 use strict;
  28         68  
  28         1035  
19 28     28   143 use warnings;
  28         172  
  28         2023  
20              
21 28     28   240 use Log::Report 'mail-message', import => [ qw// ];
  28         79  
  28         313  
22              
23 28     28   21961 use Mail::Message::Field::Attribute ();
  28         6022  
  28         1305  
24 28     28   208 use Storable qw/dclone/;
  28         108  
  28         42492  
25              
26             #--------------------
27              
28             sub init($)
29 158     158 0 522 { my ($self, $args) = @_;
30 158         2336 $self->{MMFS_attrs} = {};
31 158         508 $self->{MMFS_datum} = $args->{datum};
32              
33 158         793 $self->SUPER::init($args);
34              
35 158   50     903 my $attr = $args->{attributes} || [];
36 158 50       616 $attr = [ %$attr ] if ref $attr eq 'HASH';
37              
38 158         507 while(@$attr)
39 0         0 { my $name = shift @$attr;
40 0 0       0 if(ref $name) { $self->attribute($name) }
  0         0  
41 0         0 else { $self->attribute($name, shift @$attr) }
42             }
43              
44 158         1376 $self;
45             }
46              
47 195     195 1 17124 sub clone() { dclone(shift) }
48              
49             #--------------------
50              
51             sub attribute($;$)
52 320     320 1 3286 { my ($self, $attr) = (shift, shift);
53 320         558 my $name;
54 320 100       1002 if(ref $attr) { $name = $attr->name }
  77 100       266  
55 200         1090 elsif( !@_ ) { return $self->{MMFS_attrs}{lc $attr} }
56             else
57 43         87 { $name = $attr;
58 43         191 $attr = Mail::Message::Field::Attribute->new($name, @_);
59             }
60              
61 120         275 delete $self->{MMFF_body};
62 120         507 $self->{MMFS_attrs}{lc $name} = $attr;
63             }
64              
65              
66 2     2 1 7 sub attributes() { values %{$_[0]->{MMFS_attrs}} }
  2         18  
67 7     7 1 35 sub beautify() { delete $_[0]->{MMFF_body} }
68              
69              
70 0     0 1 0 sub attrPairs() { map +($_->name, $_->value), $_[0]->attributes }
71              
72             #--------------------
73              
74             sub parse($)
75 93     93 1 256 { my ($self, $string) = @_;
76              
77 93         269 for($string)
78             { # remove FWS, even within quoted strings
79 93         346 s/\r?\n(\s)/$1/gs;
80 93         711 s/\r?\n/ /gs;
81 93         651 s/\s+$//;
82             }
83              
84 93         205 my $datum = '';
85 93   100     636 while(length $string && substr($string, 0, 1) ne ';')
86 105         477 { (undef, $string) = $self->consumeComment($string);
87 105 100       1060 $datum .= $1 if $string =~ s/^([^;(]+)//;
88             }
89 93         284 $self->{MMFS_datum} = $datum;
90              
91 93         200 my $found = '';
92 93         394 while($string =~ m/\S/)
93 157         276 { my $len = length $string;
94              
95 157 100 100     799 if($string =~ s/^\s*\;\s*// && length $found)
96 5         17 { my ($name) = $found =~ m/^([^*]+)\*/;
97 5 100 100     30 if($name && (my $cont = $self->attribute($name)))
98 1         4 { $cont->addComponent($found); # continuation
99             }
100             else
101 4         19 { my $attr = Mail::Message::Field::Attribute->new($found);
102 4         15 $self->attribute($attr);
103             }
104 5         13 $found = '';
105             }
106              
107 157         621 (undef, $string) = $self->consumeComment($string);
108 157         326 $string =~ s/^\n//;
109 157         463 (my $text, $string) = $self->consumePhrase($string);
110 157 100       528 $found .= $text if defined $text;
111              
112 157 50       613 if(length($string) == $len)
113             { # nothing consumed, remove character to avoid endless loop
114 0         0 $string =~ s/^\s*\S//;
115             }
116             }
117              
118 93 100       297 if(length $found)
119 75         237 { my ($name) = $found =~ m/^([^*]+)\*/;
120 75 100 66     274 if($name && (my $cont = $self->attribute($name)))
121 2         5 { $cont->addComponent($found); # continuation
122             }
123             else
124 73         480 { my $attr = Mail::Message::Field::Attribute->new($found);
125 73         272 $self->attribute($attr);
126             }
127             }
128              
129 93         297 1;
130             }
131              
132             sub produceBody()
133 49     49 1 711 { my $self = shift;
134 49         119 my $attrs = $self->{MMFS_attrs};
135 49         116 my $datum = $self->{MMFS_datum};
136              
137             join '; ', ($datum // ''),
138 49   100     314 map $_->string, @{$attrs}{sort keys %$attrs};
  49         293  
139             }
140              
141              
142             sub datum(@)
143 2     2 1 533 { my $self = shift;
144 2 100       17 @_ or return $self->{MMFS_datum};
145 1         4 delete $self->{MMFF_body};
146 1         9 $self->{MMFS_datum} = shift;
147             }
148              
149             1;