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 Encode qw/find_encoding/;
8 use Encode::MIME::Header;
9 use Email::MIME::ContentType qw/parse_content_type/;
10 use PublicInbox::Hval;
11 use PublicInbox::MID qw/mid_clean mid_compressed/;
14 # TODO: make these constants tunable
15 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
16 use constant MAX_TRUNC_LEN => 72;
17 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
19 *ascii_html = *PublicInbox::Hval::ascii_html;
21 my $enc_utf8 = find_encoding('UTF-8');
25 my ($class, $mime, $full_pfx, $footer, $srch) = @_;
26 if (defined $footer) {
27 $footer = "\n" . $footer;
31 headers_to_html_header($mime, $full_pfx) .
32 multipart_text_as_html($mime, $full_pfx) .
33 '</pre><hr />' . PRE_WRAP .
34 html_footer($mime, 1, $full_pfx, $srch) . $footer .
35 '</pre></body></html>';
39 my ($class, $mime, $full_pfx) = @_;
41 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
44 # this is already inside a <pre>
46 my ($class, $mime, $level, $state) = @_;
47 my ($now, $seen, $first) = @$state;
48 my $midx = $state->[3]++;
49 my ($prev, $next) = ($midx - 1, $midx + 1);
52 my $enc_msg = enc_for($mime->header("Content-Type"));
53 my $subj = $mime->header('Subject');
54 my $header_obj = $mime->header_obj;
56 my $mid_raw = $header_obj->header_raw('Message-ID');
57 my $id = anchor_for($mid_raw);
58 $seen->{$id} = "#$id"; # save the anchor for later
60 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
61 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
62 my @from = Email::Address->parse($from);
63 $from = $from[0]->name;
64 (defined($from) && length($from)) or $from = $from[0]->address;
66 $from = PublicInbox::Hval->new_oneline($from)->as_html;
67 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
68 my $pfx = (' ' x $level);
70 my $ts = $mime->header('X-PI-Date');
71 my $fmt = '%Y-%m-%d %H:%M UTC';
72 $ts = POSIX::strftime($fmt, gmtime($ts));
74 $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
75 $rv .= "- by $from @ $ts - ";
76 $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
78 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
82 my $irp = $header_obj->header_raw('In-Reply-To');
83 my ($anchor_idx, $anchor);
85 $anchor_idx = anchor_for($irp);
86 $anchor = $seen->{$anchor_idx};
88 my $href = $mid->as_href;
89 my $mhref = "m/$href.html";
90 my $fhref = "f/$href.html";
92 # scan through all parts, looking for displayable text
93 $mime->walk_parts(sub {
95 return if $part->subparts; # walk_parts already recurses
96 my $ct = $part->content_type;
98 # account for filter bugs...
99 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
101 my $enc = enc_for($ct, $enc_msg);
104 my $fn = $part->filename;
105 defined($fn) or $fn = "part #" . ($part_nr + 1);
106 $rv .= $pfx . add_filename_line($enc->decode($fn));
109 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
111 # drop the remainder of git patches, they're usually better
112 # to review when the full message is viewed
113 $s =~ s!^---+\n.*\z!!ms and $more = 'more...';
116 $s =~ s/^-- \n.*\z//ms and $more = 'more...';
118 # kill any leading or trailing whitespace
131 $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
132 my $txt = "m/$href.txt";
133 $rv .= "<a\nhref=\"$txt\">raw</a> ";
134 $rv .= html_footer($mime, 0);
137 unless (defined $anchor) {
138 my $v = PublicInbox::Hval->new_msgid($irp);
139 my $html = $v->as_html;
140 $anchor = 'm/' . $v->as_href . '.html';
141 $seen->{$anchor_idx} = $anchor;
143 $rv .= " <a\nhref=\"$anchor\">parent</a>";
145 $rv .= " <a\nhref=\"?r=$first#$id\">threadlink</a>";
150 # only private functions below.
153 my ($ct, $default) = @_;
154 $default ||= $enc_utf8;
155 defined $ct or return $default;
156 my $ct_parsed = parse_content_type($ct);
158 if (my $charset = $ct_parsed->{attributes}->{charset}) {
159 my $enc = find_encoding($charset);
166 sub multipart_text_as_html {
167 my ($mime, $full_pfx) = @_;
170 my $enc_msg = enc_for($mime->header("Content-Type"));
172 # scan through all parts, looking for displayable text
173 $mime->walk_parts(sub {
175 return if $part->subparts; # walk_parts already recurses
176 my $ct = $part->content_type;
178 # account for filter bugs...
179 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
181 my $enc = enc_for($ct, $enc_msg);
184 my $fn = $part->filename;
185 defined($fn) or $fn = "part #" . ($part_nr + 1);
186 $rv .= add_filename_line($enc->decode($fn));
189 if (defined $full_pfx) {
190 $rv .= add_text_body_short($enc, $part, $part_nr,
193 $rv .= add_text_body_full($enc, $part, $part_nr);
195 $rv .= "\n" unless $rv =~ /\n\z/s;
201 sub add_filename_line {
207 $pad x= ($len/2) if ($len > 0);
208 "$pad " . ascii_html($fn) . " $pad\n";
211 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
214 # no newlines added here since it'd break the splitting we do
216 $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
219 sub add_text_body_short {
220 my ($enc, $part, $part_nr, $full_pfx) = @_;
222 my $s = ascii_html($enc->decode($part->body));
224 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
226 my @lines = split(/\n/, $cur);
227 if (@lines > MAX_INLINE_QUOTED) {
228 # show a short snippet of quoted text
229 $cur = join(' ', @lines);
230 $cur =~ s/^>\s*//;
232 my @sum = split(/\s+/, $cur);
235 my $tmp = shift(@sum);
236 my $len = length($tmp) + length($cur);
237 if ($len > MAX_TRUNC_LEN) {
242 } while (@sum && length($cur) < MAX_TRUNC_LEN);
244 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
245 "\">$cur<\/a>>\n";
253 sub add_text_body_full {
254 my ($enc, $part, $part_nr) = @_;
256 my $s = ascii_html($enc->decode($part->body));
258 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
260 my @lines = split(/\n/, $cur);
261 if (@lines > MAX_INLINE_QUOTED) {
262 "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
270 sub headers_to_html_header {
271 my ($mime, $full_pfx) = @_;
275 foreach my $h (qw(From To Cc Subject Date)) {
276 my $v = $mime->header($h);
277 defined($v) && length($v) or next;
278 $v = PublicInbox::Hval->new_oneline($v);
279 $rv .= "$h: " . $v->as_html . "\n";
282 my @from = Email::Address->parse($v->raw);
284 unless (defined($v) && length($v)) {
285 $v = '<' . $from[0]->address . '>';
287 $title[1] = ascii_html($v);
288 } elsif ($h eq 'Subject') {
289 $title[0] = $v->as_html;
293 my $header_obj = $mime->header_obj;
294 my $mid = $header_obj->header_raw('Message-ID');
296 $mid = PublicInbox::Hval->new_msgid($mid);
297 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
298 my $href = $mid->as_href;
299 $href = "../m/$href" unless $full_pfx;
300 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
303 my $irp = $header_obj->header_raw('In-Reply-To');
305 my $v = PublicInbox::Hval->new_msgid($irp);
306 my $html = $v->as_html;
307 my $href = $v->as_href;
308 $rv .= "In-Reply-To: <";
309 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
312 my $refs = $header_obj->header_raw('References');
314 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
315 my @refs = ($refs =~ /<([^>]+)>/g);
317 $rv .= 'References: '. linkify_refs(@refs) . "\n";
323 ("<html><head><title>". join(' - ', @title) .
324 '</title></head><body>' . PRE_WRAP . $rv);
328 my ($mime, $standalone, $full_pfx, $srch) = @_;
329 my %cc; # everyone else
330 my $to; # this is the From address
332 foreach my $h (qw(From To Cc)) {
333 my $v = $mime->header($h);
334 defined($v) && length($v) or next;
335 my @addrs = Email::Address->parse($v);
336 foreach my $recip (@addrs) {
337 my $address = $recip->address;
338 my $dst = lc($address);
339 $cc{$dst} ||= $address;
343 Email::Address->purge_cache if $standalone;
345 my $subj = $mime->header('Subject') || '';
346 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
347 my $mid = $mime->header_obj->header_raw('Message-ID');
348 my $irp = uri_escape_utf8($mid);
350 $to = uri_escape_utf8($to);
351 $subj = uri_escape_utf8($subj);
353 my $cc = uri_escape_utf8(join(',', sort values %cc));
354 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
357 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
359 my $res = $srch->get_replies($mid);
360 if (my $c = $res->{count}) {
361 $c = $c == 1 ? '1 reply' : "$c replies";
363 thread_replies(\$idx, $mime, $res);
365 $idx .= "\n(no replies yet)\n";
367 $irt = $mime->header_obj->header_raw('In-Reply-To');
369 $irt = PublicInbox::Hval->new_msgid($irt);
370 $irt = $irt->as_href;
371 $irt = "<a\nhref=\"$irt\">parent</a> ";
373 $irt = ' ' x length('parent ');
377 "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
382 my $v = PublicInbox::Hval->new_msgid($_);
383 my $html = $v->as_html;
384 my $href = $v->as_href;
385 "<<a\nhref=\"$href.html\">$html</a>>";
391 'm' . mid_compressed(mid_clean($msgid));
394 # children are chronological
395 sub simple_sort_children {
397 (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
398 (eval { $b->topmost->message->header('X-PI-TS') } || 0)
403 my ($dst, $root, $node, $level) = @_;
404 $$dst .= ' ' x $level;
405 if (my $x = $node->message) {
406 my $mid = $x->header('Message-ID');
407 if ($root->[0] ne $mid) {
408 my $s = clean_subj($x->header('Subject'));
409 if ($root->[1] eq $s) {
412 $s = PublicInbox::Hval->new($s);
413 $s = $s->as_html . ' ';
415 my $m = PublicInbox::Hval->new_msgid($mid);
416 my $f = PublicInbox::Hval->new($x->header('X-PI-From'));
417 my $d = PublicInbox::Hval->new($x->header('X-PI-Date'));
418 $m = $m->as_href . '.html';
421 $$dst .= "` <a\nhref=\"$m\">$s$f @ $d UTC</a>\n";
424 simple_dump($dst, $root, $node->child, $level + 1) if $node->child;
425 simple_dump($dst, $root, $node->next, $level) if $node->next;
432 $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
438 my ($dst, $root, $res) = @_;
439 my @msgs = map { $_->mini_mime } @{$res->{msgs}};
440 require PublicInbox::Thread;
441 $root->header_set('X-PI-TS', '0');
442 my $th = PublicInbox::Thread->new($root, @msgs);
444 $th->order(sub { simple_sort_children(@_) });
445 $root = [ $root->header('Message-ID'),
446 clean_subj($root->header('Subject')) ];
447 simple_dump($dst, $root, $_, 0) for $th->rootset;