/usr/share/perl5/Mail/Message/Construct/Rebuild.pm is in libmail-box-perl 2.099-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | # Copyrights 2001-2011 by Mark Overmeer.
# For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.00.
use strict;
package Mail::Message;
use vars '$VERSION';
$VERSION = '2.099';
use Mail::Message::Head::Complete;
use Mail::Message::Body::Lines;
use Mail::Message::Body::Multipart;
use Mail::Address;
use Scalar::Util 'blessed';
use List::Util 'first';
use Mail::Box::FastScalar;
my @default_rules =
qw/replaceDeletedParts descendMultiparts descendNested
flattenMultiparts flattenEmptyMultiparts/;
sub rebuild(@)
{ my ($self, %args) = @_;
# Collect the rules to be run
my @rules = $args{rules} ? @{$args{rules}} : @default_rules;
unshift @rules, @{$args{extra_rules}} if $args{extra_rules};
unshift @rules, @{$args{extraRules}} if $args{extraRules}; #old name
foreach my $rule (@rules)
{ next if ref $rule;
unless($self->can($rule))
{ $self->log(ERROR => "No rebuild rule '$rule' defined.\n");
return 1;
}
}
# Start off with the message
my $rebuild = $self->recursiveRebuildPart($self, rules => \@rules)
or return;
# Be sure we end-up with a message
if($rebuild->isa('Mail::Message::Part'))
{ # a bit too much information is lost: we are left without the
# main message headers....
my $clone = Mail::Message->new(head => $self->head->clone);
$clone->body($rebuild->body); # to update the Content lines
$rebuild = $clone;
}
$rebuild->takeMessageId unless $args{keep_message_id};
$rebuild;
}
#------------------------------------------
# The general rules
sub flattenNesting($@)
{ my ($self, $part) = @_;
$part->isNested ? $part->body->nested : $part;
}
sub flattenMultiparts($@)
{ my ($self, $part) = @_;
return $part unless $part->isMultipart;
my @active = $part->parts('ACTIVE');
@active==1 ? $active[0] : $part;
}
sub removeEmptyMultiparts($@)
{ my ($self, $part) = @_;
$part->isMultipart && $part->body->parts==0 ? undef : $part;
}
sub flattenEmptyMultiparts($@)
{ my ($self, $part) = @_;
return $part unless $part->isMultipart && $part->parts('ACTIVE')==0;
my $body = $part->body;
my $preamble = $body->preamble
|| Mail::Message::Body::Lines->new(data => '');
my $epilogue = $body->epilogue;
my $newbody = $preamble->concatenate($preamble, <<NO_PARTS, $epilogue);
* PLEASE NOTE:
* This multipart did not contain any parts (anymore)
* and was therefore flattened.
NO_PARTS
my $rebuild = Mail::Message::Part->new
( head => $part->head->clone
, container => undef
);
$rebuild->body($newbody);
$rebuild;
}
sub removeEmptyBodies($@)
{ my ($self, $part) = @_;
$part->body->lines==0 ? undef : $part;
}
sub descendMultiparts($@)
{ my ($self, $part, %args) = @_;
return $part unless $part->isMultipart;
my $body = $part->body;
my $changed = 0;
my @newparts;
foreach my $part ($body->parts)
{ my $new = $self->recursiveRebuildPart($part, %args);
if(!defined $new) { $changed++ }
elsif($new==$part) { push @newparts, $part }
else { push @newparts, $new; $changed++ }
}
$changed
or return $part;
my $newbody = ref($body)->new
( based_on => $body
, parts => \@newparts
);
my $rebuild = ref($part)->new
( head => $part->head->clone
, container => undef
);
$rebuild->body($newbody); # update Content-* lines
$rebuild;
}
sub descendNested($@)
{ my ($self, $part, %args) = @_;
return $part unless $part->isNested;
my $body = $part->body;
my $srcnested = $body->nested;
my $newnested = $self->recursiveRebuildPart($srcnested, %args);
return undef unless defined $newnested;
return $part if $newnested==$srcnested;
# Changes in the encapsulated message
my $newbody = ref($body)->new
( based_on => $body
, nested => $newnested
);
my $rebuild = ref($part)->new
( head => $part->head->clone
, container => undef
);
$rebuild->body($newbody);
$rebuild;
}
sub removeDeletedParts($@)
{ my ($self, $part) = @_;
$part->isDeleted ? undef : $part;
}
sub replaceDeletedParts($@)
{ my ($self, $part) = @_;
return $part
unless ($part->isNested && $part->body->nested->isDeleted)
|| $part->isDeleted;
my $structure = '';
my $output = Mail::Box::FastScalar->new(\$structure, ' ');
$part->printStructure($output);
Mail::Message::Part->build
( data => "Removed content:\n\n$structure"
);
}
#------------------------------------------
# The more complex rules
sub removeHtmlAlternativeToText($@)
{ my ($self, $part, %args) = @_;
return $part unless $part->body->mimeType eq 'text/html';
my $container = $part->container;
return $part
unless defined $container
&& $container->mimeType eq 'multipart/alternative';
foreach my $subpart ($container->parts)
{ return undef if $subpart->body->mimeType eq 'text/plain';
}
$part;
}
my $has_hft;
sub textAlternativeForHtml($@)
{ my ($self, $part, %args) = @_;
my $hft = 'Mail::Message::Convert::HtmlFormatText';
unless(defined $has_hft)
{ eval "require Mail::Message::Convert::HtmlFormatText";
$has_hft = $hft->can('format');
}
return $part
unless $has_hft && $part->body->mimeType eq 'text/html';
my $container = $part->container;
my $in_alt = defined $container
&& $container->mimeType eq 'multipart/alternative';
return $part
if $in_alt
&& first { $_->body->mimeType eq 'text/plain' } $container->parts;
# Create the plain part
my $html_body = $part->body;
my $plain_body = $hft->new->format($html_body);
my $plain_part = Mail::Message::Part->new(container => undef);
$plain_part->body($plain_body);
return $container->attach($plain_part)
if $in_alt;
# Recreate the html part to loose some header lines
my $html_part = Mail::Message::Part->new(container => undef);
$html_part->body($html_body);
# Create the new part, with the headers of the html part
my $mp = Mail::Message::Body::Multipart->new
( mime_type => 'multipart/alternative'
, parts => [ $plain_part, $html_part ]
);
my $newpart = ref($part)->new
( head => $part->head->clone # Subject field, and such
, container => undef
);
$newpart->body($mp);
$newpart;
}
#------------------------------------------
sub recursiveRebuildPart($@)
{ my ($self, $part, %args) = @_;
RULES:
foreach my $rule (@{$args{rules}})
{ my $rebuild
= ref $rule ? $rule->($self, $part, %args)
: $self->$rule($part, %args);
defined $rebuild
or return undef;
if($part != $rebuild)
{ $part = $rebuild;
redo RULES;
}
}
$part;
}
#------------------------------------------
1;
|