auto
     eˆÄŽþ_•g®–Ÿt�ùß159ÞÁÛ  Ÿ    e    <   e   o   p   s   w   |   €   �   ‰   Œ   �   ”   %f%N%a%N%d%N%b%N%s %h %e %r%N%z %T%N%c%N Australia  AU AUS AUS  $    English en eng eng ISO-8859-1       gˆË�þE(åY¹DŠIDàÀçß„„ÞÊÁÛ /¯    gpackage Mail::AuthenticationResults::Parser;
# ABSTRACT: Class for parsing Authentication Results Headers

require 5.008;
use strict;
use warnings;
our $VERSION = '2.20230112'; # VERSION
use Carp;

use Mail::AuthenticationResults::Header;
use Mail::AuthenticationResults::Header::AuthServID;
use Mail::AuthenticationResults::Header::Comment;
use Mail::AuthenticationResults::Header::Entry;
use Mail::AuthenticationResults::Header::SubEntry;
use Mail::AuthenticationResults::Header::Version;

use Mail::AuthenticationResults::Token::Assignment;
use Mail::AuthenticationResults::Token::Comment;
use Mail::AuthenticationResults::Token::QuotedString;
use Mail::AuthenticationResults::Token::Separator;
use Mail::AuthenticationResults::Token::String;

use JSON;


sub new {
    my ( $class, $auth_header ) = @_;
    my $self = {};
    bless $self, $class;

    if ( $auth_header ) {
        $self->parse( $auth_header );
    }

    return $self;
}


sub parse {
    my ( $self, $header ) = @_;

    $self->tokenise( $header );

    $self->_parse_authservid();

    while ( @{ $self->{ 'tokenised' } } ) {
        $self->_parse_entry();
    }

    return $self->parsed();
}


sub from_authentication_results_json {
    my ( $self, $json ) = @_;
    my $j = JSON->new();
    my $hashref = $j->decode( $json );
    return $self->_from_hashref( $hashref );
}

sub _from_hashref {
    my ( $self, $hashref ) = @_;
    my $type = $hashref->{'type'};
    my $object
        = $type eq 'header'     ? Mail::AuthenticationResults::Header->new()
        : $type eq 'authservid' ? Mail::AuthenticationResults::Header::AuthServID->new()
        : $type eq 'entry'      ? Mail::AuthenticationResults::Header::Entry->new()
        : $type eq 'subentry'   ? Mail::AuthenticationResults::Header::SubEntry->new()
        : $type eq 'comment'    ? Mail::AuthenticationResults::Header::Comment->new()
        : croak "unknown type $type";

    if ( $type eq 'header' ) {
        my $authserv_id = $self->_from_hashref( $hashref->{ 'authserv_id' } );
        $object->set_value( $authserv_id );
    }
    else {
        $object->set_key( $hashref->{'key'} ) if exists $hashref->{'key'};
        $object->safe_set_value( $hashref->{'value'} ) if exists $hashref->{'value'};
    }

    if ( exists $hashref->{'children'} ) {
        for my $child ( @{ $hashref->{'children'} } ) {
            my $child_object = $self->_from_hashref( $child );
            $object->add_child( $child_object );
        }
    }

    return $object;
}



sub tokenise {
    my ( $self, $header ) = @_;

    my @tokenised;

    $header =~ s/\n/ /g;
    $header =~ s/\r/ /g;
    $header =~ s/^\s+//;

    # Remove Header part if present
    if ( $header =~ /^Authentication-Results:/i ) {
        $header =~ s/^Authentication-Results://i;
    }

    my $args = {};
    while ( length($header) > 0 ) {

        my $token;
        $header =~ s/^\s+//;

        my $last_non_comment_type = exists( $args->{ 'last_non_comment_type' } ) ? $args->{ 'last_non_comment_type' }->is() : 'none';

        if ( length( $header ) == 0 ) {
            last;
        }
        elsif ( $header =~ /^\(/ ) {
            $token = Mail::AuthenticationResults::Token::Comment->new( $header, $args );
        }
        elsif ( $header =~ /^;/ ) {
            $token = Mail::AuthenticationResults::Token::Separator->new( $header, $args );
            $args->{ 'last_non_comment_type' } = $token;
        }
        elsif ( $header =~ /^"/ ) {
            $token = Mail::AuthenticationResults::Token::QuotedString->new( $header, $args );
            $args->{ 'last_non_comment_type' } = $token;
        }
        elsif ( $last_non_comment_type ne 'assignment' && $header =~ /^\./ ) {
            $token = Mail::AuthenticationResults::Token::Assignment->new( $header, $args );
            $args->{ 'last_non_c