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 $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
110 my $fn = $part->filename;
111 defined($fn) or $fn = "part #" . ($part_nr + 1);
112 $rv .= $pfx . add_filename_line($enc->decode($fn));
115 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
117 # drop the remainder of git patches, they're usually better
118 # to review when the full message is viewed
119 $s =~ s!^---+\n.*\z!!ms and $more = 'more...';
122 $s =~ s/^-- \n.*\z//ms and $more = 'more...';
124 # kill any leading or trailing whitespace
137 $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
138 my $txt = "m/$href.txt";
139 $rv .= "<a\nhref=\"$txt\">raw</a> ";
140 $rv .= html_footer($mime, 0);
143 unless (defined $anchor) {
144 my $v = PublicInbox::Hval->new_msgid($irp);
145 my $html = $v->as_html;
146 $anchor = 'm/' . $v->as_href . '.html';
147 $seen->{$anchor_idx} = $anchor;
149 $rv .= " <a\nhref=\"$anchor\">parent</a>";
151 $rv .= " <a\nhref=\"?r=$first#$name\">threadlink</a>";
156 # only private functions below.
160 defined $ct or return $enc_utf8;
161 my $ct_parsed = parse_content_type($ct);
163 if (my $charset = $ct_parsed->{attributes}->{charset}) {
164 my $enc = find_encoding($charset);
171 sub multipart_text_as_html {
172 my ($mime, $full_pfx) = @_;
175 my $enc_msg = enc_for($mime->header("Content-Type"));
177 # scan through all parts, looking for displayable text
178 $mime->walk_parts(sub {
180 return if $part->subparts; # walk_parts already recurses
181 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
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 sub add_text_body_short {
212 my ($enc, $part, $part_nr, $full_pfx) = @_;
214 my $s = ascii_html($enc->decode($part->body));
215 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
217 my @lines = split(/\n/, $cur);
218 if (@lines > MAX_INLINE_QUOTED) {
219 # show a short snippet of quoted text
220 $cur = join(' ', @lines);
221 $cur =~ s/^>\s*//;
223 my @sum = split(/\s+/, $cur);
226 my $tmp = shift(@sum);
227 my $len = length($tmp) + length($cur);
228 if ($len > MAX_TRUNC_LEN) {
233 } while (@sum && length($cur) < MAX_TRUNC_LEN);
235 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
236 "\">$cur<\/a>>\n";
244 sub add_text_body_full {
245 my ($enc, $part, $part_nr) = @_;
247 my $s = ascii_html($enc->decode($part->body));
248 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
250 my @lines = split(/\n/, $cur);
251 if (@lines > MAX_INLINE_QUOTED) {
252 "<a\nname=q${part_nr}_" . $n++ . ">$cur</a>";
260 sub headers_to_html_header {
261 my ($mime, $full_pfx) = @_;
265 foreach my $h (qw(From To Cc Subject Date)) {
266 my $v = $mime->header($h);
267 defined($v) && length($v) or next;
268 $v = PublicInbox::Hval->new_oneline($v);
269 $rv .= "$h: " . $v->as_html . "\n";
272 my @from = Email::Address->parse($v->raw);
274 unless (defined($v) && length($v)) {
275 $v = '<' . $from[0]->address . '>';
277 $title[1] = ascii_html($v);
278 } elsif ($h eq 'Subject') {
279 $title[0] = $v->as_html;
283 my $header_obj = $mime->header_obj;
284 my $mid = $header_obj->header_raw('Message-ID');
286 $mid = PublicInbox::Hval->new_msgid($mid);
287 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
288 my $href = $mid->as_href;
289 $href = "../m/$href" unless $full_pfx;
290 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
293 my $irp = $header_obj->header_raw('In-Reply-To');
295 my $v = PublicInbox::Hval->new_msgid($irp);
296 my $html = $v->as_html;
297 my $href = $v->as_href;
298 $rv .= "In-Reply-To: <";
299 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
302 my $refs = $header_obj->header_raw('References');
304 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
305 my @refs = ($refs =~ /<([^>]+)>/g);
307 $rv .= 'References: '. linkify_refs(@refs) . "\n";
313 ("<html><head><title>". join(' - ', @title) .
314 '</title></head><body>' . PRE_WRAP . $rv);
318 my ($mime, $standalone) = @_;
319 my %cc; # everyone else
320 my $to; # this is the From address
322 foreach my $h (qw(From To Cc)) {
323 my $v = $mime->header($h);
324 defined($v) && length($v) or next;
325 my @addrs = Email::Address->parse($v);
326 foreach my $recip (@addrs) {
327 my $address = $recip->address;
328 my $dst = lc($address);
329 $cc{$dst} ||= $address;
333 Email::Address->purge_cache if $standalone;
335 my $subj = $mime->header('Subject') || '';
336 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
337 my $irp = uri_escape_utf8(
338 $mime->header_obj->header_raw('Message-ID') || '');
340 $to = uri_escape_utf8($to);
341 $subj = uri_escape_utf8($subj);
343 my $cc = uri_escape_utf8(join(',', values %cc));
344 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
346 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
348 "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
353 my $v = PublicInbox::Hval->new_msgid($_);
354 my $html = $v->as_html;
355 my $href = $v->as_href;
356 "<<a\nhref=\"$href.html\">$html</a>>";
363 $msgid =~ s/\A\s*<?//;
364 $msgid =~ s/>?\s*\z//;
365 Digest::SHA::sha1_hex($msgid);