#!/usr/bin/perl # # Create blog entries from POP3 email account # * only accept mail from allowed senders # * put images in an images directory # * with a unique name # * with "now.jpg" name # * delete messages from POP3 account # * if successfully blogged # * if just an image # * still create both unique name and "now.jpg" files # * report subject of messages not blogged # Charlie Reitsma and Chris Marshall January 2003, Denison University # # Enhanced slightly by Tom Shields, May 2004 # - use a mail folder instead of POP to get the mail # - authenticate against the entire email address, not just user portion # - strip .signature from the email # # if perl modules are not in the module search path (@INC) # uncomment the following line and change the path(s) appropriately: use Mail::Box::Manager; use Net::Blogger; use File::Temp qw / :mktemp /; use File::Basename; # initialize variables $imgdir = "..../archives/images/"; # absolute file path to images directory $imgtemplate = "imgXXXXXX"; $imgpath = "/images/"; # web path to images directory $blogsite = "http://www.myblogsite.com/mt/mt-xmlrpc.cgi"; $blogid = "1"; $bloguser = "user"; $blogpass = "password"; #$popsite = "popserver"; #$popuser = "username"; #$poppass = "password"; $mailfolder = "..../Inboxes/Blog"; # list of addresses allowed to post %allowed = ('me@mysite.com',1, 'me@othersite.com',1, 'me@mysite.net',1); $sigstart = "--\n"; $sigstart .= '.*$'; # prepare the blog $blg = Net::Blogger->new(engine=>'movabletype'); $blg->Proxy($blogsite); $blg->BlogId($blogid); $blg->Username($bloguser); $blg->Password($blogpass); # prepare the POP account $mgr = Mail::Box::Manager->new; $folder = $mgr->open(folder=>$mailfolder, #$folder = $mgr->open(type=>'pop3', # username=>$popuser, # password=>$poppass, # server_name=>$popsite, access=>'rw', log=>'ERRORS'); # loop through the POP account and blog the messages foreach $message ($folder->messages) { $sender = $message->sender; # print "Got msg sender ",$sender->address,"\n"; $from = $sender->address; if ($allowed{lc $from}) { # only accept messages from allowed senders $subject = $message->get('Subject') || ''; # $format = $sender->format; # $format =~ s//>/; # $body = join(" ","From:",$format,"\n"); $body = ""; $imgname = ''; &parse($message); if ($body && $imgname) { $base = basename($imgname); $body .= "\n\"${base}\""; } if ($body) { $id = $blg->metaWeblog()->newPost(title=>$subject,description=>$body,publish=>1); if ($id) { $message->delete; # delete the message if successfully blogged } else { # print message that cannot be blogged print "Could not blog message:\n",$subject,"\n"; } } else { #empty message, probably just updated now.jpg $message->delete; # delete the message } } else { # $message->delete; # delete message from sources other than allowed # an alternative would be to keep these and manage manually # print $from,": ",$subject,"\n"; } } #$folder->close(write => 'NEVER'); #close POP without a local store $folder->close(); #close POP without a local store sub parse($) { my $msg = shift; my $type = $msg->get('Content-Type'); if ($type =~ /text/) { $body .= $msg->decoded; # collect text from all parts $body =~ s/$sigstart//s; $body =~ s/s+$//; # eliminate white space at end of text chomp($body); # eliminate line feed at end of text } else { if ($type =~ /image/) { ($fh,$imgname) = mkstemps($imgdir.$imgtemplate,".jpg"); print $fh $msg->decoded; $fh->close; open(F,">".$imgdir."now.jpg"); print F $msg->decoded; close(F); chmod 0644, $imgname, $imgdir."now.jpg"; } } if ($msg->isMultipart) { foreach $part ($msg->parts) { &parse($part); } } }