]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MsgIter.pm
5be06a44be16dc96d064aa7b3eefbaff8e021f97
[public-inbox.git] / lib / PublicInbox / MsgIter.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 package PublicInbox::MsgIter;
5 use strict;
6 use warnings;
7 use base qw(Exporter);
8 our @EXPORT = qw(msg_iter);
9 use PublicInbox::MIME;
10
11 # Like Email::MIME::walk_parts, but this is:
12 # * non-recursive
13 # * passes depth and indices to the iterator callback
14 sub msg_iter ($$) {
15         my ($mime, $cb) = @_;
16         my @parts = $mime->subparts;
17         if (@parts) {
18                 my $i = 0;
19                 @parts = map { [ $_, 1, ++$i ] } @parts;
20                 while (my $p = shift @parts) {
21                         my ($part, $depth, @idx) = @$p;
22                         my @sub = $part->subparts;
23                         if (@sub) {
24                                 $depth++;
25                                 $i = 0;
26                                 @sub = map { [ $_, $depth, @idx, ++$i ] } @sub;
27                                 @parts = (@sub, @parts);
28                         } else {
29                                 $cb->($p);
30                         }
31                 }
32         } else {
33                 $cb->([$mime, 0, 0]);
34         }
35 }
36
37 1;