]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
view: add newline before list-wide footer
[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
12 # TODO: make these constants tunable
13 use constant MAX_INLINE_QUOTED => 5;
14 use constant MAX_TRUNC_LEN => 72;
15
16 *ascii_html = *PublicInbox::Hval::ascii_html;
17
18 my $enc_utf8 = find_encoding('UTF-8');
19 my $enc_mime = find_encoding('MIME-Header');
20
21 # public functions:
22 sub msg_html {
23         my ($class, $mime, $full_pfx, $footer) = @_;
24         if (defined $footer) {
25                 $footer = "\n" . $footer;
26         } else {
27                 $footer = '';
28         }
29         headers_to_html_header($mime, $full_pfx) .
30                 multipart_text_as_html($mime, $full_pfx) .
31                 '</pre><hr /><pre>' .
32                 html_footer($mime) . $footer .
33                 '</pre></body></html>';
34 }
35
36 sub feed_entry {
37         my ($class, $mime, $full_pfx) = @_;
38
39         '<pre>' . multipart_text_as_html($mime, $full_pfx) . '</pre>';
40 }
41
42
43 # only private functions below.
44
45 sub enc_for {
46         my ($ct) = @_;
47         defined $ct or return $enc_utf8;
48         my $ct_parsed = parse_content_type($ct);
49         if ($ct_parsed) {
50                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
51                         my $enc = find_encoding($charset);
52                         return $enc if $enc;
53                 }
54         }
55         $enc_utf8;
56 }
57
58 sub multipart_text_as_html {
59         my ($mime, $full_pfx) = @_;
60         my $rv = "";
61         my $part_nr = 0;
62         my $enc_msg = enc_for($mime->header("Content-Type"));
63
64         # scan through all parts, looking for displayable text
65         $mime->walk_parts(sub {
66                 my ($part) = @_;
67                 return if $part->subparts; # walk_parts already recurses
68                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
69
70                 if ($part_nr > 0) {
71                         my $fn = $part->filename;
72                         defined($fn) or $fn = "part #" . ($part_nr + 1);
73                         $rv .= add_filename_line($enc->decode($fn));
74                 }
75
76                 if (defined $full_pfx) {
77                         $rv .= add_text_body_short($enc, $part, $part_nr,
78                                                 $full_pfx);
79                 } else {
80                         $rv .= add_text_body_full($enc, $part, $part_nr);
81                 }
82                 $rv .= "\n" unless $rv =~ /\n\z/s;
83                 ++$part_nr;
84         });
85         $rv;
86 }
87
88 sub add_filename_line {
89         my ($fn) = @_;
90         my $len = 72;
91         my $pad = "-";
92
93         $len -= length($fn);
94         $pad x= ($len/2) if ($len > 0);
95         "$pad " . ascii_html($fn) . " $pad\n";
96 }
97
98 sub add_text_body_short {
99         my ($enc, $part, $part_nr, $full_pfx) = @_;
100         my $n = 0;
101         my $s = ascii_html($enc->decode($part->body));
102         # TODO: fold the "so-and-so wrote:" attribute line here, too:
103         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
104                 my $cur = $1;
105                 my @lines = split(/\n/, $cur);
106                 if (@lines > MAX_INLINE_QUOTED) {
107                         # show a short snippet of quoted text
108                         $cur = join(' ', @lines);
109                         $cur =~ s/^&gt;\s*//;
110
111                         my @sum = split(/\s+/, $cur);
112                         $cur = '';
113                         do {
114                                 my $tmp = shift(@sum);
115                                 my $len = length($tmp) + length($cur);
116                                 if ($len > MAX_TRUNC_LEN) {
117                                         @sum = ();
118                                 } else {
119                                         $cur .= $tmp . ' ';
120                                 }
121                         } while (@sum && length($cur) < MAX_TRUNC_LEN);
122                         $cur =~ s/ \z/ .../;
123                         "&gt; &lt;<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
124                                 "\">$cur<\/a>&gt;\n";
125                 } else {
126                         $cur;
127                 }
128         !emg;
129         $s;
130 }
131
132 sub add_text_body_full {
133         my ($enc, $part, $part_nr) = @_;
134         my $n = 0;
135         my $s = ascii_html($enc->decode($part->body));
136         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
137                 my $cur = $1;
138                 my @lines = split(/\n/, $cur);
139                 if (@lines > MAX_INLINE_QUOTED) {
140                         "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
141                 } else {
142                         $cur;
143                 }
144         !emg;
145         $s;
146 }
147
148 sub headers_to_html_header {
149         my ($mime, $full_pfx) = @_;
150
151         my $rv = "";
152         my @title;
153         foreach my $h (qw(From To Cc Subject Date)) {
154                 my $v = $mime->header($h);
155                 defined($v) && length($v) or next;
156                 $v = PublicInbox::Hval->new_oneline($v);
157                 $rv .= "$h: " . $v->as_html . "\n";
158
159                 if ($h eq 'From') {
160                         my @from = Email::Address->parse($v->raw);
161                         $v = $from[0]->name;
162                         unless (defined($v) && length($v)) {
163                                 $v = '<' . $from[0]->address . '>';
164                         }
165                         $title[1] = ascii_html($v);
166                 } elsif ($h eq 'Subject') {
167                         $title[0] = $v->as_html;
168                 }
169         }
170
171         my $header_obj = $mime->header_obj;
172         my $mid = $header_obj->header_raw('Message-ID');
173         if (defined $mid) {
174                 $mid = PublicInbox::Hval->new_msgid($mid);
175                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
176                 my $href = $mid->as_href;
177                 $href = "../m/$href" unless $full_pfx;
178                 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
179         }
180
181         my $irp = $header_obj->header_raw('In-Reply-To');
182         if (defined $irp) {
183                 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
184                 my $html = $v->as_html;
185                 my $href = $v->as_href;
186                 $rv .= "In-Reply-To: &lt;";
187                 $rv .= "<a href=\"$href.html\">$html</a>&gt;\n";
188         }
189
190         my $refs = $header_obj->header_raw('References');
191         if ($refs) {
192                 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
193                 my @refs = ($refs =~ /<([^>]+)>/g);
194                 if (@refs) {
195                         $rv .= 'References: '. linkify_refs(@refs) . "\n";
196                 }
197         }
198
199         $rv .= "\n";
200
201         ("<html><head><title>".  join(' - ', @title) .
202          '</title></head><body><pre style="white-space:pre-wrap">' .  $rv);
203 }
204
205 sub html_footer {
206         my ($mime) = @_;
207         my %cc; # everyone else
208         my $to; # this is the From address
209
210         foreach my $h (qw(From To Cc)) {
211                 my $v = $mime->header($h);
212                 defined($v) && length($v) or next;
213                 my @addrs = Email::Address->parse($v);
214                 foreach my $recip (@addrs) {
215                         my $address = $recip->address;
216                         my $dst = lc($address);
217                         $cc{$dst} ||= $address;
218                         $to ||= $dst;
219                 }
220         }
221         Email::Address->purge_cache;
222
223         my $subj = $mime->header('Subject') || '';
224         $subj = "Re: $subj" unless $subj =~ /\bRe:/;
225         my $irp = uri_escape_utf8(
226                         $mime->header_obj->header_raw('Message-ID') || '');
227         delete $cc{$to};
228         $to = uri_escape_utf8($to);
229         $subj = uri_escape_utf8($subj);
230
231         my $cc = uri_escape_utf8(join(',', values %cc));
232         my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
233
234         '<a href="' . ascii_html($href) . '">reply</a>';
235 }
236
237 sub linkify_refs {
238         join(' ', map {
239                 my $v = PublicInbox::Hval->new_msgid($_);
240                 my $html = $v->as_html;
241                 my $href = $v->as_href;
242                 "&lt;<a href=\"$href.html\">$html</a>&gt;";
243         } @_);
244 }
245
246 1;