]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
view: hoist out index_walk function
[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
22 # public functions:
23 sub msg_html {
24         my ($class, $mime, $full_pfx, $footer) = @_;
25         if (defined $footer) {
26                 $footer = "\n" . $footer;
27         } else {
28                 $footer = '';
29         }
30         headers_to_html_header($mime, $full_pfx) .
31                 multipart_text_as_html($mime, $full_pfx) .
32                 '</pre><hr />' . PRE_WRAP .
33                 html_footer($mime, 1) . $footer .
34                 '</pre></body></html>';
35 }
36
37 sub feed_entry {
38         my ($class, $mime, $full_pfx) = @_;
39
40         PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
41 }
42
43 # this is already inside a <pre>
44 sub index_entry {
45         my ($class, $mime, $level, $state) = @_;
46         my ($now, $seen, $first) = @$state;
47         my $midx = $state->[3]++;
48         my ($prev, $next) = ($midx - 1, $midx + 1);
49         my $rv = '';
50         my $part_nr = 0;
51         my $enc_msg = enc_for($mime->header("Content-Type"));
52         my $subj = $mime->header('Subject');
53         my $header_obj = $mime->header_obj;
54
55         my $mid_raw = $header_obj->header_raw('Message-ID');
56         my $id = anchor_for($mid_raw);
57         $seen->{$id} = "#$id"; # save the anchor for later
58
59         my $mid = PublicInbox::Hval->new_msgid($mid_raw);
60         my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
61         my @from = Email::Address->parse($from);
62         $from = $from[0]->name;
63         (defined($from) && length($from)) or $from = $from[0]->address;
64
65         $from = PublicInbox::Hval->new_oneline($from)->as_html;
66         $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
67         my $pfx = ('  ' x $level);
68
69         my $ts = $mime->header('X-PI-Date');
70         my $fmt = '%Y-%m-%d %H:%M UTC';
71         $ts = POSIX::strftime($fmt, gmtime($ts));
72
73         $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
74         $rv .= "- by $from @ $ts - ";
75         $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
76         if ($prev >= 0) {
77                 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
78         }
79         $rv .= "\n\n";
80
81         my $irp = $header_obj->header_raw('In-Reply-To');
82         my ($anchor_idx, $anchor);
83         if (defined $irp) {
84                 $anchor_idx = anchor_for($irp);
85                 $anchor = $seen->{$anchor_idx};
86         }
87         my $href = $mid->as_href;
88         my $mhref = "m/$href.html";
89         my $fhref = "f/$href.html";
90         my $more = 'message';
91         # scan through all parts, looking for displayable text
92         $mime->walk_parts(sub {
93                 $rv .= index_walk($_[0], $pfx, $enc_msg, $part_nr, $fhref,
94                                   \$more);
95                 $part_nr++;
96         });
97
98         $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
99         my $txt = "m/$href.txt";
100         $rv .= "<a\nhref=\"$txt\">raw</a> ";
101         $rv .= html_footer($mime, 0);
102
103         if (defined $irp) {
104                 unless (defined $anchor) {
105                         my $v = PublicInbox::Hval->new_msgid($irp);
106                         my $html = $v->as_html;
107                         $anchor = 'm/' . $v->as_href . '.html';
108                         $seen->{$anchor_idx} = $anchor;
109                 }
110                 $rv .= " <a\nhref=\"$anchor\">parent</a>";
111         }
112         $rv .= " <a\nhref=\"?r=$first#$id\">threadlink</a>";
113
114         $rv . "\n\n";
115 }
116
117 # only private functions below.
118
119 sub index_walk {
120         my ($part, $pfx, $enc_msg, $part_nr, $fhref, $more) = @_;
121         my $rv = '';
122         return $rv if $part->subparts; # walk_parts already recurses
123         my $ct = $part->content_type;
124
125         # account for filter bugs...
126         return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
127
128         my $enc = enc_for($ct, $enc_msg);
129
130         if ($part_nr > 0) {
131                 my $fn = $part->filename;
132                 defined($fn) or $fn = "part #" . ($part_nr + 1);
133                 $rv .= $pfx . add_filename_line($enc->decode($fn));
134         }
135
136         my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
137
138         # drop the remainder of git patches, they're usually better
139         # to review when the full message is viewed
140         $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
141
142         # Drop signatures
143         $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
144
145         # kill any leading or trailing whitespace
146         $s =~ s/\A\s+//s;
147         $s =~ s/\s+\z//s;
148
149         if (length $s) {
150                 # add prefix:
151                 $s =~ s/^/$pfx/sgm;
152
153                 $rv .= $s . "\n";
154         }
155         $rv;
156 }
157
158 sub enc_for {
159         my ($ct, $default) = @_;
160         $default ||= $enc_utf8;
161         defined $ct or return $default;
162         my $ct_parsed = parse_content_type($ct);
163         if ($ct_parsed) {
164                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
165                         my $enc = find_encoding($charset);
166                         return $enc if $enc;
167                 }
168         }
169         $default;
170 }
171
172 sub multipart_text_as_html {
173         my ($mime, $full_pfx) = @_;
174         my $rv = "";
175         my $part_nr = 0;
176         my $enc_msg = enc_for($mime->header("Content-Type"));
177
178         # scan through all parts, looking for displayable text
179         $mime->walk_parts(sub {
180                 my ($part) = @_;
181                 return if $part->subparts; # walk_parts already recurses
182                 my $ct = $part->content_type;
183
184                 # account for filter bugs...
185                 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
186
187                 my $enc = enc_for($ct, $enc_msg);
188
189                 if ($part_nr > 0) {
190                         my $fn = $part->filename;
191                         defined($fn) or $fn = "part #" . ($part_nr + 1);
192                         $rv .= add_filename_line($enc->decode($fn));
193                 }
194
195                 if (defined $full_pfx) {
196                         $rv .= add_text_body_short($enc, $part, $part_nr,
197                                                 $full_pfx);
198                 } else {
199                         $rv .= add_text_body_full($enc, $part, $part_nr);
200                 }
201                 $rv .= "\n" unless $rv =~ /\n\z/s;
202                 ++$part_nr;
203         });
204         $rv;
205 }
206
207 sub add_filename_line {
208         my ($fn) = @_;
209         my $len = 72;
210         my $pad = "-";
211
212         $len -= length($fn);
213         $pad x= ($len/2) if ($len > 0);
214         "$pad " . ascii_html($fn) . " $pad\n";
215 }
216
217 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
218
219 sub linkify {
220         # no newlines added here since it'd break the splitting we do
221         # to fold quotes
222         $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
223 }
224
225 sub add_text_body_short {
226         my ($enc, $part, $part_nr, $full_pfx) = @_;
227         my $n = 0;
228         my $s = ascii_html($enc->decode($part->body));
229         linkify($s);
230         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
231                 my $cur = $1;
232                 my @lines = split(/\n/, $cur);
233                 if (@lines > MAX_INLINE_QUOTED) {
234                         # show a short snippet of quoted text
235                         $cur = join(' ', @lines);
236                         $cur =~ s/^&gt;\s*//;
237
238                         my @sum = split(/\s+/, $cur);
239                         $cur = '';
240                         do {
241                                 my $tmp = shift(@sum);
242                                 my $len = length($tmp) + length($cur);
243                                 if ($len > MAX_TRUNC_LEN) {
244                                         @sum = ();
245                                 } else {
246                                         $cur .= $tmp . ' ';
247                                 }
248                         } while (@sum && length($cur) < MAX_TRUNC_LEN);
249                         $cur =~ s/ \z/ .../;
250                         "&gt; &lt;<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
251                                 "\">$cur<\/a>&gt;\n";
252                 } else {
253                         $cur;
254                 }
255         !emg;
256         $s;
257 }
258
259 sub add_text_body_full {
260         my ($enc, $part, $part_nr) = @_;
261         my $n = 0;
262         my $s = ascii_html($enc->decode($part->body));
263         linkify($s);
264         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
265                 my $cur = $1;
266                 my @lines = split(/\n/, $cur);
267                 if (@lines > MAX_INLINE_QUOTED) {
268                         "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
269                 } else {
270                         $cur;
271                 }
272         !emg;
273         $s;
274 }
275
276 sub headers_to_html_header {
277         my ($mime, $full_pfx) = @_;
278
279         my $rv = "";
280         my @title;
281         foreach my $h (qw(From To Cc Subject Date)) {
282                 my $v = $mime->header($h);
283                 defined($v) && length($v) or next;
284                 $v = PublicInbox::Hval->new_oneline($v);
285                 $rv .= "$h: " . $v->as_html . "\n";
286
287                 if ($h eq 'From') {
288                         my @from = Email::Address->parse($v->raw);
289                         $v = $from[0]->name;
290                         unless (defined($v) && length($v)) {
291                                 $v = '<' . $from[0]->address . '>';
292                         }
293                         $title[1] = ascii_html($v);
294                 } elsif ($h eq 'Subject') {
295                         $title[0] = $v->as_html;
296                 }
297         }
298
299         my $header_obj = $mime->header_obj;
300         my $mid = $header_obj->header_raw('Message-ID');
301         if (defined $mid) {
302                 $mid = PublicInbox::Hval->new_msgid($mid);
303                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
304                 my $href = $mid->as_href;
305                 $href = "../m/$href" unless $full_pfx;
306                 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
307         }
308
309         my $irp = $header_obj->header_raw('In-Reply-To');
310         if (defined $irp) {
311                 my $v = PublicInbox::Hval->new_msgid($irp);
312                 my $html = $v->as_html;
313                 my $href = $v->as_href;
314                 $rv .= "In-Reply-To: &lt;";
315                 $rv .= "<a\nhref=\"$href.html\">$html</a>&gt;\n";
316         }
317
318         my $refs = $header_obj->header_raw('References');
319         if ($refs) {
320                 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
321                 my @refs = ($refs =~ /<([^>]+)>/g);
322                 if (@refs) {
323                         $rv .= 'References: '. linkify_refs(@refs) . "\n";
324                 }
325         }
326
327         $rv .= "\n";
328
329         ("<html><head><title>".  join(' - ', @title) .
330          '</title></head><body>' . PRE_WRAP . $rv);
331 }
332
333 sub html_footer {
334         my ($mime, $standalone) = @_;
335         my %cc; # everyone else
336         my $to; # this is the From address
337
338         foreach my $h (qw(From To Cc)) {
339                 my $v = $mime->header($h);
340                 defined($v) && length($v) or next;
341                 my @addrs = Email::Address->parse($v);
342                 foreach my $recip (@addrs) {
343                         my $address = $recip->address;
344                         my $dst = lc($address);
345                         $cc{$dst} ||= $address;
346                         $to ||= $dst;
347                 }
348         }
349         Email::Address->purge_cache if $standalone;
350
351         my $subj = $mime->header('Subject') || '';
352         $subj = "Re: $subj" unless $subj =~ /\bRe:/;
353         my $irp = uri_escape_utf8(
354                         $mime->header_obj->header_raw('Message-ID') || '');
355         delete $cc{$to};
356         $to = uri_escape_utf8($to);
357         $subj = uri_escape_utf8($subj);
358
359         my $cc = uri_escape_utf8(join(',', sort values %cc));
360         my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
361
362         my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
363
364         "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
365 }
366
367 sub linkify_refs {
368         join(' ', map {
369                 my $v = PublicInbox::Hval->new_msgid($_);
370                 my $html = $v->as_html;
371                 my $href = $v->as_href;
372                 "&lt;<a\nhref=\"$href.html\">$html</a>&gt;";
373         } @_);
374 }
375
376 require Digest::SHA;
377 sub anchor_for {
378         my ($msgid) = @_;
379         $msgid =~ s/\A\s*<?//;
380         $msgid =~ s/>?\s*\z//;
381         'm' . Digest::SHA::sha1_hex($msgid);
382 }
383
384 1;