#!/usr/bin/env perl # # getmboxaddr - lists email addreses with the headers they were found in # # $Id$ # see also ./selectmail use strict; use warnings; use Getopt::Std; use Mail::Box::Manager; use Text::CSV; my %opt; getopts( 'hHq:t:v', \%opt ); $opt{h} and HELP_MESSAGE(); sub HELP_MESSAGE { print STDERR < ( $opt{t} // ',' ), eol => $/, quote_space => 0, ( defined $opt{q} ? ( quote_char => $opt{q} ) : () ) } ) or oomph("the CSV writer doesn't work (not installed?)"); my $mgr = new Mail::Box::Manager; my %hdr2addr2ph2c2nr; # maps header to mail address to phrase to comment to nr. of occurrences my %xid; # maps Message IDs to 1 $opt{H} and $csvw->print( *STDOUT, [qw(header address phrase comment)] ); @ARGV = ('-') if !@ARGV; foreach my $mboxfilename (@ARGV) { my $mbox = $mgr->open( folder => $mboxfilename, access => 'r', lock_type => 'NONE', ) or do { warn "cannot open $mboxfilename as mailbox, skipped\n"; next }; my @msgs = $mbox->messages; mmm( scalar(@msgs), "messages in mbox $mboxfilename" ); foreach my $msg (@msgs) { next if $xid{ $msg->messageId }++; # don't process message copy twice add_addresses( 'from', $msg->from() ); add_addresses( 'to', $msg->to() ); add_addresses( 'cc', $msg->cc() ); add_addresses( 'bcc', $msg->bcc() ); $mgr->close( $mbox, write => 'NEVER' ); } } sub add_addresses { my $hdrname = shift(@_); my $addr2ph2c2nr = $hdr2addr2ph2c2nr{$hdrname}; foreach my $addr (@_) { my ( $a, $p, $c ) = ( $addr->address(), $addr->phrase(), $addr->comment() ); $addr2ph2c2nr->{$a}->{$p}->{$c}++ or $csvw->print( *STDOUT, [ $hdrname, $a, $p, $c ] ); } }