#!/usr/local/bin/perl # $Id$ # # Yoshiki Kurihara # # use strict; use MIME::Parser; use Image::Magick; use File::Copy; use Date::Calc qw(Today Now); use Net::FTP; use Image::Info qw(image_info); use constant DEBUG => 0; use constant SERVER => q(ftp.yourserver.com); use constant USERNAME => q(yourusername); use constant PASSWORD => q(yourpassword); use constant REMOTEDIR => q(/path/to/upload/directory); use constant OWNER => [ 'your@example.com', 'foo@example.com' ]; my ($mail_from, $mail_date, $mail_subject, $mail_body); my $filename; my $filename_ = '/tmp/feelh_photo.png'; my $buf; { local $/; $buf = <>; } my $parser = new MIME::Parser; $parser->output_to_core(1); $parser->tmp_recycling(1); $parser->tmp_to_core(1); $parser->use_inner_files(1); print "parsing mail data...\n" if DEBUG; my $entity = $parser->parse_data($buf) or die "Can't parse data.\n"; print "dump entities...\n" if DEBUG; dump_entity($entity); if (DEBUG) { print "Filename: $filename\n"; print "From: $mail_from"; print "Date: $mail_date"; print "Subject: $mail_subject"; print $mail_body; print "\n"; } if (check_email($mail_from) and !DEBUG) { copy($filename,$filename_); my $ftp = Net::FTP->new(SERVER); $ftp->login(USERNAME, PASSWORD); print "ftp: login successful.\n" if DEBUG; $ftp->cwd(REMOTEDIR); print "ftp: change directory successful.\n" if DEBUG; $ftp->type('I'); print "ftp: change type successful.\n" if DEBUG; $ftp->put($filename); $ftp->put($filename_); print "ftp: put successful.\n" if DEBUG; $ftp->quit; print "ftp: quit.\n" if DEBUG; unlink($filename); unlink($filename_); } else { print "you are not owner.\n" if DEBUG; } sub dump_entity { my ($entity) = @_; $mail_from .= $entity->head->get('from'); $mail_date .= $entity->head->get('date'); $mail_subject .= $entity->head->get('subject'); my @parts = $entity->parts; if (@parts) { foreach my $i (0 .. $#parts) { print "dump entities...\n" if DEBUG; dump_entity($parts[$i]); } } else { print "save image...\n" if DEBUG; my ($type, $subtype) = split '/', $entity->head->mime_type; my $body = $entity->bodyhandle; if ($type =~ /^(text|message)$/) { $mail_body .= $body->as_string; } else { my $realname = $entity->head->recommended_filename; my $ext; my $image_data = $body->as_string; my $orig_image = image_info(\$image_data); my $ext = $orig_image->{file_ext}; print "$ext\n" if DEBUG; $filename = sprintf '/tmp/%04d%02d%02d%02d%02d%02d.png', Today(), Now(); my $image = Image::Magick->new(magick => $ext); $image->BlobToImage($body->as_string); my ($resized, $rotated) = (0, 0); if ($mail_body ne '') { my @orders = split /\n/, $mail_body; foreach my $order (@orders) { print $order, "\n" if DEBUG; chomp($order); my ($command, $param) = split /\s/, $order; print "$command $param\n" if DEBUG; # allow command (rotate and resize) if ($command eq 'rotate') { next if $param % 90 != 0; print "Rotate $param\n" if DEBUG; $image->Rotate(degrees => $param); $rotated = 1; } elsif ($command eq 'resize') { next if $param >= 100; my $width = $orig_image->{width}; my $height = $orig_image->{height}; if ($rotated == 1) { my $tmp = $width; $width = $height; $height = $tmp; } my $width = int($width * ($param / 100)); my $height = int($height * ($param / 100)); print "Resize $width $height\n" if DEBUG; $image->Resize(width => $width, height => $height); $resized = 1; } } } # if no resized and original width > 120 if ($resized == 0 and $orig_image->{width} >= 120) { my $width = $orig_image->{width}; my $height = $orig_image->{height}; if ($rotated == 1) { my $tmp = $width; $width = $height; $height = $tmp; } $width = int($width * 0.7); $height = int($height * 0.7); $image->Resize(width => $width, height => $height); } # write file $image->Write($filename); chmod 0644, $filename; } } } # check allow email sub check_email { my $email = shift; chomp $email; my $owner = OWNER; my $MATCH = 0; foreach my $check (@$owner) { $MATCH = 1 if $check eq $email; } return $MATCH; }