#!/usr/bin/perl -w
use strict;
use MIME::Fast;
use Image::Magick;
# setup
my $blogbase = '/usr/local/www/data/blosxom/data/';
my $blogpart = 'phone';
# parse the message into a MIME object
my $msg = MIME::Fast::Parser::construct_message (\*STDIN);
# do some 'security' checks
my ($header) = MIME::Fast::Utils::header_decode_text($msg->get_sender());
exit unless ($header =~ m/you\@example.net/i);
$header = MIME::Fast::Utils::header_decode_text($msg->get_message_id());
exit unless ($header =~ m/example.net/i);
# set up some storage for various bits
my (@text, @image);
# parse the message for data
$msg->foreach_part (
sub {
my ($mime, $data) = @_;
my $ct = $mime->get_content_type();
if ($ct->is_type('text', 'plain')) {
push @text, $mime;
} elsif ($ct->is_type('image', '*')) {
push @image, $mime;
} else {
print STDERR $msg->get_message_id() . ": ignored content " . $ct->to_string() . "\n";
}
} , 'none');
# bits we need to set up an entry
my ($msgid, undef) = $msg->get_date();
$msgid = time() if ($msgid > time());
my $subject = MIME::Fast::Utils::header_decode_text($msg->get_subject());
# save the images to files
for (my $i = 0; $i <= $#image; $i++) {
# write the image to file
if (open (IMG, ">$blogbase/$blogpart/$msgid.$i." . lc($image[$i]->get_content_type()->subtype()))) {
print IMG $image[$i]->get_content();
close IMG;
}
# create a thumbnail
my ($imgmgk) = new Image::Magick;
$imgmgk->Read("$blogbase/$blogpart/$msgid.$i." . lc($image[$i]->get_content_type()->subtype()));
$imgmgk->Resize(geometry=>'100x100>');
$imgmgk->Write("$blogbase/$blogpart/$msgid.$i.t." . lc($image[$i]->get_content_type()->subtype()));
}
# make a blog entry.
if (open ENTRY, ">$blogbase/$blogpart/$msgid.txt") {
print ENTRY "$subject\n";
# post any text that was sent
my $txt = join ("\n\n", map { $_->get_content() } @text) . "\n\n";
# handle inline images
while ($txt =~ m/(\&i(\d+)\;)/gi) {
my $it = "
\nget_content_type()->subtype()) . "\">
get_content_type()->subtype()) . "\" alt=\"" . $image[$2]->get_filename() . "\">
\n";
delete($image[$2]); # make a note that it has been handled
$txt =~ s/$1/$it/;
}
print ENTRY $txt;
# now post any images (if we haven't handled them inline);
for (my $i = 0; $i <= $#image; $i++) {
if (defined ($image[$i])) {
print ENTRY "get_content_type()->subtype()) . "\">
get_content_type()->subtype()) . "\" alt=\"" . $image[$i]->get_filename() . "\">\n";
}
}
close (ENTRY);
}
# set the date of the entry
utime ($msgid, $msgid, "$blogbase/$blogpart/$msgid.txt");