From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/6] msg_iter: new internal API for iterating through MIME
Date: Thu, 19 May 2016 21:28:42 +0000 [thread overview]
Message-ID: <20160519212847.4822-1-e@80x24.org> (raw)
In-Reply-To: <20160519212552.4530-1-e@80x24.org>
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 ++++++++++++++++++++++++++++++++++++
t/msg_iter.t | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+)
create mode 100644 lib/PublicInbox/MsgIter.pm
create mode 100644 t/msg_iter.t
diff --git a/lib/PublicInbox/MsgIter.pm b/lib/PublicInbox/MsgIter.pm
new file mode 100644
index 0000000..d0dd82f
--- /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;
diff --git a/t/msg_iter.t b/t/msg_iter.t
new file mode 100644
index 0000000..cc58b93
--- /dev/null
+++ b/t/msg_iter.t
@@ -0,0 +1,40 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use Email::MIME;
+use_ok('PublicInbox::MsgIter');
+
+{
+ my $parts = [ Email::MIME->create(body => 'a'),
+ Email::MIME->create(body => 'b') ];
+ my $mime = Email::MIME->create(parts => $parts,
+ header_str => [ From => 'root@localhost' ]);
+ my @parts;
+ msg_iter($mime, sub {
+ my ($part, $level, @ex) = @{$_[0]};
+ push @parts, [ $part->body_str, $level, @ex ];
+ });
+ is_deeply(\@parts, [ [ qw(a 1 1) ], [ qw(b 1 2) ] ], 'order is fine');
+}
+
+{
+ my $parts = [ Email::MIME->create(body => 'a'),
+ Email::MIME->create(body => 'b') ];
+ $parts = [ Email::MIME->create(parts => $parts,
+ header_str => [ From => 'sub@localhost' ]),
+ Email::MIME->create(body => 'sig') ];
+ my $mime = Email::MIME->create(parts => $parts,
+ header_str => [ From => 'root@localhost' ]);
+ my @parts;
+ msg_iter($mime, sub {
+ my ($part, $level, @ex) = @{$_[0]};
+ push @parts, [ $part->body_str, $level, @ex ];
+ });
+ is_deeply(\@parts, [ [ qw(a 2 1 1)], [qw(b 2 1 2)], [qw(sig 1 2)] ],
+ 'nested part shows up properly');
+}
+
+done_testing();
+1;
--
EW
next prev parent reply other threads:[~2016-05-19 21:28 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-19 21:25 [PATCH 1/7] view: rely on Email::MIME::body_str for decoding Eric Wong
2016-05-19 21:28 ` Eric Wong [this message]
2016-05-19 21:28 ` [PATCH 2/6] switch read-only uses of walk_parts to msg_iter Eric Wong
2016-05-19 21:28 ` [PATCH 3/6] www: support downloading attachments Eric Wong
2016-05-19 21:28 ` [PATCH 4/6] msg_iter: workaround broken Email::MIME versions Eric Wong
2016-05-19 21:28 ` [PATCH 5/6] www: validate and check filenames in URLs Eric Wong
2016-05-19 21:28 ` [PATCH 6/6] view: reduce clutter for attachments w/o description Eric Wong
2016-05-19 22:06 ` [PATCH 8/7] www: tighten up allowable filenames for attachments Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160519212847.4822-1-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).