Skip to content

Commit

Permalink
add a stringify method to the Policy class
Browse files Browse the repository at this point in the history
issue #243
  • Loading branch information
bigio committed Dec 10, 2024
1 parent ad52c4e commit c18ca48
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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=' . $dmarc_record{v} . '; ';
foreach my $key ( keys %dmarc_record ) {
next if ($key =~ /^domain|v$/);
$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

0 comments on commit c18ca48

Please sign in to comment.