#!/usr/bin/perl
use LWP::UserAgent;

#
# picturemail2web
# 0.1
# david@dellanave.com
# http://www.dellanave.com/projects/picturemail2web/

#
# This script is highly unportable, so it more than likely will need tweaking.  It depends on
# ImageMagick convert and an outside gallery program.  I could have bundled or written a gallery
# program, but I figure I will leave that up to the user to choose.
#

#
# I suspected that Sprint was doing something using cookies to make it harder to download the image, 
# or just inherent in their dynamic display of images.  Rather than hacking on it further, I looked to
# see if someone else had already done it.  Pete Carter wrote mail2pix and that is where this code was
# ripped from.  He just uses LWP whereas I was using wget (even with -U) but LWP works and wget doesn't.
# Thanks for the code.  GPL is beautiful.

$convert = "/usr/X11R6/bin/convert"; # Path to ImageMagick convert
$log = "/home/david/mail/pictures.log"; # Log file.  Leave blank for none.

# This is where the images get put after being downloaded, and the "message" being added to the header
$album_directory = "/home/david/public_html/phone_pictures/albums/";

# May be used in the future.  I use Mig mig.sourceforge.net
$gallery_software = "mig";

if($log) {
open(LOG, ">>$log");
} else {}

while(<>) {
		if (/shareImage/i ) {			
			#Get the URL 
			$start = index($_, '<img src="');
			$start = $start+10;
			$end = index($_,'"',$start);
			$picurl=substr($_,$start,$end-$start);
			$picurl=~s/&amp;/&/;			#parse out the http encoding back to norm
			$picurl=~s/_235/_640/;			#change the image max size from 235 to 640 (in URL)
			print LOG "Found URL: $picurl\n";			
			#Request PIC from web
			my $ua = LWP::UserAgent->new;
			$ua ->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1');
			$accept='text/html, text/plain, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*';			
			$response = $ua->get($picurl,'Accept'=>$accept);
   			
   			if ($response->is_success) {
        		print LOG "Successfully retrieved image from web.\n";
       		}
    		else {
        		print LOG "ERROR: ", $response->status_line, "\n";
    		}
			$picdata = $response->content; 
		    } elsif(/Message/) {
			($message) = $_ =~ /<b>Message:<\/b><br\/>(.*)<\/font>/;
		    }
	    }

	if ($picdata) {
		$picname = localtime();
		$picname = $picname . ".jpg";
		$picname =~ s/://g;
		$picname =~ s/ /_/g;
		print LOG "Attempting to write file: $picpath$picname\n";
		open (picfile,">$picname")|| die "cannot open: $!";
		binmode (picfile);
		print picfile $picdata; 
		close (picfile);
		#Only delete Email if pic was retrieved..
		#&deleteMail();
	}
	else {
		print LOG "No Valid Image Found. Skipping Message.\n";
	}
print LOG "Message: $message\n";
$action = "$convert -comment " . "\"$message\" " . "$picname " . "$album_directory" . "$picname";
print LOG "Action: $action\n";
system("$action");
unlink("$picname");
chdir("$album_directory");
if($gallery_software == "mig") {
    system("../mkGallery.pl -a -t -d -e -M suffix -f ../config.php"); # Only used if your gallery program is mig
} else {
}
system("chmod -R 755 *");  # Make sure the pictures are readable for the web server
