#!/usr/bin/perl -w ##################################################################### # mail.xargs - run "perldoc mail.xargs" for details # # 2006 (C) Horms # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA # 02111-1307 USA # ##################################################################### =head1 NAME mail.xargs - xargs for mail folders =head1 SYNOPSIS B I [I]... =head1 DESCRIPTION Reads an mbox mailbox from stdin and executes a command once per message in the mailbox. =head1 OPTIONS None =head1 EXAMPLES mail.xargs sendmail < ~/INBOX =head1 TODO Could be extended to support maildr by passing the path to the folder on the commandline =head1 AUTHOR Horms =head1 COPYRIGHT Copyright (C) 2006 Horms . This is free software. You may redistribute copies of it under the terms of the GNU General Public License (http://www.gnu.org/licenses/gpl.html). There is NO WARRANTY, to the extent permitted by law. =cut use strict; use POSIX qw(tmpnam); use IO::File; use Symbol; use IPC::Open3; use Mail::Box::Manager; my $tempfile; my $tempfh; my $mbox_mgr; my $folder; my $msg; my $status; my $line; sub usage { print STDERR "mail.xargs CMD [ARG]...\n"; exit 1; } if (scalar @ARGV == 0) { usage(); } for my $i (1 .. 100) { $tempfile = tmpnam(); $tempfh = IO::File->new($tempfile, O_RDWR|O_CREAT|O_EXCL); if ($tempfh) { last; } } if (not $tempfh) { die ("Could not create tempoary file\n"); } while() { print $tempfh $_; } close $tempfh; $mbox_mgr = Mail::Box::Manager->new; $folder = $mbox_mgr->open(folder => $tempfile, access => "r"); for $msg ($folder->messages) { open3(\*IN, \*OUT, \*ERR, @ARGV) or die; $msg->print(\*IN); close IN; $status = 0; while ($status != 0x3) { unless ($status & 0x1) { if ($line = ) { print $line; } else { $status |= 0x1; } } unless ($status & 0x2) { if ($line = ) { print STDERR $line; } else { $status |= 0x2; } } } close OUT; close ERR; } $folder->close(); END { if (defined $tempfile and not unlink($tempfile)) { die "Couldn't unlink $tempfile: $!"; } }