]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Reply.pm
view: split out reply logic into its own module
[public-inbox.git] / lib / PublicInbox / Reply.pm
1 # Copyright (C) 2014-2017 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::Reply;
4 use strict;
5 use warnings;
6 use URI::Escape qw/uri_escape_utf8/;
7 use PublicInbox::Hval qw/ascii_html/;
8 use PublicInbox::Address;
9 use PublicInbox::MID qw/mid_clean mid_escape/;
10
11 sub squote_maybe ($) {
12         my ($val) = @_;
13         if ($val =~ m{([^\w@\./,\%\+\-])}) {
14                 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
15                 return "'$val'";
16         }
17         $val;
18 }
19
20 sub mailto_arg_link {
21         my ($hdr) = @_;
22         my %cc; # everyone else
23         my $to; # this is the From address
24
25         foreach my $h (qw(From To Cc)) {
26                 my $v = $hdr->header($h);
27                 defined($v) && ($v ne '') or next;
28                 my @addrs = PublicInbox::Address::emails($v);
29                 foreach my $address (@addrs) {
30                         my $dst = lc($address);
31                         $cc{$dst} ||= $address;
32                         $to ||= $dst;
33                 }
34         }
35         my @arg;
36
37         my $subj = $hdr->header('Subject') || '';
38         $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
39         my $mid = $hdr->header_raw('Message-ID');
40         push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
41         my $irt = mid_escape($mid);
42         delete $cc{$to};
43         push @arg, "--to=$to";
44         $to = uri_escape_utf8($to);
45         $subj = uri_escape_utf8($subj);
46         my @cc = sort values %cc;
47         push(@arg, map { "--cc=$_" } @cc);
48         my $cc = uri_escape_utf8(join(',', @cc));
49         my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
50
51         (\@arg, ascii_html($href));
52 }
53
54 1;