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;
7 use URI::Escape qw/uri_escape_utf8/;
8 use Encode qw/find_encoding/;
9 use Encode::MIME::Header;
10 use Email::MIME::ContentType qw/parse_content_type/;
13 # TODO: make these constants tunable
14 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
15 use constant MAX_TRUNC_LEN => 72;
16 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
18 *ascii_html = *PublicInbox::Hval::ascii_html;
20 my $enc_utf8 = find_encoding('UTF-8');
21 my $enc_mime = find_encoding('MIME-Header');
25 my ($class, $mime, $full_pfx, $footer) = @_;
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) . $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 $name = anchor_for($mid_raw);
58 $seen->{$name} = "#$name"; # 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 $time = '%H:%M UTC';
73 if ($now > ($ts + (365 * 24 * 60 * 60))) {
74 # doesn't have to be exactly 1 year
75 $fmt = '%Y/%m/%d ' . $time;
76 } elsif ($now > ($ts + (24 * 60 * 60))) {
77 $fmt = '%m/%d ' . $time;
79 $ts = POSIX::strftime($fmt, gmtime($ts));
81 $rv .= "$pfx<a\nname=\"$name\"></a>" .
82 "<a\nname=\"s$midx\"></a>" .
85 $rv .= "- by $from @ $ts - ";
87 $rv .= "<a\nhref=\"#s$next\">next</a>";
89 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
93 my $irp = $header_obj->header_raw('In-Reply-To');
94 my ($anchor_idx, $anchor);
96 $anchor_idx = anchor_for($irp);
97 $anchor = $seen->{$anchor_idx};
99 my $href = $mid->as_href;
100 my $mhref = "m/$href.html";
101 my $fhref = "f/$href.html";
102 my $more = 'message';
103 # scan through all parts, looking for displayable text
104 $mime->walk_parts(sub {
106 return if $part->subparts; # walk_parts already recurses
107 my $ct = $part->content_type;
109 # account for filter bugs...
110 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
112 my $enc = enc_for($ct) || $enc_msg || $enc_utf8;
115 my $fn = $part->filename;
116 defined($fn) or $fn = "part #" . ($part_nr + 1);
117 $rv .= $pfx . add_filename_line($enc->decode($fn));
120 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
122 # drop the remainder of git patches, they're usually better
123 # to review when the full message is viewed
124 $s =~ s!^---+\n.*\z!!ms and $more = 'more...';
127 $s =~ s/^-- \n.*\z//ms and $more = 'more...';
129 # kill any leading or trailing whitespace
142 $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
143 my $txt = "m/$href.txt";
144 $rv .= "<a\nhref=\"$txt\">raw</a> ";
145 $rv .= html_footer($mime, 0);
148 unless (defined $anchor) {
149 my $v = PublicInbox::Hval->new_msgid($irp);
150 my $html = $v->as_html;
151 $anchor = 'm/' . $v->as_href . '.html';
152 $seen->{$anchor_idx} = $anchor;
154 $rv .= " <a\nhref=\"$anchor\">parent</a>";
156 $rv .= " <a\nhref=\"?r=$first#$name\">threadlink</a>";
161 # only private functions below.
165 defined $ct or return $enc_utf8;
166 my $ct_parsed = parse_content_type($ct);
168 if (my $charset = $ct_parsed->{attributes}->{charset}) {
169 my $enc = find_encoding($charset);
176 sub multipart_text_as_html {
177 my ($mime, $full_pfx) = @_;
180 my $enc_msg = enc_for($mime->header("Content-Type"));
182 # scan through all parts, looking for displayable text
183 $mime->walk_parts(sub {
185 return if $part->subparts; # walk_parts already recurses
186 my $ct = $part->content_type;
188 # account for filter bugs...
189 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
191 my $enc = enc_for($ct) || $enc_msg || $enc_utf8;
194 my $fn = $part->filename;
195 defined($fn) or $fn = "part #" . ($part_nr + 1);
196 $rv .= add_filename_line($enc->decode($fn));
199 if (defined $full_pfx) {
200 $rv .= add_text_body_short($enc, $part, $part_nr,
203 $rv .= add_text_body_full($enc, $part, $part_nr);
205 $rv .= "\n" unless $rv =~ /\n\z/s;
211 sub add_filename_line {
217 $pad x= ($len/2) if ($len > 0);
218 "$pad " . ascii_html($fn) . " $pad\n";
221 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
224 # no newlines added here since it'd break the splitting we do
226 $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
229 sub add_text_body_short {
230 my ($enc, $part, $part_nr, $full_pfx) = @_;
232 my $s = ascii_html($enc->decode($part->body));
234 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
236 my @lines = split(/\n/, $cur);
237 if (@lines > MAX_INLINE_QUOTED) {
238 # show a short snippet of quoted text
239 $cur = join(' ', @lines);
240 $cur =~ s/^>\s*//;
242 my @sum = split(/\s+/, $cur);
245 my $tmp = shift(@sum);
246 my $len = length($tmp) + length($cur);
247 if ($len > MAX_TRUNC_LEN) {
252 } while (@sum && length($cur) < MAX_TRUNC_LEN);
254 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
255 "\">$cur<\/a>>\n";
263 sub add_text_body_full {
264 my ($enc, $part, $part_nr) = @_;
266 my $s = ascii_html($enc->decode($part->body));
268 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
270 my @lines = split(/\n/, $cur);
271 if (@lines > MAX_INLINE_QUOTED) {
272 "<a\nname=q${part_nr}_" . $n++ . ">$cur</a>";
280 sub headers_to_html_header {
281 my ($mime, $full_pfx) = @_;
285 foreach my $h (qw(From To Cc Subject Date)) {
286 my $v = $mime->header($h);
287 defined($v) && length($v) or next;
288 $v = PublicInbox::Hval->new_oneline($v);
289 $rv .= "$h: " . $v->as_html . "\n";
292 my @from = Email::Address->parse($v->raw);
294 unless (defined($v) && length($v)) {
295 $v = '<' . $from[0]->address . '>';
297 $title[1] = ascii_html($v);
298 } elsif ($h eq 'Subject') {
299 $title[0] = $v->as_html;
303 my $header_obj = $mime->header_obj;
304 my $mid = $header_obj->header_raw('Message-ID');
306 $mid = PublicInbox::Hval->new_msgid($mid);
307 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
308 my $href = $mid->as_href;
309 $href = "../m/$href" unless $full_pfx;
310 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
313 my $irp = $header_obj->header_raw('In-Reply-To');
315 my $v = PublicInbox::Hval->new_msgid($irp);
316 my $html = $v->as_html;
317 my $href = $v->as_href;
318 $rv .= "In-Reply-To: <";
319 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
322 my $refs = $header_obj->header_raw('References');
324 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
325 my @refs = ($refs =~ /<([^>]+)>/g);
327 $rv .= 'References: '. linkify_refs(@refs) . "\n";
333 ("<html><head><title>". join(' - ', @title) .
334 '</title></head><body>' . PRE_WRAP . $rv);
338 my ($mime, $standalone) = @_;
339 my %cc; # everyone else
340 my $to; # this is the From address
342 foreach my $h (qw(From To Cc)) {
343 my $v = $mime->header($h);
344 defined($v) && length($v) or next;
345 my @addrs = Email::Address->parse($v);
346 foreach my $recip (@addrs) {
347 my $address = $recip->address;
348 my $dst = lc($address);
349 $cc{$dst} ||= $address;
353 Email::Address->purge_cache if $standalone;
355 my $subj = $mime->header('Subject') || '';
356 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
357 my $irp = uri_escape_utf8(
358 $mime->header_obj->header_raw('Message-ID') || '');
360 $to = uri_escape_utf8($to);
361 $subj = uri_escape_utf8($subj);
363 my $cc = uri_escape_utf8(join(',', values %cc));
364 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
366 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
368 "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
373 my $v = PublicInbox::Hval->new_msgid($_);
374 my $html = $v->as_html;
375 my $href = $v->as_href;
376 "<<a\nhref=\"$href.html\">$html</a>>";
383 $msgid =~ s/\A\s*<?//;
384 $msgid =~ s/>?\s*\z//;
385 Digest::SHA::sha1_hex($msgid);