From 4f7977b42aab1595b3a45b41e19499b38c46c8cc Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 18 May 2016 20:30:31 +0000 Subject: msg_iter: new internal API for iterating through MIME Unlike Email::MIME::walk_parts, this is non-recursive and gives depth + index offset information about the part for creating links for later retrieval It is intended for read-only access and changes are not propagated to the parent; however future versions of it may clobber bodies or the original version as it iterates to reduce memory overhead. It is intended for making it easy to locate attachments within a message in the WWW view. --- lib/PublicInbox/MsgIter.pm | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/PublicInbox/MsgIter.pm (limited to 'lib/PublicInbox') diff --git a/lib/PublicInbox/MsgIter.pm b/lib/PublicInbox/MsgIter.pm new file mode 100644 index 00000000..d0dd82fb --- /dev/null +++ b/lib/PublicInbox/MsgIter.pm @@ -0,0 +1,36 @@ +# Copyright (C) 2016 all contributors +# License: AGPL-3.0+ +# +package PublicInbox::MsgIter; +use strict; +use warnings; +use base qw(Exporter); +our @EXPORT = qw(msg_iter); + +# Like Email::MIME::walk_parts, but this is: +# * non-recursive +# * passes depth and indices to the iterator callback +sub msg_iter ($$) { + my ($mime, $cb) = @_; + my @parts = $mime->subparts; + if (@parts) { + my $i = 0; + @parts = map { [ $_, 1, ++$i ] } @parts; + while (my $p = shift @parts) { + my ($part, $depth, @idx) = @$p; + my @sub = $part->subparts; + if (@sub) { + $depth++; + $i = 0; + @sub = map { [ $_, $depth, @idx, ++$i ] } @sub; + @parts = (@sub, @parts); + } else { + $cb->($p); + } + } + } else { + $cb->([$mime, 0, 0]); + } +} + +1; -- cgit v1.2.3-24-ge0c7