| 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::Visitor 0.007; | ||||||||||
| 4 | # ABSTRACT: walk through MIME parts and do stuff (like rewrite) | ||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use MIME::BodyMunger; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | #pod =head1 SYNOPSIS | ||||||||||
| 10 | #pod | ||||||||||
| 11 | #pod # This will reverse all lines in each text part, taking care of all encoding | ||||||||||
| 12 | #pod # for you. | ||||||||||
| 13 | #pod | ||||||||||
| 14 | #pod MIME::Visitor->rewrite_all_lines( | ||||||||||
| 15 | #pod $mime_entity, | ||||||||||
| 16 | #pod sub { chomp; $_ = reverse . "\n"; }, | ||||||||||
| 17 | #pod ); | ||||||||||
| 18 | #pod | ||||||||||
| 19 | #pod =head1 DESCRIPTION | ||||||||||
| 20 | #pod | ||||||||||
| 21 | #pod MIME::Visitor provides a simple way to walk through the parts of a MIME | ||||||||||
| 22 | #pod message, taking action on each one. In general, this is not a very complicated | ||||||||||
| 23 | #pod thing to do, but having it in one place is convenient. | ||||||||||
| 24 | #pod | ||||||||||
| 25 | #pod The most powerful feature of MIME::Visitor, though, is its methods for | ||||||||||
| 26 | #pod rewriting text parts. These methods take care of character sets for you so | ||||||||||
| 27 | #pod that you can treat everything like text instead of worrying about content | ||||||||||
| 28 | #pod transfer encoding or character set encoding. | ||||||||||
| 29 | #pod | ||||||||||
| 30 | #pod At present, only MIME::Entity messages can be handled. Other types will be | ||||||||||
| 31 | #pod added in the future. | ||||||||||
| 32 | #pod | ||||||||||
| 33 | #pod =method walk_parts | ||||||||||
| 34 | #pod | ||||||||||
| 35 | #pod MIME::Visitor->walk_parts($root, sub { ... }); | ||||||||||
| 36 | #pod | ||||||||||
| 37 | #pod This method calls the given code on every part of the given root message, | ||||||||||
| 38 | #pod including the root itself. | ||||||||||
| 39 | #pod | ||||||||||
| 40 | #pod =cut | ||||||||||
| 41 | |||||||||||
| 42 | 14 | ▶ | sub walk_parts { | ||||||||
|
Subroutine
walk_parts: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 43 | 14 | my ($self, $root, $code) = @_; | |||||||||
| 44 | |||||||||||
| 45 | 14 | $code->($root); | |||||||||
| 46 | 14 | for my $part ($root->parts) { | |||||||||
| 47 | 8 | $self->walk_parts($part, $code); | |||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | #pod =method walk_leaves | ||||||||||
| 52 | #pod | ||||||||||
| 53 | #pod MIME::Visitor->walk_leaves($root, sub { ... }); | ||||||||||
| 54 | #pod | ||||||||||
| 55 | #pod This method calls the given code on every leaf part of the given root message. | ||||||||||
| 56 | #pod It descends into multipart parts of the message without calling the callback on | ||||||||||
| 57 | #pod them. | ||||||||||
| 58 | #pod | ||||||||||
| 59 | #pod =cut | ||||||||||
| 60 | |||||||||||
| 61 | 6 | ▶ | sub walk_leaves { | ||||||||
|
Subroutine
walk_leaves: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 62 | 6 | my ($self, $root, $code) = @_; | |||||||||
| 63 | |||||||||||
| 64 | $self->walk_parts( | ||||||||||
| 65 | $root, | ||||||||||
| 66 | 14 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 67 | 14 | ▶ | return if $_[0]->is_multipart; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 10 | $code->($_[0]); | |||||||||
| 69 | }, | ||||||||||
| 70 | 6 | ); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | #pod =method walk_text_leaves | ||||||||||
| 74 | #pod | ||||||||||
| 75 | #pod MIME::Visitor->walk_text_leaves($root, sub { ... }); | ||||||||||
| 76 | #pod | ||||||||||
| 77 | #pod This method behaves like C<walk_leaves>, but only calls the callback on parts | ||||||||||
| 78 | #pod with a content type of text/plain or text/html. | ||||||||||
| 79 | #pod | ||||||||||
| 80 | #pod =cut | ||||||||||
| 81 | |||||||||||
| 82 | 6 | ▶ | sub walk_text_leaves { | ||||||||
|
Subroutine
walk_text_leaves: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 83 | 6 | my ($self, $root, $code) = @_; | |||||||||
| 84 | 10 | ▶ | $self->walk_leaves($root, sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 85 | 10 | ▶ | return unless $_[0]->effective_type =~ qr{\Atext/(?:html|plain)(?:$|;)}i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 8 | $code->($_[0]); | |||||||||
| 87 | 6 | }); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | #pod =method rewrite_parts | ||||||||||
| 91 | #pod | ||||||||||
| 92 | #pod MIME::Visitor->rewrite_parts($root, sub { ... }); | ||||||||||
| 93 | #pod | ||||||||||
| 94 | #pod This method walks the text leaves of the MIME message, rewriting the content of | ||||||||||
| 95 | #pod the parts. For each text leaf, the callback is invoked like this: | ||||||||||
| 96 | #pod | ||||||||||
| 97 | #pod $code->(\$content, $part); | ||||||||||
| 98 | #pod | ||||||||||
| 99 | #pod For more information, see L<MIME::BodyMunger/rewrite_content>. | ||||||||||
| 100 | #pod | ||||||||||
| 101 | #pod =cut | ||||||||||
| 102 | |||||||||||
| 103 | 1 | ▶ | sub rewrite_parts { | ||||||||
|
Subroutine
rewrite_parts: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||
| 104 | 1 | my ($self, $root, $code) = @_; | |||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | $self->walk_text_leaves($root, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 107 | 0 | my ($part) = @_; | |||||||||
| 108 | 0 | MIME::BodyMunger->rewrite_content($part, $code); | |||||||||
| 109 | 1 | }); | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | #pod =method rewrite_all_lines | ||||||||||
| 113 | #pod | ||||||||||
| 114 | #pod MIME::Visitor->rewrite_all_lines($root, sub { ... }); | ||||||||||
| 115 | #pod | ||||||||||
| 116 | #pod This method behaves like C<rewrite_parts>, but the callback is called for each | ||||||||||
| 117 | #pod line of each relevant part, rather than for the part's body as a whole. | ||||||||||
| 118 | #pod | ||||||||||
| 119 | #pod =cut | ||||||||||
| 120 | |||||||||||
| 121 | 5 | ▶ | sub rewrite_all_lines { | ||||||||
|
Subroutine
rewrite_all_lines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 122 | 5 | my ($self, $root, $code) = @_; | |||||||||
| 123 | |||||||||||
| 124 | 8 | ▶ | $self->walk_text_leaves($root, sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 8 | my ($part) = @_; | |||||||||
| 126 | 8 | MIME::BodyMunger->rewrite_lines($part, $code); | |||||||||
| 127 | 5 | }); | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | #pod =head1 THANKS | ||||||||||
| 131 | #pod | ||||||||||
| 132 | #pod Thanks to Pobox.com and Listbox.com, who sponsored the development of this | ||||||||||
| 133 | #pod module. | ||||||||||
| 134 | #pod | ||||||||||
| 135 | #pod =cut | ||||||||||
| 136 | |||||||||||
| 137 | 2 | 1; | |||||||||
| 138 | |||||||||||
| 139 | __END__ | ||||||||||