| 1 | package Mail::AuthenticationResults::Header::Base; | ||
| 2 | # ABSTRACT: Base class for modelling parts of the Authentication Results Header | ||
| 3 | |||
| 4 | require 5.008; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | our $VERSION = '2.20230112'; # VERSION | ||
| 8 | use Scalar::Util qw{ weaken refaddr }; | ||
| 9 | use JSON; | ||
| 10 | use Carp; | ||
| 11 | use Clone qw{ clone }; | ||
| 12 | |||
| 13 | use Mail::AuthenticationResults::Header::Group; | ||
| 14 | use Mail::AuthenticationResults::FoldableHeader; | ||
| 15 | |||
| 16 | |||
| 17 | sub _HAS_KEY{ return 0; } | ||
| 18 | sub _HAS_VALUE{ return 0; } | ||
| 19 | sub _HAS_CHILDREN{ return 0; } | ||
| 20 | sub _ALLOWED_CHILDREN{ # uncoverable subroutine | ||
| 21 | # does not run in Base as HAS_CHILDREN returns 0 | ||
| 22 | return 0; # uncoverable statement | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | sub new { | ||
| 27 | my ( $class ) = @_; | ||
| 28 | my $self = {}; | ||
| 29 | bless $self, $class; | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | sub set_key { | ||
| 35 | my ( $self, $key ) = @_; | ||
| 36 | croak 'Does not have key' if ! $self->_HAS_KEY(); | ||
| 37 | croak 'Key cannot be undefined' if ! defined $key; | ||
| 38 | croak 'Key cannot be empty' if $key eq q{}; | ||
| 39 | croak 'Invalid characters in key' if $key =~ /"/; | ||
| 40 | croak 'Invalid characters in key' if $key =~ /\n/; | ||
| 41 | croak 'Invalid characters in key' if $key =~ /\r/; | ||
| 42 | $self->{ 'key' } = $key; | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | sub key { | ||
| 48 | my ( $self ) = @_; | ||
| 49 | croak 'Does not have key' if ! $self->_HAS_KEY(); | ||
| 50 | return q{} if ! defined $self->{ 'key' }; #5.8 | ||
| 51 | return $self->{ 'key' }; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub safe_set_value { | ||
| 56 | my ( $self, $value ) = @_; | ||
| 57 | |||
| 58 | $value = q{} if ! defined $value; | ||
| 59 | |||
| 60 | $value =~ s/\t/ /g; | ||
| 61 | $value =~ s/\n/ /g; | ||
| 62 | $value =~ s/\r/ /g; | ||
| 63 | $value =~ s/\(/ /g; | ||
| 64 | $value =~ s/\)/ /g; | ||
| 65 | $value =~ s/\\/ /g; | ||
| 66 | $value =~ s/"/ /g; | ||
| 67 | $value =~ s/;/ /g; | ||
| 68 | $value =~ s/^\s+//; | ||
| 69 | $value =~ s/\s+$//; | ||
| 70 | |||
| 71 | #$value =~ s/ /_/g; | ||
| 72 | |||
| 73 | $self->set_value( $value ); | ||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub set_value { | ||
| 79 | my ( $self, $value ) = @_; | ||
| 80 | croak 'Does not have value' if ! $self->_HAS_VALUE(); | ||
| 81 | croak 'Value cannot be undefined' if ! defined $value; | ||
| 82 | #croak 'Value cannot be empty' if $value eq q{}; | ||
| 83 | croak 'Invalid characters in value' if $value =~ /"/; | ||
| 84 | croak 'Invalid characters in value' if $value =~ /\n/; | ||
| 85 | croak 'Invalid characters in value' if $value =~ /\r/; | ||
| 86 | $self->{ 'value' } = $value; | ||
| 87 | return $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | sub value { | ||
| 92 | my ( $self ) = @_; | ||
| 93 | croak 'Does not have value' if ! $self->_HAS_VALUE(); | ||
| 94 | return q{} if ! defined $self->{ 'value' }; # 5.8 | ||
| 95 | return $self->{ 'value' }; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | sub stringify { | ||
| 100 | my ( $self, $value ) = @_; | ||
| 101 | my $string = $value; | ||
| 102 | $string = q{} if ! defined $string; #5.8; | ||
| 103 | |||
| 104 | if ( $string =~ /[\s\t \(\);=]/ ) { | ||
| 105 | $string = '"' . $string . '"'; | ||
| 106 | } | ||
| 107 | |||
| 108 | return $string; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | sub children { | ||
| 113 | my ( $self ) = @_; | ||
| 114 | croak 'Does not have children' if ! $self->_HAS_CHILDREN(); | ||
| 115 | return [] if ! defined $self->{ 'children' }; #5.8 | ||
| 116 | return $self->{ 'children' }; | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub orphan { | ||
| 121 | my ( $self, $parent ) = @_; | ||
| 122 | croak 'Child does not have a parent' if ! exists $self->{ 'parent' }; | ||
| 123 | delete $self->{ 'parent' }; | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | sub copy_children_from { | ||
| 129 | my ( $self, $object ) = @_; | ||
| 130 | for my $original_entry (@{$object->children()}) { | ||
| 131 | my $entry = clone $original_entry; | ||
| 132 | $entry->orphan if exists $entry->{ 'parent' };; | ||
| 133 | $self->add_child( $entry ); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | sub add_parent { | ||
| 139 | my ( $self, $parent ) = @_; | ||
| 140 | return if ( ref $parent eq 'Mail::AuthenticationResults::Header::Group' ); | ||
| 141 | croak 'Child already has a parent' if exists $self->{ 'parent' }; | ||
| 142 | croak 'Cannot add parent' if ! $parent->_ALLOWED_CHILDREN( $self ); # uncoverable branch true | ||
| 143 | # Does not run as test is also done in add_child before add_parent is called. | ||
| 144 | $self->{ 'parent' } = $parent; | ||
| 145 | weaken $self->{ 'parent' }; | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | sub parent { | ||
| 151 | my ( $self ) = @_; | ||
| 152 | return $self->{ 'parent' }; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | sub remove_child { | ||
| 157 | my ( $self, $child ) = @_; | ||
| 158 | croak 'Does not have children' if ! $self->_HAS_CHILDREN(); | ||
| 159 | croak 'Cannot add child' if ! $self->_ALLOWED_CHILDREN( $child ); | ||
| 160 | croak 'Cannot add a class as its own parent' if refaddr $self == refaddr $child; # uncoverable branch true | ||
| 161 | # Does not run as there are no ALLOWED_CHILDREN results which permit this | ||
| 162 | |||
| 163 | my @children; | ||
| 164 | my $child_removed = 0; | ||
| 165 | foreach my $mychild ( @{ $self->{ 'children' } } ) { | ||
| 166 | if ( refaddr $child == refaddr $mychild ) { | ||
| 167 | if ( ref $self ne 'Mail::AuthenticationResults::Header::Group' ) { | ||
| 168 | $child->orphan(); | ||
| 169 | } | ||
| 170 | $child_removed = 1; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | push @children, $mychild; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | my $children = $self->{ 'children' }; | ||
| 177 | |||
| 178 | croak 'Not a child of this class' if ! $child_removed; | ||
| 179 | |||
| 180 | $self->{ 'children' } = \@children; | ||
| 181 | |||
| 182 | return $self; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | sub add_child { | ||
| 187 | my ( $self, $child ) = @_; | ||
| 188 | croak 'Does not have children' if ! $self->_HAS_CHILDREN(); | ||
| 189 | croak 'Cannot add child' if ! $self->_ALLOWED_CHILDREN( $child ); | ||
| 190 | croak 'Cannot add a class as its own parent' if refaddr $self == refaddr $child; # uncoverable branch true | ||
| 191 | # Does not run as there are no ALLOWED_CHILDREN results which permit this | ||
| 192 | |||
| 193 | $child->add_parent( $self ); | ||
| 194 | push @{ $self->{ 'children' } }, $child; | ||
| 195 | |||
| 196 | return $child; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | sub ancestor { | ||
| 201 | my ( $self ) = @_; | ||
| 202 | |||
| 203 | my $depth = 0; | ||
| 204 | my $ancestor = $self->parent(); | ||
| 205 | my $eldest = $self; | ||
| 206 | while ( defined $ancestor ) { | ||
| 207 | $eldest = $ancestor; | ||
| 208 | $ancestor = $ancestor->parent(); | ||
| 209 | $depth++; | ||
| 210 | } | ||
| 211 | |||
| 212 | return ( $eldest, $depth ); | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | sub as_string_prefix { | ||
| 217 | my ( $self, $header ) = @_; | ||
| 218 | |||
| 219 | my ( $eldest, $depth ) = $self->ancestor(); | ||
| 220 | |||
| 221 | my $indents = 1; | ||
| 222 | if ( $eldest->can( 'indent_by' ) ) { | ||
| 223 | $indents = $eldest->indent_by(); | ||
| 224 | } | ||
| 225 | |||
| 226 | my $eol = "\n"; | ||
| 227 | if ( $eldest->can( 'eol' ) ) { | ||
| 228 | $eol = $eldest->eol(); | ||
| 229 | } | ||
| 230 | |||
| 231 | my $indent = ' '; | ||
| 232 | my $added = 0; | ||
| 233 | if ( $eldest->can( 'indent_on' ) ) { | ||
| 234 | if ( $eldest->indent_on( ref $self ) ) { | ||
| 235 | $header->space( $eol ); | ||
| 236 | $header->space( ' ' x ( $indents * $depth ) ); | ||
| 237 | $added = 1; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | $header->space( ' ' ) if ! $added; | ||
| 241 | |||
| 242 | return $indent; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub _as_hashref { | ||
| 246 | my ( $self ) = @_; | ||
| 247 | |||
| 248 | my $type = lc ref $self; | ||
| 249 | $type =~ s/^(.*::)//; | ||
| 250 | my $hashref = { 'type' => $type }; | ||
| 251 | |||
| 252 | $hashref->{'key'} = $self->key() if $self->_HAS_KEY(); | ||
| 253 | $hashref->{'value'} = $self->value() if $self->_HAS_VALUE(); | ||
| 254 | if ( $self->_HAS_CHILDREN() ) { | ||
| 255 | my @children = map { $_->_as_hashref() } @{ $self->children() }; | ||
| 256 | $hashref->{'children'} = \@children; | ||
| 257 | } | ||
| 258 | return $hashref; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | sub as_json { | ||
| 263 | my ( $self ) = @_; | ||
| 264 | my $J = JSON->new(); | ||
| 265 | $J->canonical(); | ||
| 266 | return $J->encode( $self->_as_hashref() ); | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | sub as_string { | ||
| 271 | my ( $self ) = @_; | ||
| 272 | my $header = Mail::AuthenticationResults::FoldableHeader->new(); | ||
| 273 | $self->build_string( $header ); | ||
| 274 | return $header->as_string(); | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | sub build_string { | ||
| 279 | my ( $self, $header ) = @_; | ||
| 280 | |||
| 281 | if ( ! $self->key() ) { | ||
| 282 | return; | ||
| 283 | } | ||
| 284 | |||
| 285 | $header->string( $self->stringify( $self->key() ) ); | ||
| 286 | if ( $self->value() ) { | ||
| 287 | $header->assignment( '=' ); | ||
| 288 | $header->string( $self->stringify( $self->value() ) ); | ||
| 289 | } | ||
| 290 | elsif ( $self->value() eq '0' ) { | ||
| 291 | $header->assignment( '=' ); | ||
| 292 | $header->string( '0' ); | ||
| 293 | } | ||
| 294 | elsif ( $self->value() eq q{} ) { | ||
| 295 | # special case none here | ||
| 296 | if ( $self->key() ne 'none' ) { | ||
| 297 | $header->assignment( '=' ); | ||
| 298 | $header->string( '""' ); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | if ( $self->_HAS_CHILDREN() ) { # uncoverable branch false | ||
| 302 | # There are no classes which run this code without having children | ||
| 303 | foreach my $child ( @{$self->children()} ) { | ||
| 304 | $child->as_string_prefix( $header ); | ||
| 305 | $child->build_string( $header ); | ||
| 306 | } | ||
| 307 | } | ||
| 308 | return; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | sub search { | ||
| 313 | my ( $self, $search ) = @_; | ||
| 314 | |||
| 315 | my $group = Mail::AuthenticationResults::Header::Group->new(); | ||
| 316 | |||
| 317 | my $match = 1; | ||
| 318 | |||
| 319 | if ( exists( $search->{ 'key' } ) ) { | ||
| 320 | if ( $self->_HAS_KEY() ) { | ||
| 321 | if ( ref $search->{ 'key' } eq 'Regexp' && $self->key() =~ m/$search->{'key'}/ ) { | ||
| 322 | $match = $match && 1; # uncoverable statement | ||
| 323 | # $match is always 1 at this point, left this way for consistency | ||
| 324 | } | ||
| 325 | elsif ( lc $search->{ 'key' } eq lc $self->key() ) { | ||
| 326 | $match = $match && 1; # uncoverable statement | ||
| 327 | # $match is always 1 at this point, left this way for consistency | ||
| 328 | } | ||
| 329 | else { | ||
| 330 | $match = 0; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | $match = 0; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | if ( exists( $search->{ 'value' } ) ) { | ||
| 339 | $search->{ 'value' } = '' if ! defined $search->{ 'value' }; | ||
| 340 | if ( $self->_HAS_VALUE() ) { | ||
| 341 | if ( ref $search->{ 'value' } eq 'Regexp' && $self->value() =~ m/$search->{'value'}/ ) { | ||
| 342 | $match = $match && 1; | ||
| 343 | } | ||
| 344 | elsif ( lc $search->{ 'value' } eq lc $self->value() ) { | ||
| 345 | $match = $match && 1; | ||
| 346 | } | ||
| 347 | else { | ||
| 348 | $match = 0; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | else { | ||
| 352 | $match = 0; # uncoverable statement | ||
| 353 | # There are no code paths with the current classes which end up here | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | if ( exists( $search->{ 'authserv_id' } ) ) { | ||
| 358 | if ( $self->_HAS_VALUE() ) { | ||
| 359 | if ( lc ref $self eq 'mail::authenticationresults::header' ) { | ||
| 360 | my $authserv_id = eval{ $self->value()->value() } || q{}; | ||
| 361 | if ( ref $search->{ 'authserv_id' } eq 'Regexp' && $authserv_id =~ m/$search->{'authserv_id'}/ ) { | ||
| 362 | $match = $match && 1; | ||
| 363 | } | ||
| 364 | elsif ( lc $search->{ 'authserv_id' } eq lc $authserv_id ) { | ||
| 365 | $match = $match && 1; | ||
| 366 | } | ||
| 367 | else { | ||
| 368 | $match = 0; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | else { | ||
| 372 | $match = 0; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | else { | ||
| 376 | $match = 0; # uncoverable statement | ||
| 377 | # There are no code paths with the current classes which end up here | ||
| 378 | } | ||
| 379 | } | ||
| 380 | |||
| 381 | if ( exists( $search->{ 'isa' } ) ) { | ||
| 382 | if ( lc ref $self eq 'mail::authenticationresults::header::' . lc $search->{ 'isa' } ) { | ||
| 383 | $match = $match && 1; | ||
| 384 | } | ||
| 385 | elsif ( lc ref $self eq 'mail::authenticationresults::header' && lc $search->{ 'isa' } eq 'header' ) { | ||
| 386 | $match = $match && 1; | ||
| 387 | } | ||
| 388 | else { | ||
| 389 | $match = 0; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | |||
| 393 | if ( exists( $search->{ 'has' } ) ) { | ||
| 394 | foreach my $query ( @{ $search->{ 'has' } } ) { | ||
| 395 | $match = 0 if ( scalar @{ $self->search( $query )->children() } == 0 ); | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | if ( $match ) { | ||
| 400 | $group->add_child( $self ); | ||
| 401 | } | ||
| 402 | |||
| 403 | if ( $self->_HAS_CHILDREN() ) { | ||
| 404 | foreach my $child ( @{$self->children()} ) { | ||
| 405 | my $childfound = $child->search( $search ); | ||
| 406 | if ( scalar @{ $childfound->children() } ) { | ||
| 407 | $group->add_child( $childfound ); | ||
| 408 | } | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | return $group; | ||
| 413 | } | ||
| 414 | |||
| 415 | 1; | ||
| 416 | |||
| 417 | __END__ |