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             # Copyrights 2001-2022 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.03.
5             # This code is part of distribution Mail-Message. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Mail::Message::Field::AuthResults;
10 21     21   698 use vars '$VERSION';
  21         43  
  21         1098  
11             $VERSION = '3.012';
12              
13 21     21   112 use base 'Mail::Message::Field::Structured';
  21         55  
  21         2473  
14              
15 21     21   125 use warnings;
  21         46  
  21         687  
16 21     21   102 use strict;
  21         42  
  21         520  
17              
18 21     21   10282 use URI;
  21         81007  
  21         36027  
19              
20              
21              
22             sub init($)
23 9     9 0 16 { my ($self, $args) = @_;
24 9         65 $self->{MMFA_server} = delete $args->{server};
25 9         15 $self->{MMFA_version} = delete $args->{version};
26              
27 9         18 $self->{MMFA_results} = [];
28 9 50       11 $self->addResult($_) for @{delete $args->{results} || []};
  9         37  
29              
30 9         29 $self->SUPER::init($args);
31             }
32              
33             sub parse($)
34 8     8 1 13 { my ($self, $string) = @_;
35 8         58 $string =~ s/\r?\n/ /g;
36              
37 8         23 (undef, $string) = $self->consumeComment($string);
38 8 100       42 $self->{MMFA_server} = $string =~ s/^\s*([.\w-]*\w)// ? $1 : 'unknown';
39              
40 8         18 (undef, $string) = $self->consumeComment($string);
41 8 100       28 $self->{MMFA_version} = $string =~ s/^\s*([0-9]+)// ? $1 : 1;
42              
43 8         15 (undef, $string) = $self->consumeComment($string);
44 8         26 $string =~ s/^.*?\;/;/; # remove accidents
45              
46 8         13 my @results;
47 8         24 while( $string =~ s/^\s*\;// )
48             {
49 12         29 (undef, $string) = $self->consumeComment($string);
50 12 100       33 if($string =~ s/^\s*none//)
51 3         7 { (undef, $string) = $self->consumeComment($string);
52 3         7 next;
53             }
54              
55 9         17 my %result;
56 9         16 push @results, \%result;
57              
58 9 50       33 $string =~ s/^\s*([\w-]*\w)// or next;
59 9         21 $result{method} = $1;
60              
61 9         19 (undef, $string) = $self->consumeComment($string);
62 9 100       22 if($string =~ s!^\s*/!!)
63 1         3 { (undef, $string) = $self->consumeComment($string);
64 1 50       7 $result{method_version} = $1 if $string =~ s/^\s*([0-9]+)//;
65             }
66              
67 9         17 (undef, $string) = $self->consumeComment($string);
68 9 50       31 if($string =~ s/^\s*\=//)
69 9         19 { (undef, $string) = $self->consumeComment($string);
70 9 50       34 $result{result} = $1
71             if $string =~ s/^\s*(\w+)//;
72             }
73              
74 9         21 (my $comment, $string) = $self->consumeComment($string);
75 9 100       20 if($comment)
76 3         8 { $result{comment} = $comment;
77 3         5 (undef, $string) = $self->consumeComment($string);
78             }
79              
80 9 100       23 if($string =~ s/\s*reason//)
81 2         5 { (undef, $string) = $self->consumeComment($string);
82 2 50       8 if($string =~ s/\s*\=//)
83 2         5 { (undef, $string) = $self->consumeComment($string);
84 2 0 33     13 $result{reason} = $1
      33        
85             if $string =~ s/^\"([^"]*)\"//
86             || $string =~ s/^\'([^']*)\'//
87             || $string =~ s/^(\w+)//;
88             }
89             }
90              
91 9         27 while($string =~ /\S/)
92 13         25 { (undef, $string) = $self->consumeComment($string);
93 13 100       42 last if $string =~ /^\s*\;/;
94              
95 9 100       33 my $ptype = $string =~ s/^\s*([\w-]+)// ? $1 : last;
96 8         18 (undef, $string) = $self->consumeComment($string);
97              
98 8         11 my ($property, $value);
99 8 50       31 if($string =~ s/^\s*\.//)
100 8         16 { (undef, $string) = $self->consumeComment($string);
101 8 50       28 $property = $string =~ s/^\s*([\w-]+)// ? $1 : last;
102 8         46 (undef, $string) = $self->consumeComment($string);
103 8 50       29 if($string =~ s/^\s*\=//)
104 8         16 { (undef, $string) = $self->consumeComment($string);
105 8         18 $string =~ s/^\s+//;
106 8 50 33     59 $string =~ s/^\"([^"]*)\"//
      33        
107             || $string =~ s/^\'([^']*)\'//
108             || $string =~ s/^([\w@.-]+)//
109             or last;
110              
111 8         19 $value = $1;
112             }
113             }
114              
115 8 50       12 if(defined $value)
116 8         33 { $result{"$ptype.$property"} = $value;
117             }
118             else
119 0         0 { $string =~ s/^.*?\;/;/g; # recover from parser problem
120             }
121             }
122             }
123 8         19 $self->addResult($_) for @results;
124              
125 8         13 $self;
126             }
127              
128             sub produceBody()
129 3     3 1 5 { my $self = shift;
130 3         6 my $source = $self->server;
131 3         7 my $version = $self->version;
132 3 50       7 $source .= " $version" if $version!=1;
133              
134 3         4 my @results;
135 3         6 foreach my $r ($self->results)
136 3         4 { my $method = $r->{method};
137             $method .= "/$r->{method_version}"
138 3 100       9 if $r->{method_version} != 1;
139              
140 3         6 my $result = "$method=$r->{result}";
141              
142             $result .= ' ' . $self->createComment($r->{comment})
143 3 100       12 if defined $r->{comment};
144              
145 3 50       9 if(my $reason = $r->{reason})
146 0         0 { $reason =~ s/"/\\"/g;
147 0         0 $result .= qq{ reason="$reason"};
148             }
149              
150 3         12 foreach my $prop (sort keys %$r)
151 12 100       23 { index($prop, '.') > -1 or next;
152 2         4 my $value = $r->{$prop};
153 2         3 $value =~ s/"/\\"/g;
154 2         5 $result .= qq{ $prop="$value"};
155             }
156              
157 3         6 push @results, $result;
158             }
159              
160 3 100       8 push @results, 'none' unless @results;
161 3         17 join '; ', $source, @results;
162             }
163              
164             #------------------------------------------
165              
166              
167              
168             sub addAttribute($;@)
169 0     0 1 0 { my $self = shift;
170 0         0 $self->log(ERROR => 'No attributes for Authentication-Results.');
171 0         0 $self;
172             }
173              
174              
175              
176 11     11 1 2148 sub server() { shift->{MMFA_server} }
177 10     10 1 38 sub version() { shift->{MMFA_version} }
178              
179              
180 10     10 1 13 sub results() { @{shift->{MMFA_results}} }
  10         33  
181              
182              
183             sub addResult($)
184 11     11 1 30 { my $self = shift;
185              
186 11 100       29 my $r = @_==1 ? shift : {@_};
187 11 50 33     39 $r->{method} && $r->{result} or return ();
188 11   100     35 $r->{method_version} ||= 1;
189 11         13 push @{$self->{MMFA_results}}, $r;
  11         26  
190 11         14 delete $self->{MMFF_body};
191              
192 11         19 $r;
193             }
194              
195             #------------------------------------------
196              
197              
198             1;