#!/usr/bin/perl ## this program based largely on perl code from ## http://www.bitwaste.com/wasted-bits/archives/000093.html#000093 ## by Raffi Krikorian ## (i believe it's gpl-ed, but didn't see that in the code i copied- ## and-pasted) ## ## modified somewhat for my own purposes by ryan@ryan.net ### this is installed on my unix box that gets my mail ### where i also use procmail for filtering and lots of other stuff ### the procmail rule looks like this: ### ### :0fw ### * ^To:.*mailblog@mydomain.net ### | /home/yourname/bin/blog ### ### .... so inbound mail to that address (changed, so you guys ### can't post to my blog) gets filtered through this program, and ### posted. ### ### this filter could just as easily be a procmail rule so that ### mail to your normal address, but with a subject like "blog:mysubject" ### would trigger the script... read procmail docs for that. ### you need all these perl modules installed on both sides, for ### the most part. i know for a fact things won't work without ### xmlrpc installed on each side. ### for easy instructions on how to do this locally, as opposed to ### system-wide, see ### http://www.movabletype.org/docs/mtinstall.html ### and look for "If you need to install SOAP::Lite" use Mail::Header; # use this to parse out the e-mail header use Mail::Internet; # and to parse out the entire mail message use XMLRPC::Lite; # use this to call the blogger API use strict; ################################################################################ ### what comes here is the location of your blog's ### implementation of the xml-rpc "catcher" program. ### for MT, it's "mt-xmlrpc.cgi". my $blogURL = "http://www.mydomain.net/cgi-bin/mt-xmlrpc.cgi"; ## (note: to debug this, i changed the url to something like ## "http://www.ryan.net:12345/cgi-bin/mt-xmlrpc.cgi" ## and then opened a shell on my unix box, and intercepted the ## call with ## nc -l -p 12345 ## ...using netcat, so i could see what it was really posting. ## other debugging note: ## its handy to take an email message and just pipe it to this. ## use the whole headers and everything, though. ## what comes here is the blog ID number you wanna post to. ## using movabletype, the easiest way to determine this number ## is to log into your MT setup, and from the main menu, hover over ## the links for "your existing blogs"... ## the status bar of your browser will show the url, which looks like ## "http://www.psychicfriends.net/mt/mt.cgi?__mode=menu&blog_id=8" ## ...obviously, my blog id is "8" my $blogID = 8; ## here, enter a username and password that have permission ## to post to this blog. my $blogUsername = "LexLuthor"; # the username of the person to post with my $blogPassword = "xxxxxx"; # the password to the blog # what goes before and after the post my $prepost = ""; my $postpost = ""; ## the addresses that are allowed to post to this blog ## #_#_#_#_# NOTE: i don't care about this right now ## so the code farther down that normally would ## exit upon detecting a non-kosher address is ## commented out. my @authAddress = ( "ryan\@ryan.net" ); ## $convert_breaks can be set to "0" or "1" ## if it's set to "1", all linebreaks will be converted to
tags ## this is good for cellphoneblogs and stuff, but can get annoying ## otherwise. ## ## the convert_breaks stuff didn't actually work in movabletype ## until their recent upgrade to version 2.51 my $convert_breaks = 1; ################################################################################ # parse the mail and pull out the header my $mail = new Mail::Internet \*STDIN; my $header = $mail->head(); # confirm that the message is from somebody that is allowed to post to # the blog my $from = $header->get( "From", 0 ); $from =~ s/^(?:\"?.*?\"?\s*\<)?(\S+\@[^\s\>]+)\>?.*?$/$1/; chop $from; my %is_authAddress; for( @authAddress ) { $is_authAddress{$_} = 1; } ### don't care about authaddress... ### so this next line is commented out. ###exit 0 if( !$is_authAddress{$from} ); # pull out the content of the message my $subject = $header->get( "Subject", 0 ); my $content = $mail->body(); $content = join " \n", @$content; $content = $prepost . $content . $postpost; # post the blog my $xml = new XMLRPC::Lite; my $status = $xml->proxy( $blogURL )->call( "metaWeblog.newPost", $blogID, $blogUsername, $blogPassword, { "title" => $subject, "description" => $content, "mt_convert_breaks" => $convert_breaks }, 1 ); # compose a response e-mail to say that we posted the stuff my $mail_header_response = new Mail::Header; if( $status ) { $mail_header_response->add( "Subject", "blog entry posted" ); print "blog entry apparently succeeded\n"; } else { $mail_header_response->add( "Subject", "blog entry failed" ); print "blog entry failed\n"; } my $mail_response = new Mail::Internet( "Header" => $mail_header_response ); $mail_response->smtpsend( "To", $from );