Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a stringify method to the Policy class #253

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/Mail/DMARC/Policy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ sub parse {
return bless \%policy, ref $self; # inherited defaults + overrides
}

sub stringify {
my ( $self ) = @_;

my %dmarc_record = %{$self};
# "v" tag must be the first one
my $dmarc_txt = 'v=' . delete $dmarc_record{v} . '; ';
msimerson marked this conversation as resolved.
Show resolved Hide resolved
foreach my $key ( keys %dmarc_record ) {
next if $key eq 'domain';
$dmarc_txt .= "$key=$dmarc_record{$key}; ";
}
$dmarc_txt =~ s/;\s$//;
return $dmarc_txt;
}

sub apply_defaults {
my $self = shift;

Expand Down Expand Up @@ -269,6 +283,13 @@ via DNS.
$pol->parse( 'v=DMARC1; p=none; rua=mailto:dmarc@example.com' );
$pol->parse( 'v=DMARC1' ); # external reporting record

=head2 stringify

Returns the textual representation of the DMARC record.

my $pol = Mail::DMARC::Policy->new('v=DMARC1; p=none;');
print $pol->stringify;

=head1 Record Tags

=head2 Tag Overview
Expand Down
6 changes: 6 additions & 0 deletions t/01.Policy.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ stderr_is { test_parse() } $expected_parse_warning, 'STDERR yields parse warning
test_setter_values();
test_apply_defaults();
test_is_valid();
test_stringify();
handles_common_record_errors();

done_testing();
Expand Down Expand Up @@ -259,6 +260,11 @@ sub test_is_valid {
ok( $pol && $pol->is_valid, "is_valid, pos, implicit p=none w/rua" );
}

sub test_stringify {
$pol = Mail::DMARC::Policy->new( 'v=DMARC1; p=reject' );
ok($pol->stringify, 'v=DMARC1; p=reject');
}

sub handles_common_record_errors {

foreach my $d (<DATA>) {
Expand Down
Loading