File Coverage

blib/lib/MIME/BodyMunger.pm
Criterion Covered Total %
statement 33 47 70.2
branch 8 24 33.3
condition 2 4 50.0
subroutine 7 9 77.7
pod 2 2 100.0
total 52 86 60.4


line stmt bran cond sub pod time code
1 2     2   10 use strict;
  2         3  
  2         64  
2 2     2   11 use warnings;
  2         4  
  2         93  
3             package MIME::BodyMunger;
4             {
5             $MIME::BodyMunger::VERSION = '0.006';
6             }
7             # ABSTRACT: rewrite the content of text parts, minding charset
8              
9 2     2   17 use Carp ();
  2         4  
  2         29  
10 2     2   14 use Encode;
  2         3  
  2         146  
11 2     2   1895 use Variable::Magic ();
  2         2881  
  2         1196  
12              
13              
14             sub rewrite_content {
15 0     0 1 0 my ($self, $entity, $code) = @_;
16              
17 0 0       0 Carp::confess "rewrite_content called on non-text part"
18             unless $entity->effective_type =~ qr{\Atext/(?:html|plain)(?:$|;)}i;
19              
20 0   0     0 my $charset = $entity->head->mime_attr('content-type.charset')
21             || 'ISO-8859-1';
22              
23 0 0       0 $charset = 'MacRoman' if lc $charset eq 'macintosh';
24              
25 0 0       0 Carp::carp(qq{rewriting message in unknown charset "$charset"})
26             unless my $known_charset = Encode::find_encoding($charset);
27              
28 0         0 my $changed = 0;
29 0     0   0 my $got_set = Variable::Magic::wizard(set => sub { $changed = 1 });
  0         0  
30              
31 0 0       0 my $body = $known_charset
32             ? Encode::decode($charset, $entity->bodyhandle->as_string)
33             : $entity->bodyhandle->as_string;
34              
35 0         0 Variable::Magic::cast($body, $got_set);
36 0         0 $code->(\$body, $entity);
37              
38 0 0       0 if ($changed) {
39 0         0 my $io = $entity->open('w');
40 0 0       0 $io->print($known_charset ? Encode::encode($charset, $body) : $body);
41             }
42             }
43              
44              
45             sub rewrite_lines {
46 8     8 1 17 my ($self, $entity, $code) = @_;
47              
48 8 50       29 Carp::confess "rewrite_lines called on non-text part"
49             unless $entity->effective_type =~ qr{\Atext/(?:html|plain)(?:$|;)}i;
50              
51 8   100     1225 my $charset = $entity->head->mime_attr('content-type.charset')
52             || 'ISO-8859-1';
53              
54 8 100       1095 $charset = 'MacRoman' if lc $charset eq 'macintosh';
55              
56 8 50       35 Carp::carp(qq{rewriting message in unknown charset "$charset"})
57             unless my $known_charset = Encode::find_encoding($charset);
58              
59 8         4636 my $changed = 0;
60 8     26   62 my $got_set = Variable::Magic::wizard(set => sub { $changed = 1 });
  26         201  
61              
62 8         279 my @lines = $entity->bodyhandle->as_lines;
63              
64 8         2678 for my $line (@lines) {
65 13 50       59 local $_ = $known_charset ? Encode::decode($charset, $line) : $line;
66 13         491 Variable::Magic::cast($_, $got_set);
67 13         55 $code->(\$_, $entity);
68 13         152 Variable::Magic::dispell($_, $got_set);
69 13         40 $line = $_;
70             };
71              
72 8 100       28 if ($changed) {
73 7         31 my $io = $entity->open('w');
74 7 50       1374 $io->print($known_charset ? Encode::encode($charset, $_) : $_) for @lines;
75             }
76             }
77              
78              
79             1;
80              
81             __END__