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