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 Date::Parse qw/str2time/;
8 use Encode qw/find_encoding/;
9 use Encode::MIME::Header;
10 use Email::MIME::ContentType qw/parse_content_type/;
11 use PublicInbox::Hval;
12 use PublicInbox::MID qw/mid_clean mid_compressed mid2path/;
16 # TODO: make these constants tunable
17 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
18 use constant MAX_TRUNC_LEN => 72;
19 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
20 use constant T_ANCHOR => '#u';
22 *ascii_html = *PublicInbox::Hval::ascii_html;
24 my $enc_utf8 = find_encoding('UTF-8');
28 my ($class, $mime, $full_pfx, $footer, $srch) = @_;
29 if (defined $footer) {
30 $footer = "\n" . $footer;
34 headers_to_html_header($mime, $full_pfx, $srch) .
35 multipart_text_as_html($mime, $full_pfx) .
37 html_footer($mime, 1, $full_pfx, $srch) .
39 '</pre></body></html>';
43 my ($class, $mime, $full_pfx) = @_;
45 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
48 # this is already inside a <pre>
49 # state = [ time, seen = {}, first_commit, page_nr = 0 ]
51 my (undef, $mime, $level, $state) = @_;
52 my ($srch, $seen, $first_commit) = @$state;
53 my $midx = $state->[3]++;
54 my ($prev, $next) = ($midx - 1, $midx + 1);
56 my $enc = enc_for($mime->header("Content-Type"));
57 my $subj = $mime->header('Subject');
58 my $header_obj = $mime->header_obj;
60 my $mid_raw = $header_obj->header('Message-ID');
61 my $id = anchor_for($mid_raw);
62 $seen->{$id} = "#$id"; # save the anchor for later
64 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
65 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
66 my @from = Email::Address->parse($from);
67 $from = $from[0]->name;
68 (defined($from) && length($from)) or $from = $from[0]->address;
70 $from = PublicInbox::Hval->new_oneline($from)->as_html;
71 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
72 my $root_anchor = $seen->{root_anchor};
73 my $more = 'permalink';
74 my $path = $root_anchor ? '../' : '';
75 my $href = $mid->as_href;
76 my $irt = $header_obj->header('In-Reply-To');
77 my ($anchor_idx, $anchor, $t_anchor);
79 $anchor_idx = anchor_for($irt);
80 $anchor = $seen->{$anchor_idx};
86 $subj = "<a\nhref=\"${path}t/$href.html#u\">$subj</a>";
88 if ($root_anchor && $root_anchor eq $id) {
89 $subj = "<u\nid=\"u\">$subj</u>";
92 my $ts = $mime->header('X-PI-TS');
93 unless (defined $ts) {
94 $ts = msg_timestamp($mime);
96 my $fmt = '%Y-%m-%d %H:%M';
97 $ts = POSIX::strftime($fmt, gmtime($ts));
99 my $rv = "<table\nsummary=l$level><tr>";
101 $rv .= '<td><pre>' . (' ' x $level) . '</pre></td>';
103 $rv .= "<td\nid=s$midx>" . PRE_WRAP;
104 $rv .= "<b\nid=\"$id\">$subj</b>\n";
105 $rv .= "- by $from @ $ts UTC - ";
106 $rv .= "<a\nhref=\"#s$next\">next</a>";
108 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
112 my ($fhref, $more_ref);
113 my $mhref = "${path}m/$href.html";
115 $fhref = "${path}f/$href.html";
118 # scan through all parts, looking for displayable text
119 $mime->walk_parts(sub {
120 $rv .= index_walk($_[0], $enc, \$part_nr, $fhref, $more_ref);
124 $rv .= "\n<a\nhref=\"$mhref\">$more</a> ";
125 my $txt = "${path}m/$href.txt";
126 $rv .= "<a\nhref=\"$txt\">raw</a> ";
127 $rv .= html_footer($mime, 0);
130 unless (defined $anchor) {
131 my $v = PublicInbox::Hval->new_msgid($irt);
133 $anchor = "${path}m/$v.html";
134 $seen->{$anchor_idx} = $anchor;
136 $rv .= " <a\nhref=\"$anchor\">parent</a>";
140 $rv .= " <a\nhref=\"${path}t/$href.html$t_anchor\">" .
144 $rv .= '</pre></td></tr></table>';
148 my (undef, $ctx, $foot, $srch) = @_;
149 my $mid = mid_compressed($ctx->{mid});
150 my $res = $srch->get_thread($mid);
152 my $msgs = load_results($res);
153 my $nr = scalar @$msgs;
154 return $rv if $nr == 0;
155 my $th = thread_results($msgs);
156 my $state = [ $srch, { root_anchor => anchor_for($mid) }, undef, 0 ];
158 require PublicInbox::GitCatFile;
159 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
160 thread_entry(\$rv, $git, $state, $_, 0) for $th->rootset;
162 my $final_anchor = $state->[3];
163 my $next = "<a\nid=\"s$final_anchor\">";
165 if ($final_anchor == 1) {
166 $next .= 'only message in thread';
168 $next .= 'end of thread';
170 $next .= "</a>, back to <a\nhref=\"../\">index</a>\n";
172 $rv .= "<hr />" . PRE_WRAP . $next . $foot . "</pre>";
175 # only private functions below.
178 my ($part, $enc, $part_nr, $fhref, $more) = @_;
179 my $s = add_text_body($enc, $part, $part_nr, $fhref);
182 # drop the remainder of git patches, they're usually better
183 # to review when the full message is viewed
184 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
187 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
190 # kill any leading or trailing whitespace lines
195 # kill per-line trailing whitespace
196 $s =~ s/[ \t]+$//sgm;
197 $s .= "\n" unless $s =~ /\n\z/s;
203 my ($ct, $default) = @_;
204 $default ||= $enc_utf8;
205 defined $ct or return $default;
206 my $ct_parsed = parse_content_type($ct);
208 if (my $charset = $ct_parsed->{attributes}->{charset}) {
209 my $enc = find_encoding($charset);
216 sub multipart_text_as_html {
217 my ($mime, $full_pfx, $srch) = @_;
220 my $enc = enc_for($mime->header("Content-Type"));
222 # scan through all parts, looking for displayable text
223 $mime->walk_parts(sub {
225 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx);
231 sub add_filename_line {
235 $fn = $enc->decode($fn);
237 $pad x= ($len/2) if ($len > 0);
238 "$pad " . ascii_html($fn) . " $pad\n";
241 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
244 # no newlines added here since it'd break the splitting we do
246 $_[0] =~ s!$LINK_RE!<a\nhref="$1">$1</a>!g;
250 my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
253 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
255 my $rv = join("\n", map { linkify($_); $_ } @$quot);
260 # show a short snippet of quoted text and link to full version:
261 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
262 my $cur = join(' ', @$quot);
263 @$quot = split(/\s+/, $cur);
266 my $tmp = shift(@$quot);
267 my $len = length($tmp) + length($cur);
268 if ($len > MAX_TRUNC_LEN) {
273 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
275 $cur =~ s/ \z/ .../s;
277 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
279 # show everything in the full version with anchor from
280 # short version (see above)
282 my $rv = "<a\nid=q${part_nr}_$nr></a>";
283 $rv .= join("\n", map { linkify($_); $_ } @$quot) . "\n";
290 my ($enc_msg, $part, $part_nr, $full_pfx) = @_;
291 return '' if $part->subparts;
293 my $ct = $part->content_type;
294 # account for filter bugs...
295 if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
299 my $enc = enc_for($ct, $enc_msg);
304 $s = $enc->decode($s);
306 my @lines = split(/\n/, $s);
310 my $fn = $part->filename;
311 defined($fn) or $fn = "part #" . ($$part_nr + 1);
312 $s .= add_filename_line($enc, $fn);
316 while (defined(my $cur = shift @lines)) {
317 if ($cur !~ /^>/) {
318 # show the previously buffered quote inline
320 $s .= flush_quote(\@quot, \$n, $$part_nr,
332 $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1) if scalar @quot;
333 $s .= "\n" unless $s =~ /\n\z/s;
338 sub headers_to_html_header {
339 my ($mime, $full_pfx, $srch) = @_;
343 my $header_obj = $mime->header_obj;
344 my $mid = $header_obj->header('Message-ID');
345 $mid = PublicInbox::Hval->new_msgid($mid);
346 my $mid_href = $mid->as_href;
347 foreach my $h (qw(From To Cc Subject Date)) {
348 my $v = $mime->header($h);
349 defined($v) && length($v) or next;
350 $v = PublicInbox::Hval->new_oneline($v);
353 my @from = Email::Address->parse($v->raw);
354 $title[1] = ascii_html($from[0]->name);
355 } elsif ($h eq 'Subject') {
356 $title[0] = $v->as_html;
358 $rv .= "$h: <a\nhref=\"../t/$mid_href.html\">";
359 $rv .= $v->as_html . "</a>\n";
363 $rv .= "$h: " . $v->as_html . "\n";
367 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
368 $mid_href = "../m/$mid_href" unless $full_pfx;
369 $rv .= "(<a\nhref=\"$mid_href.txt\">raw</a>)\n";
371 my $irt = $header_obj->header('In-Reply-To');
373 my $v = PublicInbox::Hval->new_msgid($irt);
374 my $html = $v->as_html;
375 my $href = $v->as_href;
376 $rv .= "In-Reply-To: <";
377 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
380 my $refs = $header_obj->header('References');
382 # avoid redundant URLs wasting bandwidth
384 $seen{mid_clean($irt)} = 1 if defined $irt;
386 my @raw_refs = ($refs =~ /<([^>]+)>/g);
387 foreach my $ref (@raw_refs) {
390 push @refs, linkify_ref($ref);
394 $rv .= 'References: '. join(' ', @refs) . "\n";
400 ("<html><head><title>". join(' - ', @title) .
401 '</title></head><body>' . PRE_WRAP . $rv);
405 my ($mime, $standalone, $full_pfx, $srch) = @_;
406 my %cc; # everyone else
407 my $to; # this is the From address
409 foreach my $h (qw(From To Cc)) {
410 my $v = $mime->header($h);
411 defined($v) && length($v) or next;
412 my @addrs = Email::Address->parse($v);
413 foreach my $recip (@addrs) {
414 my $address = $recip->address;
415 my $dst = lc($address);
416 $cc{$dst} ||= $address;
420 Email::Address->purge_cache if $standalone;
422 my $subj = $mime->header('Subject') || '';
423 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
424 my $mid = $mime->header_obj->header('Message-ID');
425 my $irt = uri_escape_utf8($mid);
427 $to = uri_escape_utf8($to);
428 $subj = uri_escape_utf8($subj);
430 my $cc = uri_escape_utf8(join(',', sort values %cc));
431 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
433 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
435 $irt = $mime->header_obj->header('In-Reply-To') || '';
436 $mid = mid_compressed(mid_clean($mid));
437 my $t_anchor = length $irt ? T_ANCHOR : '';
438 $idx = " <a\nhref=\"../t/$mid.html$t_anchor\">".
439 "threadlink</a>$idx";
440 my $res = $srch->get_followups($mid);
441 if (my $c = $res->{total}) {
442 $c = $c == 1 ? '1 followup' : "$c followups";
444 $res->{srch} = $srch;
445 thread_followups(\$idx, $mime, $res);
447 $idx .= "\n(no followups, yet)\n";
450 $irt = PublicInbox::Hval->new_msgid($irt);
451 $irt = $irt->as_href;
452 $irt = "<a\nhref=\"$irt\">parent</a> ";
454 $irt = ' ' x length('parent ');
460 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
464 my $v = PublicInbox::Hval->new_msgid($_[0]);
465 my $html = $v->as_html;
466 my $href = $v->as_href;
467 "<<a\nhref=\"$href.html\">$html</a>>";
473 if ($id !~ /\A[a-f0-9]{40}\z/) {
474 $id = mid_compressed(mid_clean($id), 1);
480 my ($dst, $root, $node, $level) = @_;
482 # $root = [ Root Message-ID, \%seen, $srch ];
483 if (my $x = $node->message) {
484 my $mid = $x->header('Message-ID');
485 if ($root->[0] ne $mid) {
486 my $pfx = ' ' x $level;
488 my $s = $x->header('Subject');
489 my $h = $root->[2]->subject_path($s);
490 if ($root->[1]->{$h}) {
493 $root->[1]->{$h} = 1;
494 $s = PublicInbox::Hval->new($s);
497 my $m = PublicInbox::Hval->new_msgid($mid);
498 my $f = PublicInbox::Hval->new($x->header('X-PI-From'));
499 my $d = PublicInbox::Hval->new($x->header('X-PI-Date'));
500 $m = $m->as_href . '.html';
502 $d = $d->as_html . ' UTC';
503 if (length($s) == 0) {
504 $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
506 $$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
511 simple_dump($dst, $root, $node->child, $level+1);
512 simple_dump($dst, $root, $node->next, $level);
515 sub thread_followups {
516 my ($dst, $root, $res) = @_;
517 $root->header_set('X-PI-TS', '0');
518 my $msgs = load_results($res);
520 my $th = thread_results($msgs);
521 my $srch = $res->{srch};
522 my $subj = $srch->subject_path($root->header('Subject'));
523 my %seen = ($subj => 1);
524 $root = [ $root->header('Message-ID'), \%seen, $srch ];
525 simple_dump($dst, $root, $_, 0) for $th->rootset;
528 sub thread_html_head {
530 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
532 "<html><head><title>$s</title></head><body>";
536 my ($dst, $git, $state, $node, $level) = @_;
538 # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
539 # $seen is overloaded with 3 types of fields:
540 # 1) "root_anchor" => anchor_for(Message-ID),
541 # 2) seen subject hashes: sha1(subject) => 1
542 # 3) anchors hashes: "#$sha1_hex" (same as $seen in index_entry)
543 if (my $mime = $node->message) {
545 # lazy load the full message from mini_mime:
546 my $path = mid2path(mid_clean($mime->header('Message-ID')));
547 $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
549 if (length($$dst) == 0) {
550 $$dst .= thread_html_head($mime);
552 $$dst .= index_entry(undef, $mime, $level, $state);
555 thread_entry($dst, $git, $state, $node->child, $level + 1);
556 thread_entry($dst, $git, $state, $node->next, $level);
562 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
567 my $ts = eval { str2time($mime->header('Date')) };
568 defined($ts) ? $ts : 0;
573 require PublicInbox::Thread;
574 my $th = PublicInbox::Thread->new(@$msgs);
576 $th->order(*PublicInbox::Thread::sort_ts);