MessageParser.pm

filter: Statementstmt 7.9% 16 / 202 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 15.0% 3 / 20 Podpod 100% 17 / 17 totaltotal 9.6% 36 / 374 CC 61 SCAR 57.4
CC
61
Cov
5%
body
812.0
parse_header
90.0
extract_body
90.0
CRAP 311.2
↑ Summary yahootils.pl »
1 # (C) Simon Drabble 2002,2003
2 # sdrabble@cpan.org 2002/10/23
3  
4 # $Id: MessageParser.pm,v 1.14 2003/10/10 02:39:27 simon Exp $
5 #
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Mail::Webmail::MessageParser;
11  
12 1 ▶use base 'HTML::TreeBuilder';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = 0.2;
15  
16 # This results in false positives, for example on lines like
17 # From somebody you know HH:MM:SS dd:mm:yy
18 # Which is why a futher check is made against mail_header_names..
19 1 our $LOOKS_LIKE_A_HEADER = qr{\b[-\w]+:};
20  
21  
22 1 our @mail_header_names = qw(
23 To From Reply-To Subject
24 Date X- Received Content-
25  
26 );
27 # qr{^To}, qr{From}, qr{Reply-To}, qr{Subject},
28 # qr{Date}, qr{X-[-\w]+}, qr{Received}, qr{Content-[-\w]+},
29  
30  
31 # Tags that "shouldn't" appear in the message body.
32 1 our $disallow = {
33 xbody => 1,
34 html => 1,
35 head => 1,
36 meta => 1,
37 xmeta => 1,
38 body => 1,
39 };
40  
41  
42  
43 # Fix some crappiness introduced by our fiends in Redmond.
44 # These come from the chart at
45 # http://www.pemberley.com/janeinfo/latin1.html#unicode
46 1 our $ms_to_unicode = {
47 "\x82" => "&#8218", # Single Low-9 Quotation Mark
48 "\x83" => "&#402", # Latin Small Letter F With Hook
49 "\x84" => "&#8222", # Double Low-9 Quotation Mark
50 "\x85" => "&#8230", # Horizontal Ellipsis
51 "\x86" => "&#8224", # Dagger
52 "\x87" => "&#8225", # Double Dagger
53 "\x88" => "&#710", # Modifier Letter Circumflex Accent
54 "\x89" => "&#8240", # Per Mille Sign
55 "\x8a" => "&#352", # Latin Capital Letter S With Caron
56 "\x8b" => "&#8249", # Single Left-Pointing Angle Quotation Mark
57 "\x8c" => "&#338", # Latin Capital Ligature OE
58 "\x91" => "&#8216", # Left Single Quotation Mark
59 "\x92" => "&#8217", # Right Single Quotation Mark
60 "\x93" => "&#8220", # Left Double Quotation Mark
61 "\x94" => "&#8221", # Right Double Quotation Mark
62 "\x95" => "&#8226", # Bullet
63 "\x96" => "&#8211", # En Dash
64 "\x97" => "&#8212", # Em Dash
65 "\x98" => "&#732", # Small Tilde
66 "\x99" => "&#8482", # Trade Mark Sign
67 "\x9a" => "&#353", # Latin Small Letter S With Caron
68 "\x9b" => "&#8250", # Single Right-Pointing Angle Quotation Mark
69 "\x9c" => "&#339", # Latin Small Ligature OE
70 "\x9f" => "&#376", # Latin Capital Letter Y With Diaeresis
71 };
72  
73  
74 1 our $unicode_to_text = {
75 "&#8218" => "'", # Single Low-9 Quotation Mark
76 "&#402" => '', # Latin Small Letter F With Hook
77 "&#8222" => "'", # Double Low-9 Quotation Mark
78 "&#8230" => '..', # Horizontal Ellipsis
79 "&#8224" => '', # Dagger
80 "&#8225" => '', # Double Dagger
81 "&#710" => '', # Modifier Letter Circumflex Accent
82 "&#8240" => '', # Per Mille Sign
83 "&#352" => '', # Latin Capital Letter S With Caron
84 "&#8249" => '<', # Single Left-Pointing Angle Quotation Mark
85 "&#338" => 'OE', # Latin Capital Ligature OE
86 "&#8216" => '`', # Left Single Quotation Mark
87 "&#8217" => "'", # Right Single Quotation Mark
88 "&#8220" => '"', # Left Double Quotation Mark
89 "&#8221" => '"', # Right Double Quotation Mark
90 "&#8226" => 'o', # Bullet
91 "&#8211" => '--', # En Dash
92 "&#8212" => '---', # Em Dash
93 "&#732" => '~', # Small Tilde
94 "&#8482" => 'TM', # Trade Mark Sign
95 "&#353" => '', # Latin Small Letter S With Caron
96 "&#8250" => ">", # Single Right-Pointing Angle Quotation Mark
97 "&#339" => 'oe', # Latin Small Ligature OE
98 "&#376" => '', # Latin Capital Letter Y With Diaeresis
99 };
100  
101  
102  
103 sub message_start
104 0 ▶{
Subroutine message_start: not called CC=1 SCAR=6.9
Pod documented
105 0 my ($self, @gubbins) = @_;
106 0 $self->{_message_start} = \@gubbins;
107 }
108  
109  
110  
111 sub parse_header
112 0 ▶{
Subroutine parse_header: not called CC=9 SCAR=45.0
Pod documented
113 0 my ($self, $field, $val) = @_;
114  
115 # We could only look for HEADER fields in the textual contents of <td>'s (or
116 # whatever we have in $field). However, to do that would require searching the
117 # entire tree in $self. It's probably quicker to run the regexp for each
118 # (longer) item than to parse the tree and /then/ run the regexp on the
119 # (shorter) parsed output. It does mean that the regexp for checking cannot
120 # be anchored at the beginning of the text :(
121 # TODO: test this assumption.
122  
123 0 my $found = 0;
124 0 ▶ if ($field =~ /$LOOKS_LIKE_A_HEADER/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /$Mail::Webmail::MessageParser::LOOKS_LIKE_A_HEADER/) 0 0 0
125 0 $self->parse($field);
126 0 $self->eof();
127 0 for (@mail_header_names) {
128 0 ▶ if ($self->as_text =~ /^($_[-\w]*:)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->as_text =~ /^(${_}[-\w]*:)/i) 0 0 0
129 0 $found = $1;
130 0 last;
131 }
132 }
133 0 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 0 0 0
134 0 $val =~ s/&nbsp;//g; #Stupid yahboo.
135 0 ▶ if ($self->{_debug}) { print "sdd 050; Header: ($found)=($val)\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
136 0 $self->parse($val);
137 0 $self->eof();
138 }
139 }
140 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
141 0 ▶ print "sdd 051a; Header: ($found)=(", $self->as_trimmed_text, ")\n" if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
142 }
143 0 ▶ return $found ? $self->as_trimmed_text : undef;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 0 0 0
144 }
145  
146  
147  
148 sub message_read
149 0 ▶{
Subroutine message_read: not called CC=4 SCAR=30.0
Pod documented
150 0 my ($self, $html) = @_;
151 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
152 0 print "sdd 024; ------------------------------------------------\n";
153 0 print $html, "\n";
154 }
155  
156 0 $self->parse($html);
157 0 $self->eof();
158  
159 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
160 0 print "sdd 025; ------------------------------------------------\n";
161 0 print $self->as_HTML(undef, "\t"), "\n";
162 }
163  
164  
165 # Normalise content
166 0 my $page = new HTML::Element('html');
167 0 my $head = $page->push_content(new HTML::Element('head'));
168 0 my $body = new HTML::Element('body');
169 0 $page->push_content($body);
170 0 my $msg = $self->look_down(@{$self->{_message_start}});
171 0 $body->push_content($msg);
172 0 $self->{_message_element} = $page;
173  
174 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
175 0 print "sdd 026a; ------------------------------------------------\n";
176 0 print $page->as_HTML(undef, "\t"), "\n";
177 0 print "sdd 026b; ------------------------------------------------\n";
178 }
179  
180 0 return 1;
181 }
182  
183  
184  
185 0 ▶sub body_as_html { body(@_, 'html') }
Subroutine body_as_html: not called CC=1 SCAR=6.9
Pod documented
186 0 ▶sub body_as_plain { body(@_, 'plain') }
Subroutine body_as_plain: not called CC=1 SCAR=6.9
Pod documented
187 0 ▶sub body_as_text { body(@_, 'plain') }
Subroutine body_as_text: not called CC=1 SCAR=6.9
Pod documented
188 0 ▶sub body_as_appropriate { body(@_) } # Guess html or text based on contents
Subroutine body_as_appropriate: not called CC=1 SCAR=6.9
Pod documented
189  
190  
191 # FIXME: Remove in version 0.3
192 0 ▶sub parse_body_as_html { warn "parse_body_as_xxx is deprecated; please use body_as_xxx instead\n"; body_as_html(@_) }
Subroutine parse_body_as_html: not called CC=1 SCAR=6.9
Pod documented
193 0 ▶sub parse_body_as_text { warn "parse_body_as_xxx is deprecated; please use body_as_xxx instead\n"; body_as_text(@_) }
Subroutine parse_body_as_text: not called CC=1 SCAR=6.9
Pod documented
194 sub parse_body
195 0 ▶{
Subroutine parse_body: not called CC=1 SCAR=6.9
Pod documented
196 0 warn "parse_body is deprecated; please use body instead.\n";
197 0 body(@_);
198 }
199  
200  
201  
202 # removes extra tags from around the message body, drilling down through @args
203 # bread (array of arrayrefs to pass to look_down) until meat is encountered.
204 sub extract_body
205 0 ▶{
Subroutine extract_body: not called CC=9 SCAR=45.0
Pod documented
206 0 my ($self, @args) = @_;
207  
208  
209 0 my $page = $self->{_message_element};
210 0 ▶ unless ($page) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page) 0 0 0
211 0 warn "No message content found - has message been read?\n";
212 0 return undef;
213 }
214 # my $e = $page->look_down(@{shift @args});
215 0 my $e = $page;
216 0 my $save = $e;
217 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 0 0
218 # TODO: Check $e in the below loop, and use page text if look_down fails
219 JULIE:
220 0 for (@args) {
221 0 $e = $e->look_down(@$_);
222 0 ▶ last JULIE if !$e;
Branch: 1 branch
BranchTrueFalseTotal
unless $e 0 0 0
223 0 $save = $e;
224 }
225 0 ▶ if (!$e) { $e = $save } # restore last 'good' element
Branch: 1 branch
BranchTrueFalseTotal
unless ($e) 0 0 0
226 0 ▶ my $text = join '', map { ref($_) ? $_->as_HTML : $_ } $e->content_list;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
227 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
228 0 print "sdd 028a; ------------------------------------------------\n";
229 0 print $text, "\n";
230 0 print "sdd 028b; ------------------------------------------------\n";
231 }
232 0 my $el = new HTML::TreeBuilder();
233 # even though this is set to false, TreeBuilder adds an implicit HTML tag.
234 # Bah. Just have to live with that for now.
235 0 $el->implicit_tags(0);
236 0 $el->parse($text);
237 0 $el->eof();
238 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
239 0 print "sdd 029a; ------------------------------------------------\n";
240 0 print $el->dump, "\n";
241 0 print "sdd 029b; ------------------------------------------------\n";
242 0 print $el->as_HTML(undef, "\t"), "\n";
243 0 print "sdd 029c; ------------------------------------------------\n";
244 }
245 0 $self->{_body_content} = $text;
246 # Ahaha! extract_body might be called serially!
247 0 $self->{_message_element} = $el;
248  
249 0 return 1; # Notify caller something was changed
250 }
251 0 return 0; # No change made to settings
252 }
253  
254  
255  
256 # TODO: allow trimming of yahoo banner/ footer
257 # $style can be 'html', 'plain', or 'both'.
258 # TODO: complicated rules described here
259 # If only one style is provided, use it. If no styles, and no multipart/alt
260 # header, try and guess at the type by looking for html tags in the
261 # content.
262 # TODO: Allow override of Content-Type. Some messages have an html body even
263 # though the message Content-Type is text/plain. It's kept as-is for now but
264 # it might be nice in future to allow the user to specify that the
265 # Content-Type be re-written if HTML is detected.
266 sub body
267 0 ▶{
Subroutine body: not called CC=28 SCAR=67.0
Pod documented
268 0 my ($self, $message, $style) = @_;
269  
270 0 my $msg = $self->{_body_content};
271 0 my $body = $self->{_message_element};
272 # TODO: call read_body ourselves here ? no tag info.. assume "none".
273 0 ▶ unless ($msg && $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg and $body) 0 0 0
Condition0%0/3
$msg and $body
!ll&&!rl&&r
000
MC/DC0%0/2
$msg and $body
$msg $body
Truth table0%0/3
A and B
A= $msgB= $body
ABresult
0X0
100
111
274 0 warn "No message content found - has message been read?\n";
275 0 return undef;
276 }
277  
278 0 my @mesg = (); # will store the message body as we build it.
279  
280 # If no style provided, see if we can figure it out from the MIME type. If
281 # not able to, check the message contents themselves.
282 0 my $boundary = '';
283 0 my $ct = $message->get('Content-Type');
284  
285 0 my $style_src = '';
286  
287 0 ▶ if (!$style) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style) 0 0 0
288 0 ▶ if ($self->{_debug}) { print "sdd 070a; ($ct)\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
289 0 ▶ if ($ct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct) 0 0 0
290 0 ▶ if ($ct =~ m{multipart/alternative;?\s*(?:boundary=)"?([^"]*)"?}i) { #"
Branch: 3 branches
BranchTrueFalseTotal
if ($ct =~ m[multipart/alternative;?\s*(?:boundary=)"?([^"]*)"?]i) { } 0 0 0
elsif ($ct =~ m[text/(\w+)]i) { } 0 0 0
elsif ($ct =~ m[multipart/mixed;?\s*(?:boundary=)"?([^"]*)"?]i) { } 0 0 0
291 0 $style = 'both';
292 0 $boundary = $1;
293 0 $style_src = 'content-type (multipart/alternative)';
294 } elsif ($ct =~ m:text/(\w+):i) {
295 0 $style = lc $1;
296 0 $style_src = "content-type (text/$style)";
297 } elsif ($ct =~ m{multipart/mixed;?\s*(?:boundary=)"?([^"]*)"?}i) { #"
298 0 $style = ''; # Figure out later what content type to use.
299 0 $boundary = $1;
300 }
301 }
302 }
303 0 ▶ if (!$style) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($style) 0 0 0
304 0 $style = $self->might_be_html($message, $msg);
305 # If html was found in the message body, and Content-Type is
306 # empty, we can add a multipart/alternative Content-Type.
307 # TODO: requires 'make_message_boundary'.
308 # if ($style eq 'html') {
309 # $style = 'both';
310 # if (!$ct) {
311 # my $bdry = $self->make_message_boundary($message);
312 # $message->add('Content-Type', "multipart/alternative; boundary=$bdry");
313 # }
314 # }
315  
316 0 $style_src = "guessed ($style)";
317 }
318  
319 0 my $text = '';
320  
321 0 ▶ if ($style eq 'both' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "both" or $style eq "html" and $ct =~ m[multipart/(alternative|mixed)]i) 0 0 0
Condition0%0/6
$style eq "both" or $style eq "html" and $ct =~ m[multipart/(alternative|mixed)]i
l!l&&r!l&&!r
000
$style eq "html" and $ct =~ m[multipart/(alternative|mixed)]i
!ll&&!rl&&r
000
MC/DC0%0/3
$style eq "both" or $style eq "html" and $ct =~ m[multipart/(alternative|mixed)]i
$style eq "both" $style eq "html" $ct =~ m[multipart/(alternative|mixed)]i
Truth table0%0/4
A or B and C
A= $style eq "both"B= $style eq "html"C= $ct =~ m[multipart/(alternative|mixed)]i
ABCresult
1XX1
0111
00X0
0100
322 ($style eq 'html' && $ct =~ m:multipart/(alternative|mixed):i)) {
323 0 ▶ if ($style ne 'both' && $ct !~ /alternative/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style ne "both" and not $ct =~ /alternative/i) 0 0 0
Condition0%0/3
$style ne "both" and not $ct =~ /alternative/i
!ll&&!rl&&r
000
MC/DC0%0/2
$style ne "both" and not $ct =~ /alternative/i
$style ne "both" not $ct =~ /alternative/i
Truth table0%0/3
A and not(B)
A= $style ne "both"B= not $ct =~ /alternative/i
ABresult
0X0
100
111
324 0 $text .= "This is a multi-part message in MIME format.\n",
325 }
326 0 $message->replace('MIME-Version', '1.0');
327  
328 0 $text .= join "\n",
329 "--$boundary",
330 "Content-Type: text/html\n\n"
331 ;
332 }
333  
334 0 ▶ if ($style eq 'html' || $style eq 'both') { $text .= $msg }
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "html" or $style eq "both") 0 0 0
Condition0%0/3
$style eq "html" or $style eq "both"
l!l&&r!l&&!r
000
MC/DC0%0/2
$style eq "html" or $style eq "both"
$style eq "html" $style eq "both"
Truth table0%0/3
A or B
A= $style eq "html"B= $style eq "both"
ABresult
1X1
011
000
335  
336 0 ▶ if ($style eq 'both' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "both" or $style eq "plain" and $ct =~ m[multipart/(alternative|mixed)]i) 0 0 0
Condition0%0/6
$style eq "both" or $style eq "plain" and $ct =~ m[multipart/(alternative|mixed)]i
l!l&&r!l&&!r
000
$style eq "plain" and $ct =~ m[multipart/(alternative|mixed)]i
!ll&&!rl&&r
000
MC/DC0%0/3
$style eq "both" or $style eq "plain" and $ct =~ m[multipart/(alternative|mixed)]i
$style eq "both" $style eq "plain" $ct =~ m[multipart/(alternative|mixed)]i
Truth table0%0/4
A or B and C
A= $style eq "both"B= $style eq "plain"C= $ct =~ m[multipart/(alternative|mixed)]i
ABCresult
1XX1
0111
00X0
0100
337 ($style eq 'plain' && $ct =~ m:multipart/(alternative|mixed):i)) {
338 0 ▶ if ($style ne 'both' && $ct !~ /alternative/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style ne "both" and not $ct =~ /alternative/i) 0 0 0
Condition0%0/3
$style ne "both" and not $ct =~ /alternative/i
!ll&&!rl&&r
000
MC/DC0%0/2
$style ne "both" and not $ct =~ /alternative/i
$style ne "both" not $ct =~ /alternative/i
Truth table0%0/3
A and not(B)
A= $style ne "both"B= not $ct =~ /alternative/i
ABresult
0X0
100
111
339 0 $text .= "This is a multi-part message in MIME format.\n\n",
340 }
341 0 $message->replace('MIME-Version', '1.0');
342 0 $text .= join "\n",
343 "--$boundary",
344 "Content-Type: text/plain\n\n",
345 ;
346 }
347 0 ▶ if ($style eq 'plain' || $style eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "plain" or $style eq "both") 0 0 0
Condition0%0/3
$style eq "plain" or $style eq "both"
l!l&&r!l&&!r
000
MC/DC0%0/2
$style eq "plain" or $style eq "both"
$style eq "plain" $style eq "both"
Truth table0%0/3
A or B
A= $style eq "plain"B= $style eq "both"
ABresult
1X1
011
000
348 # $body->as_text might not be sufficient..
349 0 $text .= $self->html2text($body);
350 }
351  
352 0 $text =~ s/$_/$ms_to_unicode->{$_}/g for (keys %$ms_to_unicode);
353 0 ▶ if ($text =~ /\&#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\&#/) 0 0 0
354 # HTML::Entities_decode_entities does not seem to know about chars above 255,
355 # although Unicode support is claimed in perl > 5.7. So we have to decode
356 # those ourselves using the ms_to_unicode_to_text tables.
357 0 $text =~ s/$_/$unicode_to_text->{$_}/g for (keys %$unicode_to_text);
358 }
359 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
360 0 print "sdd 085; Final '$style' text=($text)\n-=-=-=-=-=-=-=-=-=-=-=-=\n";
361 0 $message->add('X-WMY-Style', $style);
362 0 $message->add('X-WMY-Style-Source', $style_src);
363 }
364  
365 0 return $text;
366 }
367  
368  
369  
370 sub remove_matching
371 0 ▶{
Subroutine remove_matching: not called CC=3 SCAR=24.8
Pod documented
372 0 my ($self, @args) = @_;
373  
374 0 my $body = $self->{_message_element};
375  
376 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
377 0 print "sdd 027a; ------------------------------------------------\n";
378 0 print $body->as_HTML(undef, "\t"), "\n";
379 0 print "sdd 027b; ------------------------------------------------\n";
380 0 print $body->dump, "\n";
381 0 print "sdd 027c; ------------------------------------------------\n";
382 }
383  
384 0 my @s = $body->look_down(@args);
385 0 for (@s) { $_->delete() }
386 }
387  
388  
389  
390  
391 sub might_be_html
392 0 ▶{
Subroutine might_be_html: not called CC=6 SCAR=37.4
Pod documented
393 0 my ($self, $message, $text) = @_;
394 # Here all we care about is the message body itself. For Yahoo, this is
395 # defined inside a '<div id="message"></div>' block.. but Yahoo adds extra
396 # HTML inside this div (as of 2003/08/12, a table is added). What we'd like to
397 # do is remove this extraneous HTML and focus only on the text actually
398 # delivered by the sender.. which may or may not include HTML tags itself.
399 # To achieve this, we rely on the caller to have filtered out any stuff not
400 # interesting to MessageParser.
401 # TODO: Move this comment block to where it came from (where?)
402  
403 # If the message text contains a <tag> with a matching </tag>, consider it
404 # HTML. This is not very robust, or complete, but should be adequate for our
405 # purposes.
406  
407 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
408 0 print "sdd 086a; text=($text)\n-=-=-=-=-=-=-=-=-=-=-=-=\n";
409 }
410
411 0 ▶ if ($text =~ /<([a-z]+)>/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /<([a-z]+)>/is) 0 0 0
412 0 ▶ if ($self->{_debug}) { print "sdd 086b; match=($1)\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
413 0 ▶ if ($text =~ m:</($1)>:is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ m[</($1)>]is) 0 0 0
414 0 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 0 0
415 0 print "sdd 090; Set message type to html on the basis of '$1'\n";
416 0 $message->add('X-Body-Conversion-Trigger', "<$1>");
417 }
418 0 $message->add('X-Body-Converted-To', 'html');
419 0 return 'html';
420 }
421 }
422 0 return 'plain';
423 }
424  
425  
426 sub html2text
427 0 ▶{
Subroutine html2text: not called CC=4 SCAR=30.0
Pod documented
428 0 my ($self, $html) = @_;
429  
430 0 ▶ if (ref($self) !~ /Mail::Webmail::MessageParser/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($self) =~ /Mail::Webmail::MessageParser/) 0 0 0
431 0 $html = $self;
432 }
433  
434 0 ▶ if (ref($html) =~ /HTML::(Tree|Element)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($html) =~ /HTML::(Tree|Element)/) 0 0 0
435 # Very cheap'n'cheerful converter. Simply replaces <br>s with newlines.
436 0 my $h = $html->as_HTML(undef, "\t");
437 0 my $repl = '##!BR!##';
438 0 ▶ while ($h =~ /$repl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h =~ /$repl/) 0 0 0
439 0 $repl = chr(ord('a') + (rand 65)) . $repl . chr(ord('a') + (rand 65));
440 }
441 0 $h =~ s/<br>/$repl/ig;
442 0 my $parser = new HTML::TreeBuilder;
443 0 $parser->parse($h);
444 0 $parser->eof();
445 0 $html = $parser->as_text;
446 0 $html =~ s/$repl/\n/g;
447 0 return $html;
448 }
449  
450 0 return $html;
451 }
452  
453  
454  
455  
456 sub start
457 0 ▶{
Subroutine start: not called CC=3 SCAR=24.8
Pod documented
458 0 my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
459  
460 # Messages are generally embedded inside a nominally valid HTML doc. Sometimes
461 # they will have their own html/ body tags - unfortunately, these cause
462 # HTML::TreeBuilder problems since they are invalid.
463 0 ▶ if ($tagname eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname eq "html") 0 0 0
464 0 $tagname = 'pre';
465 0 $origtext =~ s/html/pre/g;
466 }
467  
468 0 ▶ return if exists $disallow->{$tagname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Mail::Webmail::MessageParser::disallow->{$tagname} 0 0 0
469 0 $self->SUPER::start($tagname, $attr, $attrseq, $origtext);
470 }
471  
472  
473 sub end
474 0 ▶{
Subroutine end: not called CC=3 SCAR=24.8
Pod documented
475 0 my ($self, $tagname) = @_;
476 0 ▶ if ($tagname eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname eq "html") 0 0 0
477 0 $tagname = 'pre';
478 }
479 0 ▶ return if exists $disallow->{$tagname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Mail::Webmail::MessageParser::disallow->{$tagname} 0 0 0
480 0 shift;
481 0 $self->SUPER::end(@_);
482 }
483  
484  
485 1 1;
486  
487  
488 __END__
↑ Summary yahootils.pl »