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 $mhref = "${path}m/$href.html";
113 my $fhref = "${path}f/$href.html";
114 # scan through all parts, looking for displayable text
115 $mime->walk_parts(sub {
116 $rv .= index_walk($_[0], $enc_msg, $part_nr, $fhref, \$more);
121 $rv .= "\n<a\nhref=\"$mhref\">$more</a> ";
122 my $txt = "${path}m/$href.txt";
123 $rv .= "<a\nhref=\"$txt\">raw</a> ";
124 $rv .= html_footer($mime, 0);
127 unless (defined $anchor) {
128 my $v = PublicInbox::Hval->new_msgid($irt);
130 $anchor = "${path}m/$v.html";
131 $seen->{$anchor_idx} = $anchor;
133 $rv .= " <a\nhref=\"$anchor\">parent</a>";
137 $rv .= " <a\nhref=\"${path}t/$href.html$t_anchor\">" .
141 $rv .= '</pre></td></tr></table>';
145 my (undef, $ctx, $foot, $srch) = @_;
146 my $mid = mid_compressed($ctx->{mid});
147 my $res = $srch->get_thread($mid);
149 my $msgs = load_results($ctx, $res);
150 my $nr = scalar @$msgs;
151 return $rv if $nr == 0;
152 require PublicInbox::Thread;
153 my $th = PublicInbox::Thread->new(@$msgs);
155 $th->order(*PublicInbox::Thread::sort_ts);
156 my $state = [ $srch, { root_anchor => anchor_for($mid) }, undef, 0 ];
157 thread_entry(\$rv, $state, $_, 0) for $th->rootset;
158 my $final_anchor = $state->[3];
159 my $next = "<a\nid=\"s$final_anchor\">";
161 if ($final_anchor == 1) {
162 $next .= 'only message in thread';
164 $next .= 'end of thread';
166 $next .= "</a>, back to <a\nhref=\"../\">index</a>\n";
168 $rv .= "<hr />" . PRE_WRAP . $next . $foot . "</pre>";
171 sub subject_path_html {
172 my (undef, $ctx, $foot, $srch) = @_;
173 my $path = $ctx->{subject_path};
174 my $res = $srch->get_subject_path($path);
176 my $msgs = load_results($ctx, $res);
177 my $nr = scalar @$msgs;
178 return $rv if $nr == 0;
179 require PublicInbox::Thread;
180 my $th = PublicInbox::Thread->new(@$msgs);
182 $th->order(*PublicInbox::Thread::sort_ts);
183 my $state = [ $srch, { root_anchor => 'dummy' }, undef, 0 ];
184 thread_entry(\$rv, $state, $_, 0) for $th->rootset;
185 my $final_anchor = $state->[3];
186 my $next = "<a\nid=\"s$final_anchor\">end of thread</a>\n";
188 $rv .= "<hr />" . PRE_WRAP . $next . $foot . "</pre>";
191 # only private functions below.
194 my ($part, $enc_msg, $part_nr, $fhref, $more) = @_;
196 return $rv if $part->subparts; # walk_parts already recurses
197 my $ct = $part->content_type;
199 # account for filter bugs...
200 return '' if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
202 my $enc = enc_for($ct, $enc_msg);
205 my $fn = $part->filename;
206 defined($fn) or $fn = "part #" . ($part_nr + 1);
207 $rv .= add_filename_line($enc->decode($fn));
210 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
212 # drop the remainder of git patches, they're usually better
213 # to review when the full message is viewed
214 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
217 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
219 # kill any leading or trailing whitespace lines
224 # kill per-line trailing whitespace
225 $s =~ s/[ \t]+$//sgm;
233 my ($ct, $default) = @_;
234 $default ||= $enc_utf8;
235 defined $ct or return $default;
236 my $ct_parsed = parse_content_type($ct);
238 if (my $charset = $ct_parsed->{attributes}->{charset}) {
239 my $enc = find_encoding($charset);
246 sub multipart_text_as_html {
247 my ($mime, $full_pfx, $srch) = @_;
250 my $enc_msg = enc_for($mime->header("Content-Type"));
252 # scan through all parts, looking for displayable text
253 $mime->walk_parts(sub {
255 return if $part->subparts; # walk_parts already recurses
256 my $ct = $part->content_type;
258 # account for filter bugs...
259 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
261 my $enc = enc_for($ct, $enc_msg);
264 my $fn = $part->filename;
265 defined($fn) or $fn = "part #" . ($part_nr + 1);
266 $rv .= add_filename_line($enc->decode($fn));
269 if (defined $full_pfx) {
270 $rv .= add_text_body_short($enc, $part, $part_nr,
273 $rv .= add_text_body_full($enc, $part, $part_nr);
275 $rv .= "\n" unless $rv =~ /\n\z/s;
282 sub add_filename_line {
288 $pad x= ($len/2) if ($len > 0);
289 "$pad " . ascii_html($fn) . " $pad\n";
292 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
295 # no newlines added here since it'd break the splitting we do
297 $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
300 sub add_text_body_short {
301 my ($enc, $part, $part_nr, $full_pfx) = @_;
303 my $s = ascii_html($enc->decode($part->body));
305 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
307 my @lines = split(/\n/, $cur);
308 if (@lines > MAX_INLINE_QUOTED) {
309 # show a short snippet of quoted text
310 $cur = join(' ', @lines);
311 $cur =~ s/^>\s*//;
313 my @sum = split(/\s+/, $cur);
316 my $tmp = shift(@sum);
317 my $len = length($tmp) + length($cur);
318 if ($len > MAX_TRUNC_LEN) {
323 } while (@sum && length($cur) < MAX_TRUNC_LEN);
325 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
326 "\">$cur<\/a>>\n";
334 sub add_text_body_full {
335 my ($enc, $part, $part_nr) = @_;
337 my $s = ascii_html($enc->decode($part->body));
339 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
341 my @lines = split(/\n/, $cur);
342 if (@lines > MAX_INLINE_QUOTED) {
343 "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
351 sub headers_to_html_header {
352 my ($mime, $full_pfx, $srch) = @_;
356 my $header_obj = $mime->header_obj;
357 my $mid = $header_obj->header_raw('Message-ID');
358 $mid = PublicInbox::Hval->new_msgid($mid);
359 my $mid_href = $mid->as_href;
360 foreach my $h (qw(From To Cc Subject Date)) {
361 my $v = $mime->header($h);
362 defined($v) && length($v) or next;
363 $v = PublicInbox::Hval->new_oneline($v);
366 my @from = Email::Address->parse($v->raw);
367 $title[1] = ascii_html($from[0]->name);
368 } elsif ($h eq 'Subject') {
369 $title[0] = $v->as_html;
371 $rv .= "$h: <a\nhref=\"../t/$mid_href.html\">";
372 $rv .= $v->as_html . "</a>\n";
376 $rv .= "$h: " . $v->as_html . "\n";
380 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
381 $mid_href = "../m/$mid_href" unless $full_pfx;
382 $rv .= "(<a\nhref=\"$mid_href.txt\">raw</a>)\n";
384 my $irt = $header_obj->header_raw('In-Reply-To');
386 my $v = PublicInbox::Hval->new_msgid($irt);
387 my $html = $v->as_html;
388 my $href = $v->as_href;
389 $rv .= "In-Reply-To: <";
390 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
393 my $refs = $header_obj->header_raw('References');
395 # avoid redundant URLs wasting bandwidth
397 $seen{mid_clean($irt)} = 1 if defined $irt;
399 my @raw_refs = ($refs =~ /<([^>]+)>/g);
400 foreach my $ref (@raw_refs) {
403 push @refs, linkify_ref($ref);
407 $rv .= 'References: '. join(' ', @refs) . "\n";
413 ("<html><head><title>". join(' - ', @title) .
414 '</title></head><body>' . PRE_WRAP . $rv);
418 my ($mime, $standalone, $full_pfx, $srch) = @_;
419 my %cc; # everyone else
420 my $to; # this is the From address
422 foreach my $h (qw(From To Cc)) {
423 my $v = $mime->header($h);
424 defined($v) && length($v) or next;
425 my @addrs = Email::Address->parse($v);
426 foreach my $recip (@addrs) {
427 my $address = $recip->address;
428 my $dst = lc($address);
429 $cc{$dst} ||= $address;
433 Email::Address->purge_cache if $standalone;
435 my $subj = $mime->header('Subject') || '';
436 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
437 my $mid = $mime->header_obj->header_raw('Message-ID');
438 my $irt = uri_escape_utf8($mid);
440 $to = uri_escape_utf8($to);
441 $subj = uri_escape_utf8($subj);
443 my $cc = uri_escape_utf8(join(',', sort values %cc));
444 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
446 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
448 $irt = $mime->header_obj->header_raw('In-Reply-To') || '';
449 $mid = mid_compressed(mid_clean($mid));
450 my $t_anchor = length $irt ? T_ANCHOR : '';
451 $idx = " <a\nhref=\"../t/$mid.html$t_anchor\">".
452 "threadlink</a>$idx";
453 my $res = $srch->get_followups($mid);
454 if (my $c = $res->{count}) {
455 $c = $c == 1 ? '1 followup' : "$c followups";
457 $res->{srch} = $srch;
458 thread_followups(\$idx, $mime, $res);
460 $idx .= "\n(no followups, yet)\n";
463 $irt = PublicInbox::Hval->new_msgid($irt);
464 $irt = $irt->as_href;
465 $irt = "<a\nhref=\"$irt\">parent</a> ";
467 $irt = ' ' x length('parent ');
473 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
477 my $v = PublicInbox::Hval->new_msgid($_[0]);
478 my $html = $v->as_html;
479 my $href = $v->as_href;
480 "<<a\nhref=\"$href.html\">$html</a>>";
486 if ($id !~ /\A[a-f0-9]{40}\z/) {
487 $id = mid_compressed(mid_clean($id), 1);
493 my ($dst, $root, $node, $level) = @_;
494 # $root = [ Root Message-ID, \%seen, $srch ];
495 if (my $x = $node->message) {
496 my $mid = $x->header('Message-ID');
497 if ($root->[0] ne $mid) {
498 my $pfx = ' ' x $level;
500 my $s = $x->header('Subject');
501 my $h = $root->[2]->subject_path($s);
502 if ($root->[1]->{$h}) {
505 $root->[1]->{$h} = 1;
506 $s = PublicInbox::Hval->new($s);
509 my $m = PublicInbox::Hval->new_msgid($mid);
510 my $f = PublicInbox::Hval->new($x->header('X-PI-From'));
511 my $d = PublicInbox::Hval->new($x->header('X-PI-Date'));
512 $m = $m->as_href . '.html';
514 $d = $d->as_html . ' UTC';
515 if (length($s) == 0) {
516 $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
518 $$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
523 simple_dump($dst, $root, $node->child, $level + 1) if $node->child;
524 simple_dump($dst, $root, $node->next, $level) if $node->next;
527 sub thread_followups {
528 my ($dst, $root, $res) = @_;
529 my @msgs = map { $_->mini_mime } @{$res->{msgs}};
530 require PublicInbox::Thread;
531 $root->header_set('X-PI-TS', '0');
532 my $th = PublicInbox::Thread->new($root, @msgs);
534 $th->order(*PublicInbox::Thread::sort_ts);
535 my $srch = $res->{srch};
536 my $subj = $srch->subject_path($root->header('Subject'));
537 my %seen = ($subj => 1);
538 $root = [ $root->header('Message-ID'), \%seen, $srch ];
539 simple_dump($dst, $root, $_, 0) for $th->rootset;
542 sub thread_html_head {
544 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
546 "<html><head><title>$s</title></head><body>";
550 my ($dst, $state, $node, $level) = @_;
551 # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
552 # $seen is overloaded with 3 types of fields:
553 # 1) "root_anchor" => anchor_for(Message-ID),
554 # 2) seen subject hashes: sha1(subject) => 1
555 # 3) anchors hashes: "#$sha1_hex" (same as $seen in index_entry)
556 if (my $mime = $node->message) {
557 if (length($$dst) == 0) {
558 $$dst .= thread_html_head($mime);
560 $$dst .= index_entry(undef, $mime, $level, $state);
562 thread_entry($dst, $state, $node->child, $level + 1) if $node->child;
563 thread_entry($dst, $state, $node->next, $level) if $node->next;
567 my ($ctx, $res) = @_;
569 require PublicInbox::GitCatFile;
570 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
572 while (my $smsg = shift @{$res->{msgs}}) {
574 my $path = mid2path($m);
576 # FIXME: duplicated code from Feed.pm
578 my $str = $git->cat_file("HEAD:$path");
579 Email::MIME->new($str);
582 $mime->header_set('X-PI-TS', msg_timestamp($mime));
591 my $ts = eval { str2time($mime->header('Date')) };
592 defined($ts) ? $ts : 0;