aboutsummaryrefslogtreecommitdiff
path: root/lib/Sietima/Role/Headers.pm
blob: 1c536d887da17f10d44fa48372ebc10fed1505c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package Sietima::Role::Headers; 
use Moo::Role;
use Sietima::Policy;
use Sietima::HeaderURI;
use Types::Standard qw(Str);
use Sietima::Types qw(HeaderUriFromThings);
use namespace::clean;
 
our $VERSION = '1.1.2'# VERSION 
# ABSTRACT: adds standard list-related headers to messages 
 
 
has name => (
    isa => Str,
    is => 'ro',
    required => 0,
);
 
sub _normalise_address($self,$address) {
    my @items = ref($addresseq 'ARRAY' ? $address->@* : $address;
 
    return map {
        HeaderUriFromThings->coerce($_)
    @items;
}
 
sub _set_header($self,$mail,$name,$value) {
    my $header_name = 'List-' . ucfirst($name =~ s{[^[:alnum:]]+}{-}gr);
    my @items = $self->_normalise_address($value);
 
    $mail->header_raw_set(
        $header_name => join ''map { $_->as_header_raw } @items,
    );
}
 
sub _add_headers_to($self,$message) {
    my $addresses = $self->list_addresses;
    my $mail = $message->mail;
 
    # see RFC 2919 "List-Id: A Structured Field and Namespace for the 
    # Identification of Mailing Lists" 
    my $return_path = delete $addresses->{return_path};
    if (my $name = $self->name) {
        $mail->header_raw_set(
            'List-Id',
            sprintf '%s <%s>'$name,$return_path->address =~ s{\@}{.}r,
        );
    }
 
    # if nobody declared a "post" address, let's guess it's the same 
    # as the address we send from 
    if (not exists $addresses->{post}) {
        $self->_set_header( $mailpost => $return_path );
    }
    # but if they explicitly set a false value, this list does not 
    # allow posting, so we need to set the special value 'NO' 
    elsif (not $addresses->{post}) {
        delete $addresses->{post};
        $mail->header_raw_set('List-Post','NO');
    }
    # otherwise we can treat 'post' as normal 
 
    for my $name (sort keys $addresses->%*) {
        $self->_set_header( $mail$name => $addresses->{$name} );
    }
    return;
}
 
 
around munge_mail => sub ($orig,$self,$mail) {
    my @messages = $self->$orig($mail);
    $self->_add_headers_to($_for @messages;
    return @messages;
};
 
1;
 
__END__
 
=pod
 
=encoding UTF-8
 
=head1 NAME
 
Sietima::Role::Headers - adds standard list-related headers to messages
 
=head1 VERSION
 
version 1.1.2
 
=head1 SYNOPSIS
 
  my $sietima = Sietima->with_traits('Headers')->new({
   %args,
   name => $name_of_the_list,
  });
 
=head1 DESCRIPTION
 
A L<< C<Sietima> >> list with this role applied will add, to each
outgoing message, the set of headers defined in RFC 2919 and RFC 2369.
 
This role uses the L<< C<list_addresses>|Sietima/list_addresses >>
method to determine what headers to add.
 
If the C<name> attribute is set, a C<List-Id:> header will be added,
with a value built out of the name and the C<<
$self->list_addresses->{return_path} >> value (which is normally the
same as the L<< C<return_path>|Sietima/return_path >> attribute).
 
Other C<List-*:> headers are built from the other values in the
C<list_addresses> hashref. Each of those values can be:
 
=over 4
 
=item *
 
an L<< C<Sietima::HeaderURI> >> object
 
=item *
 
a thing that can be passed to that class's constructor:
 
=over 4
 
=item *
 
an L<< C<Email::Address> >> object
 
=item *
 
a L<< C<URI> >> object
 
=item *
 
a string parseable as either
 
=back
 
=item *
 
an arrayref containing any mix of the above
 
=back
 
As a special case, if C<< $self->list_addresses->{post} >> exists and
is false, the C<List-Post> header will have the value C<NO> to
indicate that the list does not accept incoming messages (e.g. it's an
announcement list).
 
=head1 ATTRIBUTES
 
=head2 C<name>
 
Optional string, the name of the mailing list. If this attribute is
set, a C<List-Id:> header will be added, with a value built out of the
name and the C<< $self->list_addresses->{return_path} >> value (which
is normally the same as the L<< C<return_path>|Sietima/return_path >>
attribute).
 
=head1 MODIFIED METHODS
 
=head2 C<munge_mail>
 
This method adds list-management headers to each message returned by
the original method.
 
=head1 AUTHOR
 
Gianni Ceccarelli <dakkar@thenautilus.net>
 
=head1 COPYRIGHT AND LICENSE
 
This software is copyright (c) 2023 by Gianni Ceccarelli <dakkar@thenautilus.net>.
 
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
 
=cut