#!/usr/bin/perl
#
# curvr
# Richard Crowley <r@rcrowley.org>
#
# This works with my Nokia N82. I don't care if it works on your phone.
# Tread lightly.
use strict;
# Flickr API goodness
my $FLICKR_API_KEY = '';
my $FLICKR_SECRET = '';
my $FLICKR_TOKEN = '';
# Fire Bagel!
my $FIREEAGLE_CONSUMER_KEY = '';
my $FIREEAGLE_CONSUMER_SECRET = '';
my $FIREEAGLE_ACCESS_TOKEN = '';
my $FIREEAGLE_ACCESS_SECRET = '';
my $CURVR_BIN = 'curvr';
my $CURVR_PROCESS = 'curve';
my $CURVR_VERSION = '0.2.1';
use MIME::Base64;
use Flickr::Upload::FireEagle;
# Tags to add to every photo
my $TAGS = "curvr curvr:process=$CURVR_PROCESS curvr:version=$CURVR_VERSION";
# Get the raw email body from procmail
my @lines = <STDIN>;
my $boundary = '';
my $content_type = '';
my $blank = 0;
my @body;
my @jpeg;
foreach (@lines) {
chomp;
# First we have to find the boundary
if (!$boundary && '--' eq substr($_, 0, 2)) {
$boundary = $_;
}
# Seeing another boundary indicates the end of a part
elsif ($boundary eq $_) {
$content_type = '';
$blank = 0;
}
# Note the content type if this line specifies it
elsif (/^Content-Type: ([a-z\/]+)/) {
$content_type = $1;
}
# Now find the blank line that indicates the beginning of content
elsif (!$blank && /^\s*$/) {
$blank = 1;
}
# The body
elsif ($blank && 'text/plain' eq $content_type) {
push @body, $_;
}
# The JPEG attachment
elsif ($blank && 'image/jpeg' eq $content_type) {
push @jpeg, $_;
}
}
my $body = join '', @body;
# Curvr ur JPEG
my $ts = time();
my $in = "/tmp/curvrmail_$ts.jpg";
my $out = "/tmp/curvrmail_${ts}_curve.jpg";
unlink $in;
open JPEG, '>', $in;
print JPEG decode_base64(join('', @jpeg));
close JPEG;
`$CURVR_BIN $in $CURVR_PROCESS $out`;
# Sneaky rotate syntax
# Why doesn't my phone have an accelerometer in it?
my $tags = '';
if ($body =~ /^([lr])\s+/i) {
my $rotate = lc($1);
my $degrees = '0';
if ('l' eq $rotate) {
$degrees = '-90';
$tags = 'curvr:rotate=left';
} elsif ('r' eq $rotate) {
$degrees = '90';
$tags = 'curvr:rotate=right';
}
`gm convert $out -rotate $degrees $out`;
$body =~ s/^[lr]\s+//i;
}
# Pick apart title and tags
my $title = '';
if ($body =~ /^(.*)tags:(.*)$/i) {
$title = $1;
$tags = "$2 $tags";
} else {
$title = $body;
}
$title =~ s/^\s+//;
$title =~ s/\s+$//;
$tags =~ s/^\s+//;
$tags =~ s/\s+$//;
$tags .= " $TAGS";
# Upload with Fire Bagel
my %fireeagle = (
'consumer_key' => $FIREEAGLE_CONSUMER_KEY,
'consumer_secret' => $FIREEAGLE_CONSUMER_SECRET,
'access_token' => $FIREEAGLE_ACCESS_TOKEN,
'access_token_secret' => $FIREEAGLE_ACCESS_SECRET,
'tagify' => 'delicious'
);
my %flickr = (
'key' => $FLICKR_API_KEY,
'secret' => $FLICKR_SECRET,
'fireeagle' => \%fireeagle
);
my $uploadr = Flickr::Upload::FireEagle->new(\%flickr);
my $photo_id = $uploadr->upload(
'photo' => $out,
'auth_token' => $FLICKR_TOKEN,
'title' => $title,
'tags' => $tags
);