]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
index group state parameters together for generation
[public-inbox.git] / lib / PublicInbox / View.pm
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;
4 use strict;
5 use warnings;
6 use PublicInbox::Hval;
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/;
11 require POSIX;
12
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\">";
17
18 *ascii_html = *PublicInbox::Hval::ascii_html;
19
20 my $enc_utf8 = find_encoding('UTF-8');
21 my $enc_mime = find_encoding('MIME-Header');
22
23 # public functions:
24 sub msg_html {
25         my ($class, $mime, $full_pfx, $footer) = @_;
26         if (defined $footer) {
27                 $footer = "\n" . $footer;
28         } else {
29                 $footer = '';
30         }
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>';
36 }
37
38 sub feed_entry {
39         my ($class, $mime, $full_pfx) = @_;
40
41         PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
42 }
43
44 # this is already inside a <pre>
45 sub index_entry {
46         my ($class, $mime, $level, $state) = @_;
47         my ($now, $seen, $first) = @$state;
48         my $rv = '';
49         my $part_nr = 0;
50         my $enc_msg = enc_for($mime->header("Content-Type"));
51         my $subj = $mime->header('Subject');
52         my $header_obj = $mime->header_obj;
53
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
57
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;
63
64         $from = PublicInbox::Hval->new_oneline($from)->as_html;
65         $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
66         my $pfx = ('  ' x $level);
67
68         my $ts = $mime->header('X-PI-Date');
69         my $fmt = '%H:%M';
70         if ($now > ($ts + (365 * 24 * 60 * 60))) {
71                 # doesn't have to be exactly 1 year
72                 $fmt = '%Y/%m/%d';
73         } elsif ($now > ($ts + (24 * 60 * 60))) {
74                 $fmt = '%m/%d';
75         }
76         $ts = POSIX::strftime($fmt, gmtime($ts));
77
78         $rv .= "$pfx<a\nname=\"$name\"><b>$subj</b> $from - $ts</a>\n\n";
79
80         my $irp = $header_obj->header_raw('In-Reply-To');
81         my ($anchor_idx, $anchor);
82         if (defined $irp) {
83                 $anchor_idx = anchor_for($irp);
84                 $anchor = $seen->{$anchor_idx};
85         }
86         my $href = $mid->as_href;
87         my $mhref = "m/$href.html";
88         my $fhref = "f/$href.html";
89         my $more = 'message';
90         # scan through all parts, looking for displayable text
91         $mime->walk_parts(sub {
92                 my ($part) = @_;
93                 return if $part->subparts; # walk_parts already recurses
94                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
95
96                 if ($part_nr > 0) {
97                         my $fn = $part->filename;
98                         defined($fn) or $fn = "part #" . ($part_nr + 1);
99                         $rv .= $pfx . add_filename_line($enc->decode($fn));
100                 }
101
102                 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
103
104                 # keep signatures for now?  They shold usually be short,
105                 # and sometimes footnotes/"P.S." appear there.
106
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...';
110
111                 # kill any leading or trailing whitespace
112                 $s =~ s/\A\s+//s;
113                 $s =~ s/\s+\z//s;
114
115                 if (length $s) {
116                         # add prefix:
117                         $s =~ s/^/$pfx/sgm;
118
119                         $rv .= $s . "\n";
120                 }
121                 ++$part_nr;
122         });
123
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);
128
129         if (defined $irp) {
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;
135                 }
136                 $rv .= " <a\nhref=\"$anchor\">parent</a>";
137         }
138         $rv .= " <a\nhref=\"?r=$first#$name\">permalink</a>";
139
140         $rv . "\n\n";
141 }
142
143 # only private functions below.
144
145 sub enc_for {
146         my ($ct) = @_;
147         defined $ct or return $enc_utf8;
148         my $ct_parsed = parse_content_type($ct);
149         if ($ct_parsed) {
150                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
151                         my $enc = find_encoding($charset);
152                         return $enc if $enc;
153                 }
154         }
155         $enc_utf8;
156 }
157
158 sub multipart_text_as_html {
159         my ($mime, $full_pfx) = @_;
160         my $rv = "";
161         my $part_nr = 0;
162         my $enc_msg = enc_for($mime->header("Content-Type"));
163
164         # scan through all parts, looking for displayable text
165         $mime->walk_parts(sub {
166                 my ($part) = @_;
167                 return if $part->subparts; # walk_parts already recurses
168                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
169
170                 if ($part_nr > 0) {
171                         my $fn = $part->filename;
172                         defined($fn) or $fn = "part #" . ($part_nr + 1);
173                         $rv .= add_filename_line($enc->decode($fn));
174                 }
175
176                 if (defined $full_pfx) {
177                         $rv .= add_text_body_short($enc, $part, $part_nr,
178                                                 $full_pfx);
179                 } else {
180                         $rv .= add_text_body_full($enc, $part, $part_nr);
181                 }
182                 $rv .= "\n" unless $rv =~ /\n\z/s;
183                 ++$part_nr;
184         });
185         $rv;
186 }
187
188 sub add_filename_line {
189         my ($fn) = @_;
190         my $len = 72;
191         my $pad = "-";
192
193         $len -= length($fn);
194         $pad x= ($len/2) if ($len > 0);
195         "$pad " . ascii_html($fn) . " $pad\n";
196 }
197
198 sub add_text_body_short {
199         my ($enc, $part, $part_nr, $full_pfx) = @_;
200         my $n = 0;
201         my $s = ascii_html($enc->decode($part->body));
202         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
203                 my $cur = $1;
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/^&gt;\s*//;
209
210                         my @sum = split(/\s+/, $cur);
211                         $cur = '';
212                         do {
213                                 my $tmp = shift(@sum);
214                                 my $len = length($tmp) + length($cur);
215                                 if ($len > MAX_TRUNC_LEN) {
216                                         @sum = ();
217                                 } else {
218                                         $cur .= $tmp . ' ';
219                                 }
220                         } while (@sum && length($cur) < MAX_TRUNC_LEN);
221                         $cur =~ s/ \z/ .../;
222                         "&gt; &lt;<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
223                                 "\">$cur<\/a>&gt;\n";
224                 } else {
225                         $cur;
226                 }
227         !emg;
228         $s;
229 }
230
231 sub add_text_body_full {
232         my ($enc, $part, $part_nr) = @_;
233         my $n = 0;
234         my $s = ascii_html($enc->decode($part->body));
235         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
236                 my $cur = $1;
237                 my @lines = split(/\n/, $cur);
238                 if (@lines > MAX_INLINE_QUOTED) {
239                         "<a\nname=q${part_nr}_" . $n++ . ">$cur</a>";
240                 } else {
241                         $cur;
242                 }
243         !emg;
244         $s;
245 }
246
247 sub headers_to_html_header {
248         my ($mime, $full_pfx) = @_;
249
250         my $rv = "";
251         my @title;
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";
257
258                 if ($h eq 'From') {
259                         my @from = Email::Address->parse($v->raw);
260                         $v = $from[0]->name;
261                         unless (defined($v) && length($v)) {
262                                 $v = '<' . $from[0]->address . '>';
263                         }
264                         $title[1] = ascii_html($v);
265                 } elsif ($h eq 'Subject') {
266                         $title[0] = $v->as_html;
267                 }
268         }
269
270         my $header_obj = $mime->header_obj;
271         my $mid = $header_obj->header_raw('Message-ID');
272         if (defined $mid) {
273                 $mid = PublicInbox::Hval->new_msgid($mid);
274                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
275                 my $href = $mid->as_href;
276                 $href = "../m/$href" unless $full_pfx;
277                 $rv .= "(<a\nhref=\"$href.txt\">original</a>)\n";
278         }
279
280         my $irp = $header_obj->header_raw('In-Reply-To');
281         if (defined $irp) {
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: &lt;";
286                 $rv .= "<a\nhref=\"$href.html\">$html</a>&gt;\n";
287         }
288
289         my $refs = $header_obj->header_raw('References');
290         if ($refs) {
291                 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
292                 my @refs = ($refs =~ /<([^>]+)>/g);
293                 if (@refs) {
294                         $rv .= 'References: '. linkify_refs(@refs) . "\n";
295                 }
296         }
297
298         $rv .= "\n";
299
300         ("<html><head><title>".  join(' - ', @title) .
301          '</title></head><body>' . PRE_WRAP . $rv);
302 }
303
304 sub html_footer {
305         my ($mime, $standalone) = @_;
306         my %cc; # everyone else
307         my $to; # this is the From address
308
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;
317                         $to ||= $dst;
318                 }
319         }
320         Email::Address->purge_cache if $standalone;
321
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') || '');
326         delete $cc{$to};
327         $to = uri_escape_utf8($to);
328         $subj = uri_escape_utf8($subj);
329
330         my $cc = uri_escape_utf8(join(',', values %cc));
331         my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
332
333         my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
334
335         "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
336 }
337
338 sub linkify_refs {
339         join(' ', map {
340                 my $v = PublicInbox::Hval->new_msgid($_);
341                 my $html = $v->as_html;
342                 my $href = $v->as_href;
343                 "&lt;<a\nhref=\"$href.html\">$html</a>&gt;";
344         } @_);
345 }
346
347 require Digest::SHA;
348 sub anchor_for {
349         my ($msgid) = @_;
350         $msgid =~ s/\A\s*<?//;
351         $msgid =~ s/>?\s*\z//;
352         Digest::SHA::sha1_hex($msgid);
353 }
354
355 1;