Discussion:
[Mimedefang] Part’s parent content-type
Amit Gupta
2017-11-04 05:37:46 UTC
Permalink
When iterating through the parts of a MIME::Entity using parts_DFS, what would be the best way to get a reference to a part’s parent entity or parent entity type? I’m trying to do some logic on an HTML part depending on if the parent type is multipart/related or not. I was thinking of just setting a variable as I iterate through the parts, but it can get tricky if I don’t know the depth level I’m at.
_______________________________________________
NOTE: If there is a disclaimer or other legal boilerplate in the above
message, it is NULL AND VOID. You may ignore it.

Visit http://www.mimedefang.org and http://www.roaringpenguin.com
MIMEDefang mailing list ***@lists.roaringpenguin.com
http://lists.roaringpenguin.com
Dianne Skoll
2017-11-04 13:37:23 UTC
Permalink
On Fri, 3 Nov 2017 22:37:46 -0700
Post by Amit Gupta
When iterating through the parts of a MIME::Entity using parts_DFS,
what would be the best way to get a reference to a part's parent
entity or parent entity type?
Pass it in when you recurse.

sub process {
my ($entity, $parent_entity) = @_;

if ($entity->is_multipart()) {
foreach my $p (@{$entity->parts()}) {
process ($p, $entity);
}
return;
}

# Process non-multiparts here
}

Call it with: process($toplevel, undef)

If you need the entire chain of entities all the way to the top,
use an array:

sub process
{
my ($entity, $parents) = @_;
$parents ||= [];
if ($entity->is_multipart) {
my @parents_copy = @$parents;
push(@parents_copy, $entity);
foreach my $p (@{$entity->parts()}) {
process($p, \@parents_copy);
}
return;
}
# Process non-multiparts here
}

Regards,

Dianne.
_______________________________________________
NOTE: If there is a disclaimer or other legal boilerplate in the above
message, it is NULL AND VOID. You may ignore it.

Visit http://www.mimedefang.org and http://www.roaringpenguin.com
MIMEDefang mailing list ***@lists.roaringpenguin.com
http://l
Amit Gupta
2017-11-05 01:25:20 UTC
Permalink
Thank you Dianne! This is going to sound silly, but what I'm looking
for is a function like

$parent_part = get_parent ($top_entity, $part);

So given the top level entity and a specific part, the function will
return the MIME::Entity for the parent of $part. I'm struggling with
getting the recursion on this. Any tips on a simple way to do this?
_______________________________________________
NOTE: If there is a disclaimer or other legal boilerplate in the above
message, it is NULL AND VOID. You may ignore it.

Visit http://www.mimedefang.org and http://www.roaringpenguin.com
MIMEDefang mailing list ***@lists.roaringpenguin.com
http://lists.roaringpenguin.com/mailma
Dianne Skoll
2017-11-05 02:29:36 UTC
Permalink
Post by Amit Gupta
Thank you Dianne! This is going to sound silly, but what I'm looking
for is a function like
$parent_part = get_parent ($top_entity, $part);
No such function exists. That's why you have to do it the way I illustrated.

You could probably use the framework I posted to write such a function,
but I would rethink what you're trying to do to make it fit more naturally
into the recursive MIME structure. You are thinking non-recursively
when you request such a function.

Regards,

Dianne.

_______________________________________________
NOTE: If there is a disclaimer or other legal boilerplate in the above
message, it is NULL AND VOID. You may ignore it.

Visit http://www.mimedefang.org and http://www.roaringpenguin.com
MIMEDefang mailing list ***@lists.roaringpenguin.com
http://lists.roaringpenguin.com/mailman/listin

Continue reading on narkive:
Loading...