[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Emailing myself pictures to a Maildir; how to extract with script?



On Sat, Sep 11, 2004 at 08:09:48PM -0400, William Ballard wrote:
> On Sat, Sep 11, 2004 at 04:41:34PM -0400, Kevin Mark wrote:
> > cell->server->smtp->procmail->[process here]->maildir->[process
> > there]->PICS
> > do you want to [process here] or [process there]?
> 
> [process there]
> 
> I already know how to write the recipies in .procmailrc which implement 
> [process here].
> 
> Each individual message is a seperate file in MAILDIR/new or MAILDIR/cur 
> which contains base64-encoded .jpg attachment.  I need to extract these 
> attachments and save them to the file system.
> 
> It's probably something super obvious but I don't know the "canonical" 
> tool for doing it.

I'd suggest [process here].

.procmailrc:

:0 w
* ^X-Is: picture$
| /home/will/mutilator

Have mutilator be a script that extracts the image from the message,
sample included.

-- 
The world's most effective spam filter:
        ln -sf /dev/full /var/mail/$USER
#! /usr/bin/perl
use warnings;
use POSIX;
use MIME::Base64 ();

$/ = "";  $header = <STDIN>;
undef $/; $body   = <STDIN>;

sub header2href {
	my $header = shift;
	$header =~ s/\n\s+/ /g;  # fix continuation lines
	return { (UNIX_FROM => split /^(\S*?):\s*/m, $header) };
}

$hdrs = header2href ($header);

($hdrs->{'Content-Type'} =~ m[multipart/mixed;\s*boundary="(.*)"]m) or die "Single part message\n";

$boundary = "\n--$1(--)?\n";

@parts = split $boundary, $body;

@attachments = grep { /^Content-Disposition:[ \t]+attachment/m } @parts;

die "No attachments\n" unless scalar (@attachments);

$attach1 = $attachments[0];

($at_header, $at_body) = split ("\n\n", $attach1, 2);

$at_hdrs = header2href $at_header;

$at_enc = $at_hdrs->{'Content-Transfer-Encoding'};

%encmap = (
	base64	=> \&MIME::Base64::decode,
);

$decoder = $encmap{$at_enc};

die "Unknown encoding\n" unless defined $decoder;

while (1) {
	last if (mkdir "$ENV{HOME}/campictures/.LOCK");
	die "cannot lock: $!\n" unless ($! == POSIX::EEXIST);
	warn "waiting for lock\n";
	sleep 1;
}

$fail = 0;

((open FILE, "> $ENV{HOME}/campictures/${\ time }") &&
 (print FILE $decoder->($at_body)) &&
 (close FILE)) or ((warn "Cannot write file: $!\n"), ($fail = 1));

rmdir "$ENV{HOME}/campictures/.LOCK" or die "cannot unlock: $!\n";

exit $fail;

Reply to: