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 (undef, $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';
78 $subj = "<u\nid=\"u\">$subj</u>" if $root_anchor eq $id;
83 my $ts = $mime->header('X-PI-TS');
84 my $fmt = '%Y-%m-%d %H:%M UTC';
85 $ts = POSIX::strftime($fmt, gmtime($ts));
87 $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
88 $rv .= "- by $from @ $ts - ";
89 $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
91 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
95 my $irt = $header_obj->header_raw('In-Reply-To');
96 my ($anchor_idx, $anchor, $t_anchor);
98 $anchor_idx = anchor_for($irt);
99 $anchor = $seen->{$anchor_idx};
100 $t_anchor = T_ANCHOR;
104 my $href = $mid->as_href;
105 my $mhref = "${path}m/$href.html";
106 my $fhref = "${path}f/$href.html";
107 # scan through all parts, looking for displayable text
108 $mime->walk_parts(sub {
109 $rv .= index_walk($_[0], $pfx, $enc_msg, $part_nr, $fhref,
114 $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
115 my $txt = "${path}m/$href.txt";
116 $rv .= "<a\nhref=\"$txt\">raw</a> ";
117 $rv .= html_footer($mime, 0);
120 unless (defined $anchor) {
121 my $v = PublicInbox::Hval->new_msgid($irt);
123 $anchor = "${path}m/$v.html";
124 $seen->{$anchor_idx} = $anchor;
126 $rv .= " <a\nhref=\"$anchor\">parent</a>";
130 $rv .= " <a\nhref=\"t/$href.html$t_anchor\">thread</a>";
137 my (undef, $ctx, $foot, $srch) = @_;
138 my $mid = mid_compressed($ctx->{mid});
139 my $res = $srch->get_thread($mid);
141 my $msgs = load_results($ctx, $res);
142 my $nr = scalar @$msgs;
143 return $rv if $nr == 0;
144 require PublicInbox::Thread;
145 my $th = PublicInbox::Thread->new(@$msgs);
147 $th->order(*PublicInbox::Thread::sort_ts);
148 my $state = [ undef, { root_anchor => anchor_for($mid) }, undef, 0 ];
149 thread_entry(\$rv, $state, $_, 0) for $th->rootset;
150 my $final_anchor = $state->[3];
151 my $next = "<a\nid=\"s$final_anchor\">end of thread</a>\n";
153 $rv .= "</pre><hr />" . PRE_WRAP . $next . $foot . "</pre>";
156 sub subject_path_html {
157 my (undef, $ctx, $foot, $srch) = @_;
158 my $path = $ctx->{subject_path};
159 my $res = $srch->get_subject_path($path);
161 my $msgs = load_results($ctx, $res);
162 my $nr = scalar @$msgs;
163 return $rv if $nr == 0;
164 require PublicInbox::Thread;
165 my $th = PublicInbox::Thread->new(@$msgs);
167 $th->order(*PublicInbox::Thread::sort_ts);
168 my $state = [ undef, { root_anchor => 'dummy' }, undef, 0 ];
169 thread_entry(\$rv, $state, $_, 0) for $th->rootset;
170 my $final_anchor = $state->[3];
171 my $next = "<a\nid=\"s$final_anchor\">end of thread</a>\n";
173 $rv .= "</pre><hr />" . PRE_WRAP . $next . $foot . "</pre>";
176 # only private functions below.
179 my ($part, $pfx, $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 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
187 my $enc = enc_for($ct, $enc_msg);
190 my $fn = $part->filename;
191 defined($fn) or $fn = "part #" . ($part_nr + 1);
192 $rv .= $pfx . add_filename_line($enc->decode($fn));
195 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
197 # drop the remainder of git patches, they're usually better
198 # to review when the full message is viewed
199 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
202 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
204 # kill any leading or trailing whitespace lines
212 # kill per-line trailing whitespace
213 $s =~ s/[ \t]+$//sgm;
221 my ($ct, $default) = @_;
222 $default ||= $enc_utf8;
223 defined $ct or return $default;
224 my $ct_parsed = parse_content_type($ct);
226 if (my $charset = $ct_parsed->{attributes}->{charset}) {
227 my $enc = find_encoding($charset);
234 sub multipart_text_as_html {
235 my ($mime, $full_pfx, $srch) = @_;
238 my $enc_msg = enc_for($mime->header("Content-Type"));
240 # scan through all parts, looking for displayable text
241 $mime->walk_parts(sub {
243 return if $part->subparts; # walk_parts already recurses
244 my $ct = $part->content_type;
246 # account for filter bugs...
247 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
249 my $enc = enc_for($ct, $enc_msg);
252 my $fn = $part->filename;
253 defined($fn) or $fn = "part #" . ($part_nr + 1);
254 $rv .= add_filename_line($enc->decode($fn));
257 if (defined $full_pfx) {
258 $rv .= add_text_body_short($enc, $part, $part_nr,
261 $rv .= add_text_body_full($enc, $part, $part_nr);
263 $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 href="$1">$1</a>!g;
287 sub add_text_body_short {
288 my ($enc, $part, $part_nr, $full_pfx) = @_;
290 my $s = ascii_html($enc->decode($part->body));
292 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
294 my @lines = split(/\n/, $cur);
295 if (@lines > MAX_INLINE_QUOTED) {
296 # show a short snippet of quoted text
297 $cur = join(' ', @lines);
298 $cur =~ s/^>\s*//;
300 my @sum = split(/\s+/, $cur);
303 my $tmp = shift(@sum);
304 my $len = length($tmp) + length($cur);
305 if ($len > MAX_TRUNC_LEN) {
310 } while (@sum && length($cur) < MAX_TRUNC_LEN);
312 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
313 "\">$cur<\/a>>\n";
321 sub add_text_body_full {
322 my ($enc, $part, $part_nr) = @_;
324 my $s = ascii_html($enc->decode($part->body));
326 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
328 my @lines = split(/\n/, $cur);
329 if (@lines > MAX_INLINE_QUOTED) {
330 "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
338 sub headers_to_html_header {
339 my ($mime, $full_pfx, $srch) = @_;
343 foreach my $h (qw(From To Cc Subject Date)) {
344 my $v = $mime->header($h);
345 defined($v) && length($v) or next;
346 $v = PublicInbox::Hval->new_oneline($v);
349 my @from = Email::Address->parse($v->raw);
350 $title[1] = ascii_html($from[0]->name);
351 } elsif ($h eq 'Subject') {
352 $title[0] = $v->as_html;
354 my $path = $srch->subject_path($v->raw);
355 $rv .= "$h: <a\nhref=\"../s/$path.html\">";
356 $rv .= $v->as_html . "</a>\n";
360 $rv .= "$h: " . $v->as_html . "\n";
364 my $header_obj = $mime->header_obj;
365 my $mid = $header_obj->header_raw('Message-ID');
366 $mid = PublicInbox::Hval->new_msgid($mid);
367 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
368 my $href = $mid->as_href;
369 $href = "../m/$href" unless $full_pfx;
370 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
372 my $irt = $header_obj->header_raw('In-Reply-To');
374 my $v = PublicInbox::Hval->new_msgid($irt);
375 my $html = $v->as_html;
376 my $href = $v->as_href;
377 $rv .= "In-Reply-To: <";
378 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
381 my $refs = $header_obj->header_raw('References');
383 $refs =~ s/\s*\Q$irt\E\s*// if (defined $irt);
384 my @refs = ($refs =~ /<([^>]+)>/g);
386 $rv .= 'References: '. linkify_refs(@refs) . "\n";
392 ("<html><head><title>". join(' - ', @title) .
393 '</title></head><body>' . PRE_WRAP . $rv);
397 my ($mime, $standalone, $full_pfx, $srch) = @_;
398 my %cc; # everyone else
399 my $to; # this is the From address
401 foreach my $h (qw(From To Cc)) {
402 my $v = $mime->header($h);
403 defined($v) && length($v) or next;
404 my @addrs = Email::Address->parse($v);
405 foreach my $recip (@addrs) {
406 my $address = $recip->address;
407 my $dst = lc($address);
408 $cc{$dst} ||= $address;
412 Email::Address->purge_cache if $standalone;
414 my $subj = $mime->header('Subject') || '';
415 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
416 my $mid = $mime->header_obj->header_raw('Message-ID');
417 my $irt = uri_escape_utf8($mid);
419 $to = uri_escape_utf8($to);
420 $subj = uri_escape_utf8($subj);
422 my $cc = uri_escape_utf8(join(',', sort values %cc));
423 my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
425 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
427 $irt = $mime->header_obj->header_raw('In-Reply-To') || '';
428 $mid = mid_compressed(mid_clean($mid));
429 my $t_anchor = length $irt ? T_ANCHOR : '';
430 $idx = " <a\nhref=\"../t/$mid.html$t_anchor\">thread</a>$idx";
431 my $res = $srch->get_replies($mid);
432 if (my $c = $res->{count}) {
433 $c = $c == 1 ? '1 reply' : "$c replies";
435 thread_replies(\$idx, $mime, $res);
437 $idx .= "\n(no replies yet)\n";
440 $irt = PublicInbox::Hval->new_msgid($irt);
441 $irt = $irt->as_href;
442 $irt = "<a\nhref=\"$irt\">parent</a> ";
444 $irt = ' ' x length('parent ');
450 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
455 my $v = PublicInbox::Hval->new_msgid($_);
456 my $html = $v->as_html;
457 my $href = $v->as_href;
458 "<<a\nhref=\"$href.html\">$html</a>>";
464 'm' . mid_compressed(mid_clean($msgid));
468 my ($dst, $root, $node, $level) = @_;
469 my $pfx = ' ' x $level;
471 if (my $x = $node->message) {
472 my $mid = $x->header('Message-ID');
473 if ($root->[0] ne $mid) {
474 my $s = $x->header('Subject');
475 my $h = hash_subj($s);
476 if ($root->[1]->{$h}) {
479 $root->[1]->{$h} = 1;
480 $s = PublicInbox::Hval->new($s);
483 my $m = PublicInbox::Hval->new_msgid($mid);
484 my $f = PublicInbox::Hval->new($x->header('X-PI-From'));
485 my $d = PublicInbox::Hval->new($x->header('X-PI-Date'));
486 $m = $m->as_href . '.html';
488 $d = $d->as_html . ' UTC';
489 if (length($s) == 0) {
490 $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
492 $$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
497 simple_dump($dst, $root, $node->child, $level + 1) if $node->child;
498 simple_dump($dst, $root, $node->next, $level) if $node->next;
505 $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
507 Digest::SHA::sha1($subj);
511 my ($dst, $root, $res) = @_;
512 my @msgs = map { $_->mini_mime } @{$res->{msgs}};
513 foreach (@{$res->{msgs}}) {
514 print STDERR "smsg->path: <", $_->path, ">\n";
516 require PublicInbox::Thread;
517 $root->header_set('X-PI-TS', '0');
518 my $th = PublicInbox::Thread->new($root, @msgs);
520 $th->order(*PublicInbox::Thread::sort_ts);
521 $root = [ $root->header('Message-ID'),
522 { hash_subj($root->header('Subject')) => 1 } ];
523 simple_dump($dst, $root, $_, 0) for $th->rootset;
526 sub thread_html_head {
528 my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
530 "<html><head><title>$s</title></head><body>" . PRE_WRAP
535 my ($dst, $state, $node, $level) = @_;
536 # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
537 # $seen is overloaded with 3 types of fields:
538 # 1) "root_anchor" => anchor_for(Message-ID),
539 # 2) seen subject hashes: sha1(subject) => 1
540 # 3) anchors hashes: "#$sha1_hex" (same as $seen in index_entry)
541 if (my $mime = $node->message) {
542 if (length($$dst) == 0) {
543 $$dst .= thread_html_head($mime);
545 $$dst .= index_entry(undef, $mime, $level, $state);
547 thread_entry($dst, $state, $node->child, $level + 1) if $node->child;
548 thread_entry($dst, $state, $node->next, $level) if $node->next;
552 my ($ctx, $res) = @_;
554 require PublicInbox::GitCatFile;
555 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
557 while (my $smsg = shift @{$res->{msgs}}) {
559 my $path = mid2path($m);
561 # FIXME: duplicated code from Feed.pm
563 my $str = $git->cat_file("HEAD:$path");
564 Email::MIME->new($str);
567 my $t = eval { str2time($mime->header('Date')) };
568 defined($t) or $t = 0;
569 $mime->header_set('X-PI-TS', $t);