From 563206c94db52f7e0e6d899fef2681c9e63f8fee Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 11 Jan 2023 11:00:49 +0000 Subject: hoist MailDiff and ContentDigestDbg out of lei These will be reused in the web UI, too. --- lib/PublicInbox/MailDiff.pm | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/PublicInbox/MailDiff.pm (limited to 'lib/PublicInbox/MailDiff.pm') diff --git a/lib/PublicInbox/MailDiff.pm b/lib/PublicInbox/MailDiff.pm new file mode 100644 index 00000000..06eb3a0d --- /dev/null +++ b/lib/PublicInbox/MailDiff.pm @@ -0,0 +1,50 @@ +# Copyright (C) all contributors +# License: AGPL-3.0+ +package PublicInbox::MailDiff; +use v5.12; +use File::Temp 0.19 (); # 0.19 for ->newdir +use PublicInbox::ContentHash qw(content_digest); +use PublicInbox::ContentDigestDbg; +use Data::Dumper (); +use PublicInbox::MsgIter qw(msg_part_text); + +sub write_part { # Eml->each_part callback + my ($ary, $self) = @_; + my ($part, $depth, $idx) = @$ary; + if ($idx ne '1' || $self->{-raw_hdr}) { + open my $fh, '>', "$self->{curdir}/$idx.hdr" or die "open: $!"; + print $fh ${$part->{hdr}} or die "print $!"; + close $fh or die "close $!"; + } + my $ct = $part->content_type || 'text/plain'; + my ($s, $err) = msg_part_text($part, $ct); + my $sfx = defined($s) ? 'txt' : 'bin'; + open my $fh, '>', "$self->{curdir}/$idx.$sfx" or die "open: $!"; + print $fh ($s // $part->body) or die "print $!"; + close $fh or die "close $!"; +} + +# public +sub dump_eml ($$$) { + my ($self, $dir, $eml) = @_; + local $self->{curdir} = $dir; + mkdir $dir or die "mkdir($dir): $!"; + $eml->each_part(\&write_part, $self); + + open my $fh, '>', "$dir/content_digest" or die "open: $!"; + my $dig = PublicInbox::ContentDigestDbg->new($fh); + local $Data::Dumper::Useqq = 1; + local $Data::Dumper::Terse = 1; + content_digest($eml, $dig); + print $fh "\n", $dig->hexdigest, "\n" or die "print $!"; + close $fh or die "close: $!"; +} + +# public +sub prep_a ($$) { + my ($self, $eml) = @_; + $self->{tmp} = File::Temp->newdir('mail-diff-XXXX', TMPDIR => 1); + dump_eml($self, "$self->{tmp}/a", $eml); +} + +1; -- cgit v1.2.3-24-ge0c7