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_msg = 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_raw('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_raw('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>' . PRE_WRAP;
104 $rv .= "<b\nid=\"$id\">$subj</b>\n";
105 $rv .= "- by $from @ $ts UTC - ";
106 $rv .= "<a\nid=\"s$midx\"\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_msg, $part_nr, $fhref, $more_ref);
125 $rv .= "\n<a\nhref=\"$mhref\">$more</a> ";
126 my $txt = "${path}m/$href.txt";
127 $rv .= "<a\nhref=\"$txt\">raw</a> ";
128 $rv .= html_footer($mime, 0);
131 unless (defined $anchor) {
132 my $v = PublicInbox::Hval->new_msgid($irt);
134 $anchor = "${path}m/$v.html";
135 $seen->{$anchor_idx} = $anchor;
137 $rv .= " <a\nhref=\"$anchor\">parent</a>";
141 $rv .= " <a\nhref=\"${path}t/$href.html$t_anchor\">" .
145 $rv .= '</pre></td></tr></table>';
149 my (undef, $ctx, $foot, $srch) = @_;
150 my $mid = mid_compressed($ctx->{mid});
151 my $res = $srch->get_thread($mid);
153 my $msgs = load_results($res);
154 my $nr = scalar @$msgs;
155 return $rv if $nr == 0;
156 my $th = thread_results($msgs);
157 my $state = [ $srch, { root_anchor => anchor_for($mid) }, undef, 0 ];
159 require PublicInbox::GitCatFile;
160 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
161 thread_entry(\$rv, $git, $state, $_, 0) for $th->rootset;
163 my $final_anchor = $state->[3];
164 my $next = "<a\nid=\"s$final_anchor\">";
166 if ($final_anchor == 1) {
167 $next .= 'only message in thread';
169 $next .= 'end of thread';
171 $next .= "</a>, back to <a\nhref=\"../\">index</a>\n";
173 $rv .= "<hr />" . PRE_WRAP . $next . $foot . "</pre>";
176 # only private functions below.
179 my ($part, $enc_msg, $part_nr, $fhref, $more) = @_;
181 return $rv if $part->subparts; # walk_parts already recurses
182 my $ct = $part->content_type;
184 # account for filter bugs...
185 if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
190 my $enc = enc_for($ct, $enc_msg);
193 my $fn = $part->filename;
194 defined($fn) or $fn = "part #" . ($part_nr + 1);
195 $rv .= add_filename_line($enc->decode($fn));
198 my $s = add_text_body($enc, $part, $part_nr, $fhref);
201 # drop the remainder of git patches, they're usually better
202 # to review when the full message is viewed
203 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
206 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
209 # kill any leading or trailing whitespace lines
214 # kill per-line trailing whitespace
215 $s =~ s/[ \t]+$//sgm;
225 my ($ct, $default) = @_;
226 $default ||= $enc_utf8;
227 defined $ct or return $default;
228 my $ct_parsed = parse_content_type($ct);
230 if (my $charset = $ct_parsed->{attributes}->{charset}) {
231 my $enc = find_encoding($charset);
238 sub multipart_text_as_html {
239 my ($mime, $full_pfx, $srch) = @_;
242 my $enc_msg = enc_for($mime->header("Content-Type"));
244 # scan through all parts, looking for displayable text
245 $mime->walk_parts(sub {
247 return if $part->subparts; # walk_parts already recurses
248 my $ct = $part->content_type;
250 # account for filter bugs...
251 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
253 my $enc = enc_for($ct, $enc_msg);
256 my $fn = $part->filename;
257 defined($fn) or $fn = "part #" . ($part_nr + 1);
258 $rv .= add_filename_line($enc->decode($fn));
261 $rv .= add_text_body($enc, $part, $part_nr, $full_pfx);
262 $rv .= "\n" unless $rv =~ /\n\z/s;
269 sub add_filename_line {
275 $pad x= ($len/2) if ($len > 0);
276 "$pad " . ascii_html($fn) . " $pad\n";
279 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
282 # no newlines added here since it'd break the splitting we do
284 $_[0] =~ s!$LINK_RE!<a\nhref="$1">$1</a>!g;
288 my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
291 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
293 my $rv = join("\n", map { linkify($_); $_ } @$quot);
298 # show a short snippet of quoted text and link to full version:
299 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
300 my $cur = join(' ', @$quot);
301 @$quot = split(/\s+/, $cur);
304 my $tmp = shift(@$quot);
305 my $len = length($tmp) + length($cur);
306 if ($len > MAX_TRUNC_LEN) {
311 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
313 $cur =~ s/ \z/ .../s;
315 "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
317 # show everything in the full version with anchor from
318 # short version (see above)
320 my $rv = "<a\nid=q${part_nr}_$nr></a>";
321 $rv .= join("\n", map { linkify($_); $_ } @$quot) . "\n";
328 my ($enc, $part, $part_nr, $full_pfx) = @_;
333 $s = $enc->decode($s);
335 my @lines = split(/\n/, $s);
338 while (defined(my $cur = shift @lines)) {
339 if ($cur !~ /^>/) {
340 # show the previously buffered quote inline
342 $s .= flush_quote(\@quot, \$n, $part_nr,
354 $s .= flush_quote(\@quot, \$n, $part_nr, $full_pfx, 1) if scalar @quot;
358 sub headers_to_html_header {
359 my ($mime, $full_pfx, $srch) = @_;
363 my $header_obj = $mime->header_obj;
364 my $mid = $header_obj->header_raw('Message-ID');
365 $mid = PublicInbox::Hval->new_msgid($mid);
366 my $mid_href = $mid->as_href;
367 foreach my $h (qw(From To Cc Subject Date)) {
368 my $v = $mime->header($h);
369 defined($v) && length($v) or next;
370 $v = PublicInbox::Hval->new_oneline($v);
373 my @from = Email::Address->parse($v->raw);
374 $title[1] = ascii_html($from[0]->name);
375 } elsif ($h eq 'Subject') {
376 $title[0] = $v->as_html;
378 $rv .= "$h: <a\nhref=\"../t/$mid_href.html\">";
379 $rv .= $v->as_html . "</a>\n";
383 $rv .= "$h: " . $v->as_html . "\n";
387 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
388 $mid_href = "../m/$mid_href" unless $full_pfx;
389 $rv .= "(<a\nhref=\"$mid_href.txt\">raw</a>)\n";
391 my $irt = $header_obj->header_raw('In-Reply-To');
393 my $v = PublicInbox::Hval->new_msgid($irt);
394 my $html = $v->as_html;
395 my $href = $v->as_href;
396 $rv .= "In-Reply-To: <";
397 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
400 my $refs = $header_obj->header_raw('References');
402 # avoid redundant URLs wasting bandwidth
404 $seen{mid_clean($irt)} = 1 if defined $irt;
406 my @raw_refs = ($refs =~ /<([^>]+)>/g);
407 foreach my $ref (@raw_refs) {
410 push @refs, linkify_ref($ref);
414 $rv .= 'References: '. join(' ', @refs) . "\n";
420 ("<html><head><title>". join(' - ', @title) .
421 '</title></head><body>' . PRE_WRAP . $rv);
425 my ($mime, $standalone, $full_pfx, $srch) = @_;
426 my %cc; # everyone else
427 my $to; # this is the From address
429 foreach my $h (qw(From To Cc)) {
430 my $v = $mime->header($h);
431 defined($v) && length($v) or next;
432 my @addrs = Email::Address->parse($v);
433 foreach my $recip (@addrs) {
434 my $address = $recip->address;
435 my $dst = lc($address);
436 $cc{$dst} ||= $address;
440 Email::Address->purge_cache if $standalone;
442 my $subj = $mime->header('Subject') || '';
443 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
444 my $mid = $mime->header_obj->header_raw('Message-ID');
445 my $irt = uri_escape_utf8($mid);
447 $to = uri_escape_utf8($to);
448 $subj = uri_escape_utf8($subj);
450 my $cc = uri_escape_utf8(join(',', sort values %cc));
451 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
453 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
455 $irt = $mime->header_obj->header_raw('In-Reply-To') || '';
456 $mid = mid_compressed(mid_clean($mid));
457 my $t_anchor = length $irt ? T_ANCHOR : '';
458 $idx = " <a\nhref=\"../t/$mid.html$t_anchor\">".
459 "threadlink</a>$idx";
460 my $res = $srch->get_followups($mid);
461 if (my $c = $res->{count}) {
462 $c = $c == 1 ? '1 followup' : "$c followups";
464 $res->{srch} = $srch;
465 thread_followups(\$idx, $mime, $res);
467 $idx .= "\n(no followups, yet)\n";
470 $irt = PublicInbox::Hval->new_msgid($irt);
471 $irt = $irt->as_href;
472 $irt = "<a\nhref=\"$irt\">parent</a> ";
474 $irt = ' ' x length('parent ');
480 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
484 my $v = PublicInbox::Hval->new_msgid($_[0]);
485 my $html = $v->as_html;
486 my $href = $v->as_href;
487 "<<a\nhref=\"$href.html\">$html</a>>";
493 if ($id !~ /\A[a-f0-9]{40}\z/) {
494 $id = mid_compressed(mid_clean($id), 1);
500 my ($dst, $root, $node, $level) = @_;
501 # $root = [ Root Message-ID, \%seen, $srch ];
502 if (my $x = $node->message) {
503 my $mid = $x->header('Message-ID');
504 if ($root->[0] ne $mid) {
505 my $pfx = ' ' x $level;
507 my $s = $x->header('Subject');
508 my $h = $root->[2]->subject_path($s);
509 if ($root->[1]->{$h}) {
512 $root->[1]->{$h} = 1;
513 $s = PublicInbox::Hval->new($s);
516 my $m = PublicInbox::Hval->new_msgid($mid);
517 my $f = PublicInbox::Hval->new($x->header('X-PI-From'));
518 my $d = PublicInbox::Hval->new($x->header('X-PI-Date'));
519 $m = $m->as_href . '.html';
521 $d = $d->as_html . ' UTC';
522 if (length($s) == 0) {
523 $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
525 $$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
530 simple_dump($dst, $root, $node->child, $level + 1) if $node->child;
531 simple_dump($dst, $root, $node->next, $level) if $node->next;
534 sub thread_followups {
535 my ($dst, $root, $res) = @_;
536 $root->header_set('X-PI-TS', '0');
537 my $msgs = load_results($res);
539 my $th = thread_results($msgs);
540 my $srch = $res->{srch};
541 my $subj = $srch->subject_path($root->header('Subject'));
542 my %seen = ($subj => 1);
543 $root = [ $root->header('Message-ID'), \%seen, $srch ];
544 simple_dump($dst, $root, $_, 0) for $th->rootset;
547 sub thread_html_head {
549 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
551 "<html><head><title>$s</title></head><body>";
555 my ($dst, $git, $state, $node, $level) = @_;
556 # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
557 # $seen is overloaded with 3 types of fields:
558 # 1) "root_anchor" => anchor_for(Message-ID),
559 # 2) seen subject hashes: sha1(subject) => 1
560 # 3) anchors hashes: "#$sha1_hex" (same as $seen in index_entry)
561 if (my $mime = $node->message) {
563 # lazy load the full message from mini_mime:
564 my $path = mid2path(mid_clean($mime->header('Message-ID')));
565 $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
567 if (length($$dst) == 0) {
568 $$dst .= thread_html_head($mime);
570 $$dst .= index_entry(undef, $mime, $level, $state);
574 $cur = $node->child and
575 thread_entry($dst, $git, $state, $cur, $level + 1);
576 $cur = $node->next and
577 thread_entry($dst, $git, $state, $cur, $level);
583 [ map { $_->mini_mime } @{delete $res->{msgs}} ];
588 my $ts = eval { str2time($mime->header('Date')) };
589 defined($ts) ? $ts : 0;
594 require PublicInbox::Thread;
595 my $th = PublicInbox::Thread->new(@$msgs);
597 $th->order(*PublicInbox::Thread::sort_ts);