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) .
36 '</pre><hr />' . PRE_WRAP .
37 html_footer($mime, 1, $full_pfx, $srch) . $footer .
38 '</pre></body></html>';
42 my ($class, $mime, $full_pfx) = @_;
44 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
47 # this is already inside a <pre>
48 # state = [ time, seen = {}, first_commit, page_nr = 0 ]
50 my (undef, $mime, $level, $state) = @_;
51 my ($srch, $seen, $first_commit) = @$state;
52 my $midx = $state->[3]++;
53 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 $pfx = (' ' x $level);
73 my $root_anchor = $seen->{root_anchor};
75 my $more = 'permalink';
81 my $href = $mid->as_href;
82 my $irt = $header_obj->header_raw('In-Reply-To');
83 my ($anchor_idx, $anchor, $t_anchor);
85 $anchor_idx = anchor_for($irt);
86 $anchor = $seen->{$anchor_idx};
93 $subj = "<a\nhref=\"${path}t/$href.html#u\">$subj</a>";
95 if ($root_anchor && $root_anchor eq $id) {
96 $subj = "<u\nid=\"u\">$subj</u>";
99 my $ts = $mime->header('X-PI-TS');
100 my $fmt = '%Y-%m-%d %H:%M UTC';
101 $ts = POSIX::strftime($fmt, gmtime($ts));
103 $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
104 $rv .= "- by $from @ $ts - ";
105 $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
107 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
111 my $mhref = "${path}m/$href.html";
112 my $fhref = "${path}f/$href.html";
113 # scan through all parts, looking for displayable text
114 $mime->walk_parts(sub {
115 $rv .= index_walk($_[0], $pfx, $enc_msg, $part_nr, $fhref,
120 $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
121 my $txt = "${path}m/$href.txt";
122 $rv .= "<a\nhref=\"$txt\">raw</a> ";
123 $rv .= html_footer($mime, 0);
126 unless (defined $anchor) {
127 my $v = PublicInbox::Hval->new_msgid($irt);
129 $anchor = "${path}m/$v.html";
130 $seen->{$anchor_idx} = $anchor;
132 $rv .= " <a\nhref=\"$anchor\">parent</a>";
136 $rv .= " <a\nhref=\"${path}t/$href.html$t_anchor\">" .
144 my (undef, $ctx, $foot, $srch) = @_;
145 my $mid = mid_compressed($ctx->{mid});
146 my $res = $srch->get_thread($mid);
148 my $msgs = load_results($ctx, $res);
149 my $nr = scalar @$msgs;
150 return $rv if $nr == 0;
151 require PublicInbox::Thread;
152 my $th = PublicInbox::Thread->new(@$msgs);
154 $th->order(*PublicInbox::Thread::sort_ts);
155 my $state = [ $srch, { root_anchor => anchor_for($mid) }, undef, 0 ];
156 thread_entry(\$rv, $state, $_, 0) for $th->rootset;
157 my $final_anchor = $state->[3];
158 my $next = "<a\nid=\"s$final_anchor\">";
160 if ($final_anchor == 1) {
161 $next .= 'only message in thread';
163 $next .= 'end of thread';
165 $next .= ", back to <a\nhref=\"../\">index</a>\n";
167 $rv .= "</pre><hr />" . PRE_WRAP . $next . $foot . "</pre>";
170 sub subject_path_html {
171 my (undef, $ctx, $foot, $srch) = @_;
172 my $path = $ctx->{subject_path};
173 my $res = $srch->get_subject_path($path);
175 my $msgs = load_results($ctx, $res);
176 my $nr = scalar @$msgs;
177 return $rv if $nr == 0;
178 require PublicInbox::Thread;
179 my $th = PublicInbox::Thread->new(@$msgs);
181 $th->order(*PublicInbox::Thread::sort_ts);
182 my $state = [ $srch, { root_anchor => 'dummy' }, undef, 0 ];
183 thread_entry(\$rv, $state, $_, 0) for $th->rootset;
184 my $final_anchor = $state->[3];
185 my $next = "<a\nid=\"s$final_anchor\">end of thread</a>\n";
187 $rv .= "</pre><hr />" . PRE_WRAP . $next . $foot . "</pre>";
190 # only private functions below.
193 my ($part, $pfx, $enc_msg, $part_nr, $fhref, $more) = @_;
195 return $rv if $part->subparts; # walk_parts already recurses
196 my $ct = $part->content_type;
198 # account for filter bugs...
199 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
201 my $enc = enc_for($ct, $enc_msg);
204 my $fn = $part->filename;
205 defined($fn) or $fn = "part #" . ($part_nr + 1);
206 $rv .= $pfx . add_filename_line($enc->decode($fn));
209 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
211 # drop the remainder of git patches, they're usually better
212 # to review when the full message is viewed
213 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
216 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
218 # kill any leading or trailing whitespace lines
226 # kill per-line trailing whitespace
227 $s =~ s/[ \t]+$//sgm;
235 my ($ct, $default) = @_;
236 $default ||= $enc_utf8;
237 defined $ct or return $default;
238 my $ct_parsed = parse_content_type($ct);
240 if (my $charset = $ct_parsed->{attributes}->{charset}) {
241 my $enc = find_encoding($charset);
248 sub multipart_text_as_html {
249 my ($mime, $full_pfx, $srch) = @_;
252 my $enc_msg = enc_for($mime->header("Content-Type"));
254 # scan through all parts, looking for displayable text
255 $mime->walk_parts(sub {
257 return if $part->subparts; # walk_parts already recurses
258 my $ct = $part->content_type;
260 # account for filter bugs...
261 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
263 my $enc = enc_for($ct, $enc_msg);
266 my $fn = $part->filename;
267 defined($fn) or $fn = "part #" . ($part_nr + 1);
268 $rv .= add_filename_line($enc->decode($fn));
271 if (defined $full_pfx) {
272 $rv .= add_text_body_short($enc, $part, $part_nr,
275 $rv .= add_text_body_full($enc, $part, $part_nr);
277 $rv .= "\n" unless $rv =~ /\n\z/s;
283 sub add_filename_line {
289 $pad x= ($len/2) if ($len > 0);
290 "$pad " . ascii_html($fn) . " $pad\n";
293 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
296 # no newlines added here since it'd break the splitting we do
298 $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
301 sub add_text_body_short {
302 my ($enc, $part, $part_nr, $full_pfx) = @_;
304 my $s = ascii_html($enc->decode($part->body));
306 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
308 my @lines = split(/\n/, $cur);
309 if (@lines > MAX_INLINE_QUOTED) {
310 # show a short snippet of quoted text
311 $cur = join(' ', @lines);
312 $cur =~ s/^>\s*//;
314 my @sum = split(/\s+/, $cur);
317 my $tmp = shift(@sum);
318 my $len = length($tmp) + length($cur);
319 if ($len > MAX_TRUNC_LEN) {
324 } while (@sum && length($cur) < MAX_TRUNC_LEN);
326 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
327 "\">$cur<\/a>>\n";
335 sub add_text_body_full {
336 my ($enc, $part, $part_nr) = @_;
338 my $s = ascii_html($enc->decode($part->body));
340 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
342 my @lines = split(/\n/, $cur);
343 if (@lines > MAX_INLINE_QUOTED) {
344 "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
352 sub headers_to_html_header {
353 my ($mime, $full_pfx, $srch) = @_;
357 my $header_obj = $mime->header_obj;
358 my $mid = $header_obj->header_raw('Message-ID');
359 $mid = PublicInbox::Hval->new_msgid($mid);
360 my $mid_href = $mid->as_href;
361 foreach my $h (qw(From To Cc Subject Date)) {
362 my $v = $mime->header($h);
363 defined($v) && length($v) or next;
364 $v = PublicInbox::Hval->new_oneline($v);
367 my @from = Email::Address->parse($v->raw);
368 $title[1] = ascii_html($from[0]->name);
369 } elsif ($h eq 'Subject') {
370 $title[0] = $v->as_html;
372 $rv .= "$h: <a\nhref=\"../t/$mid_href.html\">";
373 $rv .= $v->as_html . "</a>\n";
377 $rv .= "$h: " . $v->as_html . "\n";
381 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
382 $mid_href = "../m/$mid_href" unless $full_pfx;
383 $rv .= "(<a\nhref=\"$mid_href.txt\">raw</a>)\n";
385 my $irt = $header_obj->header_raw('In-Reply-To');
387 my $v = PublicInbox::Hval->new_msgid($irt);
388 my $html = $v->as_html;
389 my $href = $v->as_href;
390 $rv .= "In-Reply-To: <";
391 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
394 my $refs = $header_obj->header_raw('References');
396 $refs =~ s/\s*\Q$irt\E\s*// if (defined $irt);
397 my @refs = ($refs =~ /<([^>]+)>/g);
399 $rv .= 'References: '. linkify_refs(@refs) . "\n";
405 ("<html><head><title>". join(' - ', @title) .
406 '</title></head><body>' . PRE_WRAP . $rv);
410 my ($mime, $standalone, $full_pfx, $srch) = @_;
411 my %cc; # everyone else
412 my $to; # this is the From address
414 foreach my $h (qw(From To Cc)) {
415 my $v = $mime->header($h);
416 defined($v) && length($v) or next;
417 my @addrs = Email::Address->parse($v);
418 foreach my $recip (@addrs) {
419 my $address = $recip->address;
420 my $dst = lc($address);
421 $cc{$dst} ||= $address;
425 Email::Address->purge_cache if $standalone;
427 my $subj = $mime->header('Subject') || '';
428 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
429 my $mid = $mime->header_obj->header_raw('Message-ID');
430 my $irt = uri_escape_utf8($mid);
432 $to = uri_escape_utf8($to);
433 $subj = uri_escape_utf8($subj);
435 my $cc = uri_escape_utf8(join(',', sort values %cc));
436 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
438 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
440 $irt = $mime->header_obj->header_raw('In-Reply-To') || '';
441 $mid = mid_compressed(mid_clean($mid));
442 my $t_anchor = length $irt ? T_ANCHOR : '';
443 $idx = " <a\nhref=\"../t/$mid.html$t_anchor\">".
444 "threadlink</a>$idx";
445 my $res = $srch->get_followups($mid);
446 if (my $c = $res->{count}) {
447 $c = $c == 1 ? '1 followup' : "$c followups";
449 thread_followups(\$idx, $mime, $res);
451 $idx .= "\n(no followups, yet)\n";
454 $irt = PublicInbox::Hval->new_msgid($irt);
455 $irt = $irt->as_href;
456 $irt = "<a\nhref=\"$irt\">parent</a> ";
458 $irt = ' ' x length('parent ');
464 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
469 my $v = PublicInbox::Hval->new_msgid($_);
470 my $html = $v->as_html;
471 my $href = $v->as_href;
472 "<<a\nhref=\"$href.html\">$html</a>>";
478 'm' . mid_compressed(mid_clean($msgid));
482 my ($dst, $root, $node, $level) = @_;
483 my $pfx = ' ' x $level;
485 if (my $x = $node->message) {
486 my $mid = $x->header('Message-ID');
487 if ($root->[0] ne $mid) {
488 my $s = $x->header('Subject');
489 my $h = hash_subj($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) if $node->child;
512 simple_dump($dst, $root, $node->next, $level) if $node->next;
519 $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
521 Digest::SHA::sha1($subj);
524 sub thread_followups {
525 my ($dst, $root, $res) = @_;
526 my @msgs = map { $_->mini_mime } @{$res->{msgs}};
527 require PublicInbox::Thread;
528 $root->header_set('X-PI-TS', '0');
529 my $th = PublicInbox::Thread->new($root, @msgs);
531 $th->order(*PublicInbox::Thread::sort_ts);
532 $root = [ $root->header('Message-ID'),
533 { hash_subj($root->header('Subject')) => 1 } ];
534 simple_dump($dst, $root, $_, 0) for $th->rootset;
537 sub thread_html_head {
539 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
541 "<html><head><title>$s</title></head><body>" . PRE_WRAP
546 my ($dst, $state, $node, $level) = @_;
547 # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
548 # $seen is overloaded with 3 types of fields:
549 # 1) "root_anchor" => anchor_for(Message-ID),
550 # 2) seen subject hashes: sha1(subject) => 1
551 # 3) anchors hashes: "#$sha1_hex" (same as $seen in index_entry)
552 if (my $mime = $node->message) {
553 if (length($$dst) == 0) {
554 $$dst .= thread_html_head($mime);
556 $$dst .= index_entry(undef, $mime, $level, $state);
558 thread_entry($dst, $state, $node->child, $level + 1) if $node->child;
559 thread_entry($dst, $state, $node->next, $level) if $node->next;
563 my ($ctx, $res) = @_;
565 require PublicInbox::GitCatFile;
566 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
568 while (my $smsg = shift @{$res->{msgs}}) {
570 my $path = mid2path($m);
572 # FIXME: duplicated code from Feed.pm
574 my $str = $git->cat_file("HEAD:$path");
575 Email::MIME->new($str);
578 my $t = eval { str2time($mime->header('Date')) };
579 defined($t) or $t = 0;
580 $mime->header_set('X-PI-TS', $t);