1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # PublicInbox::Eml to (optionally colorized) text coverter for terminals
5 # the non-HTML counterpart to PublicInbox::View
6 package PublicInbox::LeiViewText;
9 use PublicInbox::MsgIter qw(msg_part_text);
10 use PublicInbox::MID qw(references);
11 use PublicInbox::View;
12 use PublicInbox::Hval;
13 use PublicInbox::ViewDiff;
14 use PublicInbox::Spawn qw(popen_rd);
17 use PublicInbox::Address;
20 # xhtml_map works since we don't search for HTML ([&<>'"])
21 $_[0] =~ s/([\x7f\x00-\x1f])/$PublicInbox::Hval::xhtml_map{$1}/sge;
25 # mutt names, loaded from ~/.config/lei/config
28 status => 'bright_cyan', # smsg stuff
29 attachment => 'bright_red',
31 # git names and defaults, falls back to ~/.gitconfig
40 my $COLOR = qr/(?:bright)?
41 (?:normal|black|red|green|yellow|blue|magenta|cyan|white)/x;
44 my ($self, $slot, $buf) = @_;
45 my $val = $self->{"color.$slot"} //=
46 $self->{-leicfg}->{"color.$slot"} //
47 $self->{-gitcfg}->{"color.diff.$slot"} //
48 $self->{-gitcfg}->{"diff.color.$slot"} //
49 $DEFAULT_COLOR{$slot};
50 $val = $val->[-1] if ref($val) eq 'ARRAY';
53 # git doesn't use "_", Term::ANSIColor does
54 $val =~ s/\Abright([^_])/bright_$1/ig;
56 # git: "green black" => T::A: "green on_black"
57 $val =~ s/($COLOR)(.+?)($COLOR)/$1$2on_$3/;
59 # FIXME: convert git #XXXXXX to T::A-compatible colors
60 # for 256-color terminals
62 ${$self->{obuf}} .= colored($buf, $val);
64 ${$self->{obuf}} .= $buf;
68 sub uncolored { ${$_[0]->{obuf}} .= $_[2] }
71 my ($cls, $lei, $fmt) = @_;
72 my $self = bless { %{$lei->{opt}}, -colored => \&uncolored }, $cls;
73 $self->{-quote_reply} = 1 if $fmt eq 'reply';
74 return $self unless $self->{color} //= -t $lei->{1};
75 my $cmd = [ qw(git config -z --includes -l) ];
76 my ($r, $pid) = popen_rd($cmd, undef, { 2 => $lei->{2} });
77 my $cfg = PublicInbox::Config::config_fh_parse($r, "\0", "\n");
80 $lei->err("# git-config failed, no color (non-fatal)");
83 $self->{-colored} = \&my_colored;
84 $self->{-gitcfg} = $cfg;
85 $self->{-leicfg} = $lei->{cfg};
89 sub quote_hdr_buf ($$) {
90 my ($self, $eml) = @_;
92 my $to = $eml->header_raw('Reply-To') //
93 $eml->header_raw('From') //
94 $eml->header_raw('Sender');
96 for my $f (qw(To Cc)) {
97 for my $v ($eml->header_raw($f)) {
103 substr($cc, 0, 2, ''); # s/^, //;
104 PublicInbox::View::fold_addresses($to);
105 PublicInbox::View::fold_addresses($cc);
108 $hbuf .= "To: $to\n" if defined $to && $to =~ /\S/;
109 $hbuf .= "Cc: $cc\n" if $cc =~ /\S/;
110 my $s = $eml->header_str('Subject') // 'your mail';
112 substr($s, 0, 0, 'Re: ') if $s !~ /\bRe:/i;
113 $hbuf .= "Subject: $s\n";
114 if (defined(my $irt = $eml->header_raw('Message-ID'))) {
116 $hbuf .= "In-Reply-To: $irt\n";
118 $self->{-colored}->($self, 'hdrdefault', $hbuf);
119 my ($n) = PublicInbox::Address::names($eml->header_str('From') //
120 $eml->header_str('Sender') //
121 $eml->header_str('Reply-To') //
123 my $d = $eml->header_raw('Date') // 'some unknown date';
126 ${delete $self->{obuf}} . "\nOn $d, $n wrote:\n";
130 my ($self, $eml) = @_;
132 for my $f (qw(From To Cc)) {
133 for my $v ($eml->header($f)) {
135 PublicInbox::View::fold_addresses($v);
140 for my $f (qw(Subject Date Newsgroups Message-ID X-Message-ID)) {
141 for my $v ($eml->header($f)) {
146 if (my @irt = $eml->header_raw('In-Reply-To')) {
149 $hbuf .= "In-Reply-To: $v\n";
152 my $refs = references($eml);
153 if (defined(my $irt = pop @$refs)) {
155 $hbuf .= "In-Reply-To: <$irt>\n";
158 my $max = $self->{-max_cols};
159 $hbuf .= 'References: ' .
160 join("\n\t", map { '<'._xs($_).'>' } @$refs) .
164 $self->{-colored}->($self, 'hdrdefault', $hbuf .= "\n");
167 sub attach_note ($$$$;$) {
168 my ($self, $ct, $p, $fn, $err) = @_;
169 my ($part, $depth, $idx) = @$p;
170 my $nl = $idx eq '1' ? '' : "\n"; # like join("\n", ...)
171 my $abuf = $err ? <<EOF : '';
172 [-- Warning: decoded text below may be mangled, UTF-8 assumed --]
174 $abuf .= "[-- Attachment #$idx: ";
176 my $size = length($part->body);
177 my $ts = "Type: $ct, Size: $size bytes";
178 my $d = $part->header('Content-Description') // $fn // '';
180 $abuf .= $d eq '' ? "$ts --]\n" : "$d --]\n[-- $ts --]\n";
181 if (my $blob = $self->{-smsg}->{blob}) {
182 $abuf .= "[-- lei blob $blob:$idx --]\n";
184 $self->{-colored}->($self, 'attachment', $abuf);
185 hdr_buf($self, $part) if $part->{is_submsg};
188 sub flush_text_diff ($$) {
189 my ($self, $cur) = @_;
190 my @top = split($PublicInbox::ViewDiff::EXTRACT_DIFFS, $$cur);
191 undef $$cur; # free memory
193 my $obuf = $self->{obuf};
194 my $colored = $self->{-colored};
195 while (defined(my $x = shift @top)) {
196 if (scalar(@top) >= 4 &&
197 $top[1] =~ $PublicInbox::ViewDiff::IS_OID &&
198 $top[0] =~ $PublicInbox::ViewDiff::IS_OID) {
201 $colored->($self, 'meta', $x);
203 # Quiet "Complex regular subexpression recursion limit"
204 # warning. Perl will truncate matches upon hitting
205 # that limit, giving us more (and shorter) scalars than
206 # would be ideal, but otherwise it's harmless.
208 # We could replace the `+' metacharacter with `{1,100}'
209 # to limit the matches ourselves to 100, but we can
210 # let Perl do it for us, quietly.
211 no warnings 'regexp';
213 for my $s (split(/((?:(?:^\+[^\n]*\n)+)|
215 (?:^@@ [^\n]+\n))/xsm, $x)) {
216 if (!defined($dctx)) {
217 ${$self->{obuf}} .= $s;
218 } elsif ($s =~ s/\A(@@ \S+ \S+ @@\s*)//) {
219 $colored->($self, 'frag', $1);
220 $colored->($self, 'func', $s);
221 } elsif ($s =~ /\A\+/) {
222 $colored->($self, 'new', $s);
223 } elsif ($s =~ /\A-- $/sm) { # email sig starts
225 ${$self->{obuf}} .= $s;
226 } elsif ($s =~ /\A-/) {
227 $colored->($self, 'old', $s);
229 $colored->($self, 'context', $s);
233 ${$self->{obuf}} .= $x;
238 sub add_text_buf { # callback for Eml->each_part
240 my ($part, $depth, $idx) = @$p;
241 my $ct = $part->content_type || 'text/plain';
242 my $fn = $part->filename;
243 my ($s, $err) = msg_part_text($part, $ct);
244 return attach_note($self, $ct, $p, $fn) unless defined $s;
245 hdr_buf($self, $part) if $part->{is_submsg};
248 $s .= "\n" unless substr($s, -1, 1) eq "\n";
249 my $diff = ($s =~ /^--- [^\n]+\n\+{3} [^\n]+\n@@ /ms);
250 my @sections = PublicInbox::MsgIter::split_quotes($s);
251 undef $s; # free memory
252 if (defined($fn) || ($depth > 0 && !$part->{is_submsg}) || $err) {
253 # badly-encoded message with $err? tell the world about it!
254 attach_note($self, $ct, $p, $fn, $err);
255 ${$self->{obuf}} .= "\n";
257 my $colored = $self->{-colored};
258 for my $cur (@sections) {
260 $colored->($self, 'quoted', $cur);
262 flush_text_diff($self, \$cur);
264 ${$self->{obuf}} .= $cur;
266 undef $cur; # free memory
270 # returns a stringref suitable for $lei->out or print
272 my ($self, $smsg, $eml) = @_;
273 local $Term::ANSIColor::EACHLINE = "\n";
274 $self->{obuf} = \(my $obuf = '');
275 $self->{-smsg} = $smsg;
276 $self->{-max_cols} = ($self->{columns} //= 80) - 8; # for header wrap
278 if ($self->{-quote_reply}) {
279 my $blob = $smsg->{blob} // 'unknown-blob';
280 my $pct = $smsg->{pct} // 'unknown';
281 my $t = POSIX::asctime(gmtime($smsg->{ts} // $smsg->{ds} // 0));
282 $h->[0] = "From $blob\@$pct $t";
284 for my $f (qw(blob pct)) {
285 push @$h, "$f:$smsg->{$f}" if defined $smsg->{$f};
287 @$h = ("# @$h\n") if @$h;
288 for my $f (qw(kw L)) {
289 my $v = $smsg->{$f} or next;
290 push @$h, "# $f:".join(',', @$v)."\n" if @$v;
294 $self->{-colored}->($self, 'status', $h);
296 if ($self->{-quote_reply}) {
297 $quote_hdr = ${delete $self->{obuf}};
298 $quote_hdr .= quote_hdr_buf($self, $eml);
300 hdr_buf($self, $eml);
302 $eml->each_part(\&add_text_buf, $self, 1);
303 if (defined $quote_hdr) {
304 ${$self->{obuf}} =~ s/^/> /sgm;
305 substr(${$self->{obuf}}, 0, 0, $quote_hdr);
307 delete $self->{obuf};