1 # Copyright (C) 2014-2018 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 obfuscate_addrs/;
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 defaultq
36 my $reply_to_all = 'reply-to-all'; # the only good default :P
38 foreach my $rt (split(/\s*,\s*/, $ibx->{replyto} || ':all')) {
40 foreach my $h (@reply_headers) {
41 my $v = $hdr->header($h);
42 defined($v) && ($v ne '') or next;
43 my @addrs = PublicInbox::Address::emails($v);
44 add_addrs(\$to, $cc, @addrs);
46 } elsif ($rt eq ':list') {
47 $reply_to_all = 'reply-to-list';
48 add_addrs(\$to, $cc, $ibx->{-primary_address});
49 } elsif ($rt =~ /\A(?:$reply_headers)\z/io) {
50 # ugh, this is weird...
51 my $v = $hdr->header($rt);
52 if (defined($v) && ($v ne '')) {
53 my @addrs = PublicInbox::Address::emails($v);
54 add_addrs(\$to, $cc, @addrs);
56 } elsif ($rt =~ /@/) {
57 add_addrs(\$to, $cc, $rt);
59 warn "Unrecognized replyto = '$rt' in config\n";
64 my $obfs = $ibx->{obfuscate};
65 my $subj = $hdr->header('Subject') || '';
66 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
67 my $mid = $hdr->header_raw('Message-ID');
68 push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
69 my $irt = mid_escape($mid);
73 obfuscate_addrs($ibx, $arg_to, '$(echo .)');
74 push @arg, "--to=$arg_to";
75 # no $subj for $href below
77 push @arg, "--to=$to";
78 $to = uri_escape_utf8($to);
79 $subj = uri_escape_utf8($subj);
81 my @cc = sort values %$cc;
87 obfuscate_addrs($ibx, $addr, '$(echo .)');
91 $cc = '&Cc=' . uri_escape_utf8(join(',', @cc));
92 push(@arg, map { "--cc=$_" } @cc);
96 # I'm not sure if address obfuscation and mailto: links can
97 # be made compatible; and address obfuscation is misguided,
99 return (\@arg, '', $reply_to_all) if $obfs;
101 # order matters, Subject is the least important header,
102 # so it is last in case it's lost/truncated in a copy+paste
103 my $href = "mailto:$to?In-Reply-To=$irt${cc}&Subject=$subj";
105 (\@arg, ascii_html($href), $reply_to_all);