1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::MailDiff;
5 use File::Temp 0.19 (); # 0.19 for ->newdir
6 use PublicInbox::ContentHash qw(content_digest);
7 use PublicInbox::MsgIter qw(msg_part_text);
8 use PublicInbox::ViewDiff qw(flush_diff);
9 use PublicInbox::GitAsyncCat;
11 sub write_part { # Eml->each_part callback
12 my ($ary, $self) = @_;
13 my ($part, $depth, $idx) = @$ary;
14 if ($idx ne '1' || $self->{-raw_hdr}) {
15 open my $fh, '>', "$self->{curdir}/$idx.hdr" or die "open: $!";
16 print $fh ${$part->{hdr}} or die "print $!";
17 close $fh or die "close $!";
19 my $ct = $part->content_type || 'text/plain';
20 my ($s, $err) = msg_part_text($part, $ct);
21 my $sfx = defined($s) ? 'txt' : 'bin';
24 open my $fh, '>:utf8', "$self->{curdir}/$idx.$sfx" or die "open: $!";
25 print $fh $s or die "print $!";
26 close $fh or die "close $!";
31 my ($self, $dir, $eml) = @_;
32 local $self->{curdir} = $dir;
33 mkdir $dir or die "mkdir($dir): $!";
34 $eml->each_part(\&write_part, $self);
36 return if $self->{ctx}; # don't need content_digest noise in WWW UI
37 require PublicInbox::ContentDigestDbg;
39 # XXX is this even useful? perhaps hide it behind a CLI switch
40 open my $fh, '>', "$dir/content_digest" or die "open: $!";
41 my $dig = PublicInbox::ContentDigestDbg->new($fh);
42 content_digest($eml, $dig);
43 print $fh "\n", $dig->hexdigest, "\n" or die "print $!";
44 close $fh or die "close: $!";
49 my ($self, $eml) = @_;
50 $self->{tmp} = File::Temp->newdir('mail-diff-XXXX', TMPDIR => 1);
51 dump_eml($self, "$self->{tmp}/a", $eml);
56 my $ctx = $self->{ctx};
57 my $over = $ctx->{ibx}->over;
58 $self->{smsg} = $over ? $over->next_by_mid(@{$self->{next_arg}})
61 $ctx->write($ctx->_html_end);
64 my $async = $self->{ctx}->{env}->{'pi-httpd.async'};
65 $async->(undef, undef, $self) if $async # PublicInbox::HTTPD::Async->new
69 my ($bref, $self) = @_; # bref is `git diff' output
70 # will be escaped to `•' in HTML
72 $self->{ctx}->{ibx}->{obfuscate} and
73 obfuscate_addrs($self->{ctx}->{ibx}, $$bref, "\x{2022}");
74 print { $self->{ctx}->{zfh} } '</pre><hr><pre>' if $self->{nr} > 1;
75 flush_diff($self->{ctx}, $bref);
80 my ($self, $eml) = @_;
81 my $n = 'N'.(++$self->{nr});
82 my $dir = "$self->{tmp}/$n";
83 $self->dump_eml($dir, $eml);
84 my $cmd = [ qw(git diff --no-index --no-color -- a), $n ];
85 my $opt = { -C => "$self->{tmp}", quiet => 1 };
86 my $qsp = PublicInbox::Qspawn->new($cmd, undef, $opt);
87 $qsp->psgi_qx($self->{ctx}->{env}, undef, \&emit_msg_diff, $self);
91 my ($self, $eml) = @_;
93 if ($self->{tmp}) { # 2nd..last message
95 } else { # first message:
100 warn "W: $self->{smsg}->{blob} missing\n";
105 sub diff_msg_i_async {
106 my ($bref, $oid, $type, $size, $self) = @_;
107 diff_msg_i($self, $bref ? PublicInbox::Eml->new($bref) : undef);
113 my $ctx = $self->{ctx};
114 if ($ctx->{env}->{'pi-httpd.async'}) {
115 ibx_async_cat($ctx->{ibx}, $self->{smsg}->{blob},
116 \&diff_msg_i_async, $self);
118 diff_msg_i($self, $ctx->{ibx}->smsg_eml($self->{smsg}));
123 delete $self->{smsg};
128 sub begin_mail_diff {
130 if (my $async = $self->{ctx}->{env}->{'pi-httpd.async'}) {
131 $async->(undef, undef, $self); # PublicInbox::HTTPD::Async->new
133 event_step($self) while $self->{smsg};