aboutsummaryrefslogtreecommitdiff
path: root/lib/Sietima/HeaderURI.pm
blob: 219672452e819aead7af43e09a9f24f97dda5b0f (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package Sietima::HeaderURI; 
use Moo;
use Sietima::Policy;
use Sietima::Types qw(Address AddressFromStr is_Address);
use Types::Standard qw(Str is_Str ClassName HashRef Optional);
use Type::Params -sigs;
use Types::URI qw(Uri is_Uri);
use Email::Address;
use namespace::clean;
 
# VERSION 
# ABSTRACT: annotated URI for list headers 
 
=head1 SYNOPSIS
 
  around list_addresses => sub($orig,$self) {
   return +{
    $self->$orig->%*,
    one => Sietima::HeaderURI->new({
      uri => 'http://foo/',
      comment => 'a thing',
    }),
    two => Sietima::HeaderURI->new_from_address(
     $self->owner,
     { subject => 'Hello' },
    ),
    three => Sietima::HeaderURI->new('http://some/url'),
    four => Sietima::HeaderURI->new('(comment) address@example.com'),
   };
  }
 
=head1 DESCRIPTION
 
This class pairs a L<< C<URI> >> with a comment, and knows how to
render itself as a string that can be used in a list management header
(see L<< C<Sietima::Role::Headers> >>).
 
=head1 ATTRIBUTES
 
All attributes are read-only.
 
=attr C<uri>
 
Required L<< C<URI> >> object, coercible from a string or a hashref
(see L<< C<Types::Uri> >> for the details). This is the URI that users
should follow to perform the action implied by the list management
header.
 
=cut
 
has uri => (
    is => 'ro',
    isa => Uri,
    required => 1,
    coerce => 1,
);
 
=attr C<comment>
 
Optional string, will be added to the list management header as a
comment (in parentheses).
 
=cut
 
has comment => (
    is => 'ro',
    isa => Str,
);
 
=method C<new>
 
 Sietima::HeaderURI->new({
   uri => 'http://foo/', comment => 'a thing',
 });
 
 Sietima::HeaderURI->new(
  Email::Address->parse('(comment) address@example.com'),
 );
 
 Sietima::HeaderURI->new( '(comment) address@example.com' );
 
 Sietima::HeaderURI->new(
  URI->new('http://some/url'),
 );
 
 Sietima::HeaderURI->new( 'http://some/url' );
 
Objects of this class can be constructed in several ways.
 
You can pass a hashref with URI (or something that L<< C<Types::Uri>
>> can coerce into a URI) and a comment string, as in the first
example.
 
Or you can pass a single value that can be (or can be coerced into)
either a L<< C<Email::Address> >> or a L<< C<URI> >>.
 
Email addresse became C<mailto:> URIs, and the optional comment is
preserved.
 
=for Pod::Coverage BUILDARGS
 
=cut
 
sub _args_from_address($address$query={}) {
    my $uri = URI->new($address->address,'mailto');
    $uri->query_form($query->%*);
 
    my $comment = $address->comment;
    # Email::Address::comment always returns a string in paretheses, 
    # but we don't want that, since we add them back in as_header_raw 
    $comment =~ s{\A\((.*)\)\z}{$1} if $comment;
 
    return {
        uri => $uri,
        comment => $comment,
    };
}
 
around BUILDARGS => sub($orig$class@args) {
    if (@args != 1 or ref($args[0]) eq 'HASH' and $args[0]->{uri}) {
        return $class->$orig(@args);
    }
 
    my $item = $args[0];
    if (is_Address($item)) {
        return _args_from_address($item);
    }
    elsif (is_Uri($item)) {
        return { uri => $item };
    }
    elsif (is_Str($itemand my $address = AddressFromStr->coerce($item)) {
        return _args_from_address($address);
    }
    else {
        return { uri => $item };
    };
};
 
=method C<new_from_address>
 
 Sietima::HeaderURI->new_from_address(
  $email_address,
  \%query,
 );
 
This constructor builds a complex C<mailto:> URI with the query hash
you provide. It's a shortcut for:
 
 my $uri = URI->new("mailto:$email_address");
 $uri->query_form(\%query);
 
Common query keys are C<subject> and C<body>. See RFC 6068 ("The
'mailto' URI Scheme") for details.
 
=cut
 
signature_for new_from_address => (
    method => Str,
    positional => [
        Address->plus_coercions(AddressFromStr),
        Optional[HashRef],
    ],
);
sub new_from_address($class$address$query={}) {
    return $class->new(_args_from_address($address,$query));
}
 
=method C<as_header_raw>
 
  $mail->header_raw_set('List-Thing' => $headeruri->as_header_raw);
 
This method returns a string representation of the L</URI> and
L</comment> in the format specified by RFC 2369 ("The Use of URLs as
Meta-Syntax for Core Mail List Commands and their Transport through
Message Header Fields").
 
For example:
 
 Sietima::HeaderURI->new({
   uri => 'http://foo/', comment => 'a thing',
 })->as_header_raw eq '<http://foo/> (a thing)';
 
 Sietima::HeaderURI->new( '(comment) address@example.com' )
 ->as_header_raw eq '<mailto:address@example.com> (comment)';
 
 Sietima::HeaderURI->new( 'http://some/url' )
 ->as_header_raw eq '<http://some/url>';
 
Notice that, since the list management headers are I<structured>, they
should always be set with L<<
C<header_raw_set>|Email::Simple::Header/header_raw_set >>.
 
=cut
 
sub as_header_raw($self) {
    my $str = sprintf '<%s>',$self->uri;
    if (my $c = $self->comment) {
        $str .= sprintf ' (%s)',$c;
    }
 
    return $str;
}
 
1;