| 1 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | package MIME::BodyMunger 0.007; | ||||||||||||
| 4 | # ABSTRACT: rewrite the content of text parts, minding charset | ||||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Encode; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Variable::Magic (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | #pod =head1 SYNOPSIS | ||||||||||||
| 11 | #pod | ||||||||||||
| 12 | #pod MIME::BodyMunger->rewrite_content( | ||||||||||||
| 13 | #pod $mime_entity, | ||||||||||||
| 14 | #pod sub { | ||||||||||||
| 15 | #pod my ($body_ref) = @_; | ||||||||||||
| 16 | #pod $$body_ref =~ s/zig/zag/; | ||||||||||||
| 17 | #pod }, | ||||||||||||
| 18 | #pod ); | ||||||||||||
| 19 | #pod | ||||||||||||
| 20 | #pod =head1 DESCRIPTION | ||||||||||||
| 21 | #pod | ||||||||||||
| 22 | #pod MIME::BodyMunger provides methods for rewriting text parts. These methods | ||||||||||||
| 23 | #pod take care of character sets for you so that you can treat everything like text | ||||||||||||
| 24 | #pod instead of worrying about content transfer encoding or character set encoding. | ||||||||||||
| 25 | #pod | ||||||||||||
| 26 | #pod At present, only MIME::Entity messages can be handled. Other types will be | ||||||||||||
| 27 | #pod added in the future. | ||||||||||||
| 28 | #pod | ||||||||||||
| 29 | #pod =method rewrite_content | ||||||||||||
| 30 | #pod | ||||||||||||
| 31 | #pod MIME::BodyMunger->rewrite_content($message, sub { ... }); | ||||||||||||
| 32 | #pod | ||||||||||||
| 33 | #pod This method uses the given callback to rewrite the content (body) of the | ||||||||||||
| 34 | #pod message. It decodes the content (using Content-Transfer-Encoding and the | ||||||||||||
| 35 | #pod Content-Type charset (or ISO-8859-1, if none is given)) and provides a | ||||||||||||
| 36 | #pod reference to the character string to the coderef. If the content is altered, | ||||||||||||
| 37 | #pod the body will be re-encoded into the original charset and the message will be | ||||||||||||
| 38 | #pod updated. | ||||||||||||
| 39 | #pod | ||||||||||||
| 40 | #pod The callback is invoked like this: | ||||||||||||
| 41 | #pod | ||||||||||||
| 42 | #pod $code->(\$content, $message); | ||||||||||||
| 43 | #pod | ||||||||||||
| 44 | #pod In the future, there should be an option to re-encode to an alternate charset. | ||||||||||||
| 45 | #pod | ||||||||||||
| 46 | #pod =cut | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub rewrite_content { | ||||||||||
|
Subroutine
rewrite_content: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||
| 49 | 0 | my ($self, $entity, $code) = @_; | |||||||||||
| 50 | |||||||||||||
| 51 | 0 | ▶ | Carp::confess "rewrite_content called on non-text part" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | unless $entity->effective_type =~ qr{\Atext/(?:html|plain)(?:$|;)}i; | ||||||||||||
| 53 | |||||||||||||
| 54 | 0 | ▶ | my $charset = $entity->head->mime_attr('content-type.charset') | ||||||||||
|
Condition0%0/2
$entity->head->mime_attr('content-type.charset') || 'ISO-8859-1'
MC/DC0%0/1
$entity->head->mime_attr('content-type.charset') || 'ISO-8859-1'
$entity->head->mime_attr('content-type.charset')
Truth table0%0/2
A
A= $entity->head->mime_attr('content-type.charset')
| |||||||||||||
| 55 | || 'ISO-8859-1'; | ||||||||||||
| 56 | |||||||||||||
| 57 | 0 | ▶ | $charset = 'MacRoman' if lc $charset eq 'macintosh'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | |||||||||||||
| 59 | 0 | ▶ | Carp::carp(qq{rewriting message in unknown charset "$charset"}) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | unless my $known_charset = Encode::find_encoding($charset); | ||||||||||||
| 61 | |||||||||||||
| 62 | 0 | my $changed = 0; | |||||||||||
| 63 | 0 | ▶ | my $got_set = Variable::Magic::wizard(set => sub { $changed = 1 }); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 64 | |||||||||||||
| 65 | 0 | ▶ | my $body = $known_charset | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | ? Encode::decode($charset, $entity->bodyhandle->as_string) | ||||||||||||
| 67 | : $entity->bodyhandle->as_string; | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | Variable::Magic::cast($body, $got_set); | |||||||||||
| 70 | 0 | $code->(\$body, $entity); | |||||||||||
| 71 | |||||||||||||
| 72 | 0 | ▶ | if ($changed) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 0 | my $io = $entity->open('w'); | |||||||||||
| 74 | 0 | ▶ | $io->print($known_charset ? Encode::encode($charset, $body) : $body); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | } | ||||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | #pod =method rewrite_lines | ||||||||||||
| 79 | #pod | ||||||||||||
| 80 | #pod MIME::BodyMunger->rewrite_lines($message, sub { ... }); | ||||||||||||
| 81 | #pod | ||||||||||||
| 82 | #pod This method behaves like C<rewrite_content>, but the callback is invoked once | ||||||||||||
| 83 | #pod per line, like this: | ||||||||||||
| 84 | #pod | ||||||||||||
| 85 | #pod local $_ = $line; | ||||||||||||
| 86 | #pod $code->($message); | ||||||||||||
| 87 | #pod | ||||||||||||
| 88 | #pod If any line is changed, the entire body will be reencoded and updated. | ||||||||||||
| 89 | #pod | ||||||||||||
| 90 | #pod =cut | ||||||||||||
| 91 | |||||||||||||
| 92 | 8 | ▶ | sub rewrite_lines { | ||||||||||
|
Subroutine
rewrite_lines: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||
| 93 | 8 | my ($self, $entity, $code) = @_; | |||||||||||
| 94 | |||||||||||||
| 95 | 8 | ▶ | Carp::confess "rewrite_lines called on non-text part" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | unless $entity->effective_type =~ qr{\Atext/(?:html|plain)(?:$|;)}i; | ||||||||||||
| 97 | |||||||||||||
| 98 | 8 | ▶ | my $charset = $entity->head->mime_attr('content-type.charset') | ||||||||||
|
Condition100%2/2
$entity->head->mime_attr('content-type.charset') || 'ISO-8859-1'
MC/DC100%1/1
$entity->head->mime_attr('content-type.charset') || 'ISO-8859-1'
$entity->head->mime_attr('content-type.charset')
Truth table100%2/2
A
A= $entity->head->mime_attr('content-type.charset')
| |||||||||||||
| 99 | || 'ISO-8859-1'; | ||||||||||||
| 100 | |||||||||||||
| 101 | 8 | ▶ | $charset = 'MacRoman' if lc $charset eq 'macintosh'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | |||||||||||||
| 103 | 8 | ▶ | Carp::carp(qq{rewriting message in unknown charset "$charset"}) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | unless my $known_charset = Encode::find_encoding($charset); | ||||||||||||
| 105 | |||||||||||||
| 106 | 8 | my $changed = 0; | |||||||||||
| 107 | 8 | ▶ | my $got_set = Variable::Magic::wizard(set => sub { $changed = 1 }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 108 | |||||||||||||
| 109 | 8 | my @lines = $entity->bodyhandle->as_lines; | |||||||||||
| 110 | |||||||||||||
| 111 | 8 | for my $line (@lines) { | |||||||||||
| 112 | 13 | ▶ | local $_ = $known_charset ? Encode::decode($charset, $line) : $line; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 13 | Variable::Magic::cast($_, $got_set); | |||||||||||
| 114 | 13 | $code->(\$_, $entity); | |||||||||||
| 115 | 13 | Variable::Magic::dispell($_, $got_set); | |||||||||||
| 116 | 13 | $line = $_; | |||||||||||
| 117 | }; | ||||||||||||
| 118 | |||||||||||||
| 119 | 8 | ▶ | if ($changed) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 7 | my $io = $entity->open('w'); | |||||||||||
| 121 | 7 | ▶ | $io->print($known_charset ? Encode::encode($charset, $_) : $_) for @lines; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | } | ||||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | #pod =head1 THANKS | ||||||||||||
| 126 | #pod | ||||||||||||
| 127 | #pod Thanks to Pobox.com and Listbox.com, who sponsored the development of this | ||||||||||||
| 128 | #pod module. | ||||||||||||
| 129 | #pod | ||||||||||||
| 130 | #pod Thanks to Brian Cassidy for writing some tests for the initial release. | ||||||||||||
| 131 | #pod | ||||||||||||
| 132 | #pod =cut | ||||||||||||
| 133 | |||||||||||||
| 134 | 2 | 1; | |||||||||||
| 135 | |||||||||||||
| 136 | __END__ | ||||||||||||