about summary refs log tree commit homepage
path: root/lib/PublicInbox/View.pm
blob: 811e4703e5ee5e1588739cab9f55d6bffb504683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
package PublicInbox::View;
use strict;
use warnings;
use PublicInbox::Hval;
use URI::Escape qw/uri_escape/;
use CGI qw/escapeHTML/;
use Encode qw/find_encoding/;
use Encode::MIME::Header;
use Email::MIME::ContentType qw/parse_content_type/;
use constant MAX_INLINE_QUOTED => 5;
use constant MAX_TRUNC_LEN => 72;
*ascii_html = *PublicInbox::Hval::ascii_html;

my $enc_utf8 = find_encoding('utf8');
my $enc_ascii = find_encoding('us-ascii');
my $enc_mime = find_encoding('MIME-Header');

# public functions:
sub as_html {
	my ($class, $mime, $full_pfx) = @_;

	headers_to_html_header($mime, $full_pfx) .
		multipart_text_as_html($mime, $full_pfx) .
		'</pre></body></html>';
}

sub as_feed_entry {
	my ($class, $mime, $full_pfx) = @_;

	"<pre>" . multipart_text_as_html($mime, $full_pfx) . "</pre>";
}


# only private functions below.

sub enc_for {
	my ($ct) = @_;
	defined $ct or return $enc_utf8;
	my $ct_parsed = parse_content_type($ct);
	if ($ct_parsed) {
		if (my $charset = $ct_parsed->{attributes}->{charset}) {
			my $enc = find_encoding($charset);
			return $enc if $enc;
		}
	}
	$enc_utf8;
}

sub multipart_text_as_html {
	my ($mime, $full_pfx) = @_;
	my $rv = "";
	my $part_nr = 0;
	my $enc_msg = enc_for($mime->header("Content-Type"));

	# scan through all parts, looking for displayable text
	$mime->walk_parts(sub {
		my ($part) = @_;
		return if $part->subparts; # walk_parts already recurses
		my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;

		if ($part_nr > 0) {
			my $fn = $part->filename;
			defined($fn) or $fn = "part #" . ($part_nr + 1);
			$rv .= add_filename_line($enc->decode($fn));
		}

		if (defined $full_pfx) {
			$rv .= add_text_body_short($enc, $part, $part_nr,
						$full_pfx);
		} else {
			$rv .= add_text_body_full($enc, $part, $part_nr);
		}
		$rv .= "\n" unless $rv =~ /\n\z/s;
		++$part_nr;
	});
	$rv;
}

sub add_filename_line {
	my ($fn) = @_;
	my $len = 72;
	my $pad = "-";

	$len -= length($fn);
	$pad x= ($len/2) if ($len > 0);
	"$pad " . ascii_html($fn) . " $pad\n";
}

sub add_text_body_short {
	my ($enc, $part, $part_nr, $full_pfx) = @_;
	my $n = 0;
	my $s = ascii_html($enc->decode($part->body));
	$s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
		my $cur = $1;
		my @lines = split(/\n/, $cur);
		if (@lines > MAX_INLINE_QUOTED) {
			# show a short snippet of quoted text
			$cur = join(' ', @lines);
			$cur =~ s/^&gt;\s*//;

			my @sum = split(/\s+/, $cur);
			$cur = '';
			do {
				my $tmp = shift(@sum);
				my $len = length($tmp) + length($cur);
				if ($len > MAX_TRUNC_LEN) {
					@sum = ();
				} else {
					$cur .= $tmp . ' ';
				}
			} while (@sum && length($cur) < MAX_TRUNC_LEN);
			$cur =~ s/ \z/ .../;
			"&gt; &lt;<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
				"\">$cur<\/a>&gt;\n";
		} else {
			$cur;
		}
	!emg;
	$s;
}

sub add_text_body_full {
	my ($enc, $part, $part_nr) = @_;
	my $n = 0;
	my $s = ascii_html($enc->decode($part->body));
	$s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
		my $cur = $1;
		my @lines = split(/\n/, $cur);
		if (@lines > MAX_INLINE_QUOTED) {
			"<a name=q${part_nr}_" . $n++ . ">$cur</a>";
		} else {
			$cur;
		}
	!emg;
	$s;
}

sub headers_to_html_header {
	my ($simple, $full_pfx) = @_;

	my $rv = "";
	my @title;
	foreach my $h (qw(From To Cc Subject Date)) {
		my $v = $simple->header($h);
		defined $v or next;
		$v =~ tr/\n/ /s;
		$v =~ tr/\r//d;
		my $raw = $enc_mime->decode($v);
		$v = ascii_html($raw);
		$rv .= "$h: $v\n";

		if ($h eq 'From') {
			my @from = Email::Address->parse($raw);
			$raw = $from[0]->name;
			unless (defined($raw) && length($raw)) {
				$raw = '<' . $from[0]->address . '>';
			}
			$title[1] = ascii_html($raw);

		} elsif ($h eq 'Subject') {
			$title[0] = $v;
		}
	}

	my $mid = $simple->header('Message-ID');
	if (defined $mid) {
		$mid = PublicInbox::Hval->new_msgid($mid);
		$rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
		my $href = $mid->as_href;
		$href = "../m/$href" unless $full_pfx;
		$rv .= "(<a href=\"$href.txt\">original</a>)\n";
	}

	my $irp = $simple->header('In-Reply-To');
	if (defined $irp) {
		$irp = PublicInbox::Hval->new_msgid($irp);
		my $html = $irp->as_html;
		my $href = $irp->as_href;
		$rv .= "In-Reply-To: &lt;";
		$rv .= "<a href=\"$href.html\">$html</a>&gt;\n";
	}
	$rv .= "\n";

	("<html><head><title>".  join(' - ', @title) .
	 '</title></head><body><pre style="white-space:pre-wrap">' .  $rv);
}

1;