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
37 my $reply_to_cfg = $ibx->{replyto};
39 $reply_to_cfg ||= ':all';
40 if ($reply_to_cfg =~ /\A:none=(.*)/) {
42 $msg = 'replies disabled' if $msg eq '';
46 foreach my $rt (split(/\s*,\s*/, $reply_to_cfg)) {
48 foreach my $h (@reply_headers) {
49 my $v = $hdr->header($h);
50 defined($v) && ($v ne '') or next;
51 my @addrs = PublicInbox::Address::emails($v);
52 add_addrs(\$to, $cc, @addrs);
54 } elsif ($rt eq ':list') {
55 $reply_to_all = 'reply-to-list';
56 add_addrs(\$to, $cc, $ibx->{-primary_address});
57 } elsif ($rt =~ /\A(?:$reply_headers)\z/io) {
58 # ugh, this is weird...
59 my $v = $hdr->header($rt);
60 if (defined($v) && ($v ne '')) {
61 my @addrs = PublicInbox::Address::emails($v);
62 add_addrs(\$to, $cc, @addrs);
64 } elsif ($rt =~ /@/) {
65 add_addrs(\$to, $cc, $rt);
67 warn "Unrecognized replyto = '$rt' in config\n";
72 my $obfs = $ibx->{obfuscate};
73 my $subj = $hdr->header('Subject') || '';
74 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
75 my $mid = $hdr->header_raw('Message-ID');
76 push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
77 my $irt = mid_escape($mid);
81 obfuscate_addrs($ibx, $arg_to, '$(echo .)');
82 push @arg, "--to=$arg_to";
83 # no $subj for $href below
85 push @arg, "--to=$to";
86 $to = uri_escape_utf8($to);
87 $subj = uri_escape_utf8($subj);
89 my @cc = sort values %$cc;
95 obfuscate_addrs($ibx, $addr, '$(echo .)');
99 $cc = '&Cc=' . uri_escape_utf8(join(',', @cc));
100 push(@arg, map { "--cc=$_" } @cc);
104 # I'm not sure if address obfuscation and mailto: links can
105 # be made compatible; and address obfuscation is misguided,
107 return (\@arg, '', $reply_to_all) if $obfs;
109 # order matters, Subject is the least important header,
110 # so it is last in case it's lost/truncated in a copy+paste
111 my $href = "mailto:$to?In-Reply-To=$irt${cc}&Subject=$subj";
113 (\@arg, ascii_html($href), $reply_to_all);