#!/usr/bin/env perl # # ./undup-mbox, but with a proper mailbox processing library # # $Id: selectmail 809 2011-10-11 14:12:25Z rp $ use strict; use warnings; use Getopt::Std; use Mail::Box::Manager; my %opt; getopts( 'e:o:ux', \%opt ); my $outbox = $opt{'o'}; my $regex = defined( $opt{'e'} ) ? $opt{'e'} : '.'; my $mgr = new Mail::Box::Manager; my $out; if ( defined $outbox ) { $out = $mgr->open( folder => $outbox, access => 'rw', create => 1 ) or die "cannot open/create output mbox '$outbox'\n"; # access should be 'w' except for a bug in Mail::Box::File, see # https://rt.cpan.org/Ticket/Display.html?id=44439 } my %uid; # the IDs of messages found, if -u is specified my %xid; # the IDs of messages to exclude, if -x is specified if ( $opt{'x'} ) { # the first argument is the mailbox to *subtract* from the # other messages if ( !@ARGV ) { die "-x requires the specification of at east one argument\n"; } my $xbox = shift(@ARGV); my $xin = $mgr->open( folder => $xbox, access => 'r', keep_dups => 0 ) or die "cannot open input mbox '$xbox'\n"; foreach my $msg ( $xin->messages ) { ++$xid{ $msg->messageId }; } } foreach my $inbox (@ARGV) { my $in = $mgr->open( folder => $inbox, access => 'r', lock_type => 'NONE', keep_dups => !$opt{'u'} ) or die "cannot open input mbox '$inbox'\n"; foreach my $msg ( $in->messages ) { next if defined $xid{ $msg->messageId }; if ( $opt{'u'} ) { # de-duplicate even between different input folders next if defined $uid{ $msg->messageId }; ++$uid{ $msg->messageId }; } if ( grep { /$regex/ } ( $msg->head, $msg->body ) ) { if ( defined $out ) { $mgr->copyMessage( $out, $msg ); } else { $msg->write; } } } $mgr->close( $in, write => 'NEVER' ); } if ( defined $out ) { $mgr->close( $out, write => 'ALWAYS' ) or die "could not write outbox '$outbox': $!\n"; }