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;
50 my $enc_msg = enc_for($mime->header("Content-Type"));
51 my $subj = $mime->header('Subject');
52 my $header_obj = $mime->header_obj;
54 my $mid_raw = $header_obj->header_raw('Message-ID');
55 my $name = anchor_for($mid_raw);
56 $seen->{$name} = "#$name"; # save the anchor for later
58 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
59 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
60 my @from = Email::Address->parse($from);
61 $from = $from[0]->name;
62 (defined($from) && length($from)) or $from = $from[0]->address;
64 $from = PublicInbox::Hval->new_oneline($from)->as_html;
65 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
66 my $pfx = (' ' x $level);
68 my $ts = $mime->header('X-PI-Date');
70 if ($now > ($ts + (365 * 24 * 60 * 60))) {
71 # doesn't have to be exactly 1 year
73 } elsif ($now > ($ts + (24 * 60 * 60))) {
76 $ts = POSIX::strftime($fmt, gmtime($ts));
78 $rv .= "$pfx<a\nname=\"$name\"><b>$subj</b> $from - $ts</a>\n\n";
80 my $irp = $header_obj->header_raw('In-Reply-To');
81 my ($anchor_idx, $anchor);
83 $anchor_idx = anchor_for($irp);
84 $anchor = $seen->{$anchor_idx};
86 my $href = $mid->as_href;
87 my $mhref = "m/$href.html";
88 my $fhref = "f/$href.html";
90 # scan through all parts, looking for displayable text
91 $mime->walk_parts(sub {
93 return if $part->subparts; # walk_parts already recurses
94 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
97 my $fn = $part->filename;
98 defined($fn) or $fn = "part #" . ($part_nr + 1);
99 $rv .= $pfx . add_filename_line($enc->decode($fn));
102 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
104 # keep signatures for now? They shold usually be short,
105 # and sometimes footnotes/"P.S." appear there.
107 # drop the remainder of git patches, they're usually better
108 # to review when the full message is viewed
109 $s =~ s!^---\n.*\z!!ms and $more = 'more...';
111 # kill any leading or trailing whitespace
124 $rv .= "$pfx<a\nhref=\"$mhref\">$more</a> ";
125 my $txt = "m/$href.txt";
126 $rv .= "<a\nhref=\"$txt\">raw</a> ";
127 $rv .= html_footer($mime, 0);
130 unless (defined $anchor) {
131 my $v = PublicInbox::Hval->new_msgid($irp);
132 my $html = $v->as_html;
133 $anchor = 'm/' . $v->as_href . '.html';
134 $seen->{$anchor_idx} = $anchor;
136 $rv .= " <a\nhref=\"$anchor\">parent</a>";
138 $rv .= " <a\nhref=\"?r=$first#$name\">permalink</a>";
143 # only private functions below.
147 defined $ct or return $enc_utf8;
148 my $ct_parsed = parse_content_type($ct);
150 if (my $charset = $ct_parsed->{attributes}->{charset}) {
151 my $enc = find_encoding($charset);
158 sub multipart_text_as_html {
159 my ($mime, $full_pfx) = @_;
162 my $enc_msg = enc_for($mime->header("Content-Type"));
164 # scan through all parts, looking for displayable text
165 $mime->walk_parts(sub {
167 return if $part->subparts; # walk_parts already recurses
168 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
171 my $fn = $part->filename;
172 defined($fn) or $fn = "part #" . ($part_nr + 1);
173 $rv .= add_filename_line($enc->decode($fn));
176 if (defined $full_pfx) {
177 $rv .= add_text_body_short($enc, $part, $part_nr,
180 $rv .= add_text_body_full($enc, $part, $part_nr);
182 $rv .= "\n" unless $rv =~ /\n\z/s;
188 sub add_filename_line {
194 $pad x= ($len/2) if ($len > 0);
195 "$pad " . ascii_html($fn) . " $pad\n";
198 sub add_text_body_short {
199 my ($enc, $part, $part_nr, $full_pfx) = @_;
201 my $s = ascii_html($enc->decode($part->body));
202 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
204 my @lines = split(/\n/, $cur);
205 if (@lines > MAX_INLINE_QUOTED) {
206 # show a short snippet of quoted text
207 $cur = join(' ', @lines);
208 $cur =~ s/^>\s*//;
210 my @sum = split(/\s+/, $cur);
213 my $tmp = shift(@sum);
214 my $len = length($tmp) + length($cur);
215 if ($len > MAX_TRUNC_LEN) {
220 } while (@sum && length($cur) < MAX_TRUNC_LEN);
222 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
223 "\">$cur<\/a>>\n";
231 sub add_text_body_full {
232 my ($enc, $part, $part_nr) = @_;
234 my $s = ascii_html($enc->decode($part->body));
235 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
237 my @lines = split(/\n/, $cur);
238 if (@lines > MAX_INLINE_QUOTED) {
239 "<a\nname=q${part_nr}_" . $n++ . ">$cur</a>";
247 sub headers_to_html_header {
248 my ($mime, $full_pfx) = @_;
252 foreach my $h (qw(From To Cc Subject Date)) {
253 my $v = $mime->header($h);
254 defined($v) && length($v) or next;
255 $v = PublicInbox::Hval->new_oneline($v);
256 $rv .= "$h: " . $v->as_html . "\n";
259 my @from = Email::Address->parse($v->raw);
261 unless (defined($v) && length($v)) {
262 $v = '<' . $from[0]->address . '>';
264 $title[1] = ascii_html($v);
265 } elsif ($h eq 'Subject') {
266 $title[0] = $v->as_html;
270 my $header_obj = $mime->header_obj;
271 my $mid = $header_obj->header_raw('Message-ID');
273 $mid = PublicInbox::Hval->new_msgid($mid);
274 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
275 my $href = $mid->as_href;
276 $href = "../m/$href" unless $full_pfx;
277 $rv .= "(<a\nhref=\"$href.txt\">original</a>)\n";
280 my $irp = $header_obj->header_raw('In-Reply-To');
282 my $v = PublicInbox::Hval->new_msgid($irp);
283 my $html = $v->as_html;
284 my $href = $v->as_href;
285 $rv .= "In-Reply-To: <";
286 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
289 my $refs = $header_obj->header_raw('References');
291 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
292 my @refs = ($refs =~ /<([^>]+)>/g);
294 $rv .= 'References: '. linkify_refs(@refs) . "\n";
300 ("<html><head><title>". join(' - ', @title) .
301 '</title></head><body>' . PRE_WRAP . $rv);
305 my ($mime, $standalone) = @_;
306 my %cc; # everyone else
307 my $to; # this is the From address
309 foreach my $h (qw(From To Cc)) {
310 my $v = $mime->header($h);
311 defined($v) && length($v) or next;
312 my @addrs = Email::Address->parse($v);
313 foreach my $recip (@addrs) {
314 my $address = $recip->address;
315 my $dst = lc($address);
316 $cc{$dst} ||= $address;
320 Email::Address->purge_cache if $standalone;
322 my $subj = $mime->header('Subject') || '';
323 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
324 my $irp = uri_escape_utf8(
325 $mime->header_obj->header_raw('Message-ID') || '');
327 $to = uri_escape_utf8($to);
328 $subj = uri_escape_utf8($subj);
330 my $cc = uri_escape_utf8(join(',', values %cc));
331 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
333 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
335 "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
340 my $v = PublicInbox::Hval->new_msgid($_);
341 my $html = $v->as_html;
342 my $href = $v->as_href;
343 "<<a\nhref=\"$href.html\">$html</a>>";
350 $msgid =~ s/\A\s*<?//;
351 $msgid =~ s/>?\s*\z//;
352 Digest::SHA::sha1_hex($msgid);