1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::View;
6 use URI::Escape qw/uri_escape_utf8/;
7 use Encode qw/find_encoding/;
8 use Encode::MIME::Header;
9 use Email::MIME::ContentType qw/parse_content_type/;
10 use PublicInbox::Hval;
11 use PublicInbox::MID qw/mid_clean mid_compressed/;
15 # TODO: make these constants tunable
16 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
17 use constant MAX_TRUNC_LEN => 72;
18 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
20 *ascii_html = *PublicInbox::Hval::ascii_html;
22 my $enc_utf8 = find_encoding('UTF-8');
26 my ($class, $mime, $full_pfx, $footer, $srch) = @_;
27 if (defined $footer) {
28 $footer = "\n" . $footer;
32 headers_to_html_header($mime, $full_pfx) .
33 multipart_text_as_html($mime, $full_pfx) .
34 '</pre><hr />' . PRE_WRAP .
35 html_footer($mime, 1, $full_pfx, $srch) . $footer .
36 '</pre></body></html>';
40 my ($class, $mime, $full_pfx) = @_;
42 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
45 # this is already inside a <pre>
47 my ($class, $mime, $level, $state) = @_;
48 my ($now, $seen, $first) = @$state;
49 my $midx = $state->[3]++;
50 my ($prev, $next) = ($midx - 1, $midx + 1);
53 my $enc_msg = enc_for($mime->header("Content-Type"));
54 my $subj = $mime->header('Subject');
55 my $header_obj = $mime->header_obj;
57 my $mid_raw = $header_obj->header_raw('Message-ID');
58 my $id = anchor_for($mid_raw);
59 $seen->{$id} = "#$id"; # save the anchor for later
61 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
62 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
63 my @from = Email::Address->parse($from);
64 $from = $from[0]->name;
65 (defined($from) && length($from)) or $from = $from[0]->address;
67 $from = PublicInbox::Hval->new_oneline($from)->as_html;
68 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
69 my $pfx = (' ' x $level);
71 my $ts = $mime->header('X-PI-TS');
72 my $fmt = '%Y-%m-%d %H:%M UTC';
73 $ts = POSIX::strftime($fmt, gmtime($ts));
75 $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
76 $rv .= "- by $from @ $ts - ";
77 $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
79 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
83 my $irp = $header_obj->header_raw('In-Reply-To');
84 my ($anchor_idx, $anchor);
86 $anchor_idx = anchor_for($irp);
87 $anchor = $seen->{$anchor_idx};
89 my $href = $mid->as_href;
90 my $mhref = "m/$href.html";
91 my $fhref = "f/$href.html";
93 # scan through all parts, looking for displayable text
94 $mime->walk_parts(sub {
95 $rv .= index_walk($_[0], $pfx, $enc_msg, $part_nr, $fhref,
100 $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
101 my $txt = "m/$href.txt";
102 $rv .= "<a\nhref=\"$txt\">raw</a> ";
103 $rv .= html_footer($mime, 0);
106 unless (defined $anchor) {
107 my $v = PublicInbox::Hval->new_msgid($irp);
108 my $html = $v->as_html;
109 $anchor = 'm/' . $v->as_href . '.html';
110 $seen->{$anchor_idx} = $anchor;
112 $rv .= " <a\nhref=\"$anchor\">parent</a>";
114 $rv .= " <a\nhref=\"?r=$first#$id\">threadlink</a>";
119 # only private functions below.
122 my ($part, $pfx, $enc_msg, $part_nr, $fhref, $more) = @_;
124 return $rv if $part->subparts; # walk_parts already recurses
125 my $ct = $part->content_type;
127 # account for filter bugs...
128 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
130 my $enc = enc_for($ct, $enc_msg);
133 my $fn = $part->filename;
134 defined($fn) or $fn = "part #" . ($part_nr + 1);
135 $rv .= $pfx . add_filename_line($enc->decode($fn));
138 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
140 # drop the remainder of git patches, they're usually better
141 # to review when the full message is viewed
142 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
145 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
147 # kill any leading or trailing whitespace
161 my ($ct, $default) = @_;
162 $default ||= $enc_utf8;
163 defined $ct or return $default;
164 my $ct_parsed = parse_content_type($ct);
166 if (my $charset = $ct_parsed->{attributes}->{charset}) {
167 my $enc = find_encoding($charset);
174 sub multipart_text_as_html {
175 my ($mime, $full_pfx) = @_;
178 my $enc_msg = enc_for($mime->header("Content-Type"));
180 # scan through all parts, looking for displayable text
181 $mime->walk_parts(sub {
183 return if $part->subparts; # walk_parts already recurses
184 my $ct = $part->content_type;
186 # account for filter bugs...
187 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
189 my $enc = enc_for($ct, $enc_msg);
192 my $fn = $part->filename;
193 defined($fn) or $fn = "part #" . ($part_nr + 1);
194 $rv .= add_filename_line($enc->decode($fn));
197 if (defined $full_pfx) {
198 $rv .= add_text_body_short($enc, $part, $part_nr,
201 $rv .= add_text_body_full($enc, $part, $part_nr);
203 $rv .= "\n" unless $rv =~ /\n\z/s;
209 sub add_filename_line {
215 $pad x= ($len/2) if ($len > 0);
216 "$pad " . ascii_html($fn) . " $pad\n";
219 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
222 # no newlines added here since it'd break the splitting we do
224 $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
227 sub add_text_body_short {
228 my ($enc, $part, $part_nr, $full_pfx) = @_;
230 my $s = ascii_html($enc->decode($part->body));
232 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
234 my @lines = split(/\n/, $cur);
235 if (@lines > MAX_INLINE_QUOTED) {
236 # show a short snippet of quoted text
237 $cur = join(' ', @lines);
238 $cur =~ s/^>\s*//;
240 my @sum = split(/\s+/, $cur);
243 my $tmp = shift(@sum);
244 my $len = length($tmp) + length($cur);
245 if ($len > MAX_TRUNC_LEN) {
250 } while (@sum && length($cur) < MAX_TRUNC_LEN);
252 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
253 "\">$cur<\/a>>\n";
261 sub add_text_body_full {
262 my ($enc, $part, $part_nr) = @_;
264 my $s = ascii_html($enc->decode($part->body));
266 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
268 my @lines = split(/\n/, $cur);
269 if (@lines > MAX_INLINE_QUOTED) {
270 "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
278 sub headers_to_html_header {
279 my ($mime, $full_pfx) = @_;
283 foreach my $h (qw(From To Cc Subject Date)) {
284 my $v = $mime->header($h);
285 defined($v) && length($v) or next;
286 $v = PublicInbox::Hval->new_oneline($v);
287 $rv .= "$h: " . $v->as_html . "\n";
290 my @from = Email::Address->parse($v->raw);
292 unless (defined($v) && length($v)) {
293 $v = '<' . $from[0]->address . '>';
295 $title[1] = ascii_html($v);
296 } elsif ($h eq 'Subject') {
297 $title[0] = $v->as_html;
301 my $header_obj = $mime->header_obj;
302 my $mid = $header_obj->header_raw('Message-ID');
304 $mid = PublicInbox::Hval->new_msgid($mid);
305 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
306 my $href = $mid->as_href;
307 $href = "../m/$href" unless $full_pfx;
308 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
311 my $irp = $header_obj->header_raw('In-Reply-To');
313 my $v = PublicInbox::Hval->new_msgid($irp);
314 my $html = $v->as_html;
315 my $href = $v->as_href;
316 $rv .= "In-Reply-To: <";
317 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
320 my $refs = $header_obj->header_raw('References');
322 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
323 my @refs = ($refs =~ /<([^>]+)>/g);
325 $rv .= 'References: '. linkify_refs(@refs) . "\n";
331 ("<html><head><title>". join(' - ', @title) .
332 '</title></head><body>' . PRE_WRAP . $rv);
336 my ($mime, $standalone, $full_pfx, $srch) = @_;
337 my %cc; # everyone else
338 my $to; # this is the From address
340 foreach my $h (qw(From To Cc)) {
341 my $v = $mime->header($h);
342 defined($v) && length($v) or next;
343 my @addrs = Email::Address->parse($v);
344 foreach my $recip (@addrs) {
345 my $address = $recip->address;
346 my $dst = lc($address);
347 $cc{$dst} ||= $address;
351 Email::Address->purge_cache if $standalone;
353 my $subj = $mime->header('Subject') || '';
354 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
355 my $mid = $mime->header_obj->header_raw('Message-ID');
356 my $irp = uri_escape_utf8($mid);
358 $to = uri_escape_utf8($to);
359 $subj = uri_escape_utf8($subj);
361 my $cc = uri_escape_utf8(join(',', sort values %cc));
362 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
365 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
367 my $res = $srch->get_replies($mid);
368 if (my $c = $res->{count}) {
369 $c = $c == 1 ? '1 reply' : "$c replies";
371 thread_replies(\$idx, $mime, $res);
373 $idx .= "\n(no replies yet)\n";
375 $irt = $mime->header_obj->header_raw('In-Reply-To');
377 $irt = PublicInbox::Hval->new_msgid($irt);
378 $irt = $irt->as_href;
379 $irt = "<a\nhref=\"$irt\">parent</a> ";
381 $irt = ' ' x length('parent ');
385 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
390 my $v = PublicInbox::Hval->new_msgid($_);
391 my $html = $v->as_html;
392 my $href = $v->as_href;
393 "<<a\nhref=\"$href.html\">$html</a>>";
399 'm' . mid_compressed(mid_clean($msgid));
403 my ($dst, $root, $node, $level) = @_;
404 my $pfx = ' ' x $level;
406 if (my $x = $node->message) {
407 my $mid = $x->header('Message-ID');
408 if ($root->[0] ne $mid) {
409 my $s = $x->header('Subject');
410 my $h = hash_subj($s);
411 if ($root->[1]->{$h}) {
414 $root->[1]->{$h} = 1;
415 $s = PublicInbox::Hval->new($s);
418 my $m = PublicInbox::Hval->new_msgid($mid);
419 my $f = PublicInbox::Hval->new($x->header('X-PI-From'));
420 my $d = PublicInbox::Hval->new($x->header('X-PI-Date'));
421 $m = $m->as_href . '.html';
423 $d = $d->as_html . ' UTC';
424 if (length($s) == 0) {
425 $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
427 $$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
432 simple_dump($dst, $root, $node->child, $level + 1) if $node->child;
433 simple_dump($dst, $root, $node->next, $level) if $node->next;
440 $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
442 Digest::SHA::sha1($subj);
446 my ($dst, $root, $res) = @_;
447 my @msgs = map { $_->mini_mime } @{$res->{msgs}};
448 require PublicInbox::Thread;
449 $root->header_set('X-PI-TS', '0');
450 my $th = PublicInbox::Thread->new($root, @msgs);
452 $th->order(*PublicInbox::Thread::sort_ts);
453 $root = [ $root->header('Message-ID'),
454 { hash_subj($root->header('Subject')) => 1 } ];
455 simple_dump($dst, $root, $_, 0) for $th->rootset;