#!/usr/bin/perl # # silpb # # silfreed's photo blogger # $Header: /var/cvsroot/silpb/silpb,v 1.5 2004/04/03 17:23:00 silfreed Exp $ # use strict; use POSIX qw(strftime); use MIME::Parser; use DBD::mysql; use GD; ## prog variables our $progname = "silpb"; our $version = "0.1"; ## configuration our %db_image = ( "host" => "localhost", "name" => "silpb", "user" => "silpb", "pass" => "silpb", "port" => 3306, "dbh" => "", "table" => "silpb_image", "blogidcol" => "blogid", "whencol" => "whenadded", "widthcol" => "width", "heightcol" => "height", "mimetypecol" => "mimetype", "filenamecol" => "filename", "imagecol" => "image", ); # end %db_image; our %db_blog = ( "host" => "localhost", "name" => "silpb", "user" => "silpb", "pass" => "silpb", "port" => 3306, "dbh" => "", "table" => "silpb_blog", "whencol" => "whenadded", "fromcol" => "fromwho", "usermapcol" => "usermap", "categorycol" => "category", "subjectcol" => "subject", "blogcol" => "blog", ); # end %db_blog; our $tmpdir = "/tmp/$progname"; our %usermap = ( #'regex' => 'literal who' #'.*?@whatsit.com' => 'thats it', ); # end %usermap; ## local variables my $parser; # MIME::Parser my $entity; # MIME::Entity my $headers; # MIME::Head my $body; # MIME::Body my $rotate = ""; # string my $from = ""; # string my $mappedto = ""; # string my $category = "general"; # string my $subject = ""; # string my $blog = ""; # string my @images; # list of hashes # @images = [ # ("filename" => "2004-03-01-123213-0.jpg", # "type" => "image/jpeg", # "data" => "..."), # ]; # end @images; ### MAIN ## make mysql connections $db_image{'dbh'} = DBI->connect("DBI:mysql:database=$db_image{'name'};host=$db_image{'host'};port=$db_image{'port'}", $db_image{'user'}, $db_image{'pass'}, {RaiseError => 0, PrintError => 0}); print $db_image{'dbh'}->{'mysql_error'} && exit(1) if (!defined($db_image{'dhb'})); $db_blog{'dbh'} = DBI->connect("DBI:mysql:database=$db_blog{'name'};host=$db_blog{'host'};port=$db_blog{'port'}", $db_blog{'user'}, $db_blog{'pass'}, {RaiseError => 0, PrintError => 0}); print $db_blog{'dbh'}->{'mysql_error'} && exit(1) if (!defined($db_blog{'dhb'})); # someplace to store our parsed objects mkdir ($tmpdir) if (! -d $tmpdir); ## Create parser, and set some parsing options: $parser = new MIME::Parser; $parser->output_under($tmpdir); $parser->decode_headers(1); ## Parse input: $entity = $parser->parse(\*STDIN) or die "parse failed\n"; ## read in headers and body as MIME::Head and MIME::Body respectively $headers = $entity->head(); $body = $entity->bodyhandle(); ## grab from $from = $headers->get('From', 0); chomp($from); ## grab subject line $subject = $headers->get('Subject', 0); chomp($subject); ## get our rotation, if necessary $rotate = $1 if ($subject =~ s/^rot\((\d+)\)\s+//); ## get our category, if one exists $category = $1 if ($subject =~ s/^(.*?):\s+//); ## figure out who this user maps to foreach my $regex (keys(%usermap)) { $mappedto = $usermap{$regex} if ($from =~ /$regex/); } # end foreach usermap ## construct the blog text my $nowstring = strftime("%Y-%m-%d-%H%M%S", gmtime); for (my $i = 0; $i < $entity->parts(); $i++) { my $att = $entity->parts($i); if ($att->mime_type() =~ /text\/plain/i) { $blog .= $att->bodyhandle()->as_string(); chomp($blog); } # end if text/plain elsif ($att->mime_type() =~ /image\/jpeg/i) { push(@images, { "mimetype" => $att->mime_type(), "filename" => "$nowstring-$i.jpg", "data" => $att->bodyhandle()->as_string() } ); # end push image onto array #$att->bodyhandle()->as_string(); } # end if recognized attachment } # end foreach attachment ## insert the blog $db_blog{'dbh'}->do("INSERT INTO $db_blog{'table'} SET $db_blog{'whencol'} = now(), $db_blog{'fromcol'} = ".$db_blog{'dbh'}->quote($from).", $db_blog{'usermapcol'} = ".$db_blog{'dbh'}->quote($mappedto).", $db_blog{'categorycol'} = ".$db_blog{'dbh'}->quote($category).", $db_blog{'subjectcol'} = ".$db_blog{'dbh'}->quote($subject).", $db_blog{'blogcol'} = ".$db_blog{'dbh'}->quote($blog) ) or die("ERROR: ".$db_blog{'dbh'}->{'mysql_error'}); my $blog_insertid = $db_blog{'dbh'}->{'mysql_insertid'}; ## insert the images foreach my $img (@images) { # make a gd image for us to manipulate my $gdimage = GD::Image->newFromJpegData($img->{'data'}, 1); # rotate image if needed if ($rotate =~ /^(90|180|270)$/) { my $copyRotateDeg = "copyRotate$1"; $gdimage = $gdimage->$copyRotateDeg(); } # end if rotate image # image width/height my ($imgwidth, $imgheight) = $gdimage->getBounds(); my $image_query = "INSERT INTO $db_image{'table'} SET $db_image{'blogidcol'} = '$blog_insertid', $db_image{'whencol'} = now(), $db_image{'widthcol'} = '$imgwidth', $db_image{'heightcol'} = '$imgheight', $db_image{'mimetypecol'} = ".$db_image{'dbh'}->quote($img->{'mimetype'}).", $db_image{'filenamecol'} = ".$db_image{'dbh'}->quote($img->{'filename'}).", $db_image{'imagecol'} = ".$db_image{'dbh'}->quote($gdimage->jpeg(90)); $db_image{'dbh'}->do($image_query) or die ("ERROR: ".$db_image{'dbh'}->{'mysql_error'}); } # end foreach image ## clean up after ourselves $parser->filer->purge;