#!/usr/bin/perl -w

use strict;
use LWP::Simple;
use MIME::Parser;
use MIME::Lite;

my $from = 'rich@lafferty.ca';
my $to   = 'mysecretaddress@photos.flickr.com';
my $tags = 'phone untagged';

# read the message, keep the HTML part - the text part doesn't
# include the text in the MMS.

my $parser = new MIME::Parser;
$parser->output_under("/tmp");

my $entity = $parser->parse(\*STDIN);

for ($entity->parts)
{
    if ($_->mime_type eq 'multipart/alternative')
    {
        for ($_->parts)
        {
            if ($_->mime_type eq 'text/html')
            {
                process($_->body);
            }
        }
    }
}

$parser->filer->purge();

# follow the trail of URLs that eventually lead to the full-size image
sub process
{
   my $body = shift;
   my $nearby = 0;
   my $message;
   my $thumburl;
   
   # pull out text and thumb URL
   for (@{$body})
   {
       if ($nearby == 0)
       {
           $nearby = 1 if /just PXT'ed you/;
       }
       elsif ($nearby == 1 and m|<p>(.*?)</p>|)
       {
           $message = $1;
       }
       elsif ($nearby == 1 and m|href="(http://pxt.*?share.*?)"|)
       {
           $thumburl = $1;
           last;
       }
   }

   die "Image URL is missing" unless defined $thumburl;

   # get thumbnail page

   my $thumbpage = get($thumburl);
   die "Thumbnail page failed" unless defined $thumbpage;
  
   die "Can't find image page URL in thumbnail page"
     unless $thumbpage =~ m|(/guest/view/message/large.do.*INBOX)|smi;

   # get large image page
   #
   my $imagepageurl =
   "http://pxt.virginmobile.ca${1}&slide=0&pictureCount=1&fromMessage=true";

   my $imagepage = get($imagepageurl);
   die "Image page failed" unless defined $imagepage;

   die "Can't find image URL in image page"
      unless $imagepage =~ m|<img src="(/mmps/RECIPIENT/.*?)"|;

   my $imageurl = "http://pxt.virginmobile.ca${1}";
 
   # LWP produces 500 error
   open(CURL, "/usr/bin/curl -s '$imageurl' |") or die "curl died";
   my $image; 
   { local $/; $image = <CURL>; } 
   close CURL;

   # send message
   my $msg = MIME::Lite->new(
       From    => $from,
       To      => $to,
       Subject => $message,
       Type    => 'TEXT',
       Data    => "tags: $tags"
   );
   $msg->attach(
       Type    => "image/jpeg",
       Data    => $image,
   );
   $msg->send;
}

__END__

### HTML from message: ###

                  <b>6132657876</b> just PXT'ed you</p>
                <p>Testing automatic flickr posting</p>
                <p>
                  <img src="cid:A2.9978903950016615">
                  <a
                    href="http://pxt.virginmobile.ca:80/shareMessage.do?invite=nEIr4nJK2YhNchTz70u8">View Message</a>
                </p>

### From thumbnail page:

function displayViewLargeLink(show,whichImg){ //show only for images
var viewLargeURL = viewLargeURL = '/guest/view/message/large.do?fromAddress=6132
657876&invite=nEIr4nJK2YhNchTz70u8&msgNo=0&folderFullName=INBOX&slide=' + whichI
mg + '&pictureCount=' + pictureCount + '&fromMessage=true';

### From image page:

      <img src="/mmps/RECIPIENT/000_0155a938df2fb98a_1/2.jpg?partExt=.jpg&amp;rand=-3760524631539375607&amp;ext=.jpg&amp;limitsize=600,800&inviteToken=nEIr4nJK2YhNchTz70u8" border="0" alt="" name="img0"><br>
