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;
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/;
11 sub squote_maybe ($) {
13 if ($val =~ m{([^\w@\./,\%\+\-])}) {
14 $val =~ s/(['!])/'\\$1'/g; # '!' for csh
21 my ($to, $cc, @addrs) = @_;
22 foreach my $address (@addrs) {
23 my $dst = lc($address);
24 $cc->{$dst} ||= $address;
29 my @reply_headers = qw(From To Cc Reply-To);
30 my $reply_headers = join('|', @reply_headers);
34 my $cc = {}; # everyone else
35 my $to; # this is the From address by default
37 foreach my $rt (split(/\s*,\s*/, $ibx->{replyto} || ':all')) {
39 foreach my $h (@reply_headers) {
40 my $v = $hdr->header($h);
41 defined($v) && ($v ne '') or next;
42 my @addrs = PublicInbox::Address::emails($v);
43 add_addrs(\$to, $cc, @addrs);
45 } elsif ($rt eq ':list') {
46 add_addrs(\$to, $cc, $ibx->{-primary_address});
47 } elsif ($rt =~ /\A(?:$reply_headers)\z/io) {
48 my $v = $hdr->header($rt);
49 if (defined($v) && ($v ne '')) {
50 my @addrs = PublicInbox::Address::emails($v);
51 add_addrs(\$to, $cc, @addrs);
53 } elsif ($rt =~ /@/) {
54 add_addrs(\$to, $cc, $rt);
56 warn "Unrecognized replyto = '$rt' in config\n";
61 my $subj = $hdr->header('Subject') || '';
62 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
63 my $mid = $hdr->header_raw('Message-ID');
64 push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
65 my $irt = mid_escape($mid);
67 push @arg, "--to=$to";
68 $to = uri_escape_utf8($to);
69 $subj = uri_escape_utf8($subj);
70 my @cc = sort values %$cc;
73 push(@arg, map { "--cc=$_" } @cc);
74 $cc = '&Cc=' . uri_escape_utf8(join(',', @cc));
77 # order matters, Subject is the least important header,
78 # so it is last in case it's lost/truncated in a copy+paste
79 my $href = "mailto:$to?In-Reply-To=$irt${cc}&Subject=$subj";
81 (\@arg, ascii_html($href));