#!/usr/bin/perl # Ben Milleare's MT-Moblog Script # http://ben.milleare.com/archives/000133.html # ------ # Based on original code by Yoz # http://cheerleader.yoz.com/archives/000771.html # ------ # All I've done really is hack around some bits and add POP3 support # rather than use STDIN so credit to Yoz. # # (minor update on 01/10/2003) # # Version 0.3b # Updated by Eirik Solheim Sept. 2004 # http://www.solstue.com/HTPCtips/archives/2004/12/how_to_post_by.html # # Fixed thumbnail functionallity by Trond Isaksen Nov. 2004 # Major update to support norwegian characters and links # to media files by Kenneth Raknes Dec. 2004 # # A new update by Kenneth Raknes to fix some bugs: # 1. The problem that some permalinks got a file name with a space in it # 2. The problem that some posts done by MMS linked to the SMIL-file and not the picture # 3. Problems with norwegian characters in the name of the sender # 4. Fixed line spacing in the HTML-code my($MT_DIR,$MIME_DIR,$BLOG_ID,$AUTHOR_ID,$CAT_ID,$WIDTH); BEGIN { ############################# # Settings: # # Directory that MT lives in $MT_DIR = '/home/yoursite/public_html/mt'; # subdir of your blog dir, to make message dirs in $MIME_DIR= 'images'; # you need to make this dir and leave it (mostly) alone, as # the images will be there. # For example: # Suppose your blog lives in /home/ted/public_html/blog/ # You make /home/ted/public_html/blog/mime/ # then all the image files end up in dirs called # /home/ted/public_html/mime/$DATETIME/ # where $DATETIME is a numeric string like 200302111206, # corresponding to the date and time the message was received # You can work these values out from the MT interface URLs $BLOG_ID = 10; $AUTHOR_ID = 1; $CAT_ID = 13; # you don't need this if you don't want to categorise # width of the produced thumbnail - perhaps I'll do height one day too $WIDTH = 120; # ############################# } #use strict; use lib "$MT_DIR/lib"; use MIME::Parser; use MIME::Base64 qw(decode_base64); use MIME::QuotedPrint; use HTML::Entities; use MT; use MT::Entry; use MT::Blog; use MT::Image; use MT::Placement; use Net::POP3; use Image::Magick; umask 0022; # call me superstitious # initialise MT stuff my $MT = MT->new( Config => "$MT_DIR/mt.cfg" ) or die "Make: ".MT->errstr; my $BLOG = MT::Blog->load($BLOG_ID); my $WWW_DIR = trailslash($BLOG->site_path()); my $WWW_URL = trailslash($BLOG->site_url()); ##### start pop3 stuff ##### # make the connection my $conn = Net::POP3->new( "mail.yoursite.com" ) or die "[err] There was a problem connecting to the server.\n"; # and login $conn->login( "yourPOPlogin", "yourPOPpassword" ) or die "[err] There was a problem logging in.\n"; # get some stats about the pop box my ($msg_total, $mbox_size) = $conn->popstat(); if ($msg_total > 0) { # get the messages my $msgnum = 1; $msg = $conn->get($msgnum); # grab mail $conn->delete($msgnum); # delete mail ###### end pop info ###### # get date string my @l = localtime(time); my $year = $l[5] + 1900; my $month = $l[4] + 1; my $ds = sprintf("%4d%02d%02d%02d%02d",$year,$month,@l[3,2,1]); my $dir = $WWW_DIR."/".$MIME_DIR."/".$ds; mkdir($dir,0755); # parse and save my $parser = new MIME::Parser; $parser->output_dir($dir); my $entity = $parser->parse_data($msg); # sort the data out my ($text,$imagepath); work_entity($entity); # get header data from mail my $head = $entity->head; my $subject = $head->get('Subject'); my $from = $head->get('From'); # decode name of sender my $nameofsender = get_sender($from); $nameofsender = decode_header($nameofsender); # CR 3: Remove special characters in sender field $nameofsender = html_encode($nameofsender); # final removal of any mail address @ $nameofsender =~ s/@/(- at -)/g; # clean up the subject $subject = decode_header($subject); $subject = html_encode($subject); # CR 1 Remove any leading/trailing spaces and control chars $subject = trimwhitespace($subject); # Now fix body text $text = html_encode($text); my $directlink=0; # make a thumbnail my $imgext; if ($imagepath){ $imagepath =~ s/\.\w+$//; # chop the extension $imgext= $&; # the extension we chopped #image if (($imgext =~ /^\.jpg/i) || ($imgext =~ /^\.jpeg/i) || ($imgext =~ /^\.gif/i) || ($imgext =~ /^\.png/i) || ($imgext =~ /^\.bmp/i)) { my($image, $x); $image = Image::Magick->new; $x = $image -> read($imagepath.$imgext); warn "$x" if "$x"; my $maxx=240; my $maxy=360; my ($ox,$oy) = $image->Get('width','height'); #warn "Testing"; if ($ox>$maxx){ #warn "Resizing"; my $r = $ox / $maxx; #warn "Ratio $r, width $ox, maxx $maxx"; $x=$image->Resize(width=>$ox/$r,height=>$oy/$r); warn "$x" if "$x"; my ($nx,$ny)=$image->Get('width','height'); #warn "New size: $nx , $ny"; } #$x = $image -> Resize(width => $WIDTH); #warn "$x" if "$x"; #$imagepath =~ s/\.\w+$//; # chop the extension #my $imgext = $&; # the extension we chopped $x = $image->write($imagepath."_t".$imgext); #warn "$x" if "$x"; } else { $directlink = 1; } } # do some chmodding # yes, I know Perl has a function for it, but this is far quicker `chmod -R +rx $dir`; # now make the MT entry my $entry = MT::Entry->new; $entry->blog_id($BLOG->id); $entry->author_id($AUTHOR_ID); $entry->status(MT::Entry::RELEASE()); $entry->category($CAT_ID); $entry->allow_comments($BLOG->allow_comments_default); $entry->convert_breaks($BLOG->convert_paras); $imagepath =~ s/^.*\///; # remove everything from the start up to # and including the last slash my $mainhtml; # CR4. Adjust line heights with p tags if ($imagepath){ if($directlink==1){ $mainhtml="
$text
Media"; }else{ $mainhtml = "$text | ";
$mainhtml.= "";
$mainhtml.= " "; $mainhtml.= "Click here for the original picture |
Sent from: "; $mainhtml.=$nameofsender; $mainhtml.="
"; $entry->text($mainhtml); $entry->text_more(""); # what shall we put in the title? if ($subject) { $entry->title($subject); } else { $subject = "Ny melding fra "; $subject.= $nameofsender; $entry->title($subject); # the date string } #print "Entry title: << $subject >>"; $entry->save or die "Entry saving error: ".$entry->errstr; # Right - now the excerpt needs to use a permalink # which is why we saved it once already # and need to save it again my $link = $entry->permalink(); #print "Permalink: << $link >>" ; my $exthtml = "