1 # Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # For reply instructions and address generation in WWW UI
5 package PublicInbox::Reply;
8 use URI::Escape qw/uri_escape_utf8/;
9 use PublicInbox::Hval qw(ascii_html obfuscate_addrs mid_href);
10 use PublicInbox::Address;
11 use PublicInbox::MID qw(mid_clean);
12 use PublicInbox::Config;
14 *squote_maybe = \&PublicInbox::Config::squote_maybe;
17 my ($to, $cc, @addrs) = @_;
18 foreach my $address (@addrs) {
19 my $dst = lc($address);
20 $cc->{$dst} ||= $address;
25 my @reply_headers = qw(From To Cc Reply-To);
26 my $reply_headers = join('|', @reply_headers);
30 my $cc = {}; # everyone else
31 my $to; # this is the From address by default
32 my $reply_to_all = 'reply-to-all'; # the only good default :P
33 my $reply_to_cfg = $ibx->{replyto};
35 $reply_to_cfg ||= ':all';
36 if ($reply_to_cfg =~ /\A:none=(.*)/) {
38 $msg = 'replies disabled' if $msg eq '';
42 foreach my $rt (split(/\s*,\s*/, $reply_to_cfg)) {
44 foreach my $h (@reply_headers) {
45 my $v = $hdr->header($h);
46 defined($v) && ($v ne '') or next;
47 my @addrs = PublicInbox::Address::emails($v);
48 add_addrs(\$to, $cc, @addrs);
50 } elsif ($rt eq ':list') {
51 $reply_to_all = 'reply-to-list';
52 add_addrs(\$to, $cc, $ibx->{-primary_address});
53 } elsif ($rt =~ /\A(?:$reply_headers)\z/io) {
54 # ugh, this is weird...
55 my $v = $hdr->header($rt);
56 if (defined($v) && ($v ne '')) {
57 my @addrs = PublicInbox::Address::emails($v);
58 add_addrs(\$to, $cc, @addrs);
60 } elsif ($rt =~ /@/) {
61 add_addrs(\$to, $cc, $rt);
63 warn "Unrecognized replyto = '$rt' in config\n";
68 my $obfs = $ibx->{obfuscate};
69 my $subj = $hdr->header('Subject') || '';
70 $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
72 my $mid = $hdr->header_raw('Message-ID');
73 push @arg, '--in-reply-to='.squote_maybe(mid_clean($mid));
74 my $irt = mid_href($mid);
75 add_addrs(\$to, $cc, $ibx->{-primary_address}) unless defined($to);
79 obfuscate_addrs($ibx, $arg_to, '$(echo .)');
80 push @arg, "--to=$arg_to";
81 # no $subj for $href below
83 push @arg, "--to=$to";
84 $to = uri_escape_utf8($to);
85 $subj = uri_escape_utf8($subj);
87 my @cc = sort values %$cc;
93 obfuscate_addrs($ibx, $addr, '$(echo .)');
97 $cc = '&Cc=' . uri_escape_utf8(join(',', @cc));
98 push(@arg, map { "--cc=$_" } @cc);
102 push @arg, "--subject=".squote_maybe($subj_raw);
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);