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, $now, $level, $seen, $first) = @_;
49 my $enc_msg = enc_for($mime->header("Content-Type"));
50 my $subj = $mime->header('Subject');
51 my $header_obj = $mime->header_obj;
53 my $mid_raw = $header_obj->header_raw('Message-ID');
54 my $name = anchor_for($mid_raw);
55 $seen->{$name} = "#$name"; # save the anchor for later
57 my $mid = PublicInbox::Hval->new_msgid($mid_raw);
58 my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
59 my @from = Email::Address->parse($from);
60 $from = $from[0]->name;
61 (defined($from) && length($from)) or $from = $from[0]->address;
63 $from = PublicInbox::Hval->new_oneline($from)->as_html;
64 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
65 my $pfx = (' ' x $level);
67 my $ts = $mime->header('X-PI-Date');
69 if ($now > ($ts + (365 * 24 * 60 * 60))) {
70 # doesn't have to be exactly 1 year
72 } elsif ($now > ($ts + (24 * 60 * 60))) {
75 $ts = POSIX::strftime($fmt, gmtime($ts));
77 $rv .= "$pfx<a\nname=\"$name\"><b>$subj</b> $from - $ts</a>\n\n";
79 my $irp = $header_obj->header_raw('In-Reply-To');
80 my ($anchor_idx, $anchor);
82 $anchor_idx = anchor_for($irp);
83 $anchor = $seen->{$anchor_idx};
85 my $href = $mid->as_href;
86 my $mhref = "m/$href.html";
87 my $fhref = "f/$href.html";
89 # scan through all parts, looking for displayable text
90 $mime->walk_parts(sub {
92 return if $part->subparts; # walk_parts already recurses
93 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
96 my $fn = $part->filename;
97 defined($fn) or $fn = "part #" . ($part_nr + 1);
98 $rv .= $pfx . add_filename_line($enc->decode($fn));
101 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
103 # keep signatures for now? They shold usually be short,
104 # and sometimes footnotes/"P.S." appear there.
106 # drop the remainder of git patches, they're usually better
107 # to review when the full message is viewed
108 $s =~ s!^---\n.*\z!!ms and $more = 'more...';
110 # kill any leading or trailing whitespace
123 $rv .= "$pfx<a\nhref=\"$mhref\">$more</a> ";
124 my $txt = "m/$href.txt";
125 $rv .= "<a\nhref=\"$txt\">raw</a> ";
126 $rv .= html_footer($mime, 0);
129 unless (defined $anchor) {
130 my $v = PublicInbox::Hval->new_msgid($irp);
131 my $html = $v->as_html;
132 $anchor = 'm/' . $v->as_href . '.html';
133 $seen->{$anchor_idx} = $anchor;
135 $rv .= " <a\nhref=\"$anchor\">parent</a>";
137 $rv .= " <a\nhref=\"?r=$first#$name\">permalink</a>";
142 # only private functions below.
146 defined $ct or return $enc_utf8;
147 my $ct_parsed = parse_content_type($ct);
149 if (my $charset = $ct_parsed->{attributes}->{charset}) {
150 my $enc = find_encoding($charset);
157 sub multipart_text_as_html {
158 my ($mime, $full_pfx) = @_;
161 my $enc_msg = enc_for($mime->header("Content-Type"));
163 # scan through all parts, looking for displayable text
164 $mime->walk_parts(sub {
166 return if $part->subparts; # walk_parts already recurses
167 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
170 my $fn = $part->filename;
171 defined($fn) or $fn = "part #" . ($part_nr + 1);
172 $rv .= add_filename_line($enc->decode($fn));
175 if (defined $full_pfx) {
176 $rv .= add_text_body_short($enc, $part, $part_nr,
179 $rv .= add_text_body_full($enc, $part, $part_nr);
181 $rv .= "\n" unless $rv =~ /\n\z/s;
187 sub add_filename_line {
193 $pad x= ($len/2) if ($len > 0);
194 "$pad " . ascii_html($fn) . " $pad\n";
197 sub add_text_body_short {
198 my ($enc, $part, $part_nr, $full_pfx) = @_;
200 my $s = ascii_html($enc->decode($part->body));
201 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
203 my @lines = split(/\n/, $cur);
204 if (@lines > MAX_INLINE_QUOTED) {
205 # show a short snippet of quoted text
206 $cur = join(' ', @lines);
207 $cur =~ s/^>\s*//;
209 my @sum = split(/\s+/, $cur);
212 my $tmp = shift(@sum);
213 my $len = length($tmp) + length($cur);
214 if ($len > MAX_TRUNC_LEN) {
219 } while (@sum && length($cur) < MAX_TRUNC_LEN);
221 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
222 "\">$cur<\/a>>\n";
230 sub add_text_body_full {
231 my ($enc, $part, $part_nr) = @_;
233 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 "<a\nname=q${part_nr}_" . $n++ . ">$cur</a>";
246 sub headers_to_html_header {
247 my ($mime, $full_pfx) = @_;
251 foreach my $h (qw(From To Cc Subject Date)) {
252 my $v = $mime->header($h);
253 defined($v) && length($v) or next;
254 $v = PublicInbox::Hval->new_oneline($v);
255 $rv .= "$h: " . $v->as_html . "\n";
258 my @from = Email::Address->parse($v->raw);
260 unless (defined($v) && length($v)) {
261 $v = '<' . $from[0]->address . '>';
263 $title[1] = ascii_html($v);
264 } elsif ($h eq 'Subject') {
265 $title[0] = $v->as_html;
269 my $header_obj = $mime->header_obj;
270 my $mid = $header_obj->header_raw('Message-ID');
272 $mid = PublicInbox::Hval->new_msgid($mid);
273 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
274 my $href = $mid->as_href;
275 $href = "../m/$href" unless $full_pfx;
276 $rv .= "(<a\nhref=\"$href.txt\">original</a>)\n";
279 my $irp = $header_obj->header_raw('In-Reply-To');
281 my $v = PublicInbox::Hval->new_msgid($irp);
282 my $html = $v->as_html;
283 my $href = $v->as_href;
284 $rv .= "In-Reply-To: <";
285 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
288 my $refs = $header_obj->header_raw('References');
290 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
291 my @refs = ($refs =~ /<([^>]+)>/g);
293 $rv .= 'References: '. linkify_refs(@refs) . "\n";
299 ("<html><head><title>". join(' - ', @title) .
300 '</title></head><body>' . PRE_WRAP . $rv);
304 my ($mime, $standalone) = @_;
305 my %cc; # everyone else
306 my $to; # this is the From address
308 foreach my $h (qw(From To Cc)) {
309 my $v = $mime->header($h);
310 defined($v) && length($v) or next;
311 my @addrs = Email::Address->parse($v);
312 foreach my $recip (@addrs) {
313 my $address = $recip->address;
314 my $dst = lc($address);
315 $cc{$dst} ||= $address;
319 Email::Address->purge_cache if $standalone;
321 my $subj = $mime->header('Subject') || '';
322 $subj = "Re: $subj" unless $subj =~ /\bRe:/;
323 my $irp = uri_escape_utf8(
324 $mime->header_obj->header_raw('Message-ID') || '');
326 $to = uri_escape_utf8($to);
327 $subj = uri_escape_utf8($subj);
329 my $cc = uri_escape_utf8(join(',', values %cc));
330 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
332 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
334 "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
339 my $v = PublicInbox::Hval->new_msgid($_);
340 my $html = $v->as_html;
341 my $href = $v->as_href;
342 "<<a\nhref=\"$href.html\">$html</a>>";
349 $msgid =~ s/\A\s*<?//;
350 $msgid =~ s/>?\s*\z//;
351 Digest::SHA::sha1_hex($msgid);