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