File Coverage

blib/lib/Mail/Message/Field/AuthResults.pm
Criterion Covered Total %
statement 107 113 94.6
branch 40 56 71.4
condition 7 17 41.1
subroutine 12 13 92.3
pod 7 8 87.5
total 173 207 83.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::Field::AuthResults;{
13             our $VERSION = '4.04';
14             }
15              
16 28     28   1613 use parent 'Mail::Message::Field::Structured';
  28         78  
  28         221  
17              
18 28     28   2713 use warnings;
  28         68  
  28         1812  
19 28     28   170 use strict;
  28         63  
  28         1160  
20              
21 28     28   160 use Log::Report 'mail-message', import => [ qw/__x error/ ];
  28         54  
  28         250  
22              
23 28     28   24603 use URI;
  28         215207  
  28         95696  
24              
25             #--------------------
26              
27             #--------------------
28              
29             sub init($)
30 9     9 0 27 { my ($self, $args) = @_;
31 9         114 $self->{MMFA_server} = delete $args->{server};
32 9         26 $self->{MMFA_version} = delete $args->{version};
33              
34 9         28 $self->{MMFA_results} = [];
35 9 50       17 $self->addResult($_) for @{delete $args->{results} || []};
  9         62  
36              
37 9         54 $self->SUPER::init($args);
38             }
39              
40             sub parse($)
41 8     8 1 22 { my ($self, $string) = @_;
42 8         80 $string =~ s/\r?\n/ /g;
43              
44 8         44 (undef, $string) = $self->consumeComment($string);
45 8 100       62 $self->{MMFA_server} = $string =~ s/^\s*([.\w-]*\w)// ? $1 : 'unknown';
46              
47 8         22 (undef, $string) = $self->consumeComment($string);
48 8 100       41 $self->{MMFA_version} = $string =~ s/^\s*([0-9]+)// ? $1 : 1;
49              
50 8         19 (undef, $string) = $self->consumeComment($string);
51 8         44 $string =~ s/^.*?\;/;/; # remove accidents
52              
53 8         13 my @results;
54 8         47 while( $string =~ s/^\s*\;// )
55             {
56 12         27 (undef, $string) = $self->consumeComment($string);
57 12 100       42 if($string =~ s/^\s*none//)
58 3         10 { (undef, $string) = $self->consumeComment($string);
59 3         10 next;
60             }
61              
62 9         13 my %result;
63 9         21 push @results, \%result;
64              
65 9 50       44 $string =~ s/^\s*([\w-]*\w)// or next;
66 9         28 $result{method} = $1;
67              
68 9         23 (undef, $string) = $self->consumeComment($string);
69 9 100       26 if($string =~ s!^\s*/!!)
70 1         5 { (undef, $string) = $self->consumeComment($string);
71 1 50       10 $result{method_version} = $1 if $string =~ s/^\s*([0-9]+)//;
72             }
73              
74 9         19 (undef, $string) = $self->consumeComment($string);
75 9 50       36 if($string =~ s/^\s*\=//)
76 9         21 { (undef, $string) = $self->consumeComment($string);
77 9 50       47 $result{result} = $1
78             if $string =~ s/^\s*(\w+)//;
79             }
80              
81 9         22 (my $comment, $string) = $self->consumeComment($string);
82 9 100       36 if($comment)
83 3         9 { $result{comment} = $comment;
84 3         13 (undef, $string) = $self->consumeComment($string);
85             }
86              
87 9 100       40 if($string =~ s/\s*reason//)
88 2         6 { (undef, $string) = $self->consumeComment($string);
89 2 50       11 if($string =~ s/\s*\=//)
90 2         4 { (undef, $string) = $self->consumeComment($string);
91 2 0 33     29 $result{reason} = $1
      33        
92             if $string =~ s/^\"([^"]*)\"//
93             || $string =~ s/^\'([^']*)\'//
94             || $string =~ s/^(\w+)//;
95             }
96             }
97              
98 9         108 while($string =~ /\S/)
99 13         40 { (undef, $string) = $self->consumeComment($string);
100 13 100       53 last if $string =~ /^\s*\;/;
101              
102 9 100       109 my $ptype = $string =~ s/^\s*([\w-]+)// ? $1 : last;
103 8         30 (undef, $string) = $self->consumeComment($string);
104              
105 8         20 my ($property, $value);
106 8 50       42 if($string =~ s/^\s*\.//)
107 8         20 { (undef, $string) = $self->consumeComment($string);
108 8 50       69 $property = $string =~ s/^\s*([\w-]+)// ? $1 : last;
109 8         25 (undef, $string) = $self->consumeComment($string);
110 8 50       33 if($string =~ s/^\s*\=//)
111 8         16 { (undef, $string) = $self->consumeComment($string);
112 8         17 $string =~ s/^\s+//;
113 8 50 33     65 $string =~ s/^\"([^"]*)\"// || $string =~ s/^\'([^']*)\'// || $string =~ s/^([\w@.-]+)//
      33        
114             or last;
115              
116 8         21 $value = $1;
117             }
118             }
119              
120 8 50       18 if(defined $value)
121 8         50 { $result{"$ptype.$property"} = $value;
122             }
123             else
124 0         0 { $string =~ s/^.*?\;/;/g; # recover from parser problem
125             }
126             }
127             }
128 8         38 $self->addResult($_) for @results;
129              
130 8         18 $self;
131             }
132              
133             sub produceBody()
134 3     3 1 4 { my $self = shift;
135 3         9 my $source = $self->server;
136 3         6 my $version = $self->version;
137 3 50       9 $source .= " $version" if $version!=1;
138              
139 3         4 my @results;
140 3         7 foreach my $r ($self->results)
141 3         5 { my $method = $r->{method};
142             $method .= "/$r->{method_version}"
143 3 100       8 if $r->{method_version} != 1;
144              
145 3         5 my $result = "$method=$r->{result}";
146              
147             $result .= ' ' . $self->createComment($r->{comment})
148 3 100       17 if defined $r->{comment};
149              
150 3 50       8 if(my $reason = $r->{reason})
151 0         0 { $reason =~ s/"/\\"/g;
152 0         0 $result .= qq{ reason="$reason"};
153             }
154              
155 3         17 foreach my $prop (sort keys %$r)
156 12 100       21 { index($prop, '.') > -1 or next;
157 2         4 my $value = $r->{$prop};
158 2         4 $value =~ s/"/\\"/g;
159 2         4 $result .= qq{ $prop="$value"};
160             }
161              
162 3         4 push @results, $result;
163             }
164              
165 3 100       9 push @results, 'none' unless @results;
166 3         18 join '; ', $source, @results;
167             }
168              
169             #--------------------
170              
171             sub addAttribute($;@)
172 0     0 1 0 { my $self = shift;
173 0         0 error __x"no attributes for Authentication-Results.";
174 0         0 $self;
175             }
176              
177              
178 11     11 1 2847 sub server() { $_[0]->{MMFA_server} }
179 10     10 1 54 sub version() { $_[0]->{MMFA_version} }
180              
181              
182 10     10 1 21 sub results() { @{ $_[0]->{MMFA_results}} }
  10         42  
183              
184              
185             sub addResult($)
186 11     11 1 19 { my $self = shift;
187              
188 11 100       60 my $r = @_==1 ? shift : {@_};
189 11 50 33     73 $r->{method} && $r->{result} or return ();
190 11   100     46 $r->{method_version} ||= 1;
191 11         15 push @{$self->{MMFA_results}}, $r;
  11         27  
192 11         38 delete $self->{MMFF_body};
193              
194 11         23 $r;
195             }
196              
197             #--------------------
198              
199             1;