diff options
Diffstat (limited to 'lib/PublicInbox')
-rw-r--r-- | lib/PublicInbox/MsgIter.pm | 36 |
1 files changed, 36 insertions, 0 deletions
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 <meta@public-inbox.org> +# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> +# +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; |