#!/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 = ""; $mainhtml.= "

$text

"; $mainhtml.= "\""; $mainhtml.= "
"; $mainhtml.= "Click here for the original picture
"; } }else{ $mainhtml=$text; } # Add the extra Sent from at the bottom of message $mainhtml.="

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 = "\"$subject\""; $entry->excerpt(""); $entry->save or die "Entry saving error: ".$entry->errstr; # do the category my $place = MT::Placement->new; $place->entry_id($entry->id); $place->blog_id($entry->blog_id); $place->category_id($CAT_ID) if $CAT_ID; $place->is_primary(1); $place->save or die "Placement saving error: ".$place->errstr; # save the whole thing $BLOG->save or die "Blog saving error: ".$BLOG->errstr; # rebuild it all $MT->rebuild(BlogID => $BLOG_ID) or die "Rebuild error: " . $MT->errstr; # and we're done # this sub chews through the MIME objects my $imagefound; sub work_entity { my $ent = shift; my @parts = $ent->parts; if (@parts) { # multipart... map { work_entity($_) } @parts; } else { # single part... if ($ent->head->mime_type =~ /^text/) { if($ent->head->mime_type !~ /html/) { my $IO = $ent->bodyhandle->open("r"); $text .= $_ while (defined($_ = $IO->getline)); #print "TEXT: $text\n\n"; $IO->close; } } # handles octet-stream for S60 and anything else that # doesn't send proper MIME types # CR 2: Skip SMIL attachments if($ent->head->mime_type !~ /smil/) { if(($ent->head->mime_type =~ /^image/)) { $imagepath = $ent->bodyhandle->path; $imagefound = "true"; } if (($ent->head->mime_type =~ /octet\-stream$/) || ($ent->head->mime_type =~ /^audio/) || ($ent->head->mime_type =~ /^video/) || ($ent->head->mime_type =~ /^application/)) { if(!$imagefound) { $imagepath = $ent->bodyhandle->path; } } } } } # removes the trailing slash from a string if there is one sub trailslash { my $t = shift; $t =~ s/\/$//; return $t; } # removes the trailing slash from beginning and end of a string if there is one sub trimwhitespace($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } sub strip_whitespace { my $t = shift; for ($t) { s/^\s+//; s/\s+$//; } return $t; } sub get_sender { my $originalsender = shift; my $tmpsender = $originalsender; $tmpsender =~ s/<[^>]*>//g; $tmpsender = strip_whitespace($tmpsender); if(!$tmpsender) { $tmpsender = $originalsender; } $tmpsender =~ s/@.+//g; $tmpsender =~ s///g; $tmpsender =~ s/\"//g; return $tmpsender; } sub html_encode { my $htmltext = shift; # Now fix some special characters in main text # The following are fixed: æøå ÆØÅ < = > $htmltext =~ s/&/&/g; $htmltext =~ s/æ/æ/g; $htmltext =~ s/ø/ø/g; $htmltext =~ s/å/å/g; $htmltext =~ s/Æ/Æ/g; $htmltext =~ s/Ø/Ø/g; $htmltext =~ s/Å/Å/g; $htmltext =~ s//>/g; return $htmltext; } sub decode_header { my($text)= @_; $text=~ s/=\?(iso-?8859-.?|us-ascii|utf-8)\?(q|b)\?([^?]*)\?=(\s*(?==\?))?/&decode_header_block(lc $1,$2,$3);/gei; return $text; } sub decode_header_block { my ($input,$enc,$text) = @_; if ($enc =~ /q/i) { $text=~ s/_/ /g; $text= decode_qp($text); } else { $text= decode_base64($text); } return $text; } } # close of if ($msg_total > 0) $conn->quit;