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 $obfs = $ibx->{obfuscate};
62 my $subj = $hdr->header('Subject') || '';
63 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
64 my $mid = $hdr->header_raw('Message-ID');
65 push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
66 my $irt = mid_escape($mid);
70 $arg_to =~ s/\./\$(echo .)/;
71 push @arg, "--to=$arg_to";
73 push @arg, "--to=$to";
74 $to = uri_escape_utf8($to);
75 $subj = uri_escape_utf8($subj);
77 my @cc = sort values %$cc;
86 $cc = '&Cc=' . uri_escape_utf8(join(',', @cc));
87 push(@arg, map { "--cc=$_" } @cc);
91 # I'm not sure if address obfuscation and mailto: links can
92 # be made compatible; and address obfuscation is misguided,
94 return (\@arg, '') if $obfs;
96 # order matters, Subject is the least important header,
97 # so it is last in case it's lost/truncated in a copy+paste
98 my $href = "mailto:$to?In-Reply-To=$irt${cc}&Subject=$subj";
100 (\@arg, ascii_html($href));