File Coverage

blib/lib/Email/Store/HTML.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Email::Store::HTML;
2 1     1   817 use base "Email::Store::DBI";
  1         2  
  1         702  
3             use strict;
4             use Email::Store::Mail;
5             __PACKAGE__->table("html_body");
6             __PACKAGE__->columns( All => qw[ id mail raw scrubbed as_text ] );
7             __PACKAGE__->columns( Primary => qw/id/);
8             Email::Store::HTML->has_a(mail => "Email::Store::Mail");
9             __PACKAGE__->add_constructor(from_mail => 'mail = ?');
10            
11              
12              
13             use HTML::Scrubber;
14             use HTML::FormatText::WithLinks;
15             use vars qw($VERSION @allow @rules @default);
16              
17             $VERSION = "0.1";
18              
19              
20             sub on_store_order { 2 }
21              
22             sub on_store {
23             my ($self, $mail) = @_;
24              
25             # create the text formatter
26             my $f = HTML::FormatText::WithLinks->new(
27             before_link => '',
28             after_link => ' [ %l ]',
29             footnote => ''
30             );
31              
32              
33             # create the scrubber
34             my $scrubber = HTML::Scrubber->new(
35             allow => \@allow,
36             rules => \@rules,
37             default => \@default,
38             comment => 1,
39             process => 0,
40             );
41              
42              
43              
44              
45             for ($mail->attachments) {
46             next unless $_->content_type eq 'text/html';
47             my $raw = $_->payload;
48             my $scrubbed = $scrubber->scrub($raw);
49             my $text = $f->parse($raw);
50             Email::Store::HTML->create( { mail => $mail->id, raw => $raw, scrubbed => $scrubbed, as_text => $text } );
51             }
52             }
53              
54             =head1 NAME
55              
56             Email::Store::HTML - various HTML related functions for Email::Store::Mail
57              
58             =head1 SYNOPSIS
59              
60             my $mail = Email::Store::Mail->retrieve( $msgid );
61             exit unless $mail->html;
62              
63             for ($mail->html) {
64             print $_->raw; # prints out the raw HTML version of the attachment
65             print $_->scrubbed; # prints out a scrubbed version of the mail which should be safe
66             print $_->as_text; # prints out a version of the HTML converted to plain text
67             }
68              
69             =head1 DESCRIPTION
70              
71             =head1 METHODS
72              
73             =head2 on_store
74              
75             This finds every HTML attachment in the mail and performs various operations on them
76             before storing them as a new C object.
77              
78             =head2 raw
79              
80             The raw HTML, exactly as we found it.
81              
82             =head2 scrubbed
83              
84             A scrubbed version of the HTML with things like javascript removed.
85              
86             =head2 as_text
87              
88             The HTML run through C. Links are placed after the anchor
89             word(a) in square brackets so that
90              
91             HOME!
92              
93             becomes
94              
95             HOME! [ http://thegestalt.org ]
96              
97              
98             =head1 BUGS AND TODO
99              
100             No bugs known at the moment.
101              
102             It might be nice to give people access to to the scrubber and formatter so that they
103             could change the options.
104              
105             =head1 SUPPORT
106              
107             This module is part of the Perl Email Project - http://pep.kwiki.org/
108              
109             There is a mailing list at pep@perl.org (subscribe at pep-subscribe@perl.org)
110             and an archive available at http://nntp.perl.org/group/pep.php
111              
112             =head1 AUTHOR
113              
114             Simon Wistow
115              
116             =head1 COPYRIGHT
117              
118             Copyright 2005, Simon Wistow
119              
120             This code is distributed under the same terms as Perl itself.
121              
122              
123             =head1 SEE ALSO
124              
125             L, L
126              
127             =cut
128              
129              
130              
131             ###
132             # Configuration for HTML::Scrubber
133             ###
134              
135             my @allow = qw[ br hr b a p pre ul ol li i em strong table tr td th div ];
136             #
137             my @rules = (
138             script => 0,
139             img => {
140             border => 1,
141             alt => 1, # alt attribute allowed
142             '*' => 0, # deny all other attributes
143             },
144             );
145             #
146             my @default = (
147             0 => # default rule, deny all tags
148             {
149             '*' => 1, # default rule, allow all attributes
150             'href' => qr{^(?!(?:java)?script)}i,
151             'src' => qr{^(?!(?:java)?script)}i,
152             'cite' => '(?i-xsm:^(?!(?:java)?script))',
153             'language' => 0,
154             'name' => 1, # could be sneaky, but hey ;)
155             'onblur' => 0,
156             'onchange' => 0,
157             'onclick' => 0,
158             'ondblclick' => 0,
159             'onerror' => 0,
160             'onfocus' => 0,
161             'onkeydown' => 0,
162             'onkeypress' => 0,
163             'onkeyup' => 0,
164             'onload' => 0,
165             'onmousedown' => 0,
166             'onmousemove' => 0,
167             'onmouseout' => 0,
168             'onmouseover' => 0,
169             'onmouseup' => 0,
170             'onreset' => 0,
171             'onselect' => 0,
172             'onsubmit' => 0,
173             'onunload' => 0,
174             'src' => 0,
175             'type' => 0,
176             }
177             );
178              
179             package Email::Store::Mail;
180             sub html {
181             my ($self) = @_;
182             return Email::Store::HTML->from_mail($self->message_id);
183             }
184              
185             package Email::Store::HTML;
186             1;
187              
188             __DATA__