#!/usr/bin/perl -w # Copyright 2006 Jason Simpson (jason@xio.com) # Licensed under the Creative Commons Attribution-ShareAlike 2.5 License # # TODO -- permissions masks are ickybad. my $license = < Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. END_LICENSE use strict; use MIME::Parser; use Getopt::Std; use vars qw/ $opt_i /; getopts('i:'); my $logfile = "$ENV{HOME}/.procmail/lookatme.log"; my $outdir = "$ENV{HOME}/public_html/lookatme"; open LOG, ">>$logfile" or die; printf LOG "old umask=%o\n", umask 0022; printf LOG "new umask=%o\n", umask; printf LOG "request received at %s\n", scalar localtime(time); print LOG "ARGV=<@ARGV>\n"; my $parser = new MIME::Parser; $parser->output_to_core(1); my $entity = $parser->parse(\*STDIN) or die "parse failed\n"; my $head = $entity->head; print LOG $head->as_string; my $subject = $head->get('Subject',0); my ($image_base) = ($opt_i) || $subject =~ /^lookatme ([a-zA-Z][-a-zA-Z0-9_]*)$/; if (!defined $image_base) { print LOG "ERROR: no image_base name found\n"; exit 1; } print LOG "parts:\n"; my @newfiles; foreach my $part ($entity->parts) { my $head = $part->head; my $type = $head->mime_type; my $filename = $head->recommended_filename; if ($type eq "image/jpeg" || $filename =~ /\.jpe?g/i) { if (!$part->bodyhandle) { print LOG "ERROR: unable to get bodyhandle for part\n"; next; } if (-d "$outdir/$image_base" || mkdir "$outdir/$image_base", 0755) { my $image_index = ""; $image_index++ while (-e "$outdir/$image_base/$image_base$image_index.jpg"); my $outfile = "$image_base$image_index.jpg"; my $outfilepath = "$outdir/$image_base/$outfile"; my $tempfile = "/tmp/$$.jpg"; open TEMPFILE, ">$tempfile" or die "can't write to $outfilepath: $!\n"; print TEMPFILE $part->bodyhandle->as_string; close TEMPFILE; system("jpegtran","-optimize","-progressive","-outfile",$outfilepath,$tempfile); if (-e $outfilepath) { print LOG "$type OUTFILE=$outfile $type\n"; my $type2 = `file "$outfilepath"`; chomp $type2; print LOG "$type2\n"; #chmod 0644, $outfilepath; unlink "$tempfile"; push @newfiles, $outfile; } else { print LOG "ERROR: jpegtran $tempfile -> $outfilepath failed: $!\n"; } } else { print LOG "ERROR: couldn't make directory $outdir/$image_base\n"; } } else { print LOG "skipping $type part\n"; } } if (@newfiles && -d "$outdir/$image_base") { chdir "$outdir/$image_base"; my @allfiles = glob "*.jpg"; system("perl","$ENV{HOME}/scripts/htmlthumbnail.pl","-progressive","-update","00index.html","-output","00index.html",@allfiles); } close LOG;